Apache GraphAr C++ Library
The C++ Library for Apache GraphAr
types.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 <map>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 #include "graphar/fwd.h"
29 #include "graphar/macros.h"
30 
31 // forward declaration
32 namespace arrow {
33 class DataType;
34 }
35 
36 namespace graphar {
37 
39 enum class Type {
41  BOOL = 0,
42 
44  INT32,
45 
47  INT64,
48 
50  FLOAT,
51 
53  DOUBLE,
54 
56  STRING,
57 
59  LIST,
60 
62  DATE,
63 
65  TIMESTAMP,
66 
68  USER_DEFINED,
69 
70  // Leave this at the end
71  MAX_ID,
72 };
73 
78 class DataType {
79  public:
80  DataType() : id_(Type::BOOL) {}
81 
82  explicit DataType(Type id, const std::string& user_defined_type_name = "")
83  : id_(id),
84  child_(nullptr),
85  user_defined_type_name_(user_defined_type_name) {}
86 
87  explicit DataType(Type id, const std::shared_ptr<DataType>& child)
88  : id_(id), child_(std::move(child)), user_defined_type_name_("") {}
89 
90  DataType(const DataType& other)
91  : id_(other.id_),
92  child_(other.child_),
93  user_defined_type_name_(other.user_defined_type_name_) {}
94 
95  explicit DataType(DataType&& other)
96  : id_(other.id_),
97  child_(std::move(other.child_)),
98  user_defined_type_name_(std::move(other.user_defined_type_name_)) {}
99 
100  inline DataType& operator=(const DataType& other) = default;
101 
102  bool Equals(const DataType& other) const {
103  if (id_ != other.id_ ||
104  user_defined_type_name_ != other.user_defined_type_name_) {
105  return false;
106  }
107  if (child_ == nullptr && other.child_ == nullptr) {
108  return true;
109  }
110  if (child_ != nullptr && other.child_ != nullptr) {
111  return child_->Equals(other.child_);
112  }
113  return false;
114  }
115 
116  bool Equals(const std::shared_ptr<DataType>& other) const {
117  if (!other) {
118  return false;
119  }
120  return Equals(*other.get());
121  }
122 
123  const std::shared_ptr<DataType>& value_type() const { return child_; }
124 
125  bool operator==(const DataType& other) const { return Equals(other); }
126 
127  bool operator!=(const DataType& other) const { return !Equals(other); }
128 
129  static std::shared_ptr<arrow::DataType> DataTypeToArrowDataType(
130  const std::shared_ptr<DataType>& type);
131 
132  static std::shared_ptr<DataType> ArrowDataTypeToDataType(
133  const std::shared_ptr<arrow::DataType>& type);
134 
135  static std::shared_ptr<DataType> TypeNameToDataType(const std::string& str);
136 
138  Type id() const { return id_; }
139 
140  std::string ToTypeName() const;
141 
142  private:
143  Type id_;
144  std::shared_ptr<DataType> child_;
145  std::string user_defined_type_name_;
146 }; // struct DataType
147 
148 // Define a Timestamp class to represent timestamp data type value
149 class Timestamp {
150  public:
151  using c_type = int64_t;
152  explicit Timestamp(c_type value) : value_(value) {}
153 
154  c_type value() const { return value_; }
155 
156  private:
157  c_type value_;
158 };
159 
160 // Define a Date class to represent date data type value
161 class Date {
162  public:
163  using c_type = int32_t;
164  explicit Date(c_type value) : value_(value) {}
165 
166  c_type value() const { return value_; }
167 
168  private:
169  c_type value_;
170 };
171 
173 enum class AdjListType : std::uint8_t {
175  unordered_by_source = 0b00000001,
178  unordered_by_dest = 0b00000010,
180  ordered_by_source = 0b00000100,
183  ordered_by_dest = 0b00001000,
184 };
185 
186 constexpr AdjListType operator|(AdjListType lhs, AdjListType rhs) {
187  return static_cast<AdjListType>(
188  static_cast<std::underlying_type_t<AdjListType>>(lhs) |
189  static_cast<std::underlying_type_t<AdjListType>>(rhs));
190 }
191 
192 constexpr AdjListType operator&(AdjListType lhs, AdjListType rhs) {
193  return static_cast<AdjListType>(
194  static_cast<std::underlying_type_t<AdjListType>>(lhs) &
195  static_cast<std::underlying_type_t<AdjListType>>(rhs));
196 }
197 
198 static inline const char* AdjListTypeToString(AdjListType adj_list_type) {
199  static const std::map<AdjListType, const char*> adj_list2string{
200  {AdjListType::unordered_by_source, "unordered_by_source"},
201  {AdjListType::unordered_by_dest, "unordered_by_dest"},
202  {AdjListType::ordered_by_source, "ordered_by_source"},
203  {AdjListType::ordered_by_dest, "ordered_by_dest"}};
204  return adj_list2string.at(adj_list_type);
205 }
206 
207 static inline AdjListType OrderedAlignedToAdjListType(
208  bool ordered, const std::string& aligned) {
209  if (ordered) {
210  return aligned == "src" ? AdjListType::ordered_by_source
211  : AdjListType::ordered_by_dest;
212  }
213  return aligned == "src" ? AdjListType::unordered_by_source
214  : AdjListType::unordered_by_dest;
215 }
216 
217 static inline std::pair<bool, std::string> AdjListTypeToOrderedAligned(
218  AdjListType adj_list_type) {
219  switch (adj_list_type) {
220  case AdjListType::unordered_by_source:
221  return std::make_pair(false, "src");
222  case AdjListType::unordered_by_dest:
223  return std::make_pair(false, "dst");
224  case AdjListType::ordered_by_source:
225  return std::make_pair(true, "src");
226  case AdjListType::ordered_by_dest:
227  return std::make_pair(true, "dst");
228  default:
229  return std::make_pair(false, "dst");
230  }
231 }
232 
233 static inline FileType StringToFileType(const std::string& str) {
234  static const std::map<std::string, FileType> str2file_type{
235  {"csv", FileType::CSV},
236  {"json", FileType::JSON},
237  {"parquet", FileType::PARQUET},
238  {"orc", FileType::ORC}};
239  try {
240  return str2file_type.at(str.c_str());
241  } catch (const std::exception& e) {
242  throw std::runtime_error("KeyError: " + str);
243  }
244 }
245 
246 static inline const char* FileTypeToString(FileType file_type) {
247  static const std::map<FileType, const char*> file_type2string{
248  {FileType::CSV, "csv"},
249  {FileType::JSON, "json"},
250  {FileType::PARQUET, "parquet"},
251  {FileType::ORC, "orc"}};
252  return file_type2string.at(file_type);
253 }
254 
255 static inline Cardinality StringToCardinality(const std::string& str) {
256  static const std::map<std::string, Cardinality> str2cardinality{
257  {"single", Cardinality::SINGLE},
258  {"list", Cardinality::LIST},
259  {"set", Cardinality::SET},
260  };
261  try {
262  return str2cardinality.at(str.c_str());
263  } catch (const std::exception& e) {
264  throw std::runtime_error("KeyError: " + str);
265  }
266 }
267 
268 static inline const char* CardinalityToString(Cardinality cardinality) {
269  static const std::map<Cardinality, const char*> cardinality2string{
270  {Cardinality::SINGLE, "single"},
271  {Cardinality::LIST, "list"},
272  {Cardinality::SET, "set"},
273  };
274  try {
275  return cardinality2string.at(cardinality);
276  } catch (const std::exception& e) {
277  throw std::runtime_error("KeyError: " +
278  std::to_string(static_cast<int>(cardinality)));
279  }
280 }
281 
282 // Helper function to split a string by a delimiter
283 inline std::vector<std::string> SplitString(const std::string& str,
284  char delimiter) {
285  std::vector<std::string> tokens;
286  std::string token;
287  std::istringstream tokenStream(str);
288  while (std::getline(tokenStream, token, delimiter)) {
289  tokens.push_back(token);
290  }
291  return tokens;
292 }
293 } // namespace graphar
The DataType struct to provide enum type for data type and functions to parse data type.
Definition: types.h:78
Type id() const
Definition: types.h:138