Skip to content

Commit acc3993

Browse files
authored
Merge pull request #894 from Altinity/backports/25.3.5/79743
25.3.5 Backport of ClickHouse#79743: Fix wrong results for grouping sets with ColumnConst
2 parents dca3980 + ada143d commit acc3993

File tree

6 files changed

+60
-5
lines changed

6 files changed

+60
-5
lines changed

src/Interpreters/ActionsDAG.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ ActionsDAG::ActionsDAG(const NamesAndTypesList & inputs_)
210210
outputs.push_back(&addInput(input.name, input.type));
211211
}
212212

213-
ActionsDAG::ActionsDAG(const ColumnsWithTypeAndName & inputs_)
213+
ActionsDAG::ActionsDAG(const ColumnsWithTypeAndName & inputs_, bool duplicate_const_columns)
214214
{
215215
for (const auto & input : inputs_)
216216
{
217-
if (input.column && isColumnConst(*input.column))
217+
if (input.column && isColumnConst(*input.column) && duplicate_const_columns)
218218
{
219219
addInput(input);
220220

src/Interpreters/ActionsDAG.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class ActionsDAG
115115
ActionsDAG & operator=(ActionsDAG &&) = default;
116116
ActionsDAG & operator=(const ActionsDAG &) = delete;
117117
explicit ActionsDAG(const NamesAndTypesList & inputs_);
118-
explicit ActionsDAG(const ColumnsWithTypeAndName & inputs_);
118+
explicit ActionsDAG(const ColumnsWithTypeAndName & inputs_, bool duplicate_const_columns = true);
119119

120120
const Nodes & getNodes() const { return nodes; }
121121
static Nodes detachNodes(ActionsDAG && dag) { return std::move(dag.nodes); }

src/Planner/PlannerExpressionAnalysis.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ std::optional<AggregationAnalysisResult> analyzeAggregation(const QueryTreeNodeP
125125
Names aggregation_keys;
126126

127127
ActionsAndProjectInputsFlagPtr before_aggregation_actions = std::make_shared<ActionsAndProjectInputsFlag>();
128-
before_aggregation_actions->dag = ActionsDAG(input_columns);
128+
/// Here it is OK to materialize const columns: if column is used in GROUP BY, it may be expected to become non-const
129+
/// See https://github.com/ClickHouse/ClickHouse/issues/70655 for example
130+
before_aggregation_actions->dag = ActionsDAG(input_columns, false);
129131
before_aggregation_actions->dag.getOutputs().clear();
130132

131133
std::unordered_set<std::string_view> before_aggregation_actions_output_node_names;

tests/queries/0_stateless/00757_enum_defaults_const_analyzer.reference

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ iphone 1
33
iphone 1
44
iphone 1
55

6-
iphone 1
6+
\N 1
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Const column in grouping set, analyzer on:
2+
0 0 value 0 1
3+
0 0 value 1 1
4+
0 0 value 2 1
5+
0 0 value 3 1
6+
1 0 0 1
7+
1 0 1 1
8+
1 0 2 1
9+
1 0 3 1
10+
Non-const column in grouping set, analyzer on:
11+
0 0 value 0 1
12+
0 0 value 1 1
13+
0 0 value 2 1
14+
0 0 value 3 1
15+
1 0 0 1
16+
1 0 1 1
17+
1 0 2 1
18+
1 0 3 1
19+
Const column in grouping set, analyzer off:
20+
0 0 value 0 1
21+
0 0 value 1 1
22+
0 0 value 2 1
23+
0 0 value 3 1
24+
1 0 0 1
25+
1 0 1 1
26+
1 0 2 1
27+
1 0 3 1
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
SET enable_analyzer=1;
2+
3+
SELECT 'Const column in grouping set, analyzer on:';
4+
5+
SELECT grouping(key_a), grouping(key_b), key_a, key_b, count() FROM (
6+
SELECT 'value' as key_a, number as key_b FROM numbers(4)
7+
)
8+
GROUP BY GROUPING SETS((key_b), (key_a, key_b))
9+
ORDER BY (grouping(key_a), grouping(key_b), key_a, key_b);
10+
11+
SELECT 'Non-const column in grouping set, analyzer on:';
12+
13+
SELECT grouping(key_a), grouping(key_b), key_a, key_b, count() FROM (
14+
SELECT materialize('value') as key_a, number as key_b FROM numbers(4)
15+
)
16+
GROUP BY GROUPING SETS((key_b), (key_a, key_b))
17+
ORDER BY (grouping(key_a), grouping(key_b), key_a, key_b);
18+
19+
SELECT 'Const column in grouping set, analyzer off:';
20+
21+
SELECT grouping(key_a), grouping(key_b), key_a, key_b, count() FROM (
22+
SELECT 'value' as key_a, number as key_b FROM numbers(4)
23+
)
24+
GROUP BY GROUPING SETS((key_b), (key_a, key_b))
25+
ORDER BY (grouping(key_a), grouping(key_b), key_a, key_b)
26+
SETTINGS allow_experimental_analyzer=0;

0 commit comments

Comments
 (0)