From 218366e17fd3c41fe4d83a0e4c54fd1fe0537a0a Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Fri, 12 Oct 2018 00:31:36 +0300 Subject: [PATCH 1/2] Fixed error introduced in #3342 --- dbms/src/Core/callOnTypeIndex.h | 60 ++++++++++++++++------- dbms/src/Functions/FunctionsComparison.h | 2 +- dbms/src/Functions/FunctionsConditional.h | 2 +- dbms/src/Functions/FunctionsMath.h | 4 +- 4 files changed, 45 insertions(+), 23 deletions(-) diff --git a/dbms/src/Core/callOnTypeIndex.h b/dbms/src/Core/callOnTypeIndex.h index e531e64d158..8f4424ec016 100644 --- a/dbms/src/Core/callOnTypeIndex.h +++ b/dbms/src/Core/callOnTypeIndex.h @@ -16,7 +16,7 @@ struct TypePair -template +template bool callOnBasicType(TypeIndex number, F && f) { if constexpr (_int) @@ -40,7 +40,7 @@ bool callOnBasicType(TypeIndex number, F && f) } } - if constexpr (_dec) + if constexpr (_decimal) { switch (number) { @@ -63,40 +63,51 @@ bool callOnBasicType(TypeIndex number, F && f) } } + if constexpr (_datetime) + { + switch (number) + { + case TypeIndex::Date: return f(TypePair()); + case TypeIndex::DateTime: return f(TypePair()); + default: + break; + } + } + return false; } /// Unroll template using TypeIndex -template +template inline bool callOnBasicTypes(TypeIndex type_num1, TypeIndex type_num2, F && f) { if constexpr (_int) { switch (type_num1) { - case TypeIndex::UInt8: return callOnBasicType(type_num2, std::forward(f)); - case TypeIndex::UInt16: return callOnBasicType(type_num2, std::forward(f)); - case TypeIndex::UInt32: return callOnBasicType(type_num2, std::forward(f)); - case TypeIndex::UInt64: return callOnBasicType(type_num2, std::forward(f)); - //case TypeIndex::UInt128: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::UInt8: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::UInt16: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::UInt32: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::UInt64: return callOnBasicType(type_num2, std::forward(f)); + //case TypeIndex::UInt128: return callOnBasicType(type_num2, std::forward(f)); - case TypeIndex::Int8: return callOnBasicType(type_num2, std::forward(f)); - case TypeIndex::Int16: return callOnBasicType(type_num2, std::forward(f)); - case TypeIndex::Int32: return callOnBasicType(type_num2, std::forward(f)); - case TypeIndex::Int64: return callOnBasicType(type_num2, std::forward(f)); - case TypeIndex::Int128: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::Int8: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::Int16: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::Int32: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::Int64: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::Int128: return callOnBasicType(type_num2, std::forward(f)); default: break; } } - if constexpr (_dec) + if constexpr (_decimal) { switch (type_num1) { - case TypeIndex::Decimal32: return callOnBasicType(type_num2, std::forward(f)); - case TypeIndex::Decimal64: return callOnBasicType(type_num2, std::forward(f)); - case TypeIndex::Decimal128: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::Decimal32: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::Decimal64: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::Decimal128: return callOnBasicType(type_num2, std::forward(f)); default: break; } @@ -106,8 +117,19 @@ inline bool callOnBasicTypes(TypeIndex type_num1, TypeIndex type_num2, F && f) { switch (type_num1) { - case TypeIndex::Float32: return callOnBasicType(type_num2, std::forward(f)); - case TypeIndex::Float64: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::Float32: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::Float64: return callOnBasicType(type_num2, std::forward(f)); + default: + break; + } + } + + if constexpr (_datetime) + { + switch (type_num1) + { + case TypeIndex::Date: return callOnBasicType(type_num2, std::forward(f)); + case TypeIndex::DateTime: return callOnBasicType(type_num2, std::forward(f)); default: break; } diff --git a/dbms/src/Functions/FunctionsComparison.h b/dbms/src/Functions/FunctionsComparison.h index 36132cbba4e..3f7f6f39008 100644 --- a/dbms/src/Functions/FunctionsComparison.h +++ b/dbms/src/Functions/FunctionsComparison.h @@ -725,7 +725,7 @@ private: return true; }; - if (!callOnBasicTypes(left_number, right_number, call)) + if (!callOnBasicTypes(left_number, right_number, call)) throw Exception("Wrong call for " + getName() + " with " + col_left.type->getName() + " and " + col_right.type->getName(), ErrorCodes::LOGICAL_ERROR); } diff --git a/dbms/src/Functions/FunctionsConditional.h b/dbms/src/Functions/FunctionsConditional.h index 573ae32b2a2..00026f1b83f 100644 --- a/dbms/src/Functions/FunctionsConditional.h +++ b/dbms/src/Functions/FunctionsConditional.h @@ -975,7 +975,7 @@ public: if (auto rigth_array = checkAndGetDataType(arg_else.type.get())) right_id = rigth_array->getNestedType()->getTypeId(); - bool executed_with_nums = callOnBasicTypes(left_id, right_id, call); + bool executed_with_nums = callOnBasicTypes(left_id, right_id, call); if (!( executed_with_nums || executeString(cond_col, block, arguments, result) diff --git a/dbms/src/Functions/FunctionsMath.h b/dbms/src/Functions/FunctionsMath.h index 21404789ad3..5ab1bf899c2 100644 --- a/dbms/src/Functions/FunctionsMath.h +++ b/dbms/src/Functions/FunctionsMath.h @@ -162,7 +162,7 @@ private: return execute(block, col_vec, result); }; - if (!callOnBasicType(col.type->getTypeId(), call)) + if (!callOnBasicType(col.type->getTypeId(), call)) throw Exception{"Illegal column " + col.column->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN}; } @@ -385,7 +385,7 @@ private: TypeIndex left_index = col_left.type->getTypeId(); TypeIndex right_index = col_right.type->getTypeId(); - if (!callOnBasicTypes(left_index, right_index, call)) + if (!callOnBasicTypes(left_index, right_index, call)) throw Exception{"Illegal column " + col_left.column->getName() + " of argument of function " + getName(), ErrorCodes::ILLEGAL_COLUMN}; } From f2e161156e39a0fdf94801d28b6093f39fbd86e8 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Fri, 12 Oct 2018 03:18:36 +0300 Subject: [PATCH 2/2] Added test #3342 --- .../0_stateless/00733_if_datetime.reference | 16 ++++++++++++++++ .../queries/0_stateless/00733_if_datetime.sql | 9 +++++++++ 2 files changed, 25 insertions(+) create mode 100644 dbms/tests/queries/0_stateless/00733_if_datetime.reference create mode 100644 dbms/tests/queries/0_stateless/00733_if_datetime.sql diff --git a/dbms/tests/queries/0_stateless/00733_if_datetime.reference b/dbms/tests/queries/0_stateless/00733_if_datetime.reference new file mode 100644 index 00000000000..de6934f87b2 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00733_if_datetime.reference @@ -0,0 +1,16 @@ +2001-02-03 04:05:06 +2000-01-01 00:00:00 +2001-02-03 04:05:06 +2000-01-01 00:00:00 +2001-02-03 04:05:06 +2000-01-01 00:00:00 +2001-02-03 04:05:06 +2000-01-01 00:00:00 +2001-02-03 +2000-01-01 +2001-02-03 +2000-01-01 +2001-02-03 +2000-01-01 +2001-02-03 +2000-01-01 diff --git a/dbms/tests/queries/0_stateless/00733_if_datetime.sql b/dbms/tests/queries/0_stateless/00733_if_datetime.sql new file mode 100644 index 00000000000..23ebc3d8a43 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00733_if_datetime.sql @@ -0,0 +1,9 @@ +SELECT number % 2 ? toDateTime('2000-01-01 00:00:00') : toDateTime('2001-02-03 04:05:06') FROM numbers(2); +SELECT number % 2 ? toDateTime('2000-01-01 00:00:00') : materialize(toDateTime('2001-02-03 04:05:06')) FROM numbers(2); +SELECT number % 2 ? materialize(toDateTime('2000-01-01 00:00:00')) : toDateTime('2001-02-03 04:05:06') FROM numbers(2); +SELECT number % 2 ? materialize(toDateTime('2000-01-01 00:00:00')) : materialize(toDateTime('2001-02-03 04:05:06')) FROM numbers(2); + +SELECT number % 2 ? toDate('2000-01-01') : toDate('2001-02-03') FROM numbers(2); +SELECT number % 2 ? toDate('2000-01-01') : materialize(toDate('2001-02-03')) FROM numbers(2); +SELECT number % 2 ? materialize(toDate('2000-01-01')) : toDate('2001-02-03') FROM numbers(2); +SELECT number % 2 ? materialize(toDate('2000-01-01')) : materialize(toDate('2001-02-03')) FROM numbers(2);