dolcino-li opened a new issue, #7812:
URL: https://github.com/apache/kyuubi/issues/7812

   ### Code of Conduct
   
   - [x] I agree to follow this project's [Code of 
Conduct](https://www.apache.org/foundation/policies/conduct)
   
   
   ### Search before asking
   
   - [x] I have searched in the 
[issues](https://github.com/apache/kyuubi/issues?q=is%3Aissue) and found no 
similar issues.
   
   
   ### Describe the bug
   
   When the kyuubi-spark-authz plugin is enabled, a MERGE INTO statement 
against an Iceberg (copy-on-write) table fails with Invalid call to exprId on 
unresolved object if the source query aggregates a permanent view that contains 
a CTE.
   
   The plugin wraps each permanent view in a PermanentViewMarker so that the 
view's internal plan is hidden from other analyzer/optimizer rules until the 
marker is eliminated by RuleEliminatePermanentViewMarker.
   
   However, a plan subtree can be copied into several places of the same 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 of the view 
share the same CTE ids, but since PermanentViewMarker is a LeafNode, each copy 
is unwrapped at a different time:
   
   The subquery copy is unwrapped by OptimizeSubqueries first, and its CTE 
definition gets pruned/rewritten by PushdownPredicatesAndPruneColumnsForCTEDef, 
while the main copy is still hidden behind the marker.
   Later, InlineCTE keys CTE definitions by id, so the pruned definition 
overwrites the other one.
   The main query's CTERelationRef is then zipped against fewer, reordered 
columns, producing an unresolved Project that fails with:
   ```
   org.apache.spark.sql.catalyst.analysis.UnresolvedException: Invalid call to 
exprId on unresolved object
   ```
   
   
   ### Affects Version(s)
   
   master
   
   ### Kyuubi Server Log Output
   
   ```logtalk
   N/A
   ```
   
   ### Kyuubi Engine Log Output
   
   ```logtalk
   N/A
   ```
   
   ### Kyuubi Server Configurations
   
   ```yaml
   N/A
   ```
   
   ### Kyuubi Engine Configurations
   
   ```yaml
   N/A
   ```
   
   ### Additional context
   
   Steps to reproduce (with the authz plugin and an Iceberg catalog):
   ```
   CREATE TABLE raw (id bigint, user_id bigint, d date, amt double);
   INSERT INTO raw VALUES (1, 1, date'2024-01-01', 10.0);
   
   -- permanent view containing a CTE
   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);
   
   -- fails with UnresolvedException
   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;
   ```
   Expected result: the MERGE succeeds (target row updated to 11.0). Actual 
result: the query fails with Invalid call to exprId on unresolved object.
   
   Suggested fix: give the CTEs of a view fresh ids when its 
PermanentViewMarker is unwrapped (renew the ids of CTERelationDef / 
CTERelationRef inside the unwrapped child), so every copy of the view is 
self-contained and CTE rules never mix definitions belonging to different 
copies.
   
   ### Are you willing to submit PR?
   
   - [x] Yes. I would be willing to submit a PR with guidance from the Kyuubi 
community to fix.
   - [ ] No. I cannot submit a PR at this time.


-- 
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]

Reply via email to