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: 6 additions & 0 deletions native/spark-expr/src/agg_funcs/avg_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ impl AggregateUDFImpl for AvgDecimal {
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
avg_return_type(self.name(), &arg_types[0])
}

fn is_nullable(&self) -> bool {
Comment thread
vaibhawvipul marked this conversation as resolved.
// In Spark, Sum.nullable and Average.nullable both return true irrespective of ANSI mode.
// AvgDecimal is always nullable because overflows can cause null values.
true
}
}

/// An accumulator to compute the average for decimals
Expand Down
3 changes: 2 additions & 1 deletion native/spark-expr/src/agg_funcs/sum_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ impl AggregateUDFImpl for SumDecimal {
}

fn is_nullable(&self) -> bool {
// SumDecimal is always nullable because overflows can cause null values
// In Spark, Sum.nullable and Average.nullable both return true irrespective of ANSI mode.
// SumDecimal is always nullable because overflows can cause null values.
true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1917,6 +1917,45 @@ class CometAggregateSuite extends CometTestBase with AdaptiveSparkPlanHelper {
}
}

test("SumDecimal and AvgDecimal nullable should always be true") {
// SumDecimal and AvgDecimal currently hardcode nullable=true.
// This matches Spark's Sum.nullable and Average.nullable which always return true,
// regardless of ANSI mode or input nullability.
val nonNullableData: Seq[(java.math.BigDecimal, Int)] = Seq(
(new java.math.BigDecimal("10.00"), 1),
(new java.math.BigDecimal("20.00"), 1),
(new java.math.BigDecimal("30.00"), 2))

val nullableData: Seq[(java.math.BigDecimal, Int)] = Seq(
(new java.math.BigDecimal("10.00"), 1),
(null.asInstanceOf[java.math.BigDecimal], 1),
(new java.math.BigDecimal("30.00"), 2))

Seq(true, false).foreach { ansiEnabled =>
withSQLConf(SQLConf.ANSI_ENABLED.key -> ansiEnabled.toString) {
withParquetTable(nonNullableData, "tbl") {
val sumRes = sql("SELECT _2, sum(_1) FROM tbl GROUP BY _2")
checkSparkAnswerAndOperator(sumRes)
assert(sumRes.schema.fields(1).nullable == true)

val avgRes = sql("SELECT _2, avg(_1) FROM tbl GROUP BY _2")
checkSparkAnswerAndOperator(avgRes)
assert(avgRes.schema.fields(1).nullable == true)
}

withParquetTable(nullableData, "tbl") {
val sumRes = sql("SELECT _2, sum(_1) FROM tbl GROUP BY _2")
checkSparkAnswerAndOperator(sumRes)
assert(sumRes.schema.fields(1).nullable == true)

val avgRes = sql("SELECT _2, avg(_1) FROM tbl GROUP BY _2")
checkSparkAnswerAndOperator(avgRes)
assert(avgRes.schema.fields(1).nullable == true)
}
Comment thread
vaibhawvipul marked this conversation as resolved.
}
}
}

protected def checkSparkAnswerAndNumOfAggregates(query: String, numAggregates: Int): Unit = {
val df = sql(query)
checkSparkAnswer(df)
Expand Down
Loading