Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat][C++] Support List Data Type, use list<float> as example #296

Merged
merged 10 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpp/include/gar/fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class DataType;
enum FileType { CSV = 0, PARQUET = 1, ORC = 2 };
enum class AdjListType : uint8_t;

template <typename T>
class Array;

class InfoVersion;

class Property;
Expand Down Expand Up @@ -120,6 +123,7 @@ const std::shared_ptr<DataType>& int64();
const std::shared_ptr<DataType>& float32();
const std::shared_ptr<DataType>& float64();
const std::shared_ptr<DataType>& string();
std::shared_ptr<DataType> list(const std::shared_ptr<DataType>& value_type);

namespace util {
struct FilterOptions;
Expand Down
33 changes: 6 additions & 27 deletions cpp/include/gar/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
// forward declarations
namespace arrow {
class ChunkedArray;
}
class Array;
} // namespace arrow

namespace GAR_NAMESPACE_INTERNAL {

Expand Down Expand Up @@ -69,23 +70,12 @@ class Vertex {
* @return Result: The property value or error.
*/
template <typename T>
inline Result<T> property(const std::string& property) noexcept {
if (properties_.find(property) == properties_.end()) {
return Status::KeyError("Property with name ", property,
" does not exist in the vertex.");
}
try {
T ret = std::any_cast<T>(properties_[property]);
return ret;
} catch (const std::bad_any_cast& e) {
return Status::TypeError("Any cast failed, the property type of ",
property, " is not matched ", e.what());
}
}
Result<T> property(const std::string& property) const;

private:
IdType id_;
std::map<std::string, std::any> properties_;
std::map<std::string, std::shared_ptr<arrow::Array>> list_properties_;
};

/**
Expand Down Expand Up @@ -124,23 +114,12 @@ class Edge {
* @return Result: The property value or error.
*/
template <typename T>
inline Result<T> property(const std::string& property) noexcept {
if (properties_.find(property) == properties_.end()) {
return Status::KeyError("Property with name ", property,
" does not exist in the edge.");
}
try {
T ret = std::any_cast<T>(properties_[property]);
return ret;
} catch (const std::bad_any_cast& e) {
return Status::TypeError("Any cast failed, the property type of ",
property, " is not matched ", e.what());
}
}
Result<T> property(const std::string& property) const;

private:
IdType src_id_, dst_id_;
std::map<std::string, std::any> properties_;
std::map<std::string, std::shared_ptr<arrow::Array>> list_properties_;
};

/**
Expand Down
16 changes: 14 additions & 2 deletions cpp/include/gar/util/convert_to_arrow_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,25 @@
namespace GAR_NAMESPACE_INTERNAL {

/** Struct to convert DataType to arrow::DataType. */
template <typename T>
struct CTypeToArrowType {};

template <Type T>
struct ConvertToArrowType {};
struct TypeToArrowType {};

#define CONVERT_TO_ARROW_TYPE(type, c_type, arrow_type, array_type, \
builder_type, type_value, str) \
template <> \
struct ConvertToArrowType<type> { \
struct TypeToArrowType<type> { \
using CType = c_type; \
using ArrowType = arrow_type; \
using ArrayType = array_type; \
using BuilderType = builder_type; \
static std::shared_ptr<arrow::DataType> TypeValue() { return type_value; } \
static const char* type_to_string() { return str; } \
}; \
template <> \
struct CTypeToArrowType<c_type> { \
using CType = c_type; \
using ArrowType = arrow_type; \
using ArrayType = array_type; \
Expand Down
20 changes: 16 additions & 4 deletions cpp/include/gar/util/data_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ enum class Type {
/** UTF8 variable-length string */
STRING,

/** List of some logical data type */
acezen marked this conversation as resolved.
Show resolved Hide resolved
LIST,
acezen marked this conversation as resolved.
Show resolved Hide resolved

/** User-defined data type */
USER_DEFINED,

Expand All @@ -67,14 +70,21 @@ class DataType {
DataType() : id_(Type::BOOL) {}

explicit DataType(Type id, const std::string& user_defined_type_name = "")
: id_(id), user_defined_type_name_(user_defined_type_name) {}
: id_(id),
child_(nullptr),
user_defined_type_name_(user_defined_type_name) {}

explicit DataType(Type id, const std::shared_ptr<DataType>& child)
: id_(id), child_(std::move(child)), user_defined_type_name_("") {}

DataType(const DataType& other)
: id_(other.id_),
child_(other.child_),
user_defined_type_name_(other.user_defined_type_name_) {}

explicit DataType(DataType&& other)
: id_(other.id_),
child_(std::move(other.child_)),
user_defined_type_name_(std::move(other.user_defined_type_name_)) {}

inline DataType& operator=(const DataType& other) = default;
Expand All @@ -91,18 +101,19 @@ class DataType {
return Equals(*other.get());
}

const std::shared_ptr<DataType>& value_type() const { return child_; }

bool operator==(const DataType& other) const { return Equals(other); }

bool operator!=(const DataType& other) const { return !Equals(other); }

static std::shared_ptr<arrow::DataType> DataTypeToArrowDataType(
const std::shared_ptr<DataType>& type);

static const std::shared_ptr<DataType>& ArrowDataTypeToDataType(
static std::shared_ptr<DataType> ArrowDataTypeToDataType(
const std::shared_ptr<arrow::DataType>& type);

static const std::shared_ptr<DataType>& TypeNameToDataType(
const std::string& str);
static std::shared_ptr<DataType> TypeNameToDataType(const std::string& str);

/** Return the type category of the DataType. */
Type id() const { return id_; }
Expand All @@ -111,6 +122,7 @@ class DataType {

private:
Type id_;
std::shared_ptr<DataType> child_;
std::string user_defined_type_name_;
}; // struct DataType
} // namespace GAR_NAMESPACE_INTERNAL
Expand Down
44 changes: 44 additions & 0 deletions cpp/include/gar/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,49 @@ struct ValueGetter<std::string> {

} // namespace util

template <typename T>
class Array final {
public:
using ValueType = T;
Array() : data_(nullptr), size_(0) {}
Array(const T* data, size_t size) : data_(data), size_(size) {}
Array(const Array& other) = default;
Array(Array&& other) = default;
Array& operator=(const Array& other) = default;
Array& operator=(Array&& other) = default;
~Array() = default;

const T& operator[](size_t index) const { return data_[index]; }

const T* data() const { return data_; }

size_t size() const { return size_; }

void clear() {
data_ = nullptr;
size_ = 0;
}

bool empty() const { return size_ == 0; }

void swap(Array& other) {
std::swap(data_, other.data_);
std::swap(size_, other.size_);
}

const T* begin() { return data_; }

const T* end() { return data_ + size_; }

private:
const T* data_;
size_t size_;
};

using Int32Array = Array<int32_t>;
using Int64Array = Array<int64_t>;
using FloatArray = Array<float>;
using DoubleArray = Array<double>;

} // namespace GAR_NAMESPACE_INTERNAL
#endif // GAR_UTIL_UTIL_H_
1 change: 1 addition & 0 deletions cpp/include/gar/writer/edges_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Edge {
* @param name The name of the property.
* @param val The value of the property.
*/
// TODO(@acezen): Enable the property to be a vector(list).
inline void AddProperty(const std::string& name, const std::any& val) {
empty_ = false;
properties_[name] = val;
Expand Down
1 change: 1 addition & 0 deletions cpp/include/gar/writer/vertices_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Vertex {
* @param name The name of the property.
* @param val The value of the property.
*/
// TODO(@acezen): Enable the property to be a vector(list).
inline void AddProperty(const std::string& name, const std::any& val) {
empty_ = false;
properties_[name] = val;
Expand Down
22 changes: 19 additions & 3 deletions cpp/src/data_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ std::shared_ptr<arrow::DataType> DataType::DataTypeToArrowDataType(
return arrow::float64();
case Type::STRING:
return arrow::large_utf8();
case Type::LIST:
return arrow::list(DataTypeToArrowDataType(type->child_));
default:
throw std::runtime_error("Unsupported data type");
}
}

const std::shared_ptr<DataType>& DataType::ArrowDataTypeToDataType(
std::shared_ptr<DataType> DataType::ArrowDataTypeToDataType(
const std::shared_ptr<arrow::DataType>& type) {
switch (type->id()) {
case arrow::Type::BOOL:
Expand All @@ -61,6 +63,8 @@ const std::shared_ptr<DataType>& DataType::ArrowDataTypeToDataType(
return string();
case arrow::Type::LARGE_STRING:
return string();
case arrow::Type::LIST:
return list(ArrowDataTypeToDataType(type->field(0)->type()));
default:
throw std::runtime_error("Unsupported data type");
}
Expand All @@ -85,13 +89,14 @@ std::string DataType::ToTypeName() const {
#undef TO_STRING_CASE
case Type::USER_DEFINED:
return user_defined_type_name_;
case Type::LIST:
return "list<" + child_->ToTypeName() + ">";
default:
return "unknown";
}
}

const std::shared_ptr<DataType>& DataType::TypeNameToDataType(
const std::string& str) {
std::shared_ptr<DataType> DataType::TypeNameToDataType(const std::string& str) {
if (str == "bool") {
return boolean();
} else if (str == "int32") {
Expand All @@ -104,6 +109,14 @@ const std::shared_ptr<DataType>& DataType::TypeNameToDataType(
return float64();
} else if (str == "string") {
return string();
} else if (str == "list<int32>") {
return list(int32());
} else if (str == "list<int64>") {
return list(int64());
} else if (str == "list<float>") {
return list(float32());
} else if (str == "list<double>") {
return list(float64());
} else {
throw std::runtime_error("Unsupported data type " + str);
}
Expand All @@ -123,4 +136,7 @@ TYPE_FACTORY(float32, Type::FLOAT)
TYPE_FACTORY(float64, Type::DOUBLE)
TYPE_FACTORY(string, Type::STRING)

std::shared_ptr<DataType> list(const std::shared_ptr<DataType>& value_type) {
return std::make_shared<DataType>(Type::LIST, value_type);
}
} // namespace GAR_NAMESPACE_INTERNAL
16 changes: 8 additions & 8 deletions cpp/src/edges_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,37 @@ Status EdgesBuilder::validate(const Edge& e,
switch (type->id()) {
case Type::BOOL:
if (property.second.type() !=
typeid(typename ConvertToArrowType<Type::BOOL>::CType)) {
typeid(typename TypeToArrowType<Type::BOOL>::CType)) {
invalid_type = true;
}
break;
case Type::INT32:
if (property.second.type() !=
typeid(typename ConvertToArrowType<Type::INT32>::CType)) {
typeid(typename TypeToArrowType<Type::INT32>::CType)) {
invalid_type = true;
}
break;
case Type::INT64:
if (property.second.type() !=
typeid(typename ConvertToArrowType<Type::INT64>::CType)) {
typeid(typename TypeToArrowType<Type::INT64>::CType)) {
invalid_type = true;
}
break;
case Type::FLOAT:
if (property.second.type() !=
typeid(typename ConvertToArrowType<Type::FLOAT>::CType)) {
typeid(typename TypeToArrowType<Type::FLOAT>::CType)) {
invalid_type = true;
}
break;
case Type::DOUBLE:
if (property.second.type() !=
typeid(typename ConvertToArrowType<Type::DOUBLE>::CType)) {
typeid(typename TypeToArrowType<Type::DOUBLE>::CType)) {
invalid_type = true;
}
break;
case Type::STRING:
if (property.second.type() !=
typeid(typename ConvertToArrowType<Type::STRING>::CType)) {
typeid(typename TypeToArrowType<Type::STRING>::CType)) {
invalid_type = true;
}
break;
Expand Down Expand Up @@ -193,9 +193,9 @@ Status EdgesBuilder::tryToAppend(
const std::string& property_name,
std::shared_ptr<arrow::Array>& array, // NOLINT
const std::vector<Edge>& edges) {
using CType = typename ConvertToArrowType<type>::CType;
using CType = typename TypeToArrowType<type>::CType;
arrow::MemoryPool* pool = arrow::default_memory_pool();
typename ConvertToArrowType<type>::BuilderType builder(pool);
typename TypeToArrowType<type>::BuilderType builder(pool);
for (const auto& e : edges) {
if (e.Empty() || (!e.ContainProperty(property_name))) {
RETURN_NOT_ARROW_OK(builder.AppendNull());
Expand Down
Loading
Loading