diff --git a/src/Interpreters/InterpreterSelectQuery.cpp b/src/Interpreters/InterpreterSelectQuery.cpp index 5d9baadbfdf..038d52d63c2 100644 --- a/src/Interpreters/InterpreterSelectQuery.cpp +++ b/src/Interpreters/InterpreterSelectQuery.cpp @@ -877,7 +877,7 @@ static FillColumnDescription getWithFillDescription(DataTypePtr type, const ASTO return descr; } -static SortDescription getSortDescription(const ASTSelectQuery & query, const Block & result_block, const Aliases & aliases, ContextPtr context) +static SortDescription getSortDescription(const ASTSelectQuery & query, const Block & source_block, const Block & result_block, const Aliases & aliases, ContextPtr context) { SortDescription order_descr; order_descr.reserve(query.orderBy()->children.size()); @@ -892,6 +892,8 @@ static SortDescription getSortDescription(const ASTSelectQuery & query, const Bl if (order_by_elem.with_fill) { auto column = result_block.findByName(name); + if (!column) + column = source_block.findByName(name); if (!column) for (auto &[alias, ast] : aliases) if (name == ast->getColumnName()) @@ -1504,7 +1506,7 @@ void InterpreterSelectQuery::executeImpl(QueryPlan & query_plan, std::optional
aliases, context);
+ SortDescription order_descr = getSortDescription(query, source_header, result_header, syntax_analyzer_result->aliases, context);
for (auto & desc : order_descr)
if (desc.with_fill)
{
@@ -2117,7 +2119,7 @@ void InterpreterSelectQuery::executeFetchColumns(QueryProcessingStage::Enum proc
// TODO Do we need a projection variant for this field?
query,
analysis_result.order_by_elements_actions,
- getSortDescription(query, result_header, syntax_analyzer_result->aliases, context),
+ getSortDescription(query, source_header, result_header, syntax_analyzer_result->aliases, context),
query_info.syntax_analyzer_result);
}
else
@@ -2125,7 +2127,7 @@ void InterpreterSelectQuery::executeFetchColumns(QueryProcessingStage::Enum proc
query_info.order_optimizer = std::make_shared