lidavidm commented on a change in pull request #10927: URL: https://github.com/apache/arrow/pull/10927#discussion_r687986863
########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,131 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <mutex> + +#include "arrow/api.h" +#include "arrow/compute/api.h" +#include "arrow/compute/exec/exec_plan.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/util.h" +#include "arrow/util/bitmap_ops.h" +#include "arrow/util/checked_cast.h" +#include "arrow/util/future.h" +#include "arrow/util/logging.h" +#include "arrow/util/thread_pool.h" + +namespace arrow { + +using internal::checked_cast; + +namespace compute { + +struct UnionNode : ExecNode { + UnionNode(ExecNode* lhs_input, ExecNode* rhs_input, ExecContext* ctx) Review comment: Most other nodes take ExecPlan* plan as the first input now. Additionally, is there any reason why this couldn't be made a variadic node? ########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,131 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <mutex> + +#include "arrow/api.h" +#include "arrow/compute/api.h" +#include "arrow/compute/exec/exec_plan.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/util.h" +#include "arrow/util/bitmap_ops.h" +#include "arrow/util/checked_cast.h" +#include "arrow/util/future.h" +#include "arrow/util/logging.h" +#include "arrow/util/thread_pool.h" + +namespace arrow { + +using internal::checked_cast; + +namespace compute { + +struct UnionNode : ExecNode { + UnionNode(ExecNode* lhs_input, ExecNode* rhs_input, ExecContext* ctx) + : ExecNode(lhs_input->plan(), {lhs_input, rhs_input}, + {"left_input_union", "right_input_union"}, + /*output_schema=*/lhs_input->output_schema(), + /*num_outputs=*/1), + ctx_(ctx) {} + + const char* kind_name() override { return "UnionNode"; } + + inline bool IsLeftInput(ExecNode* input) { return input == inputs_[0]; } + + void InputReceived(ExecNode* input, int seq, ExecBatch batch) override { + ARROW_DCHECK(input == inputs_[0] || input == inputs_[1]); + + if (finished_.is_finished()) { + return; + } + { + std::unique_lock<std::mutex> lock(mutex_); + ++batch_count_; + } + outputs_[0]->InputReceived(this, seq, std::move(batch)); + } + + void ErrorReceived(ExecNode* input, Status error) override { + DCHECK_EQ(input, inputs_[0]); + outputs_[0]->ErrorReceived(this, std::move(error)); + + StopProducing(); + } + + void InputFinished(ExecNode* input, int num_total) override { + ARROW_DCHECK(input == inputs_[0] || input == inputs_[1]); + { + std::unique_lock<std::mutex> lk(mutex_); + ++input_count_; + } + std::unique_lock<std::mutex> lk(mutex_); + if (input_count_ == 2) { + finished_.MarkFinished(); + outputs_[0]->InputFinished(this, batch_count_); Review comment: InputFinished isn't guaranteed to be called after calls to InputReceived finish. Instead we should add `num_total` to a counter here, then when we've gotten InputFinished from all inputs, we can then call InputFinished on the output. We shouldn't MarkFinished until we get all the batches. You can see this pattern in aggregate_node.cc: https://github.com/apache/arrow/blob/5c5a0d63a42dc8d5ecab5996574c466f2e9c2ed5/cpp/src/arrow/compute/exec/aggregate_node.cc#L483-L492 and https://github.com/apache/arrow/blob/5c5a0d63a42dc8d5ecab5996574c466f2e9c2ed5/cpp/src/arrow/compute/exec/aggregate_node.cc#L464-L475 ########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,131 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <mutex> + +#include "arrow/api.h" +#include "arrow/compute/api.h" +#include "arrow/compute/exec/exec_plan.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/util.h" +#include "arrow/util/bitmap_ops.h" +#include "arrow/util/checked_cast.h" +#include "arrow/util/future.h" +#include "arrow/util/logging.h" +#include "arrow/util/thread_pool.h" + +namespace arrow { + +using internal::checked_cast; + +namespace compute { + +struct UnionNode : ExecNode { + UnionNode(ExecNode* lhs_input, ExecNode* rhs_input, ExecContext* ctx) + : ExecNode(lhs_input->plan(), {lhs_input, rhs_input}, + {"left_input_union", "right_input_union"}, + /*output_schema=*/lhs_input->output_schema(), + /*num_outputs=*/1), + ctx_(ctx) {} + + const char* kind_name() override { return "UnionNode"; } + + inline bool IsLeftInput(ExecNode* input) { return input == inputs_[0]; } + + void InputReceived(ExecNode* input, int seq, ExecBatch batch) override { + ARROW_DCHECK(input == inputs_[0] || input == inputs_[1]); + + if (finished_.is_finished()) { + return; + } + { + std::unique_lock<std::mutex> lock(mutex_); + ++batch_count_; + } + outputs_[0]->InputReceived(this, seq, std::move(batch)); + } + + void ErrorReceived(ExecNode* input, Status error) override { + DCHECK_EQ(input, inputs_[0]); + outputs_[0]->ErrorReceived(this, std::move(error)); + + StopProducing(); + } + + void InputFinished(ExecNode* input, int num_total) override { + ARROW_DCHECK(input == inputs_[0] || input == inputs_[1]); + { + std::unique_lock<std::mutex> lk(mutex_); + ++input_count_; + } + std::unique_lock<std::mutex> lk(mutex_); + if (input_count_ == 2) { + finished_.MarkFinished(); + outputs_[0]->InputFinished(this, batch_count_); Review comment: So it would look something like (assuming we use AtomicCounter) ```cpp input_count_.SetTotal(2); // or inputs.size() if we want to make this a variadic node // ... // InputFinished total_batches_ += num_total; if (input_count_.Increment()) { outputs_[0]->InputFinished(this, total_batches_); if (batch_count.SetTotal(total_batches_)) { finished_.MarkFinished(); } } // InputReceived outputs_[0]->InputReceived(this, seq, std::move(batch)); if (batch_count_.Increment()) { finished_.MarkFinished(); } // StopProducing if (batch_count_.Cancel()) finished_.MarkFinished(); ``` ########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,131 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <mutex> + +#include "arrow/api.h" +#include "arrow/compute/api.h" +#include "arrow/compute/exec/exec_plan.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/util.h" +#include "arrow/util/bitmap_ops.h" +#include "arrow/util/checked_cast.h" +#include "arrow/util/future.h" +#include "arrow/util/logging.h" +#include "arrow/util/thread_pool.h" + +namespace arrow { + +using internal::checked_cast; + +namespace compute { + +struct UnionNode : ExecNode { + UnionNode(ExecNode* lhs_input, ExecNode* rhs_input, ExecContext* ctx) + : ExecNode(lhs_input->plan(), {lhs_input, rhs_input}, + {"left_input_union", "right_input_union"}, + /*output_schema=*/lhs_input->output_schema(), + /*num_outputs=*/1), + ctx_(ctx) {} + + const char* kind_name() override { return "UnionNode"; } + + inline bool IsLeftInput(ExecNode* input) { return input == inputs_[0]; } + + void InputReceived(ExecNode* input, int seq, ExecBatch batch) override { + ARROW_DCHECK(input == inputs_[0] || input == inputs_[1]); + + if (finished_.is_finished()) { + return; + } + { + std::unique_lock<std::mutex> lock(mutex_); + ++batch_count_; + } + outputs_[0]->InputReceived(this, seq, std::move(batch)); + } + + void ErrorReceived(ExecNode* input, Status error) override { + DCHECK_EQ(input, inputs_[0]); + outputs_[0]->ErrorReceived(this, std::move(error)); + + StopProducing(); + } + + void InputFinished(ExecNode* input, int num_total) override { + ARROW_DCHECK(input == inputs_[0] || input == inputs_[1]); + { + std::unique_lock<std::mutex> lk(mutex_); + ++input_count_; + } + std::unique_lock<std::mutex> lk(mutex_); + if (input_count_ == 2) { + finished_.MarkFinished(); + outputs_[0]->InputFinished(this, batch_count_); + } + } + + Status StartProducing() override { + finished_ = Future<>::Make(); + return Status::OK(); + } + + void PauseProducing(ExecNode* output) override {} + + void ResumeProducing(ExecNode* output) override {} + + void StopProducing(ExecNode* output) override { + DCHECK_EQ(output, outputs_[0]); + finished_.MarkFinished(); + + for (auto&& input : inputs_) { + input->StopProducing(this); + } + } + + void StopProducing() override { inputs_[0]->StopProducing(this); } + + Future<> finished() override { return finished_; } + + private: + ExecContext* ctx_; + std::mutex mutex_; + int batch_count_{0}; + int input_count_{0}; Review comment: There's a helper called AtomicCounter to manage this sort of thing without having to keep the mutex. ########## File path: cpp/src/arrow/compute/exec/union_node_test.cc ########## @@ -0,0 +1,188 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <gmock/gmock-matchers.h> + +#include <iostream> + +#include "arrow/api.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/test_util.h" +#include "arrow/pretty_print.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/matchers.h" + +using testing::UnorderedElementsAreArray; + +namespace arrow { +namespace compute { + +void GenerateBatchesFromString(const std::shared_ptr<Schema>& schema, + const std::vector<util::string_view>& json_strings, + BatchesWithSchema* out_batches, int multiplicity = 1) { + std::vector<ValueDescr> descrs; + for (auto&& field : schema->fields()) { + descrs.emplace_back(field->type()); + } + + for (auto&& s : json_strings) { + out_batches->batches.push_back(ExecBatchFromJSON(descrs, s)); + } + + size_t batch_count = out_batches->batches.size(); + for (int repeat = 1; repeat < multiplicity; ++repeat) { + for (size_t i = 0; i < batch_count; ++i) { + out_batches->batches.push_back(out_batches->batches[i]); + } + } + + out_batches->schema = schema; +} + +void CheckRunOutput(const BatchesWithSchema& l_batches, + const BatchesWithSchema& r_batches, + const BatchesWithSchema& exp_batches, bool parallel = false) { + SCOPED_TRACE("serial"); + + ASSERT_OK_AND_ASSIGN(auto plan, ExecPlan::Make()); + + ExecNodeOptions Union_options{}; + Declaration union_decl{"union", Union_options}; Review comment: ```suggestion ExecNodeOptions union_options{}; Declaration union_decl{"union", union_options}; ``` ########## File path: cpp/src/arrow/compute/exec/union_node_test.cc ########## @@ -0,0 +1,188 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <gmock/gmock-matchers.h> + +#include <iostream> + +#include "arrow/api.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/test_util.h" +#include "arrow/pretty_print.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/matchers.h" + +using testing::UnorderedElementsAreArray; + +namespace arrow { +namespace compute { + +void GenerateBatchesFromString(const std::shared_ptr<Schema>& schema, + const std::vector<util::string_view>& json_strings, + BatchesWithSchema* out_batches, int multiplicity = 1) { + std::vector<ValueDescr> descrs; + for (auto&& field : schema->fields()) { + descrs.emplace_back(field->type()); + } + + for (auto&& s : json_strings) { + out_batches->batches.push_back(ExecBatchFromJSON(descrs, s)); + } + + size_t batch_count = out_batches->batches.size(); + for (int repeat = 1; repeat < multiplicity; ++repeat) { + for (size_t i = 0; i < batch_count; ++i) { + out_batches->batches.push_back(out_batches->batches[i]); + } + } + + out_batches->schema = schema; +} + +void CheckRunOutput(const BatchesWithSchema& l_batches, + const BatchesWithSchema& r_batches, + const BatchesWithSchema& exp_batches, bool parallel = false) { + SCOPED_TRACE("serial"); + + ASSERT_OK_AND_ASSIGN(auto plan, ExecPlan::Make()); + + ExecNodeOptions Union_options{}; + Declaration union_decl{"union", Union_options}; + + // add left source + union_decl.inputs.emplace_back(Declaration{ + "source", SourceNodeOptions{l_batches.schema, l_batches.gen(parallel, + /*slow=*/false)}}); + // add right source + union_decl.inputs.emplace_back(Declaration{ + "source", SourceNodeOptions{r_batches.schema, r_batches.gen(parallel, + /*slow=*/false)}}); + AsyncGenerator<util::optional<ExecBatch>> sink_gen; + + ASSERT_OK(Declaration::Sequence({union_decl, {"sink", SinkNodeOptions{&sink_gen}}}) + .AddToPlan(plan.get())); + + Future<std::vector<ExecBatch>> actual = StartAndCollect(plan.get(), sink_gen); + + auto expected_matcher = + Finishes(ResultWith(UnorderedElementsAreArray(exp_batches.batches))); + ASSERT_THAT(actual, expected_matcher); +} + +void RunNonEmptyTest(bool parallel) { + auto l_schema = schema({field("l_i32", int32()), field("l_str", utf8())}); + auto r_schema = schema({field("r_i32", int32()), field("r_str", utf8())}); + BatchesWithSchema l_batches, r_batches, exp_batches; + + int multiplicity = parallel ? 100 : 1; + + GenerateBatchesFromString(l_schema, + { + R"([[0,"d"], [1,"b"]])", + R"([[2,"d"], [3,"a"], [4,"a"]])", + }, + &l_batches, multiplicity); + + GenerateBatchesFromString(r_schema, + { + R"([[10,"A"]])", + }, + &r_batches, multiplicity); + + GenerateBatchesFromString(l_schema, + { + R"([[0,"d"], [1,"b"]])", + R"([[2,"d"], [3,"a"], [4,"a"]])", + + R"([[10,"A"]])", + }, + &exp_batches, multiplicity); + CheckRunOutput(l_batches, r_batches, exp_batches, parallel); +} + +void RunEmptyTest(bool parallel) { + auto l_schema = schema({field("l_i32", int32()), field("l_str", utf8())}); + auto r_schema = schema({field("r_i32", int32()), field("r_str", utf8())}); + + int multiplicity = parallel ? 100 : 1; + + BatchesWithSchema l_empty, r_empty, output_batches; + + GenerateBatchesFromString(l_schema, {R"([])"}, &l_empty, multiplicity); + GenerateBatchesFromString(r_schema, {R"([])"}, &r_empty, multiplicity); + + GenerateBatchesFromString(l_schema, {R"([])", R"([])"}, &output_batches, multiplicity); + + CheckRunOutput(l_empty, r_empty, output_batches); +} + +class UnionTest : public testing::TestWithParam<std::tuple<bool>> {}; + +INSTANTIATE_TEST_SUITE_P(UnionTest, UnionTest, + ::testing::Combine(::testing::Values(false, true))); + +TEST_P(UnionTest, TestNonEmpty) { RunNonEmptyTest(std::get<0>(GetParam())); } + +TEST_P(UnionTest, TestEmpty) { RunEmptyTest(std::get<0>(GetParam())); } + +void TestUnionRandom(const std::shared_ptr<DataType>& data_type, bool parallel, + int num_batches, int batch_size) { + auto l_schema = schema({field("l0", data_type), field("l1", data_type)}); + auto r_schema = schema({field("r0", data_type), field("r1", data_type)}); + + // generate data + auto l_batches = MakeRandomBatches(l_schema, num_batches, batch_size); + auto r_batches = MakeRandomBatches(r_schema, num_batches, batch_size); + + ASSERT_OK_AND_ASSIGN(auto plan, ExecPlan::Make()); + + Declaration Union{"union", ExecNodeOptions{}}; + + // add left source + Union.inputs.emplace_back(Declaration{ + "source", SourceNodeOptions{l_batches.schema, l_batches.gen(parallel, + /*slow=*/false)}}); + // add right source + Union.inputs.emplace_back(Declaration{ + "source", SourceNodeOptions{r_batches.schema, r_batches.gen(parallel, + /*slow=*/false)}}); + AsyncGenerator<util::optional<ExecBatch>> sink_gen; + + ASSERT_OK(Declaration::Sequence({Union, {"sink", SinkNodeOptions{&sink_gen}}}) + .AddToPlan(plan.get())); + + ASSERT_FINISHES_OK_AND_ASSIGN(auto res, StartAndCollect(plan.get(), sink_gen)); +} Review comment: You should be able to check the batches here too. ########## File path: cpp/src/arrow/compute/exec/union_node_test.cc ########## @@ -0,0 +1,188 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <gmock/gmock-matchers.h> + +#include <iostream> + +#include "arrow/api.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/test_util.h" +#include "arrow/pretty_print.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/matchers.h" + +using testing::UnorderedElementsAreArray; + +namespace arrow { +namespace compute { + +void GenerateBatchesFromString(const std::shared_ptr<Schema>& schema, + const std::vector<util::string_view>& json_strings, + BatchesWithSchema* out_batches, int multiplicity = 1) { + std::vector<ValueDescr> descrs; + for (auto&& field : schema->fields()) { + descrs.emplace_back(field->type()); + } + + for (auto&& s : json_strings) { + out_batches->batches.push_back(ExecBatchFromJSON(descrs, s)); + } + + size_t batch_count = out_batches->batches.size(); + for (int repeat = 1; repeat < multiplicity; ++repeat) { + for (size_t i = 0; i < batch_count; ++i) { + out_batches->batches.push_back(out_batches->batches[i]); + } + } + + out_batches->schema = schema; +} + +void CheckRunOutput(const BatchesWithSchema& l_batches, + const BatchesWithSchema& r_batches, + const BatchesWithSchema& exp_batches, bool parallel = false) { + SCOPED_TRACE("serial"); + + ASSERT_OK_AND_ASSIGN(auto plan, ExecPlan::Make()); + + ExecNodeOptions Union_options{}; + Declaration union_decl{"union", Union_options}; Review comment: (You should be able to get away with `Declaration union_decl{"union", {}}`?) ########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,131 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <mutex> + +#include "arrow/api.h" +#include "arrow/compute/api.h" +#include "arrow/compute/exec/exec_plan.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/util.h" +#include "arrow/util/bitmap_ops.h" +#include "arrow/util/checked_cast.h" +#include "arrow/util/future.h" +#include "arrow/util/logging.h" +#include "arrow/util/thread_pool.h" + +namespace arrow { + +using internal::checked_cast; + +namespace compute { + +struct UnionNode : ExecNode { + UnionNode(ExecNode* lhs_input, ExecNode* rhs_input, ExecContext* ctx) + : ExecNode(lhs_input->plan(), {lhs_input, rhs_input}, + {"left_input_union", "right_input_union"}, + /*output_schema=*/lhs_input->output_schema(), + /*num_outputs=*/1), + ctx_(ctx) {} + + const char* kind_name() override { return "UnionNode"; } + + inline bool IsLeftInput(ExecNode* input) { return input == inputs_[0]; } + + void InputReceived(ExecNode* input, int seq, ExecBatch batch) override { + ARROW_DCHECK(input == inputs_[0] || input == inputs_[1]); + + if (finished_.is_finished()) { + return; + } + { + std::unique_lock<std::mutex> lock(mutex_); + ++batch_count_; + } + outputs_[0]->InputReceived(this, seq, std::move(batch)); + } + + void ErrorReceived(ExecNode* input, Status error) override { + DCHECK_EQ(input, inputs_[0]); + outputs_[0]->ErrorReceived(this, std::move(error)); + + StopProducing(); + } + + void InputFinished(ExecNode* input, int num_total) override { + ARROW_DCHECK(input == inputs_[0] || input == inputs_[1]); + { + std::unique_lock<std::mutex> lk(mutex_); + ++input_count_; + } + std::unique_lock<std::mutex> lk(mutex_); + if (input_count_ == 2) { + finished_.MarkFinished(); + outputs_[0]->InputFinished(this, batch_count_); Review comment: I wonder if we could make some sort of testing node that purposely permuted the order of operations to try to catch this… ########## File path: cpp/src/arrow/compute/exec/union_node_test.cc ########## @@ -0,0 +1,188 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <gmock/gmock-matchers.h> + +#include <iostream> + +#include "arrow/api.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/test_util.h" +#include "arrow/pretty_print.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/matchers.h" + +using testing::UnorderedElementsAreArray; + +namespace arrow { +namespace compute { + +void GenerateBatchesFromString(const std::shared_ptr<Schema>& schema, + const std::vector<util::string_view>& json_strings, + BatchesWithSchema* out_batches, int multiplicity = 1) { + std::vector<ValueDescr> descrs; + for (auto&& field : schema->fields()) { + descrs.emplace_back(field->type()); + } + + for (auto&& s : json_strings) { + out_batches->batches.push_back(ExecBatchFromJSON(descrs, s)); + } + + size_t batch_count = out_batches->batches.size(); + for (int repeat = 1; repeat < multiplicity; ++repeat) { + for (size_t i = 0; i < batch_count; ++i) { + out_batches->batches.push_back(out_batches->batches[i]); + } + } + + out_batches->schema = schema; +} + +void CheckRunOutput(const BatchesWithSchema& l_batches, + const BatchesWithSchema& r_batches, + const BatchesWithSchema& exp_batches, bool parallel = false) { + SCOPED_TRACE("serial"); + + ASSERT_OK_AND_ASSIGN(auto plan, ExecPlan::Make()); + + ExecNodeOptions Union_options{}; + Declaration union_decl{"union", Union_options}; + + // add left source + union_decl.inputs.emplace_back(Declaration{ + "source", SourceNodeOptions{l_batches.schema, l_batches.gen(parallel, + /*slow=*/false)}}); + // add right source + union_decl.inputs.emplace_back(Declaration{ + "source", SourceNodeOptions{r_batches.schema, r_batches.gen(parallel, + /*slow=*/false)}}); + AsyncGenerator<util::optional<ExecBatch>> sink_gen; + + ASSERT_OK(Declaration::Sequence({union_decl, {"sink", SinkNodeOptions{&sink_gen}}}) + .AddToPlan(plan.get())); + + Future<std::vector<ExecBatch>> actual = StartAndCollect(plan.get(), sink_gen); + + auto expected_matcher = + Finishes(ResultWith(UnorderedElementsAreArray(exp_batches.batches))); + ASSERT_THAT(actual, expected_matcher); +} + +void RunNonEmptyTest(bool parallel) { + auto l_schema = schema({field("l_i32", int32()), field("l_str", utf8())}); + auto r_schema = schema({field("r_i32", int32()), field("r_str", utf8())}); + BatchesWithSchema l_batches, r_batches, exp_batches; + + int multiplicity = parallel ? 100 : 1; + + GenerateBatchesFromString(l_schema, + { + R"([[0,"d"], [1,"b"]])", + R"([[2,"d"], [3,"a"], [4,"a"]])", + }, + &l_batches, multiplicity); + + GenerateBatchesFromString(r_schema, + { + R"([[10,"A"]])", + }, + &r_batches, multiplicity); + + GenerateBatchesFromString(l_schema, + { + R"([[0,"d"], [1,"b"]])", + R"([[2,"d"], [3,"a"], [4,"a"]])", + + R"([[10,"A"]])", + }, + &exp_batches, multiplicity); + CheckRunOutput(l_batches, r_batches, exp_batches, parallel); +} + +void RunEmptyTest(bool parallel) { + auto l_schema = schema({field("l_i32", int32()), field("l_str", utf8())}); + auto r_schema = schema({field("r_i32", int32()), field("r_str", utf8())}); + + int multiplicity = parallel ? 100 : 1; + + BatchesWithSchema l_empty, r_empty, output_batches; + + GenerateBatchesFromString(l_schema, {R"([])"}, &l_empty, multiplicity); + GenerateBatchesFromString(r_schema, {R"([])"}, &r_empty, multiplicity); + + GenerateBatchesFromString(l_schema, {R"([])", R"([])"}, &output_batches, multiplicity); + + CheckRunOutput(l_empty, r_empty, output_batches); +} + +class UnionTest : public testing::TestWithParam<std::tuple<bool>> {}; + +INSTANTIATE_TEST_SUITE_P(UnionTest, UnionTest, + ::testing::Combine(::testing::Values(false, true))); + +TEST_P(UnionTest, TestNonEmpty) { RunNonEmptyTest(std::get<0>(GetParam())); } + +TEST_P(UnionTest, TestEmpty) { RunEmptyTest(std::get<0>(GetParam())); } Review comment: Why not just inline these? ########## File path: cpp/src/arrow/compute/exec/union_node_test.cc ########## @@ -0,0 +1,188 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include <gmock/gmock-matchers.h> + +#include <iostream> + +#include "arrow/api.h" +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/test_util.h" +#include "arrow/pretty_print.h" +#include "arrow/testing/gtest_util.h" +#include "arrow/testing/matchers.h" + +using testing::UnorderedElementsAreArray; + +namespace arrow { +namespace compute { + +void GenerateBatchesFromString(const std::shared_ptr<Schema>& schema, + const std::vector<util::string_view>& json_strings, + BatchesWithSchema* out_batches, int multiplicity = 1) { + std::vector<ValueDescr> descrs; + for (auto&& field : schema->fields()) { + descrs.emplace_back(field->type()); + } + + for (auto&& s : json_strings) { + out_batches->batches.push_back(ExecBatchFromJSON(descrs, s)); + } + + size_t batch_count = out_batches->batches.size(); + for (int repeat = 1; repeat < multiplicity; ++repeat) { + for (size_t i = 0; i < batch_count; ++i) { + out_batches->batches.push_back(out_batches->batches[i]); + } + } + + out_batches->schema = schema; +} + +void CheckRunOutput(const BatchesWithSchema& l_batches, + const BatchesWithSchema& r_batches, + const BatchesWithSchema& exp_batches, bool parallel = false) { + SCOPED_TRACE("serial"); Review comment: This should depend on `parallel`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
