Skip to content

Commit bf90b5d

Browse files
feat(duckdb): Add transpilation support for SPACE function (#6867)
1 parent f0618b9 commit bf90b5d

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

sqlglot/dialects/duckdb.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2922,6 +2922,15 @@ def startswith_sql(self, expression: exp.StartsWith) -> str:
29222922
_cast_to_varchar(expression.expression),
29232923
)
29242924

2925+
def space_sql(self, expression: exp.Space) -> str:
2926+
# DuckDB's REPEAT requires BIGINT for the count parameter
2927+
return self.sql(
2928+
exp.Repeat(
2929+
this=exp.Literal.string(" "),
2930+
times=exp.cast(expression.this, exp.DataType.Type.BIGINT),
2931+
)
2932+
)
2933+
29252934
def tablefromrows_sql(self, expression: exp.TableFromRows) -> str:
29262935
# For GENERATOR, unwrap TABLE() - just emit the Generator (becomes RANGE)
29272936
if isinstance(expression.this, exp.Generator):

tests/dialects/test_snowflake.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5635,6 +5635,34 @@ def test_array_flatten(self):
56355635
},
56365636
)
56375637

5638+
def test_space(self):
5639+
# Integer literal
5640+
self.validate_all(
5641+
"SELECT SPACE(5)",
5642+
write={
5643+
"snowflake": "SELECT REPEAT(' ', 5)",
5644+
"duckdb": "SELECT REPEAT(' ', CAST(5 AS BIGINT))",
5645+
},
5646+
)
5647+
5648+
# Float literal (tests rounding behavior)
5649+
self.validate_all(
5650+
"SELECT SPACE(3.7)",
5651+
write={
5652+
"snowflake": "SELECT REPEAT(' ', 3.7)",
5653+
"duckdb": "SELECT REPEAT(' ', CAST(3.7 AS BIGINT))",
5654+
},
5655+
)
5656+
5657+
# NULL value
5658+
self.validate_all(
5659+
"SELECT SPACE(NULL)",
5660+
write={
5661+
"snowflake": "SELECT REPEAT(' ', NULL)",
5662+
"duckdb": "SELECT REPEAT(' ', CAST(NULL AS BIGINT))",
5663+
},
5664+
)
5665+
56385666
def test_directed_joins(self):
56395667
self.validate_identity("SELECT * FROM a CROSS DIRECTED JOIN b USING (id)")
56405668
self.validate_identity("SELECT * FROM a INNER DIRECTED JOIN b USING (id)")

0 commit comments

Comments
 (0)