Apache GraphAr C++ Library
The C++ Library for Apache GraphAr
types.cc
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 #include <algorithm>
21 #include <memory>
22 
23 #include "arrow/api.h"
24 #include "arrow/type.h"
25 
26 #include "graphar/fwd.h"
27 #include "graphar/types.h"
28 
29 namespace graphar {
30 
31 std::shared_ptr<arrow::DataType> DataType::DataTypeToArrowDataType(
32  const std::shared_ptr<DataType>& type) {
33  switch (type->id()) {
34  case Type::BOOL:
35  return arrow::boolean();
36  case Type::INT32:
37  return arrow::int32();
38  case Type::INT64:
39  return arrow::int64();
40  case Type::FLOAT:
41  return arrow::float32();
42  case Type::DOUBLE:
43  return arrow::float64();
44  case Type::STRING:
45  return arrow::large_utf8();
46  case Type::DATE:
47  return arrow::date32();
48  case Type::TIMESTAMP:
49  return arrow::timestamp(arrow::TimeUnit::MILLI);
50  case Type::LIST:
51  return arrow::list(DataTypeToArrowDataType(type->child_));
52  default:
53  std::string msg = "The data type " + type->ToTypeName() +
54  " is not supported yet in GraphAr";
55  throw std::runtime_error(msg);
56  }
57 }
58 
59 std::shared_ptr<DataType> DataType::ArrowDataTypeToDataType(
60  const std::shared_ptr<arrow::DataType>& type) {
61  switch (type->id()) {
62  case arrow::Type::BOOL:
63  return boolean();
64  case arrow::Type::INT32:
65  return int32();
66  case arrow::Type::INT64:
67  return int64();
68  case arrow::Type::FLOAT:
69  return float32();
70  case arrow::Type::DOUBLE:
71  return float64();
72  case arrow::Type::STRING:
73  return string();
74  case arrow::Type::LARGE_STRING:
75  return string();
76  case arrow::Type::DATE32:
77  return date();
78  case arrow::Type::TIMESTAMP:
79  case arrow::Type::DATE64: // Date64 of Arrow is used to represent timestamp
80  // milliseconds
81  return timestamp();
82  case arrow::Type::LIST:
83  return list(ArrowDataTypeToDataType(type->field(0)->type()));
84  default:
85  std::string msg = "The arrow data type " + type->name() +
86  " is not supported yet in GraphAr";
87  throw std::runtime_error(msg);
88  }
89 }
90 
91 std::string DataType::ToTypeName() const {
92  switch (id_) {
93 #define TO_STRING_CASE(_id) \
94  case Type::_id: { \
95  std::string name(GAR_STRINGIFY(_id)); \
96  std::transform(name.begin(), name.end(), name.begin(), ::tolower); \
97  return name; \
98  }
99 
100  TO_STRING_CASE(BOOL)
101  TO_STRING_CASE(INT32)
102  TO_STRING_CASE(INT64)
103  TO_STRING_CASE(FLOAT)
104  TO_STRING_CASE(DOUBLE)
105  TO_STRING_CASE(STRING)
106  TO_STRING_CASE(DATE)
107  TO_STRING_CASE(TIMESTAMP)
108 
109 #undef TO_STRING_CASE
110  case Type::USER_DEFINED:
111  return user_defined_type_name_;
112  case Type::LIST:
113  return "list<" + child_->ToTypeName() + ">";
114  default:
115  return "unknown";
116  }
117 }
118 
119 std::shared_ptr<DataType> DataType::TypeNameToDataType(const std::string& str) {
120  if (str == "bool") {
121  return boolean();
122  } else if (str == "int32") {
123  return int32();
124  } else if (str == "int64") {
125  return int64();
126  } else if (str == "float") {
127  return float32();
128  } else if (str == "double") {
129  return float64();
130  } else if (str == "string") {
131  return string();
132  } else if (str == "date") {
133  return date();
134  } else if (str == "timestamp") {
135  return timestamp();
136  } else if (str == "list<int32>") {
137  return list(int32());
138  } else if (str == "list<int64>") {
139  return list(int64());
140  } else if (str == "list<float>") {
141  return list(float32());
142  } else if (str == "list<double>") {
143  return list(float64());
144  } else if (str == "list<string>") {
145  return list(string());
146  } else {
147  throw std::runtime_error("Unsupported data type " + str);
148  }
149 }
150 
151 #define TYPE_FACTORY(NAME, TYPE) \
152  const std::shared_ptr<DataType>& NAME() { \
153  static std::shared_ptr<DataType> result = \
154  std::make_shared<DataType>(TYPE); \
155  return result; \
156  }
157 
158 TYPE_FACTORY(boolean, Type::BOOL)
159 TYPE_FACTORY(int32, Type::INT32)
160 TYPE_FACTORY(int64, Type::INT64)
161 TYPE_FACTORY(float32, Type::FLOAT)
162 TYPE_FACTORY(float64, Type::DOUBLE)
163 TYPE_FACTORY(string, Type::STRING)
164 TYPE_FACTORY(date, Type::DATE)
165 TYPE_FACTORY(timestamp, Type::TIMESTAMP)
166 
167 std::shared_ptr<DataType> list(const std::shared_ptr<DataType>& value_type) {
168  return std::make_shared<DataType>(Type::LIST, value_type);
169 }
170 } // namespace graphar