Better UNION ALL: development #1947

This commit is contained in:
Alexey Milovidov 2018-02-28 00:03:28 +03:00
parent 492053bc43
commit 17ffe77b8c
2 changed files with 20 additions and 37 deletions

View File

@ -165,17 +165,28 @@ ExpressionAnalyzer::ExpressionAnalyzer(
: ast(ast_), context(context_), settings(context.getSettings()), : ast(ast_), context(context_), settings(context.getSettings()),
subquery_depth(subquery_depth_), subquery_depth(subquery_depth_),
source_columns(source_columns_), required_result_columns(required_result_columns_.begin(), required_result_columns_.end()), source_columns(source_columns_), required_result_columns(required_result_columns_.begin(), required_result_columns_.end()),
storage(storage_ ? storage_ : getTable()), storage(storage_),
do_global(do_global_), subqueries_for_sets(subqueries_for_set_) do_global(do_global_), subqueries_for_sets(subqueries_for_set_)
{
init();
}
void ExpressionAnalyzer::init()
{ {
select_query = typeid_cast<ASTSelectQuery *>(ast.get()); select_query = typeid_cast<ASTSelectQuery *>(ast.get());
if (!storage && select_query)
{
auto select_database = select_query->database();
auto select_table = select_query->table();
if (select_table
&& !typeid_cast<const ASTSelectWithUnionQuery *>(select_table.get())
&& !typeid_cast<const ASTFunction *>(select_table.get()))
{
String database = select_database
? typeid_cast<const ASTIdentifier &>(*select_database).name
: "";
const String & table = typeid_cast<const ASTIdentifier &>(*select_table).name;
storage = context.tryGetTable(database, table);
}
}
removeDuplicateColumns(source_columns); removeDuplicateColumns(source_columns);
addAliasColumns(); addAliasColumns();
@ -909,29 +920,6 @@ void ExpressionAnalyzer::addASTAliases(ASTPtr & ast, int ignore_levels)
} }
StoragePtr ExpressionAnalyzer::getTable()
{
if (const ASTSelectQuery * select = typeid_cast<const ASTSelectQuery *>(ast.get()))
{
auto select_database = select->database();
auto select_table = select->table();
if (select_table
&& !typeid_cast<const ASTSelectWithUnionQuery *>(select_table.get())
&& !typeid_cast<const ASTFunction *>(select_table.get()))
{
String database = select_database
? typeid_cast<const ASTIdentifier &>(*select_database).name
: "";
const String & table = typeid_cast<const ASTIdentifier &>(*select_table).name;
return context.tryGetTable(database, table);
}
}
return StoragePtr();
}
void ExpressionAnalyzer::normalizeTree() void ExpressionAnalyzer::normalizeTree()
{ {
SetOfASTs tmp_set; SetOfASTs tmp_set;

View File

@ -161,8 +161,8 @@ private:
/// Columns after ARRAY JOIN, JOIN, and/or aggregation. /// Columns after ARRAY JOIN, JOIN, and/or aggregation.
NamesAndTypesList aggregated_columns; NamesAndTypesList aggregated_columns;
/// The table from which the query is made. /// The main table in FROM clause, if exists.
const StoragePtr storage; StoragePtr storage;
bool has_aggregation = false; bool has_aggregation = false;
NamesAndTypesList aggregation_keys; NamesAndTypesList aggregation_keys;
@ -211,8 +211,6 @@ private:
Tables external_tables; Tables external_tables;
size_t external_table_id = 1; size_t external_table_id = 1;
void init();
static NamesAndTypesList::iterator findColumn(const String & name, NamesAndTypesList & cols); static NamesAndTypesList::iterator findColumn(const String & name, NamesAndTypesList & cols);
NamesAndTypesList::iterator findColumn(const String & name) { return findColumn(name, source_columns); } NamesAndTypesList::iterator findColumn(const String & name) { return findColumn(name, source_columns); }
@ -307,9 +305,6 @@ private:
const NameSet & available_columns, NameSet & required_source_columns, NameSet & ignored_names, const NameSet & available_columns, NameSet & required_source_columns, NameSet & ignored_names,
const NameSet & available_joined_columns, NameSet & required_joined_columns); const NameSet & available_joined_columns, NameSet & required_joined_columns);
/// Get the table from which the query is made
StoragePtr getTable();
/// columns - the columns that are present before the transformations begin. /// columns - the columns that are present before the transformations begin.
void initChain(ExpressionActionsChain & chain, const NamesAndTypesList & columns) const; void initChain(ExpressionActionsChain & chain, const NamesAndTypesList & columns) const;