#include #include #include #include #include namespace DB { void RewriteOrderBy::visit(ASTPtr & ast, Data &) { auto * query = ast->as(); if (!query) return; const ASTPtr & order_by = query->orderBy(); if (!order_by) return; const auto * expr_list = order_by->as(); if (!expr_list) return; if (expr_list->children.size() != 1) return; const auto * order_by_elem = expr_list->children.front()->as(); if (!order_by_elem) return; const auto * func = order_by_elem->children.front()->as(); if (!func || func->name != "tuple") return; if (const auto * inner_list = func->children.front()->as()) { auto new_order_by = std::make_shared(); for (const auto & identifier : inner_list->children) { // clone w/o children auto clone = std::make_shared(*order_by_elem); clone->children.clear(); clone->children.emplace_back(identifier); new_order_by->children.emplace_back(clone); } if (!new_order_by->children.empty()) query->setExpression(ASTSelectQuery::Expression::ORDER_BY, std::move(new_order_by)); } } }