From 2fda2dbe917b68d5dccb054140a68ec5fea014c4 Mon Sep 17 00:00:00 2001 From: mwish Date: Sun, 18 Aug 2024 01:24:05 +0800 Subject: [PATCH] extract helper function --- cpp/src/arrow/compute/kernels/row_encoder.cc | 32 +++++++++----------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/cpp/src/arrow/compute/kernels/row_encoder.cc b/cpp/src/arrow/compute/kernels/row_encoder.cc index e600dec21cd95..b367c85d50751 100644 --- a/cpp/src/arrow/compute/kernels/row_encoder.cc +++ b/cpp/src/arrow/compute/kernels/row_encoder.cc @@ -75,33 +75,31 @@ void BooleanKeyEncoder::AddLengthNull(int32_t* length) { Status BooleanKeyEncoder::Encode(const ExecValue& data, int64_t batch_length, uint8_t** encoded_bytes) { + auto handle_next_valid_value = [encoded_bytes](bool value) mutable { + auto& encoded_ptr = *encoded_bytes++; + *encoded_ptr++ = kValidByte; + *encoded_ptr++ = value; + }; + auto handle_next_null_value = [encoded_bytes]() mutable { + auto& encoded_ptr = *encoded_bytes++; + *encoded_ptr++ = kNullByte; + *encoded_ptr++ = 0; + }; + if (data.is_array()) { VisitArraySpanInline( - data.array, - [&](bool value) { - auto& encoded_ptr = *encoded_bytes++; - *encoded_ptr++ = kValidByte; - *encoded_ptr++ = value; - }, - [&] { - auto& encoded_ptr = *encoded_bytes++; - *encoded_ptr++ = kNullByte; - *encoded_ptr++ = 0; - }); + data.array, [&](bool value) { handle_next_valid_value(value); }, + [&] { handle_next_null_value(); }); } else { const auto& scalar = data.scalar_as(); if (!scalar.is_valid) { for (int64_t i = 0; i < batch_length; i++) { - auto& encoded_ptr = *encoded_bytes++; - *encoded_ptr++ = kNullByte; - *encoded_ptr++ = 0; + handle_next_null_value(); } } else { const bool value = scalar.value; for (int64_t i = 0; i < batch_length; i++) { - auto& encoded_ptr = *encoded_bytes++; - *encoded_ptr++ = kValidByte; - *encoded_ptr++ = value; + handle_next_valid_value(value); } } }