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

Method ComputeMode

cpp/src/arrow/compute/kernels/aggregate_mode.cc:275–325  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

273
274 template <typename Container>
275 Status ComputeMode(KernelContext* ctx, const Container& arr, int64_t length,
276 int64_t null_count, const DataType& type, ExecResult* out) {
277 const ModeOptions& options = ModeState::Get(ctx);
278 const int64_t in_length = length - null_count;
279 if ((!options.skip_nulls && null_count > 0) || (in_length < options.min_count)) {
280 return PrepareOutput<T>(/*n=*/0, ctx, type, out).status();
281 }
282
283 // copy all chunks to a buffer, ignore nulls and nans
284 std::vector<CType, Allocator> values(Allocator(ctx->memory_pool()));
285
286 uint64_t nan_count = 0;
287 if (length > 0) {
288 values.resize(length - null_count);
289 CopyNonNullValues(arr, values.data());
290
291 // drop nan
292 if (is_floating_type<T>::value) {
293 const auto& it =
294 std::remove_if(values.begin(), values.end(), [](CType v) { return v != v; });
295 nan_count = values.end() - it;
296 values.resize(it - values.begin());
297 }
298 }
299 // sort the input data to count same values
300 std::sort(values.begin(), values.end());
301
302 // generator to emit next value:count pair
303 auto it = values.cbegin();
304 auto gen = [&]() {
305 if (ARROW_PREDICT_FALSE(it == values.cend())) {
306 // handle NAN at last
307 if (nan_count > 0) {
308 auto value_count = std::make_pair(GetNan(), nan_count);
309 nan_count = 0;
310 return value_count;
311 }
312 return std::pair<CType, uint64_t>(static_cast<CType>(0), kCountEOF);
313 }
314 // count same values
315 const CType value = *it;
316 uint64_t count = 0;
317 do {
318 ++it;
319 ++count;
320 } while (it != values.cend() && *it == value);
321 return std::make_pair(value, count);
322 };
323
324 return Finalize<T>(ctx, type, out, std::move(gen));
325 }
326
327 Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
328 const ArraySpan& values = batch[0].array;

Callers

nothing calls this directly

Calls 10

CopyNonNullValuesFunction · 0.85
resizeMethod · 0.80
cbeginMethod · 0.80
cendMethod · 0.80
GetFunction · 0.50
statusMethod · 0.45
memory_poolMethod · 0.45
dataMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected