From 276b3a02154512f358d3c9c499e5bd0e22dfec16 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Fri, 10 Jul 2020 09:30:49 +0300 Subject: [PATCH] Avoid exception when negative or floating point constant is used in WHERE condition for indexed tables #11905 --- src/Storages/MergeTree/KeyCondition.cpp | 24 ++++++++++++------- ...ive_float_constant_key_condition.reference | 4 ++++ ..._negative_float_constant_key_condition.sql | 19 +++++++++++++++ 3 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 tests/queries/0_stateless/01386_negative_float_constant_key_condition.reference create mode 100644 tests/queries/0_stateless/01386_negative_float_constant_key_condition.sql diff --git a/src/Storages/MergeTree/KeyCondition.cpp b/src/Storages/MergeTree/KeyCondition.cpp index 929242b2815..5905272d827 100644 --- a/src/Storages/MergeTree/KeyCondition.cpp +++ b/src/Storages/MergeTree/KeyCondition.cpp @@ -866,17 +866,23 @@ bool KeyCondition::tryParseAtomFromAST(const ASTPtr & node, const Context & cont return atom_it->second(out, const_value); } - else if (getConstant(node, block_with_constants, const_value, const_type)) /// For cases where it says, for example, `WHERE 0 AND something` + else if (getConstant(node, block_with_constants, const_value, const_type)) { - if (const_value.getType() == Field::Types::UInt64 - || const_value.getType() == Field::Types::Int64 - || const_value.getType() == Field::Types::Float64) - { - /// Zero in all types is represented in memory the same way as in UInt64. - out.function = const_value.safeGet() - ? RPNElement::ALWAYS_TRUE - : RPNElement::ALWAYS_FALSE; + /// For cases where it says, for example, `WHERE 0 AND something` + if (const_value.getType() == Field::Types::UInt64) + { + out.function = const_value.safeGet() ? RPNElement::ALWAYS_TRUE : RPNElement::ALWAYS_FALSE; + return true; + } + else if (const_value.getType() == Field::Types::Int64) + { + out.function = const_value.safeGet() ? RPNElement::ALWAYS_TRUE : RPNElement::ALWAYS_FALSE; + return true; + } + else if (const_value.getType() == Field::Types::Float64) + { + out.function = const_value.safeGet() ? RPNElement::ALWAYS_TRUE : RPNElement::ALWAYS_FALSE; return true; } } diff --git a/tests/queries/0_stateless/01386_negative_float_constant_key_condition.reference b/tests/queries/0_stateless/01386_negative_float_constant_key_condition.reference new file mode 100644 index 00000000000..44e0be8e356 --- /dev/null +++ b/tests/queries/0_stateless/01386_negative_float_constant_key_condition.reference @@ -0,0 +1,4 @@ +0 +0 +0 +0 diff --git a/tests/queries/0_stateless/01386_negative_float_constant_key_condition.sql b/tests/queries/0_stateless/01386_negative_float_constant_key_condition.sql new file mode 100644 index 00000000000..216f43c4285 --- /dev/null +++ b/tests/queries/0_stateless/01386_negative_float_constant_key_condition.sql @@ -0,0 +1,19 @@ +DROP TABLE IF EXISTS t0; + +CREATE TABLE t0 +( + `c0` Int32, + `c1` Int32 CODEC(NONE) +) +ENGINE = MergeTree() +ORDER BY tuple() +SETTINGS index_granularity = 8192; + +INSERT INTO t0 VALUES (0, 0); + +SELECT t0.c1 FROM t0 WHERE NOT (t0.c1 OR (t0.c0 AND -1524532316)); +SELECT t0.c1 FROM t0 WHERE NOT (t0.c1 OR (t0.c0 AND -1.0)); +SELECT t0.c1 FROM t0 WHERE NOT (t0.c1 OR (t0.c0 AND inf)); +SELECT t0.c1 FROM t0 WHERE NOT (t0.c1 OR (t0.c0 AND nan)); + +DROP TABLE t0;