KonaeAkira commented on code in PR #23648: URL: https://github.com/apache/datafusion/pull/23648#discussion_r3601347169
########## datafusion/physical-plan/src/aggregates/group_values/multi_group_by/list.rs: ########## @@ -0,0 +1,866 @@ +// 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 datafusion_execution::memory_pool::proxy::VecAllocExt; +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(array: &ArrayRef) -> &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<()> { + // Guard against usize overflow first (cheap, defense in depth: a + // List<i32> with cumulative length close to i32::MAX could wrap + // before the `O::from_usize` range check below would fire). + let next = self + .current_end() + .as_usize() + .checked_add(additional) + .ok_or_else(|| { + internal_datafusion_err!( + "List offset usize overflow: current={} additional={}", + 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); Review Comment: Nit: why convert `self.current_end()` to `usize` and then convert back to `O` when you can do just one conversion by converting `additional` to `O` and doing the `checked_add` on `O`? Makes the logic a bit easier to follow. ```suggestion let additional = O::from_usize(additional).ok_or_else(|| { internal_datafusion_err!( "List sublist length {additional} overflows the offset type" ) })?; let next = self.current_end().checked_add(&additional).ok_or_else(|| { internal_datafusion_err!( "List offset overflow: current={} additional={}", self.current_end(), additional ) })?; self.offsets.push(next); ``` -- 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]
