Set function divide as suitable for short-circuit in case of Nullable(Decimal)

This commit is contained in:
Pavel Kruglov 2021-08-17 16:24:28 +03:00
parent 98eb619b4b
commit bc853d250f
5 changed files with 26 additions and 1 deletions

View File

@ -958,7 +958,7 @@ public:
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & arguments) const override
{
return ((IsOperation<Op>::div_int || IsOperation<Op>::modulo) && !arguments[1].is_const)
|| (IsOperation<Op>::div_floating && (isDecimal(arguments[0].type) || isDecimal(arguments[1].type)));
|| (IsOperation<Op>::div_floating && (isDecimalOrNullableDecimal(arguments[0].type) || isDecimalOrNullableDecimal(arguments[1].type)));
}
DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override

View File

@ -303,4 +303,14 @@ NullPresence getNullPresense(const ColumnsWithTypeAndName & args)
return res;
}
bool isDecimalOrNullableDecimal(const DataTypePtr & type)
{
WhichDataType which(type);
if (which.isDecimal())
return true;
if (!which.isNullable())
return false;
return isDecimal(assert_cast<const DataTypeNullable *>(type.get())->getNestedType());
}
}

View File

@ -3,6 +3,7 @@
#include <Common/typeid_cast.h>
#include <Common/assert_cast.h>
#include <DataTypes/IDataType.h>
#include <DataTypes/DataTypeNullable.h>
#include <Columns/IColumn.h>
#include <Columns/ColumnArray.h>
#include <Columns/ColumnConst.h>
@ -171,4 +172,5 @@ struct NullPresence
NullPresence getNullPresense(const ColumnsWithTypeAndName & args);
bool isDecimalOrNullableDecimal(const DataTypePtr & type);
}

View File

@ -1395,3 +1395,11 @@ Decimal32
21.00
14.00
10.50
0.00
42.00
21.00
14.00
10.50
\N
\N
\N

View File

@ -123,3 +123,8 @@ select if(number > 0, intDiv(42, number), 1) from numbers(5);
select if(number > 0, intDiv(42, number), 1) from numbers(5);
select if(number > 0, 42 / toDecimal32(number, 2), 0) from numbers(5);
select if(number = 0, 0, toDecimal32(42, 2) / number) from numbers(5);
select if(isNull(x), Null, 42 / x) from (select CAST(materialize(Null), 'Nullable(Decimal32(2))') as x);
select if(isNull(x), Null, x / 0) from (select CAST(materialize(Null), 'Nullable(Decimal32(2))') as x);
select if(isNull(x), Null, intDiv(42, x)) from (select CAST(materialize(Null), 'Nullable(Int64)') as x);