diff --git a/dbms/include/DB/Interpreters/ExpressionAnalyzer.h b/dbms/include/DB/Interpreters/ExpressionAnalyzer.h index d7a1350b4f5..b1b221a0f4e 100644 --- a/dbms/include/DB/Interpreters/ExpressionAnalyzer.h +++ b/dbms/include/DB/Interpreters/ExpressionAnalyzer.h @@ -208,6 +208,9 @@ private: */ void collectJoinedColumns(NameSet & joined_columns, NamesAndTypesList & joined_columns_name_type); + /** Добавляет ALIAS столбцы из storage в aliases, если запрос не является SELECT с ARRAY JOIN. При наличии + * ARRAY JOIN их добавлять нельзя, иначе ломается логика его выполнения. + */ void addStorageAliases(); /** Создать словарь алиасов. @@ -229,6 +232,9 @@ private: /// Превратить перечисление значений или подзапрос в ASTSet. node - функция in или notIn. void makeSet(ASTFunction * node, const Block & sample_block); + /// Добавляет список ALIAS столбцов из таблицы + void addAliasColumns(); + /// Замена скалярных подзапросов на значения-константы. void executeScalarSubqueries(); void executeScalarSubqueriesImpl(ASTPtr & ast); diff --git a/dbms/src/Interpreters/ExpressionAnalyzer.cpp b/dbms/src/Interpreters/ExpressionAnalyzer.cpp index 71b4fe794b5..49d41bc3195 100644 --- a/dbms/src/Interpreters/ExpressionAnalyzer.cpp +++ b/dbms/src/Interpreters/ExpressionAnalyzer.cpp @@ -117,15 +117,18 @@ void ExpressionAnalyzer::init() /// Оптимизирует логические выражения. LogicalExpressionsOptimizer(select_query, settings).perform(); - /// Добавляет в множество известных алиасов те, которые объявлены в структуре таблицы (ALIAS-столбцы). - addStorageAliases(); - /// Создаёт словарь aliases: alias -> ASTPtr addASTAliases(ast); + /// Добавляет ALIAS столбцы из стаблицы в aliases, если применимо. + addStorageAliases(); + /// Common subexpression elimination. Rewrite rules. normalizeTree(); + /// ALIAS столбцы не должны подставляться вместо ASTAsterisk, добавим их теперь, после normalizeTree. + addAliasColumns(); + /// Выполнение скалярных подзапросов - замена их на значения-константы. executeScalarSubqueries(); @@ -317,12 +320,17 @@ NamesAndTypesList::iterator ExpressionAnalyzer::findColumn(const String & name, void ExpressionAnalyzer::addStorageAliases() { + if (select_query && select_query->array_join_expression_list) + return; + if (!storage) return; /// @todo: consider storing default expressions with alias set to avoid cloning + /// Добавляем ALIAS из таблицы, только если такого ALIAS еще не объявлено в запросе. for (const auto & alias : storage->alias_columns) - (aliases[alias.name] = storage->column_defaults[alias.name].expression->clone())->setAlias(alias.name); + if (!aliases.count(alias.name)) + aliases[alias.name] = setAlias(storage->column_defaults[alias.name].expression->clone(), alias.name); } @@ -566,6 +574,18 @@ void ExpressionAnalyzer::normalizeTreeImpl( } +void ExpressionAnalyzer::addAliasColumns() +{ + if (!(select_query && select_query->array_join_expression_list)) + return; + + if (!storage) + return; + + columns.insert(std::end(columns), std::begin(storage->alias_columns), std::end(storage->alias_columns)); +} + + void ExpressionAnalyzer::executeScalarSubqueries() { if (!select_query) @@ -2256,19 +2276,6 @@ void ExpressionAnalyzer::collectUsedColumns() ++it; } - for (NamesAndTypesList::iterator it = columns.begin(); it != columns.end();) - { - unknown_required_columns.erase(it->name); - - if (!required.count(it->name)) - { - required.erase(it->name); - columns.erase(it++); - } - else - ++it; - } - /// Возможно, среди неизвестных столбцов есть виртуальные. Удаляем их из списка неизвестных и добавляем /// в columns list, чтобы при дальнейшей обработке запроса они воспринимались как настоящие. if (storage) diff --git a/dbms/src/Interpreters/InterpreterSelectQuery.cpp b/dbms/src/Interpreters/InterpreterSelectQuery.cpp index 7c3d43b63c3..fcbc3e4cb67 100644 --- a/dbms/src/Interpreters/InterpreterSelectQuery.cpp +++ b/dbms/src/Interpreters/InterpreterSelectQuery.cpp @@ -34,6 +34,7 @@ #include + namespace DB { @@ -650,6 +651,37 @@ QueryProcessingStage::Enum InterpreterSelectQuery::executeFetchColumns() /// Список столбцов, которых нужно прочитать, чтобы выполнить запрос. Names required_columns = query_analyzer->getRequiredColumns(); + /// Действия для вычисления ALIAS, если потребуется. + ExpressionActionsPtr alias_actions; + /// Требуются ли ALIAS столбцы для выполнения запроса? + auto alias_columns_required = false; + + if (storage && !storage->alias_columns.empty()) + { + for (const auto & column : required_columns) + { + const auto default_it = storage->column_defaults.find(column); + if (default_it != std::end(storage->column_defaults) && default_it->second.type == ColumnDefaultType::Alias) + { + alias_columns_required = true; + break; + } + } + + if (alias_columns_required) + { + /// Составим выражение для возврата всех запрошенных столбцов, с вычислением требуемых ALIAS столбцов. + ASTPtr required_columns_expr_list{new ASTExpressionList}; + + for (const auto & column : required_columns) + required_columns_expr_list->children.emplace_back(new ASTIdentifier{{}, column}); + + alias_actions = ExpressionAnalyzer{required_columns_expr_list, context, storage, table_column_names}.getActions(true); + + /// Множество требуемых столбцов могло быть дополнено в результате добавления действия для вычисления ALIAS. + required_columns = alias_actions->getRequiredColumns(); + } + } if (query.table && typeid_cast(query.table.get())) { @@ -761,6 +793,13 @@ QueryProcessingStage::Enum InterpreterSelectQuery::executeFetchColumns() context, settings_for_storage, from_stage, settings.max_block_size, max_streams); + if (alias_actions) + /// Обернем каждый поток, возвращенный из таблицы, с целью вычисления и добавления ALIAS столбцов + transformStreams([&] (auto & stream) + { + stream = new ExpressionBlockInputStream{stream, alias_actions}; + }); + transformStreams([&](auto & stream) { stream->addTableLock(table_lock); diff --git a/dbms/tests/queries/0_stateless/00261_storage_aliases_and_array_join.reference b/dbms/tests/queries/0_stateless/00261_storage_aliases_and_array_join.reference new file mode 100644 index 00000000000..da01364729f --- /dev/null +++ b/dbms/tests/queries/0_stateless/00261_storage_aliases_and_array_join.reference @@ -0,0 +1,189 @@ +-- Ensure ALIAS columns are not selected by asterisk +0000-00-00 0 ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] +-- select DEFAULT and ALIAS arrays +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +-- select DEFAULT and ALIAS nested columns +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +zero zero +one one +two two +zero ['zero','one','two'] +one ['zero','one','two'] +two ['zero','one','two'] +['zero','one','two'] zero +['zero','one','two'] one +['zero','one','two'] two +-- array join, but request the original columns +['zero','one','two'] ['zero','one','two'] +['zero','one','two'] ['zero','one','two'] +['zero','one','two'] ['zero','one','two'] +-- array join, do not use the result +['zero','one','two'] +['zero','one','two'] +['zero','one','two'] +['zero','one','two'] +['zero','one','two'] +['zero','one','two'] +-- select DEFAULT and ALIAS arrays, array joining one at a time +['zero','one','two'] zero ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] one ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] two ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] zero ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] one ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] two ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] zero ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] one ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] two ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] zero ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] one ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] two ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] 0 ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] 1 ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] 2 ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] 0 ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] 1 ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] 2 ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] 0 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] 1 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] 2 +-- select DEFAULT and ALIAS arrays, array joining one at a time and aliasing result with original name +['zero','one','two'] zero ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] one ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] two ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] zero ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] one ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] two ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] zero ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] one ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] two ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] zero ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] one ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] two ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] 0 ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] 1 ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] 2 ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] 0 ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] 1 ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] 2 ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] 0 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] 1 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] 2 +-- select DEFAULT and ALIAS arrays and array join result, aliased as `joined` +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] zero +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] one +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] two +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] zero +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] one +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] two +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] zero +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] one +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] two +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] zero +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] one +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] two +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 0 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 1 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 2 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 0 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 1 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 2 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 0 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 1 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 2 +-- select DEFAULT and ALIAS nested columns, array joining one at a time +['zero','one','two'] zero ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] one ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] two ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] zero ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] one ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] two ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] zero ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] one ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] two ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] zero ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] one ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] two ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] 0 ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] 1 ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] 2 ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] 0 ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] 1 ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] 2 ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] 0 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] 1 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] 2 +-- select DEFAULT and ALIAS nested columns, array joining one at a time and aliasing result with original name +['zero','one','two'] zero ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] one ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] two ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] zero ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] one ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] two ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] zero ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] one ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] two ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] zero ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] one ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] two ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] 0 ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] 1 ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] 2 ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] 0 ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] 1 ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] 2 ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] 0 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] 1 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] 2 +-- select DEFAULT and ALIAS nested columns and array join result, aliased as `joined` +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] zero +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] one +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] two +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] zero +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] one +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] two +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] zero +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] one +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] two +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] zero +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] one +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] two +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 0 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 1 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 2 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 0 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 1 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 2 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 0 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 1 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] 2 +-- array join whole nested table +['zero','one','two'] zero zero zero zero 0 0 0 +['zero','one','two'] one one one one 1 1 1 +['zero','one','two'] two two two two 2 2 2 +-- array join whole nested table not using the result +['zero','one','two'] +['zero','one','two'] +['zero','one','two'] +-- array join whole nested table, aliasing with original name +['zero','one','two'] zero zero zero zero 0 0 0 +['zero','one','two'] one one one one 1 1 1 +['zero','one','two'] two two two two 2 2 2 +-- array join whole nested table, aliasing with original name not using the result +['zero','one','two'] +['zero','one','two'] +['zero','one','two'] +-- array join whole nested table, aliasing as `class` +['zero','one','two'] zero zero zero zero 0 0 0 +['zero','one','two'] one one one one 1 1 1 +['zero','one','two'] two two two two 2 2 2 +-- array join whole nested table, aliasing as `class` and not using the result +['zero','one','two'] +['zero','one','two'] +['zero','one','two'] +-- array join whole nested table, aliasing as `class` but requesting the original columns +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] zero zero zero zero 0 0 0 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] one one one one 1 1 1 +['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['zero','one','two'] ['0','1','2'] ['0','1','2'] ['0','1','2'] two two two two 2 2 2 diff --git a/dbms/tests/queries/0_stateless/00261_storage_aliases_and_array_join.sql b/dbms/tests/queries/0_stateless/00261_storage_aliases_and_array_join.sql new file mode 100644 index 00000000000..71562ffd6fa --- /dev/null +++ b/dbms/tests/queries/0_stateless/00261_storage_aliases_and_array_join.sql @@ -0,0 +1,114 @@ +drop table if exists aliases_test; + +create table aliases_test ( +date Date, id UInt64, +array default ['zero','one','two'], +d1 default array, +a1 alias array, a2 alias a1, a3 alias a2, +a4 alias arrayMap(x -> toString(x), range(3)), a5 alias a4, a6 alias a5, +`struct.d1` default array, +`struct.a1` alias array, `struct.a2` alias struct.a1, `struct.a3` alias struct.a2, +`struct.a4` alias arrayMap(x -> toString(x), range(3)), `struct.a5` alias struct.a4, `struct.a6` alias struct.a5 +) engine=MergeTree(date, id, 1); + +insert into aliases_test (id) values (0); + +select '-- Ensure ALIAS columns are not selected by asterisk'; +select * from aliases_test; + +select '-- select DEFAULT and ALIAS arrays'; +select d1, a1, a2, a3, a4, a5, a6 from aliases_test; +select '-- select DEFAULT and ALIAS nested columns'; +select struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test; + +select d1, a1 from aliases_test array join d1, a1; +select d1, a1 from aliases_test array join d1, a1 as a2; +select d1, a1 from aliases_test array join d1 as d2, a1; +select '-- array join, but request the original columns'; +select d1, a1 from aliases_test array join d1 as d2, a1 as a2; + +select '-- array join, do not use the result'; +select array from aliases_test array join d1, a1; +select array from aliases_test array join d1 as d2, a1 as a1; + +select '-- select DEFAULT and ALIAS arrays, array joining one at a time'; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join d1; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join a1; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join a2; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join a3; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join a4; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join a5; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join a6; + +select '-- select DEFAULT and ALIAS arrays, array joining one at a time and aliasing result with original name'; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join d1 as d1; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join a1 as a1; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join a2 as a2; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join a3 as a3; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join a4 as a4; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join a5 as a5; +select array, d1, a1, a2, a3, a4, a5, a6 from aliases_test array join a6 as a6; + +select '-- select DEFAULT and ALIAS arrays and array join result, aliased as `joined`'; +select array, d1, a1, a2, a3, a4, a5, a6, joined from aliases_test array join d1 as joined; +select array, d1, a1, a2, a3, a4, a5, a6, joined from aliases_test array join a1 as joined; +select array, d1, a1, a2, a3, a4, a5, a6, joined from aliases_test array join a2 as joined; +select array, d1, a1, a2, a3, a4, a5, a6, joined from aliases_test array join a3 as joined; +select array, d1, a1, a2, a3, a4, a5, a6, joined from aliases_test array join a4 as joined; +select array, d1, a1, a2, a3, a4, a5, a6, joined from aliases_test array join a5 as joined; +select array, d1, a1, a2, a3, a4, a5, a6, joined from aliases_test array join a6 as joined; + +select '-- select DEFAULT and ALIAS nested columns, array joining one at a time'; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.d1; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.a1; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.a2; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.a3; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.a4; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.a5; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.a6; + +select '-- select DEFAULT and ALIAS nested columns, array joining one at a time and aliasing result with original name'; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.d1 as `struct.d1`; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.a1 as `struct.a1`; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.a2 as `struct.a2`; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.a3 as `struct.a3`; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.a4 as `struct.a4`; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.a5 as `struct.a5`; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct.a6 as `struct.a6`; + +select '-- select DEFAULT and ALIAS nested columns and array join result, aliased as `joined`'; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6, joined from aliases_test array join struct.d1 as joined; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6, joined from aliases_test array join struct.a1 as joined; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6, joined from aliases_test array join struct.a2 as joined; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6, joined from aliases_test array join struct.a3 as joined; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6, joined from aliases_test array join struct.a4 as joined; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6, joined from aliases_test array join struct.a5 as joined; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6, joined from aliases_test array join struct.a6 as joined; + +select '-- array join whole nested table'; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct; + +select '-- array join whole nested table not using the result'; +select array from aliases_test array join struct; + +select '-- array join whole nested table, aliasing with original name'; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct as struct; + +select '-- array join whole nested table, aliasing with original name not using the result'; +select array from aliases_test array join struct as struct; + +select '-- array join whole nested table, aliasing as `class`'; +select array, class.d1, class.a1, class.a2, class.a3, class.a4, class.a5, class.a6 from aliases_test array join struct as class; + +select '-- array join whole nested table, aliasing as `class` and not using the result'; +select array from aliases_test array join struct as class; + +select '-- array join whole nested table, aliasing as `class` but requesting the original columns'; +select array, struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6 from aliases_test array join struct as class; + +select array, +struct.d1, struct.a1, struct.a2, struct.a3, struct.a4, struct.a5, struct.a6, +class.d1, class.a1, class.a2, class.a3, class.a4, class.a5, class.a6 +from aliases_test array join struct as class; + +drop table aliases_test; diff --git a/dbms/tests/queries/0_stateless/00262_alter_alias.reference b/dbms/tests/queries/0_stateless/00262_alter_alias.reference new file mode 100644 index 00000000000..0ac7cb12860 --- /dev/null +++ b/dbms/tests/queries/0_stateless/00262_alter_alias.reference @@ -0,0 +1,14 @@ +[0,1,2] +[0,1,2] +[0,1,2] +[0,1,2] [0,1,2] +[0,1,2] [0,1,2] +0 0 +1 1 +2 2 +0 0 +1 1 +2 2 +0 0 +1 1 +2 2 diff --git a/dbms/tests/queries/0_stateless/00262_alter_alias.sql b/dbms/tests/queries/0_stateless/00262_alter_alias.sql new file mode 100644 index 00000000000..5a7007a6e0c --- /dev/null +++ b/dbms/tests/queries/0_stateless/00262_alter_alias.sql @@ -0,0 +1,24 @@ +drop table if exists aliases_test; + +create table aliases_test (date default today(), id default rand(), array default [0, 1, 2]) engine=MergeTree(date, id, 1); + +insert into aliases_test (id) values (0); +select array from aliases_test; + +alter table aliases_test modify column array alias [0, 1, 2]; +select array from aliases_test; + +alter table aliases_test modify column array default [0, 1, 2]; +select array from aliases_test; + +alter table aliases_test add column struct.key default [0, 1, 2], add column struct.value default array; +select struct.key, struct.value from aliases_test; + +alter table aliases_test modify column struct.value alias array; +select struct.key, struct.value from aliases_test; + +select struct.key, struct.value from aliases_test array join struct; +select struct.key, struct.value from aliases_test array join struct as struct; +select class.key, class.value from aliases_test array join struct as class; + +drop table aliases_test;