Skip to content

Commit bc5df49

Browse files
Merge pull request ClickHouse#77307 from ClickHouse/test-constant-alias-fix
Fix THERE_IS_NO_COLUMN for parallel replicas with constant table ALIAS
2 parents 1abb692 + fe8736a commit bc5df49

File tree

5 files changed

+58
-5
lines changed

5 files changed

+58
-5
lines changed

src/Planner/CollectTableExpressionData.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,12 @@ class CollectSourceColumnsVisitor : public InDepthQueryTreeVisitorWithContext<Co
109109
if (outputs.size() != 1)
110110
throw Exception(ErrorCodes::LOGICAL_ERROR,
111111
"Expected single output in actions dag for alias column {}. Actual {}", column_node->dumpTree(), outputs.size());
112+
113+
auto & alias_node = outputs[0];
112114
const auto & column_name = column_node->getColumnName();
113-
const auto & alias_node = alias_column_actions_dag.addAlias(*outputs[0], column_name);
114-
alias_column_actions_dag.addOrReplaceInOutputs(alias_node);
115+
alias_node = &alias_column_actions_dag.addAlias(*alias_node, column_name);
116+
117+
alias_column_actions_dag.getOutputs() = std::move(outputs);
115118
table_expression_data.addAliasColumn(column_node->getColumn(), column_identifier, std::move(alias_column_actions_dag), select_added_columns);
116119
}
117120
else

src/Planner/PlannerActionsVisitor.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,17 @@ PlannerActionsVisitorImpl::NodeNameAndNodeMinLevel PlannerActionsVisitorImpl::vi
675675
{
676676
auto column_node_name = action_node_name_helper.calculateActionNodeName(node);
677677
const auto & column_node = node->as<ColumnNode &>();
678-
if (column_node.hasExpression() && !use_column_identifier_as_action_node_name)
679-
return visitImpl(column_node.getExpression());
678+
if (column_node.hasExpression())
679+
{
680+
auto expression = column_node.getExpression();
681+
/// In case of constant expression, prefer constant value from QueryTree vs. re-calculating the expression.
682+
/// It is possible that during the execution of distributed queries
683+
/// source columns from constant expression are removed, so that the attempt to recalculate it fails.
684+
if (expression->getNodeType() == QueryTreeNodeType::CONSTANT)
685+
return visitConstant(expression);
686+
else if (!use_column_identifier_as_action_node_name)
687+
return visitImpl(expression);
688+
}
680689
Int64 actions_stack_size = static_cast<Int64>(actions_stack.size() - 1);
681690
for (Int64 i = actions_stack_size; i >= 0; --i)
682691
{

src/Planner/PlannerJoinTree.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,12 +1156,19 @@ JoinTreeQueryPlan buildQueryPlanForTableExpression(QueryTreeNodePtr table_expres
11561156
else
11571157
{
11581158
/// Create step which reads from empty source if storage has no data.
1159-
const auto & column_names = table_expression_data.getSelectedColumnsNames();
1159+
const auto & column_names = table_expression_data.getColumnNames();
11601160
auto source_header = storage_snapshot->getSampleBlockForColumns(column_names);
11611161
Pipe pipe(std::make_shared<NullSource>(source_header));
11621162
auto read_from_pipe = std::make_unique<ReadFromPreparedSource>(std::move(pipe));
11631163
read_from_pipe->setStepDescription("Read from NullSource");
11641164
query_plan.addStep(std::move(read_from_pipe));
1165+
1166+
auto & alias_column_expressions = table_expression_data.getAliasColumnExpressions();
1167+
if (!alias_column_expressions.empty())
1168+
{
1169+
auto alias_column_step = createComputeAliasColumnsStep(alias_column_expressions, query_plan.getCurrentHeader());
1170+
query_plan.addStep(std::move(alias_column_step));
1171+
}
11651172
}
11661173
}
11671174
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
['qwqw']
2+
----
3+
['qwqw']
4+
['qwqw']
5+
6+
['qwqw']
7+
['qwqw']
8+
----
9+
0 0 0 0 ['a'] ['qwqw']
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
SET parallel_replicas_for_non_replicated_merge_tree = 1;
2+
SET allow_experimental_parallel_reading_from_replicas = 1;
3+
SET cluster_for_parallel_replicas = 'parallel_replicas';
4+
DROP TABLE IF EXISTS test_table;
5+
CREATE TABLE test_table (a UInt64, b UInt64, c UInt64, d UInt64, x Array(String))
6+
ENGINE MergeTree() PARTITION BY b ORDER BY a;
7+
INSERT INTO test_table SELECT number, number % 2, number, number % 3, ['a', 'b', 'c'] FROM numbers(1);
8+
ALTER TABLE test_table ADD COLUMN y Array(String) ALIAS ['qwqw'] AFTER x;
9+
10+
SELECT y FROM test_table ORDER BY c;
11+
12+
SET allow_experimental_parallel_reading_from_replicas = 0;
13+
SELECT '----';
14+
15+
SELECT y FROM remote('127.0.0.{1,2}', currentDatabase(), test_table) ORDER BY c settings extremes=1;
16+
17+
SELECT '----';
18+
DROP TABLE IF EXISTS test_table;
19+
SET allow_experimental_parallel_reading_from_replicas = 1;
20+
21+
CREATE TABLE test_table (a UInt64, b UInt64, c UInt64, d UInt64, n Nested(x String))
22+
ENGINE MergeTree() PARTITION BY b ORDER BY a;
23+
INSERT INTO test_table SELECT number, number % 2, number, number % 3, ['a'] FROM numbers(1);
24+
ALTER TABLE test_table ADD COLUMN n.y Array(String) ALIAS ['qwqw'] AFTER n.x;
25+
SELECT a, b, c, d, n.x, n.y FROM test_table ORDER BY c;

0 commit comments

Comments
 (0)