You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "js8544 (via GitHub)" <gi...@apache.org> on 2023/06/28 14:52:49 UTC

[GitHub] [arrow] js8544 commented on a diff in pull request #35787: GH-35786: [C++] Add pairwise_diff function

js8544 commented on code in PR #35787:
URL: https://github.com/apache/arrow/pull/35787#discussion_r1245355801


##########
cpp/src/arrow/compute/kernels/vector_pairwise.cc:
##########
@@ -0,0 +1,272 @@
+// 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.
+
+// Vector kernels for pairwise computation
+
+#include <memory>
+#include "arrow/builder.h"
+#include "arrow/compute/api_vector.h"
+#include "arrow/compute/exec.h"
+#include "arrow/compute/function.h"
+#include "arrow/compute/kernel.h"
+#include "arrow/compute/kernels/base_arithmetic_internal.h"
+#include "arrow/compute/kernels/codegen_internal.h"
+#include "arrow/compute/registry.h"
+#include "arrow/status.h"
+#include "arrow/type.h"
+#include "arrow/type_fwd.h"
+#include "arrow/type_traits.h"
+#include "arrow/util/checked_cast.h"
+#include "arrow/util/logging.h"
+#include "arrow/visit_type_inline.h"
+
+namespace arrow::compute::internal {
+
+template <typename InputType>
+Result<std::shared_ptr<DataType>> GetDiffOutputType(
+    const std::shared_ptr<arrow::DataType>& type) {
+  std::shared_ptr<DataType> output_type;
+  if constexpr (is_timestamp_type<InputType>::value) {  // timestamp -> duration with same
+                                                        // time unit
+    const auto* real_type = checked_cast<const TimestampType*>(type.get());
+    return std::make_shared<DurationType>(real_type->unit());
+  } else if constexpr (is_time_type<InputType>::value) {  // time -> duration with same
+                                                          // time unit
+    const auto* real_type = checked_cast<const InputType*>(type.get());
+    return std::make_shared<DurationType>(real_type->unit());
+  } else if constexpr (is_date_type<InputType>::value) {  // date -> duration
+    if constexpr (InputType::type_id == Type::DATE32) {   // date32 -> second
+      return duration(TimeUnit::SECOND);
+    } else {  // date64 -> millisecond
+      return duration(TimeUnit::MILLI);
+    }
+  } else if constexpr (is_decimal_type<InputType>::value) {  // decimal -> decimal with
+                                                             // precision + 1
+    const auto* real_type = checked_cast<const InputType*>(type.get());
+    if constexpr (InputType::type_id == Type::DECIMAL128) {
+      return Decimal128Type::Make(real_type->precision() + 1, real_type->scale());
+    } else {
+      return Decimal256Type::Make(real_type->precision() + 1, real_type->scale());
+    }
+  } else {
+    return type;
+  }
+}
+
+/// A generic pairwise implementation that can be reused by different Ops.
+template <typename InputType, typename OutputType, typename Op>
+Status PairwiseKernelImpl(const ArraySpan& input, int64_t periods,
+                          const std::shared_ptr<DataType>& output_type,
+                          std::shared_ptr<ArrayData>* result) {
+  typename TypeTraits<OutputType>::BuilderType builder(output_type,
+                                                       default_memory_pool());
+  RETURN_NOT_OK(builder.Reserve(input.length));
+
+  Status status;
+  auto valid_func = [&](typename GetViewType<InputType>::T left,
+                        typename GetViewType<InputType>::T right) {
+    auto result = Op::template Call<typename GetOutputType<OutputType>::T>(
+        nullptr, left, right, &status);
+    builder.UnsafeAppend(result);
+  };
+  auto null_func = [&]() { builder.UnsafeAppendNull(); };
+
+  if (periods > 0) {
+    periods = std::min(periods, input.length);
+    RETURN_NOT_OK(builder.AppendNulls(periods));
+    ArraySpan left(input);
+    left.SetSlice(periods, input.length - periods);
+    ArraySpan right(input);
+    right.SetSlice(0, input.length - periods);
+    VisitTwoArrayValuesInline<InputType, InputType>(left, right, valid_func, null_func);
+    RETURN_NOT_OK(status);
+  } else {
+    periods = std::max(periods, -input.length);
+    ArraySpan left(input);
+    left.SetSlice(0, input.length + periods);
+    ArraySpan right(input);
+    right.SetSlice(-periods, input.length + periods);
+    VisitTwoArrayValuesInline<InputType, InputType>(left, right, valid_func, null_func);
+    RETURN_NOT_OK(status);
+    RETURN_NOT_OK(builder.AppendNulls(-periods));
+  }
+  RETURN_NOT_OK(builder.FinishInternal(result));
+  return Status::OK();
+}
+
+template <typename InputType, typename OutputType, typename Op>
+Status PairwiseDiffKernel(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {

Review Comment:
   Done



-- 
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-unsubscribe@arrow.apache.org

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