1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
// 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 crate::builder::ArrayBuilder;
use crate::{ArrayRef, NullArray};
use arrow_data::ArrayData;
use arrow_schema::DataType;
use std::any::Any;
use std::sync::Arc;
/// Builder for [`NullArray`]
///
/// # Example
///
/// Create a `NullArray` from a `NullBuilder`
///
/// ```
///
/// # use arrow_array::{Array, NullArray, builder::NullBuilder};
///
/// let mut b = NullBuilder::new();
/// b.append_empty_value();
/// b.append_null();
/// b.append_nulls(3);
/// b.append_empty_values(3);
/// let arr = b.finish();
///
/// assert_eq!(8, arr.len());
/// assert_eq!(8, arr.null_count());
/// ```
#[derive(Debug)]
pub struct NullBuilder {
len: usize,
}
impl Default for NullBuilder {
fn default() -> Self {
Self::new()
}
}
impl NullBuilder {
/// Creates a new null builder
pub fn new() -> Self {
Self { len: 0 }
}
/// Creates a new null builder with space for `capacity` elements without re-allocating
pub fn with_capacity(capacity: usize) -> Self {
Self { len: capacity }
}
/// Returns the capacity of this builder measured in slots of type `T`
pub fn capacity(&self) -> usize {
self.len
}
/// Appends a null slot into the builder
#[inline]
pub fn append_null(&mut self) {
self.len += 1;
}
/// Appends `n` `null`s into the builder.
#[inline]
pub fn append_nulls(&mut self, n: usize) {
self.len += n;
}
/// Appends a null slot into the builder
#[inline]
pub fn append_empty_value(&mut self) {
self.append_null();
}
/// Appends `n` `null`s into the builder.
#[inline]
pub fn append_empty_values(&mut self, n: usize) {
self.append_nulls(n);
}
/// Builds the [NullArray] and reset this builder.
pub fn finish(&mut self) -> NullArray {
let len = self.len();
let builder = ArrayData::new_null(&DataType::Null, len).into_builder();
let array_data = unsafe { builder.build_unchecked() };
NullArray::from(array_data)
}
/// Builds the [NullArray] without resetting the builder.
pub fn finish_cloned(&self) -> NullArray {
let len = self.len();
let builder = ArrayData::new_null(&DataType::Null, len).into_builder();
let array_data = unsafe { builder.build_unchecked() };
NullArray::from(array_data)
}
}
impl ArrayBuilder for NullBuilder {
/// Returns the builder as a non-mutable `Any` reference.
fn as_any(&self) -> &dyn Any {
self
}
/// Returns the builder as a mutable `Any` reference.
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
/// Returns the boxed builder as a box of `Any`.
fn into_box_any(self: Box<Self>) -> Box<dyn Any> {
self
}
/// Returns the number of array slots in the builder
fn len(&self) -> usize {
self.len
}
/// Returns whether the number of array slots is zero
fn is_empty(&self) -> bool {
self.len() == 0
}
/// Builds the array and reset this builder.
fn finish(&mut self) -> ArrayRef {
Arc::new(self.finish())
}
/// Builds the array without resetting the builder.
fn finish_cloned(&self) -> ArrayRef {
Arc::new(self.finish_cloned())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Array;
#[test]
fn test_null_array_builder() {
let mut builder = NullArray::builder(10);
builder.append_null();
builder.append_nulls(4);
builder.append_empty_value();
builder.append_empty_values(4);
let arr = builder.finish();
assert_eq!(20, arr.len());
assert_eq!(0, arr.offset());
assert_eq!(20, arr.null_count());
}
#[test]
fn test_null_array_builder_finish_cloned() {
let mut builder = NullArray::builder(16);
builder.append_null();
builder.append_empty_value();
builder.append_empty_values(3);
let mut array = builder.finish_cloned();
assert_eq!(21, array.null_count());
builder.append_empty_values(5);
array = builder.finish();
assert_eq!(26, array.null_count());
}
}