diff --git a/src/Parsers/ASTProjectionSelectQuery.cpp b/src/Parsers/ASTProjectionSelectQuery.cpp index 90d9ede7337..4bb1d2eef30 100644 --- a/src/Parsers/ASTProjectionSelectQuery.cpp +++ b/src/Parsers/ASTProjectionSelectQuery.cpp @@ -143,10 +143,19 @@ ASTPtr ASTProjectionSelectQuery::cloneToASTSelect() const if (groupBy()) select_query->setExpression(ASTSelectQuery::Expression::GROUP_BY, groupBy()->clone()); + /// Attach settings to prevent AST transformations. We already have ignored AST optimizations + /// for projection queries. Only remaining settings need to be added here. + /// + /// NOTE: `count_distinct_implementation` has already been selected during the creation of the + /// projection, so there will be no countDistinct(...) to rewrite in projection queries. + /// Ideally, we should aim for a unique and normalized query representation that remains + /// unchanged after the AST rewrite. For instance, we can add -OrEmpty, realIn as the default + /// behavior w.r.t -OrNull, nullIn. auto settings_query = std::make_shared(); SettingsChanges settings_changes; - settings_changes.insertSetting("optimize_aggregators_of_group_by_keys", false); - settings_changes.insertSetting("optimize_group_by_function_keys", false); + settings_changes.insertSetting("aggregate_functions_null_for_empty", false); + settings_changes.insertSetting("transform_null_in", false); + settings_changes.insertSetting("legacy_column_name_of_tuple_literal", false); settings_query->changes = std::move(settings_changes); settings_query->is_standalone = false; select_query->setExpression(ASTSelectQuery::Expression::SETTINGS, std::move(settings_query)); diff --git a/tests/queries/0_stateless/01710_projection_with_ast_rewrite_settings.reference b/tests/queries/0_stateless/01710_projection_with_ast_rewrite_settings.reference new file mode 100644 index 00000000000..ee824fc9e0a --- /dev/null +++ b/tests/queries/0_stateless/01710_projection_with_ast_rewrite_settings.reference @@ -0,0 +1,3 @@ +81920 +81920 +81920 diff --git a/tests/queries/0_stateless/01710_projection_with_ast_rewrite_settings.sql b/tests/queries/0_stateless/01710_projection_with_ast_rewrite_settings.sql new file mode 100644 index 00000000000..1286b0e74eb --- /dev/null +++ b/tests/queries/0_stateless/01710_projection_with_ast_rewrite_settings.sql @@ -0,0 +1,29 @@ +DROP TABLE IF EXISTS aggregate_functions_null_for_empty; + +CREATE TABLE aggregate_functions_null_for_empty (`x` UInt32, `y` UInt64, PROJECTION p (SELECT sum(y))) ENGINE = MergeTree ORDER BY tuple(); + +INSERT INTO aggregate_functions_null_for_empty SELECT number, number * 2 FROM numbers(8192 * 10) SETTINGS aggregate_functions_null_for_empty = true; + +SELECT count() FROM aggregate_functions_null_for_empty; + +DROP TABLE aggregate_functions_null_for_empty; + +DROP TABLE IF EXISTS transform_null_in; + +CREATE TABLE transform_null_in (`x` UInt32, `y` UInt64, PROJECTION p (SELECT sum(y in (1,2,3)))) ENGINE = MergeTree ORDER BY tuple(); + +INSERT INTO transform_null_in SELECT number, number * 2 FROM numbers(8192 * 10) SETTINGS transform_null_in = true; + +SELECT count() FROM transform_null_in; + +DROP TABLE transform_null_in; + +DROP TABLE IF EXISTS legacy_column_name_of_tuple_literal; + +CREATE TABLE legacy_column_name_of_tuple_literal (`x` UInt32, `y` UInt64, PROJECTION p (SELECT sum(y in (1,2,3)))) ENGINE = MergeTree ORDER BY tuple(); + +INSERT INTO legacy_column_name_of_tuple_literal SELECT number, number * 2 FROM numbers(8192 * 10) SETTINGS legacy_column_name_of_tuple_literal = true; + +SELECT count() FROM legacy_column_name_of_tuple_literal; + +DROP TABLE legacy_column_name_of_tuple_literal;