Repository navigation
[SPARK-60387][SQL][4.1] Keep the existing table when saveAsTable overwrite fails before writing - #59656
Open
dongjoon-hyun wants to merge 1 commit into
Open
[SPARK-60387][SQL][4.1] Keep the existing table when saveAsTable overwrite fails before writing#59656dongjoon-hyun wants to merge 1 commit into
saveAsTable overwrite fails before writing#59656dongjoon-hyun wants to merge 1 commit into
Conversation
…erwrite fails before writing
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, `SaveAsV1TableCommand` 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.
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.
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.
Added tests to `DataFrameReaderWriterSuite` and `MetastoreDataSourcesSuite`. The tests of failed overwrites fail without this fix.
Generated-by: Claude Opus 5.5
Closes apache#59643 from dongjoon-hyun/SPARK-60387.
Authored-by: Dongjoon Hyun <dongjoon@apache.org>
Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
(cherry picked from commit 5637184)
Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
(cherry picked from commit 8a7c856)
HyukjinKwon
approved these changes
Oct 20, 2026
This branch has not been deployed
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.1.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,
SaveAsV1TableCommanddropped 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.Since
branch-4.1doesn't haveQueryExecution.runCommand(SPARK-54835),DataSource.writeAndReadruns the file source write command in the same way as the eager command execution ofbranch-4.1, and callsbeforeWritewith the physical plan right before the execution.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