← Back to Pull requests

fix: skip IN candidates for a NULL or fallible value under ANSI #7741

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 21, 2026 20:12 UTC
Author

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>

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>

Diff: 650 lines (+487 / -163) across 8 files Reviewer: comphead Review duration: 13091m
All Vouch review confidence checks passed
Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
0.52
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
46%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.388
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #7741"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 46.7% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 52% (MEDIUM) · Higher = larger blast radius
52%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 46.7% · Lower = rushed or shallow review
46.7%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 27.7% · Safe to merge
27.7%