Skip to content

ANSI: comparisons evaluate the right operand on rows where the left one is NULL #7468

Open

Description

@tlfua

Describe the bug

Under ANSI mode, Comet evaluates the right side of =, <, <=, >, >= on rows where the left side is NULL. Spark skips the right side on those rows: EqualTo and the ordering comparisons use defineCodeGen, which goes through nullSafeCodeGen, so the right operand is only evaluated where the left one is not NULL. When the right side can fail, such as an ANSI cast of a malformed string, Comet raises an error that Spark never raises.

This is the same shape as #7246, which covers arithmetic and concat and is being fixed in #7456. It was raised in review there by @comphead: #7456 (review)

Steps to reproduce

With spark.sql.ansi.enabled=true and spark.sql.adaptive.enabled=false (Spark 4.1.3, Comet built from #7456):

CREATE TABLE cmp(id int, n int, txt string) USING parquet;
INSERT INTO cmp SELECT /*+ REPARTITION(1) */ * FROM VALUES
  (1, CAST(NULL AS INT), 'bad'), (2, 2, '2') AS t(id, n, txt);

SELECT id, n < CAST(txt AS INT) FROM cmp;

Comet fails in CometProject with:

[CAST_INVALID_INPUT] The value 'bad' of the type "STRING" cannot be cast to "INT" because it is malformed. ...

Expected behavior

Same as Spark: (1, NULL), (2, false). The cast is never evaluated for row 1 because n is NULL.

Additional context

SELECT id FROM cmp WHERE n = CAST(txt AS INT) did not fail in the same run, presumably because the inferred isnotnull(n) filter and Comet's lazy AND skip the row first. A projection, or any position where no such filter is inferred, does fail.

A fix could mark the comparisons the same way #7456 marks arithmetic, with NullShortCircuit over the comparison when the right side can fail.

Willingness to contribute

I can contribute a fix for this bug independently

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:expressionsExpression evaluationbugSomething isn't workingpriority:mediumFunctional bugs, performance regressions, broken features

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions