Skip to content

Commit

Permalink
Fix: build null types
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Jun 16, 2023
1 parent 8e1b6a7 commit cacf8bf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 5 additions & 2 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3373,15 +3373,18 @@ class Type(AutoName):
Type.DATETIME64,
}

META_TYPES = {"UNKNOWN", "NULL"}

@classmethod
def build(
cls, dtype: str | DataType | DataType.Type, dialect: DialectType = None, **kwargs
) -> DataType:
from sqlglot import parse_one

if isinstance(dtype, str):
if dtype.upper() == "UNKNOWN":
data_type_exp: t.Optional[Expression] = DataType(this=DataType.Type.UNKNOWN)
upper = dtype.upper()
if upper in DataType.META_TYPES:
data_type_exp: t.Optional[Expression] = DataType(this=DataType.Type[upper])
else:
data_type_exp = parse_one(dtype, read=dialect, into=DataType)

Expand Down
2 changes: 2 additions & 0 deletions tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,8 @@ def test_data_type_builder(self):
self.assertEqual(exp.DataType.build("NULLABLE").sql(), "NULLABLE")
self.assertEqual(exp.DataType.build("HLLSKETCH", dialect="redshift").sql(), "HLLSKETCH")
self.assertEqual(exp.DataType.build("HSTORE", dialect="postgres").sql(), "HSTORE")
self.assertEqual(exp.DataType.build("NULL").sql(), "NULL")
self.assertEqual(exp.DataType.build("NULL", dialect="bigquery").sql(), "NULL")
self.assertEqual(exp.DataType.build("UNKNOWN").sql(), "UNKNOWN")
self.assertEqual(exp.DataType.build("UNKNOWN", dialect="bigquery").sql(), "UNKNOWN")
self.assertEqual(exp.DataType.build("UNKNOWN", dialect="snowflake").sql(), "UNKNOWN")
Expand Down

0 comments on commit cacf8bf

Please sign in to comment.