Skip to content
Draft
Changes from all commits
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
15 changes: 12 additions & 3 deletions src/snowflake/snowpark/_internal/analyzer/select_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def __init__(
self.pre_actions: Optional[List["Query"]] = None
self.post_actions: Optional[List["Query"]] = None
self.flatten_disabled: bool = False
self.protect_dropped_new_columns: bool = False
self._column_states: Optional[ColumnStateDict] = None
self._snowflake_plan: Optional[SnowflakePlan] = None
self.expr_to_alias = (
Expand Down Expand Up @@ -1499,7 +1500,7 @@ def select(self, cols: List[Expression]) -> "SelectStatement":
can_be_flattened = False
else:
can_be_flattened = can_select_statement_be_flattened(
self.column_states, new_column_states
self.column_states, new_column_states, self.protect_dropped_new_columns
)

if can_be_flattened:
Expand Down Expand Up @@ -2196,7 +2197,9 @@ def parse_column_name(


def can_select_statement_be_flattened(
subquery_column_states: ColumnStateDict, new_column_states: ColumnStateDict
subquery_column_states: ColumnStateDict,
new_column_states: ColumnStateDict,
protect_dropped_new_columns: bool = False,
) -> bool:
for col, state in new_column_states.items():
dependent_columns = state.dependent_columns
Expand All @@ -2219,7 +2222,13 @@ def can_select_statement_be_flattened(
state.change_state == ColumnChangeState.DROPPED
and (subquery_state := subquery_column_states.get(col))
and subquery_state.change_state == ColumnChangeState.NEW
and subquery_state.is_referenced_by_same_level_column
and (
subquery_state.is_referenced_by_same_level_column
# If the subquery was explicitly marked (e.g. by the SCOS withColumn
# path), preserve it so that future filter/sort clauses can still
# reference the dropped NEW column.
or protect_dropped_new_columns
)
):
return False
return True
Expand Down
Loading