-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathtest_large_query_breakdown.py
More file actions
180 lines (166 loc) · 5.8 KB
/
test_large_query_breakdown.py
File metadata and controls
180 lines (166 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#
# Copyright (c) 2012-2025 Snowflake Computing Inc. All rights reserved.
#
from unittest import mock
import pytest
from snowflake.snowpark._internal.analyzer.analyzer import Analyzer
from snowflake.snowpark._internal.analyzer.binary_plan_node import (
Except,
Intersect,
Union,
)
from snowflake.snowpark._internal.analyzer.expression import Expression
from snowflake.snowpark._internal.analyzer.select_statement import (
SET_EXCEPT,
SET_INTERSECT,
SET_UNION,
SET_UNION_ALL,
SelectSnowflakePlan,
SelectSQL,
SelectStatement,
SetOperand,
SetStatement,
)
from snowflake.snowpark._internal.analyzer.snowflake_plan import SnowflakePlan
from snowflake.snowpark._internal.analyzer.snowflake_plan_node import (
LogicalPlan,
WithQueryBlock,
)
from snowflake.snowpark._internal.analyzer.unary_plan_node import (
Aggregate,
Pivot,
Sample,
Sort,
Unpivot,
)
from snowflake.snowpark._internal.compiler.large_query_breakdown import (
LargeQueryBreakdown,
)
from snowflake.snowpark.session import Session
dummy_session = mock.create_autospec(Session)
dummy_analyzer = mock.create_autospec(Analyzer)
dummy_analyzer.session = dummy_session
empty_logical_plan = LogicalPlan()
empty_expression = Expression()
empty_selectable = SelectSQL("dummy_query", analyzer=dummy_analyzer)
@pytest.mark.parametrize(
"node_generator,expected",
[
(lambda _: Pivot([], empty_expression, [], [], None, empty_logical_plan), True),
(
lambda _: Unpivot("value_col", "name_col", [], False, empty_logical_plan),
True,
),
(lambda _: Sort([], empty_logical_plan), True),
(lambda _: Aggregate([], [], empty_logical_plan), True),
(lambda _: Sample(empty_logical_plan, None, 2, None), True),
(lambda _: Union(empty_logical_plan, empty_logical_plan, is_all=False), True),
(lambda _: Union(empty_logical_plan, empty_logical_plan, is_all=True), False),
(lambda _: Except(empty_logical_plan, empty_logical_plan), True),
(lambda _: Intersect(empty_logical_plan, empty_logical_plan), True),
(lambda _: WithQueryBlock("dummy_cte", empty_logical_plan), True),
(
lambda x: SelectStatement(
from_=empty_selectable, order_by=[empty_expression], analyzer=x
),
True,
),
(
lambda x: SetStatement(
SetOperand(empty_selectable),
SetOperand(empty_selectable, SET_UNION),
analyzer=x,
),
True,
),
(
lambda x: SetStatement(
SetOperand(empty_selectable),
SetOperand(empty_selectable, SET_INTERSECT),
analyzer=x,
),
True,
),
(
lambda x: SetStatement(
SetOperand(empty_selectable),
SetOperand(empty_selectable, SET_EXCEPT),
analyzer=x,
),
True,
),
(
lambda x: SetStatement(
SetOperand(empty_selectable),
SetOperand(empty_selectable, SET_UNION_ALL),
SetOperand(empty_selectable, SET_UNION),
analyzer=x,
),
True,
),
(
lambda x: SetStatement(
SetOperand(empty_selectable),
SetOperand(empty_selectable, SET_UNION_ALL),
SetOperand(empty_selectable, SET_INTERSECT),
analyzer=x,
),
False,
),
],
)
def test_pipeline_breaker_node(mock_session, mock_analyzer, node_generator, expected):
large_query_breakdown = LargeQueryBreakdown(
mock_session,
mock_analyzer,
[],
mock_session.large_query_breakdown_complexity_bounds,
)
node = node_generator(mock_analyzer)
assert (
large_query_breakdown._is_node_pipeline_breaker(node) is expected
), f"Node {type(node)} is not detected as a pipeline breaker node"
resolved_node = mock_analyzer.resolve(node)
assert isinstance(resolved_node, SnowflakePlan)
assert (
large_query_breakdown._is_node_pipeline_breaker(resolved_node) is expected
), f"Resolved node of {type(node)} is not detected as a pipeline breaker node"
select_snowflake_plan = SelectSnowflakePlan(resolved_node, analyzer=mock_analyzer)
assert (
large_query_breakdown._is_node_pipeline_breaker(select_snowflake_plan)
is expected
), "SelectSnowflakePlan node is not detected as a pipeline breaker node"
@pytest.mark.parametrize(
"node_generator,expected",
[
(
lambda x: SelectStatement(
from_=empty_selectable, order_by=[empty_expression], analyzer=x
),
True,
),
],
)
def test_relaxed_pipeline_breaker_node(
mock_session, mock_analyzer, node_generator, expected
):
large_query_breakdown = LargeQueryBreakdown(
mock_session,
mock_analyzer,
[],
mock_session.large_query_breakdown_complexity_bounds,
)
node = node_generator(mock_analyzer)
assert (
large_query_breakdown._is_relaxed_pipeline_breaker(node) is expected
), f"Node {type(node)} is not detected as a pipeline breaker node"
resolved_node = mock_analyzer.resolve(node)
assert isinstance(resolved_node, SnowflakePlan)
assert (
large_query_breakdown._is_relaxed_pipeline_breaker(resolved_node) is expected
), f"Resolved node of {type(node)} is not detected as a pipeline breaker node"
select_snowflake_plan = SelectSnowflakePlan(resolved_node, analyzer=mock_analyzer)
assert (
large_query_breakdown._is_relaxed_pipeline_breaker(select_snowflake_plan)
is expected
), "SelectSnowflakePlan node is not detected as a pipeline breaker node"