Skip to content

Commit 48b787c

Browse files
committed
Fix the issue that MinMax kernel emits inf for all NaN array
1 parent 79d6458 commit 48b787c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

cpp/src/arrow/compute/kernels/aggregate_basic.inc.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,8 @@ struct MinMaxState<ArrowType, SimdLevel, enable_if_floating_point<ArrowType>> {
694694
this->max = std::fmax(this->max, value);
695695
}
696696

697-
T min = std::numeric_limits<T>::infinity();
698-
T max = -std::numeric_limits<T>::infinity();
697+
T min = std::numeric_limits<T>::quiet_NaN();
698+
T max = std::numeric_limits<T>::quiet_NaN();
699699
bool has_nulls = false;
700700
};
701701

cpp/src/arrow/compute/kernels/aggregate_test.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,24 @@ class TestPrimitiveMinMaxKernel : public ::testing::Test {
18411841
AssertMinMaxIsNull(array, options);
18421842
}
18431843

1844+
void AssertMinMaxIsNaN(const Datum& array, const ScalarAggregateOptions& options) {
1845+
ASSERT_OK_AND_ASSIGN(Datum out, MinMax(array, options));
1846+
for (const auto& val : out.scalar_as<StructScalar>().value) {
1847+
ASSERT_TRUE(std::isnan(checked_cast<const ScalarType&>(*val).value));
1848+
}
1849+
}
1850+
1851+
void AssertMinMaxIsNaN(const std::string& json, const ScalarAggregateOptions& options) {
1852+
auto array = ArrayFromJSON(type_singleton(), json);
1853+
AssertMinMaxIsNaN(array, options);
1854+
}
1855+
1856+
void AssertMinMaxIsNaN(const std::vector<std::string>& json,
1857+
const ScalarAggregateOptions& options) {
1858+
auto array = ChunkedArrayFromJSON(type_singleton(), json);
1859+
AssertMinMaxIsNaN(array, options);
1860+
}
1861+
18441862
std::shared_ptr<DataType> type_singleton() {
18451863
return default_type_instance<ArrowType>();
18461864
}
@@ -1963,6 +1981,9 @@ TYPED_TEST(TestFloatingMinMaxKernel, Floats) {
19631981
this->AssertMinMaxIs("[5, Inf, 2, 3, 4]", 2.0, INFINITY, options);
19641982
this->AssertMinMaxIs("[5, NaN, 2, 3, 4]", 2, 5, options);
19651983
this->AssertMinMaxIs("[5, -Inf, 2, 3, 4]", -INFINITY, 5, options);
1984+
this->AssertMinMaxIs("[NaN, null, 42]", 42, 42, options);
1985+
this->AssertMinMaxIsNaN("[NaN, NaN]", options);
1986+
this->AssertMinMaxIsNaN("[NaN, null]", options);
19661987
this->AssertMinMaxIs(chunked_input1, 1, 9, options);
19671988
this->AssertMinMaxIs(chunked_input2, 1, 9, options);
19681989
this->AssertMinMaxIs(chunked_input3, 1, 9, options);
@@ -1980,6 +2001,7 @@ TYPED_TEST(TestFloatingMinMaxKernel, Floats) {
19802001
this->AssertMinMaxIs("[5, -Inf, 2, 3, 4]", -INFINITY, 5, options);
19812002
this->AssertMinMaxIsNull("[5, null, 2, 3, 4]", options);
19822003
this->AssertMinMaxIsNull("[5, -Inf, null, 3, 4]", options);
2004+
this->AssertMinMaxIsNull("[NaN, null]", options);
19832005
this->AssertMinMaxIsNull(chunked_input1, options);
19842006
this->AssertMinMaxIsNull(chunked_input2, options);
19852007
this->AssertMinMaxIsNull(chunked_input3, options);

0 commit comments

Comments
 (0)