Skip to content
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

SNOW-1413958: Skip optimizing dynamic pivot query plan using CTE #1571

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/snowflake/snowpark/_internal/analyzer/cte_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ def traverse(root: "TreeNode") -> None:
while len(current_level) > 0:
next_level = []
for node in current_level:
# all subqueries under dynamic pivot node will not be optimized now
# due to a server side bug
# TODO: SNOW-1413967 Remove it when the bug is fixed
if is_dynamic_pivot_node(node):
continue
node_count_map[node] += 1
for child in node.children_plan_nodes:
# converting non-SELECT child query to SELECT query here,
Expand All @@ -84,6 +89,21 @@ def is_duplicate_subtree(node: "TreeNode") -> bool:
return True
return False

def is_dynamic_pivot_node(node: "TreeNode") -> bool:
from snowflake.snowpark._internal.analyzer.select_statement import (
SelectSnowflakePlan,
)
from snowflake.snowpark._internal.analyzer.snowflake_plan import SnowflakePlan
from snowflake.snowpark._internal.analyzer.unary_plan_node import Pivot

if isinstance(node, SelectSnowflakePlan):
source_plan = node.snowflake_plan.source_plan
elif isinstance(node, SnowflakePlan):
source_plan = node.source_plan
else:
return False
return isinstance(source_plan, Pivot) and source_plan.pivot_values is None

traverse(root)
return {node for node in node_count_map if is_duplicate_subtree(node)}

Expand Down
7 changes: 7 additions & 0 deletions tests/integ/test_cte.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,13 @@ def test_pivot_unpivot(session):
check_result(session, df_result, expect_cte_optimized=True)
assert count_number_of_ctes(df_result.queries["queries"][-1]) == 1

# Because of SNOW-1375062, dynamic pivot doesn't work with nested CTE
# TODO: SNOW-1413967 Remove it when the bug is fixed
df_nested = session.table("monthly_sales").select("*")
df_nested = df_nested.union_all(df_nested).select("*")
df_dynamic_pivot = df_nested.pivot("month").sum("amount")
check_result(session, df_dynamic_pivot, expect_cte_optimized=False)


def test_window_function(session):
window1 = (
Expand Down
Loading