Skip to main content

arrow_array/builder/
buffer_builder.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5pub use arrow_buffer::BufferBuilder;
6pub use arrow_buffer::OffsetBufferBuilder;
7
8use half::f16;
9
10use crate::types::*;
11
12/// Buffer builder for signed 8-bit integer type.
13pub type Int8BufferBuilder = BufferBuilder<i8>;
14/// Buffer builder for signed 16-bit integer type.
15pub type Int16BufferBuilder = BufferBuilder<i16>;
16/// Buffer builder for signed 32-bit integer type.
17pub type Int32BufferBuilder = BufferBuilder<i32>;
18/// Buffer builder for signed 64-bit integer type.
19pub type Int64BufferBuilder = BufferBuilder<i64>;
20/// Buffer builder for unsigned 8-bit integer type.
21pub type UInt8BufferBuilder = BufferBuilder<u8>;
22/// Buffer builder for unsigned 16-bit integer type.
23pub type UInt16BufferBuilder = BufferBuilder<u16>;
24/// Buffer builder for unsigned 32-bit integer type.
25pub type UInt32BufferBuilder = BufferBuilder<u32>;
26/// Buffer builder for unsigned 64-bit integer type.
27pub type UInt64BufferBuilder = BufferBuilder<u64>;
28/// Buffer builder for 16-bit floating point type.
29pub type Float16BufferBuilder = BufferBuilder<f16>;
30/// Buffer builder for 32-bit floating point type.
31pub type Float32BufferBuilder = BufferBuilder<f32>;
32/// Buffer builder for 64-bit floating point type.
33pub type Float64BufferBuilder = BufferBuilder<f64>;
34
35/// Buffer builder for 32-bit decimal type.
36pub type Decimal32BufferBuilder = BufferBuilder<<Decimal32Type as ArrowPrimitiveType>::Native>;
37/// Buffer builder for 64-bit decimal type.
38pub type Decimal64BufferBuilder = BufferBuilder<<Decimal64Type as ArrowPrimitiveType>::Native>;
39/// Buffer builder for 128-bit decimal type.
40pub type Decimal128BufferBuilder = BufferBuilder<<Decimal128Type as ArrowPrimitiveType>::Native>;
41/// Buffer builder for 256-bit decimal type.
42pub type Decimal256BufferBuilder = BufferBuilder<<Decimal256Type as ArrowPrimitiveType>::Native>;
43
44/// Buffer builder for timestamp type of second unit.
45pub type TimestampSecondBufferBuilder =
46    BufferBuilder<<TimestampSecondType as ArrowPrimitiveType>::Native>;
47/// Buffer builder for timestamp type of millisecond unit.
48pub type TimestampMillisecondBufferBuilder =
49    BufferBuilder<<TimestampMillisecondType as ArrowPrimitiveType>::Native>;
50/// Buffer builder for timestamp type of microsecond unit.
51pub type TimestampMicrosecondBufferBuilder =
52    BufferBuilder<<TimestampMicrosecondType as ArrowPrimitiveType>::Native>;
53/// Buffer builder for timestamp type of nanosecond unit.
54pub type TimestampNanosecondBufferBuilder =
55    BufferBuilder<<TimestampNanosecondType as ArrowPrimitiveType>::Native>;
56
57/// Buffer builder for 32-bit date type.
58pub type Date32BufferBuilder = BufferBuilder<<Date32Type as ArrowPrimitiveType>::Native>;
59/// Buffer builder for 64-bit date type.
60pub type Date64BufferBuilder = BufferBuilder<<Date64Type as ArrowPrimitiveType>::Native>;
61
62/// Buffer builder for 32-bit elapsed time since midnight of second unit.
63pub type Time32SecondBufferBuilder =
64    BufferBuilder<<Time32SecondType as ArrowPrimitiveType>::Native>;
65/// Buffer builder for 32-bit elapsed time since midnight of millisecond unit.
66pub type Time32MillisecondBufferBuilder =
67    BufferBuilder<<Time32MillisecondType as ArrowPrimitiveType>::Native>;
68/// Buffer builder for 64-bit elapsed time since midnight of microsecond unit.
69pub type Time64MicrosecondBufferBuilder =
70    BufferBuilder<<Time64MicrosecondType as ArrowPrimitiveType>::Native>;
71/// Buffer builder for 64-bit elapsed time since midnight of nanosecond unit.
72pub type Time64NanosecondBufferBuilder =
73    BufferBuilder<<Time64NanosecondType as ArrowPrimitiveType>::Native>;
74
75/// Buffer builder for “calendar” interval in months.
76pub type IntervalYearMonthBufferBuilder =
77    BufferBuilder<<IntervalYearMonthType as ArrowPrimitiveType>::Native>;
78/// Buffer builder for “calendar” interval in days and milliseconds.
79pub type IntervalDayTimeBufferBuilder =
80    BufferBuilder<<IntervalDayTimeType as ArrowPrimitiveType>::Native>;
81/// Buffer builder “calendar” interval in months, days, and nanoseconds.
82pub type IntervalMonthDayNanoBufferBuilder =
83    BufferBuilder<<IntervalMonthDayNanoType as ArrowPrimitiveType>::Native>;
84
85/// Buffer builder for elapsed time of second unit.
86pub type DurationSecondBufferBuilder =
87    BufferBuilder<<DurationSecondType as ArrowPrimitiveType>::Native>;
88/// Buffer builder for elapsed time of milliseconds unit.
89pub type DurationMillisecondBufferBuilder =
90    BufferBuilder<<DurationMillisecondType as ArrowPrimitiveType>::Native>;
91/// Buffer builder for elapsed time of microseconds unit.
92pub type DurationMicrosecondBufferBuilder =
93    BufferBuilder<<DurationMicrosecondType as ArrowPrimitiveType>::Native>;
94/// Buffer builder for elapsed time of nanoseconds unit.
95pub type DurationNanosecondBufferBuilder =
96    BufferBuilder<<DurationNanosecondType as ArrowPrimitiveType>::Native>;
97
98#[cfg(test)]
99mod tests {
100    use crate::Array;
101    use crate::builder::{ArrayBuilder, Int8Builder, Int32BufferBuilder, UInt8BufferBuilder};
102
103    #[test]
104    fn test_builder_i32_empty() {
105        let mut b = Int32BufferBuilder::new(5);
106        assert_eq!(0, b.len());
107        assert_eq!(16, b.capacity());
108        let a = b.finish();
109        assert_eq!(0, a.len());
110    }
111
112    #[test]
113    fn test_builder_i32_alloc_zero_bytes() {
114        let mut b = Int32BufferBuilder::new(0);
115        b.append(123);
116        let a = b.finish();
117        assert_eq!(4, a.len());
118    }
119
120    #[test]
121    fn test_builder_i32() {
122        let mut b = Int32BufferBuilder::new(5);
123        for i in 0..5 {
124            b.append(i);
125        }
126        assert_eq!(16, b.capacity());
127        let a = b.finish();
128        assert_eq!(20, a.len());
129    }
130
131    #[test]
132    fn test_builder_i32_grow_buffer() {
133        let mut b = Int32BufferBuilder::new(2);
134        assert_eq!(16, b.capacity());
135        for i in 0..20 {
136            b.append(i);
137        }
138        assert_eq!(32, b.capacity());
139        let a = b.finish();
140        assert_eq!(80, a.len());
141    }
142
143    #[test]
144    fn test_builder_finish() {
145        let mut b = Int32BufferBuilder::new(5);
146        assert_eq!(16, b.capacity());
147        for i in 0..10 {
148            b.append(i);
149        }
150        let mut a = b.finish();
151        assert_eq!(40, a.len());
152        assert_eq!(0, b.len());
153        assert_eq!(0, b.capacity());
154
155        // Try build another buffer after cleaning up.
156        for i in 0..20 {
157            b.append(i)
158        }
159        assert_eq!(32, b.capacity());
160        a = b.finish();
161        assert_eq!(80, a.len());
162    }
163
164    #[test]
165    fn test_reserve() {
166        let mut b = UInt8BufferBuilder::new(2);
167        assert_eq!(64, b.capacity());
168        b.reserve(64);
169        assert_eq!(64, b.capacity());
170        b.reserve(65);
171        assert_eq!(128, b.capacity());
172
173        let mut b = Int32BufferBuilder::new(2);
174        assert_eq!(16, b.capacity());
175        b.reserve(16);
176        assert_eq!(16, b.capacity());
177        b.reserve(17);
178        assert_eq!(32, b.capacity());
179    }
180
181    #[test]
182    fn test_append_slice() {
183        let mut b = UInt8BufferBuilder::new(0);
184        b.append_slice(b"Hello, ");
185        b.append_slice(b"World!");
186        let buffer = b.finish();
187        assert_eq!(13, buffer.len());
188
189        let mut b = Int32BufferBuilder::new(0);
190        b.append_slice(&[32, 54]);
191        let buffer = b.finish();
192        assert_eq!(8, buffer.len());
193    }
194
195    #[test]
196    fn test_append_values() {
197        let mut a = Int8Builder::new();
198        a.append_value(1);
199        a.append_null();
200        a.append_value(-2);
201        assert_eq!(a.len(), 3);
202
203        // append values
204        let values = &[1, 2, 3, 4];
205        let is_valid = &[true, true, false, true];
206        a.append_values(values, is_valid);
207
208        assert_eq!(a.len(), 7);
209        let array = a.finish();
210        assert_eq!(array.value(0), 1);
211        assert!(array.is_null(1));
212        assert_eq!(array.value(2), -2);
213        assert_eq!(array.value(3), 1);
214        assert_eq!(array.value(4), 2);
215        assert!(array.is_null(5));
216        assert_eq!(array.value(6), 4);
217    }
218}