Apache GraphAr C++ Library
The C++ Library for Apache GraphAr
convert_to_arrow_type.h
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #pragma once
21 
22 #include <memory>
23 #include <string>
24 
25 #include "arrow/api.h"
26 #include "arrow/type.h"
27 
28 #include "graphar/types.h"
29 
30 namespace graphar {
31 
33 template <typename T>
34 struct CTypeToArrowType {};
35 
36 template <Type T>
37 struct TypeToArrowType {};
38 
39 #define CONVERT_TO_ARROW_TYPE(type, c_type, arrow_type, array_type, \
40  builder_type, type_value, str) \
41  template <> \
42  struct TypeToArrowType<type> { \
43  using CType = c_type; \
44  using ArrowType = arrow_type; \
45  using ArrayType = array_type; \
46  using BuilderType = builder_type; \
47  static std::shared_ptr<arrow::DataType> TypeValue() { return type_value; } \
48  static const char* type_to_string() { return str; } \
49  }; \
50  template <> \
51  struct CTypeToArrowType<c_type> { \
52  using CType = c_type; \
53  using ArrowType = arrow_type; \
54  using ArrayType = array_type; \
55  using BuilderType = builder_type; \
56  static std::shared_ptr<arrow::DataType> TypeValue() { return type_value; } \
57  static const char* type_to_string() { return str; } \
58  };
59 
60 CONVERT_TO_ARROW_TYPE(Type::BOOL, bool, arrow::BooleanType, arrow::BooleanArray,
61  arrow::BooleanBuilder, arrow::boolean(), "boolean")
62 CONVERT_TO_ARROW_TYPE(Type::INT32, int32_t, arrow::Int32Type, arrow::Int32Array,
63  arrow::Int32Builder, arrow::int32(), "int32")
64 CONVERT_TO_ARROW_TYPE(Type::INT64, int64_t, arrow::Int64Type, arrow::Int64Array,
65  arrow::Int64Builder, arrow::int64(), "int64")
66 CONVERT_TO_ARROW_TYPE(Type::FLOAT, float, arrow::FloatType, arrow::FloatArray,
67  arrow::FloatBuilder, arrow::float32(), "float")
68 CONVERT_TO_ARROW_TYPE(Type::DOUBLE, double, arrow::DoubleType,
69  arrow::DoubleArray, arrow::DoubleBuilder,
70  arrow::float64(), "double")
71 CONVERT_TO_ARROW_TYPE(Type::STRING, std::string, arrow::LargeStringType,
72  arrow::LargeStringArray, arrow::LargeStringBuilder,
73  arrow::large_utf8(), "string")
74 CONVERT_TO_ARROW_TYPE(Type::TIMESTAMP, Timestamp, arrow::TimestampType,
75  arrow::TimestampArray, arrow::TimestampBuilder,
76  arrow::timestamp(arrow::TimeUnit::MILLI), "timestamp")
77 CONVERT_TO_ARROW_TYPE(Type::DATE, Date, arrow::Date32Type, arrow::Date32Array,
78  arrow::Date32Builder, arrow::date32(), "date")
79 
80 } // namespace graphar