Repository navigation
[SPARK-60387][SQL][4.0] Keep the existing table when saveAsTable overwrite fails before writing - #59658
Closed
dongjoon-hyun wants to merge 1 commit into
Closed
[SPARK-60387][SQL][4.0] Keep the existing table when saveAsTable overwrite fails before writing#59658dongjoon-hyun wants to merge 1 commit into
saveAsTable overwrite fails before writing#59658dongjoon-hyun wants to merge 1 commit into
Conversation
…erwrite fails before writing
HyukjinKwon
approved these changes
Oct 20, 2026
Member
Author
|
Thank you, @HyukjinKwon ! |
dongjoon-hyun
added a commit
that referenced
this pull request
Oct 21, 2026
…erwrite fails before writing ### What changes were proposed in this pull request? This PR aims to backport #59643 to `branch-4.0`. This PR makes `saveAsTable` with `SaveMode.Overwrite` drop the existing V1 table only after the checks that run before writing, such as the analysis, the data schema check, and the write options. Previously, `DataFrameWriter.saveAsTable` dropped the table before running the CTAS. Now the CTAS drops it: - `CreateDataSourceTableAsSelectCommand` drops it right before the write job starts, in the `beforeSetupJob` callback of `FileFormatWriter.write` (SPARK-60360), or right before `CreatableRelationProvider.createRelation`. If an extension replaces `InsertIntoHadoopFsRelationCommand` in the physical plan (e.g. a native writer), it drops the table before the write, as before. - `CreateHiveTableAsSelectCommand` drops it after the analysis, because it creates the new table before writing. Note that this backport differs from the original patch in the following `branch-4.0`-specific points: - `branch-4.0` has no `SaveAsV1TableCommand`, so the drop is removed from `DataFrameWriter.saveAsTable` instead. - `branch-4.0` has no `QueryExecution.runCommand` (SPARK-54835), so `DataSource.writeAndRead` inlines the eager command execution, like the `branch-4.1` backport. ### Why are the changes needed? When these checks fail, the table has already been dropped. A managed table loses its data too. ```scala spark.range(3).write.saveAsTable("t") spark.range(5).write.mode("overwrite").option("compression", "bogus").saveAsTable("t") // SparkIllegalArgumentException: [CODEC_NOT_AVAILABLE.WITH_AVAILABLE_CODECS_SUGGESTION] ... spark.table("t") // AnalysisException: [TABLE_OR_VIEW_NOT_FOUND] The table or view `t` cannot be found. ... ``` The same happens with an unsupported data type (e.g. a map column in CSV) or a nonexistent partition column. This has been the behavior since Spark 1.3.0. ### Does this PR introduce _any_ user-facing change? Yes. Such an overwrite keeps the existing table and its data. With `format("hive")`, only a failure in the analysis keeps the table, because `CreateHiveTableAsSelectCommand` creates the new table before writing. Valid writes are unchanged. ### How was this patch tested? Pass the CIs with the newly added tests in `DataFrameReaderWriterSuite` and `MetastoreDataSourcesSuite`. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Opus 5.5 Closes #59658 from dongjoon-hyun/SPARK-60387-4.0. Authored-by: Dongjoon Hyun <dongjoon@apache.org> Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
Member
Author
|
Merge Summary:
Posted by |
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.
What changes were proposed in this pull request?
This PR aims to backport #59643 to
branch-4.0.This PR makes
saveAsTablewithSaveMode.Overwritedrop the existing V1 table only after the checks that run before writing, such as the analysis, the data schema check, and the write options.Previously,
DataFrameWriter.saveAsTabledropped the table before running the CTAS. Now the CTAS drops it:CreateDataSourceTableAsSelectCommanddrops it right before the write job starts, in thebeforeSetupJobcallback ofFileFormatWriter.write(SPARK-60360), or right beforeCreatableRelationProvider.createRelation. If an extension replacesInsertIntoHadoopFsRelationCommandin the physical plan (e.g. a native writer), it drops the table before the write, as before.CreateHiveTableAsSelectCommanddrops it after the analysis, because it creates the new table before writing.Note that this backport differs from the original patch in the following
branch-4.0-specific points:branch-4.0has noSaveAsV1TableCommand, so the drop is removed fromDataFrameWriter.saveAsTableinstead.branch-4.0has noQueryExecution.runCommand(SPARK-54835), soDataSource.writeAndReadinlines the eager command execution, like thebranch-4.1backport.Why are the changes needed?
When these checks fail, the table has already been dropped. A managed table loses its data too.
The same happens with an unsupported data type (e.g. a map column in CSV) or a nonexistent partition column. This has been the behavior since Spark 1.3.0.
Does this PR introduce any user-facing change?
Yes. Such an overwrite keeps the existing table and its data. With
format("hive"), only a failure in the analysis keeps the table, becauseCreateHiveTableAsSelectCommandcreates the new table before writing. Valid writes are unchanged.How was this patch tested?
Pass the CIs with the newly added tests in
DataFrameReaderWriterSuiteandMetastoreDataSourcesSuite.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5