Skip to content

Commit 82739a7

Browse files
pr feedback
1 parent 3b8adea commit 82739a7

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

sqlglot/generators/duckdb.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,26 @@ def _struct_sql(self: DuckDBGenerator, expression: exp.Struct) -> str:
655655
return f"ROW({csv_args})" if is_bq_inline_struct else f"{{{csv_args}}}"
656656

657657

658+
def _big_decimal_sql(self: DuckDBGenerator, expression: exp.DataType) -> str:
659+
if params := expression.expressions:
660+
precision_param = params[0].this if isinstance(params[0], exp.DataTypeParam) else params[0]
661+
if (
662+
isinstance(precision_param, exp.Literal)
663+
and precision_param.is_number
664+
and int(precision_param.to_py()) > 38
665+
):
666+
self.unsupported(
667+
"DECIMAL precision exceeds maximum of 38 for DuckDB, capping to (38,5)"
668+
)
669+
expression = expression.copy()
670+
expression.expressions[0].set("this", exp.Literal.number(38))
671+
expression.expressions[1].set("this", exp.Literal.number(5))
672+
673+
return f"DECIMAL({self.expressions(expression, flat=True)})"
674+
675+
return "DECIMAL(38, 5)"
676+
677+
658678
def _datatype_sql(self: DuckDBGenerator, expression: exp.DataType) -> str:
659679
if expression.is_type("array"):
660680
return f"{self.expressions(expression, flat=True)}[{self.expressions(expression, key='values', flat=True)}]"
@@ -663,9 +683,8 @@ def _datatype_sql(self: DuckDBGenerator, expression: exp.DataType) -> str:
663683
if expression.is_type(exp.DType.TIME, exp.DType.TIMETZ, exp.DType.TIMESTAMPTZ):
664684
return expression.this.value
665685

666-
# BIGDECIMAL type maps to DECIMAL(38, 5) but we should pass through user params when provided
667-
if expression.is_type(exp.DType.BIGDECIMAL) and expression.expressions:
668-
return f"DECIMAL({self.expressions(expression, flat=True)})"
686+
if expression.is_type(exp.DType.BIGDECIMAL):
687+
return _big_decimal_sql(self, expression)
669688

670689
return self.datatype_sql(expression)
671690

@@ -1745,7 +1764,7 @@ class DuckDBGenerator(generator.Generator):
17451764
exp.DType.TIMESTAMP_S: "TIMESTAMP_S",
17461765
exp.DType.TIMESTAMP_MS: "TIMESTAMP_MS",
17471766
exp.DType.TIMESTAMP_NS: "TIMESTAMP_NS",
1748-
exp.DType.BIGDECIMAL: "DECIMAL(38, 5)",
1767+
exp.DType.BIGDECIMAL: "DECIMAL",
17491768
}
17501769

17511770
# https://github.com/duckdb/duckdb/blob/ff7f24fd8e3128d94371827523dae85ebaf58713/third_party/libpg_query/grammar/keywords/reserved_keywords.list#L1-L77

tests/dialects/test_bigquery.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3824,11 +3824,19 @@ def test_bignumeric(self):
38243824
},
38253825
)
38263826

3827+
self.validate_all(
3828+
f"DECLARE x {type_}(20, 4)",
3829+
write={
3830+
"bigquery": "DECLARE x BIGNUMERIC(20, 4)",
3831+
"duckdb": "DECLARE x DECIMAL(20, 4)",
3832+
},
3833+
)
3834+
38273835
self.validate_all(
38283836
f"DECLARE x {type_}(76, 38)",
38293837
write={
38303838
"bigquery": "DECLARE x BIGNUMERIC(76, 38)",
3831-
"duckdb": "DECLARE x DECIMAL(76, 38)",
3839+
"duckdb": "DECLARE x DECIMAL(38, 5)",
38323840
},
38333841
)
38343842

0 commit comments

Comments
 (0)