-
Notifications
You must be signed in to change notification settings - Fork 690
Fix NULL comparison in IN and NOT IN operators to follow SQL semantics #2315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
6
commits into
develop
Choose a base branch
from
copilot/fix-18397742-25780780-20e1a003-180b-4cd2-bdb8-314a0de54479
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fa21e7a
Initial plan
Copilot 06a51a1
Implement SQL-compliant NULL handling for IN and NOT IN operators
Copilot 13261b4
Fix context parameter consistency in NOT IN operator
Copilot 0f51cdc
Merge branch 'develop' into copilot/fix-18397742-25780780-20e1a003-18…
mathiasrw 7f5f328
Fix NULL handling in IN/NOT IN after merge with develop branch cachin…
Copilot 386672b
Simplify NULL handling with utility functions for IN/NOT IN operators
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| if (typeof exports === 'object') { | ||
| var assert = require('assert'); | ||
| var alasql = require('..'); | ||
| } | ||
|
|
||
| describe('Test 1414 - NOT IN with NULL values should follow SQL semantics', function () { | ||
| const test = '1414'; | ||
|
|
||
| before(function () { | ||
| alasql('create database test' + test); | ||
| alasql('use test' + test); | ||
| }); | ||
|
|
||
| after(function () { | ||
| alasql('drop database test' + test); | ||
| }); | ||
|
|
||
| it('A) NOT IN with NULL in subquery should return empty result', function () { | ||
| alasql('CREATE TABLE R (a number)'); | ||
| alasql('CREATE TABLE S (b number)'); | ||
| alasql.tables.R.data = [{a: 1}, {a: null}]; | ||
| alasql.tables.S.data = [{b: null}]; | ||
| var res = alasql('select a from R where a not in (select b from S)'); | ||
| // When subquery contains NULL, NOT IN should return empty result | ||
| // because comparison with NULL is UNKNOWN, and NOT IN UNKNOWN = UNKNOWN (false in WHERE) | ||
| assert.deepEqual(res, []); | ||
| }); | ||
|
|
||
| it('B) NOT IN with NULL value on left side', function () { | ||
| alasql('DROP TABLE IF EXISTS R'); | ||
| alasql('DROP TABLE IF EXISTS S'); | ||
| alasql('CREATE TABLE R (a number)'); | ||
| alasql('CREATE TABLE S (b number)'); | ||
| alasql.tables.R.data = [{a: null}, {a: 2}]; | ||
| alasql.tables.S.data = [{b: 1}]; | ||
| var res = alasql('select a from R where a not in (select b from S)'); | ||
| // NULL NOT IN (1) should evaluate to UNKNOWN (excluded from WHERE result) | ||
| // 2 NOT IN (1) should be TRUE (included) | ||
| assert.deepEqual(res, [{a: 2}]); | ||
| }); | ||
|
|
||
| it('C) NOT IN without NULL should work normally', function () { | ||
| alasql('DROP TABLE IF EXISTS R'); | ||
| alasql('DROP TABLE IF EXISTS S'); | ||
| alasql('CREATE TABLE R (a number)'); | ||
| alasql('CREATE TABLE S (b number)'); | ||
| alasql.tables.R.data = [{a: 1}, {a: 2}, {a: 3}]; | ||
| alasql.tables.S.data = [{b: 2}]; | ||
| var res = alasql('select a from R where a not in (select b from S)'); | ||
| // 1 NOT IN (2) = TRUE, 2 NOT IN (2) = FALSE, 3 NOT IN (2) = TRUE | ||
| assert.deepEqual(res, [{a: 1}, {a: 3}]); | ||
| }); | ||
|
|
||
| it('D) NOT IN with multiple values including NULL', function () { | ||
| alasql('DROP TABLE IF EXISTS R'); | ||
| alasql('DROP TABLE IF EXISTS S'); | ||
| alasql('CREATE TABLE R (a number)'); | ||
| alasql('CREATE TABLE S (b number)'); | ||
| alasql.tables.R.data = [{a: 1}, {a: 2}, {a: 3}]; | ||
| alasql.tables.S.data = [{b: 2}, {b: null}]; | ||
| var res = alasql('select a from R where a not in (select b from S)'); | ||
| // When subquery contains NULL, all comparisons are UNKNOWN | ||
| assert.deepEqual(res, []); | ||
| }); | ||
|
|
||
| it('E) NOT IN with array literal containing NULL', function () { | ||
| alasql('DROP TABLE IF EXISTS R'); | ||
| alasql('CREATE TABLE R (a number)'); | ||
| alasql.tables.R.data = [{a: 1}, {a: 2}, {a: 3}]; | ||
| var res = alasql('select a from R where a not in (2, NULL)'); | ||
| // When list contains NULL, all NOT IN comparisons are UNKNOWN | ||
| assert.deepEqual(res, []); | ||
| }); | ||
|
|
||
| it('F) IN with NULL in subquery', function () { | ||
| alasql('DROP TABLE IF EXISTS R'); | ||
| alasql('DROP TABLE IF EXISTS S'); | ||
| alasql('CREATE TABLE R (a number)'); | ||
| alasql('CREATE TABLE S (b number)'); | ||
| alasql.tables.R.data = [{a: 1}, {a: 2}]; | ||
| alasql.tables.S.data = [{b: 1}, {b: null}]; | ||
| var res = alasql('select a from R where a in (select b from S)'); | ||
| // 1 IN (1, NULL) = TRUE, 2 IN (1, NULL) = UNKNOWN (excluded) | ||
| assert.deepEqual(res, [{a: 1}]); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like way way too much code to make sure we return something correct for in and not in operatoins. Please rethink how this could be implemented in a much less invasive way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've simplified the implementation significantly. The new approach:
Changes made (commit 386672b):
sqlInCheckandsqlNotInCheck(37 lines in src/15utility.js)Result:
The utilities handle SQL NULL semantics in a centralized, maintainable way rather than duplicating complex logic inline.