better column to table matching: check if table has qualified column

This commit is contained in:
chertus 2019-11-13 18:00:21 +03:00
parent 30acb1b653
commit d70904d7ee
5 changed files with 21 additions and 1 deletions

View File

@ -66,6 +66,13 @@ void TranslateQualifiedNamesMatcher::visit(ASTIdentifier & identifier, ASTPtr &,
bool allow_ambiguous = data.join_using_columns.count(short_name);
if (IdentifierSemantic::chooseTable(identifier, data.tables, table_pos, allow_ambiguous))
{
if (data.unknownColumn(table_pos, short_name))
{
String table_name = data.tables[table_pos].first.getQualifiedNamePrefix(false);
throw Exception("There's no column '" + identifier.name + "' in table '" + table_name + "'",
ErrorCodes::UNKNOWN_IDENTIFIER);
}
IdentifierSemantic::setMembership(identifier, table_pos);
/// In case if column from the joined table are in source columns, change it's name to qualified.

View File

@ -39,6 +39,15 @@ public:
bool hasTable() const { return !tables.empty(); }
bool processAsterisks() const { return hasTable() && has_columns; }
bool unknownColumn(size_t table_pos, const String & name) const
{
const Names & names = tables[table_pos].second;
for (auto & known_name : names)
if (name == known_name)
return false;
return !names.empty();
}
static std::vector<TableWithColumnNames> tablesOnly(const std::vector<DatabaseAndTableWithAlias> & tables)
{
std::vector<TableWithColumnNames> tables_with_columns;

View File

@ -1,7 +1,7 @@
SELECT
q0.dt,
q0.cnt,
q0.cnt2
q1.cnt2
FROM
(
SELECT

View File

@ -0,0 +1,3 @@
SELECT l.c FROM (SELECT 1 AS a, 2 AS b) AS l join (SELECT 2 AS b, 3 AS c) AS r USING b; -- { serverError 47 }
SELECT r.a FROM (SELECT 1 AS a, 2 AS b) AS l join (SELECT 2 AS b, 3 AS c) AS r USING b; -- { serverError 47 }
SELECT l.a, r.c FROM (SELECT 1 AS a, 2 AS b) AS l join (SELECT 2 AS b, 3 AS c) AS r USING b;