You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "yinli-systems (via GitHub)" <gi...@apache.org> on 2026/08/10 11:57:14 UTC
Re: [PR] feat: add native GroupsAccumulator for any_value [datafusion]
yinli-systems commented on code in PR #23065:
URL: https://github.com/apache/datafusion/pull/23065#discussion_r3749238138
##########
datafusion/functions-aggregate/src/any_value.rs:
##########
@@ -0,0 +1,356 @@
+// 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.
+
+//! Defines the ANY_VALUE aggregation.
+
+use std::fmt::Debug;
+use std::hash::Hash;
+use std::mem::size_of_val;
+use std::sync::Arc;
+
+use arrow::array::{Array, ArrayRef, BooleanArray, BooleanBufferBuilder};
+use arrow::buffer::BooleanBuffer;
+use arrow::datatypes::{DataType, Field, FieldRef};
+use datafusion_common::cast::as_boolean_array;
+use datafusion_common::{Result, ScalarValue, not_impl_err};
+use datafusion_expr::function::{AccumulatorArgs, StateFieldsArgs};
+use datafusion_expr::utils::{AggregateOrderSensitivity, format_state_name};
+use datafusion_expr::{
+ Accumulator, AggregateUDFImpl, Documentation, EmitTo, GroupsAccumulator, Signature,
+ Volatility,
+};
+use datafusion_macros::user_doc;
+
+use crate::first_last::TrivialFirstValueAccumulator;
+
+make_udaf_expr_and_func!(
+ AnyValue,
+ any_value,
+ expression,
+ "Returns an arbitrary non-null value",
+ any_value_udaf
+);
+
+#[user_doc(
+ doc_section(label = "General Functions"),
+ description = "Returns an arbitrary non-null value from a group, or NULL if the group contains only NULL values.",
+ syntax_example = "any_value(expression)",
+ sql_example = r#"```sql
+> SELECT any_value(column_name) FROM table_name;
++------------------------+
+| any_value(column_name) |
++------------------------+
+| arbitrary_value |
++------------------------+
+```"#,
+ standard_argument(name = "expression",)
+)]
+#[derive(PartialEq, Eq, Hash, Debug)]
+pub struct AnyValue {
+ signature: Signature,
+}
+
+impl Default for AnyValue {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl AnyValue {
+ pub fn new() -> Self {
+ Self {
+ signature: Signature::any(1, Volatility::Immutable),
+ }
+ }
+}
+
+impl AggregateUDFImpl for AnyValue {
+ fn name(&self) -> &str {
+ "any_value"
+ }
+
+ fn signature(&self) -> &Signature {
+ &self.signature
+ }
+
+ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+ not_impl_err!("Not called because return_field is implemented")
+ }
+
+ fn return_field(&self, arg_fields: &[FieldRef]) -> Result<FieldRef> {
+ Ok(Arc::new(
+ Field::new(self.name(), arg_fields[0].data_type().clone(), true)
+ .with_metadata(arg_fields[0].metadata().clone()),
+ ))
+ }
+
+ fn accumulator(&self, acc_args: AccumulatorArgs) -> Result<Box<dyn Accumulator>> {
+ TrivialFirstValueAccumulator::try_new(acc_args.return_field.data_type(), true)
+ .map(|acc| Box::new(acc) as _)
+ }
+
+ fn state_fields(&self, args: StateFieldsArgs) -> Result<Vec<FieldRef>> {
+ Ok(vec![
+ Field::new(
+ format_state_name(args.name, "any_value"),
+ args.return_type().clone(),
+ true,
+ )
+ .into(),
+ Field::new(
+ format_state_name(args.name, "any_value_is_set"),
+ DataType::Boolean,
+ true,
+ )
+ .into(),
+ ])
+ }
+
+ fn groups_accumulator_supported(&self, _args: AccumulatorArgs) -> bool {
+ true
+ }
+
+ fn create_groups_accumulator(
+ &self,
+ args: AccumulatorArgs,
+ ) -> Result<Box<dyn GroupsAccumulator>> {
+ Ok(Box::new(AnyValueGroupsAccumulator::try_new(
+ args.return_field.data_type(),
+ )?))
+ }
+
+ fn order_sensitivity(&self) -> AggregateOrderSensitivity {
+ AggregateOrderSensitivity::Insensitive
+ }
+
+ fn documentation(&self) -> Option<&Documentation> {
+ self.doc()
+ }
+}
+
+#[derive(Debug)]
+struct AnyValueGroupsAccumulator {
+ values: Vec<ScalarValue>,
Review Comment:
Implemented on current head `1818c9e`. Primitive, decimal, temporal, and timestamp types now use `PrimitiveAnyValueGroupsAccumulator<T>` with native `Vec<T::Native>` state; Utf8/LargeUtf8/Utf8View and Binary/LargeBinary/BinaryView use the allocation-aware `BytesAnyValueGroupsAccumulator`. The `ScalarValue` path remains only as a fallback for other supported types. Focused protocol tests and the native-vs-adapter benchmark are included, and the full current CI is green.
--
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-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org