Skip to content

Commit eaf4292

Browse files
committed
overwrite_condition should only work with overwrite mode, reverting support for append mode
1 parent 7a463f9 commit eaf4292

File tree

4 files changed

+505
-770
lines changed

4 files changed

+505
-770
lines changed

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,21 +1462,16 @@ def get_overwrite_delete_insert_plan(child: SnowflakePlan):
14621462
if mode == SaveMode.APPEND:
14631463
assert table_exists is not None
14641464
if table_exists:
1465-
if overwrite_condition is not None:
1466-
return get_overwrite_delete_insert_plan(child)
1467-
else:
1468-
# Normal append without overwrite_condition
1469-
return self.build(
1470-
lambda x: insert_into_statement(
1471-
table_name=full_table_name,
1472-
child=x,
1473-
column_names=column_names,
1474-
),
1475-
child,
1476-
source_plan,
1477-
)
1465+
return self.build(
1466+
lambda x: insert_into_statement(
1467+
table_name=full_table_name,
1468+
child=x,
1469+
column_names=column_names,
1470+
),
1471+
child,
1472+
source_plan,
1473+
)
14781474
else:
1479-
# Table doesn't exist, just create and insert (overwrite_condition is no-op)
14801475
return get_create_and_insert_plan(child, replace=False, error=False)
14811476

14821477
elif mode == SaveMode.TRUNCATE:

src/snowflake/snowpark/dataframe_writer.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def save_as_table(
333333
Set to ``True`` if table exists, ``False`` if it doesn't, or ``None`` (default) for automatic detection.
334334
Primarily useful for "append", "truncate", and "overwrite" with overwrite_condition modes to avoid running query for automatic detection.
335335
overwrite_condition: Specifies the overwrite condition to perform atomic targeted delete-insert.
336-
Can be used when ``mode`` is "append" or "overwrite" when the table exists. Rows matching the
336+
Can only be used when ``mode`` is "overwrite" and the table exists. Rows matching the
337337
condition are deleted from the target table, then all rows from the DataFrame are inserted.
338338
339339
@@ -380,7 +380,7 @@ def save_as_table(
380380
[Row(ID=1, VAL='a'), Row(ID=2, VAL='b'), Row(ID=3, VAL='c')]
381381
382382
>>> new_df = session.create_dataframe([[2, "updated2"], [5, "updated5"]], schema=["id", "val"])
383-
>>> new_df.write.mode("append").save_as_table("my_table", overwrite_condition="id = 1 or val = 'b'")
383+
>>> new_df.write.mode("overwrite").save_as_table("my_table", overwrite_condition="id = 1 or val = 'b'")
384384
>>> session.table("my_table").order_by("id").collect()
385385
[Row(ID=2, VAL='updated2'), Row(ID=3, VAL='c'), Row(ID=5, VAL='updated5')]
386386
"""
@@ -513,13 +513,10 @@ def save_as_table(
513513
f"Unsupported table type. Expected table types: {SUPPORTED_TABLE_TYPES}"
514514
)
515515

516-
# overwrite_condition must be used with APPEND or OVERWRITE mode
517-
if overwrite_condition is not None and save_mode not in (
518-
SaveMode.APPEND,
519-
SaveMode.OVERWRITE,
520-
):
516+
# overwrite_condition must be used with OVERWRITE mode only
517+
if overwrite_condition is not None and save_mode != SaveMode.OVERWRITE:
521518
raise ValueError(
522-
f"'overwrite_condition' is only supported with mode='append' or mode='overwrite'. "
519+
f"'overwrite_condition' is only supported with mode='overwrite'. "
523520
f"Got mode='{save_mode.value}'."
524521
)
525522

0 commit comments

Comments
 (0)