Improve isNullable/isConstant/isNull/isNotNull performance for LowCardinality argument

This commit is contained in:
avogar 2022-07-13 17:56:34 +00:00
parent 71aba5b4c6
commit 390b1ac2f7
7 changed files with 53 additions and 1 deletions

View File

@ -29,6 +29,8 @@ public:
bool useDefaultImplementationForNothing() const override { return false; }
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
size_t getNumberOfArguments() const override

View File

@ -4,6 +4,7 @@
#include <DataTypes/DataTypesNumber.h>
#include <Core/ColumnNumbers.h>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnLowCardinality.h>
#include <Common/assert_cast.h>
@ -32,6 +33,7 @@ public:
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
@ -43,6 +45,17 @@ public:
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
const ColumnWithTypeAndName & elem = arguments[0];
if (elem.type->isLowCardinalityNullable())
{
const auto * low_cardinality_column = checkAndGetColumn<ColumnLowCardinality>(*elem.column);
size_t null_index = low_cardinality_column->getDictionary().getNullValueIndex();
auto res = DataTypeUInt8().createColumn();
auto & data = typeid_cast<ColumnUInt8 &>(*res).getData();
for (size_t i = 0; i != low_cardinality_column->size(); ++i)
data.push_back(low_cardinality_column->getIndexAt(i) != null_index);
return res;
}
if (const auto * nullable = checkAndGetColumn<ColumnNullable>(*elem.column))
{
/// Return the negated null map.

View File

@ -4,6 +4,7 @@
#include <DataTypes/DataTypesNumber.h>
#include <Core/ColumnNumbers.h>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnLowCardinality.h>
namespace DB
@ -30,6 +31,7 @@ public:
size_t getNumberOfArguments() const override { return 1; }
bool useDefaultImplementationForNulls() const override { return false; }
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
bool useDefaultImplementationForConstants() const override { return true; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
@ -42,6 +44,17 @@ public:
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t) const override
{
const ColumnWithTypeAndName & elem = arguments[0];
if (elem.type->isLowCardinalityNullable())
{
const auto * low_cardinality_column = checkAndGetColumn<ColumnLowCardinality>(*elem.column);
size_t null_index = low_cardinality_column->getDictionary().getNullValueIndex();
auto res = DataTypeUInt8().createColumn();
auto & data = typeid_cast<ColumnUInt8 &>(*res).getData();
for (size_t i = 0; i != low_cardinality_column->size(); ++i)
data.push_back(low_cardinality_column->getIndexAt(i) == null_index);
return res;
}
if (const auto * nullable = checkAndGetColumn<ColumnNullable>(*elem.column))
{
/// Merely return the embedded null map.

View File

@ -29,6 +29,8 @@ public:
bool useDefaultImplementationForConstants() const override { return true; }
bool useDefaultImplementationForLowCardinalityColumns() const override { return false; }
ColumnNumbers getArgumentsThatDontImplyNullableReturnType(size_t /*number_of_arguments*/) const override { return {0}; }
bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return false; }
@ -46,7 +48,7 @@ public:
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override
{
const auto & elem = arguments[0];
return ColumnUInt8::create(input_rows_count, isColumnNullable(*elem.column));
return ColumnUInt8::create(input_rows_count, isColumnNullable(*elem.column) || elem.type->isLowCardinalityNullable());
}
};

View File

@ -0,0 +1,12 @@
<test>
<create_query>
CREATE TABLE test_lc (x UInt64, lc LowCardinality(Nullable(String))) ENGINE = MergeTree order by x
</create_query>
<fill_query>INSERT INTO test_lc SELECT number, number % 10 ? NULL : toString(number % 10000) FROM numbers(1000000)</fill_query>
<query>SELECT isNullable(lc) FROM test_lc </query>
<query>SELECT isConstant(lc) FROM test_lc </query>
<query>SELECT isNull(lc) FROM test_lc </query>
<query>SELECT isNotNull(lc) FROM test_lc </query>
</test>

View File

@ -2,3 +2,7 @@
1
1
1
0
1
0
1

View File

@ -3,3 +3,9 @@ SELECT isNullable(toNullable(3));
SELECT isNullable(NULL);
SELECT isNullable(materialize(NULL));
SELECT isNullable(toLowCardinality(1));
SELECT isNullable(toNullable(toLowCardinality(1)));
SELECT isNullable(toLowCardinality(materialize(1)));
SELECT isNullable(toNullable(toLowCardinality(materialize(1))));