Skip to content

Commit

Permalink
Merge pull request #55019 from Avogar/fix-bad-cast-to-int128
Browse files Browse the repository at this point in the history
Fix bad cast to ColumnVector<Int128> in function if
  • Loading branch information
alexey-milovidov committed Sep 26, 2023
2 parents 6634043 + d5de17f commit d781ee3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Functions/if.cpp
Expand Up @@ -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<DataTypeArray>(arg_then.type.get()))
left_id = left_array->getNestedType()->getTypeId();
left_type = left_array->getNestedType();

if (const auto * right_array = checkAndGetDataType<DataTypeArray>(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<DataTypeInt64>();
else if (isNativeInteger(right_type) && isUInt64ThatCanBeInt64(left_type))
left_type = std::make_shared<DataTypeInt64>();

TypeIndex left_id = left_type->getTypeId();
TypeIndex right_id = right_type->getTypeId();

if (!(callOnBasicTypes<true, true, true, false>(left_id, right_id, call)
|| (res = executeTyped<UUID, UUID>(cond_col, arguments, result_type, input_rows_count))
Expand Down
@@ -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]]
@@ -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);

0 comments on commit d781ee3

Please sign in to comment.