🌐 US-Proxy
class="logged-out env-production page-responsive" style="word-wrap: break-word;" >
Skip to content

[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
apache:branch-4.1from
dongjoon-hyun:SPARK-60387-4.1
Open

dongjoon-hyun wants to merge 1 commit into
apache:branch-4.1from
dongjoon-hyun:SPARK-60387-4.1

Conversation

@dongjoon-hyun

Copy link
Copy Markdown
Member

What changes were proposed in this pull request?

This PR aims to backport #59643 to branch-4.1.

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.

Since branch-4.1 doesn't have QueryExecution.runCommand (SPARK-54835), DataSource.writeAndRead runs the file source write command in the same way as the eager command execution of branch-4.1, and calls beforeWrite with 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.

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

…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)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants