Skip to content

Commit 0b46b39

Browse files
fix(snowflake): transpile REDUCE to DuckDB LIST_REDUCE (#7505)
* fix(snowflake): transpile REDUCE to DuckDB LIST_REDUCE * Add comment explaining finish parameter fallback * refactor(duckdb): use @unsupported_args decorator for REDUCE finish parameter Replace manual fallback check with @unsupported_args decorator as suggested in PR review. * Sync w/ integration tests * Fix tests --------- Co-authored-by: George Sittas <giwrgos.sittas@gmail.com>
1 parent 9556ee2 commit 0b46b39

4 files changed

Lines changed: 12 additions & 3 deletions

File tree

sqlglot-integration-tests

sqlglot/generators/duckdb.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,6 +2332,17 @@ def randstr_sql(self, expression: exp.Randstr) -> str:
23322332
replacements = {"seed": seed_value, "length": length}
23332333
return f"({self.sql(exp.replace_placeholders(self.RANDSTR_TEMPLATE, **replacements))})"
23342334

2335+
@unsupported_args("finish")
2336+
def reduce_sql(self, expression: exp.Reduce) -> str:
2337+
array_arg = expression.this
2338+
initial_value = expression.args.get("initial")
2339+
merge_lambda = expression.args.get("merge")
2340+
2341+
if merge_lambda:
2342+
merge_lambda.set("colon", True)
2343+
2344+
return self.func("list_reduce", array_arg, merge_lambda, initial_value)
2345+
23352346
def zipf_sql(self, expression: exp.Zipf) -> str:
23362347
"""
23372348
Transpile Snowflake's ZIPF to DuckDB using CDF-based inverse sampling.

tests/dialects/test_dialect.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,6 @@ def test_array(self):
13991399
"REDUCE(x, 0, (acc, x) -> acc + x, acc -> acc)",
14001400
write={
14011401
"trino": "REDUCE(x, 0, (acc, x) -> acc + x, acc -> acc)",
1402-
"duckdb": "REDUCE(x, 0, (acc, x) -> acc + x, acc -> acc)",
14031402
"hive": "REDUCE(x, 0, (acc, x) -> acc + x, acc -> acc)",
14041403
"spark": "AGGREGATE(x, 0, (acc, x) -> acc + x, acc -> acc)",
14051404
"presto": "REDUCE(x, 0, (acc, x) -> acc + x, acc -> acc)",

tests/dialects/test_spark.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,6 @@ def test_spark(self):
786786
"AGGREGATE(my_arr, 0, (acc, x) -> acc + x, s -> s * 2)",
787787
write={
788788
"trino": "REDUCE(my_arr, 0, (acc, x) -> acc + x, s -> s * 2)",
789-
"duckdb": "REDUCE(my_arr, 0, (acc, x) -> acc + x, s -> s * 2)",
790789
"hive": "REDUCE(my_arr, 0, (acc, x) -> acc + x, s -> s * 2)",
791790
"presto": "REDUCE(my_arr, 0, (acc, x) -> acc + x, s -> s * 2)",
792791
"spark": "AGGREGATE(my_arr, 0, (acc, x) -> acc + x, s -> s * 2)",

0 commit comments

Comments
 (0)