Skip to content

Handle NULL values in SQL filters, projections and joins - #904

Open
AarushShetty346 wants to merge 2 commits into
apache:mainfrom
AarushShetty346:fix-sql-nulls-everywhere
Open

AarushShetty346 wants to merge 2 commits into
apache:mainfrom
AarushShetty346:fix-sql-nulls-everywhere

Conversation

@AarushShetty346

Copy link
Copy Markdown

Follow-up to #893, as Juri Petersen asked there: NULL handling for the other SQL operations. Related: #890, #891. Based on #893, so it includes that commit until #893 is merged.

What changes were proposed in this pull request?

  • Filters (FilterPredicateImpl): comparisons, LIKE, NOT, +/- and CAST give NULL (unknown) for a NULL operand instead of an NPE. AND/OR use three-valued logic. SEARCH uses the Sarg's NULL result (e.g. NULL AS TRUE for x IN (1, 3) OR x IS NULL), kept in a small SargValue record. A row passes only if the condition is TRUE.
  • Projections (ProjectMapFuncImpl): + - * / with a NULL operand give NULL.
  • Joins: a new JoinKeyNotNullFilter drops records with a NULL key before the join (only when a key column is nullable), so NULL never matches NULL.
  • Scans: no change. Empty CSV fields of typed columns already become NULL; string columns stay "", as in Calcite's file adapter.

On exampleNull.csv (col1 = 1, NULL, 3, NULL):

Query Before After
WHERE col1 > 1 NPE 1 row
WHERE col1 <> 1 3 rows, incl. NULLs 1 row
WHERE col1 > 1 OR col2 > 15 NPE 2 rows
SELECT col1 + col2 NPE NULL for NULL rows
a JOIN b ON a.col1 = b.col1 6 rows, NULLs matched 2 rows

How was this patch tested?

10 new tests in SqlToWayangRelTest with a new exampleNull.csv: scans, filters, projections and joins (one and two keys) on Java, filters, projections and joins on Spark, plus a unit test of FilterPredicateImpl for NULL strings in LIKE and NOT over unknown. 9 of them fail without this change (the scan test passes either way). All 44 wayang-api-sql tests pass, and mvn verify on the module (RAT included) succeeds.

Not in this PR

SEARCH only checks the span of the Sarg, so IN (1, 5) also matches 3 once FILTER_INTO_JOIN makes it a Sarg, and NOT IN then fails with "range unbounded". That isn't about NULLs, so I can open a separate issue.

Follow-up to apache#893, as asked in its review. The SQL API now follows SQL
NULL semantics outside aggregates as well:

- Filters: comparisons, LIKE, NOT, PLUS/MINUS and CAST give NULL
  (unknown) for a NULL operand, AND/OR use three-valued logic, SEARCH
  uses the Sarg's NULL result (e.g. NULL AS TRUE), and a WHERE keeps a
  row only if its condition is TRUE.
- Projections: arithmetic with a NULL operand gives NULL.
- Joins: rows with a NULL key are dropped before the join when a key
  column is nullable, so NULL never matches NULL.
- Scans already read empty CSV fields of typed columns as NULL; a new
  test covers that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant