Skip to content

[SPARK-52312][SQL] Ignore V2WriteCommand when caching DataFrame #51032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

qiyuandong-db
Copy link

What changes were proposed in this pull request?

We found an issue that V2WriteCommand plans were not properly excluded from DataFrame caching, which can cause unintended side effects.

For example, when cache() is called on a DataFrame created from an INSERT SQL statement, the INSERT command gets re-executed during the caching process because the underlying plan is not being ignored.

This PR fixes this by

  • making V2WriteCommand extend the IgnoreCachedData trait
  • updating the caching logic to skip plans that extend IgnoreCachedData, preventing inapplicable plans from being cached

Why are the changes needed?

This is a bug, since calling cache() on a DataFrame shouldn't re-execute the command that created it.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

New tests were added.

Was this patch authored or co-authored using generative AI tooling?

No.

@github-actions github-actions bot added the SQL label May 27, 2025
tableName,
storageLevel
)
if (query.queryExecution.analyzed.isInstanceOf[IgnoreCachedData]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we do it in cacheQueryInternal which is lower level?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that would be better. We have two cacheQuery() variants and they both call cacheQueryInternal().
Moved the check there.

@@ -875,4 +875,53 @@ class DataFrameWriterV2Suite extends QueryTest with SharedSparkSession with Befo
}
}
}

test("SPARK-52312: caching dataframe created from INSERT shouldn't re-execute the command") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this test only. It's a unit test, and we don't need to iterate all v2 commands to prove the fix. Testing a common v2 command (table INSERT) is good.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I've removed repeated ones.

@qiyuandong-db qiyuandong-db requested a review from cloud-fan May 28, 2025 11:35
@@ -127,6 +127,11 @@ class CacheManager extends Logging with AdaptiveSparkPlanHelper {
// Do nothing for StorageLevel.NONE since it will not actually cache any data.
} else if (lookupCachedDataInternal(normalizedPlan).nonEmpty) {
logWarning("Asked to cache already cached data.")
} else if (unnormalizedPlan.isInstanceOf[IgnoreCachedData]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we move it before the above else if? For IgnoreCachedData, we don't even need to do a lookup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants