[SPARK-58370][SQL] Check table write privileges when the write target is also read in the same statement - #57563
Open
peter-toth wants to merge 1 commit into
Conversation
… is also read in the same statement
peter-toth
force-pushed
the
SPARK-58370-write-privileges-self-reference
branch
from
July 27, 2026 10:32
6b8c220 to
c633941
Compare
uros-b
approved these changes
Jul 27, 2026
Member
|
Thank you @peter-toth! |
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?
RelationResolution.tryResolvePersistentanswers a hit in the per-queryAnalysisContext.relationCachebefore reaching the branch that callsCatalogV2Util.loadTable(catalog, ident, timeTravelSpec, Option(writePrivileges)). This PR stops serving a reference that carriesREQUIRED_WRITE_PRIVILEGESfrom that cache, so a write target always goes throughTableCatalog.loadTable(ident, writePrivileges):Loaded relations are still stored in the cache, so a self-MERGE's source keeps sharing the target's
Tableinstance (one snapshot).V2TableReferencealready treats write targets this way (V2TableReference.WriteTargetContext.cacheable = false).Why are the changes needed?
TableCatalog.loadTable(Identifier, Set<TableWritePrivilege>)(@since 3.5.3) is where a catalog authorizes a write; the only other caller in the tree isResolveSchemaEvolution. A write target hits the relation cache whenever another reference to the same table was resolved earlier in the same analyzer run, which is exactly the case forInsertIntoStatementandV2WriteCommand- their target is resolved at the command node after its query. So the catalog is never asked to authorize these statements:INSERT INTO t SELECT 1on the same table is authorized normally, andUPDATE/DELETE/MERGEare unaffected (their target is a plain child, resolved before the source, so it is a cache miss), as isDataFrameWriter.insertInto(it passes the already-analyzed plan).Does this PR introduce any user-facing change?
Yes. On a catalog that enforces privileges in
loadTable(ident, writePrivileges), the statements above are now authorized like every other write, so a user without the required privilege gets the catalog's error instead of silently writing. Catalogs that ignore the privileges argument (including Spark's built-inV2SessionCatalog) are unaffected. The write target of a self-referencing INSERT is now loaded once more, which is what a non-self-referencing INSERT already does.How was this patch tested?
New test in
DataSourceV2SQLSuitenext to "SPARK-49246: read-only catalog" (whose write cases all use a constant source, which is why this gap was never covered), asserting theReadOnlyCatalogprivilege error for every statement above plus the UPDATE / DELETE / MERGE self-reference cases, for both a custom v2 catalog and aReadOnlyCatalog-backedspark_catalog. It fails on master ("Expected exception java.lang.RuntimeException to be thrown, but no exception was thrown") and passes with the fix.Also green: 17 suites / 1506 tests -
DataSourceV2SQLSuite(V1+V2 filter),DataSourceV2DataFrameSuite,DataSourceV2OptionSuite,PlanResolutionSuite,CachedTableSuite,*ViewTestSuite, group/delta-based MERGE/UPDATE/DELETE suites,InsertSuite.dev/lint-scalaclean.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5)