Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
stl.h
Go to the documentation of this file.
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 #ifndef ARROW_STL_H
19 #define ARROW_STL_H
20 
21 #include <memory>
22 #include <string>
23 #include <tuple>
24 #include <vector>
25 
26 #include "arrow/type.h"
27 
28 namespace arrow {
29 
30 class Schema;
31 
32 namespace stl {
33 
35 template <typename T>
36 struct ConversionTraits {};
37 
38 #define ARROW_STL_CONVERSION(c_type, ArrowType_) \
39  template <> \
40  struct ConversionTraits<c_type> { \
41  static std::shared_ptr<DataType> arrow_type() { \
42  return std::make_shared<ArrowType_>(); \
43  } \
44  constexpr static bool nullable = false; \
45  };
46 
48 ARROW_STL_CONVERSION(int8_t, Int8Type)
49 ARROW_STL_CONVERSION(int16_t, Int16Type)
52 ARROW_STL_CONVERSION(uint8_t, UInt8Type)
53 ARROW_STL_CONVERSION(uint16_t, UInt16Type)
54 ARROW_STL_CONVERSION(uint32_t, UInt32Type)
55 ARROW_STL_CONVERSION(uint64_t, UInt64Type)
58 ARROW_STL_CONVERSION(std::string, StringType)
59 
60 template <typename value_c_type>
61 struct ConversionTraits<std::vector<value_c_type>> {
62  static std::shared_ptr<DataType> arrow_type() {
64  }
65  constexpr static bool nullable = false;
66 };
67 
72 template <typename Tuple, std::size_t N = std::tuple_size<Tuple>::value>
74  using Element = typename std::tuple_element<N - 1, Tuple>::type;
75 
76  // Implementations that take a vector-like object for the column names.
77 
81  static std::vector<std::shared_ptr<Field>> MakeSchemaRecursion(
82  const std::vector<std::string>& names) {
83  std::vector<std::shared_ptr<Field>> ret =
85  std::shared_ptr<DataType> type = ConversionTraits<Element>::arrow_type();
86  ret.push_back(field(names[N - 1], type, ConversionTraits<Element>::nullable));
87  return ret;
88  }
89 
100  static std::shared_ptr<Schema> MakeSchema(const std::vector<std::string>& names) {
101  return std::make_shared<Schema>(MakeSchemaRecursion(names));
102  }
103 
104  // Implementations that take a tuple-like object for the column names.
105 
109  template <typename NamesTuple>
110  static std::vector<std::shared_ptr<Field>> MakeSchemaRecursionT(
111  const NamesTuple& names) {
112  std::vector<std::shared_ptr<Field>> ret =
114  std::shared_ptr<DataType> type = ConversionTraits<Element>::arrow_type();
115  ret.push_back(
116  field(std::get<N - 1>(names), type, ConversionTraits<Element>::nullable));
117  return ret;
118  }
119 
130  template <typename NamesTuple>
131  static std::shared_ptr<Schema> MakeSchema(const NamesTuple& names) {
132  return std::make_shared<Schema>(MakeSchemaRecursionT<NamesTuple>(names));
133  }
134 };
135 
136 template <typename Tuple>
137 struct SchemaFromTuple<Tuple, 0> {
138  static std::vector<std::shared_ptr<Field>> MakeSchemaRecursion(
139  const std::vector<std::string>& names) {
140  std::vector<std::shared_ptr<Field>> ret;
141  ret.reserve(names.size());
142  return ret;
143  }
144 
145  template <typename NamesTuple>
146  static std::vector<std::shared_ptr<Field>> MakeSchemaRecursionT(
147  const NamesTuple& names) {
148  std::vector<std::shared_ptr<Field>> ret;
149  ret.reserve(std::tuple_size<NamesTuple>::value);
150  return ret;
151  }
152 };
154 
155 } // namespace stl
156 } // namespace arrow
157 
158 #endif // ARROW_STL_H
std::shared_ptr< DataType > list(const std::shared_ptr< Field > &value_type)
Create a ListType instance from its child Field type.
Definition: common.h:148
DataType< Type::FLOAT > FloatType
Definition: types.h:280
typename std::tuple_element< N - 1, Tuple >::type Element
Definition: stl.h:74
Build an arrow::Schema based upon the types defined in a std::tuple-like structure.
Definition: stl.h:73
static std::vector< std::shared_ptr< Field > > MakeSchemaRecursion(const std::vector< std::string > &names)
Definition: stl.h:138
std::shared_ptr< Field > field(const std::string &name, const std::shared_ptr< DataType > &type, bool nullable=true, const std::shared_ptr< const KeyValueMetadata > &metadata=NULLPTR)
Create a Field instance.
#define ARROW_STL_CONVERSION(c_type, ArrowType_)
Definition: stl.h:38
static std::vector< std::shared_ptr< Field > > MakeSchemaRecursionT(const NamesTuple &names)
Definition: stl.h:146
DataType< Type::BOOLEAN > BooleanType
Definition: types.h:276
static std::shared_ptr< Schema > MakeSchema(const NamesTuple &names)
Build a Schema from the types of the tuple-like structure passed in as template parameter assign the ...
Definition: stl.h:131
static std::shared_ptr< DataType > arrow_type()
Definition: stl.h:62
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
DataType< Type::INT64 > Int64Type
Definition: types.h:278
Traits meta class to map standard C/C++ types to equivalent Arrow types.
Definition: stl.h:36
DataType< Type::INT32 > Int32Type
Definition: types.h:277
static std::vector< std::shared_ptr< Field > > MakeSchemaRecursion(const std::vector< std::string > &names)
Recursively build a vector of arrow::Field from the defined types.
Definition: stl.h:81
DataType< Type::DOUBLE > DoubleType
Definition: types.h:281
static std::vector< std::shared_ptr< Field > > MakeSchemaRecursionT(const NamesTuple &names)
Recursively build a vector of arrow::Field from the defined types.
Definition: stl.h:110
static std::shared_ptr< Schema > MakeSchema(const std::vector< std::string > &names)
Build a Schema from the types of the tuple-like structure passed in as template parameter assign the ...
Definition: stl.h:100