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