Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions cpp/src/arrow/compute/kernels/base_arithmetic_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -834,11 +834,9 @@ template <>
struct Identity<Max> {
template <typename Value>
static constexpr Value value() {
// Note that `min()` returns the smallest positive value for
// floating-point types, and `lowest()` doesn't satisfy the identity
// property for -inf inputs, so use -infinity for those types.
if constexpr (std::is_floating_point_v<Value> || std::is_same_v<Float16, Value>) {
return -std::numeric_limits<Value>::infinity();
// Max ignores NaN when the other operand is a number.
if constexpr (std::numeric_limits<Value>::has_quiet_NaN) {
return std::numeric_limits<Value>::quiet_NaN();
} else {
return std::numeric_limits<Value>::lowest();
}
Expand All @@ -849,9 +847,9 @@ template <>
struct Identity<Min> {
template <typename Value>
static constexpr Value value() {
// Mirror of Identity<Max>: use +infinity for floating-point types.
if constexpr (std::is_floating_point_v<Value> || std::is_same_v<Float16, Value>) {
return std::numeric_limits<Value>::infinity();
// Min ignores NaN when the other operand is a number.
if constexpr (std::numeric_limits<Value>::has_quiet_NaN) {
return std::numeric_limits<Value>::quiet_NaN();
} else {
return std::numeric_limits<Value>::max();
}
Expand Down
15 changes: 6 additions & 9 deletions cpp/src/arrow/compute/kernels/vector_cumulative_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,26 +241,23 @@ const FunctionDoc cumulative_prod_checked_doc{
const FunctionDoc cumulative_max_doc{
"Compute the cumulative max over a numeric input",
("`values` must be numeric. Return an array/chunked array which is the\n"
"cumulative max computed over `values`. The default start is the minimum\n"
"value of input type (so that any other value will replace the\n"
"start as the new maximum)."),
"cumulative max computed over `values`. NaNs are ignored once a non-NaN\n"
"value is available. Without a start value, leading NaNs produce NaN."),
{"values"},
"CumulativeOptions"};

const FunctionDoc cumulative_min_doc{
"Compute the cumulative min over a numeric input",
("`values` must be numeric. Return an array/chunked array which is the\n"
"cumulative min computed over `values`. The default start is the maximum\n"
"value of input type (so that any other value will replace the\n"
"start as the new minimum)."),
"cumulative min computed over `values`. NaNs are ignored once a non-NaN\n"
"value is available. Without a start value, leading NaNs produce NaN."),
{"values"},
"CumulativeOptions"};

const FunctionDoc cumulative_mean_doc{
"Compute the cumulative mean over a numeric input",
("`values` must be numeric. Return an array/chunked array which is the\n"
"cumulative mean computed over `values`. CumulativeOptions::start_value is \n"
"ignored."),
"cumulative mean computed over `values`. The start option is ignored."),
{"values"},
"CumulativeOptions"};

Expand Down Expand Up @@ -355,7 +352,7 @@ void RegisterVectorCumulativeSum(FunctionRegistry* registry) {
registry, "cumulative_max", cumulative_max_doc);

MakeVectorCumulativeStatefulFunction<CumulativeMean, CumulativeOptions>(
registry, "cumulative_mean", cumulative_max_doc);
registry, "cumulative_mean", cumulative_mean_doc);
}

} // namespace arrow::compute::internal
23 changes: 14 additions & 9 deletions cpp/src/arrow/compute/kernels/vector_cumulative_ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -937,13 +937,19 @@ TEST(TestCumulative, NaN) {
CheckVectorUnary("cumulative_prod", ArrayFromJSON(float64(), "[1, 2, NaN, 4, 5]"),
ArrayFromJSON(float64(), "[1, 2, NaN, NaN, NaN]"));

// max with NaN is always ignored because Nan > a always returns false
CheckVectorUnary("cumulative_max", ArrayFromJSON(float64(), "[1, 2, NaN, 4, 5]"),
ArrayFromJSON(float64(), "[1, 2, 2, 4, 5]"));

// min with NaN is always ignored because Nan < a always returns false
CheckVectorUnary("cumulative_min", ArrayFromJSON(float64(), "[5, 4, NaN, 2, 1]"),
ArrayFromJSON(float64(), "[5, 4, 4, 2, 1]"));
for (const auto& ty : FloatingPointTypes()) {
auto values = ArrayFromJSON(ty, "[NaN, NaN, 2, NaN, 1, 3]");
CheckVectorUnary("cumulative_max", values,
ArrayFromJSON(ty, "[NaN, NaN, 2, 2, 2, 3]"));
CheckVectorUnary("cumulative_min", values,
ArrayFromJSON(ty, "[NaN, NaN, 2, 2, 1, 1]"));

CumulativeOptions start(4.0);
CheckVectorUnary("cumulative_max", values, ArrayFromJSON(ty, "[4, 4, 4, 4, 4, 4]"),
&start);
CheckVectorUnary("cumulative_min", values, ArrayFromJSON(ty, "[4, 4, 2, 2, 1, 1]"),
&start);
}

// mean with NaN is always Nan
CheckVectorUnary("cumulative_mean", ArrayFromJSON(float64(), "[5, 4, NaN, 2, 1]"),
Expand All @@ -970,8 +976,7 @@ TEST(TestCumulative, NegativeValues) {
CheckVectorUnary("cumulative_max", ArrayFromJSON(ty, "[-2.5, -1.5, -3.5, -0.5]"),
ArrayFromJSON(ty, "[-2.5, -1.5, -1.5, -0.5]"), &options);

// The default start must compare lower (higher for min) than every value
// of the type, including infinities
// Infinities are valid extrema.
CheckVectorUnary("cumulative_max", ArrayFromJSON(ty, "[-Inf, -2.5]"),
ArrayFromJSON(ty, "[-Inf, -2.5]"), &options);
CheckVectorUnary("cumulative_min", ArrayFromJSON(ty, "[Inf, 2.5]"),
Expand Down