Rich-T-kid commented on code in PR #11122: URL: https://github.com/apache/arrow-rs/pull/11122#discussion_r4042892010
########## arrow-select/src/coalesce/fixed_size_binary.rs: ########## @@ -0,0 +1,175 @@ +// 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 super::InProgressArray; +use crate::filter::FilterPredicate; +use arrow_array::builder::{ArrayBuilder, FixedSizeBinaryBuilder}; +use arrow_array::cast::AsArray; +use arrow_array::{Array, ArrayRef}; +use arrow_schema::ArrowError; +use std::sync::Arc; + +/// Specialized [`InProgressArray`] for `FixedSizeBinary` columns. +#[derive(Debug)] +pub(crate) struct InProgressFixedSizeBinaryArray { + source: Option<ArrayRef>, + value_length: i32, + batch_size: usize, + builder: Option<FixedSizeBinaryBuilder>, +} + +impl InProgressFixedSizeBinaryArray { + pub(crate) fn new(value_length: i32, batch_size: usize) -> Self { + Self { + source: None, + value_length, + batch_size, + builder: None, + } + } + + fn ensure_builder(&mut self) -> &mut FixedSizeBinaryBuilder { + self.builder.get_or_insert_with(|| { + FixedSizeBinaryBuilder::with_capacity(self.batch_size, self.value_length) + }) + } +} + +impl InProgressArray for InProgressFixedSizeBinaryArray { + fn set_source(&mut self, source: Option<ArrayRef>) { + self.source = source; + } + + fn copy_rows(&mut self, offset: usize, len: usize) -> Result<(), ArrowError> { + let source = self.source.as_ref().ok_or_else(|| { + ArrowError::InvalidArgumentError( + "Internal Error: InProgressFixedSizeBinaryArray: source not set".to_string(), + ) + })?; + let sliced = source.as_fixed_size_binary().slice(offset, len); + self.ensure_builder().append_array(&sliced)?; + Ok(()) + } + + fn copy_rows_by_filter_from( + &mut self, + source: ArrayRef, + filter: &FilterPredicate, + ) -> Result<(), ArrowError> { + let filtered = filter.filter(source.as_ref())?; + if !filtered.is_empty() { + self.ensure_builder() + .append_array(filtered.as_fixed_size_binary())?; + } + Ok(()) + } + + fn finish(&mut self) -> Result<ArrayRef, ArrowError> { + let mut b = self + .builder + .take() + .unwrap_or_else(|| FixedSizeBinaryBuilder::new(self.value_length)); + Ok(Arc::new(b.finish())) + } + + fn size(&self) -> usize { + let rows = self.builder.as_ref().map_or(0, |b| b.len()); + let values_bytes = rows * self.value_length as usize; + let null_bitmap_bytes = rows.div_ceil(8); + values_bytes Review Comment: in retrospect this probably could have just been included in this PR -- 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]
