Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix key not found error for queries with multiple StorageJoin #49137

Merged
merged 2 commits into from May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Interpreters/TableJoin.cpp
Expand Up @@ -147,6 +147,7 @@ void TableJoin::addDisjunct()
void TableJoin::addOnKeys(ASTPtr & left_table_ast, ASTPtr & right_table_ast)
{
addKey(left_table_ast->getColumnName(), right_table_ast->getAliasOrColumnName(), left_table_ast, right_table_ast);
right_key_aliases[right_table_ast->getColumnName()] = right_table_ast->getAliasOrColumnName();
}

/// @return how many times right key appears in ON section.
Expand Down Expand Up @@ -662,6 +663,14 @@ String TableJoin::renamedRightColumnName(const String & name) const
return name;
}

String TableJoin::renamedRightColumnNameWithAlias(const String & name) const
{
auto renamed = renamedRightColumnName(name);
if (const auto it = right_key_aliases.find(renamed); it != right_key_aliases.end())
return it->second;
return renamed;
}

void TableJoin::setRename(const String & from, const String & to)
{
renames[from] = to;
Expand Down
8 changes: 8 additions & 0 deletions src/Interpreters/TableJoin.h
Expand Up @@ -156,6 +156,13 @@ class TableJoin
/// Original name -> name. Only renamed columns.
std::unordered_map<String, String> renames;

/// Map column name to actual key name that can be an alias.
/// Example: SELECT r.id as rid from t JOIN r ON t.id = rid
/// Map: r.id -> rid
/// Required only for StorageJoin to map join keys back to original column names.
/// (workaround for ExpressionAnalyzer)
std::unordered_map<String, String> right_key_aliases;

VolumePtr tmp_volume;

std::shared_ptr<StorageJoin> right_storage_join;
Expand Down Expand Up @@ -333,6 +340,7 @@ class TableJoin
Block getRequiredRightKeys(const Block & right_table_keys, std::vector<String> & keys_sources) const;

String renamedRightColumnName(const String & name) const;
String renamedRightColumnNameWithAlias(const String & name) const;
void setRename(const String & from, const String & to);

void resetKeys();
Expand Down
5 changes: 3 additions & 2 deletions src/Storages/StorageJoin.cpp
Expand Up @@ -220,12 +220,13 @@ HashJoinPtr StorageJoin::getJoinLocked(std::shared_ptr<TableJoin> analyzed_join,
Names left_key_names_resorted;
for (const auto & key_name : key_names)
{
const auto & renamed_key = analyzed_join->renamedRightColumnName(key_name);
const auto & renamed_key = analyzed_join->renamedRightColumnNameWithAlias(key_name);
/// find position of renamed_key in key_names_right
auto it = std::find(key_names_right.begin(), key_names_right.end(), renamed_key);
if (it == key_names_right.end())
throw Exception(ErrorCodes::INCOMPATIBLE_TYPE_OF_JOIN,
"Key '{}' not found in JOIN ON section. All Join engine keys '{}' have to be used", key_name, fmt::join(key_names, ", "));
"Key '{}' not found in JOIN ON section. Join engine key{} '{}' have to be used",
key_name, key_names.size() > 1 ? "s" : "", fmt::join(key_names, ", "));
const size_t key_position = std::distance(key_names_right.begin(), it);
left_key_names_resorted.push_back(key_names_left[key_position]);
}
Expand Down
@@ -0,0 +1,6 @@
0
0
0
0
0
0
21 changes: 21 additions & 0 deletions tests/queries/0_stateless/02724_mutliple_storage_join.sql
@@ -0,0 +1,21 @@
CREATE TABLE user(id UInt32, name String) ENGINE = Join(ANY, LEFT, id);
INSERT INTO user VALUES (1,'U1')(2,'U2')(3,'U3');

CREATE TABLE product(id UInt32, name String, cate String) ENGINE = Join(ANY, LEFT, id);
INSERT INTO product VALUES (1,'P1','C1')(2,'P2','C1')(3,'P3','C2');

CREATE TABLE order(id UInt32, pId UInt32, uId UInt32) ENGINE = TinyLog;
INSERT INTO order VALUES (1,1,1)(2,1,2)(3,2,3);

SELECT ignore(*) FROM (
SELECT
uId,
user.id as `uuu`
FROM order
LEFT ANY JOIN user
ON uId = `uuu`
);

SELECT ignore(*) FROM order
LEFT ANY JOIN user ON uId = user.id
LEFT ANY JOIN product ON pId = product.id;