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 
27 #include "graphar/fwd.h"
28 #include "graphar/macros.h"
29 
30 // forward declaration
31 namespace arrow {
32 class DataType;
33 }
34 
35 namespace graphar {
36 
38 enum class Type {
40  BOOL = 0,
41 
43  INT32,
44 
46  INT64,
47 
49  FLOAT,
50 
52  DOUBLE,
53 
55  STRING,
56 
58  LIST,
59 
61  DATE,
62 
64  TIMESTAMP,
65 
67  USER_DEFINED,
68 
69  // Leave this at the end
70  MAX_ID,
71 };
72 
77 class DataType {
78  public:
79  DataType() : id_(Type::BOOL) {}
80 
81  explicit DataType(Type id, const std::string& user_defined_type_name = "")
82  : id_(id),
83  child_(nullptr),
84  user_defined_type_name_(user_defined_type_name) {}
85 
86  explicit DataType(Type id, const std::shared_ptr<DataType>& child)
87  : id_(id), child_(std::move(child)), user_defined_type_name_("") {}
88 
89  DataType(const DataType& other)
90  : id_(other.id_),
91  child_(other.child_),
92  user_defined_type_name_(other.user_defined_type_name_) {}
93 
94  explicit DataType(DataType&& other)
95  : id_(other.id_),
96  child_(std::move(other.child_)),
97  user_defined_type_name_(std::move(other.user_defined_type_name_)) {}
98 
99  inline DataType& operator=(const DataType& other) = default;
100 
101  bool Equals(const DataType& other) const {
102  return id_ == other.id_ &&
103  user_defined_type_name_ == other.user_defined_type_name_;
104  }
105 
106  bool Equals(const std::shared_ptr<DataType>& other) const {
107  if (!other) {
108  return false;
109  }
110  return Equals(*other.get());
111  }
112 
113  const std::shared_ptr<DataType>& value_type() const { return child_; }
114 
115  bool operator==(const DataType& other) const { return Equals(other); }
116 
117  bool operator!=(const DataType& other) const { return !Equals(other); }
118 
119  static std::shared_ptr<arrow::DataType> DataTypeToArrowDataType(
120  const std::shared_ptr<DataType>& type);
121 
122  static std::shared_ptr<DataType> ArrowDataTypeToDataType(
123  const std::shared_ptr<arrow::DataType>& type);
124 
125  static std::shared_ptr<DataType> TypeNameToDataType(const std::string& str);
126 
128  Type id() const { return id_; }
129 
130  std::string ToTypeName() const;
131 
132  private:
133  Type id_;
134  std::shared_ptr<DataType> child_;
135  std::string user_defined_type_name_;
136 }; // struct DataType
137 
138 // Define a Timestamp class to represent timestamp data type value
139 class Timestamp {
140  public:
141  using c_type = int64_t;
142  explicit Timestamp(c_type value) : value_(value) {}
143 
144  c_type value() const { return value_; }
145 
146  private:
147  c_type value_;
148 };
149 
150 // Define a Date class to represent date data type value
151 class Date {
152  public:
153  using c_type = int32_t;
154  explicit Date(c_type value) : value_(value) {}
155 
156  c_type value() const { return value_; }
157 
158  private:
159  c_type value_;
160 };
161 
163 enum class AdjListType : std::uint8_t {
165  unordered_by_source = 0b00000001,
168  unordered_by_dest = 0b00000010,
170  ordered_by_source = 0b00000100,
173  ordered_by_dest = 0b00001000,
174 };
175 
176 constexpr AdjListType operator|(AdjListType lhs, AdjListType rhs) {
177  return static_cast<AdjListType>(
178  static_cast<std::underlying_type_t<AdjListType>>(lhs) |
179  static_cast<std::underlying_type_t<AdjListType>>(rhs));
180 }
181 
182 constexpr AdjListType operator&(AdjListType lhs, AdjListType rhs) {
183  return static_cast<AdjListType>(
184  static_cast<std::underlying_type_t<AdjListType>>(lhs) &
185  static_cast<std::underlying_type_t<AdjListType>>(rhs));
186 }
187 
188 static inline const char* AdjListTypeToString(AdjListType adj_list_type) {
189  static const std::map<AdjListType, const char*> adj_list2string{
190  {AdjListType::unordered_by_source, "unordered_by_source"},
191  {AdjListType::unordered_by_dest, "unordered_by_dest"},
192  {AdjListType::ordered_by_source, "ordered_by_source"},
193  {AdjListType::ordered_by_dest, "ordered_by_dest"}};
194  return adj_list2string.at(adj_list_type);
195 }
196 
197 static inline AdjListType OrderedAlignedToAdjListType(
198  bool ordered, const std::string& aligned) {
199  if (ordered) {
200  return aligned == "src" ? AdjListType::ordered_by_source
201  : AdjListType::ordered_by_dest;
202  }
203  return aligned == "src" ? AdjListType::unordered_by_source
204  : AdjListType::unordered_by_dest;
205 }
206 
207 static inline std::pair<bool, std::string> AdjListTypeToOrderedAligned(
208  AdjListType adj_list_type) {
209  switch (adj_list_type) {
210  case AdjListType::unordered_by_source:
211  return std::make_pair(false, "src");
212  case AdjListType::unordered_by_dest:
213  return std::make_pair(false, "dst");
214  case AdjListType::ordered_by_source:
215  return std::make_pair(true, "src");
216  case AdjListType::ordered_by_dest:
217  return std::make_pair(true, "dst");
218  default:
219  return std::make_pair(false, "dst");
220  }
221 }
222 
223 static inline FileType StringToFileType(const std::string& str) {
224  static const std::map<std::string, FileType> str2file_type{
225  {"csv", FileType::CSV},
226  {"json", FileType::JSON},
227  {"parquet", FileType::PARQUET},
228  {"orc", FileType::ORC}};
229  try {
230  return str2file_type.at(str.c_str());
231  } catch (const std::exception& e) {
232  throw std::runtime_error("KeyError: " + str);
233  }
234 }
235 
236 static inline const char* FileTypeToString(FileType file_type) {
237  static const std::map<FileType, const char*> file_type2string{
238  {FileType::CSV, "csv"},
239  {FileType::JSON, "json"},
240  {FileType::PARQUET, "parquet"},
241  {FileType::ORC, "orc"}};
242  return file_type2string.at(file_type);
243 }
244 
245 } // namespace graphar
The DataType struct to provide enum type for data type and functions to parse data type.
Definition: types.h:77
Type id() const
Definition: types.h:128