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


##########
cpp/src/arrow/compute/kernels/vector_rolling.cc:
##########
@@ -0,0 +1,785 @@
+// 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 <functional>
+#include <set>
+#include <type_traits>
+#include <utility>
+#include "arrow/array/array_base.h"
+#include "arrow/array/builder_primitive.h"
+#include "arrow/compute/api_scalar.h"
+#include "arrow/compute/api_vector.h"
+#include "arrow/compute/cast.h"
+#include "arrow/compute/kernels/base_arithmetic_internal.h"
+#include "arrow/compute/kernels/codegen_internal.h"
+#include "arrow/compute/kernels/common_internal.h"
+#include "arrow/compute/registry.h"
+#include "arrow/result.h"
+#include "arrow/type_traits.h"
+#include "arrow/util/bit_block_counter.h"
+#include "arrow/util/bit_util.h"
+#include "arrow/util/logging.h"
+#include "arrow/visit_type_inline.h"
+
+namespace arrow::compute::internal {
+namespace {
+struct RollingOptionsWrapper : public OptionsWrapper<RollingOptions> {
+  explicit RollingOptionsWrapper(RollingOptions options)
+      : OptionsWrapper(std::move(options)) {}
+
+  static Result<std::unique_ptr<KernelState>> Init(KernelContext* ctx,
+                                                   const KernelInitArgs& args) 
{
+    auto options = checked_cast<const RollingOptions*>(args.options);
+    if (!options) {
+      return Status::Invalid(
+          "Attempted to initialize KernelState from null FunctionOptions");
+    }
+
+    if (options->min_periods > options->window_length) {
+      return Status::Invalid("min_periods must be less than or equal to 
window_length");
+    }
+
+    if (options->window_length <= 0) {
+      return Status::Invalid("window_length must be greater than 0");
+    }
+
+    if (options->min_periods <= 0) {
+      return Status::Invalid("min_periods must be greater than 0");
+    }
+
+    return std::make_unique<RollingOptionsWrapper>(*options);
+  }
+};
+
+template <typename ArgType>
+struct SumWindow {
+  using OutType = ArgType;
+  using ArgValue = typename GetViewType<ArgType>::T;
+  using OutValue = typename GetOutputType<OutType>::T;
+  KernelContext* ctx;
+  ArgValue sum = 0;
+  explicit SumWindow(KernelContext* ctx) : ctx(ctx) {}
+
+  void Append(ArgValue value, Status* st) {
+    sum = Add::Call<ArgValue>(ctx, sum, value, st);
+  }
+  void Remove(ArgValue value, Status* st) {
+    sum = Subtract::Call<ArgValue>(ctx, sum, value, st);
+  }
+  OutValue GetValue(Status* st) const { return sum; }
+};
+
+template <typename ArgType>
+struct SumCheckedWindow {
+  using OutType = ArgType;
+  using ArgValue = typename GetViewType<ArgType>::T;
+  using OutValue = typename GetOutputType<OutType>::T;
+  KernelContext* ctx;
+  ArgValue sum = 0;
+  explicit SumCheckedWindow(KernelContext* ctx) : ctx(ctx) {}
+
+  void Append(ArgValue value, Status* st) {
+    sum = AddChecked::Call<ArgValue>(ctx, sum, value, st);
+  }
+  void Remove(ArgValue value, Status* st) {
+    sum = SubtractChecked::Call<ArgValue>(ctx, sum, value, st);
+  }
+  OutValue GetValue(Status* st) const { return sum; }
+};
+
+template <typename ArgType>
+struct ProdWindow {
+  using OutType = ArgType;
+  using ArgValue = typename GetViewType<ArgType>::T;
+  using OutValue = typename GetOutputType<OutType>::T;
+  KernelContext* ctx;
+  ArgValue prod = 1;
+  explicit ProdWindow(KernelContext* ctx) : ctx(ctx) {}
+
+  void Append(ArgValue value, Status* st) {
+    prod = Multiply::Call<ArgValue, ArgValue, ArgValue>(ctx, prod, value, st);
+  }
+  void Remove(ArgValue value, Status* st) {
+    prod = Divide::Call<ArgValue, ArgValue, ArgValue>(ctx, prod, value, st);
+  }
+  OutValue GetValue(Status* st) const { return prod; }
+};
+
+template <typename ArgType>
+struct ProdCheckedWindow {
+  using OutType = ArgType;
+  using ArgValue = typename GetViewType<ArgType>::T;
+  using OutValue = typename GetOutputType<OutType>::T;
+  KernelContext* ctx;
+  ArgValue prod = 1;
+  explicit ProdCheckedWindow(KernelContext* ctx) : ctx(ctx) {}
+
+  void Append(ArgValue value, Status* st) {
+    prod = MultiplyChecked::Call<ArgValue, ArgValue, ArgValue>(ctx, prod, 
value, st);
+  }
+  void Remove(ArgValue value, Status* st) {
+    prod = DivideChecked::Call<ArgValue, ArgValue, ArgValue>(ctx, prod, value, 
st);
+  }
+  OutValue GetValue(Status* st) const { return prod; }
+};
+
+template <typename ArgType>
+struct MinWindow {
+  using OutType = ArgType;
+  using ArgValue = typename GetViewType<ArgType>::T;
+  using OutValue = typename GetOutputType<OutType>::T;
+  KernelContext* ctx;
+  std::multiset<ArgValue> values;
+  explicit MinWindow(KernelContext* ctx) : ctx(ctx) {}
+  void Append(ArgValue value, Status* st) { values.insert(value); }
+  void Remove(ArgValue value, Status* st) {
+    auto it = values.find(value);
+    DCHECK_NE(it, values.end());
+    values.erase(it);
+  }
+  OutValue GetValue(Status* st) const { return *values.begin(); }
+};
+
+template <typename ArgType>
+struct MaxWindow {
+  using OutType = ArgType;
+  using ArgValue = typename GetViewType<ArgType>::T;
+  using OutValue = typename GetOutputType<OutType>::T;
+  KernelContext* ctx;
+  std::multiset<ArgValue, std::greater<ArgValue>> values;
+  explicit MaxWindow(KernelContext* ctx) : ctx(ctx) {}
+  void Append(ArgValue value, Status* st) { values.insert(value); }
+  void Remove(ArgValue value, Status* st) {
+    auto it = values.find(value);
+    DCHECK_NE(it, values.end());
+    values.erase(it);
+  }
+  OutValue GetValue(Status* st) const { return *values.begin(); }
+};
+
+template <typename ArgType>
+struct MeanWindow {
+  using OutType = DoubleType;
+  using ArgValue = typename GetViewType<ArgType>::T;
+  using OutValue = typename GetOutputType<OutType>::T;
+  KernelContext* ctx;
+  double sum = 0;
+  int64_t count = 0;
+  explicit MeanWindow(KernelContext* ctx) : ctx(ctx) {}
+  void Append(ArgValue value, Status* st) {
+    sum += value;
+    count++;
+  }
+  void Remove(ArgValue value, Status* st) {
+    sum -= value;
+    count--;
+  }
+  OutValue GetValue(Status* st) const {
+    DCHECK_GE(count, 0);
+    return sum / count;
+  }
+};
+
+template <typename ArgType, typename Window>
+struct WindowAccumulator {
+  using OutType = typename Window::OutType;
+  using OutValue = typename GetOutputType<OutType>::T;
+  using ArgValue = typename GetViewType<ArgType>::T;

Review Comment:
   Right, and a large amount of template parameter thanks to this. I've updated 
the code.



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