cloud-fan opened a new pull request, #60662:
URL: https://github.com/apache/spark/pull/60662
### What changes were proposed in this pull request?
`CTESubstitution` replaces every single-part `UnresolvedRelation` that
matches a CTE name in scope. The target relation of DELETE, UPDATE and MERGE is
such a relation, so it was replaced by the CTE body as well.
This PR makes `CTESubstitution` leave an `UnresolvedRelation` that is a
write target unchanged. A CTE cannot be written to, so a write target never
refers to a same-named CTE. The CTE stays visible everywhere else in the
statement: WHERE subqueries, SET values, and the MERGE source and condition.
A relation is a write target if it carries the internal
`REQUIRED_WRITE_PRIVILEGES` option. The parser already sets this option on
every DELETE, UPDATE and MERGE target, and `RelationResolution` already uses it
to detect write targets. The PR adds an `UnresolvedRelation.isWriteTarget`
helper for this check and uses it in both places.
`parse_sql` lineage (`ParseSqlResult`) now follows the same rule. A DELETE,
UPDATE or MERGE target is reported as a target table even when a CTE of the
same name is in scope, just as it already did for INSERT.
### Why are the changes needed?
When a statement-level WITH defines a CTE with the same name as the
unqualified DML target, the statement modifies the wrong table or fails. In
this example, `t` and `other` are v2 tables in a catalog that supports
row-level operations:
```sql
-- t: (1, 100, 'hr'), (2, 200, 'software'), (3, 300, 'hr')
-- other: (1, 10, 'hr'), (2, 20, 'software')
WITH t AS (TABLE other)
DELETE FROM t WHERE pk IN (SELECT pk FROM t WHERE salary = 10);
-- Before this PR: the row with pk = 1 is deleted from `other`, `t` is
unchanged, no error.
```
UPDATE and MERGE with the same shape also leave `t` unchanged without an
error. If the CTE body is anything other than a bare table reference, the
statement fails instead:
```sql
WITH t AS (SELECT * FROM t WHERE salary > 250)
DELETE FROM t WHERE pk IN (SELECT pk FROM t);
-- [INTERNAL_ERROR] Unexpected table relation: Filter ...
WITH t AS (SELECT * FROM t WHERE dep = 'hr') UPDATE t SET salary = 0;
-- [UNSUPPORTED_FEATURE.TABLE_OPERATION] ... Table `unknown` does not
support UPDATE TABLE ...
-- MERGE INTO with the same CTE fails the same way (does not support MERGE
INTO TABLE).
```
The target of a DML statement names a table, not a WITH query. This matches
PostgreSQL, where the statement writes the table it names. In Spark, an INSERT
target is already never substituted with a same-named CTE: SPARK-58933 keeps a
dynamic INSERT target in a dedicated `UnresolvedInsertTarget` node for this
reason. DELETE, UPDATE and MERGE were never covered.
Introducing a new target node for these statements would change their parsed
plan. Parser-level consumers, extensions and plan-based tooling match on that
plan. For the same reason, #59750 restored `InsertIntoStatement` as the parsed
plan of a static INSERT target. This PR keeps the parsed plans as they are and
relies on the write-privileges marker that the parser already attaches to these
targets.
### Does this PR introduce _any_ user-facing change?
Yes. When a CTE has the same name as the unqualified target of DELETE,
UPDATE or MERGE:
- Before: a bare table CTE body (for example `TABLE other`) made the
statement modify the table the CTE reads. Any other CTE body made the statement
fail with `INTERNAL_ERROR` or `UNSUPPORTED_FEATURE.TABLE_OPERATION`.
- After: the statement modifies the table it names. If no such table exists,
it fails with `TABLE_OR_VIEW_NOT_FOUND`. References to the name elsewhere in
the statement still resolve to the CTE.
`parse_sql` now reports such a target as a target table reference. It used
to drop it.
### How was this patch tested?
Added tests:
- `delete/update/merge target is not replaced by a same-named CTE` in
`DeleteFromTableSuiteBase`, `UpdateTableSuiteBase` and
`MergeIntoTableSuiteBase`. They run in all group-based and delta-based variants
and cover:
- a bare table CTE body and a filtering CTE body
- an aliased target
- a target given by a non-literal `IDENTIFIER(...)` clause
- DELETE under each `spark.sql.legacy.ctePrecedencePolicy` mode
Without the fix, these tests fail because the named table is left
unchanged.
- Updated `ParseSqlResultSuite` to expect the CTE-shadowed DELETE, UPDATE
and MERGE targets as target tables, with subqueries that still read the CTE.
Also ran, all passing:
- the new tests in all 14 group-based and delta-based Delete/Update/Merge
suite variants
- the full group-based and delta-based Delete, Update and Merge suites,
`MergeIntoDataFrameSuite`, `PlanResolutionSuite`, `CTEInlineSuiteAEOff`/`AEOn`,
`CTEHintSuite`, `ParametersSuite`, `DataSourceV2SQLSuiteV1Filter` and
`ParseSqlResultSuite`
- `SQLQueryTestSuite` filtered to `cte`
- catalyst `AnalysisSuite`, `ResolveRecursiveCTESuite`, `ResolveHintsSuite`,
`ResolveAliasesSuite`, `AnalysisErrorSuite`, `PlanParserSuite` and
`DDLParserSuite`
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Isaac (Claude Opus 5.5)
This pull request and its description were written by Isaac.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]