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  using ArrowType = ArrowType_; \
42  constexpr static bool nullable = false; \
43  };
44 
45 ARROW_STL_CONVERSION(bool, BooleanType)
46 ARROW_STL_CONVERSION(int8_t, Int8Type)
47 ARROW_STL_CONVERSION(int16_t, Int16Type)
48 ARROW_STL_CONVERSION(int32_t, Int32Type)
49 ARROW_STL_CONVERSION(int64_t, Int64Type)
50 ARROW_STL_CONVERSION(uint8_t, UInt8Type)
51 ARROW_STL_CONVERSION(uint16_t, UInt16Type)
52 ARROW_STL_CONVERSION(uint32_t, UInt32Type)
53 ARROW_STL_CONVERSION(uint64_t, UInt64Type)
54 ARROW_STL_CONVERSION(float, FloatType)
55 ARROW_STL_CONVERSION(double, DoubleType)
56 ARROW_STL_CONVERSION(std::string, StringType)
57 
58 template <typename value_c_type>
59 struct ConversionTraits<std::vector<value_c_type>> {
61  constexpr static bool nullable = false;
62 };
63 
68 template <typename Tuple, std::size_t N = std::tuple_size<Tuple>::value>
70  using Element = typename std::tuple_element<N - 1, Tuple>::type;
72 
73  // Implementations that take a vector-like object for the column names.
74 
78  static std::vector<std::shared_ptr<Field>> MakeSchemaRecursion(
79  const std::vector<std::string>& names) {
80  std::vector<std::shared_ptr<Field>> ret =
82  ret.push_back(field(names[N - 1], std::make_shared<ArrowType>(),
84  return ret;
85  }
86 
97  static std::shared_ptr<Schema> MakeSchema(const std::vector<std::string>& names) {
98  return std::make_shared<Schema>(MakeSchemaRecursion(names));
99  }
100 
101  // Implementations that take a tuple-like object for the column names.
102 
106  template <typename NamesTuple>
107  static std::vector<std::shared_ptr<Field>> MakeSchemaRecursionT(
108  const NamesTuple& names) {
109  std::vector<std::shared_ptr<Field>> ret =
111  ret.push_back(field(std::get<N - 1>(names), std::make_shared<ArrowType>(),
113  return ret;
114  }
115 
126  template <typename NamesTuple>
127  static std::shared_ptr<Schema> MakeSchema(const NamesTuple& names) {
128  return std::make_shared<Schema>(MakeSchemaRecursionT<NamesTuple>(names));
129  }
130 };
131 
132 template <typename Tuple>
133 struct SchemaFromTuple<Tuple, 0> {
134  static std::vector<std::shared_ptr<Field>> MakeSchemaRecursion(
135  const std::vector<std::string>& names) {
136  std::vector<std::shared_ptr<Field>> ret;
137  ret.reserve(names.size());
138  return ret;
139  }
140 
141  template <typename NamesTuple>
142  static std::vector<std::shared_ptr<Field>> MakeSchemaRecursionT(
143  const NamesTuple& names) {
144  std::vector<std::shared_ptr<Field>> ret;
145  ret.reserve(std::tuple_size<NamesTuple>::value);
146  return ret;
147  }
148 };
150 
151 } // namespace stl
152 } // namespace arrow
153 
154 #endif // ARROW_STL_H
typename ConversionTraits< Element >::ArrowType ArrowType
Definition: stl.h:71
Additional ListType class that can be instantiated with only compile-time arguments.
Definition: type.h:411
typename std::tuple_element< N - 1, Tuple >::type Element
Definition: stl.h:70
Build an arrow::Schema based upon the types defined in a std::tuple-like structure.
Definition: stl.h:69
static std::vector< std::shared_ptr< Field > > MakeSchemaRecursion(const std::vector< std::string > &names)
Definition: stl.h:134
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:142
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:127
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
Traits meta class to map standard C/C++ types to equivalent Arrow types.
Definition: stl.h:36
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:78
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:107
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:97