Merge pull request #61656 from ClickHouse/another-fix-for-sum-if-to-count-if-pass

Another fix for `SumIfToCountIfPass`
This commit is contained in:
Antonio Andelic 2024-03-21 08:49:19 +01:00 committed by GitHub
commit 5f067c16be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 56 additions and 5 deletions

View File

@ -97,13 +97,15 @@ public:
if (!if_true_condition_constant_node || !if_false_condition_constant_node)
return;
if (auto constant_type = if_true_condition_constant_node->getResultType(); !isNativeInteger(constant_type))
return;
if (auto constant_type = if_false_condition_constant_node->getResultType(); !isNativeInteger(constant_type))
return;
const auto & if_true_condition_constant_value_literal = if_true_condition_constant_node->getValue();
const auto & if_false_condition_constant_value_literal = if_false_condition_constant_node->getValue();
if (!isInt64OrUInt64FieldType(if_true_condition_constant_value_literal.getType()) ||
!isInt64OrUInt64FieldType(if_false_condition_constant_value_literal.getType()))
return;
auto if_true_condition_value = if_true_condition_constant_value_literal.get<UInt64>();
auto if_false_condition_value = if_false_condition_constant_value_literal.get<UInt64>();

View File

@ -1,5 +1,6 @@
(5,NULL)
(5,NULL)
((6150),3)
(5,NULL)
QUERY id: 0
PROJECTION COLUMNS
@ -66,3 +67,48 @@ QUERY id: 0
ARGUMENTS
LIST id: 19, nodes: 1
CONSTANT id: 20, constant_value: UInt64_10, constant_value_type: UInt8
((6150),3)
QUERY id: 0
PROJECTION COLUMNS
((sum(if(equals(modulo(number, 2), 0), toNullable(0), 123))), toUInt8(3)) Tuple(Tuple(Nullable(UInt64)), UInt8)
PROJECTION
LIST id: 1, nodes: 1
FUNCTION id: 2, function_name: tuple, function_type: ordinary, result_type: Tuple(Tuple(Nullable(UInt64)), UInt8)
ARGUMENTS
LIST id: 3, nodes: 2
FUNCTION id: 4, function_name: tuple, function_type: ordinary, result_type: Tuple(Nullable(UInt64))
ARGUMENTS
LIST id: 5, nodes: 1
FUNCTION id: 6, function_name: sum, function_type: aggregate, nulls_action : IGNORE_NULLS, result_type: Nullable(UInt64)
ARGUMENTS
LIST id: 7, nodes: 1
FUNCTION id: 8, function_name: if, function_type: ordinary, result_type: Nullable(UInt8)
ARGUMENTS
LIST id: 9, nodes: 3
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: UInt64_0, constant_value_type: Nullable(UInt8)
EXPRESSION
FUNCTION id: 19, function_name: toNullable, function_type: ordinary, result_type: Nullable(UInt8)
ARGUMENTS
LIST id: 20, nodes: 1
CONSTANT id: 21, constant_value: UInt64_0, constant_value_type: UInt8
CONSTANT id: 22, constant_value: UInt64_123, constant_value_type: UInt8
CONSTANT id: 23, constant_value: UInt64_3, constant_value_type: UInt8
EXPRESSION
FUNCTION id: 24, function_name: toUInt8, function_type: ordinary, result_type: UInt8
ARGUMENTS
LIST id: 25, nodes: 1
CONSTANT id: 26, constant_value: UInt64_3, constant_value_type: UInt8
JOIN TREE
TABLE_FUNCTION id: 15, alias: __table1, table_function_name: numbers
ARGUMENTS
LIST id: 27, nodes: 1
CONSTANT id: 28, constant_value: UInt64_100, constant_value_type: UInt8

View File

@ -3,9 +3,12 @@ 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);
SELECT (tuple(sum(if((number % 2) = 0, toNullable(0), 123)) IGNORE NULLS), toUInt8(3)) FROM numbers(100);
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);
EXPLAIN QUERY TREE SELECT (sum(if((number % 2) = 0, toNullable(1), 0)), NULL) FROM numbers(10);
SELECT (tuple(sum(if((number % 2) = 0, toNullable(0), 123)) IGNORE NULLS), toUInt8(3)) FROM numbers(100);
EXPLAIN QUERY TREE SELECT (tuple(sum(if((number % 2) = 0, toNullable(0), 123)) IGNORE NULLS), toUInt8(3)) FROM numbers(100);