-
Notifications
You must be signed in to change notification settings - Fork 146
SNOW-2019483: fix select SQL in dynamic table #3259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 20 commits
d15ea60
b5b68e2
106385d
9a6939b
da7adca
c321884
112d7ba
6eeb25d
5576548
5c6f53f
35eda2e
4d2e82d
5b758af
ff3303e
6d829ff
999e5d4
6263efb
7ab12a3
d2a7425
94a4d8f
916e9bb
4a31b0e
c1f2865
187bb92
a65e94a
009637e
0b63111
b20bd1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,7 @@ | |
| import re | ||
| import sys | ||
| import uuid | ||
| from collections import defaultdict | ||
| from collections import defaultdict, deque | ||
| from enum import Enum | ||
| from functools import cached_property | ||
| from typing import ( | ||
|
|
@@ -30,6 +30,7 @@ | |
| from snowflake.snowpark._internal.analyzer.table_function import ( | ||
| GeneratorTableFunction, | ||
| TableFunctionRelation, | ||
| TableFunctionJoin, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
|
|
@@ -1324,6 +1325,61 @@ def create_or_replace_view( | |
| source_plan, | ||
| ) | ||
|
|
||
| def find_table_function_in_sql_tree(self, plan: SnowflakePlan) -> SnowflakePlan: | ||
| """This function is meant to find any udtf function call from a create dynamic table plan and | ||
| replace '*' with explicit column identifier in the select of table function. Since we cannot | ||
| differentiate udtf call from other table functions, we apply this change to all table functions. | ||
| """ | ||
| deepcopied_plan = copy.deepcopy(plan) | ||
| queue = deque() | ||
| queue.append(deepcopied_plan) | ||
| from snowflake.snowpark._internal.analyzer.select_statement import ( | ||
| SelectTableFunction, | ||
| Selectable, | ||
| ) | ||
|
|
||
| while queue: | ||
| deepcopied_plan = queue.popleft() | ||
| for node in deepcopied_plan.children_plan_nodes: | ||
| queue.append(node) | ||
|
|
||
| # the bug only happen when create dynamic table on top of a table function | ||
| # this is meant to decide whether the plan is select from a table function | ||
| if isinstance(deepcopied_plan, SelectTableFunction) and isinstance( | ||
| deepcopied_plan.snowflake_plan.source_plan, TableFunctionJoin | ||
| ): | ||
sfc-gh-yuwang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # if clause used to decide that right column is '*' that we need to change and there is only 1 child | ||
| # plan to change, a table function can only be right joined, so we only care about right column here. | ||
| if ( | ||
| deepcopied_plan.snowflake_plan.source_plan.right_cols == ["*"] | ||
| and len(deepcopied_plan.snowflake_plan.children_plan_nodes) == 1 | ||
| ): | ||
sfc-gh-yuwang marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| child_plan = deepcopied_plan.snowflake_plan.children_plan_nodes[0] | ||
| if isinstance(child_plan, Selectable): | ||
| child_plan = child_plan.snowflake_plan | ||
sfc-gh-yuwang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| deepcopied_plan.snowflake_plan.source_plan.right_cols = ( | ||
| deepcopied_plan.snowflake_plan.quoted_identifiers[ | ||
| len(child_plan.quoted_identifiers) : | ||
| ] | ||
| ) | ||
| new_plan = self.session._analyzer.resolve( | ||
| deepcopied_plan.snowflake_plan.source_plan | ||
| ) | ||
| deepcopied_plan._snowflake_plan = new_plan | ||
|
|
||
| # resolve the plan to apply change | ||
| self.session._analyzer.resolve( | ||
| deepcopied_plan.snowflake_plan.source_plan # type: ignore | ||
| if isinstance(deepcopied_plan, Selectable) | ||
|
||
| else deepcopied_plan.source_plan | ||
| ) | ||
| # resolve the plan to apply our change | ||
| return self.session._analyzer.resolve( | ||
| deepcopied_plan.snowflake_plan.source_plan # type: ignore | ||
| if isinstance(deepcopied_plan, Selectable) | ||
| else deepcopied_plan.source_plan | ||
| ) | ||
|
|
||
| def create_or_replace_dynamic_table( | ||
| self, | ||
| name: str, | ||
|
|
@@ -1341,6 +1397,9 @@ def create_or_replace_dynamic_table( | |
| source_plan: Optional[LogicalPlan], | ||
| iceberg_config: Optional[dict] = None, | ||
| ) -> SnowflakePlan: | ||
|
|
||
| child = self.find_table_function_in_sql_tree(child) | ||
|
|
||
| if len(child.queries) != 1: | ||
| raise SnowparkClientExceptionMessages.PLAN_CREATE_DYNAMIC_TABLE_FROM_DDL_DML_OPERATIONS() | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.