Apache DataFusion SQL Query Engine
chore: replace MaybeNullBuilder with trait over arrow NullBuilder (#24606)
Raz Luvaton150ee2d97a50
diff --git a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/boolean.rs b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/boolean.rsindex 5fdbe434f9..22f7fd4904 100644--- a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/boolean.rs+++ b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/boolean.rs@@ -19,8 +19,10 @@ use std::sync::Arc;use crate::aggregates::group_values::multi_group_by::Nulls;use crate::aggregates::group_values::multi_group_by::{GroupColumn, nulls_equal_to};-use crate::aggregates::group_values::null_builder::MaybeNullBufferBuilder;-use arrow::array::{Array as _, ArrayRef, AsArray, BooleanArray, BooleanBufferBuilder};+use crate::aggregates::group_values::null_builder::NullBufferBuilderExt;+use arrow::array::{+ Array as _, ArrayRef, AsArray, BooleanArray, BooleanBufferBuilder, NullBufferBuilder,+};use datafusion_common::Result;/// An implementation of [`GroupColumn`] for booleans@@ -33,7 +35,7 @@ use datafusion_common::Result;#[derive(Debug)]pub struct BooleanGroupValueBuilder<const NULLABLE: bool> {buffer: BooleanBufferBuilder,- nulls: MaybeNullBufferBuilder,+ nulls: NullBufferBuilder,}impl<const NULLABLE: bool> BooleanGroupValueBuilder<NULLABLE> {@@ -41,7 +43,7 @@ impl<const NULLABLE: bool> BooleanGroupValueBuilder<NULLABLE> {pub fn new() -> Self {Self {buffer: BooleanBufferBuilder::new(0),- nulls: MaybeNullBufferBuilder::new(),+ nulls: NullBufferBuilder::empty(),}}}@@ -62,10 +64,10 @@ impl<const NULLABLE: bool> GroupColumn for BooleanGroupValueBuilder<NULLABLE> {fn append_val(&mut self, array: &ArrayRef, row: usize) -> Result<()> {if NULLABLE {if array.is_null(row) {- self.nulls.append(true);+ self.nulls.append_null();self.buffer.append(bool::default());} else {- self.nulls.append(false);+ self.nulls.append_non_null();self.buffer.append(array.as_boolean().value(row));}} else {@@ -125,24 +127,24 @@ impl<const NULLABLE: bool> GroupColumn for BooleanGroupValueBuilder<NULLABLE> {(true, Nulls::Some) => {for &row in rows {if array.is_null(row) {- self.nulls.append(true);+ self.nulls.append_null();self.buffer.append(bool::default());} else {- self.nulls.append(false);+ self.nulls.append_non_null();self.buffer.append(arr.value(row));}}}(true, Nulls::None) => {- self.nulls.append_n(rows.len(), false);+ self.nulls.append_n_non_nulls(rows.len());for &row in rows {self.buffer.append(arr.value(row));}}(true, Nulls::All) => {- self.nulls.append_n(rows.len(), true);+ self.nulls.append_n_nulls(rows.len());self.buffer.append_n(rows.len(), bool::default());}diff --git a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes.rs b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes.rsindex c83b1da404..3b0e9bcb0c 100644--- a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes.rs+++ b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes.rs@@ -18,10 +18,11 @@use crate::aggregates::group_values::multi_group_by::{GroupColumn, Nulls, nulls_equal_to,};-use crate::aggregates::group_values::null_builder::MaybeNullBufferBuilder;+use crate::aggregates::group_values::null_builder::NullBufferBuilderExt;use arrow::array::{Array, ArrayRef, AsArray, BooleanBufferBuilder, BufferBuilder, GenericBinaryArray,- GenericByteArray, GenericStringArray, OffsetSizeTrait, types::GenericStringType,+ GenericByteArray, GenericStringArray, NullBufferBuilder, OffsetSizeTrait,+ types::GenericStringType,};use arrow::buffer::{OffsetBuffer, ScalarBuffer};use arrow::datatypes::{ByteArrayType, DataType, GenericBinaryType};@@ -52,7 +53,7 @@ where/// are stored as a zero length string.offsets: Vec<O>,/// Nulls- nulls: MaybeNullBufferBuilder,+ nulls: NullBufferBuilder,/// The maximum size of the buffer for `0`max_buffer_size: usize,}@@ -66,7 +67,7 @@ whereoutput_type,buffer: BufferBuilder::new(INITIAL_BUFFER_CAPACITY),offsets: vec![O::default()],- nulls: MaybeNullBufferBuilder::new(),+ nulls: NullBufferBuilder::empty(),max_buffer_size: if O::IS_LARGE {i64::MAX as usize} else {@@ -89,12 +90,12 @@ where{let arr = array.as_bytes::<B>();if arr.is_null(row) {- self.nulls.append(true);+ self.nulls.append_null();// nulls need a zero length in the offset bufferlet offset = self.buffer.len();self.offsets.push(O::usize_as(offset));} else {- self.nulls.append(false);+ self.nulls.append_non_null();self.do_append_val_inner(arr, row)?;}@@ -152,14 +153,14 @@ where}Nulls::None => {- self.nulls.append_n(rows.len(), false);+ self.nulls.append_n_non_nulls(rows.len());for &row in rows {self.do_append_val_inner(arr, row)?;}}Nulls::All => {- self.nulls.append_n(rows.len(), true);+ self.nulls.append_n_nulls(rows.len());let new_len = self.offsets.len() + rows.len();let offset = self.buffer.len();diff --git a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes_view.rs b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes_view.rsindex 37cbea2e9d..d1c5c1a7f3 100644--- a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes_view.rs+++ b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/bytes_view.rs@@ -18,9 +18,10 @@use crate::aggregates::group_values::multi_group_by::{GroupColumn, Nulls, nulls_equal_to,};-use crate::aggregates::group_values::null_builder::MaybeNullBufferBuilder;+use crate::aggregates::group_values::null_builder::NullBufferBuilderExt;use arrow::array::{Array, ArrayRef, AsArray, BooleanBufferBuilder, ByteView, GenericByteViewArray,+ NullBufferBuilder,};use arrow::buffer::{Buffer, ScalarBuffer};use arrow::datatypes::ByteViewType;@@ -69,7 +70,7 @@ pub struct ByteViewGroupValueBuilder<B: ByteViewType> {max_block_size: usize,/// Nulls- nulls: MaybeNullBufferBuilder,+ nulls: NullBufferBuilder,/// phantom data so the type requires `<B>`_phantom: PhantomData<B>,@@ -88,7 +89,7 @@ impl<B: ByteViewType> ByteViewGroupValueBuilder<B> {in_progress: Vec::new(),completed: Vec::new(),max_block_size: BYTE_VIEW_MAX_BLOCK_SIZE,- nulls: MaybeNullBufferBuilder::new(),+ nulls: NullBufferBuilder::empty(),_phantom: PhantomData {},}}@@ -110,13 +111,13 @@ impl<B: ByteViewType> ByteViewGroupValueBuilder<B> {// Null row case, set and returnif arr.is_null(row) {- self.nulls.append(true);+ self.nulls.append_null();self.views.push(0);return;}// Not null row case- self.nulls.append(false);+ self.nulls.append_non_null();self.do_append_val_inner(arr, row);}@@ -168,7 +169,7 @@ impl<B: ByteViewType> ByteViewGroupValueBuilder<B> {}Nulls::None => {- self.nulls.append_n(rows.len(), false);+ self.nulls.append_n_non_nulls(rows.len());if arr.data_buffers().is_empty() {// Fast path: all strings are inline (≤12 bytes).// The input array's u128 views are already in the correct format;@@ -191,7 +192,7 @@ impl<B: ByteViewType> ByteViewGroupValueBuilder<B> {}Nulls::All => {- self.nulls.append_n(rows.len(), true);+ self.nulls.append_n_nulls(rows.len());let new_len = self.views.len() + rows.len();self.views.resize(new_len, 0);}diff --git a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/fixed_size_binary.rs b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/fixed_size_binary.rsindex 589083c8f7..8d758cc7c4 100644--- a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/fixed_size_binary.rs+++ b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/fixed_size_binary.rs@@ -18,9 +18,10 @@use crate::aggregates::group_values::multi_group_by::{GroupColumn, Nulls, nulls_equal_to,};-use crate::aggregates::group_values::null_builder::MaybeNullBufferBuilder;+use crate::aggregates::group_values::null_builder::NullBufferBuilderExt;use arrow::array::{Array, ArrayRef, AsArray, BooleanBufferBuilder, FixedSizeBinaryArray,+ NullBufferBuilder,};use arrow::buffer::{Buffer, NullBuffer};use datafusion_common::utils::proxy::VecAllocExt;@@ -50,7 +51,7 @@ pub struct FixedSizeBinaryGroupValueBuilder {/// `byte_width` may be `0`len: usize,/// Null state (null rows still occupy `byte_width` bytes in `buffer`)- nulls: MaybeNullBufferBuilder,+ nulls: NullBufferBuilder,}impl FixedSizeBinaryGroupValueBuilder {@@ -65,18 +66,18 @@ impl FixedSizeBinaryGroupValueBuilder {byte_width: byte_width as usize,buffer: Vec::new(),len: 0,- nulls: MaybeNullBufferBuilder::new(),+ nulls: NullBufferBuilder::empty(),}}fn do_append_val_inner(&mut self, array: &FixedSizeBinaryArray, row: usize) {if array.is_null(row) {- self.nulls.append(true);+ self.nulls.append_null();// Null rows still occupy `byte_width` (zeroed) bytes in the// buffer so the value offset stays a function of the row indexself.buffer.resize(self.buffer.len() + self.byte_width, 0);} else {- self.nulls.append(false);+ self.nulls.append_non_null();self.buffer.extend_from_slice(array.value(row));}self.len += 1;@@ -187,7 +188,7 @@ impl GroupColumn for FixedSizeBinaryGroupValueBuilder {}Nulls::None => {- self.nulls.append_n(rows.len(), false);+ self.nulls.append_n_non_nulls(rows.len());for &row in rows {self.buffer.extend_from_slice(arr.value(row));}@@ -195,7 +196,7 @@ impl GroupColumn for FixedSizeBinaryGroupValueBuilder {}Nulls::All => {- self.nulls.append_n(rows.len(), true);+ self.nulls.append_n_nulls(rows.len());self.buffer.resize(self.buffer.len() + rows.len() * self.byte_width, 0);self.len += rows.len();diff --git a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/primitive.rs b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/primitive.rsindex 148c5697de..b965ccc34f 100644--- a/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/primitive.rs+++ b/datafusion/physical-plan/src/aggregates/group_values/multi_group_by/primitive.rs@@ -19,12 +19,12 @@ use crate::aggregates::group_values::HashValue;use crate::aggregates::group_values::multi_group_by::{GroupColumn, Nulls, nulls_equal_to,};-use crate::aggregates::group_values::null_builder::MaybeNullBufferBuilder;-use arrow::array::ArrowNativeTypeOp;+use crate::aggregates::group_values::null_builder::NullBufferBuilderExt;use arrow::array::{Array, ArrayRef, ArrowPrimitiveType, BooleanBufferBuilder, PrimitiveArray,cast::AsArray,};+use arrow::array::{ArrowNativeTypeOp, NullBufferBuilder};use arrow::buffer::ScalarBuffer;use arrow::datatypes::DataType;use arrow::util::bit_util::apply_bitwise_binary_op;@@ -46,7 +46,7 @@ use std::sync::Arc;pub struct PrimitiveGroupValueBuilder<T: ArrowPrimitiveType, const NULLABLE: bool> {data_type: DataType,group_values: Vec<T::Native>,- nulls: MaybeNullBufferBuilder,+ nulls: NullBufferBuilder,}impl<T, const NULLABLE: bool> PrimitiveGroupValueBuilder<T, NULLABLE>@@ -59,7 +59,7 @@ whereSelf {data_type,group_values: vec![],- nulls: MaybeNullBufferBuilder::new(),+ nulls: NullBufferBuilder::empty(),}}@@ -170,10 +170,10 @@ where// Perf: skip null check if input can't have nullsif NULLABLE {if array.is_null(row) {- self.nulls.append(true);+ self.nulls.append_null();self.group_values.push(T::default_value());} else {- self.nulls.append(false);+ self.nulls.append_non_null();self.group_values.push(array.as_primitive::<T>().value(row).canonicalize());}@@ -221,24 +221,24 @@ where(true, Nulls::Some) => {for &row in rows {if array.is_null(row) {- self.nulls.append(true);+ self.nulls.append_null();self.group_values.push(T::default_value());} else {- self.nulls.append(false);+ self.nulls.append_non_null();self.group_values.push(arr.value(row).canonicalize());}}}(true, Nulls::None) => {- self.nulls.append_n(rows.len(), false);+ self.nulls.append_n_non_nulls(rows.len());for &row in rows {self.group_values.push(arr.value(row).canonicalize());}}(true, Nulls::All) => {- self.nulls.append_n(rows.len(), true);+ self.nulls.append_n_nulls(rows.len());self.group_values.extend(iter::repeat_n(T::default_value(), rows.len()));}diff --git a/datafusion/physical-plan/src/aggregates/group_values/null_builder.rs b/datafusion/physical-plan/src/aggregates/group_values/null_builder.rsindex cfde5e3c55..a89579f274 100644--- a/datafusion/physical-plan/src/aggregates/group_values/null_builder.rs+++ b/datafusion/physical-plan/src/aggregates/group_values/null_builder.rs@@ -18,98 +18,50 @@use arrow::array::NullBufferBuilder;use arrow::buffer::NullBuffer;-/// Builder for an (optional) null mask-///-/// Optimized for avoid creating the bitmask when all values are non-null-#[derive(Debug)]-pub(crate) struct MaybeNullBufferBuilder {- /// Note this is an Arrow *VALIDITY* buffer (so it is false for nulls, true- /// for non-nulls)- nulls: NullBufferBuilder,-}--impl MaybeNullBufferBuilder {- /// Create a new builder- pub fn new() -> Self {- Self {- nulls: NullBufferBuilder::new(0),- }- }+/// Helper methods for NullBufferBuilder that are used in Group By columns+pub(crate) trait NullBufferBuilderExt {+ fn empty() -> Self;/// Return true if the row at index `row` is null- pub fn is_null(&self, row: usize) -> bool {- match self.nulls.as_slice() {- // validity mask means a unset bit is NULL- Some(_) => !self.nulls.is_valid(row),- None => false,- }- }+ fn is_null(&self, row: usize) -> bool;- /// Set the nullness of the next row to `is_null`- ///- /// If `value` is true, the row is null.- /// If `value` is false, the row is non null- pub fn append(&mut self, is_null: bool) {- self.nulls.append(!is_null)- }+ /// Returns a NullBuffer representing the first `n` rows accumulated so far+ /// shifting any remaining down by `n`+ fn take_n(&mut self, n: usize) -> Option<NullBuffer>;- pub fn append_n(&mut self, n: usize, is_null: bool) {- if is_null {- self.nulls.append_n_nulls(n);- } else {- self.nulls.append_n_non_nulls(n);- }- }+ /// Returns true if this builder might have any nulls+ ///+ /// This is guaranteed to be true if there are nulls+ /// but may be true even if there are no nulls+ fn might_have_nulls(&self) -> bool;+}- /// return the number of heap allocated bytes used by this structure to store boolean values- pub fn allocated_size(&self) -> usize {- self.nulls.allocated_size()+impl NullBufferBuilderExt for NullBufferBuilder {+ fn empty() -> Self {+ Self::new(0)}- /// Return a NullBuffer representing the accumulated nulls so far- pub fn build(mut self) -> Option<NullBuffer> {- self.nulls.finish()+ fn is_null(&self, row: usize) -> bool {+ !self.is_valid(row)}- /// Returns a NullBuffer representing the first `n` rows accumulated so far- /// shifting any remaining down by `n`- pub fn take_n(&mut self, n: usize) -> Option<NullBuffer> {+ fn take_n(&mut self, n: usize) -> Option<NullBuffer> {// Copy over the values at n..len-1 values to the start of a// new builder and leave it in self//// TODO: it would be great to use something like `set_bits` from arrow here.- let mut new_builder = NullBufferBuilder::new(self.nulls.len());- for i in n..self.nulls.len() {- new_builder.append(self.nulls.is_valid(i));+ let mut new_builder = NullBufferBuilder::new(self.len());+ for i in n..self.len() {+ new_builder.append(self.is_valid(i));}- std::mem::swap(&mut new_builder, &mut self.nulls);+ std::mem::swap(&mut new_builder, self);// take only first n values from the original buildernew_builder.truncate(n);new_builder.finish()}- /// Returns true if this builder might have any nulls- ///- /// This is guaranteed to be true if there are nulls- /// but may be true even if there are no nulls- pub(crate) fn might_have_nulls(&self) -> bool {- self.nulls.as_slice().is_some()- }-}--#[cfg(test)]-mod tests {- use super::*;- use arrow::array::BooleanBufferBuilder;-- #[test]- fn allocated_size_should_return_bytes() {- let mut expected = BooleanBufferBuilder::new(0);- expected.append(true);- let expected_capacity_in_bytes = expected.capacity() / 8;- let mut builder = MaybeNullBufferBuilder::new();- builder.append(true);- assert_eq!(builder.allocated_size(), expected_capacity_in_bytes);+ fn might_have_nulls(&self) -> bool {+ self.as_slice().is_some()}}