Copilot commented on code in PR #22706: URL: https://github.com/apache/datafusion/pull/22706#discussion_r3338974884
########## datafusion/physical-plan/src/aggregates/group_values/multi_group_by/fixed_size_list.rs: ########## @@ -0,0 +1,632 @@ +// 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::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: ArrowPrimitiveType> FixedSizeListGroupValueBuilder<T> { + 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: ArrowPrimitiveType> GroupColumn for FixedSizeListGroupValueBuilder<T> { + 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 + } + + fn append_val(&mut self, array: &ArrayRef, row: usize) -> Result<()> { + let list_array = array + .as_any() + .downcast_ref::<FixedSizeListArray>() + .expect("FixedSizeListGroupValueBuilder called with non-FixedSizeList array"); + self.outer_nulls.append(list_array.is_null(row)); + self.outer_len += 1; + let child_array = list_array.values(); + let list_len = self.list_len_usize(); + // FixedSizeList layout: outer row `row` owns child indices + // [row * list_len .. (row + 1) * list_len). The Arrow logical offset + // is already folded into `array.value(row)`; we use it indirectly via + // `values()` which is the unsliced child array, so include the outer + // array's offset here. + let start = (list_array.offset() + row) * list_len; + for j in 0..list_len { + self.child.append_val(child_array, start + j)?; + } + Ok(()) Review Comment: `FixedSizeListArray::values()` may already be sliced to the logical region when the outer array is sliced. Computing `start` as `(list_array.offset() + row) * list_len` risks double-applying the offset and reading the wrong child values (or going out of bounds) for sliced inputs. Use `FixedSizeListArray::value_offset(row)` to compute the correct starting index. ########## datafusion/physical-plan/src/aggregates/group_values/multi_group_by/list.rs: ########## @@ -0,0 +1,827 @@ +// 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 `List<T>` and `LargeList<T>` group keys. +//! +//! Storage: +//! - `offsets: Vec<O>` of length `outer_len + 1`, with `offsets[0] = 0`. The +//! children of outer row `i` live at child positions +//! `[offsets[i] .. offsets[i+1])`. +//! - `child: Box<dyn GroupColumn>` for the element type. +//! - `outer_nulls`: outer-row null bitmap. +//! +//! Null outer rows are stored with a zero-length range (i.e. +//! `offsets[i+1] == offsets[i]`). No child slots are pushed for null rows. + +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, BooleanBufferBuilder, GenericListArray, OffsetSizeTrait, +}; +use arrow::buffer::{OffsetBuffer, ScalarBuffer}; +use arrow::datatypes::FieldRef; +use datafusion_common::{Result, internal_datafusion_err}; +use std::mem::size_of_val; +use std::sync::Arc; + +/// A [`GroupColumn`] for `List<T>` (`O = i32`) and `LargeList<T>` (`O = i64`). +pub struct ListGroupValueBuilder<O: OffsetSizeTrait> { + field: FieldRef, + offsets: Vec<O>, + child: Box<dyn GroupColumn>, + outer_nulls: MaybeNullBufferBuilder, + outer_len: usize, +} + +impl<O: OffsetSizeTrait> ListGroupValueBuilder<O> { + pub fn new(field: FieldRef, child: Box<dyn GroupColumn>) -> Self { + Self { + field, + offsets: vec![O::usize_as(0)], + child, + outer_nulls: MaybeNullBufferBuilder::new(), + outer_len: 0, + } + } + + fn list_array<'a>(array: &'a ArrayRef) -> &'a GenericListArray<O> { + array + .as_any() + .downcast_ref::<GenericListArray<O>>() + .expect("ListGroupValueBuilder called with non-List/LargeList array") + } + + fn current_end(&self) -> O { + *self.offsets.last().expect("offsets is never empty") + } + + fn push_offset(&mut self, additional: usize) -> Result<()> { + let next = self.current_end().as_usize() + additional; + let next_o = O::from_usize(next) + .ok_or_else(|| internal_datafusion_err!("List offset overflows {}", next))?; + self.offsets.push(next_o); + Ok(()) + } +} Review Comment: `push_offset` does `as_usize() + additional` without overflow checking. On very large inputs this can wrap `usize` before the `O::from_usize` check runs, leading to incorrect offsets and potential memory safety issues later when building arrays. Use `checked_add` to detect overflow before converting to `O`. ########## datafusion/physical-plan/src/aggregates/group_values/multi_group_by/mod.rs: ########## @@ -1272,7 +1301,259 @@ mod tests { GroupValues, multi_group_by::GroupValuesColumn, }; - use super::GroupIndexView; + use super::{GroupIndexView, supported_schema, supported_type}; + + #[test] + fn supported_type_accepts_supported_primitives_and_strings() { + assert!(supported_type(&DataType::Int32)); + assert!(supported_type(&DataType::Int64)); + assert!(supported_type(&DataType::Float64)); + assert!(supported_type(&DataType::Boolean)); + assert!(supported_type(&DataType::Decimal128(38, 10))); + assert!(supported_type(&DataType::Utf8)); + assert!(supported_type(&DataType::LargeUtf8)); + assert!(supported_type(&DataType::Utf8View)); + } + + #[test] + fn supported_type_rejects_unsupported_primitives() { + // Float16 and Decimal256 are not in the supported set. + assert!(!supported_type(&DataType::Float16)); + assert!(!supported_type(&DataType::Decimal256(76, 10))); + // Time64(Second) and Time64(Millisecond) variants are not supported + // (only the more precise Microsecond/Nanosecond are). Review Comment: The comment says only `Time64(Microsecond/Nanosecond)` are supported, but `supported_type` currently rejects *all* `Time64` variants (it doesn’t match `DataType::Time64(_)` at all). This is misleading in tests and may hide a real dispatcher/allow-list mismatch. -- 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]
