Merge pull request #27788 from Avogar/nullable-decimal

Set function divide as suitable for short-circuit in case of Nullable(Decimal)
This commit is contained in:
Kruglov Pavel 2021-08-18 19:01:44 +03:00 committed by GitHub
commit cd07cabada
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
14
10.5
0
42
21
14
10.5
\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);