Skip to content

Commit 4d0874b

Browse files
committed
protect the change
1 parent 88d17f0 commit 4d0874b

File tree

2 files changed

+39
-15
lines changed

2 files changed

+39
-15
lines changed

src/snowflake/snowpark/_internal/analyzer/select_statement.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,15 @@ def __deepcopy__(self, memodict={}) -> "SelectableEntity": # noqa: B006
586586
deepcopy(self.entity, memodict), analyzer=self.analyzer
587587
)
588588
_deepcopy_selectable_fields(from_selectable=self, to_selectable=copied)
589-
copied._attributes = (
590-
deepcopy(self._attributes) if self._attributes is not None else None
591-
)
589+
if (
590+
self._session.reduce_describe_query_enabled
591+
and self._session.cte_optimization_enabled
592+
):
593+
copied._attributes = (
594+
deepcopy(self._attributes, memodict)
595+
if self._attributes is not None
596+
else None
597+
)
592598
return copied
593599

594600
@property
@@ -929,9 +935,6 @@ def __copy__(self):
929935
)
930936
# The following values will change if they're None in the newly copied one so reset their values here
931937
# to avoid problems.
932-
new._attributes = (
933-
self._attributes.copy() if self._attributes is not None else None
934-
)
935938
new._projection_in_str = None
936939
new._schema_query = None
937940
new._column_states = None
@@ -945,6 +948,13 @@ def __copy__(self):
945948
self._merge_projection_complexity_with_subquery
946949
)
947950
new.df_ast_ids = self.df_ast_ids.copy() if self.df_ast_ids is not None else None
951+
if (
952+
self._session.reduce_describe_query_enabled
953+
and self._session.cte_optimization_enabled
954+
):
955+
new._attributes = (
956+
self._attributes.copy() if self._attributes is not None else None
957+
)
948958
return new
949959

950960
def __deepcopy__(self, memodict={}) -> "SelectStatement": # noqa: B006
@@ -964,9 +974,15 @@ def __deepcopy__(self, memodict={}) -> "SelectStatement": # noqa: B006
964974
)
965975

966976
_deepcopy_selectable_fields(from_selectable=self, to_selectable=copied)
967-
copied._attributes = (
968-
deepcopy(self._attributes) if self._attributes is not None else None
969-
)
977+
if (
978+
self._session.reduce_describe_query_enabled
979+
and self._session.cte_optimization_enabled
980+
):
981+
copied._attributes = (
982+
deepcopy(self._attributes, memodict)
983+
if self._attributes is not None
984+
else None
985+
)
970986
copied._projection_in_str = self._projection_in_str
971987
copied._query_params = deepcopy(self._query_params)
972988
copied._merge_projection_complexity_with_subquery = (
@@ -1412,7 +1428,11 @@ def select(self, cols: List[Expression]) -> "SelectStatement":
14121428
if can_be_flattened:
14131429
new = copy(self)
14141430
final_projection = []
1415-
new._attributes = None # reset attributes since projection changed
1431+
if (
1432+
self._session.reduce_describe_query_enabled
1433+
and self._session.cte_optimization_enabled
1434+
):
1435+
new._attributes = None # reset attributes since projection changed
14161436
assert new_column_states is not None
14171437
for col, state in new_column_states.items():
14181438
if state.change_state in (

src/snowflake/snowpark/_internal/compiler/repeated_subquery_elimination.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from snowflake.snowpark._internal.utils import (
2121
TEMP_OBJECT_NAME_PREFIX,
2222
TempObjectType,
23+
random_name_for_temp_object,
2324
)
2425

2526

@@ -164,11 +165,14 @@ def _update_parents(
164165
node.encoded_node_id_with_query
165166
]
166167
else:
167-
# create a WithQueryBlock node with deterministic name
168-
# Use first 16 chars of encoded_node_id_with_query (SHA256 hash)
169-
# This ensures the same node always gets the same CTE name
170-
cte_name = f"{TEMP_OBJECT_NAME_PREFIX}{TempObjectType.CTE.value}_{node.encoded_node_id_with_query[:16].upper()}"
171-
with_block = WithQueryBlock(name=cte_name, child=node)
168+
if self._query_generator.session.reduce_describe_query_enabled:
169+
# create a deterministic name using the first 16 chars of encoded_node_id_with_query (SHA256 hash)
170+
# This ensures the same node always gets the same CTE name.
171+
# it helps when DataFrame.queries is called multiple times, they will get the same CTE name.
172+
cte_name = f"{TEMP_OBJECT_NAME_PREFIX}{TempObjectType.CTE.value}_{node.encoded_node_id_with_query[:16].upper()}"
173+
else:
174+
cte_name = random_name_for_temp_object(TempObjectType.CTE)
175+
with_block = WithQueryBlock(name=cte_name, child=node) # type: ignore
172176
with_block._is_valid_for_replacement = True
173177

174178
resolved_with_block = self._query_generator.resolve(with_block)

0 commit comments

Comments
 (0)