Skip to content
Merged
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
6 changes: 2 additions & 4 deletions native/spark-expr/src/variance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,8 @@ impl Accumulator for VarianceAccumulator {

Ok(ScalarValue::Float64(match self.count {
count if count == 0.0 => None,
count if count == 1.0 => {
if let StatsType::Population = self.stats_type {
Some(0.0)
} else if self.null_on_divide_by_zero {
count if count == 1.0 && StatsType::Sample == self.stats_type => {
if self.null_on_divide_by_zero {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked Spark StddevPop expression. Its evaluation expression is:

override val evaluateExpression: Expression = {
    If(n === 0.0, Literal.create(null, DoubleType), sqrt(m2 / n))
  }

The special handling is only for Spark StddevSamp (i.e., StatsType::Population in Comet).

None
} else {
Some(f64::NAN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ import org.apache.comet.CometSparkSessionExtensions.isSpark34Plus
class CometAggregateSuite extends CometTestBase with AdaptiveSparkPlanHelper {
import testImplicits._

test("stddev_pop should return NaN for some cases") {
withSQLConf(
CometConf.COMET_EXEC_SHUFFLE_ENABLED.key -> "true",
CometConf.COMET_EXPR_STDDEV_ENABLED.key -> "true") {
Seq(true, false).foreach { nullOnDivideByZero =>
withSQLConf("spark.sql.legacy.statisticalAggregate" -> nullOnDivideByZero.toString) {

val data: Seq[(Float, Int)] = Seq((Float.PositiveInfinity, 1))
withParquetTable(data, "tbl", false) {
val df = sql("SELECT stddev_pop(_1), stddev_pop(_2) FROM tbl")
checkSparkAnswer(df)
}
}
}
}
}

test("count with aggregation filter") {
withSQLConf(
CometConf.COMET_ENABLED.key -> "true",
Expand Down
Loading