Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jdav-dev opened this issue Aug 26, 2024 · 1 comment · Fixed by #705
Labels

Comments

@jdav-dev
Copy link

jdav-dev commented Aug 26, 2024

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: []
}
@greg-rychlewski
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@greg-rychlewski @jdav-dev and others