From 641771f66fde93f4aceeb5b4e5db2bfa93e9105a Mon Sep 17 00:00:00 2001 From: Nikolai Kochetov Date: Thu, 6 Sep 2018 20:45:49 +0300 Subject: [PATCH 1/2] Fix virtual columns for prewhere with aliases. --- dbms/src/Interpreters/ExpressionAnalyzer.cpp | 11 ++++++++++- dbms/src/Interpreters/InterpreterSelectQuery.cpp | 8 ++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/dbms/src/Interpreters/ExpressionAnalyzer.cpp b/dbms/src/Interpreters/ExpressionAnalyzer.cpp index ee15c1ee7d9..aa64a2f1833 100644 --- a/dbms/src/Interpreters/ExpressionAnalyzer.cpp +++ b/dbms/src/Interpreters/ExpressionAnalyzer.cpp @@ -201,7 +201,16 @@ ExpressionAnalyzer::ExpressionAnalyzer( } if (storage && source_columns.empty()) - source_columns = storage->getColumns().getAllPhysical(); + { + auto physical_columns = storage->getColumns().getAllPhysical(); + if (source_columns.empty()) + source_columns.swap(physical_columns); + else + { + source_columns.insert(source_columns.end(), physical_columns.begin(), physical_columns.end()); + removeDuplicateColumns(source_columns); + } + } else removeDuplicateColumns(source_columns); diff --git a/dbms/src/Interpreters/InterpreterSelectQuery.cpp b/dbms/src/Interpreters/InterpreterSelectQuery.cpp index 686d30dfb7a..f0e7c415280 100644 --- a/dbms/src/Interpreters/InterpreterSelectQuery.cpp +++ b/dbms/src/Interpreters/InterpreterSelectQuery.cpp @@ -677,8 +677,6 @@ void InterpreterSelectQuery::executeFetchColumns( /// Separate expression for columns used in prewhere. auto required_prewhere_columns_expr_list = std::make_shared(); - /// Columns which we will get after prewhere execution. - auto source_columns = storage->getColumns().getAllPhysical(); for (const auto & column : required_columns) { @@ -701,6 +699,8 @@ void InterpreterSelectQuery::executeFetchColumns( required_columns_expr_list->children.emplace_back(std::move(column_expr)); } + /// Columns which we will get after prewhere execution. + NamesAndTypesList additional_source_columns; /// Add columns which will be added by prewhere (otherwise we will remove them in project action). for (const auto & column : prewhere_actions_result) { @@ -708,10 +708,10 @@ void InterpreterSelectQuery::executeFetchColumns( continue; required_columns_expr_list->children.emplace_back(std::make_shared(column.name)); - source_columns.emplace_back(column.name, column.type); + additional_source_columns.emplace_back(column.name, column.type); } - alias_actions = ExpressionAnalyzer(required_columns_expr_list, context, nullptr, source_columns).getActions(true); + alias_actions = ExpressionAnalyzer(required_columns_expr_list, context, storage, additional_source_columns).getActions(true); /// The set of required columns could be added as a result of adding an action to calculate ALIAS. required_columns = alias_actions->getRequiredColumns(); From a6b51d1fc75105898ab6ff3a7609a1fc7068d66f Mon Sep 17 00:00:00 2001 From: Nikolai Kochetov Date: Thu, 6 Sep 2018 22:09:23 +0300 Subject: [PATCH 2/2] Fix virtual columns for prewhere with aliases. --- dbms/src/Interpreters/InterpreterSelectQuery.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/dbms/src/Interpreters/InterpreterSelectQuery.cpp b/dbms/src/Interpreters/InterpreterSelectQuery.cpp index be12d202955..01d3c28bedf 100644 --- a/dbms/src/Interpreters/InterpreterSelectQuery.cpp +++ b/dbms/src/Interpreters/InterpreterSelectQuery.cpp @@ -678,10 +678,6 @@ void InterpreterSelectQuery::executeFetchColumns( /// Separate expression for columns used in prewhere. auto required_prewhere_columns_expr_list = std::make_shared(); - /// Columns which we will get after prewhere execution. - auto source_columns = storage->getColumns().getAllPhysical(); - auto physical_columns = ext::map(source_columns, [] (const auto & it) { return it.name; }); - for (const auto & column : required_columns) { ASTPtr column_expr; @@ -714,6 +710,7 @@ void InterpreterSelectQuery::executeFetchColumns( required_columns_expr_list->children.emplace_back(std::make_shared(column.name)); additional_source_columns.emplace_back(column.name, column.type); } + auto additional_source_columns_set = ext::map(additional_source_columns, [] (const auto & it) { return it.name; }); alias_actions = ExpressionAnalyzer(required_columns_expr_list, context, storage, additional_source_columns).getActions(true); @@ -730,7 +727,7 @@ void InterpreterSelectQuery::executeFetchColumns( size_t next_req_column_pos = 0; for (size_t i = 0; i < required_columns.size(); ++i) { - if (physical_columns.count(required_columns[i])) + if (!additional_source_columns_set.count(required_columns[i])) { if (next_req_column_pos < i) std::swap(required_columns[i], required_columns[next_req_column_pos]);