Merge pull request #23501 from kitaisreal/join-to-subqueries-columns-matcher-fix

JoinToSubqueriesVisitor ASTColumnsMatcher fix
This commit is contained in:
Maksim Kita 2021-04-23 00:24:53 +03:00 committed by GitHub
commit 2b822d896d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 5 deletions

View File

@ -11,6 +11,7 @@
#include <Parsers/ASTExpressionList.h> #include <Parsers/ASTExpressionList.h>
#include <Parsers/ASTFunction.h> #include <Parsers/ASTFunction.h>
#include <Parsers/ASTAsterisk.h> #include <Parsers/ASTAsterisk.h>
#include <Parsers/ASTColumnsMatcher.h>
#include <Parsers/ASTQualifiedAsterisk.h> #include <Parsers/ASTQualifiedAsterisk.h>
#include <Parsers/ParserTablesInSelectQuery.h> #include <Parsers/ParserTablesInSelectQuery.h>
#include <Parsers/ExpressionListParsers.h> #include <Parsers/ExpressionListParsers.h>
@ -73,16 +74,29 @@ public:
} }
} }
void addTableColumns(const String & table_name) using ShouldAddColumnPredicate = std::function<bool (const String&)>;
/// Add columns from table with table_name into select expression list
/// Use should_add_column_predicate for check if column name should be added
/// By default should_add_column_predicate returns true for any column name
void addTableColumns(
const String & table_name,
ShouldAddColumnPredicate should_add_column_predicate = [](const String &) { return true; })
{ {
auto it = table_columns.find(table_name); auto it = table_columns.find(table_name);
if (it == table_columns.end()) if (it == table_columns.end())
throw Exception("Unknown qualified identifier: " + table_name, ErrorCodes::UNKNOWN_IDENTIFIER); throw Exception("Unknown qualified identifier: " + table_name, ErrorCodes::UNKNOWN_IDENTIFIER);
for (const auto & column : it->second) for (const auto & column : it->second)
new_select_expression_list->children.push_back( {
std::make_shared<ASTIdentifier>(std::vector<String>{it->first, column.name})); if (should_add_column_predicate(column.name))
{
auto identifier = std::make_shared<ASTIdentifier>(std::vector<String>{it->first, column.name});
new_select_expression_list->children.emplace_back(std::move(identifier));
}
}
} }
}; };
static bool needChildVisit(const ASTPtr &, const ASTPtr &) { return false; } static bool needChildVisit(const ASTPtr &, const ASTPtr &) { return false; }
@ -119,6 +133,13 @@ private:
data.addTableColumns(identifier.name()); data.addTableColumns(identifier.name());
} }
else if (auto * columns_matcher = child->as<ASTColumnsMatcher>())
{
has_asterisks = true;
for (auto & table_name : data.tables_order)
data.addTableColumns(table_name, [&](const String & column_name) { return columns_matcher->isColumnMatching(column_name); });
}
else else
data.new_select_expression_list->children.push_back(child); data.new_select_expression_list->children.push_back(child);
} }
@ -225,7 +246,7 @@ struct CollectColumnIdentifiersMatcher
: identifiers(identifiers_) : identifiers(identifiers_)
{} {}
void addIdentirier(const ASTIdentifier & ident) void addIdentifier(const ASTIdentifier & ident)
{ {
for (const auto & aliases : ignored) for (const auto & aliases : ignored)
if (aliases.count(ident.name())) if (aliases.count(ident.name()))
@ -267,7 +288,7 @@ struct CollectColumnIdentifiersMatcher
static void visit(const ASTIdentifier & ident, const ASTPtr &, Data & data) static void visit(const ASTIdentifier & ident, const ASTPtr &, Data & data)
{ {
data.addIdentirier(ident); data.addIdentifier(ident);
} }
static void visit(const ASTFunction & func, const ASTPtr &, Data & data) static void visit(const ASTFunction & func, const ASTPtr &, Data & data)

View File

@ -0,0 +1,4 @@
SELECT COLUMNS('test') FROM
(SELECT 1 AS id, 'a' AS test) a
LEFT JOIN (SELECT 1 AS id, 'b' AS test) b ON b.id = a.id
LEFT JOIN (SELECT 1 AS id, 'c' AS test) c ON c.id = a.id;