Copilot commented on code in PR #23128:
URL: https://github.com/apache/datafusion/pull/23128#discussion_r3464186291


##########
datafusion/physical-plan/src/aggregates/group_values/multi_group_by/mod.rs:
##########
@@ -1067,6 +1087,40 @@ fn make_group_column(field: &Field) -> Result<Box<dyn 
GroupColumn>> {
                 v.push(Box::new(BooleanGroupValueBuilder::<false>::new()));
             }
         }
+        DataType::FixedSizeList(ref child_field, _) => {
+            // `group_column_supported_type` already restricts the child to
+            // the primitive subset supported here. Any unsupported child
+            // type returned `false` upstream and was routed to the
+            // `GroupValuesRows` fallback, so the wildcard arm below is
+            // only reachable from the consistency-fuzz test.
+            macro_rules! instantiate_fsl {
+                ($t:ty) => {{
+                    let b = 
fixed_size_list::FixedSizeListGroupValueBuilder::<$t>::new(
+                        data_type,
+                    );
+                    v.push(Box::new(b) as _);
+                }};
+            }

Review Comment:
   This dispatcher currently accepts `DataType::FixedSizeList(_, n)` for any 
`n`, but the builder converts `n` to `usize`. If `n` is negative (invalid but 
constructible programmatically), that conversion will wrap and can cause 
panics/OOM. Reject negative list sizes explicitly here so `try_new` fails 
safely instead of building a broken column.



##########
datafusion/physical-plan/src/aggregates/group_values/multi_group_by/fixed_size_list.rs:
##########
@@ -0,0 +1,640 @@
+// 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.
+
+//! [`GroupColumn`] implementation for `FixedSizeList<primitive>`.
+//!
+//! Storage: outer null bitmap + a child [`PrimitiveGroupValueBuilder`] that
+//! holds every element flat (length = outer_len * list_len). The j-th element
+//! of the i-th outer row lives at child index `i * list_len + j`.
+
+use crate::aggregates::group_values::HashValue;
+use 
crate::aggregates::group_values::multi_group_by::primitive::PrimitiveGroupValueBuilder;
+use crate::aggregates::group_values::multi_group_by::{GroupColumn, 
nulls_equal_to};
+use crate::aggregates::group_values::null_builder::MaybeNullBufferBuilder;
+
+use arrow::array::{
+    Array, ArrayRef, ArrowPrimitiveType, BooleanBufferBuilder, 
FixedSizeListArray,
+};
+use arrow::datatypes::{DataType, FieldRef};
+use datafusion_common::Result;
+use std::sync::Arc;
+
+/// A [`GroupColumn`] for `FixedSizeList<T>` where `T` is a primitive type.
+pub struct FixedSizeListGroupValueBuilder<T: ArrowPrimitiveType> {
+    /// Child field, cached for `build` / `take_n`.
+    field: FieldRef,
+    /// List length per outer row.
+    list_len: i32,
+    /// Outer-level null bitmap.
+    outer_nulls: MaybeNullBufferBuilder,
+    /// Number of outer rows accumulated.
+    outer_len: usize,
+    /// Flat storage for child elements; always treated as nullable so it can
+    /// hold child nulls regardless of the outer row's nullability.
+    child: PrimitiveGroupValueBuilder<T, true>,
+}
+
+impl<T> FixedSizeListGroupValueBuilder<T>
+where
+    T: ArrowPrimitiveType,
+    T::Native: HashValue,
+{
+    pub fn new(data_type: &DataType) -> Self {
+        let (field, list_len) = match data_type {
+            DataType::FixedSizeList(f, n) => (Arc::clone(f), *n),
+            other => unreachable!(
+                "FixedSizeListGroupValueBuilder built with non-FixedSizeList 
type {other:?}"
+            ),
+        };
+        let child = PrimitiveGroupValueBuilder::<T, 
true>::new(field.data_type().clone());
+        Self {
+            field,
+            list_len,
+            outer_nulls: MaybeNullBufferBuilder::new(),
+            outer_len: 0,
+            child,
+        }
+    }
+
+    fn list_len_usize(&self) -> usize {
+        self.list_len as usize
+    }
+}
+
+impl<T> GroupColumn for FixedSizeListGroupValueBuilder<T>
+where
+    T: ArrowPrimitiveType,
+    T::Native: HashValue,
+{
+    fn equal_to(&self, lhs_row: usize, array: &ArrayRef, rhs_row: usize) -> 
bool {
+        let lhs_null = self.outer_nulls.is_null(lhs_row);
+        let rhs_null = array.is_null(rhs_row);
+        if let Some(result) = nulls_equal_to(lhs_null, rhs_null) {
+            return result;
+        }
+
+        let list_array = array
+            .as_any()
+            .downcast_ref::<FixedSizeListArray>()
+            .expect("FixedSizeListGroupValueBuilder called with 
non-FixedSizeList array");
+        let rhs_sublist: ArrayRef = list_array.value(rhs_row);
+        let list_len = self.list_len_usize();
+        let lhs_base = lhs_row * list_len;
+        for j in 0..list_len {
+            if !self.child.equal_to(lhs_base + j, &rhs_sublist, j) {
+                return false;
+            }
+        }
+        true
+    }

Review Comment:
   `equal_to` currently calls `list_array.value(rhs_row)`, which constructs a 
sliced child array for every comparison. This is on the hot path for grouping 
equality checks and can introduce avoidable per-compare overhead. You can 
compare directly against `list_array.values()` using `value_offset` (as 
`append_val` already does).



##########
datafusion/physical-plan/src/aggregates/group_values/multi_group_by/mod.rs:
##########
@@ -923,6 +924,25 @@ macro_rules! instantiate_primitive {
 /// builder for. The `group_column_supported_type_matches_make_group_column`
 /// test below pins this biconditional.
 fn group_column_supported_type(data_type: &DataType) -> bool {
+    // FixedSizeList<primitive> is the only nested type currently supported.
+    // List / LargeList / Struct will follow in subsequent PRs of #22715.
+    if let DataType::FixedSizeList(child_field, _) = data_type {
+        return matches!(
+            child_field.data_type(),
+            DataType::Int8
+                | DataType::Int16
+                | DataType::Int32
+                | DataType::Int64
+                | DataType::UInt8
+                | DataType::UInt16
+                | DataType::UInt32
+                | DataType::UInt64
+                | DataType::Float32
+                | DataType::Float64
+                | DataType::Date32
+                | DataType::Date64
+        );
+    }

Review Comment:
   `FixedSizeList` list length isn’t validated here. Because 
`FixedSizeListGroupValueBuilder` later casts the i32 length to `usize`, a 
negative length could wrap to a huge value and lead to panics/OOM in grouping. 
Since Arrow considers negative sizes invalid, it’s safer to reject them in the 
allow-list to avoid routing an invalid schema into `GroupValuesColumn`.



##########
datafusion/physical-plan/src/aggregates/group_values/multi_group_by/fixed_size_list.rs:
##########
@@ -0,0 +1,640 @@
+// 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.
+
+//! [`GroupColumn`] implementation for `FixedSizeList<primitive>`.
+//!
+//! Storage: outer null bitmap + a child [`PrimitiveGroupValueBuilder`] that
+//! holds every element flat (length = outer_len * list_len). The j-th element
+//! of the i-th outer row lives at child index `i * list_len + j`.
+
+use crate::aggregates::group_values::HashValue;
+use 
crate::aggregates::group_values::multi_group_by::primitive::PrimitiveGroupValueBuilder;
+use crate::aggregates::group_values::multi_group_by::{GroupColumn, 
nulls_equal_to};
+use crate::aggregates::group_values::null_builder::MaybeNullBufferBuilder;
+
+use arrow::array::{
+    Array, ArrayRef, ArrowPrimitiveType, BooleanBufferBuilder, 
FixedSizeListArray,
+};
+use arrow::datatypes::{DataType, FieldRef};
+use datafusion_common::Result;
+use std::sync::Arc;
+
+/// A [`GroupColumn`] for `FixedSizeList<T>` where `T` is a primitive type.
+pub struct FixedSizeListGroupValueBuilder<T: ArrowPrimitiveType> {
+    /// Child field, cached for `build` / `take_n`.
+    field: FieldRef,
+    /// List length per outer row.
+    list_len: i32,
+    /// Outer-level null bitmap.
+    outer_nulls: MaybeNullBufferBuilder,
+    /// Number of outer rows accumulated.
+    outer_len: usize,
+    /// Flat storage for child elements; always treated as nullable so it can
+    /// hold child nulls regardless of the outer row's nullability.
+    child: PrimitiveGroupValueBuilder<T, true>,
+}
+
+impl<T> FixedSizeListGroupValueBuilder<T>
+where
+    T: ArrowPrimitiveType,
+    T::Native: HashValue,
+{
+    pub fn new(data_type: &DataType) -> Self {
+        let (field, list_len) = match data_type {
+            DataType::FixedSizeList(f, n) => (Arc::clone(f), *n),
+            other => unreachable!(
+                "FixedSizeListGroupValueBuilder built with non-FixedSizeList 
type {other:?}"
+            ),
+        };
+        let child = PrimitiveGroupValueBuilder::<T, 
true>::new(field.data_type().clone());
+        Self {
+            field,
+            list_len,
+            outer_nulls: MaybeNullBufferBuilder::new(),
+            outer_len: 0,
+            child,
+        }
+    }
+
+    fn list_len_usize(&self) -> usize {
+        self.list_len as usize
+    }

Review Comment:
   `list_len_usize` uses `as usize` on an `i32`. If the list size is ever 
negative (invalid Arrow but still constructible), this will wrap to a huge 
`usize` and can lead to panics/OOM in loops and indexing. Converting with 
`try_from` avoids silent wrap-around and fails fast with a clear message.



-- 
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]

Reply via email to