Skip to content

Commit d7b3b3e

Browse files
authored
Fix(optimizer): handle TableFromRows properly in annotate_types (#4889)
1 parent df96dcf commit d7b3b3e

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

sqlglot/lineage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def to_node(
237237
# Find all columns that went into creating this one to list their lineage nodes.
238238
source_columns = set(find_all_in_scope(select, exp.Column))
239239

240-
# If the source is a UDTF find columns used in the UTDF to generate the table
240+
# If the source is a UDTF find columns used in the UDTF to generate the table
241241
if isinstance(source, exp.UDTF):
242242
source_columns |= set(source.find_all(exp.Column))
243243
derived_tables = [

sqlglot/optimizer/annotate_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def annotate_scope(self, scope: Scope) -> None:
223223
values = [expression.this.this]
224224
elif isinstance(expression, exp.Unnest):
225225
values = [expression]
226-
else:
226+
elif not isinstance(expression, exp.TableFromRows):
227227
values = expression.expressions[0].expressions
228228

229229
if not values:

tests/test_optimizer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,16 @@ def test_union_annotation(self):
14341434
"""
14351435
self.assertEqual(optimizer.optimize(sql).selects[0].type.this, exp.DataType.Type.VARCHAR)
14361436

1437+
def test_udtf_annotation(self):
1438+
table_udtf = parse_one(
1439+
"SELECT * FROM TABLE(GENERATOR(ROWCOUNT => 100000))",
1440+
read="snowflake",
1441+
)
1442+
self.assertEqual(
1443+
annotate_types(table_udtf, dialect="snowflake").sql("snowflake"),
1444+
"SELECT * FROM TABLE(GENERATOR(ROWCOUNT => 100000))",
1445+
)
1446+
14371447
def test_recursive_cte(self):
14381448
query = parse_one(
14391449
"""

0 commit comments

Comments
 (0)