arrow_array/builder/
buffer_builder.rs

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