Skip to content

Commit

Permalink
Fix(duckdb): get rid of TEXT length to facilitate transpilation (#3633)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Jun 12, 2024
1 parent fc050bd commit 47472d9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
4 changes: 3 additions & 1 deletion sqlglot/dialects/duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,11 @@ class Parser(parser.Parser):
),
}

TYPE_CONVERTER = {
TYPE_CONVERTERS = {
# https://duckdb.org/docs/sql/data_types/numeric
exp.DataType.Type.DECIMAL: build_default_decimal_type(precision=18, scale=3),
# https://duckdb.org/docs/sql/data_types/text
exp.DataType.Type.TEXT: lambda dtype: exp.DataType.build("TEXT"),
}

def _parse_table_sample(self, as_modifier: bool = False) -> t.Optional[exp.TableSample]:
Expand Down
2 changes: 1 addition & 1 deletion sqlglot/dialects/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ class Parser(parser.Parser):
"LOCATION": lambda self: self._parse_location_property(),
}

TYPE_CONVERTER = {
TYPE_CONVERTERS = {
# https://docs.snowflake.com/en/sql-reference/data-types-numeric#number
exp.DataType.Type.DECIMAL: build_default_decimal_type(precision=38, scale=0),
}
Expand Down
6 changes: 3 additions & 3 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ class Parser(metaclass=_Parser):
exp.DataType.Type.JSON: lambda self, this, _: self.expression(exp.ParseJSON, this=this),
}

TYPE_CONVERTER: t.Dict[exp.DataType.Type, t.Callable[[exp.DataType], exp.DataType]] = {}
TYPE_CONVERTERS: t.Dict[exp.DataType.Type, t.Callable[[exp.DataType], exp.DataType]] = {}

DDL_SELECT_TOKENS = {TokenType.SELECT, TokenType.WITH, TokenType.L_PAREN}

Expand Down Expand Up @@ -4472,8 +4472,8 @@ def _parse_types(
)
self._match(TokenType.R_BRACKET)

if self.TYPE_CONVERTER and isinstance(this.this, exp.DataType.Type):
converter = self.TYPE_CONVERTER.get(this.this)
if self.TYPE_CONVERTERS and isinstance(this.this, exp.DataType.Type):
converter = self.TYPE_CONVERTERS.get(this.this)
if converter:
this = converter(t.cast(exp.DataType, this))

Expand Down
7 changes: 7 additions & 0 deletions tests/dialects/test_duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,13 @@ def test_cast(self):
"CAST([{'a': 1}] AS STRUCT(a BIGINT)[])",
)

self.validate_all(
"CAST(x AS VARCHAR(5))",
write={
"duckdb": "CAST(x AS TEXT)",
"postgres": "CAST(x AS TEXT)",
},
)
self.validate_all(
"CAST(x AS DECIMAL(38, 0))",
read={
Expand Down

0 comments on commit 47472d9

Please sign in to comment.