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 128-bit decimal type.
49pub type Decimal128BufferBuilder = BufferBuilder<<Decimal128Type as ArrowPrimitiveType>::Native>;
50/// Buffer builder for 256-bit decimal type.
51pub type Decimal256BufferBuilder = BufferBuilder<<Decimal256Type as ArrowPrimitiveType>::Native>;
52
53/// Buffer builder for timestamp type of second unit.
54pub type TimestampSecondBufferBuilder =
55    BufferBuilder<<TimestampSecondType as ArrowPrimitiveType>::Native>;
56/// Buffer builder for timestamp type of millisecond unit.
57pub type TimestampMillisecondBufferBuilder =
58    BufferBuilder<<TimestampMillisecondType as ArrowPrimitiveType>::Native>;
59/// Buffer builder for timestamp type of microsecond unit.
60pub type TimestampMicrosecondBufferBuilder =
61    BufferBuilder<<TimestampMicrosecondType as ArrowPrimitiveType>::Native>;
62/// Buffer builder for timestamp type of nanosecond unit.
63pub type TimestampNanosecondBufferBuilder =
64    BufferBuilder<<TimestampNanosecondType as ArrowPrimitiveType>::Native>;
65
66/// Buffer builder for 32-bit date type.
67pub type Date32BufferBuilder = BufferBuilder<<Date32Type as ArrowPrimitiveType>::Native>;
68/// Buffer builder for 64-bit date type.
69pub type Date64BufferBuilder = BufferBuilder<<Date64Type as ArrowPrimitiveType>::Native>;
70
71/// Buffer builder for 32-bit elaspsed time since midnight of second unit.
72pub type Time32SecondBufferBuilder =
73    BufferBuilder<<Time32SecondType as ArrowPrimitiveType>::Native>;
74/// Buffer builder for 32-bit elaspsed time since midnight of millisecond unit.
75pub type Time32MillisecondBufferBuilder =
76    BufferBuilder<<Time32MillisecondType as ArrowPrimitiveType>::Native>;
77/// Buffer builder for 64-bit elaspsed time since midnight of microsecond unit.
78pub type Time64MicrosecondBufferBuilder =
79    BufferBuilder<<Time64MicrosecondType as ArrowPrimitiveType>::Native>;
80/// Buffer builder for 64-bit elaspsed time since midnight of nanosecond unit.
81pub type Time64NanosecondBufferBuilder =
82    BufferBuilder<<Time64NanosecondType as ArrowPrimitiveType>::Native>;
83
84/// Buffer builder for “calendar” interval in months.
85pub type IntervalYearMonthBufferBuilder =
86    BufferBuilder<<IntervalYearMonthType as ArrowPrimitiveType>::Native>;
87/// Buffer builder for “calendar” interval in days and milliseconds.
88pub type IntervalDayTimeBufferBuilder =
89    BufferBuilder<<IntervalDayTimeType as ArrowPrimitiveType>::Native>;
90/// Buffer builder “calendar” interval in months, days, and nanoseconds.
91pub type IntervalMonthDayNanoBufferBuilder =
92    BufferBuilder<<IntervalMonthDayNanoType as ArrowPrimitiveType>::Native>;
93
94/// Buffer builder for elaspsed time of second unit.
95pub type DurationSecondBufferBuilder =
96    BufferBuilder<<DurationSecondType as ArrowPrimitiveType>::Native>;
97/// Buffer builder for elaspsed time of milliseconds unit.
98pub type DurationMillisecondBufferBuilder =
99    BufferBuilder<<DurationMillisecondType as ArrowPrimitiveType>::Native>;
100/// Buffer builder for elaspsed time of microseconds unit.
101pub type DurationMicrosecondBufferBuilder =
102    BufferBuilder<<DurationMicrosecondType as ArrowPrimitiveType>::Native>;
103/// Buffer builder for elaspsed time of nanoseconds unit.
104pub type DurationNanosecondBufferBuilder =
105    BufferBuilder<<DurationNanosecondType as ArrowPrimitiveType>::Native>;
106
107#[cfg(test)]
108mod tests {
109    use crate::builder::{ArrayBuilder, Int32BufferBuilder, Int8Builder, UInt8BufferBuilder};
110    use crate::Array;
111
112    #[test]
113    fn test_builder_i32_empty() {
114        let mut b = Int32BufferBuilder::new(5);
115        assert_eq!(0, b.len());
116        assert_eq!(16, b.capacity());
117        let a = b.finish();
118        assert_eq!(0, a.len());
119    }
120
121    #[test]
122    fn test_builder_i32_alloc_zero_bytes() {
123        let mut b = Int32BufferBuilder::new(0);
124        b.append(123);
125        let a = b.finish();
126        assert_eq!(4, a.len());
127    }
128
129    #[test]
130    fn test_builder_i32() {
131        let mut b = Int32BufferBuilder::new(5);
132        for i in 0..5 {
133            b.append(i);
134        }
135        assert_eq!(16, b.capacity());
136        let a = b.finish();
137        assert_eq!(20, a.len());
138    }
139
140    #[test]
141    fn test_builder_i32_grow_buffer() {
142        let mut b = Int32BufferBuilder::new(2);
143        assert_eq!(16, b.capacity());
144        for i in 0..20 {
145            b.append(i);
146        }
147        assert_eq!(32, b.capacity());
148        let a = b.finish();
149        assert_eq!(80, a.len());
150    }
151
152    #[test]
153    fn test_builder_finish() {
154        let mut b = Int32BufferBuilder::new(5);
155        assert_eq!(16, b.capacity());
156        for i in 0..10 {
157            b.append(i);
158        }
159        let mut a = b.finish();
160        assert_eq!(40, a.len());
161        assert_eq!(0, b.len());
162        assert_eq!(0, b.capacity());
163
164        // Try build another buffer after cleaning up.
165        for i in 0..20 {
166            b.append(i)
167        }
168        assert_eq!(32, b.capacity());
169        a = b.finish();
170        assert_eq!(80, a.len());
171    }
172
173    #[test]
174    fn test_reserve() {
175        let mut b = UInt8BufferBuilder::new(2);
176        assert_eq!(64, b.capacity());
177        b.reserve(64);
178        assert_eq!(64, b.capacity());
179        b.reserve(65);
180        assert_eq!(128, b.capacity());
181
182        let mut b = Int32BufferBuilder::new(2);
183        assert_eq!(16, b.capacity());
184        b.reserve(16);
185        assert_eq!(16, b.capacity());
186        b.reserve(17);
187        assert_eq!(32, b.capacity());
188    }
189
190    #[test]
191    fn test_append_slice() {
192        let mut b = UInt8BufferBuilder::new(0);
193        b.append_slice(b"Hello, ");
194        b.append_slice(b"World!");
195        let buffer = b.finish();
196        assert_eq!(13, buffer.len());
197
198        let mut b = Int32BufferBuilder::new(0);
199        b.append_slice(&[32, 54]);
200        let buffer = b.finish();
201        assert_eq!(8, buffer.len());
202    }
203
204    #[test]
205    fn test_append_values() {
206        let mut a = Int8Builder::new();
207        a.append_value(1);
208        a.append_null();
209        a.append_value(-2);
210        assert_eq!(a.len(), 3);
211
212        // append values
213        let values = &[1, 2, 3, 4];
214        let is_valid = &[true, true, false, true];
215        a.append_values(values, is_valid);
216
217        assert_eq!(a.len(), 7);
218        let array = a.finish();
219        assert_eq!(array.value(0), 1);
220        assert!(array.is_null(1));
221        assert_eq!(array.value(2), -2);
222        assert_eq!(array.value(3), 1);
223        assert_eq!(array.value(4), 2);
224        assert!(array.is_null(5));
225        assert_eq!(array.value(6), 4);
226    }
227}