coderfender commented on code in PR #2600:
URL: https://github.com/apache/datafusion-comet/pull/2600#discussion_r2520309728
##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -3004,6 +3002,227 @@ class CometExpressionSuite extends CometTestBase with
AdaptiveSparkPlanHelper {
}
}
+ test("ANSI support for sum - null test") {
+ Seq(true, false).foreach { ansiEnabled =>
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> ansiEnabled.toString) {
+ withParquetTable(
+ Seq((null.asInstanceOf[java.lang.Long], "a"),
(null.asInstanceOf[java.lang.Long], "b")),
+ "null_tbl") {
+ val res = sql("SELECT sum(_1) FROM null_tbl")
+ checkSparkAnswerAndOperator(res)
+ assert(res.collect() === Array(Row(null)))
+ }
+ }
+ }
+ }
+
+ test("ANSI support for try_sum - null test") {
+ Seq(true, false).foreach { ansiEnabled =>
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> ansiEnabled.toString) {
+ withParquetTable(
+ Seq((null.asInstanceOf[java.lang.Long], "a"),
(null.asInstanceOf[java.lang.Long], "b")),
+ "null_tbl") {
+ val res = sql("SELECT try_sum(_1) FROM null_tbl")
+ checkSparkAnswerAndOperator(res)
+ assert(res.collect() === Array(Row(null)))
+ }
+ }
+ }
+ }
+
+ test("ANSI support for sum - null test (group by)") {
+ Seq(true, false).foreach { ansiEnabled =>
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> ansiEnabled.toString) {
+ withParquetTable(
+ Seq(
+ (null.asInstanceOf[java.lang.Long], "a"),
+ (null.asInstanceOf[java.lang.Long], "a"),
+ (null.asInstanceOf[java.lang.Long], "b"),
+ (null.asInstanceOf[java.lang.Long], "b"),
+ (null.asInstanceOf[java.lang.Long], "b")),
+ "tbl") {
+ val res = sql("SELECT _2, sum(_1) FROM tbl group by 1")
+ checkSparkAnswerAndOperator(res)
+ assert(res.orderBy(col("_2")).collect() === Array(Row("a", null),
Row("b", null)))
+ }
+ }
+ }
+ }
+
+ test("ANSI support for try_sum - null test (group by)") {
+ Seq(true, false).foreach { ansiEnabled =>
+ withSQLConf(SQLConf.ANSI_ENABLED.key -> ansiEnabled.toString) {
+ withParquetTable(
+ Seq(
+ (null.asInstanceOf[java.lang.Long], "a"),
+ (null.asInstanceOf[java.lang.Long], "a"),
+ (null.asInstanceOf[java.lang.Long], "b"),
+ (null.asInstanceOf[java.lang.Long], "b"),
+ (null.asInstanceOf[java.lang.Long], "b")),
+ "tbl") {
+ val res = sql("SELECT _2, sum(_1) FROM tbl group by 1")
Review Comment:
Thank you . I fixed the test
##########
native/spark-expr/src/agg_funcs/sum_int.rs:
##########
@@ -0,0 +1,573 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::{arithmetic_overflow_error, EvalMode};
+use arrow::array::{
+ cast::AsArray, Array, ArrayRef, ArrowNativeTypeOp, ArrowPrimitiveType,
BooleanArray,
+ Int64Array, PrimitiveArray,
+};
+use arrow::datatypes::{
+ ArrowNativeType, DataType, Field, FieldRef, Int16Type, Int32Type,
Int64Type, Int8Type,
+};
+use datafusion::common::{DataFusionError, Result as DFResult, ScalarValue};
+use datafusion::logical_expr::function::{AccumulatorArgs, StateFieldsArgs};
+use datafusion::logical_expr::Volatility::Immutable;
+use datafusion::logical_expr::{
+ Accumulator, AggregateUDFImpl, EmitTo, GroupsAccumulator, ReversedUDAF,
Signature,
+};
+use std::{any::Any, sync::Arc};
+
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SumInteger {
+ signature: Signature,
+ eval_mode: EvalMode,
+}
+
+impl SumInteger {
+ pub fn try_new(data_type: DataType, eval_mode: EvalMode) -> DFResult<Self>
{
+ match data_type {
+ DataType::Int8 | DataType::Int16 | DataType::Int32 |
DataType::Int64 => Ok(Self {
+ signature: Signature::user_defined(Immutable),
+ eval_mode,
+ }),
+ _ => Err(DataFusionError::Internal(
+ "Invalid data type for SumInteger".into(),
+ )),
+ }
+ }
+}
+
+impl AggregateUDFImpl for SumInteger {
+ fn as_any(&self) -> &dyn Any {
+ self
+ }
+
+ fn name(&self) -> &str {
+ "sum"
+ }
+
+ fn signature(&self) -> &Signature {
+ &self.signature
+ }
+
+ fn return_type(&self, _arg_types: &[DataType]) -> DFResult<DataType> {
+ Ok(DataType::Int64)
+ }
+
+ fn accumulator(&self, _acc_args: AccumulatorArgs) -> DFResult<Box<dyn
Accumulator>> {
+ Ok(Box::new(SumIntegerAccumulator::new(self.eval_mode)))
+ }
+
+ fn state_fields(&self, _args: StateFieldsArgs) -> DFResult<Vec<FieldRef>> {
+ if self.eval_mode == EvalMode::Try {
+ Ok(vec![
+ Arc::new(Field::new("sum", DataType::Int64, true)),
+ Arc::new(Field::new("has_all_nulls", DataType::Boolean,
false)),
+ ])
+ } else {
+ Ok(vec![Arc::new(Field::new("sum", DataType::Int64, true))])
+ }
+ }
+
+ fn groups_accumulator_supported(&self, _args: AccumulatorArgs) -> bool {
+ true
+ }
+
+ fn create_groups_accumulator(
+ &self,
+ _args: AccumulatorArgs,
+ ) -> DFResult<Box<dyn GroupsAccumulator>> {
+ Ok(Box::new(SumIntGroupsAccumulator::new(self.eval_mode)))
+ }
+
+ fn reverse_expr(&self) -> ReversedUDAF {
+ ReversedUDAF::Identical
+ }
+}
+
+#[derive(Debug)]
+struct SumIntegerAccumulator {
+ sum: Option<i64>,
+ eval_mode: EvalMode,
+ has_all_nulls: bool,
+}
+
+impl SumIntegerAccumulator {
+ fn new(eval_mode: EvalMode) -> Self {
+ if eval_mode == EvalMode::Try {
+ Self {
+ // Try mode starts with 0 (because if this is init to None we
cant say if it is none due to all nulls or due to an overflow
Review Comment:
Thank you
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]