add replaceAliasColumnsInFilter function

This commit is contained in:
sundy-li 2020-11-22 12:16:27 +08:00
parent f4808df41e
commit fe8b11fd5f
6 changed files with 43 additions and 13 deletions

View File

@ -26,16 +26,16 @@ void ColumnAliasesMatcher::visit(ASTPtr & ast, Data & data)
{ {
if (auto column_name = IdentifierSemantic::getColumnName(*node)) if (auto column_name = IdentifierSemantic::getColumnName(*node))
{ {
if (const auto column_default = data.columns.getDefault(column_name.value())) if (const auto column_default = data.columns.getDefault(*column_name))
{ {
if (column_default.value().kind == ColumnDefaultKind::Alias) if (column_default->kind == ColumnDefaultKind::Alias)
{ {
const auto alias_columns = data.columns.getAliases(); const auto alias_columns = data.columns.getAliases();
for (const auto & alias_column : alias_columns) for (const auto & alias_column : alias_columns)
{ {
if (alias_column.name == column_name.value()) if (alias_column.name == *column_name)
{ {
ast = addTypeConversionToAST(column_default.value().expression->clone(), alias_column.type->getName()); ast = addTypeConversionToAST(column_default->expression->clone(), alias_column.type->getName());
//revisit ast to track recursive alias columns //revisit ast to track recursive alias columns
Visitor(data).visit(ast); Visitor(data).visit(ast);
break; break;

View File

@ -21,6 +21,7 @@
#include <Interpreters/InterpreterSetQuery.h> #include <Interpreters/InterpreterSetQuery.h>
#include <Interpreters/evaluateConstantExpression.h> #include <Interpreters/evaluateConstantExpression.h>
#include <Interpreters/convertFieldToType.h> #include <Interpreters/convertFieldToType.h>
#include <Interpreters/replaceAliasColumnsInFilter.h>
#include <Interpreters/addTypeConversionToAST.h> #include <Interpreters/addTypeConversionToAST.h>
#include <Interpreters/ExpressionAnalyzer.h> #include <Interpreters/ExpressionAnalyzer.h>
#include <Interpreters/getTableExpressions.h> #include <Interpreters/getTableExpressions.h>
@ -1182,7 +1183,7 @@ void InterpreterSelectQuery::executeFetchColumns(
else // It's possible to optimize count() given only partition predicates else // It's possible to optimize count() given only partition predicates
{ {
SelectQueryInfo temp_query_info; SelectQueryInfo temp_query_info;
temp_query_info.query = query_ptr; temp_query_info.query = replaceAliasColumnsInFilter(query_ptr->clone(), storage->getInMemoryMetadata().getColumns());
temp_query_info.syntax_analyzer_result = syntax_analyzer_result; temp_query_info.syntax_analyzer_result = syntax_analyzer_result;
temp_query_info.sets = query_analyzer->getPreparedSets(); temp_query_info.sets = query_analyzer->getPreparedSets();
num_rows = storage->totalRowsByPartitionPredicate(temp_query_info, *context); num_rows = storage->totalRowsByPartitionPredicate(temp_query_info, *context);

View File

@ -0,0 +1,22 @@
#include <Interpreters/replaceAliasColumnsInFilter.h>
#include <Interpreters/ColumnAliasesVisitor.h>
#include <Storages/ColumnsDescription.h>
#include <Parsers/ASTSelectQuery.h>
namespace DB
{
ASTPtr replaceAliasColumnsInFilter(ASTPtr && ast, const ColumnsDescription & columns)
{
auto & temp_select = ast->as<ASTSelectQuery &>();
ColumnAliasesVisitor::Data aliase_column_data(columns);
ColumnAliasesVisitor aliase_column_visitor(aliase_column_data);
if (temp_select.where())
aliase_column_visitor.visit(temp_select.refWhere());
if (temp_select.prewhere())
aliase_column_visitor.visit(temp_select.refPrewhere());
return ast;
}
}

View File

@ -0,0 +1,12 @@
#pragma once
#include <common/types.h>
#include <Parsers/IAST_fwd.h>
namespace DB
{
class ColumnsDescription;
ASTPtr replaceAliasColumnsInFilter(ASTPtr && ast, const ColumnsDescription & columns);
}

View File

@ -153,6 +153,7 @@ SRCS(
interpretSubquery.cpp interpretSubquery.cpp
join_common.cpp join_common.cpp
loadMetadata.cpp loadMetadata.cpp
replaceAliasColumnsInFilter.cpp
sortBlock.cpp sortBlock.cpp
) )

View File

@ -23,6 +23,7 @@
#include <Parsers/parseIdentifierOrStringLiteral.h> #include <Parsers/parseIdentifierOrStringLiteral.h>
#include <Interpreters/ExpressionAnalyzer.h> #include <Interpreters/ExpressionAnalyzer.h>
#include <Interpreters/ColumnAliasesVisitor.h> #include <Interpreters/ColumnAliasesVisitor.h>
#include <Interpreters/replaceAliasColumnsInFilter.h>
#include <Interpreters/Context.h> #include <Interpreters/Context.h>
#include <Processors/ConcatProcessor.h> #include <Processors/ConcatProcessor.h>
#include <Processors/QueryPlan/QueryPlan.h> #include <Processors/QueryPlan/QueryPlan.h>
@ -214,14 +215,7 @@ QueryPlanPtr MergeTreeDataSelectExecutor::readFromParts(
SelectQueryInfo query_info_for_index = query_info; SelectQueryInfo query_info_for_index = query_info;
if (!metadata_snapshot->getColumns().getAliases().empty()) if (!metadata_snapshot->getColumns().getAliases().empty())
{ {
query_info_for_index.query = query_info.query->clone(); query_info_for_index.query = replaceAliasColumnsInFilter(query_info.query->clone(), metadata_snapshot->getColumns());
auto & temp_select = query_info_for_index.query->as<ASTSelectQuery &>();
ColumnAliasesVisitor::Data aliase_column_data(metadata_snapshot->getColumns());
ColumnAliasesVisitor aliase_column_visitor(aliase_column_data);
if (temp_select.where())
aliase_column_visitor.visit(temp_select.refWhere());
if (temp_select.prewhere())
aliase_column_visitor.visit(temp_select.refPrewhere());
} }
KeyCondition key_condition(query_info_for_index, context, primary_key_columns, primary_key.expression); KeyCondition key_condition(query_info_for_index, context, primary_key_columns, primary_key.expression);