diff --git a/src/Functions/if.cpp b/src/Functions/if.cpp index a955230f3d38..eba1733c683f 100644 --- a/src/Functions/if.cpp +++ b/src/Functions/if.cpp @@ -1096,22 +1096,25 @@ class FunctionIf : public FunctionIfBase return res != nullptr; }; - TypeIndex left_id = arg_then.type->getTypeId(); - TypeIndex right_id = arg_else.type->getTypeId(); + DataTypePtr left_type = arg_then.type; + DataTypePtr right_type = arg_else.type; if (const auto * left_array = checkAndGetDataType(arg_then.type.get())) - left_id = left_array->getNestedType()->getTypeId(); + left_type = left_array->getNestedType(); if (const auto * right_array = checkAndGetDataType(arg_else.type.get())) - right_id = right_array->getNestedType()->getTypeId(); + right_type = right_array->getNestedType(); /// Special case when one column is Integer and another is UInt64 that can be actually Int64. /// The result type for this case is Int64 and we need to change UInt64 type to Int64 /// so the NumberTraits::ResultOfIf will return Int64 instead if Int128. - if (isNativeInteger(arg_then.type) && isUInt64ThatCanBeInt64(arg_else.type)) - right_id = TypeIndex::Int64; - else if (isNativeInteger(arg_else.type) && isUInt64ThatCanBeInt64(arg_then.type)) - left_id = TypeIndex::Int64; + if (isNativeInteger(left_type) && isUInt64ThatCanBeInt64(right_type)) + right_type = std::make_shared(); + else if (isNativeInteger(right_type) && isUInt64ThatCanBeInt64(left_type)) + left_type = std::make_shared(); + + TypeIndex left_id = left_type->getTypeId(); + TypeIndex right_id = right_type->getTypeId(); if (!(callOnBasicTypes(left_id, right_id, call) || (res = executeTyped(cond_col, arguments, result_type, input_rows_count)) diff --git a/tests/queries/0_stateless/02888_integer_type_inference_in_if_function.reference b/tests/queries/0_stateless/02888_integer_type_inference_in_if_function.reference new file mode 100644 index 000000000000..b5909a82df3a --- /dev/null +++ b/tests/queries/0_stateless/02888_integer_type_inference_in_if_function.reference @@ -0,0 +1,24 @@ +-9223372036854775808 +9223372036854775806 +-9223372036854775808 +9223372036854775806 +-9223372036854775808 +9223372036854775806 +-9223372036854775808 +9223372036854775806 +[2,65537,-9223372036854775808] +[9223372036854775806] +[2,65537,-9223372036854775808] +[9223372036854775806] +[2,65537,-9223372036854775808] +[9223372036854775806] +[2,65537,-9223372036854775808] +[9223372036854775806] +[[2,65537,-9223372036854775808]] +[[9223372036854775806]] +[[2,65537,-9223372036854775808]] +[[9223372036854775806]] +[[2,65537,-9223372036854775808]] +[[9223372036854775806]] +[[2,65537,-9223372036854775808]] +[[9223372036854775806]] diff --git a/tests/queries/0_stateless/02888_integer_type_inference_in_if_function.sql b/tests/queries/0_stateless/02888_integer_type_inference_in_if_function.sql new file mode 100644 index 000000000000..065536e0e6f0 --- /dev/null +++ b/tests/queries/0_stateless/02888_integer_type_inference_in_if_function.sql @@ -0,0 +1,13 @@ +SELECT if(number % 2, 9223372036854775806, -9223372036854775808) AS res FROM numbers(2); +SELECT if(number % 2, materialize(9223372036854775806), -9223372036854775808) AS res FROM numbers(2); +SELECT if(number % 2, 9223372036854775806, materialize(-9223372036854775808)) AS res FROM numbers(2); +SELECT if(number % 2, materialize(9223372036854775806), materialize(-9223372036854775808)) AS res FROM numbers(2); +SELECT if(number % 2, [9223372036854775806], [2, 65537, -9223372036854775808]) AS res FROM numbers(2); +SELECT if(number % 2, materialize([9223372036854775806]), [2, 65537, -9223372036854775808]) AS res FROM numbers(2); +SELECT if(number % 2, [9223372036854775806], materialize([2, 65537, -9223372036854775808])) AS res FROM numbers(2); +SELECT if(number % 2, materialize([9223372036854775806]), materialize([2, 65537, -9223372036854775808])) AS res FROM numbers(2); +SELECT if(number % 2, [[9223372036854775806]], [[2, 65537, -9223372036854775808]]) AS res FROM numbers(2); +SELECT if(number % 2, materialize([[9223372036854775806]]), [[2, 65537, -9223372036854775808]]) AS res FROM numbers(2); +SELECT if(number % 2, [[9223372036854775806]], materialize([[2, 65537, -9223372036854775808]])) AS res FROM numbers(2); +SELECT if(number % 2, materialize([[9223372036854775806]]), materialize([[2, 65537, -9223372036854775808]])) AS res FROM numbers(2); +