Skip to content

Commit 84d9c88

Browse files
GH-37027: [C++] Add float16 kernels to if-else and vector-replace functions (#46446)
### Rationale for this change Support float16/halffloat type in some functions which are agnostic to the numeric value. * Includes `replace_with_mask` as requested in #37027 ### What changes are included in this PR? * Added a case to `GenerateTypeAgnosticPrimitive` for `HALF_FLOAT` * Added `float16` kernels for `if_else`, `case_when`, `coalesce`, and `choose` * Added `float16` kernels for `replace_with_mask`, `fill_null_forward`, `fill_null_backward` * Add `HalfFloatType` to the list of types tested for the above functions ### Are these changes tested? Yes ### Are there any user-facing changes? Functions listed above now support float16 * GitHub Issue: #37027 Authored-by: Colin Schultz <[email protected]> Signed-off-by: Antoine Pitrou <[email protected]>
1 parent 8262e42 commit 84d9c88

7 files changed

Lines changed: 30 additions & 9 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,7 @@ KernelType GenerateTypeAgnosticPrimitive(detail::GetTypeId get_id) {
12621262
return Generator<UInt8Type, Args...>::Exec;
12631263
case Type::UINT16:
12641264
case Type::INT16:
1265+
case Type::HALF_FLOAT:
12651266
return Generator<UInt16Type, Args...>::Exec;
12661267
case Type::UINT32:
12671268
case Type::INT32:

‎cpp/src/arrow/compute/kernels/scalar_if_else.cc‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2870,7 +2870,7 @@ void RegisterScalarIfElse(FunctionRegistry* registry) {
28702870
AddPrimitiveIfElseKernels(func, TemporalTypes());
28712871
AddPrimitiveIfElseKernels(func, IntervalTypes());
28722872
AddPrimitiveIfElseKernels(func, DurationTypes());
2873-
AddPrimitiveIfElseKernels(func, {boolean()});
2873+
AddPrimitiveIfElseKernels(func, {boolean(), float16()});
28742874
AddNullIfElseKernel(func);
28752875
AddBinaryIfElseKernels(func, BaseBinaryTypes());
28762876
AddFixedWidthIfElseKernel<FixedSizeBinaryType>(func);
@@ -2886,7 +2886,7 @@ void RegisterScalarIfElse(FunctionRegistry* registry) {
28862886
AddPrimitiveCaseWhenKernels(func, TemporalTypes());
28872887
AddPrimitiveCaseWhenKernels(func, IntervalTypes());
28882888
AddPrimitiveCaseWhenKernels(func, DurationTypes());
2889-
AddPrimitiveCaseWhenKernels(func, {boolean(), null()});
2889+
AddPrimitiveCaseWhenKernels(func, {boolean(), null(), float16()});
28902890
AddCaseWhenKernel(func, Type::FIXED_SIZE_BINARY,
28912891
CaseWhenFunctor<FixedSizeBinaryType>::Exec);
28922892
AddCaseWhenKernel(func, Type::DECIMAL128, CaseWhenFunctor<FixedSizeBinaryType>::Exec);
@@ -2902,7 +2902,7 @@ void RegisterScalarIfElse(FunctionRegistry* registry) {
29022902
AddPrimitiveCoalesceKernels(func, TemporalTypes());
29032903
AddPrimitiveCoalesceKernels(func, IntervalTypes());
29042904
AddPrimitiveCoalesceKernels(func, DurationTypes());
2905-
AddPrimitiveCoalesceKernels(func, {boolean(), null()});
2905+
AddPrimitiveCoalesceKernels(func, {boolean(), null(), float16()});
29062906
AddCoalesceKernel(func, Type::FIXED_SIZE_BINARY,
29072907
CoalesceFunctor<FixedSizeBinaryType>::Exec);
29082908
AddCoalesceKernel(func, Type::DECIMAL128, CoalesceFunctor<FixedSizeBinaryType>::Exec);
@@ -2920,7 +2920,7 @@ void RegisterScalarIfElse(FunctionRegistry* registry) {
29202920
AddPrimitiveChooseKernels(func, TemporalTypes());
29212921
AddPrimitiveChooseKernels(func, IntervalTypes());
29222922
AddPrimitiveChooseKernels(func, DurationTypes());
2923-
AddPrimitiveChooseKernels(func, {boolean(), null()});
2923+
AddPrimitiveChooseKernels(func, {boolean(), null(), float16()});
29242924
AddChooseKernel(func, Type::FIXED_SIZE_BINARY,
29252925
ChooseFunctor<FixedSizeBinaryType>::Exec);
29262926
AddChooseKernel(func, Type::DECIMAL128, ChooseFunctor<FixedSizeBinaryType>::Exec);

‎cpp/src/arrow/compute/kernels/scalar_if_else_test.cc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ using IntegralArrowTypes = ::testing::Types<Int32Type>;
8080
#else
8181
using IfElseNumericBasedTypes =
8282
::testing::Types<UInt8Type, UInt16Type, UInt32Type, UInt64Type, Int8Type, Int16Type,
83-
Int32Type, Int64Type, FloatType, DoubleType, Date32Type, Date64Type,
84-
Time32Type, Time64Type, TimestampType, MonthIntervalType,
85-
DurationType>;
83+
Int32Type, Int64Type, HalfFloatType, FloatType, DoubleType,
84+
Date32Type, Date64Type, Time32Type, Time64Type, TimestampType,
85+
MonthIntervalType, DurationType>;
8686
#endif
8787

8888
TYPED_TEST_SUITE(TestIfElsePrimitive, IfElseNumericBasedTypes);

‎cpp/src/arrow/compute/kernels/vector_replace.cc‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ void RegisterVectorFunction(FunctionRegistry* registry,
843843
}
844844
add_primitive_kernel(null());
845845
add_primitive_kernel(boolean());
846+
add_primitive_kernel(float16());
846847
AddKernel(Type::FIXED_SIZE_BINARY,
847848
Functor<FixedSizeBinaryType>::GetSignature(Type::FIXED_SIZE_BINARY),
848849
Functor<FixedSizeBinaryType>::Exec, ChunkedFunctor<FixedSizeBinaryType>::Exec,

‎cpp/src/arrow/compute/kernels/vector_replace_test.cc‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,9 @@ class TestReplaceBinary : public TestReplaceKernel<T> {
233233

234234
using NumericBasedTypes =
235235
::testing::Types<UInt8Type, UInt16Type, UInt32Type, UInt64Type, Int8Type, Int16Type,
236-
Int32Type, Int64Type, FloatType, DoubleType, Date32Type, Date64Type,
237-
Time32Type, Time64Type, TimestampType, MonthIntervalType>;
236+
Int32Type, Int64Type, HalfFloatType, FloatType, DoubleType,
237+
Date32Type, Date64Type, Time32Type, Time64Type, TimestampType,
238+
MonthIntervalType>;
238239

239240
TYPED_TEST_SUITE(TestReplaceNumeric, NumericBasedTypes);
240241
TYPED_TEST_SUITE(TestReplaceDecimal, DecimalArrowTypes);
@@ -1490,6 +1491,7 @@ TYPED_TEST(TestFillNullNumeric, FillNullForwardLargeInput) {
14901491
ASSERT_OK_AND_ASSIGN(auto array_null, MakeArrayOfNull(array_random->type(), len_null));
14911492
auto array_null_filled =
14921493
ConstantArrayGenerator::Numeric<TypeParam>(len_null, x_ptr[len_random - 1]);
1494+
ASSERT_NE(array_null_filled, nullptr);
14931495
{
14941496
ASSERT_OK_AND_ASSIGN(auto value_array,
14951497
Concatenate({array_random, array_null, array_random}));

‎cpp/src/arrow/testing/generator.cc‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ std::shared_ptr<arrow::Array> ConstantArrayGenerator::Int64(int64_t size, int64_
9292
return ConstantArray<Int64Type>(size, value);
9393
}
9494

95+
std::shared_ptr<arrow::Array> ConstantArrayGenerator::Float16(int64_t size,
96+
uint16_t value) {
97+
return ConstantArray<HalfFloatType>(size, value);
98+
}
99+
95100
std::shared_ptr<arrow::Array> ConstantArrayGenerator::Float32(int64_t size, float value) {
96101
return ConstantArray<FloatType>(size, value);
97102
}
@@ -148,6 +153,8 @@ std::shared_ptr<arrow::Array> ConstantArrayGenerator::Zeroes(
148153
EXPECT_OK_AND_ASSIGN(auto viewed, Int32(size)->View(type));
149154
return viewed;
150155
}
156+
case Type::HALF_FLOAT:
157+
return Float16(size);
151158
case Type::FLOAT:
152159
return Float32(size);
153160
case Type::DOUBLE:

‎cpp/src/arrow/testing/generator.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ class ARROW_TESTING_EXPORT ConstantArrayGenerator {
106106
/// \return a generated Array
107107
static std::shared_ptr<Array> Int64(int64_t size, int64_t value = 0);
108108

109+
/// \brief Generates a constant Float16Array
110+
///
111+
/// \param[in] size the size of the array to generate
112+
/// \param[in] value to repeat
113+
///
114+
/// \return a generated Array
115+
static std::shared_ptr<Array> Float16(int64_t size, uint16_t value = 0);
116+
109117
/// \brief Generates a constant Float32Array
110118
///
111119
/// \param[in] size the size of the array to generate
@@ -151,6 +159,8 @@ class ARROW_TESTING_EXPORT ConstantArrayGenerator {
151159
return UInt64(size, static_cast<uint64_t>(value));
152160
case Type::INT64:
153161
return Int64(size, static_cast<int64_t>(value));
162+
case Type::HALF_FLOAT:
163+
return Float16(size, static_cast<uint16_t>(value));
154164
case Type::FLOAT:
155165
return Float32(size, static_cast<float>(value));
156166
case Type::DOUBLE:

0 commit comments

Comments
 (0)