From 668b22025886acbc921b41a2e426c71fea410723 Mon Sep 17 00:00:00 2001 From: chertus Date: Fri, 8 Feb 2019 18:37:43 +0300 Subject: [PATCH] fix push down --- dbms/src/Interpreters/ExpressionAnalyzer.cpp | 1 + .../FindIdentifierBestTableVisitor.h | 4 +++- dbms/src/Interpreters/IdentifierSemantic.cpp | 15 ++++++++++++++- dbms/src/Interpreters/IdentifierSemantic.h | 1 + .../PredicateExpressionsOptimizer.cpp | 18 +++++++++++++++--- dbms/src/Parsers/ASTIdentifier.h | 2 +- 6 files changed, 35 insertions(+), 6 deletions(-) diff --git a/dbms/src/Interpreters/ExpressionAnalyzer.cpp b/dbms/src/Interpreters/ExpressionAnalyzer.cpp index 55f3b901e8a..02a74ab094f 100644 --- a/dbms/src/Interpreters/ExpressionAnalyzer.cpp +++ b/dbms/src/Interpreters/ExpressionAnalyzer.cpp @@ -1049,6 +1049,7 @@ void ExpressionAnalyzer::collectUsedColumns() if (!unknown_required_source_columns.empty()) { std::stringstream ss; + ss << "query: '" << query << "'" << std::endl; ss << columns_context; ss << "source_columns: "; for (const auto & name : source_columns) diff --git a/dbms/src/Interpreters/FindIdentifierBestTableVisitor.h b/dbms/src/Interpreters/FindIdentifierBestTableVisitor.h index 4ad4fc09ff6..96e801f7ed2 100644 --- a/dbms/src/Interpreters/FindIdentifierBestTableVisitor.h +++ b/dbms/src/Interpreters/FindIdentifierBestTableVisitor.h @@ -10,8 +10,10 @@ namespace DB struct FindIdentifierBestTableData { using TypeToVisit = ASTIdentifier; + using IdentifierWithTable = std::pair; + const std::vector & tables; - std::vector> identifier_table; + std::vector identifier_table; FindIdentifierBestTableData(const std::vector & tables_); diff --git a/dbms/src/Interpreters/IdentifierSemantic.cpp b/dbms/src/Interpreters/IdentifierSemantic.cpp index 337bbc27cfb..13a9c49c3e0 100644 --- a/dbms/src/Interpreters/IdentifierSemantic.cpp +++ b/dbms/src/Interpreters/IdentifierSemantic.cpp @@ -112,8 +112,21 @@ void IdentifierSemantic::setColumnNormalName(ASTIdentifier & identifier, const D if (identifier.semantic->need_long_name) { String prefix = db_and_table.getQualifiedNamePrefix(); - identifier.name.insert(identifier.name.begin(), prefix.begin(), prefix.end()); + if (!prefix.empty()) + { + String short_name = identifier.shortName(); + identifier.name = prefix + short_name; + prefix.resize(prefix.size() - 1); /// crop dot + identifier.name_parts = {prefix, short_name}; + } } } +String IdentifierSemantic::columnNormalName(const ASTIdentifier & identifier, const DatabaseAndTableWithAlias & db_and_table) +{ + ASTPtr copy = identifier.clone(); + setColumnNormalName(typeid_cast(*copy), db_and_table); + return copy->getAliasOrColumnName(); +} + } diff --git a/dbms/src/Interpreters/IdentifierSemantic.h b/dbms/src/Interpreters/IdentifierSemantic.h index 8bef3543a43..be721627e1a 100644 --- a/dbms/src/Interpreters/IdentifierSemantic.h +++ b/dbms/src/Interpreters/IdentifierSemantic.h @@ -25,6 +25,7 @@ struct IdentifierSemantic static std::pair extractDatabaseAndTable(const ASTIdentifier & identifier); static size_t canReferColumnToTable(const ASTIdentifier & identifier, const DatabaseAndTableWithAlias & db_and_table); + static String columnNormalName(const ASTIdentifier & identifier, const DatabaseAndTableWithAlias & db_and_table); static void setColumnNormalName(ASTIdentifier & identifier, const DatabaseAndTableWithAlias & db_and_table); static void setNeedLongName(ASTIdentifier & identifier, bool); /// if set setColumnNormalName makes qualified name diff --git a/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp b/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp index af556ebc01e..b49f02a14fa 100644 --- a/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp +++ b/dbms/src/Interpreters/PredicateExpressionsOptimizer.cpp @@ -236,10 +236,22 @@ void PredicateExpressionsOptimizer::setNewAliasesForInnerPredicate( { if (alias == qualified_name) { - if (!isIdentifier(ast) && ast->tryGetAlias().empty()) - ast->setAlias(ast->getColumnName()); + String name; + if (auto * id = typeid_cast(ast.get())) + { + name = id->tryGetAlias(); + if (name.empty()) + name = id->shortName(); + } + else + { + if (ast->tryGetAlias().empty()) + ast->setAlias(ast->getColumnName()); + name = ast->getAliasOrColumnName(); + } - identifier->resetWithAlias(ast->getAliasOrColumnName()); + IdentifierSemantic::setNeedLongName(*identifier, false); + identifier->setShortName(name); } } } diff --git a/dbms/src/Parsers/ASTIdentifier.h b/dbms/src/Parsers/ASTIdentifier.h index 6bf7eac5260..9457b7d9156 100644 --- a/dbms/src/Parsers/ASTIdentifier.h +++ b/dbms/src/Parsers/ASTIdentifier.h @@ -36,7 +36,7 @@ public: bool compound() const { return !name_parts.empty(); } bool isShort() const { return name_parts.empty() || name == name_parts.back(); } - void resetWithAlias(const String & new_name) + void setShortName(const String & new_name) { name = new_name; name_parts.clear();