ClickHouse/dbms/src/Interpreters/FindIdentifierBestTableVisitor.cpp

48 lines
1.3 KiB
C++
Raw Normal View History

#include <Interpreters/FindIdentifierBestTableVisitor.h>
#include <Interpreters/IdentifierSemantic.h>
namespace DB
{
FindIdentifierBestTableData::FindIdentifierBestTableData(const std::vector<TableWithColumnNames> & tables_)
: tables(tables_)
{
}
void FindIdentifierBestTableData::visit(ASTIdentifier & identifier, ASTPtr &)
{
const DatabaseAndTableWithAlias * best_table = nullptr;
if (!identifier.compound())
{
2019-03-04 15:53:51 +00:00
for (const auto & table_names : tables)
{
2019-03-04 15:53:51 +00:00
if (std::find(table_names.second.begin(), table_names.second.end(), identifier.name) != table_names.second.end())
{
// TODO: make sure no collision ever happens
if (!best_table)
2019-03-04 15:53:51 +00:00
best_table = &table_names.first;
}
}
}
else
{
// FIXME: make a better matcher using `names`?
size_t best_match = 0;
2019-03-04 15:53:51 +00:00
for (const auto & table_names : tables)
{
2019-03-04 15:53:51 +00:00
if (size_t match = IdentifierSemantic::canReferColumnToTable(identifier, table_names.first))
if (match > best_match)
{
best_match = match;
2019-03-04 15:53:51 +00:00
best_table = &table_names.first;
}
}
}
identifier_table.emplace_back(&identifier, best_table);
}
}