Merge pull request #61389 from ClickHouse/sum-if-count-if-nullabl

Disable `optimize_rewrite_sum_if_to_count_if` if return type is nullable (new analyzer)
This commit is contained in:
Antonio Andelic 2024-03-15 17:18:19 +01:00 committed by GitHub
commit 48a96f8798
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 81 additions and 2 deletions

View File

@ -54,10 +54,10 @@ public:
if (!constant_node)
return;
const auto & constant_value_literal = constant_node->getValue();
if (!isInt64OrUInt64FieldType(constant_value_literal.getType()))
if (auto constant_type = constant_node->getResultType(); !isNativeInteger(constant_type))
return;
const auto & constant_value_literal = constant_node->getValue();
if (getSettings().aggregate_functions_null_for_empty)
return;

View File

@ -0,0 +1,68 @@
(5,NULL)
(5,NULL)
(5,NULL)
QUERY id: 0
PROJECTION COLUMNS
(sumIf(toNullable(1), equals(modulo(number, 2), 0)), NULL) Tuple(Nullable(UInt64), Nullable(Nothing))
PROJECTION
LIST id: 1, nodes: 1
FUNCTION id: 2, function_name: tuple, function_type: ordinary, result_type: Tuple(Nullable(UInt64), Nullable(Nothing))
ARGUMENTS
LIST id: 3, nodes: 2
FUNCTION id: 4, function_name: sumIf, function_type: aggregate, result_type: Nullable(UInt64)
ARGUMENTS
LIST id: 5, nodes: 2
CONSTANT id: 6, constant_value: UInt64_1, constant_value_type: Nullable(UInt8)
EXPRESSION
FUNCTION id: 7, function_name: toNullable, function_type: ordinary, result_type: Nullable(UInt8)
ARGUMENTS
LIST id: 8, nodes: 1
CONSTANT id: 9, constant_value: UInt64_1, constant_value_type: UInt8
FUNCTION id: 10, function_name: equals, function_type: ordinary, result_type: UInt8
ARGUMENTS
LIST id: 11, nodes: 2
FUNCTION id: 12, function_name: modulo, function_type: ordinary, result_type: UInt8
ARGUMENTS
LIST id: 13, nodes: 2
COLUMN id: 14, column_name: number, result_type: UInt64, source_id: 15
CONSTANT id: 16, constant_value: UInt64_2, constant_value_type: UInt8
CONSTANT id: 17, constant_value: UInt64_0, constant_value_type: UInt8
CONSTANT id: 18, constant_value: NULL, constant_value_type: Nullable(Nothing)
JOIN TREE
TABLE_FUNCTION id: 15, alias: __table1, table_function_name: numbers
ARGUMENTS
LIST id: 19, nodes: 1
CONSTANT id: 20, constant_value: UInt64_10, constant_value_type: UInt8
(5,NULL)
QUERY id: 0
PROJECTION COLUMNS
(sum(if(equals(modulo(number, 2), 0), toNullable(1), 0)), NULL) Tuple(Nullable(UInt64), Nullable(Nothing))
PROJECTION
LIST id: 1, nodes: 1
FUNCTION id: 2, function_name: tuple, function_type: ordinary, result_type: Tuple(Nullable(UInt64), Nullable(Nothing))
ARGUMENTS
LIST id: 3, nodes: 2
FUNCTION id: 4, function_name: sumOrNullIf, function_type: aggregate, result_type: Nullable(UInt64)
ARGUMENTS
LIST id: 5, nodes: 2
CONSTANT id: 6, constant_value: UInt64_1, constant_value_type: Nullable(UInt8)
EXPRESSION
FUNCTION id: 7, function_name: toNullable, function_type: ordinary, result_type: Nullable(UInt8)
ARGUMENTS
LIST id: 8, nodes: 1
CONSTANT id: 9, constant_value: UInt64_1, constant_value_type: UInt8
FUNCTION id: 10, function_name: equals, function_type: ordinary, result_type: UInt8
ARGUMENTS
LIST id: 11, nodes: 2
FUNCTION id: 12, function_name: modulo, function_type: ordinary, result_type: UInt8
ARGUMENTS
LIST id: 13, nodes: 2
COLUMN id: 14, column_name: number, result_type: UInt64, source_id: 15
CONSTANT id: 16, constant_value: UInt64_2, constant_value_type: UInt8
CONSTANT id: 17, constant_value: UInt64_0, constant_value_type: UInt8
CONSTANT id: 18, constant_value: NULL, constant_value_type: Nullable(Nothing)
JOIN TREE
TABLE_FUNCTION id: 15, alias: __table1, table_function_name: numbers
ARGUMENTS
LIST id: 19, nodes: 1
CONSTANT id: 20, constant_value: UInt64_10, constant_value_type: UInt8

View File

@ -0,0 +1,11 @@
SET optimize_rewrite_sum_if_to_count_if = 1;
SET allow_experimental_analyzer = 0;
SELECT (sumIf(toNullable(1), (number % 2) = 0), NULL) FROM numbers(10);
SELECT (sum(if((number % 2) = 0, toNullable(1), 0)), NULL) FROM numbers(10);
SET allow_experimental_analyzer = 1;
SELECT (sumIf(toNullable(1), (number % 2) = 0), NULL) FROM numbers(10);
EXPLAIN QUERY TREE SELECT (sumIf(toNullable(1), (number % 2) = 0), NULL) FROM numbers(10);
SELECT (sum(if((number % 2) = 0, toNullable(1), 0)), NULL) FROM numbers(10);
EXPLAIN QUERY TREE SELECT (sum(if((number % 2) = 0, toNullable(1), 0)), NULL) FROM numbers(10);