jayzhan211 commented on code in PR #15356: URL: https://github.com/apache/datafusion/pull/15356#discussion_r2010093546
########## datafusion/functions-aggregate/src/average.rs: ########## @@ -60,6 +64,17 @@ make_udaf_expr_and_func!( avg_udaf ); +pub fn avg_distinct(expr: Expr) -> Expr { Review Comment: `AggregateExprBuilder::new().distinct()` is enough, we don't need this ########## datafusion/functions-aggregate-common/src/aggregate/avg_distinct/decimal.rs: ########## @@ -0,0 +1,133 @@ +// 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 ahash::RandomState; +use arrow::{ + array::{ArrayRef, ArrowNativeTypeOp, PrimitiveArray}, + datatypes::{ArrowNativeType, DecimalType}, +}; +use datafusion_common::{ + cast::{as_list_array, as_primitive_array}, + utils::{memory::estimate_memory_size, SingleRowListArrayBuilder}, + HashSet, ScalarValue, +}; +use datafusion_expr_common::accumulator::Accumulator; +use std::{fmt::Debug, sync::Arc}; + +use crate::utils::{DecimalAverager, Hashable}; + +/// Generic implementation of `AVG DISTINCT` for Decimal types. +/// Handles both Decimal128Type and Decimal256Type. +#[derive(Debug)] +pub struct DecimalDistinctAvgAccumulator<T: DecimalType + Debug> { + values: HashSet<Hashable<T::Native>, RandomState>, + sum_scale: i8, + target_precision: u8, Review Comment: In Sum, we don't specify precision and scale, so we might not need it for avg ########## datafusion/functions-aggregate-common/src/aggregate/avg_distinct/numeric.rs: ########## @@ -0,0 +1,109 @@ +// 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 std::fmt::Debug; +use std::mem::size_of_val; +use std::sync::Arc; + +use ahash::RandomState; +use arrow::array::{ArrayRef, PrimitiveArray}; + +use datafusion_common::cast::{as_list_array, as_primitive_array}; +use datafusion_common::utils::memory::estimate_memory_size; +use datafusion_common::utils::SingleRowListArrayBuilder; +use datafusion_common::HashSet; +use datafusion_common::ScalarValue; +use datafusion_expr_common::accumulator::Accumulator; + +use crate::utils::Hashable; + +/// Specialized implementation of `AVG DISTINCT` for Float64 values, handling the +/// special case for NaN values and floating-point equality. +#[derive(Debug, Default)] +pub struct Float64DistinctAvgAccumulator { Review Comment: Can you checkout `DistinctSumAccumulator`, it supports f64, decimal together in single `DistinctSumAccumulator`, I guess we can unify these types for average too -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org