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
18#include "arrow/builder.h"
19
20#include <sstream>
21#include <string>
22#include <utility>
23#include <vector>
24
25#include "arrow/status.h"
26#include "arrow/type.h"
27#include "arrow/util/checked_cast.h"
28
29namespace arrow {
30
31class MemoryPool;
32
33// ----------------------------------------------------------------------
34// Helper functions
35
36#define BUILDER_CASE(ENUM, BuilderType) \
37 case Type::ENUM: \
38 out->reset(new BuilderType(type, pool)); \
39 return Status::OK();
40
41// Initially looked at doing this with vtables, but shared pointers makes it
42// difficult
43//
44// TODO(wesm): come up with a less monolithic strategy
45Status MakeBuilder(MemoryPool* pool, const std::shared_ptr<DataType>& type,
46 std::unique_ptr<ArrayBuilder>* out) {
47 switch (type->id()) {
48 case Type::NA: {
49 out->reset(new NullBuilder(pool));
50 return Status::OK();
51 }
52 BUILDER_CASE(UINT8, UInt8Builder);
53 BUILDER_CASE(INT8, Int8Builder);
54 BUILDER_CASE(UINT16, UInt16Builder);
55 BUILDER_CASE(INT16, Int16Builder);
56 BUILDER_CASE(UINT32, UInt32Builder);
57 BUILDER_CASE(INT32, Int32Builder);
58 BUILDER_CASE(UINT64, UInt64Builder);
59 BUILDER_CASE(INT64, Int64Builder);
60 BUILDER_CASE(DATE32, Date32Builder);
61 BUILDER_CASE(DATE64, Date64Builder);
62 BUILDER_CASE(TIME32, Time32Builder);
63 BUILDER_CASE(TIME64, Time64Builder);
64 BUILDER_CASE(TIMESTAMP, TimestampBuilder);
65 BUILDER_CASE(BOOL, BooleanBuilder);
66 BUILDER_CASE(HALF_FLOAT, HalfFloatBuilder);
67 BUILDER_CASE(FLOAT, FloatBuilder);
68 BUILDER_CASE(DOUBLE, DoubleBuilder);
69 BUILDER_CASE(STRING, StringBuilder);
70 BUILDER_CASE(BINARY, BinaryBuilder);
71 BUILDER_CASE(FIXED_SIZE_BINARY, FixedSizeBinaryBuilder);
72 BUILDER_CASE(DECIMAL, Decimal128Builder);
73 case Type::LIST: {
74 std::unique_ptr<ArrayBuilder> value_builder;
75 std::shared_ptr<DataType> value_type =
76 internal::checked_cast<const ListType&>(*type).value_type();
77 RETURN_NOT_OK(MakeBuilder(pool, value_type, &value_builder));
78 out->reset(new ListBuilder(pool, std::move(value_builder)));
79 return Status::OK();
80 }
81
82 case Type::STRUCT: {
83 const std::vector<std::shared_ptr<Field>>& fields = type->children();
84 std::vector<std::shared_ptr<ArrayBuilder>> values_builder;
85
86 for (auto it : fields) {
87 std::unique_ptr<ArrayBuilder> builder;
88 RETURN_NOT_OK(MakeBuilder(pool, it->type(), &builder));
89 values_builder.emplace_back(std::move(builder));
90 }
91 out->reset(new StructBuilder(type, pool, std::move(values_builder)));
92 return Status::OK();
93 }
94
95 default: {
96 return Status::NotImplemented("MakeBuilder: cannot construct builder for type ",
97 type->ToString());
98 }
99 }
100}
101
102} // namespace arrow
103