diff --git a/src/Interpreters/TreeRewriter.cpp b/src/Interpreters/TreeRewriter.cpp index c93045a0fc1..5b28af6479e 100644 --- a/src/Interpreters/TreeRewriter.cpp +++ b/src/Interpreters/TreeRewriter.cpp @@ -1239,6 +1239,21 @@ TreeRewriterResultPtr TreeRewriter::analyzeSelect( source_columns_set, right_table.table.getQualifiedNamePrefix()); } + translateQualifiedNames(query, *select_query, source_columns_set, tables_with_columns); + + /// Optimizes logical expressions. + LogicalExpressionsOptimizer(select_query, settings.optimize_min_equality_disjunction_chain_length.value).perform(); + + NameSet all_source_columns_set = source_columns_set; + if (table_join) + { + for (const auto & [name, _] : table_join->columns_from_joined_table) + all_source_columns_set.insert(name); + } + + normalize(query, result.aliases, all_source_columns_set, select_options.ignore_alias, settings, /* allow_self_aliases = */ true, getContext()); + + if (getContext()->getSettingsRef().enable_positional_arguments) { if (select_query->groupBy()) @@ -1258,20 +1273,6 @@ TreeRewriterResultPtr TreeRewriter::analyzeSelect( } } - translateQualifiedNames(query, *select_query, source_columns_set, tables_with_columns); - - /// Optimizes logical expressions. - LogicalExpressionsOptimizer(select_query, settings.optimize_min_equality_disjunction_chain_length.value).perform(); - - NameSet all_source_columns_set = source_columns_set; - if (table_join) - { - for (const auto & [name, _] : table_join->columns_from_joined_table) - all_source_columns_set.insert(name); - } - - normalize(query, result.aliases, all_source_columns_set, select_options.ignore_alias, settings, /* allow_self_aliases = */ true, getContext()); - /// Remove unneeded columns according to 'required_result_columns'. /// Leave all selected columns in case of DISTINCT; columns that contain arrayJoin function inside. /// Must be after 'normalizeTree' (after expanding aliases, for aliases not get lost) diff --git a/src/Interpreters/replaceForPositionalArguments.cpp b/src/Interpreters/replaceForPositionalArguments.cpp index deac5801d7a..410cc5bb10c 100644 --- a/src/Interpreters/replaceForPositionalArguments.cpp +++ b/src/Interpreters/replaceForPositionalArguments.cpp @@ -8,6 +8,11 @@ namespace DB { +namespace ErrorCodes +{ + extern const int ILLEGAL_TYPE_OF_ARGUMENT; +} + bool replaceForPositionalArguments(ASTPtr & argument, const ASTSelectQuery * select_query, ASTSelectQuery::Expression expression) { auto columns = select_query->select()->children; diff --git a/src/Interpreters/replaceForPositionalArguments.h b/src/Interpreters/replaceForPositionalArguments.h index 9906978c561..9e238ba32d3 100644 --- a/src/Interpreters/replaceForPositionalArguments.h +++ b/src/Interpreters/replaceForPositionalArguments.h @@ -1,3 +1,4 @@ +#pragma once #include namespace DB diff --git a/tests/queries/0_stateless/00257_shard_no_aggregates_and_constant_keys.sql b/tests/queries/0_stateless/00257_shard_no_aggregates_and_constant_keys.sql index 48336faf382..93fa1f440b1 100644 --- a/tests/queries/0_stateless/00257_shard_no_aggregates_and_constant_keys.sql +++ b/tests/queries/0_stateless/00257_shard_no_aggregates_and_constant_keys.sql @@ -1,5 +1,7 @@ -- Tags: shard +set enable_positional_arguments=0; + select 40 as z from (select * from system.numbers limit 3) group by z; select 41 as z from remote('127.0.0.{2,3}', system.one) group by z; select count(), 42 AS z from remote('127.0.0.{2,3}', system.one) group by z; diff --git a/tests/queries/0_stateless/01119_optimize_trivial_insert_select.sql b/tests/queries/0_stateless/01119_optimize_trivial_insert_select.sql index 001e758284f..a53b60a5ad3 100644 --- a/tests/queries/0_stateless/01119_optimize_trivial_insert_select.sql +++ b/tests/queries/0_stateless/01119_optimize_trivial_insert_select.sql @@ -1,6 +1,7 @@ drop table if exists t; create table t(n int, a Int64, s String) engine = MergeTree() order by a; +set enable_positional_arguments=0; set optimize_trivial_insert_select=1; -- due to aggregate functions, optimize_trivial_insert_select will not be applied diff --git a/tests/queries/0_stateless/01414_optimize_any_bug.sql b/tests/queries/0_stateless/01414_optimize_any_bug.sql index 6f6f291c504..ec24a09fc11 100644 --- a/tests/queries/0_stateless/01414_optimize_any_bug.sql +++ b/tests/queries/0_stateless/01414_optimize_any_bug.sql @@ -8,6 +8,7 @@ CREATE TABLE test ENGINE = MergeTree() ORDER BY tuple(); +SET enable_positional_arguments=0; SET optimize_move_functions_out_of_any = 1; SELECT any(arrayFilter((c, d) -> (4 = d), `Source.C1`, `Source.C2`)[1]) AS x diff --git a/tests/queries/0_stateless/01798_having_push_down.sql b/tests/queries/0_stateless/01798_having_push_down.sql index 28b97b7f065..b3a77c8f5b5 100644 --- a/tests/queries/0_stateless/01798_having_push_down.sql +++ b/tests/queries/0_stateless/01798_having_push_down.sql @@ -12,6 +12,7 @@ SELECT c0 + -1, sum(intDivOrZero(intDivOrZero(NULL, NULL), '2'), intDivOrZero(10 SELECT sum(c0 + 257) FROM t_having GROUP BY c0 = -9223372036854775808, NULL, -2147483649, c0 HAVING c0 = -9223372036854775808 SETTINGS enable_optimize_predicate_expression = 0; +SET enable_positional_arguments=0; SELECT c0 + -2, c0 + -9223372036854775807, c0 = NULL FROM t_having GROUP BY c0 = 0.9998999834060669, 1023, c0 HAVING c0 = 0.9998999834060669 SETTINGS enable_optimize_predicate_expression = 0; DROP TABLE t_having;