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

ARROW-18205: [C++] Substrait consumer is not converting right side references correctly on joins #14558

Merged
merged 2 commits into from Nov 1, 2022
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
26 changes: 18 additions & 8 deletions cpp/src/arrow/engine/substrait/relation_internal.cc
Expand Up @@ -479,22 +479,32 @@ Result<DeclarationInfo> FromProto(const substrait::Rel& rel, const ExtensionSet&
callptr->function_name);
}

// TODO: ARROW-16624 Add Suffix support for Substrait
const auto* left_keys = callptr->arguments[0].field_ref();
const auto* right_keys = callptr->arguments[1].field_ref();
if (!left_keys || !right_keys) {
return Status::Invalid("Left keys for join cannot be null");
}

// Create output schema from left, right relations and join keys
FieldVector combined_fields = left.output_schema->fields();
const FieldVector& right_fields = right.output_schema->fields();
combined_fields.insert(combined_fields.end(), right_fields.begin(),
right_fields.end());
std::shared_ptr<Schema> join_schema = schema(std::move(combined_fields));

// adjust the join_keys according to Substrait definition where
// the join fields are defined by considering the `join_schema` which
// is the combination of the left and right relation schema.

// TODO: ARROW-16624 Add Suffix support for Substrait
const auto* left_keys = callptr->arguments[0].field_ref();
const auto* right_keys = callptr->arguments[1].field_ref();
// Validating JoinKeys
if (!left_keys || !right_keys) {
return Status::Invalid(
"join condition must include references to both left and right inputs");
}
int num_left_fields = left.output_schema->num_fields();
const auto* right_field_path = right_keys->field_path();
std::vector<int> adjusted_field_indices(right_field_path->indices());
adjusted_field_indices[0] -= num_left_fields;
FieldPath adjusted_right_keys(adjusted_field_indices);
compute::HashJoinNodeOptions join_options{{std::move(*left_keys)},
{std::move(*right_keys)}};
{std::move(adjusted_right_keys)}};
join_options.join_type = join_type;
join_options.key_cmp = {join_key_cmp};
compute::Declaration join_dec{"hashjoin", std::move(join_options)};
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/arrow/engine/substrait/serde_test.cc
Expand Up @@ -2714,7 +2714,7 @@ TEST(Substrait, JoinRelEndToEnd) {
"selection": {
"directReference": {
"structField": {
"field": 0
"field": 2
}
},
"rootReference": {
Expand Down Expand Up @@ -2871,7 +2871,7 @@ TEST(Substrait, JoinRelWithEmit) {
"selection": {
"directReference": {
"structField": {
"field": 0
"field": 2
}
},
"rootReference": {
Expand Down