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.
1718pub use arrow_buffer::BufferBuilder;
19use half::f16;
2021use crate::types::*;
2223/// 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>;
4546/// 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>;
5051/// 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>;
6364/// 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>;
6869/// 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>;
8182/// 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>;
9192/// 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>;
104105#[cfg(test)]
106mod tests {
107use crate::builder::{ArrayBuilder, Int32BufferBuilder, Int8Builder, UInt8BufferBuilder};
108use crate::Array;
109110#[test]
111fn test_builder_i32_empty() {
112let mut b = Int32BufferBuilder::new(5);
113assert_eq!(0, b.len());
114assert_eq!(16, b.capacity());
115let a = b.finish();
116assert_eq!(0, a.len());
117 }
118119#[test]
120fn test_builder_i32_alloc_zero_bytes() {
121let mut b = Int32BufferBuilder::new(0);
122 b.append(123);
123let a = b.finish();
124assert_eq!(4, a.len());
125 }
126127#[test]
128fn test_builder_i32() {
129let mut b = Int32BufferBuilder::new(5);
130for i in 0..5 {
131 b.append(i);
132 }
133assert_eq!(16, b.capacity());
134let a = b.finish();
135assert_eq!(20, a.len());
136 }
137138#[test]
139fn test_builder_i32_grow_buffer() {
140let mut b = Int32BufferBuilder::new(2);
141assert_eq!(16, b.capacity());
142for i in 0..20 {
143 b.append(i);
144 }
145assert_eq!(32, b.capacity());
146let a = b.finish();
147assert_eq!(80, a.len());
148 }
149150#[test]
151fn test_builder_finish() {
152let mut b = Int32BufferBuilder::new(5);
153assert_eq!(16, b.capacity());
154for i in 0..10 {
155 b.append(i);
156 }
157let mut a = b.finish();
158assert_eq!(40, a.len());
159assert_eq!(0, b.len());
160assert_eq!(0, b.capacity());
161162// Try build another buffer after cleaning up.
163for i in 0..20 {
164 b.append(i)
165 }
166assert_eq!(32, b.capacity());
167 a = b.finish();
168assert_eq!(80, a.len());
169 }
170171#[test]
172fn test_reserve() {
173let mut b = UInt8BufferBuilder::new(2);
174assert_eq!(64, b.capacity());
175 b.reserve(64);
176assert_eq!(64, b.capacity());
177 b.reserve(65);
178assert_eq!(128, b.capacity());
179180let mut b = Int32BufferBuilder::new(2);
181assert_eq!(16, b.capacity());
182 b.reserve(16);
183assert_eq!(16, b.capacity());
184 b.reserve(17);
185assert_eq!(32, b.capacity());
186 }
187188#[test]
189fn test_append_slice() {
190let mut b = UInt8BufferBuilder::new(0);
191 b.append_slice(b"Hello, ");
192 b.append_slice(b"World!");
193let buffer = b.finish();
194assert_eq!(13, buffer.len());
195196let mut b = Int32BufferBuilder::new(0);
197 b.append_slice(&[32, 54]);
198let buffer = b.finish();
199assert_eq!(8, buffer.len());
200 }
201202#[test]
203fn test_append_values() {
204let mut a = Int8Builder::new();
205 a.append_value(1);
206 a.append_null();
207 a.append_value(-2);
208assert_eq!(a.len(), 3);
209210// append values
211let values = &[1, 2, 3, 4];
212let is_valid = &[true, true, false, true];
213 a.append_values(values, is_valid);
214215assert_eq!(a.len(), 7);
216let array = a.finish();
217assert_eq!(array.value(0), 1);
218assert!(array.is_null(1));
219assert_eq!(array.value(2), -2);
220assert_eq!(array.value(3), 1);
221assert_eq!(array.value(4), 2);
222assert!(array.is_null(5));
223assert_eq!(array.value(6), 4);
224 }
225}