23 #include "arrow/api.h"
24 #include "arrow/type.h"
26 #include "graphar/fwd.h"
27 #include "graphar/types.h"
31 std::shared_ptr<arrow::DataType> DataType::DataTypeToArrowDataType(
32 const std::shared_ptr<DataType>& type) {
35 return arrow::boolean();
37 return arrow::int32();
39 return arrow::int64();
41 return arrow::float32();
43 return arrow::float64();
45 return arrow::large_utf8();
47 return arrow::date32();
49 return arrow::timestamp(arrow::TimeUnit::MILLI);
51 return arrow::list(DataTypeToArrowDataType(type->child_));
53 std::string msg =
"The data type " + type->ToTypeName() +
54 " is not supported yet in GraphAr";
55 throw std::runtime_error(msg);
59 std::shared_ptr<DataType> DataType::ArrowDataTypeToDataType(
60 const std::shared_ptr<arrow::DataType>& type) {
62 case arrow::Type::BOOL:
64 case arrow::Type::INT32:
66 case arrow::Type::INT64:
68 case arrow::Type::FLOAT:
70 case arrow::Type::DOUBLE:
72 case arrow::Type::STRING:
74 case arrow::Type::LARGE_STRING:
76 case arrow::Type::DATE32:
78 case arrow::Type::TIMESTAMP:
79 case arrow::Type::DATE64:
82 case arrow::Type::LIST:
83 return list(ArrowDataTypeToDataType(type->field(0)->type()));
85 std::string msg =
"The arrow data type " + type->name() +
86 " is not supported yet in GraphAr";
87 throw std::runtime_error(msg);
91 std::string DataType::ToTypeName()
const {
93 #define TO_STRING_CASE(_id) \
95 std::string name(GAR_STRINGIFY(_id)); \
96 std::transform(name.begin(), name.end(), name.begin(), ::tolower); \
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)
107 TO_STRING_CASE(TIMESTAMP)
109 #undef TO_STRING_CASE
110 case Type::USER_DEFINED:
111 return user_defined_type_name_;
113 return "list<" + child_->ToTypeName() +
">";
119 std::shared_ptr<DataType> DataType::TypeNameToDataType(
const std::string& str) {
122 }
else if (str ==
"int32") {
124 }
else if (str ==
"int64") {
126 }
else if (str ==
"float") {
128 }
else if (str ==
"double") {
130 }
else if (str ==
"string") {
132 }
else if (str ==
"date") {
134 }
else if (str ==
"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());
147 throw std::runtime_error(
"Unsupported data type " + str);
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); \
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)
167 std::shared_ptr<DataType> list(
const std::shared_ptr<DataType>& value_type) {
168 return std::make_shared<DataType>(Type::LIST, value_type);