dolcino-li opened a new pull request, #7814:
URL: https://github.com/apache/kyuubi/pull/7814
# PR Title: [KYUUBI #7812][AUTHZ] Renew CTE ids when eliminating
PermanentViewMarker
A permanent view can be copied into several places of a plan. The
copy-on-write MERGE rewrite, for example, puts the source into both the main
query and the group filter subquery of `ReplaceData`. All copies share the same
CTE ids, but `PermanentViewMarker` is a `LeafNode` and each copy is unwrapped
at a different time: the subquery copy is unwrapped by `OptimizeSubqueries`
first and pruned by `PushdownPredicatesAndPruneColumnsForCTEDef`, while the
main copy is still hidden. `InlineCTE` then keys CTE definitions by id, the
pruned definition overwrites the other one, and the main query's
`CTERelationRef` is zipped against fewer, reordered columns, producing an
unresolved `Project` that fails with "Invalid call to exprId on unresolved
object".
Give the CTEs of a view fresh ids when its marker is unwrapped, so every
copy is self-contained and CTE rules never mix definitions of different copies.
Adds a MERGE INTO test on Iceberg with a source aggregating a permanent view
that contains a CTE.
<!--
Thanks for sending a pull request!
Here are some tips for you:
1. If this is your first time, please read our contributor guidelines:
https://kyuubi.readthedocs.io/en/master/contributing/code/index.html
2. If the PR is related to an issue in
https://github.com/apache/kyuubi/issues, add '[KYUUBI #XXXX]' in your PR title,
e.g., '[KYUUBI #XXXX] Your PR title ...'.
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g.,
'[WIP][KYUUBI #XXXX] Your PR title ...'.
-->
### Why are the changes needed?
Fixes #7812
Fixes a query failure in the `kyuubi-spark-authz` plugin. When the plugin is
enabled, a `MERGE INTO` against an Iceberg (copy-on-write) table fails with
```
org.apache.spark.sql.catalyst.analysis.UnresolvedException:
Invalid call to exprId on unresolved object
```
if the source query aggregates a permanent view that contains a CTE:
```sql
CREATE TABLE raw (id bigint, user_id bigint, d date, amt double);
INSERT INTO raw VALUES (1, 1, date'2024-01-01', 10.0);
CREATE VIEW v AS WITH r AS (SELECT * FROM raw) SELECT * FROM r;
CREATE TABLE target (d date, amt double) USING iceberg;
INSERT INTO target VALUES (date'2024-01-01', 0.0);
MERGE INTO target t
USING (SELECT d, sum(amt) + count(DISTINCT user_id) AS amt FROM v GROUP BY
d) s
ON t.d = s.d
WHEN MATCHED THEN UPDATE SET amt = s.amt;
```
Root cause: the plugin hides the plan of each permanent view behind a
`PermanentViewMarker` (`LeafNode`) until `RuleEliminatePermanentViewMarker`
unwraps it. A plan subtree can be copied into several places of the same plan —
the copy-on-write MERGE rewrite puts the source into both the main query and
the group filter subquery of `ReplaceData`. All copies share the same CTE ids,
but each copy is unwrapped at a different time:
1. The subquery copy is unwrapped by `OptimizeSubqueries` first, and its CTE
definition is pruned/rewritten by `PushdownPredicatesAndPruneColumnsForCTEDef`,
while the main copy is still hidden behind the marker.
2. `InlineCTE` keys CTE definitions by id, so the pruned definition
overwrites the other one.
3. The main query's `CTERelationRef` is zipped against fewer, reordered
columns, producing an unresolved `Project`.
This affects master and release branches with permanent view authz support
in `kyuubi-spark-authz`, and requires Spark 3.5+ where the copy-on-write MERGE
rewrite adds the group filter subquery.
### How was this patch tested?
- Added a new test `MERGE INTO with source aggregating a permanent view that
contains CTE` in `IcebergCatalogRangerSparkExtensionSuite`, which merges into
an Iceberg table using a source that aggregates a permanent view containing a
CTE, and verifies the updated answer.
- Locally ran the authz unit suites (`PermanentViewMarkerSuite`,
`PrivilegesBuilderSuite`, `InMemoryCatalogRangerSparkExtensionSuite`,
`HiveCatalogRangerSparkExtensionSuite`, etc.): all passed.
- The new test requires the MySQL testcontainer, so it is exercised by CI.
### Was this patch assisted by generative AI tooling?
Assisted-by: Claude Opus 5
--
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]