Skip to content

Commit

Permalink
fix(postgres): handle enums by delegating to the parent class (#9769)
Browse files Browse the repository at this point in the history
Handle postgres enum types as unknown. Closes #9295.
  • Loading branch information
cpcloud authored Aug 5, 2024
1 parent 175e362 commit 3f01075
Showing 2 changed files with 20 additions and 9 deletions.
17 changes: 16 additions & 1 deletion ibis/backends/postgres/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -384,7 +384,7 @@ def test_infoschema_dtypes(con):


def test_password_with_bracket():
password = f"{IBIS_POSTGRES_PASS}["
password = f"{IBIS_POSTGRES_PASS}[]"
quoted_pass = quote_plus(password)
url = f"postgres://{IBIS_POSTGRES_USER}:{quoted_pass}@{IBIS_POSTGRES_HOST}:{IBIS_POSTGRES_PORT}/{POSTGRES_TEST_DB}"
with pytest.raises(
@@ -417,3 +417,18 @@ def test_create_geospatial_table_with_srid(con):
for column, dtype in zip(column_names, column_types)
}
)


@pytest.fixture(scope="module")
def enum_table(con):
name = gen_name("enum_table")
con.raw_sql("CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy')")
con.raw_sql(f"CREATE TEMP TABLE {name} (mood mood)")
yield name
con.raw_sql(f"DROP TABLE {name}")
con.raw_sql("DROP TYPE mood")


def test_enum_table(con, enum_table):
t = con.table(enum_table)
assert t.mood.type() == dt.unknown
12 changes: 4 additions & 8 deletions ibis/backends/sql/datatypes.py
Original file line number Diff line number Diff line change
@@ -193,12 +193,11 @@ def from_string(cls, text: str, nullable: bool | None = None) -> dt.DataType:

try:
sgtype = sg.parse_one(text, into=sge.DataType, read=cls.dialect)
return cls.to_ibis(sgtype, nullable=nullable)
except sg.errors.ParseError:
# If sqlglot can't parse the type fall back to `dt.unknown`
pass

return dt.unknown
return dt.unknown
else:
return cls.to_ibis(sgtype, nullable=nullable)

@classmethod
def to_string(cls, dtype: dt.DataType) -> str:
@@ -468,11 +467,8 @@ def _from_ibis_Map(cls, dtype: dt.Map) -> sge.DataType:
def from_string(cls, text: str, nullable: bool | None = None) -> dt.DataType:
if text.lower().startswith("vector"):
text = "vector"
if dtype := cls.unknown_type_strings.get(text.lower()):
return dtype

sgtype = sg.parse_one(text, into=sge.DataType, read=cls.dialect)
return cls.to_ibis(sgtype, nullable=nullable)
return super().from_string(text, nullable=nullable)


class RisingWaveType(PostgresType):

0 comments on commit 3f01075

Please sign in to comment.