Skip to content

Commit 4743e18

Browse files
cyb70289bkietz
authored andcommitted
ARROW-12074: [C++][Compute] Add scalar arithmetic kernels for decimal
Add basic binary arithmetic (+,-,*,/) kernels for decimal types. Closes #10364 from cyb70289/decimal-arith Authored-by: Yibo Cai <yibo.cai@arm.com> Signed-off-by: Benjamin Kietzman <bengilgit@gmail.com>
1 parent f1e3c2e commit 4743e18

8 files changed

Lines changed: 700 additions & 15 deletions

File tree

cpp/src/arrow/compute/kernel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ class ARROW_EXPORT OutputType {
321321
this->resolver_ = other.resolver_;
322322
}
323323

324+
OutputType& operator=(const OutputType&) = default;
325+
OutputType& operator=(OutputType&&) = default;
326+
324327
/// \brief Return the shape and type of the expected output value of the
325328
/// kernel given the value descriptors (shapes and types) of the input
326329
/// arguments. The resolver may make use of state information kept in the

cpp/src/arrow/compute/kernels/codegen_internal.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ struct GetViewType<Decimal128Type> {
149149
static T LogicalValue(PhysicalType value) {
150150
return Decimal128(reinterpret_cast<const uint8_t*>(value.data()));
151151
}
152+
153+
static T LogicalValue(T value) { return value; }
152154
};
153155

154156
template <>
@@ -159,6 +161,8 @@ struct GetViewType<Decimal256Type> {
159161
static T LogicalValue(PhysicalType value) {
160162
return Decimal256(reinterpret_cast<const uint8_t*>(value.data()));
161163
}
164+
165+
static T LogicalValue(T value) { return value; }
162166
};
163167

164168
template <typename Type, typename Enable = void>
@@ -243,6 +247,18 @@ struct ArrayIterator<Type, enable_if_base_binary<Type>> {
243247
}
244248
};
245249

250+
template <typename Type>
251+
struct ArrayIterator<Type, enable_if_decimal<Type>> {
252+
using T = typename TypeTraits<Type>::ScalarType::ValueType;
253+
using endian_agnostic = std::array<uint8_t, sizeof(T)>;
254+
const endian_agnostic* values;
255+
256+
explicit ArrayIterator(const ArrayData& data)
257+
: values(data.GetValues<endian_agnostic>(1)) {}
258+
259+
T operator()() { return T{values++->data()}; }
260+
};
261+
246262
// Iterator over various output array types, taking a GetOutputType<Type>
247263

248264
template <typename Type, typename Enable = void>
@@ -262,6 +278,20 @@ struct OutputArrayWriter<Type, enable_if_has_c_type_not_boolean<Type>> {
262278
void WriteNull() { *values++ = T{}; }
263279
};
264280

281+
template <typename Type>
282+
struct OutputArrayWriter<Type, enable_if_decimal<Type>> {
283+
using T = typename TypeTraits<Type>::ScalarType::ValueType;
284+
using endian_agnostic = std::array<uint8_t, sizeof(T)>;
285+
endian_agnostic* values;
286+
287+
explicit OutputArrayWriter(ArrayData* data)
288+
: values(data->GetMutableValues<endian_agnostic>(1)) {}
289+
290+
void Write(T value) { value.ToBytes(values++->data()); }
291+
292+
void WriteNull() { T{}.ToBytes(values++->data()); }
293+
};
294+
265295
// (Un)box Scalar to / from C++ value
266296

267297
template <typename Type, typename Enable = void>
@@ -538,6 +568,22 @@ struct OutputAdapter<Type, enable_if_base_binary<Type>> {
538568
}
539569
};
540570

571+
template <typename Type>
572+
struct OutputAdapter<Type, enable_if_decimal<Type>> {
573+
using T = typename TypeTraits<Type>::ScalarType::ValueType;
574+
using endian_agnostic = std::array<uint8_t, sizeof(T)>;
575+
576+
template <typename Generator>
577+
static Status Write(KernelContext*, Datum* out, Generator&& generator) {
578+
ArrayData* out_arr = out->mutable_array();
579+
auto out_data = out_arr->GetMutableValues<endian_agnostic>(1);
580+
for (int64_t i = 0; i < out_arr->length; ++i) {
581+
generator().ToBytes(out_data++->data());
582+
}
583+
return Status::OK();
584+
}
585+
};
586+
541587
// A kernel exec generator for unary functions that addresses both array and
542588
// scalar inputs and dispatches input iteration and output writing to other
543589
// templates

0 commit comments

Comments
 (0)