From 696aae412548b1a232c2507ffda6b8203d2967fe Mon Sep 17 00:00:00 2001 From: "wenjun.zhu" Date: Thu, 23 Aug 2018 11:39:10 +0800 Subject: [PATCH 1/3] expression involving NULL should be treated as FALSE, not TRUE. --- core/sql/exp/exp_bool.cpp | 2 +- core/sql/exp/exp_eval.cpp | 2 +- core/sql/regress/core/EXPECTED001.SB | 30 ++++++++++++++++++++++++++-- core/sql/regress/core/TEST001 | 8 ++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/core/sql/exp/exp_bool.cpp b/core/sql/exp/exp_bool.cpp index fa86efc6f6..037a8e1d36 100644 --- a/core/sql/exp/exp_bool.cpp +++ b/core/sql/exp/exp_bool.cpp @@ -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 { *(Lng32 *)op_data[0] = 0; setNextClause(branch_clause); diff --git a/core/sql/exp/exp_eval.cpp b/core/sql/exp/exp_eval.cpp index 00fbcc4da6..6ffdb253bf 100644 --- a/core/sql/exp/exp_eval.cpp +++ b/core/sql/exp/exp_eval.cpp @@ -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; diff --git a/core/sql/regress/core/EXPECTED001.SB b/core/sql/regress/core/EXPECTED001.SB index 8870be42c3..22163db453 100644 --- a/core/sql/regress/core/EXPECTED001.SB +++ b/core/sql/regress/core/EXPECTED001.SB @@ -14,7 +14,7 @@ >>invoke t001t1; -- Definition of Trafodion table TRAFODION.SCH.T001T1 --- Definition current Mon Mar 7 19:30:10 2016 +-- Definition current Mon Aug 27 15:59:38 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -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 Mon Aug 27 15:59:39 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -433,6 +433,32 @@ 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. +>>cqd pcode_opt_level 'on'; + +--- SQL operation complete. +>> +>>-- PCODE way +>>select case when cast(null as int) > 0 then 1/0 else 0 end from dual; + +(EXPR) +------- + + .0 + +--- 1 row(s) selected. +>> >>select a from t001t1 where null is null; A diff --git a/core/sql/regress/core/TEST001 b/core/sql/regress/core/TEST001 index cd0ec0ad06..f2d5b80c14 100755 --- a/core/sql/regress/core/TEST001 +++ b/core/sql/regress/core/TEST001 @@ -128,6 +128,14 @@ 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; +cqd pcode_opt_level 'on'; + +-- PCODE way +select case when cast(null as int) > 0 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) From e2ede6d8f7c392e3a48dfcd058516bdc4957e95b Mon Sep 17 00:00:00 2001 From: "wenjun.zhu" Date: Fri, 16 Nov 2018 17:26:27 +0800 Subject: [PATCH 2/3] Add some tests with NULL and composite predicates. --- core/sql/regress/core/EXPECTED001.SB | 70 ++++++++++++++++++++++++++-- core/sql/regress/core/TEST001 | 14 +++++- 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/core/sql/regress/core/EXPECTED001.SB b/core/sql/regress/core/EXPECTED001.SB index 22163db453..4f72bd9aba 100644 --- a/core/sql/regress/core/EXPECTED001.SB +++ b/core/sql/regress/core/EXPECTED001.SB @@ -14,7 +14,7 @@ >>invoke t001t1; -- Definition of Trafodion table TRAFODION.SCH.T001T1 --- Definition current Mon Aug 27 15:59:38 2018 +-- Definition current Fri Nov 16 17:12:39 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -32,7 +32,7 @@ >>invoke $$TEST_SCHEMA$$.t001ut1; -- Definition of Trafodion table TRAFODION.SCH.T001UT1 --- Definition current Mon Aug 27 15:59:39 2018 +-- Definition current Fri Nov 16 17:12:40 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -445,13 +445,75 @@ A .0 --- 1 row(s) selected. ->>cqd pcode_opt_level 'on'; +>> +>>select case when cast(null as int) > 0 and 2>1 then 1/0 else 0 end from dual; ---- SQL operation complete. +(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) ------- diff --git a/core/sql/regress/core/TEST001 b/core/sql/regress/core/TEST001 index f2d5b80c14..4ea54303bb 100755 --- a/core/sql/regress/core/TEST001 +++ b/core/sql/regress/core/TEST001 @@ -131,11 +131,23 @@ 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; -cqd pcode_opt_level 'on'; + +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) From c64986d5c4372fe60e172a29029aaf38f0126173 Mon Sep 17 00:00:00 2001 From: "wenjun.zhu" Date: Sat, 17 Nov 2018 15:12:43 +0800 Subject: [PATCH 3/3] Add a regression test on "= NULL" predicate. --- core/sql/regress/core/EXPECTED001.SB | 20 ++++++++++++++++++-- core/sql/regress/core/TEST001 | 4 ++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/core/sql/regress/core/EXPECTED001.SB b/core/sql/regress/core/EXPECTED001.SB index 4f72bd9aba..451a7f5c82 100644 --- a/core/sql/regress/core/EXPECTED001.SB +++ b/core/sql/regress/core/EXPECTED001.SB @@ -14,7 +14,7 @@ >>invoke t001t1; -- Definition of Trafodion table TRAFODION.SCH.T001T1 --- Definition current Fri Nov 16 17:12:39 2018 +-- Definition current Sat Nov 17 15:05:31 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -32,7 +32,7 @@ >>invoke $$TEST_SCHEMA$$.t001ut1; -- Definition of Trafodion table TRAFODION.SCH.T001UT1 --- Definition current Fri Nov 16 17:12:40 2018 +-- Definition current Sat Nov 17 15:05:32 2018 ( SYSKEY LARGEINT NO DEFAULT NOT NULL NOT DROPPABLE @@ -535,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 diff --git a/core/sql/regress/core/TEST001 b/core/sql/regress/core/TEST001 index 4ea54303bb..a67253e2c0 100755 --- a/core/sql/regress/core/TEST001 +++ b/core/sql/regress/core/TEST001 @@ -151,6 +151,10 @@ 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;