[spark] Make saveAsTable+overwrite behave as INSERT OVERWRITE#8225
Merged
Conversation
Previously, df.write.mode("overwrite").saveAsTable("t") produced a
ReplaceTableAsSelect plan which could drop+recreate the table if the
user did not re-specify partitionBy() and primary-key options.
Now on Spark 3.4+, saveAsTable+overwrite is rewritten to
OverwriteByExpression (or OverwritePartitionsDynamic when
partitionOverwriteMode=dynamic), preserving the existing table
definition (partitions, primary keys, properties). This aligns with
the behavior of INSERT OVERWRITE and is consistent with Delta Lake.
SQL CREATE OR REPLACE TABLE AS SELECT and V2 writeTo().replace()
are not affected.
d7c976f to
b37fdaa
Compare
Contributor
|
+1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Previously,
df.write.mode("overwrite").saveAsTable("t")produced aReplaceTableAsSelectplan, which could drop + recreate the table when the user did not re-specifypartitionBy()and primary-key options — silently losing the partition spec, primary keys, and table properties.This PR makes
saveAsTable+overwriteon an existing table (Spark 3.4+) be rewritten toOverwriteByExpression(orOverwritePartitionsDynamicwhenpartitionOverwriteMode=dynamic), preserving the existing table definition. This aligns with the behavior ofINSERT OVERWRITEand is consistent with Delta Lake.SQL
CREATE OR REPLACE TABLE AS SELECTand V2writeTo().replace()are not affected.Tests
Added cases in
DataFrameWriteTestBase:saveAsTable overwrite preserves table definition and snapshotssaveAsTable overwrite on non-partitioned tablesaveAsTable overwrite creates table when not existssaveAsTable overwrite respects dynamic partition overwrite mode