EnricoMi commented on code in PR #44738:
URL: https://github.com/apache/arrow/pull/44738#discussion_r1925461024


##########
cpp/src/arrow/acero/assert_order_node.cc:
##########
@@ -0,0 +1,402 @@
+// 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 <arrow/compute/kernels/vector_sort_internal.h>
+#include <parquet/types.h>
+#include <sstream>
+
+#include "arrow/acero/accumulation_queue.h"
+#include "arrow/acero/exec_plan.h"
+#include "arrow/acero/options.h"
+#include "arrow/acero/query_context.h"
+#include "arrow/acero/util.h"
+#include "arrow/compute/api_vector.h"
+#include "arrow/compute/exec.h"
+#include "arrow/result.h"
+#include "arrow/util/checked_cast.h"
+#include "arrow/util/future.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/tracing_internal.h"
+
+namespace arrow {
+
+using internal::checked_cast;
+
+namespace acero {
+namespace {
+
+// Compare two records in a single column (either from a batch or table)
+struct ColumnComparator {
+  ColumnComparator(const compute::NullPlacement& null_placement,
+                   const compute::SortOrder sort_order)
+      : null_placement(null_placement),
+        sort_order(sort_order),
+        left_is_null_order(null_placement == compute::NullPlacement::AtStart ? 
-1 : 1),
+        right_is_null_order(null_placement == compute::NullPlacement::AtStart 
? 1 : -1) {}
+
+  virtual ~ColumnComparator() = default;
+
+  virtual int Compare(const Array& column, const int64_t& left,
+                      const int64_t& right) const = 0;
+
+  compute::NullPlacement null_placement;
+  compute::SortOrder sort_order;
+  int left_is_null_order;
+  int right_is_null_order;
+};
+
+template <typename Type>
+struct ConcreteColumnComparator final : public ColumnComparator {
+  using ColumnComparator::ColumnComparator;
+
+  using ArrayType = typename TypeTraits<Type>::ArrayType;
+  using GetView = compute::internal::GetViewType<Type>;
+
+  int Compare(const Array& column, const int64_t& left,
+              const int64_t& right) const override {
+    const ArrayType& array = ::arrow::internal::checked_cast<const 
ArrayType&>(column);
+
+    const auto left_value = GetView::LogicalValue(array.GetView(left));
+    const auto right_value = GetView::LogicalValue(array.GetView(right));
+
+    auto left_is_null = column.IsNull(left);
+    auto right_is_null = column.IsNull(right);
+
+    auto orientation = sort_order == compute::SortOrder::Ascending ? 1 : -1;
+    auto less = -1 * orientation;
+    auto larger = 1 * orientation;
+
+    if (left_is_null && right_is_null) {
+      // both values are null (equal)
+      return 0;
+    } else if (left_is_null) {
+      // left value is null
+      return left_is_null_order;
+    } else if (right_is_null) {
+      // right value is null
+      return right_is_null_order;
+    } else if (left_value < right_value) {
+      return less;
+    } else if (left_value > right_value) {
+      return larger;
+    }
+
+    return 0;
+  }
+};
+
+template <>
+struct ConcreteColumnComparator<NullType> : public ColumnComparator {
+  using ColumnComparator::ColumnComparator;
+
+  int Compare(const Array& column, const int64_t& left,
+              const int64_t& right) const override {
+    return 0;
+  }
+};
+
+class AssertOrderNode : public ExecNode,
+                        public TracedNode,
+                        util::SequencingQueue::Processor {
+ public:
+  AssertOrderNode(ExecPlan* plan, std::vector<ExecNode*> inputs,
+                  std::shared_ptr<Schema> output_schema, const Ordering& 
ordering)
+      : ExecNode(plan, std::move(inputs), {"input"}, std::move(output_schema)),
+        TracedNode(this),
+        ordering_(ordering),
+        comparators_(MakeComparators(output_schema_, ordering_)),
+        sort_ascs_(MakeSortAscs(ordering.sort_keys())),
+        sequencing_queue_(util::SequencingQueue::Make(this)) {}
+
+  static Result<ExecNode*> Make(ExecPlan* plan, std::vector<ExecNode*> inputs,
+                                const ExecNodeOptions& options) {
+    RETURN_NOT_OK(ValidateExecNodeInputs(plan, inputs, 1, "AssertOrderNode"));
+
+    const auto& assert_options = checked_cast<const 
AssertOrderNodeOptions&>(options);
+
+    Ordering ordering = assert_options.ordering;
+
+    // check output ordering
+    if (ordering.is_implicit() || ordering.is_unordered()) {

Review Comment:
   right, fixed



-- 
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]

Reply via email to