diff --git a/mysql-test/suite/tianmu/r/select_from.result b/mysql-test/suite/tianmu/r/select_from.result index f63b23323..3685d505f 100644 --- a/mysql-test/suite/tianmu/r/select_from.result +++ b/mysql-test/suite/tianmu/r/select_from.result @@ -208,4 +208,542 @@ q10 9 q10 10 drop table t2_test; drop table t1_test; +create table tb(a bigint) engine=tianmu; +insert into tb values(-9223372036854775806), (-922337203685477580), (-55), (-22), (-10), (-4), (0), (2), (6), (9223372036854775807); +select * from tb; +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +-10 +-4 +0 +2 +6 +9223372036854775807 +select * from tb where a < -10; +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +select * from tb where a > 4567890123456789; +a +9223372036854775807 +select * from tb where a > 0; +a +2 +6 +9223372036854775807 +select * from tb where a between -9223372036854775806 and 0; +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +-10 +-4 +0 +select * from tb where a between -22 and 7; +a +-22 +-10 +-4 +0 +2 +6 +select * from tb where a not in (3); +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +-10 +-4 +0 +2 +6 +9223372036854775807 +select * from tb where a not in (-3,3); +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +-10 +-4 +0 +2 +6 +9223372036854775807 +select * from tb where a not in (-3); +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +-10 +-4 +0 +2 +6 +9223372036854775807 +select * from tb where a in (-3); +a +select * from tb where a in (-3, 3); +a +select * from tb where a is null; +a +select * from tb where a is not null; +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +-10 +-4 +0 +2 +6 +9223372036854775807 +select * from tb where a in (not null); +a +select * from tb where a not in (null); +a +insert into tb values(null), (null); +select * from tb; +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +-10 +-4 +0 +2 +6 +9223372036854775807 +NULL +NULL +select * from tb where a < -10; +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +select * from tb where a > 4567890123456789; +a +9223372036854775807 +select * from tb where a > 0; +a +2 +6 +9223372036854775807 +select * from tb where a between -9223372036854775806 and 0; +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +-10 +-4 +0 +select * from tb where a between -22 and 7; +a +-22 +-10 +-4 +0 +2 +6 +select * from tb where a not in (3); +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +-10 +-4 +0 +2 +6 +9223372036854775807 +select * from tb where a not in (-3,3); +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +-10 +-4 +0 +2 +6 +9223372036854775807 +select * from tb where a not in (-3); +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +-10 +-4 +0 +2 +6 +9223372036854775807 +select * from tb where a in (-3); +a +select * from tb where a in (-3, 3); +a +select * from tb where a is null; +a +NULL +NULL +select * from tb where a is not null; +a +-9223372036854775806 +-922337203685477580 +-55 +-22 +-10 +-4 +0 +2 +6 +9223372036854775807 +select * from tb where a in (not null); +a +select * from tb where a not in (null); +a +drop table tb; +create table tb(a bigint) engine=tianmu; +insert into tb values(-55), (-22), (-10), (-4), (0), (2), (6); +select * from tb; +a +-55 +-22 +-10 +-4 +0 +2 +6 +select * from tb where a < -55; +a +select * from tb where a < 0; +a +-55 +-22 +-10 +-4 +select * from tb where a > 6; +a +select * from tb where a > 0; +a +2 +6 +select * from tb where a between -9223372036854775806 and 0; +a +-55 +-22 +-10 +-4 +0 +select * from tb where a between -22 and 7; +a +-22 +-10 +-4 +0 +2 +6 +select * from tb where a between 22 and 33; +a +select * from tb where a between -555 and -56; +a +select * from tb where a not in (3333); +a +-55 +-22 +-10 +-4 +0 +2 +6 +select * from tb where a not in (-3,3333); +a +-55 +-22 +-10 +-4 +0 +2 +6 +select * from tb where a not in (-33333); +a +-55 +-22 +-10 +-4 +0 +2 +6 +select * from tb where a in (-3); +a +select * from tb where a in (-3, 3); +a +select * from tb where a is null; +a +select * from tb where a is not null; +a +-55 +-22 +-10 +-4 +0 +2 +6 +select * from tb where a in (not null); +a +select * from tb where a not in (null); +a +insert into tb values(null), (null); +select * from tb; +a +-55 +-22 +-10 +-4 +0 +2 +6 +NULL +NULL +select * from tb where a < -55; +a +select * from tb where a < 0; +a +-55 +-22 +-10 +-4 +select * from tb where a > 6; +a +select * from tb where a > 0; +a +2 +6 +select * from tb where a between -9223372036854775806 and 0; +a +-55 +-22 +-10 +-4 +0 +select * from tb where a between -22 and 7; +a +-22 +-10 +-4 +0 +2 +6 +select * from tb where a between 22 and 33; +a +select * from tb where a between -555 and -56; +a +select * from tb where a not in (3333); +a +-55 +-22 +-10 +-4 +0 +2 +6 +select * from tb where a not in (-3,3333); +a +-55 +-22 +-10 +-4 +0 +2 +6 +select * from tb where a not in (-33333); +a +-55 +-22 +-10 +-4 +0 +2 +6 +select * from tb where a in (-3); +a +select * from tb where a in (-3, 3); +a +select * from tb where a is null; +a +NULL +NULL +select * from tb where a is not null; +a +-55 +-22 +-10 +-4 +0 +2 +6 +select * from tb where a in (not null); +a +select * from tb where a not in (null); +a +drop table tb; +create table tb(a bigint) engine=tianmu; +insert into tb values(0), (0), (0); +select * from tb; +a +0 +0 +0 +select * from tb where a < -55; +a +select * from tb where a > 0; +a +select * from tb where a > -1; +a +0 +0 +0 +select * from tb where a between -3 and -1; +a +select * from tb where a between 1 and 3; +a +select * from tb where a between 0 and 0; +a +0 +0 +0 +select * from tb where a between -1 and 3; +a +0 +0 +0 +select * from tb where a between -1 and 0; +a +0 +0 +0 +select * from tb where a between 0 and 3; +a +0 +0 +0 +select * from tb where a not in (3); +a +0 +0 +0 +select * from tb where a not in (-3,3); +a +0 +0 +0 +select * from tb where a not in (-125); +a +0 +0 +0 +select * from tb where a in (-3); +a +select * from tb where a in (-3, 3); +a +select * from tb where a is null; +a +select * from tb where a is not null; +a +0 +0 +0 +select * from tb where a in (not null); +a +select * from tb where a not in (null); +a +insert into tb values(null), (null); +select * from tb; +a +0 +0 +0 +NULL +NULL +select * from tb where a < -55; +a +select * from tb where a > 0; +a +select * from tb where a > -1; +a +0 +0 +0 +select * from tb where a between -3 and -1; +a +select * from tb where a between 1 and 3; +a +select * from tb where a between 0 and 0; +a +0 +0 +0 +select * from tb where a between -1 and 3; +a +0 +0 +0 +select * from tb where a between -1 and 0; +a +0 +0 +0 +select * from tb where a between 0 and 3; +a +0 +0 +0 +select * from tb where a not in (3); +a +0 +0 +0 +select * from tb where a not in (-3,3); +a +0 +0 +0 +select * from tb where a not in (-125); +a +0 +0 +0 +select * from tb where a in (-3); +a +select * from tb where a in (-3, 3); +a +select * from tb where a is null; +a +NULL +NULL +select * from tb where a is not null; +a +0 +0 +0 +select * from tb where a in (not null); +a +select * from tb where a not in (null); +a +drop table tb; DROP DATABASE select_from_test; diff --git a/mysql-test/suite/tianmu/t/select_from.test b/mysql-test/suite/tianmu/t/select_from.test index 6d11433cc..9a9bcf182 100644 --- a/mysql-test/suite/tianmu/t/select_from.test +++ b/mysql-test/suite/tianmu/t/select_from.test @@ -116,4 +116,137 @@ vc1 = 9 or bi2 = 4; drop table t2_test; drop table t1_test; +# fix between ... and ... : https://github.com/stoneatom/stonedb/issues/1332 +# test values with boundary, with not null +create table tb(a bigint) engine=tianmu; +insert into tb values(-9223372036854775806), (-922337203685477580), (-55), (-22), (-10), (-4), (0), (2), (6), (9223372036854775807); +select * from tb; +select * from tb where a < -10; +select * from tb where a > 4567890123456789; +select * from tb where a > 0; +select * from tb where a between -9223372036854775806 and 0; +select * from tb where a between -22 and 7; +select * from tb where a not in (3); +select * from tb where a not in (-3,3); +select * from tb where a not in (-3); +select * from tb where a in (-3); +select * from tb where a in (-3, 3); +select * from tb where a is null; # emtpy +select * from tb where a is not null; # emtpy +select * from tb where a in (not null); # emtpy +select * from tb where a not in (null); # emtpy + +# test with null +insert into tb values(null), (null); +select * from tb; +select * from tb where a < -10; +select * from tb where a > 4567890123456789; +select * from tb where a > 0; +select * from tb where a between -9223372036854775806 and 0; +select * from tb where a between -22 and 7; +select * from tb where a not in (3); +select * from tb where a not in (-3,3); +select * from tb where a not in (-3); +select * from tb where a in (-3); +select * from tb where a in (-3, 3); +select * from tb where a is null; # emtpy +select * from tb where a is not null; # emtpy +select * from tb where a in (not null); # emtpy +select * from tb where a not in (null); # emtpy + +drop table tb; + +# test normal values, with not null +create table tb(a bigint) engine=tianmu; +insert into tb values(-55), (-22), (-10), (-4), (0), (2), (6); +select * from tb; +select * from tb where a < -55; +select * from tb where a < 0; +select * from tb where a > 6; +select * from tb where a > 0; +select * from tb where a between -9223372036854775806 and 0; +select * from tb where a between -22 and 7; +select * from tb where a between 22 and 33; +select * from tb where a between -555 and -56; +select * from tb where a not in (3333); +select * from tb where a not in (-3,3333); +select * from tb where a not in (-33333); +select * from tb where a in (-3); +select * from tb where a in (-3, 3); +select * from tb where a is null; # emtpy +select * from tb where a is not null; # emtpy +select * from tb where a in (not null); # emtpy +select * from tb where a not in (null); # emtpy + +# test with null +insert into tb values(null), (null); +select * from tb; +select * from tb where a < -55; +select * from tb where a < 0; +select * from tb where a > 6; +select * from tb where a > 0; +select * from tb where a between -9223372036854775806 and 0; +select * from tb where a between -22 and 7; +select * from tb where a between 22 and 33; +select * from tb where a between -555 and -56; +select * from tb where a not in (3333); +select * from tb where a not in (-3,3333); +select * from tb where a not in (-33333); +select * from tb where a in (-3); +select * from tb where a in (-3, 3); +select * from tb where a is null; # emtpy +select * from tb where a is not null; # emtpy +select * from tb where a in (not null); # emtpy +select * from tb where a not in (null); # emtpy + +drop table tb; + +# test max=min, with not null +create table tb(a bigint) engine=tianmu; +insert into tb values(0), (0), (0); +select * from tb; +select * from tb where a < -55; +select * from tb where a > 0; +select * from tb where a > -1; +select * from tb where a between -3 and -1; +select * from tb where a between 1 and 3; +select * from tb where a between 0 and 0; +select * from tb where a between -1 and 3; +select * from tb where a between -1 and 0; +select * from tb where a between 0 and 3; +select * from tb where a not in (3); +select * from tb where a not in (-3,3); +select * from tb where a not in (-125); +select * from tb where a in (-3); +select * from tb where a in (-3, 3); +select * from tb where a is null; # emtpy +select * from tb where a is not null; # emtpy +select * from tb where a in (not null); # emtpy +select * from tb where a not in (null); # emtpy + +# test with null +insert into tb values(null), (null); +select * from tb; +select * from tb where a < -55; +select * from tb where a > 0; +select * from tb where a > -1; +select * from tb where a between -3 and -1; +select * from tb where a between 1 and 3; +select * from tb where a between 0 and 0; +select * from tb where a between -1 and 3; +select * from tb where a between -1 and 0; +select * from tb where a between 0 and 3; +select * from tb where a not in (3); +select * from tb where a not in (-3,3); +select * from tb where a not in (-125); +select * from tb where a in (-3); +select * from tb where a in (-3, 3); +select * from tb where a is null; # emtpy +select * from tb where a is not null; # emtpy +select * from tb where a in (not null); # emtpy +select * from tb where a not in (null); # emtpy + +drop table tb; + + DROP DATABASE select_from_test; diff --git a/storage/tianmu/core/tianmu_attr_exqp.cpp b/storage/tianmu/core/tianmu_attr_exqp.cpp index aa826a4f7..4627c5e8c 100644 --- a/storage/tianmu/core/tianmu_attr_exqp.cpp +++ b/storage/tianmu/core/tianmu_attr_exqp.cpp @@ -875,19 +875,29 @@ void TianmuAttr::EvaluatePack_BetweenInt(MIUpdatingIterator &mit, int dim, Descr mit.NextPackrow(); return; } - int64_t pv1 = d.val1.vc->GetValueInt64(mit); - int64_t pv2 = d.val2.vc->GetValueInt64(mit); + int64_t pv1 = d.val1.vc->GetValueInt64(mit); // e.g.: between pv1 and pv2 + int64_t pv2 = d.val2.vc->GetValueInt64(mit); // e.g.: between pv1 and pv2 + uint64_t upv1, upv2; // used uint64_t to store level 2 encode, ranges from [0, uint64_max] + // Only used when rough check = RS_ALL(no numeric data matchs), but table has null, rough check will be reset RS_SOME, + // so function EvaluatePack_BetweenInt() still be called to filter nulls, but no data matchs. This is a tmp solution, + // we may find a better way to deal with this case. Used with upv2. + bool empty_flag = true; int64_t local_min = dpn.min_i; int64_t local_max = dpn.max_i; - if (pv1 != common::MINUS_INF_64) - pv1 = pv1 - local_min; + if (common::MINUS_INF_64 == pv1 || pv1 <= local_min) + // we reserve common::MINUS_INF_64 case for "where a < 3" tranform to "[MINUS_INF_64, 2]". + upv1 = 0; else - pv1 = 0; + upv1 = pv1 - local_min; - if (pv2 != common::PLUS_INF_64) // encode from 0-level to 2-level - pv2 = pv2 - local_min; + if (common::PLUS_INF_64 == pv2 || pv2 >= local_max) + // case: local_min < local_max <= pv2, we also reserve PLUS_INF_64 for special case like pv1 + upv2 = local_max - local_min; else - pv2 = local_max - local_min; + // case: local_min <= pv2 < local_max or pv2 < local_min(see comment empty_flag) + upv2 = pv2 - local_min; + empty_flag = (pv2 < local_min) ? true : false; // no data matchs and upv2 get a invalid value + if (local_min != local_max) { auto p = get_packN(pack); auto filter = mit.GetMultiIndex()->GetFilter(dim); @@ -898,8 +908,8 @@ void TianmuAttr::EvaluatePack_BetweenInt(MIUpdatingIterator &mit, int dim, Descr if (d.op == common::Operator::O_BETWEEN && !mit.NullsPossibleInPack(dim) && dpn.numOfNulls == 0) { // easy and fast case - no "if"s for (uint32_t n = 0; n < dpn.numOfRecords; n++) { - auto v = p->GetValInt(n); - if (pv1 > v || v > pv2) + uint64_t v = p->GetValInt(n); + if (empty_flag || v < upv1 || v > upv2) filter->Reset(pack, n); } } else { @@ -908,8 +918,8 @@ void TianmuAttr::EvaluatePack_BetweenInt(MIUpdatingIterator &mit, int dim, Descr if (unlikely(p->IsNull(n))) filter->Reset(pack, n); else { - auto v = p->GetValInt(n); - bool res = (pv1 <= v && v <= pv2); + uint64_t v = p->GetValInt(n); + bool res = empty_flag ? false : (upv1 <= v && v <= upv2); if (d.op == common::Operator::O_NOT_BETWEEN) res = !res; if (!res) @@ -922,20 +932,20 @@ void TianmuAttr::EvaluatePack_BetweenInt(MIUpdatingIterator &mit, int dim, Descr if (d.op == common::Operator::O_BETWEEN && !mit.NullsPossibleInPack(dim) && dpn.numOfNulls == 0) { // easy and fast case - no "if"s do { - auto v = p->GetValInt(mit.GetCurInpack(dim)); - if (pv1 > v || v > pv2) + uint64_t v = p->GetValInt(mit.GetCurInpack(dim)); + if (empty_flag || v < upv1 || v > upv2) mit.ResetCurrent(); ++mit; } while (mit.IsValid() && !mit.PackrowStarted()); } else { // more general case - do { + do { // e.g.: table has null, DELETE FROM cs1 WHERE d1 NOT IN (-125); --> not between: (-125, -125) int inpack = mit.GetCurInpack(dim); if (mit[dim] == common::NULL_VALUE_64 || p->IsNull(inpack)) mit.ResetCurrent(); else { - auto v = p->GetValInt(inpack); - bool res = (pv1 <= v && v <= pv2); + uint64_t v = p->GetValInt(inpack); + bool res = (empty_flag ? false : upv1 <= v && v <= upv2); if (d.op == common::Operator::O_NOT_BETWEEN) res = !res; if (!res) @@ -945,10 +955,10 @@ void TianmuAttr::EvaluatePack_BetweenInt(MIUpdatingIterator &mit, int dim, Descr } while (mit.IsValid() && !mit.PackrowStarted()); } } - } else { - // local_min==local_max, and in 2-level encoding both are 0 - if (((pv1 > 0 || pv2 < 0) && d.op == common::Operator::O_BETWEEN) || - (pv1 <= 0 && pv2 >= 0 && d.op == common::Operator::O_NOT_BETWEEN)) { + } else { // when local_min = local_max && has null in table, execute: select * from c where a not in (-125); + // local_min==local_max, and in 2-level encoding both are 0, upv1 = upv2 = 0. + if (((upv1 != 0 || upv2 != 0) && d.op == common::Operator::O_BETWEEN) || + (upv1 == 0 && upv2 == 0 && d.op == common::Operator::O_NOT_BETWEEN)) { mit.ResetCurrentPack(); mit.NextPackrow(); } else