zanmato1984 commented on code in PR #45763:
URL: https://github.com/apache/arrow/pull/45763#discussion_r1995647546


##########
cpp/src/arrow/compute/kernels/vector_statistics.cc:
##########
@@ -0,0 +1,208 @@
+// 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 <memory>
+#include <optional>
+#include <utility>
+
+#include "arrow/compute/api_aggregate.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/codegen_internal.h"
+#include "arrow/compute/registry.h"
+#include "arrow/result.h"
+#include "arrow/scalar.h"
+#include "arrow/status.h"
+#include "arrow/util/bit_run_reader.h"
+#include "arrow/util/checked_cast.h"
+#include "arrow/util/logging.h"
+
+namespace arrow::compute::internal {
+
+using ::arrow::internal::checked_cast;
+
+namespace {
+
+Status ValidateOptions(const WinsorizeOptions& options) {
+  if (!(options.lower_limit >= 0 && options.lower_limit <= 1) ||
+      !(options.upper_limit >= 0 && options.upper_limit <= 1)) {
+    return Status::Invalid("winsorize limits must be between 0 and 1");
+  }
+  if (options.lower_limit > options.upper_limit) {
+    return Status::Invalid(
+        "winsorize upper limit must be equal or greater than lower limit");
+  }
+  return Status::OK();
+}
+
+using WinsorizeState = internal::OptionsWrapper<WinsorizeOptions>;
+
+// We have a first unused template parameter for compatibility with 
GenerateNumeric.
+template <typename Unused, typename Type>
+struct Winsorize {
+  using ArrayType = typename TypeTraits<Type>::ArrayType;
+  using CType = typename TypeTraits<Type>::CType;
+
+  static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* 
out) {
+    const auto& options = WinsorizeState::Get(ctx);
+    RETURN_NOT_OK(ValidateOptions(options));
+    ARROW_ASSIGN_OR_RAISE(auto maybe_quantiles,
+                          GetQuantileValues(ctx, batch.ToExecBatch(), 
options));
+    auto data = batch.values[0].array.ToArrayData();
+    auto out_data = out->array_data_mutable();
+    if (!maybe_quantiles.has_value()) {
+      // Only nulls and NaNs => return input as-is
+      out_data->null_count = data->null_count.load();
+      out_data->length = data->length;
+      out_data->buffers = data->buffers;
+      return Status::OK();
+    }
+    return ClipValues(*data, maybe_quantiles.value(), out_data, ctx);
+  }
+
+  static Status ExecChunked(KernelContext* ctx, const ExecBatch& batch, Datum* 
out) {
+    const auto& options = WinsorizeState::Get(ctx);
+    RETURN_NOT_OK(ValidateOptions(options));
+    ARROW_ASSIGN_OR_RAISE(auto maybe_quantiles, GetQuantileValues(ctx, batch, 
options));
+    const auto& chunked_array = batch.values[0].chunked_array();
+    if (!maybe_quantiles.has_value()) {
+      // Only nulls and NaNs => return input as-is
+      *out = chunked_array;
+      return Status::OK();
+    }
+    ArrayVector out_chunks;
+    out_chunks.reserve(chunked_array->num_chunks());
+    for (const auto& chunk : chunked_array->chunks()) {
+      auto out_data = chunk->data()->Copy();
+      RETURN_NOT_OK(
+          ClipValues(*chunk->data(), maybe_quantiles.value(), out_data.get(), 
ctx));
+      out_chunks.push_back(MakeArray(out_data));
+    }
+    return ChunkedArray::Make(std::move(out_chunks)).Value(out);
+  }
+
+  struct QuantileValues {
+    CType lower_bound, upper_bound;
+  };
+
+  static Result<std::optional<QuantileValues>> GetQuantileValues(
+      KernelContext* ctx, const ExecBatch& batch, const WinsorizeOptions& 
options) {
+    // We use "nearest" to avoid the conversion of quantile values to double.
+    QuantileOptions quantile_options(/*q=*/{options.lower_limit, 
options.upper_limit},
+                                     QuantileOptions::NEAREST);
+    ARROW_ASSIGN_OR_RAISE(
+        auto quantile,
+        CallFunction("quantile", batch, &quantile_options, 
ctx->exec_context()));

Review Comment:
   Correction: s/array/datum



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