dwsmith1983 opened a new issue, #7814: URL: https://github.com/apache/datafusion-comet/issues/7814
### Describe the bug With ANSI on, subexpression elimination on and `spark.sql.codegen.factoryMode=NO_CODEGEN`, Comet raises `CAST_INVALID_INPUT` for map lookups whose key cast fails only on rows where the map is NULL. Spark returns NULL for those rows. Under NO_CODEGEN, Spark runs projections through `InterpretedUnsafeProjection` and `InterpretedMutableProjection`, whose `SubExprEvaluationRuntime` evaluates an eliminated subexpression only when an expression asks for it. A lookup on a NULL map never asks for its key, so the cast never runs. Comet's native null short-circuit (`canSkipEvaluation` and `canShortCircuitNulls` in `QueryPlanSerde`) assumes Spark evaluates a shared subexpression on every row, as its generated code does. Since `CAST(s AS INT)` is shared by both lookups in the project, and an aggregate's inputs always count as shared, native execution evaluates the key on every row and raises. Found in #5867, while following up on https://github.com/apache/datafusion-comet/pull/5867#discussion_r4735095596. That PR fixes the same evaluation order for the codegen dispatcher, not for the native short-circuit. ### Steps to reproduce Spark 4.1, main at 1c0eb0b8b, as a SQL file test: ```sql -- Config: spark.sql.ansi.enabled=true -- Config: spark.sql.subexpressionElimination.enabled=true -- Config: spark.sql.codegen.wholeStage=false -- Config: spark.sql.codegen.factoryMode=NO_CODEGEN statement CREATE TABLE t(m map<int, int>, m2 map<int, int>, s string) USING parquet statement INSERT INTO t SELECT /*+ COALESCE(1) */ * FROM VALUES (NULL, NULL, 'bad'), (map(1, 10, 2, 20), map(2, 5), '2'), (map(3, 30), map(4, 6), '4') AS v(m, m2, s) query SELECT m[CAST(s AS INT)], m2[CAST(s AS INT)] FROM t query SELECT max(m[CAST(s AS INT)]), max(m2[CAST(s AS INT)]) FROM t ``` Both queries fail in Comet with: ``` org.apache.spark.SparkNumberFormatException: [CAST_INVALID_INPUT] The value 'bad' of the type "STRING" cannot be cast to "INT" because it is malformed. at org.apache.comet.CometExecIterator.getNextBatch(CometExecIterator.scala:288) ``` The project runs as `CometProject` over `CometNativeScan`. The aggregate fails in the partial `CometHashAggregate` while `CometNativeShuffleWriter` drains it. ### Expected behavior The same results as Spark with Comet disabled: - `SELECT m[CAST(s AS INT)], m2[CAST(s AS INT)] FROM t`: `[null,null], [20,5], [null,6]` - `SELECT max(m[CAST(s AS INT)]), max(m2[CAST(s AS INT)]) FROM t`: `[20,6]` ### Additional context With generated code (the default FALLBACK mode), Spark evaluates eliminated subexpressions eagerly and raises on these queries too, so only NO_CODEGEN, or a projection that falls back to the interpreter after a compile failure, is affected. One option is to treat nothing as shared for `canSkipEvaluation` when the factory mode is NO_CODEGEN. -- 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]
