Skip to content

Commit 8e0b9f5

Browse files
authored
fix(snowflake): Fix annotate_types crashes for FLATTEN over STRUCT and WithinGroup (#7434)
1 parent c9562de commit 8e0b9f5

4 files changed

Lines changed: 43 additions & 8 deletions

File tree

sqlglot/optimizer/annotate_types.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -655,19 +655,21 @@ def _annotate_by_args(
655655
for expr in ensure_list(expressions):
656656
expr_type = expr.type
657657

658-
# Stop at the first nested data type found - we don't want to _maybe_coerce nested types
658+
if expr_type.is_type(exp.DType.UNKNOWN):
659+
self._set_type(expression, exp.DType.UNKNOWN)
660+
return expression
661+
662+
if nested_type:
663+
continue
664+
665+
# Stop coercing at the first nested data type found
659666
if expr_type.args.get("nested"):
660667
nested_type = expr_type
661-
break
662-
663-
if isinstance(expr, exp.Literal):
668+
elif isinstance(expr, exp.Literal):
664669
literal_type = self._maybe_coerce(literal_type or expr_type, expr_type)
665670
else:
666671
non_literal_type = self._maybe_coerce(non_literal_type or expr_type, expr_type)
667672

668-
if nested_type:
669-
break
670-
671673
result_type = None
672674

673675
if nested_type:

sqlglot/typing/snowflake.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def _annotate_within_group(self: TypeAnnotator, expression: exp.WithinGroup) ->
102102
and isinstance(ordered_expr := order_expr.expressions[0], exp.Ordered)
103103
):
104104
self._set_type(expression, ordered_expr.this.type)
105+
else:
106+
self._set_type(expression, expression.this.type)
105107

106108
return expression
107109

tests/fixtures/optimizer/annotate_functions.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5287,6 +5287,14 @@ DOUBLE;
52875287
PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY tbl.bigint_col) OVER (PARTITION BY 1);
52885288
BIGINT;
52895289

5290+
# dialect: snowflake
5291+
ARRAY_AGG(tbl.int_col) WITHIN GROUP (ORDER BY tbl.int_col);
5292+
ARRAY;
5293+
5294+
# dialect: snowflake
5295+
ARRAY_AGG(tbl.int_col) WITHIN GROUP (ORDER BY tbl.int_col) OVER (PARTITION BY tbl.text_col);
5296+
ARRAY;
5297+
52905298
# dialect: snowflake
52915299
PARSE_IP('192.168.1.1', 'INET');
52925300
OBJECT;

tests/fixtures/optimizer/optimizer.sql

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,4 +1566,27 @@ RIGHT JOIN GENERATE_SERIES((
15661566
FROM "x" AS "x"
15671567
), 10, 1) AS "t1"("c1")
15681568
ON "t1"."c1" > "z"."c"
1569-
;
1569+
;
1570+
1571+
# title: flatten over object_construct
1572+
# dialect: snowflake
1573+
# execute: false
1574+
WITH obj AS (SELECT object_construct('a', '1', 'b', '2') AS data), flattened AS (SELECT f.key, f.value FROM obj, lateral flatten(input => obj.data) AS f) SELECT key::varchar, value::varchar FROM flattened;
1575+
WITH "OBJ" AS (
1576+
SELECT
1577+
OBJECT_CONSTRUCT('a', '1', 'b', '2') AS "DATA"
1578+
)
1579+
SELECT
1580+
CAST("F"."KEY" AS VARCHAR) AS "KEY",
1581+
CAST("F"."VALUE" AS VARCHAR) AS "VALUE"
1582+
FROM "OBJ" AS "OBJ"
1583+
CROSS JOIN LATERAL FLATTEN(input => "OBJ"."DATA") AS "F"("SEQ", "KEY", "PATH", "INDEX", "VALUE", "THIS");
1584+
1585+
# title: array_agg within group over
1586+
# dialect: snowflake
1587+
# execute: false
1588+
SELECT array_agg(id) WITHIN GROUP (ORDER BY id) OVER (PARTITION BY grp) FROM t;
1589+
SELECT
1590+
ARRAY_AGG("T"."ID") WITHIN GROUP (ORDER BY
1591+
"T"."ID") OVER (PARTITION BY "T"."GRP") AS "_col_0"
1592+
FROM "T" AS "T";

0 commit comments

Comments
 (0)