#include #include #include #include #include #include #include namespace DB { namespace ErrorCodes { extern const int LOGICAL_ERROR; } static ASTExpressionList * extractTableFunctionArgumentsFromSelectQuery(ASTPtr & query) { auto * select_query = query->as(); if (!select_query || !select_query->tables()) return nullptr; auto * tables = select_query->tables()->as(); auto * table_expression = tables->children[0]->as()->table_expression->as(); if (!table_expression->table_function) return nullptr; auto * table_function = table_expression->table_function->as(); return table_function->arguments->as(); } void addColumnsStructureToQueryWithClusterEngine(ASTPtr & query, const String & structure, size_t max_arguments, const String & function_name) { ASTExpressionList * expression_list = extractTableFunctionArgumentsFromSelectQuery(query); if (!expression_list) throw Exception(ErrorCodes::LOGICAL_ERROR, "Expected SELECT query from table function {}, got '{}'", function_name, queryToString(query)); auto structure_literal = std::make_shared(structure); if (expression_list->children.size() < 2 || expression_list->children.size() > max_arguments) throw Exception(ErrorCodes::LOGICAL_ERROR, "Expected 2 to {} arguments in {} table functions, got {}", function_name, max_arguments, expression_list->children.size()); if (expression_list->children.size() == 2 || expression_list->children.size() == max_arguments - 1) { auto format_literal = std::make_shared("auto"); expression_list->children.push_back(format_literal); } expression_list->children.push_back(structure_literal); } }