From b35ab511cb593ab2f09db9cc3f9dc3bde8a646c1 Mon Sep 17 00:00:00 2001 From: zhang2014 Date: Thu, 27 Sep 2018 13:27:45 +0800 Subject: [PATCH] ISSUES-3225 fix alias conflict when predicate optimization --- .../Interpreters/PredicateExpressionsOptimizer.cpp | 12 ++++++++++++ .../src/Interpreters/PredicateExpressionsOptimizer.h | 2 ++ .../0_stateless/00597_push_down_predicate.reference | 1 + .../0_stateless/00597_push_down_predicate.sql | 1 + 4 files changed, 16 insertions(+) diff --git a/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp b/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp index 812dc3a0b7d..97130efa7a8 100644 --- a/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp +++ b/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp @@ -222,6 +222,8 @@ void PredicateExpressionsOptimizer::cloneOuterPredicateForInnerPredicate( { inner_predicate = outer_predicate->clone(); + /// clears the alias name contained in the outer predicate + cleanExpressionAlias(inner_predicate); IdentifiersWithQualifiedNameSet new_expression_requires; getDependenciesAndQualifiedOfExpression(inner_predicate, new_expression_requires, tables); @@ -419,4 +421,14 @@ std::vector PredicateExpressionsOptimizer::getSelectTables return tables_expression; } +void PredicateExpressionsOptimizer::cleanExpressionAlias(ASTPtr & expression) +{ + const auto my_alias = expression->tryGetAlias(); + if (!my_alias.empty()) + expression->setAlias(""); + + for (auto & child : expression->children) + cleanExpressionAlias(child); +} + } diff --git a/dbms/src/Interpreters/PredicateExpressionsOptimizer.h b/dbms/src/Interpreters/PredicateExpressionsOptimizer.h index e656c4e0a15..6687860d979 100644 --- a/dbms/src/Interpreters/PredicateExpressionsOptimizer.h +++ b/dbms/src/Interpreters/PredicateExpressionsOptimizer.h @@ -88,6 +88,8 @@ private: std::vector getSelectTablesExpression(ASTSelectQuery * select_query); ASTs evaluateAsterisk(ASTSelectQuery * select_query, const ASTPtr & asterisk); + + void cleanExpressionAlias(ASTPtr & expression); }; } diff --git a/dbms/tests/queries/0_stateless/00597_push_down_predicate.reference b/dbms/tests/queries/0_stateless/00597_push_down_predicate.reference index 22142c4748f..484107c0947 100644 --- a/dbms/tests/queries/0_stateless/00597_push_down_predicate.reference +++ b/dbms/tests/queries/0_stateless/00597_push_down_predicate.reference @@ -6,6 +6,7 @@ 1 1 1 +1 1 1 3 3 3 3 diff --git a/dbms/tests/queries/0_stateless/00597_push_down_predicate.sql b/dbms/tests/queries/0_stateless/00597_push_down_predicate.sql index ede8b008867..36378bda64a 100644 --- a/dbms/tests/queries/0_stateless/00597_push_down_predicate.sql +++ b/dbms/tests/queries/0_stateless/00597_push_down_predicate.sql @@ -17,6 +17,7 @@ SELECT 1 AS id WHERE id = 1; SELECT arrayJoin([1,2,3]) AS id WHERE id = 1; SELECT '-------Need push down-------'; +SELECT toString(value) AS value FROM (SELECT 1 AS value) WHERE value = '1'; SELECT * FROM (SELECT 1 AS id UNION ALL SELECT 2) WHERE id = 1; SELECT * FROM (SELECT arrayJoin([1, 2, 3]) AS id) WHERE id = 1; SELECT id FROM (SELECT arrayJoin([1, 2, 3]) AS id) WHERE id = 1;