Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix NaN values in Spark collect_set aggregate function #12335

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion velox/docs/functions/spark/aggregate.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ General Aggregate Functions
.. spark:function:: collect_set(x) -> array<[same as x]>
Returns an array consisting of all unique values from the input ``x`` elements excluding NULLs.
Returns empty array if input is empty or all NULL.
NaN values are considered distinct. Returns empty array if input is empty or all NULL.

Example::

Expand Down
5 changes: 5 additions & 0 deletions velox/exec/SetAccumulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,4 +523,9 @@ template <typename T>
using SetAccumulator =
typename detail::SetAccumulatorTypeTraits<T>::AccumulatorType;

/// Specialization for floating point types to handle NaNs, where NaNs are
/// treated as distinct values.
template <typename T>
using FloatSetAccumulatorNaNUnaware = typename detail::SetAccumulator<T>;

} // namespace facebook::velox::aggregate::prestosql
14 changes: 12 additions & 2 deletions velox/functions/sparksql/aggregates/CollectSetAggregate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ namespace {
template <typename T>
using SparkSetAggAggregate = SetAggAggregate<T, true, false>;

// NaN inputs are treated as distinct values.
template <typename T>
using FloatSetAggAggregateNaNUnaware = SetAggAggregate<
T,
true,
false,
velox::aggregate::prestosql::FloatSetAccumulatorNaNUnaware<T>>;

} // namespace

void registerCollectSetAggAggregate(
Expand Down Expand Up @@ -72,9 +80,11 @@ void registerCollectSetAggAggregate(
"Non-decimal use of HUGEINT is not supported");
return std::make_unique<SparkSetAggAggregate<int128_t>>(resultType);
case TypeKind::REAL:
return std::make_unique<SparkSetAggAggregate<float>>(resultType);
return std::make_unique<FloatSetAggAggregateNaNUnaware<float>>(
resultType);
case TypeKind::DOUBLE:
return std::make_unique<SparkSetAggAggregate<double>>(resultType);
return std::make_unique<FloatSetAggAggregateNaNUnaware<double>>(
resultType);
case TypeKind::TIMESTAMP:
return std::make_unique<SparkSetAggAggregate<Timestamp>>(
resultType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ TEST_F(CollectSetAggregateTest, global) {
testAggregations(
{data}, {}, {"collect_set(c0)"}, {"spark_array_sort(a0)"}, {expected});

// NaN inputs are treated as distinct values.
data = makeRowVector({
makeFlatVector<double>(
{1,
Expand All @@ -80,7 +81,10 @@ TEST_F(CollectSetAggregateTest, global) {

expected = makeRowVector({
makeArrayVector<double>({
{1, std::numeric_limits<double>::quiet_NaN()},
{1,
std::numeric_limits<double>::quiet_NaN(),
std::nan("1"),
std::nan("2")},
}),
});

Expand Down
Loading