Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

[TRAFODION-3190] expression involving NULL should be treated as FALSE, not TRUE. #1741

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/sql/exp/exp_bool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ ex_expr::exp_return_type ex_branch_clause::eval(char *op_data[],
switch (getOperType())
{
case ITM_AND:
if (*(Lng32 *)op_data[1] == 0)
if (*(Lng32 *)op_data[1] == 0 || *(Lng32 *)op_data[1] == -1) // null treated as false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this is correct. Consider the query, "select a from t1x where not(b = 0 and c = 0)". When B and C are both null, both equal predicates evaluate to null, and the AND evaluates to null. The NOT then also evaluates to null. The WHERE clause should treat the result of the NOT as false. But with this fix, the result of the AND will be false, making the NOT true. There needs to be three cases here for ITM_AND: If the first operand is false, then the AND is false. If the first operand is true, then the result is the second operand. If the first operand is null, then if the second operand is false, the result is false otherwise the result is null. Similar logic needs to be added to the ITM_OR case.

{
*(Lng32 *)op_data[0] = 0;
setNextClause(branch_clause);
Expand Down
2 changes: 1 addition & 1 deletion core/sql/exp/exp_eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6600,7 +6600,7 @@ ex_expr::exp_return_type ex_expr::evalPCode(PCodeBinary* pCode32,
PTR_DEF_ASSIGN(Int32, srcPtr, 2 + 2 * PCODEBINARIES_PER_PTR );
Int32 src = *srcPtr;

if (src == 0)
if (src == 0 || src == -1) // null(i.e. -1) means false
{
DEF_ASSIGN_PTR(Int64, branchOffset, 0 );
*tgtPtr = 0;
Expand Down
108 changes: 106 additions & 2 deletions core/sql/regress/core/EXPECTED001.SB
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
>>invoke t001t1;

-- Definition of Trafodion table TRAFODION.SCH.T001T1
-- Definition current Mon Mar 7 19:30:10 2016
-- Definition current Sat Nov 17 15:05:31 2018

(
SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE
Expand All @@ -32,7 +32,7 @@
>>invoke $$TEST_SCHEMA$$.t001ut1;

-- Definition of Trafodion table TRAFODION.SCH.T001UT1
-- Definition current Mon Mar 7 19:30:10 2016
-- Definition current Sat Nov 17 15:05:32 2018

(
SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE
Expand Down Expand Up @@ -433,6 +433,94 @@ A
--- 2 row(s) selected.
>> -- ok
>>
>>-- clause way
>>cqd pcode_opt_level 'off';

--- SQL operation complete.
>>select case when cast(null as int) > 0 then 1/0 else 0 end from dual;

(EXPR)
-------

.0

--- 1 row(s) selected.
>>
>>select case when cast(null as int) > 0 and 2>1 then 1/0 else 0 end from dual;

(EXPR)
-------

.0

--- 1 row(s) selected.
>>select case when cast(null as int) > 0 or 2>1 then 1/0 else 0 end from dual;

*** ERROR[8419] An arithmetic expression attempted a division by zero.

--- 0 row(s) selected.
>>
>>select case when cast(null as int) > 0 and 2>3 then 1/0 else 0 end from dual;

(EXPR)
-------

.0

--- 1 row(s) selected.
>>select case when cast(null as int) > 0 or 2>3 then 1/0 else 0 end from dual;

(EXPR)
-------

.0

--- 1 row(s) selected.
>>
>>-- PCODE way
>>cqd pcode_opt_level 'on';

--- SQL operation complete.
>>select case when cast(null as int) > 0 then 1/0 else 0 end from dual;

(EXPR)
-------

.0

--- 1 row(s) selected.
>>
>>select case when cast(null as int) > 0 and 2>1 then 1/0 else 0 end from dual;

(EXPR)
-------

.0

--- 1 row(s) selected.
>>select case when cast(null as int) > 0 or 2>1 then 1/0 else 0 end from dual;

*** ERROR[8419] An arithmetic expression attempted a division by zero.

--- 0 row(s) selected.
>>
>>select case when cast(null as int) > 0 and 2>3 then 1/0 else 0 end from dual;

(EXPR)
-------

.0

--- 1 row(s) selected.
>>select case when cast(null as int) > 0 or 2>3 then 1/0 else 0 end from dual;

(EXPR)
-------

.0

--- 1 row(s) selected.
>>
>>select a from t001t1 where null is null;

A
Expand All @@ -447,6 +535,22 @@ A

--- 0 row(s) selected.
>> -- ok (0 rows)
>>
>>-- = NULL is not ANSI
>>select case when 3 is null then 1/0 else 0 end from dual;

(EXPR)
-------

.0

--- 1 row(s) selected.
>>select case when 3 = null then 1/0 else 0 end from dual;

*** ERROR[4099] A NULL operand is not allowed in predicate (3 = NULL).

*** ERROR[8822] The statement was not prepared.

>>
>>
>>-- PARAM queries
Expand Down
24 changes: 24 additions & 0 deletions core/sql/regress/core/TEST001
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,33 @@ select (case a when null then 1 else 2 end) from t001t1;
select (case a when 1 then 1 when null then 2 else 3 end) from t001t1;
select (case a when 1 then 1 when 2 then 2 else null end) from t001t1; -- ok

-- clause way
cqd pcode_opt_level 'off';
select case when cast(null as int) > 0 then 1/0 else 0 end from dual;

select case when cast(null as int) > 0 and 2>1 then 1/0 else 0 end from dual;
select case when cast(null as int) > 0 or 2>1 then 1/0 else 0 end from dual;

select case when cast(null as int) > 0 and 2>3 then 1/0 else 0 end from dual;
select case when cast(null as int) > 0 or 2>3 then 1/0 else 0 end from dual;

-- PCODE way
cqd pcode_opt_level 'on';
select case when cast(null as int) > 0 then 1/0 else 0 end from dual;

select case when cast(null as int) > 0 and 2>1 then 1/0 else 0 end from dual;
select case when cast(null as int) > 0 or 2>1 then 1/0 else 0 end from dual;

select case when cast(null as int) > 0 and 2>3 then 1/0 else 0 end from dual;
select case when cast(null as int) > 0 or 2>3 then 1/0 else 0 end from dual;

select a from t001t1 where null is null; -- ok
select a from t001t1 where null is not null; -- ok (0 rows)

-- = NULL is not ANSI
select case when 3 is null then 1/0 else 0 end from dual;
select case when 3 = null then 1/0 else 0 end from dual;


-- PARAM queries
set param ?p 10;
Expand Down