MCPcopy Create free account
hub / github.com/apache/arrow / ClipValues

Function ClipValues

cpp/src/arrow/compute/kernels/vector_statistics.cc:61–157  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

59// We have a first unused template parameter for compatibility with GenerateNumeric.
60template <typename Unused, typename Type>
61struct Winsorize {
62 using ArrayType = typename TypeTraits<Type>::ArrayType;
63 using CType = typename TypeTraits<Type>::CType;
64
65 static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
66 const auto& options = WinsorizeState::Get(ctx);
67 RETURN_NOT_OK(ValidateOptions(options));
68 auto data = batch.values[0].array.ToArrayData();
69 ARROW_ASSIGN_OR_RAISE(auto maybe_quantiles, GetQuantileValues(ctx, data, options));
70 auto out_data = out->array_data_mutable();
71 if (!maybe_quantiles.has_value()) {
72 // Only nulls and NaNs => return input as-is
73 out_data->null_count = data->null_count.load();
74 out_data->length = data->length;
75 out_data->offset = data->offset;
76 out_data->buffers = data->buffers;
77 return Status::OK();
78 }
79 return ClipValues(*data, maybe_quantiles.value(), out_data, ctx);
80 }
81
82 static Status ExecChunked(KernelContext* ctx, const ExecBatch& batch, Datum* out) {
83 const auto& options = WinsorizeState::Get(ctx);
84 RETURN_NOT_OK(ValidateOptions(options));
85 const auto& chunked_array = batch.values[0].chunked_array();
86 ARROW_ASSIGN_OR_RAISE(auto maybe_quantiles,
87 GetQuantileValues(ctx, chunked_array, options));
88 if (!maybe_quantiles.has_value()) {
89 // Only nulls and NaNs => return input as-is
90 *out = chunked_array;
91 return Status::OK();
92 }
93 ArrayVector out_chunks;
94 out_chunks.reserve(chunked_array->num_chunks());
95 for (const auto& chunk : chunked_array->chunks()) {
96 auto out_data = chunk->data()->Copy();
97 RETURN_NOT_OK(
98 ClipValues(*chunk->data(), maybe_quantiles.value(), out_data.get(), ctx));
99 out_chunks.push_back(MakeArray(out_data));
100 }
101 return ChunkedArray::Make(std::move(out_chunks)).Value(out);
102 }
103
104 struct QuantileValues {
105 CType lower_bound, upper_bound;
106 };
107
108 static Result<std::optional<QuantileValues>> GetQuantileValues(
109 KernelContext* ctx, const Datum& input, const WinsorizeOptions& options) {
110 // We use "nearest" to avoid the conversion of quantile values to double.
111 QuantileOptions quantile_options(/*q=*/{options.lower_limit, options.upper_limit},
112 QuantileOptions::NEAREST);
113 ARROW_ASSIGN_OR_RAISE(
114 auto quantile,
115 CallFunction("quantile", {input}, &quantile_options, ctx->exec_context()));
116 auto quantile_array = quantile.array_as<ArrayType>();
117 DCHECK_EQ(quantile_array->length(), 2);
118 // The quantile function outputs either all nulls or no nulls at all.

Callers 2

ExecMethod · 0.85
ExecChunkedMethod · 0.85

Calls 9

VisitSetBitRunsVoidFunction · 0.85
ARROW_ASSIGN_OR_RAISEFunction · 0.50
OKFunction · 0.50
sizeMethod · 0.45
loadMethod · 0.45
memory_poolMethod · 0.45
AllocateMethod · 0.45
mutable_dataMethod · 0.45

Tested by

no test coverage detected