This commit is contained in:
kssenii 2022-12-06 16:04:26 +01:00
parent f2cad33f1b
commit 862da8f459
3 changed files with 61 additions and 20 deletions

View File

@ -40,6 +40,7 @@
#include <Parsers/ASTSelectWithUnionQuery.h>
#include <Parsers/ASTTablesInSelectQuery.h>
#include <Parsers/ASTInterpolateElement.h>
#include <Parsers/ASTOrderByElement.h>
#include <Parsers/queryToString.h>
#include <DataTypes/NestedUtils.h>
@ -325,6 +326,39 @@ struct ExistsExpressionData
using ExistsExpressionVisitor = InDepthNodeVisitor<OneTypeMatcher<ExistsExpressionData>, false>;
struct ReplacePositionalArgumentsData
{
using TypeToVisit = ASTSelectQuery;
ContextPtr context;
void visit(ASTSelectQuery & select_query, ASTPtr &) const
{
if (context->getSettingsRef().enable_positional_arguments)
{
if (select_query.groupBy())
{
for (auto & expr : select_query.groupBy()->children)
replaceForPositionalArguments(expr, &select_query, ASTSelectQuery::Expression::GROUP_BY);
}
if (select_query.orderBy())
{
for (auto & expr : select_query.orderBy()->children)
{
auto & elem = assert_cast<ASTOrderByElement &>(*expr).children.at(0);
replaceForPositionalArguments(elem, &select_query, ASTSelectQuery::Expression::ORDER_BY);
}
}
if (select_query.limitBy())
{
for (auto & expr : select_query.limitBy()->children)
replaceForPositionalArguments(expr, &select_query, ASTSelectQuery::Expression::LIMIT_BY);
}
}
}
};
using ReplacePositionalArgumentsVisitor = InDepthNodeVisitor<OneTypeMatcher<ReplacePositionalArgumentsData>, false>;
/// Translate qualified names such as db.table.column, table.column, table_alias.column to names' normal form.
/// Expand asterisks and qualified asterisks with column names.
/// There would be columns in normal form & column aliases after translation. Column & column alias would be normalized in QueryNormalizer.
@ -1316,25 +1350,6 @@ TreeRewriterResultPtr TreeRewriter::analyzeSelect(
all_source_columns_set.insert(name);
}
if (getContext()->getSettingsRef().enable_positional_arguments)
{
if (select_query->groupBy())
{
for (auto & expr : select_query->groupBy()->children)
replaceForPositionalArguments(expr, select_query, ASTSelectQuery::Expression::GROUP_BY);
}
if (select_query->orderBy())
{
for (auto & expr : select_query->orderBy()->children)
replaceForPositionalArguments(expr, select_query, ASTSelectQuery::Expression::ORDER_BY);
}
if (select_query->limitBy())
{
for (auto & expr : select_query->limitBy()->children)
replaceForPositionalArguments(expr, select_query, ASTSelectQuery::Expression::LIMIT_BY);
}
}
normalize(query, result.aliases, all_source_columns_set, select_options.ignore_alias, settings, /* allow_self_aliases = */ true, getContext());
// expand GROUP BY ALL
@ -1493,6 +1508,9 @@ void TreeRewriter::normalize(
ExistsExpressionVisitor::Data exists;
ExistsExpressionVisitor(exists).visit(query);
ReplacePositionalArgumentsVisitor::Data data_replace_positional_arguments{context_};
ReplacePositionalArgumentsVisitor(data_replace_positional_arguments).visit(query);
if (settings.transform_null_in)
{
CustomizeInVisitor::Data data_null_in{"nullIn"};

View File

@ -119,9 +119,25 @@ select b from (select 5 as a, 'Hello' as b order by 1);
Hello
drop table if exists tp2;
create table tp2(first_col String, second_col Int32) engine = MergeTree() order by tuple();
insert into tp2 select 'bbb', 1;
insert into tp2 select 'aaa', 2;
select count(*) from (select first_col, count(second_col) from tp2 group by 1);
0
2
select total from (select first_col, count(second_col) as total from tp2 group by 1);
1
1
select first_col from (select first_col, second_col as total from tp2 order by 1 desc);
bbb
aaa
select first_col from (select first_col, second_col as total from tp2 order by 2 desc);
aaa
bbb
select max from (select max(first_col) as max, second_col as total from tp2 group by 2) order by 1;
aaa
bbb
with res as (select first_col from (select first_col, second_col as total from tp2 order by 2 desc) limit 1)
select * from res;
aaa
drop table if exists test;
create table test
(

View File

@ -51,8 +51,15 @@ select b from (select 5 as a, 'Hello' as b order by 1);
drop table if exists tp2;
create table tp2(first_col String, second_col Int32) engine = MergeTree() order by tuple();
insert into tp2 select 'bbb', 1;
insert into tp2 select 'aaa', 2;
select count(*) from (select first_col, count(second_col) from tp2 group by 1);
select total from (select first_col, count(second_col) as total from tp2 group by 1);
select first_col from (select first_col, second_col as total from tp2 order by 1 desc);
select first_col from (select first_col, second_col as total from tp2 order by 2 desc);
select max from (select max(first_col) as max, second_col as total from tp2 group by 2) order by 1;
with res as (select first_col from (select first_col, second_col as total from tp2 order by 2 desc) limit 1)
select * from res;
drop table if exists test;
create table test