LuciferYang opened a new issue, #11424:
URL: https://github.com/apache/paimon/issues/11424

   ### Search before asking
   
   - [x] I searched in the [issues](https://github.com/apache/paimon/issues) 
and found nothing similar.
   
   ### Paimon version
   
   master (2.2-SNAPSHOT)
   
   ### Compute Engine
   
   Spark 4.3.0 (paimon-spark-4.3)
   
   ### Minimal reproduce step
   
   With `PaimonSparkSessionExtensions` loaded and subquery reuse on (the 
default):
   
   ```scala
   // data.csv: 0,0 / 1,10 / 2,BAD / 3,30 / 4,40
   spark.read.schema("a LONG, b LONG").option("mode", "DROPMALFORMED")
     .csv("/tmp/csv").createOrReplaceTempView("csv")
   sql("SELECT (SELECT sum(a) FROM csv), (SELECT sum(b) FROM csv)").show()
   // [8, 80]; with the rule excluded through 
spark.sql.optimizer.excludedRules: [10, 80]
   ```
   
   ```sql
   CREATE TABLE T (a INT, b INT);
   INSERT INTO T VALUES (1, 10), (2, 20), (3, 30);
   SELECT (SELECT sum((SELECT sum(a) FROM T)) FROM T);
   -- ClassCastException: Aggregate cannot be cast to CTERelationDef
   
   CREATE TABLE S (id INT, s STRUCT<a: INT, b: INT>);
   INSERT INTO S VALUES (1, named_struct('a', 1, 'b', 100)), (2, 
named_struct('a', 2, 'b', 200));
   SELECT (SELECT sum(s.a) FROM S), (SELECT max(s.b) FROM S);
   -- IllegalArgumentException: Field names must be unique. Found duplicates: 
[s]
   ```
   
   The SPARK-52818 query in Spark's `CTEInlineSuite` (a non-deterministic CTE 
in scalar subqueries) fails under `.show()` with `NoSuchElementException: key 
not found: 0`.
   
   ### What doesn't meet your expectations?
   
   Each query above works on Spark 4.3 with Paimon's rule excluded. Two things 
cause the failures:
   
   - `MergePaimonScalarSubqueries` is a copy of an older version of Spark's 
`MergeScalarSubqueries` and runs before Spark's own `MergeSubplans`. It lacks 
the nested-subquery tracking of SPARK-40618, the CTE check of SPARK-52818 and 
the projection-sensitive file read check of SPARK-59107, and it unions the 
top-level struct fields of two scans by name.
   - It runs on queries that read no Paimon table, such as the CSV one above. 
Its guard skips a query only when subquery reuse is off and no Paimon scan is 
found, and reuse is on by default.
   
   The `WithCTE` it adds also makes `MergeSubplans` skip the whole query, so a 
parquet query whose subqueries have different filters reads the table twice 
instead of once.
   
   ### Anything else?
   
   Spark 4.3 can merge DSv2 scans itself when the table declares 
`TableCapability.SCAN_MERGING` (SPARK-40259). Spark 3.3 to 4.2 use the same 
rule. The nested-subquery and struct queries fail there too, and on 4.2 so does 
the CTE query, since Spark fixed it in 4.2.0. Those versions need a separate 
fix.
   
   ### Are you willing to submit a PR?
   
   - [x] I'm willing to submit a PR!
   


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

Reply via email to