tlocke / pg8000

A Pure-Python PostgreSQL Driver
BSD 3-Clause "New" or "Revised" License
515 stars 46 forks source link

ValueError (not enough values to unpack) with array_agg on range #164

Closed nrainer-materialize closed 1 month ago

nrainer-materialize commented 1 month ago

Given the setup

CREATE TABLE t (x INT4RANGE);
INSERT INTO t VALUES ('[3,9)'::INT4RANGE), ('(,9)'::INT4RANGE), ('(,)'::INT4RANGE);

the following query causes a ValueError:

SELECT array_agg(x) FROM t;
  File "./misc/python/venv/lib/python3.11/site-packages/pg8000/converters.py", line 311, in range_in
    le, ue = [None if v == "" else elem_func(v) for v in data[1:-1].split(",")]
    ^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)
def- commented 1 month ago

This actually only happens against Materialize: https://github.com/MaterializeInc/database-issues/issues/8214 Works against Postgres:

>>> c = pg8000.connect(...)
>>> cur = c.cursor()
>>> cur.execute("SELECT array_agg(x) FROM t")
<pg8000.legacy.Cursor object at 0x744c5d69c490>
>>> cur.fetchall()
([[<Range [3,9)>, <Range (,9)>, <Range (,)>]],)
tlocke commented 1 month ago

Yes, I found it worked on PostgreSQL too, here's the code:

import pg8000.native

con = pg8000.native.Connection("postgres", password="cpsnow")
con.run("CREATE TEMPORARY TABLE t (x INT4RANGE);")
con.run(
    "INSERT INTO t VALUES ('[3,9)'::INT4RANGE), ('(,9)'::INT4RANGE), "
    "('(,)'::INT4RANGE);"
)
con.run("SELECT array_agg(x) FROM t;")