1fanwang opened a new pull request, #7741:
URL: https://github.com/apache/datafusion-comet/pull/7741
## Which issue does this PR close?
Closes #7675.
## Rationale for this change
With ANSI mode on, a query such as v IN (0, CAST(s AS INT)) fails in Comet
with CAST_INVALID_INPUT on rows where Spark returns a result. Spark's In
returns NULL for a NULL value without evaluating any candidate, and stops at
the first match. Comet still evaluated a candidate in two cases: when the value
was NULL, and when the value could itself fail, such as v + 1, where the lazy
rewrite gave up and the eager DataFusion in_list evaluated every candidate over
the batch.
## What changes are included in this PR?
The lazy IN rewrite now accepts any deterministic value, not only one that
cannot fail. The value is evaluated once per term, which is safe because a
deterministic value returns the same result each time, and a row that fails on
the value fails on the first term, as in Spark.
For a nullable value, the OR chain sits under CASE WHEN value IS NOT NULL,
so a row whose value is NULL gets NULL without any candidate running. NOT IN
negates the whole expression, so it is NULL on those rows too.
## How are these changes tested?
Three queries in logical_lazy_ansi.sql run against a table with rows (0,
'bad'), (NULL, 'bad') and (2, '2'): v IN (0, CAST(s AS INT)), the same with NOT
IN, and (v + 1) IN (1, CAST(s AS INT)). The suite compares Comet's answer with
Spark's.
Before the fix, on upstream main 0e8c133b03187ebe229b0a7e1c1d8560fd824204
with only the new SQL cases added, using Spark 4.1 and JDK 17:
```
./mvnw test -Dsuites="org.apache.comet.CometSqlFileTestSuite
logical_lazy_ansi" -Dtest=none
- sql-file: expressions/conditional/logical_lazy_ansi.sql *** FAILED *** (6
seconds, 744 milliseconds)
java.lang.RuntimeException: Error executing SQL 'SELECT v, v IN (0, CAST(s
AS INT)) FROM test_lazy_in' In SQL file
expressions/conditional/logical_lazy_ansi.sql:86, executing query:
SELECT v, v IN (0, CAST(s AS INT)) FROM test_lazy_in
Exception thrown while executing query in Comet:
...
== Physical Plan ==
*(1) CometColumnarToRow
+- CometProject [v#170, (v IN (0, CAST(s AS INT)))#179], [v#170, v#170 IN
(0,cast(s#171 as int)) AS (v IN (0, CAST(s AS INT)))#179]
+- CometNativeScan parquet
spark_catalog.default.test_lazy_in[v#170,s#171] ...
== Exception ==
org.apache.spark.SparkNumberFormatException: [CAST_INVALID_INPUT] The value
'bad' of the type "STRING" cannot be cast to "INT" because it is malformed.
Correct the value as per the syntax, or change its target type. Use `try_cast`
to tolerate malformed input and return NULL instead. SQLSTATE: 22018
```
After the fix, with the native library rebuilt by make core, the same SQL
file passes. To show the rows themselves, a temporary suite (source below, not
part of this PR) runs the three queries under ANSI, prints Comet's operators
and rows next to Spark's, and calls checkSparkAnswerAndOperator on each, which
asserts that the whole plan ran in Comet:
```
./mvnw test -Dsuites="org.apache.comet.Issue7675ReproSuite" -Dtest=none
REPRO SELECT v, v IN (0, CAST(s AS INT)) FROM test_lazy_in ORDER BY v
comet
(CometColumnarToRow,CometSort,CometExchange,CometProject,CometNativeScan
parquet spark_catalog.default.test_lazy_in): [null,null] [0,true] [2,true]
spark: [null,null] [0,true] [2,true]
REPRO SELECT v, v NOT IN (0, CAST(s AS INT)) FROM test_lazy_in ORDER BY v
comet
(CometColumnarToRow,CometSort,CometExchange,CometProject,CometNativeScan
parquet spark_catalog.default.test_lazy_in): [null,null] [0,false] [2,false]
spark: [null,null] [0,false] [2,false]
REPRO SELECT v, (v + 1) IN (1, CAST(s AS INT)) FROM test_lazy_in ORDER BY v
comet
(CometColumnarToRow,CometSort,CometExchange,CometProject,CometNativeScan
parquet spark_catalog.default.test_lazy_in): [null,null] [0,true] [2,false]
spark: [null,null] [0,true] [2,false]
```
Before the fix, the first query fails as shown above instead of returning
these rows.
<details>
<summary>Reproducer source: Issue7675ReproSuite.scala</summary>
```scala
package org.apache.comet
import org.apache.spark.sql.CometTestBase
import org.apache.spark.sql.internal.SQLConf
class Issue7675ReproSuite extends CometTestBase {
test("issue 7675 repro") {
withTable("test_lazy_in") {
sql("CREATE TABLE test_lazy_in(v int, s string) USING parquet")
sql("INSERT INTO test_lazy_in SELECT /*+ REPARTITION(1) */ * FROM
VALUES " +
"(0, 'bad'), (NULL, 'bad'), (2, '2') AS t(v, s)")
val queries = Seq(
"SELECT v, v IN (0, CAST(s AS INT)) FROM test_lazy_in ORDER BY v",
"SELECT v, v NOT IN (0, CAST(s AS INT)) FROM test_lazy_in ORDER BY
v",
"SELECT v, (v + 1) IN (1, CAST(s AS INT)) FROM test_lazy_in ORDER BY
v")
withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
queries.foreach { q =>
val df = sql(q)
val comet = df.collect().mkString(" ")
val ops = collect(df.queryExecution.executedPlan) {
case p if p.nodeName.startsWith("Comet") => p.nodeName
}.distinct.mkString(",")
var spark = ""
withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
spark = sql(q).collect().mkString(" ")
}
checkSparkAnswerAndOperator(sql(q))
// scalastyle:off println
println(s"REPRO $q\n comet ($ops): $comet\n spark: $spark")
// scalastyle:on println
}
}
}
}
}
```
</details>
--
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]