[SPARK-52401][SQL] Fix DataFrame.collect() cache invalidation after saveAsTable append; add regression test #51240
+502
−1
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.
[SPARK-52401][SQL] Fix DataFrame.collect() cache invalidation after saveAsTable append; add regression test
What changes were proposed in this pull request?
This PR fixes a cache invalidation bug in Spark SQL where
DataFrame.collect()
could return stale results after appending data to a table usingsaveAsTable
withmode="append"
. The fix ensures that all DataFrame operations (collect()
,count()
, etc.) consistently reflect the latest table contents after append, overwrite, or similar operations.Key changes:
DataSourceV2Strategy.scala
, therefreshCache
function now usesuncacheQuery
instead ofrecacheByPlan
for append/overwrite operations. This properly invalidates the cache so that subsequent DataFrame actions read fresh data from the table.DataFrameCacheSuite.scala
) to verify correct cache invalidation and DataFrame behavior after table updates.SPARK-52401_FIX_SUMMARY.md
) and Python scripts for easy verification and review.Why are the changes needed?
Previously, after appending data to a table,
.count()
would return the correct row count, but.collect()
could return outdated results (e.g., an empty list). This was due to the cache manager re-executing the same logical plan, which did not reflect the updated table data. This PR ensures that the cache is invalidated, so all DataFrame operations see the latest data.Does this PR introduce any user-facing change?
Yes. After this fix:
.collect()
,.count()
, and all DataFrame actions will always reflect the current state of the table after append, overwrite, or similar operations.How was this patch tested?
sql/core/src/test/scala/org/apache/spark/sql/DataFrameCacheSuite.scala
test_spark_52401.py
,test_spark_52401_comprehensive.py
) for manual and cross-language verification.Additional context
SPARK-52401_FIX_SUMMARY.md
for a detailed explanation of the issue, root cause, and solution.