elixir-ecto/postgrex

Arrays of custom timerange types with more than one element worked in 0.18, do not work in 0.19

jdav-dev opened this issue · 1 comments

Elixir version

Elixir 1.17.2 (compiled with Erlang/OTP 27)

Database and Version

PostgreSQL 16.4

Postgrex Version

0.19.1

Current behavior

A minimal script to reproduce the issue:

Mix.install([
  {:postgrex, "~> 0.19.0"}
])

{:ok, conn} =
  Postgrex.start_link(username: "postgres", password: "postgres", database: "postgres")

Postgrex.query!(conn, "DROP TABLE IF EXISTS availability;", [])
Postgrex.query!(conn, "DROP TYPE IF EXISTS timerange;", [])

Postgrex.query!(
  conn,
  """
  CREATE OR REPLACE FUNCTION time_subtype_diff(x time(0), y time(0)) RETURNS float8 AS
  'SELECT EXTRACT(EPOCH FROM (x - y))' LANGUAGE sql STRICT IMMUTABLE;
  """,
  []
)

Postgrex.query!(
  conn,
  """
  CREATE TYPE timerange AS RANGE (
    subtype = time(0),
    subtype_diff = time_subtype_diff
  );
  """,
  []
)

Postgrex.query!(conn, "CREATE TABLE availability (hours timerange[]);", [])

Postgrex.query!(conn, "INSERT INTO availability (hours) VALUES ($1);", [
  [
    struct!(Postgrex.Range,
      lower: ~T[09:00:00],
      upper: ~T[12:00:00],
      lower_inclusive: true,
      upper_inclusive: true
    ),
    struct!(Postgrex.Range,
      lower: ~T[14:00:00],
      upper: ~T[17:00:00],
      lower_inclusive: true,
      upper_inclusive: true
    )
  ]
])

Postgrex.query!(conn, "SELECT * FROM availability;", [])
|> inspect(pretty: true)
|> IO.puts()

The above script crashes on the select query with the following:

** (CaseClauseError) no case clause matching: {{Postgrex.Extensions.Time, nil}, nil}
    (postgrex 0.19.1) lib/postgrex/type_module.ex:1084: Postgrex.DefaultTypes.decode_list/2
    (postgrex 0.19.1) lib/postgrex/type_module.ex:1084: Postgrex.DefaultTypes."Elixir.Postgrex.Extensions.Range"/5
    (postgrex 0.19.1) lib/postgrex/type_module.ex:1084: Postgrex.DefaultTypes."Elixir.Postgrex.Extensions.Array"/9
    (postgrex 0.19.1) lib/postgrex/protocol.ex:3337: Postgrex.Protocol.rows_recv/4
    (postgrex 0.19.1) lib/postgrex/protocol.ex:2311: Postgrex.Protocol.recv_execute/5
    (postgrex 0.19.1) lib/postgrex/protocol.ex:2148: Postgrex.Protocol.bind_execute_close/4
    (db_connection 2.7.0) lib/db_connection/holder.ex:354: DBConnection.Holder.holder_apply/4
    (db_connection 2.7.0) lib/db_connection.ex:1558: DBConnection.run_execute/5

This crash happens if the array contains more than one time range. The commit that is breaks at is "Allow extensions to access type modifiers". The extension type macros made it difficult for me to follow, but I'll add more info as I trace the issue further.

Expected behavior

With postgrex 0.18, the above script returns the expected result:

%Postgrex.Result{
  command: :select,
  columns: ["hours"],
  rows: [
    [
      [
        %Postgrex.Range{
          lower: ~T[09:00:00.000000],
          upper: ~T[12:00:00.000000],
          lower_inclusive: true,
          upper_inclusive: true
        },
        %Postgrex.Range{
          lower: ~T[14:00:00.000000],
          upper: ~T[17:00:00.000000],
          lower_inclusive: true,
          upper_inclusive: true
        }
      ]
    ]
  ],
  num_rows: 1,
  connection_id: 14056,
  messages: []
}

Thanks for the report @jdav-dev . I believe I know the issue. Would you be able to try out this PR to see if it works for you: #705