lidavidm commented on a change in pull request #10927:
URL: https://github.com/apache/arrow/pull/10927#discussion_r690502753



##########
File path: cpp/src/arrow/compute/exec/union_node.cc
##########
@@ -0,0 +1,145 @@
+// 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(const 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) {
+    bool counter_completed = 
input_count_.SetTotal(static_cast<int>(inputs.size()));
+    ARROW_DCHECK(counter_completed == false);
+  }
+
+  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, 
static_cast<int>(inputs.size()),
+                                         "UnionNode"));
+    if (inputs.size() < 1) {
+      return Status::Invalid("Constructing a `UnionNode` with inputs size less 
than 1");
+    }
+    auto schema = inputs.at(0)->output_schema();
+    for (auto input : inputs) {
+      if (!input->output_schema()->Equals(schema)) {
+        return Status::Invalid(
+            "UnionNode input schemas must all match, first schema was: ",
+            schema->ToString(), " got schema: ", 
input->output_schema()->ToString());
+      }
+    }
+    return plan->EmplaceNode<UnionNode>(plan, std::move(inputs));
+  }
+
+  void InputReceived(ExecNode* input, int seq, ExecBatch batch) override {
+    ARROW_DCHECK(std::find(inputs_.begin(), inputs_.end(), 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(inputs_.begin(), inputs_.end(), input) != 
inputs_.end());
+
+    if (input_count_.Increment()) {
+      int total_batches = batch_count_.count();
+      outputs_[0]->InputFinished(this, total_batches);
+      if (batch_count_.SetTotal(total_batches)) {
+        finished_.MarkFinished();
+      }
+    }

Review comment:
       This has a logic error again: InputFinished can be called before 
InputReceived. We need to make use of num_total here.

##########
File path: cpp/src/arrow/compute/exec/union_node.cc
##########
@@ -0,0 +1,145 @@
+// 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(const 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) {
+    bool counter_completed = 
input_count_.SetTotal(static_cast<int>(inputs.size()));
+    ARROW_DCHECK(counter_completed == false);
+  }
+
+  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, 
static_cast<int>(inputs.size()),
+                                         "UnionNode"));
+    if (inputs.size() < 1) {
+      return Status::Invalid("Constructing a `UnionNode` with inputs size less 
than 1");
+    }
+    auto schema = inputs.at(0)->output_schema();
+    for (auto input : inputs) {
+      if (!input->output_schema()->Equals(schema)) {
+        return Status::Invalid(
+            "UnionNode input schemas must all match, first schema was: ",
+            schema->ToString(), " got schema: ", 
input->output_schema()->ToString());
+      }
+    }
+    return plan->EmplaceNode<UnionNode>(plan, std::move(inputs));
+  }
+
+  void InputReceived(ExecNode* input, int seq, ExecBatch batch) override {
+    ARROW_DCHECK(std::find(inputs_.begin(), inputs_.end(), 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(inputs_.begin(), inputs_.end(), input) != 
inputs_.end());
+
+    if (input_count_.Increment()) {
+      int total_batches = batch_count_.count();
+      outputs_[0]->InputFinished(this, total_batches);
+      if (batch_count_.SetTotal(total_batches)) {
+        finished_.MarkFinished();
+      }
+    }

Review comment:
       @nirandaperera I don't think the proposal in 
https://github.com/apache/arrow/pull/10927#discussion_r690444907 works because 
we need to track both 1) batches seen so far and 2) total batches as reported 
by our inputs and we can't reuse the same counter for this, since setting the 
total of an AtomicCounter is effectively a one-time-only operation. (You could 
imagine ways to stuff it into the encoding: for example, an AccumulateTotal() 
that adds a *negative* value to total_, and a separate method SetTotal(void) 
that inverts the value - that would work modulo having to express all this with 
just atomics.)




-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to