rich7420 opened a new issue, #7818:
URL: https://github.com/apache/datafusion-comet/issues/7818

   ### Describe the bug
   
   #6035 and #7195 put a `CometLocalLimit` under a projection directly below a 
limit 
([CometExecRule.scala#L798-L867](https://github.com/apache/datafusion-comet/blob/1c0eb0b8bedbf19341dcbb5b115c0ec2235de576/spark/src/main/scala/org/apache/comet/rules/CometExecRule.scala#L798-L867)),
 so the projection doesn't evaluate rows past the limit. That assumes Spark 
stops there too. With whole-stage codegen, Spark sometimes evaluates more rows, 
and the cap then hides an error Spark raises:
   
   1. A root `LIMIT` (`CollectLimit`) over a projection that reads from a 
1-to-N operator in the same codegen stage, like `explode`, `Expand`, or a hash 
join whose key matches several build rows. The operator emits every row for one 
input row without a stop check, and the stage evaluates the projection for each 
of them 
([GenerateExec.scala#L235-L240](https://github.com/apache/spark/blob/v4.1.3/sql/core/src/main/scala/org/apache/spark/sql/execution/GenerateExec.scala#L235-L240)).
   2. A `LocalLimit`/`GlobalLimit` above a projection and below an aggregate or 
sort in the same codegen stage. The blocking operator turns off the per-row 
stop check 
([WholeStageCodegenExec.scala#L437](https://github.com/apache/spark/blob/v4.1.3/sql/core/src/main/scala/org/apache/spark/sql/execution/WholeStageCodegenExec.scala#L437)).
 Batched input such as `RangeExec` then finishes its current batch before 
checking the limit again 
([basicPhysicalOperators.scala#L586-L605](https://github.com/apache/spark/blob/v4.1.3/sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala#L586-L605)),
 and `ProjectExec` evaluates shared subexpressions before the limit checks its 
count 
([basicPhysicalOperators.scala#L68-L90](https://github.com/apache/spark/blob/v4.1.3/sql/core/src/main/scala/org/apache/spark/sql/execution/basicPhysicalOperators.scala#L68-L90)).
   
   Neither PR is in `branch-1.2`, so this hasn't shipped yet.
   
   ### Steps to reproduce
   
   ANSI on, default profile (Spark 4.1.3), main at `1c0eb0b8b`:
   
   ```sql
   -- t is one Parquet row with arr = array(0, 1)
   SELECT CAST(IF(e = 1, 'bad', '1') AS INT) AS a
   FROM (SELECT explode(arr) AS e FROM t) LIMIT 1;
   
   -- spark.comet.exec.range.enabled=true
   SELECT sum(b) FROM (
     SELECT CAST(IF(id = 1, 'bad', '1') AS INT) + CAST(IF(id = 1, 'bad', '1') 
AS INT) AS b
     FROM range(0, 2, 1, 1) LIMIT 1
   ) q;
   ```
   
   Spark raises `CAST_INVALID_INPUT` for both, and Comet returns a result. A 
`/*+ BROADCAST(r) */` join under `LIMIT 1`, where the key matches `(0, '1')` 
and `(0, 'bad')` in `r`, does the same. With 
`spark.comet.exec.localLimit.enabled=false`, Comet raises the same error as 
Spark in all three.
   
   ### Expected behavior
   
   Comet raises the same error. Only add the cap where Spark also stops at the 
limit.
   
   ### Additional context
   
   This is the other direction from the "ANSI errors from rows skipped by 
LIMIT" case that #7147 tracks. #7480 also has Comet returning rows where Spark 
raises, but through filter and projection evaluation order, not the cap.
   


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