Skip to content

Commit

Permalink
Fix!: make ObjectIdentifier, IntervalSpan and PseudoType DataTypes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Sep 25, 2023
1 parent 188fbde commit cdcc564
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
10 changes: 5 additions & 5 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3697,13 +3697,13 @@ def is_type(self, *dtypes: str | DataType | DataType.Type) -> bool:


# https://www.postgresql.org/docs/15/datatype-pseudo.html
class PseudoType(Expression):
pass
class PseudoType(DataType):
arg_types = {"this": True}


# https://www.postgresql.org/docs/15/datatype-oid.html
class ObjectIdentifier(Expression):
pass
class ObjectIdentifier(DataType):
arg_types = {"this": True}


# WHERE x <OP> EXISTS|ALL|ANY|SOME(SELECT ...)
Expand Down Expand Up @@ -4034,7 +4034,7 @@ def unit(self) -> t.Optional[Var]:
# https://www.oracletutorial.com/oracle-basics/oracle-interval/
# https://trino.io/docs/current/language/types.html#interval-day-to-second
# https://docs.databricks.com/en/sql/language-manual/data-types/interval-type.html
class IntervalSpan(Expression):
class IntervalSpan(DataType):
arg_types = {"this": True, "expression": True}


Expand Down
18 changes: 15 additions & 3 deletions tests/dialects/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,27 @@ def test_array_offset(self, logger):
)

def test_postgres(self):
expr = parse_one("SELECT * FROM r CROSS JOIN LATERAL UNNEST(ARRAY[1]) AS s(location)")
expr = parse_one(
"SELECT * FROM r CROSS JOIN LATERAL UNNEST(ARRAY[1]) AS s(location)", read="postgres"
)
unnest = expr.args["joins"][0].this.this
unnest.assert_is(exp.Unnest)

alter_table_only = """ALTER TABLE ONLY "Album" ADD CONSTRAINT "FK_AlbumArtistId" FOREIGN KEY ("ArtistId") REFERENCES "Artist" ("ArtistId") ON DELETE NO ACTION ON UPDATE NO ACTION"""
expr = parse_one(alter_table_only)
expr = parse_one(alter_table_only, read="postgres")

# Checks that user-defined types are parsed into DataType instead of Identifier
parse_one("CREATE TABLE t (a udt)").this.expressions[0].args["kind"].assert_is(exp.DataType)
parse_one("CREATE TABLE t (a udt)", read="postgres").this.expressions[0].args[
"kind"
].assert_is(exp.DataType)

# Checks that OID is parsed into a DataType (ObjectIdentifier)
self.assertIsInstance(
parse_one("CREATE TABLE public.propertydata (propertyvalue oid)", read="postgres").find(
exp.DataType
),
exp.ObjectIdentifier,
)

self.assertIsInstance(expr, exp.AlterTable)
self.assertEqual(expr.sql(dialect="postgres"), alter_table_only)
Expand Down

0 comments on commit cdcc564

Please sign in to comment.