lidavidm commented on a change in pull request #10927: URL: https://github.com/apache/arrow/pull/10927#discussion_r689712083
########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,150 @@ +// 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 { + +namespace { +std::vector<std::string> GetInputLabels(ExecNode::NodeVector inputs) { Review comment: ```suggestion std::vector<std::string> GetInputLabels(const ExecNode::NodeVector& inputs) { ``` ########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,150 @@ +// 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 { + +namespace { +std::vector<std::string> GetInputLabels(ExecNode::NodeVector inputs) { + std::vector<std::string> labels(inputs.size()); + for (size_t i = 0; i < inputs.size(); i++) { + labels[i] = "input_" + std::to_string(i) + "_label"; + } + return labels; +} +} // namespace +struct UnionNode : ExecNode { + UnionNode(ExecPlan* plan, std::vector<ExecNode*> inputs) + : ExecNode(plan, inputs, GetInputLabels(inputs), + /*output_schema=*/inputs[0]->output_schema(), + /*num_outputs=*/1) { + if (this->input_count_.SetTotal(static_cast<int>(inputs.size()))) { + finished_.MarkFinished(); Review comment: Hmm, this branch shouldn't be possible unless inputs == 0 which isn't allowed by Make. ########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,150 @@ +// 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 { + +namespace { +std::vector<std::string> GetInputLabels(ExecNode::NodeVector inputs) { + std::vector<std::string> labels(inputs.size()); + for (size_t i = 0; i < inputs.size(); i++) { + labels[i] = "input_" + std::to_string(i) + "_label"; + } + return labels; +} +} // namespace +struct UnionNode : ExecNode { + UnionNode(ExecPlan* plan, std::vector<ExecNode*> inputs) + : ExecNode(plan, inputs, GetInputLabels(inputs), + /*output_schema=*/inputs[0]->output_schema(), + /*num_outputs=*/1) { + if (this->input_count_.SetTotal(static_cast<int>(inputs.size()))) { + finished_.MarkFinished(); + } + } + + const char* kind_name() override { return "UnionNode"; } + + static Result<ExecNode*> Make(ExecPlan* plan, std::vector<ExecNode*> inputs, + const ExecNodeOptions& options) { + RETURN_NOT_OK(ValidateExecNodeInputs(plan, inputs, inputs.size(), "UnionNode")); + if (inputs.size() < 2) { + return Status::Invalid("Constructing a `UnionNode` with inputs size less than 2"); + } + auto schema = inputs.at(0)->output_schema(); + for (auto input : inputs) { + if (!input->output_schema()->Equals(schema)) { + return Status::Invalid( + "Constructing a `UnionNode` with inputs with different schemas"); + } + } + return plan->EmplaceNode<UnionNode>(plan, std::move(inputs)); + } + + inline bool IsLeftInput(ExecNode* input) { return input == inputs_[0]; } + + void InputReceived(ExecNode* input, int seq, ExecBatch batch) override { + ARROW_DCHECK(std::find_if(inputs_.begin(), inputs_.end(), [input](ExecNode* n) { + return n == input; + }) != inputs_.end()); + + if (finished_.is_finished()) { + return; + } + outputs_[0]->InputReceived(this, seq, std::move(batch)); Review comment: I wonder if it actually makes sense to forward seq like this. As I understand nodes can't really rely on this anyways so I don't think it harms things, but it seems odd to even have the parameter in that case. CC @bkietz for opinions. (We could modify `Increment` to also give the count, if we wanted to give a unique seq_num to each batch we emit here.) ########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,150 @@ +// 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 { + +namespace { +std::vector<std::string> GetInputLabels(ExecNode::NodeVector inputs) { + std::vector<std::string> labels(inputs.size()); + for (size_t i = 0; i < inputs.size(); i++) { + labels[i] = "input_" + std::to_string(i) + "_label"; + } + return labels; +} +} // namespace +struct UnionNode : ExecNode { + UnionNode(ExecPlan* plan, std::vector<ExecNode*> inputs) + : ExecNode(plan, inputs, GetInputLabels(inputs), + /*output_schema=*/inputs[0]->output_schema(), + /*num_outputs=*/1) { + if (this->input_count_.SetTotal(static_cast<int>(inputs.size()))) { + finished_.MarkFinished(); + } + } + + const char* kind_name() override { return "UnionNode"; } + + static Result<ExecNode*> Make(ExecPlan* plan, std::vector<ExecNode*> inputs, + const ExecNodeOptions& options) { + RETURN_NOT_OK(ValidateExecNodeInputs(plan, inputs, inputs.size(), "UnionNode")); + if (inputs.size() < 2) { + return Status::Invalid("Constructing a `UnionNode` with inputs size less than 2"); + } + auto schema = inputs.at(0)->output_schema(); + for (auto input : inputs) { + if (!input->output_schema()->Equals(schema)) { + return Status::Invalid( + "Constructing a `UnionNode` with inputs with different schemas"); Review comment: Nit: it might be nice to provide the schemas to aid debugging, something like ```cpp return Status::Invalid("UnionNode input schemas must all match, first schema was: ", *schema, " got schema: ", *input->output_schema); ``` ########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,150 @@ +// 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 { + +namespace { +std::vector<std::string> GetInputLabels(ExecNode::NodeVector inputs) { + std::vector<std::string> labels(inputs.size()); + for (size_t i = 0; i < inputs.size(); i++) { + labels[i] = "input_" + std::to_string(i) + "_label"; + } + return labels; +} +} // namespace +struct UnionNode : ExecNode { + UnionNode(ExecPlan* plan, std::vector<ExecNode*> inputs) + : ExecNode(plan, inputs, GetInputLabels(inputs), + /*output_schema=*/inputs[0]->output_schema(), + /*num_outputs=*/1) { + if (this->input_count_.SetTotal(static_cast<int>(inputs.size()))) { + finished_.MarkFinished(); + } + } + + const char* kind_name() override { return "UnionNode"; } + + static Result<ExecNode*> Make(ExecPlan* plan, std::vector<ExecNode*> inputs, + const ExecNodeOptions& options) { + RETURN_NOT_OK(ValidateExecNodeInputs(plan, inputs, inputs.size(), "UnionNode")); + if (inputs.size() < 2) { + return Status::Invalid("Constructing a `UnionNode` with inputs size less than 2"); + } + auto schema = inputs.at(0)->output_schema(); + for (auto input : inputs) { + if (!input->output_schema()->Equals(schema)) { + return Status::Invalid( + "Constructing a `UnionNode` with inputs with different schemas"); + } + } + return plan->EmplaceNode<UnionNode>(plan, std::move(inputs)); + } + + inline bool IsLeftInput(ExecNode* input) { return input == inputs_[0]; } + + void InputReceived(ExecNode* input, int seq, ExecBatch batch) override { + ARROW_DCHECK(std::find_if(inputs_.begin(), inputs_.end(), [input](ExecNode* n) { + return n == input; + }) != inputs_.end()); + + if (finished_.is_finished()) { + return; + } + outputs_[0]->InputReceived(this, seq, std::move(batch)); + if (batch_count_.Increment()) { + finished_.MarkFinished(); + } + } + + 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(std::find_if(inputs_.begin(), inputs_.end(), [input](ExecNode* n) { + return n == input; + }) != inputs_.end()); + + total_batches_.fetch_add(num_total); + + if (input_count_.Increment()) { + outputs_[0]->InputFinished(this, total_batches_.load()); + if (batch_count_.SetTotal(total_batches_.load())) { + finished_.MarkFinished(); + } + } + } + + Status StartProducing() override { + finished_ = Future<>::Make(); + return Status::OK(); + } + + void PauseProducing(ExecNode* output) override {} + + void ResumeProducing(ExecNode* output) override {} Review comment: Or if not, we should make a followup JIRA. ########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,150 @@ +// 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 { + +namespace { +std::vector<std::string> GetInputLabels(ExecNode::NodeVector inputs) { + std::vector<std::string> labels(inputs.size()); + for (size_t i = 0; i < inputs.size(); i++) { + labels[i] = "input_" + std::to_string(i) + "_label"; + } + return labels; +} +} // namespace +struct UnionNode : ExecNode { + UnionNode(ExecPlan* plan, std::vector<ExecNode*> inputs) + : ExecNode(plan, inputs, GetInputLabels(inputs), + /*output_schema=*/inputs[0]->output_schema(), + /*num_outputs=*/1) { + if (this->input_count_.SetTotal(static_cast<int>(inputs.size()))) { + finished_.MarkFinished(); + } + } + + const char* kind_name() override { return "UnionNode"; } + + static Result<ExecNode*> Make(ExecPlan* plan, std::vector<ExecNode*> inputs, + const ExecNodeOptions& options) { + RETURN_NOT_OK(ValidateExecNodeInputs(plan, inputs, inputs.size(), "UnionNode")); + if (inputs.size() < 2) { + return Status::Invalid("Constructing a `UnionNode` with inputs size less than 2"); + } + auto schema = inputs.at(0)->output_schema(); + for (auto input : inputs) { + if (!input->output_schema()->Equals(schema)) { + return Status::Invalid( + "Constructing a `UnionNode` with inputs with different schemas"); + } + } + return plan->EmplaceNode<UnionNode>(plan, std::move(inputs)); + } + + inline bool IsLeftInput(ExecNode* input) { return input == inputs_[0]; } Review comment: This looks unused now. ########## File path: cpp/src/arrow/compute/exec/union_node_test.cc ########## @@ -0,0 +1,203 @@ +// 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(parallel ? "parallel" : "single threaded"); + + ASSERT_OK_AND_ASSIGN(auto plan, ExecPlan::Make()); + + Declaration union_decl{"union", ExecNodeOptions{}}; + + // 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("colum_i32", int32()), field("colum_str", utf8())}); + auto r_schema = schema({field("colum_i32", int32()), field("colum_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("colum_i32", int32()), field("colum_str", utf8())}); + auto r_schema = schema({field("colum_i32", int32()), field("colum_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); +} + +TEST(UnionTest, TestNonEmpty) { + for (bool parallel : {false, true}) { + RunNonEmptyTest(parallel); + } +} + +TEST(UnionTest, TestEmpty) { + for (bool parallel : {false, true}) { + RunEmptyTest(parallel); + } +} + +void TestUnionRandom(const std::shared_ptr<DataType>& data_type, bool parallel, + int num_batches, int batch_size) { + auto l_schema = schema({field("colum0", data_type), field("colum1", data_type)}); + auto r_schema = schema({field("colum0", data_type), field("colum1", 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, Review comment: Maybe we could make this test use three inputs? We could even just duplicate one of the existing inputs. ########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,150 @@ +// 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 { + +namespace { +std::vector<std::string> GetInputLabels(ExecNode::NodeVector inputs) { + std::vector<std::string> labels(inputs.size()); + for (size_t i = 0; i < inputs.size(); i++) { + labels[i] = "input_" + std::to_string(i) + "_label"; + } + return labels; +} +} // namespace +struct UnionNode : ExecNode { + UnionNode(ExecPlan* plan, std::vector<ExecNode*> inputs) + : ExecNode(plan, inputs, GetInputLabels(inputs), + /*output_schema=*/inputs[0]->output_schema(), + /*num_outputs=*/1) { + if (this->input_count_.SetTotal(static_cast<int>(inputs.size()))) { + finished_.MarkFinished(); + } + } + + const char* kind_name() override { return "UnionNode"; } + + static Result<ExecNode*> Make(ExecPlan* plan, std::vector<ExecNode*> inputs, + const ExecNodeOptions& options) { + RETURN_NOT_OK(ValidateExecNodeInputs(plan, inputs, inputs.size(), "UnionNode")); + if (inputs.size() < 2) { + return Status::Invalid("Constructing a `UnionNode` with inputs size less than 2"); + } + auto schema = inputs.at(0)->output_schema(); + for (auto input : inputs) { + if (!input->output_schema()->Equals(schema)) { + return Status::Invalid( + "Constructing a `UnionNode` with inputs with different schemas"); + } + } + return plan->EmplaceNode<UnionNode>(plan, std::move(inputs)); + } + + inline bool IsLeftInput(ExecNode* input) { return input == inputs_[0]; } + + void InputReceived(ExecNode* input, int seq, ExecBatch batch) override { + ARROW_DCHECK(std::find_if(inputs_.begin(), inputs_.end(), [input](ExecNode* n) { + return n == input; + }) != inputs_.end()); + + if (finished_.is_finished()) { + return; + } + outputs_[0]->InputReceived(this, seq, std::move(batch)); + if (batch_count_.Increment()) { + finished_.MarkFinished(); + } + } + + 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(std::find_if(inputs_.begin(), inputs_.end(), [input](ExecNode* n) { + return n == input; + }) != inputs_.end()); + + total_batches_.fetch_add(num_total); + + if (input_count_.Increment()) { + outputs_[0]->InputFinished(this, total_batches_.load()); + if (batch_count_.SetTotal(total_batches_.load())) { + finished_.MarkFinished(); + } + } + } + + 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]); + if (batch_count_.Cancel()) { + finished_.MakeFinished(); Review comment: ```suggestion finished_.MarkFinished(); ``` ########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,150 @@ +// 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 { + +namespace { +std::vector<std::string> GetInputLabels(ExecNode::NodeVector inputs) { + std::vector<std::string> labels(inputs.size()); + for (size_t i = 0; i < inputs.size(); i++) { + labels[i] = "input_" + std::to_string(i) + "_label"; + } + return labels; +} +} // namespace +struct UnionNode : ExecNode { + UnionNode(ExecPlan* plan, std::vector<ExecNode*> inputs) + : ExecNode(plan, inputs, GetInputLabels(inputs), + /*output_schema=*/inputs[0]->output_schema(), + /*num_outputs=*/1) { + if (this->input_count_.SetTotal(static_cast<int>(inputs.size()))) { + finished_.MarkFinished(); + } + } + + const char* kind_name() override { return "UnionNode"; } + + static Result<ExecNode*> Make(ExecPlan* plan, std::vector<ExecNode*> inputs, + const ExecNodeOptions& options) { + RETURN_NOT_OK(ValidateExecNodeInputs(plan, inputs, inputs.size(), "UnionNode")); + if (inputs.size() < 2) { + return Status::Invalid("Constructing a `UnionNode` with inputs size less than 2"); + } + auto schema = inputs.at(0)->output_schema(); + for (auto input : inputs) { + if (!input->output_schema()->Equals(schema)) { + return Status::Invalid( + "Constructing a `UnionNode` with inputs with different schemas"); + } + } + return plan->EmplaceNode<UnionNode>(plan, std::move(inputs)); + } + + inline bool IsLeftInput(ExecNode* input) { return input == inputs_[0]; } + + void InputReceived(ExecNode* input, int seq, ExecBatch batch) override { + ARROW_DCHECK(std::find_if(inputs_.begin(), inputs_.end(), [input](ExecNode* n) { + return n == input; + }) != inputs_.end()); + + if (finished_.is_finished()) { + return; + } + outputs_[0]->InputReceived(this, seq, std::move(batch)); + if (batch_count_.Increment()) { + finished_.MarkFinished(); + } + } + + 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(std::find_if(inputs_.begin(), inputs_.end(), [input](ExecNode* n) { + return n == input; + }) != inputs_.end()); + + total_batches_.fetch_add(num_total); + + if (input_count_.Increment()) { + outputs_[0]->InputFinished(this, total_batches_.load()); + if (batch_count_.SetTotal(total_batches_.load())) { + finished_.MarkFinished(); + } + } + } + + Status StartProducing() override { + finished_ = Future<>::Make(); + return Status::OK(); + } + + void PauseProducing(ExecNode* output) override {} + + void ResumeProducing(ExecNode* output) override {} Review comment: I think we can trivially forward pause/resume to our inputs. ########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,150 @@ +// 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 { + +namespace { +std::vector<std::string> GetInputLabels(ExecNode::NodeVector inputs) { + std::vector<std::string> labels(inputs.size()); + for (size_t i = 0; i < inputs.size(); i++) { + labels[i] = "input_" + std::to_string(i) + "_label"; + } + return labels; +} +} // namespace +struct UnionNode : ExecNode { + UnionNode(ExecPlan* plan, std::vector<ExecNode*> inputs) + : ExecNode(plan, inputs, GetInputLabels(inputs), + /*output_schema=*/inputs[0]->output_schema(), + /*num_outputs=*/1) { + if (this->input_count_.SetTotal(static_cast<int>(inputs.size()))) { + finished_.MarkFinished(); + } + } + + const char* kind_name() override { return "UnionNode"; } + + static Result<ExecNode*> Make(ExecPlan* plan, std::vector<ExecNode*> inputs, + const ExecNodeOptions& options) { + RETURN_NOT_OK(ValidateExecNodeInputs(plan, inputs, inputs.size(), "UnionNode")); + if (inputs.size() < 2) { + return Status::Invalid("Constructing a `UnionNode` with inputs size less than 2"); + } + auto schema = inputs.at(0)->output_schema(); + for (auto input : inputs) { + if (!input->output_schema()->Equals(schema)) { + return Status::Invalid( + "Constructing a `UnionNode` with inputs with different schemas"); + } + } + return plan->EmplaceNode<UnionNode>(plan, std::move(inputs)); + } + + inline bool IsLeftInput(ExecNode* input) { return input == inputs_[0]; } + + void InputReceived(ExecNode* input, int seq, ExecBatch batch) override { + ARROW_DCHECK(std::find_if(inputs_.begin(), inputs_.end(), [input](ExecNode* n) { + return n == input; + }) != inputs_.end()); + + if (finished_.is_finished()) { + return; + } + outputs_[0]->InputReceived(this, seq, std::move(batch)); + if (batch_count_.Increment()) { + finished_.MarkFinished(); + } + } + + 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(std::find_if(inputs_.begin(), inputs_.end(), [input](ExecNode* n) { + return n == input; + }) != inputs_.end()); + + total_batches_.fetch_add(num_total); + + if (input_count_.Increment()) { + outputs_[0]->InputFinished(this, total_batches_.load()); + if (batch_count_.SetTotal(total_batches_.load())) { + finished_.MarkFinished(); + } + } + } + + 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]); + if (batch_count_.Cancel()) { + finished_.MakeFinished(); + } + for (auto&& input : inputs_) { + input->StopProducing(this); + } + } + + void StopProducing() override { + if (batch_count_.Cancel()) { + finished_.MakeFinished(); + } + inputs_[0]->StopProducing(this); + } Review comment: This should call StopProducing on every input. ########## File path: cpp/src/arrow/compute/exec/union_node.cc ########## @@ -0,0 +1,150 @@ +// 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 { + +namespace { +std::vector<std::string> GetInputLabels(ExecNode::NodeVector inputs) { + std::vector<std::string> labels(inputs.size()); + for (size_t i = 0; i < inputs.size(); i++) { + labels[i] = "input_" + std::to_string(i) + "_label"; + } + return labels; +} +} // namespace +struct UnionNode : ExecNode { + UnionNode(ExecPlan* plan, std::vector<ExecNode*> inputs) + : ExecNode(plan, inputs, GetInputLabels(inputs), + /*output_schema=*/inputs[0]->output_schema(), + /*num_outputs=*/1) { + if (this->input_count_.SetTotal(static_cast<int>(inputs.size()))) { + finished_.MarkFinished(); + } + } + + const char* kind_name() override { return "UnionNode"; } + + static Result<ExecNode*> Make(ExecPlan* plan, std::vector<ExecNode*> inputs, + const ExecNodeOptions& options) { + RETURN_NOT_OK(ValidateExecNodeInputs(plan, inputs, inputs.size(), "UnionNode")); + if (inputs.size() < 2) { Review comment: It's kinda pointless, but allowing one input would be a little more consistent. (It might make life a little easier to not have to special case the n = 1 case when generating exec plans.) ########## File path: cpp/src/arrow/compute/exec/union_node_test.cc ########## @@ -0,0 +1,203 @@ +// 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(parallel ? "parallel" : "single threaded"); + + ASSERT_OK_AND_ASSIGN(auto plan, ExecPlan::Make()); + + Declaration union_decl{"union", ExecNodeOptions{}}; + + // 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("colum_i32", int32()), field("colum_str", utf8())}); + auto r_schema = schema({field("colum_i32", int32()), field("colum_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("colum_i32", int32()), field("colum_str", utf8())}); + auto r_schema = schema({field("colum_i32", int32()), field("colum_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); +} + +TEST(UnionTest, TestNonEmpty) { + for (bool parallel : {false, true}) { + RunNonEmptyTest(parallel); + } +} + +TEST(UnionTest, TestEmpty) { + for (bool parallel : {false, true}) { + RunEmptyTest(parallel); + } +} Review comment: I think it'd be good to add a test that tries to create a union node with 0 inputs, and with differing schemas, to ensure those cases are covered. -- 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]
