Skip to content

Commit

Permalink
Merge pull request #1814 from kuzudb/api-data-type-fix
Browse files Browse the repository at this point in the history
Add new data type support for C, Java, Python API
  • Loading branch information
mewim committed Jul 13, 2023
2 parents 50f9d7d + eb31b2e commit 59763cf
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 51 deletions.
10 changes: 8 additions & 2 deletions src/c_api/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,17 @@ kuzu_value* kuzu_value_get_struct_field_value(kuzu_value* value, uint64_t index)
}

kuzu_value* kuzu_value_get_recursive_rel_node_list(kuzu_value* value) {
return kuzu_value_get_list_element(value, 0);
auto* c_value = (kuzu_value*)malloc(sizeof(kuzu_value));
c_value->_is_owned_by_cpp = true;
c_value->_value = RecursiveRelVal::getNodes(static_cast<Value*>(value->_value));
return c_value;
}

kuzu_value* kuzu_value_get_recursive_rel_rel_list(kuzu_value* value) {
return kuzu_value_get_list_element(value, 1);
auto* c_value = (kuzu_value*)malloc(sizeof(kuzu_value));
c_value->_is_owned_by_cpp = true;
c_value->_value = RecursiveRelVal::getRels(static_cast<Value*>(value->_value));
return c_value;
}

kuzu_logical_type* kuzu_value_get_data_type(kuzu_value* value) {
Expand Down
21 changes: 19 additions & 2 deletions src/common/types/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ std::string NodeVal::toString(const Value* val) {
void NodeVal::throwIfNotNode(const Value* val) {
if (val->getDataType().getLogicalTypeID() != LogicalTypeID::NODE) {
auto actualType = LogicalTypeUtils::dataTypeToString(val->getDataType().getLogicalTypeID());
throw Exception(fmt::format("Expected node type, but got {} type", actualType));
throw Exception(fmt::format("Expected NODE type, but got {} type", actualType));
}
}

Expand Down Expand Up @@ -660,7 +660,24 @@ std::unique_ptr<Value> RelVal::copy(const Value* val) {
void RelVal::throwIfNotRel(const Value* val) {
if (val->getDataType().getLogicalTypeID() != LogicalTypeID::REL) {
auto actualType = LogicalTypeUtils::dataTypeToString(val->getDataType().getLogicalTypeID());
throw Exception(fmt::format("Expected relationship type, but got {} type", actualType));
throw Exception(fmt::format("Expected REL type, but got {} type", actualType));
}
}

Value* RecursiveRelVal::getNodes(const Value* val) {
throwIfNotRecursiveRel(val);
return val->getListValReference()[0].get();
}

Value* RecursiveRelVal::getRels(const Value* val) {
throwIfNotRecursiveRel(val);
return val->getListValReference()[1].get();
}

void RecursiveRelVal::throwIfNotRecursiveRel(const Value* val) {
if (val->getDataType().getLogicalTypeID() != LogicalTypeID::RECURSIVE_REL) {
auto actualType = LogicalTypeUtils::dataTypeToString(val->getDataType().getLogicalTypeID());
throw Exception(fmt::format("Expected RECURSIVE_REL type, but got {} type", actualType));
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/include/c_api/kuzu.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ KUZU_C_API typedef enum {
KUZU_ANY = 0,
KUZU_NODE = 10,
KUZU_REL = 11,
KUZU_RECURSIVE_REL = 12,
// SERIAL is a special data type that is used to represent a sequence of INT64 values that are
// incremented by 1 starting from 0.
KUZU_SERIAL = 13,
// fixed size types
KUZU_BOOL = 22,
KUZU_INT64 = 23,
KUZU_INT32 = 24,
Expand All @@ -112,9 +117,14 @@ KUZU_C_API typedef enum {
KUZU_INTERVAL = 30,
KUZU_FIXED_LIST = 31,
KUZU_INTERNAL_ID = 40,
KUZU_ARROW_COLUMN = 41,
// variable size types
KUZU_STRING = 50,
KUZU_BLOB = 51,
KUZU_VAR_LIST = 52,
KUZU_STRUCT = 53,
KUZU_MAP = 54,
KUZU_UNION = 55,
} kuzu_data_type_id;

// Database
Expand Down
20 changes: 20 additions & 0 deletions src/include/common/types/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,26 @@ class RelVal {
inline static const uint64_t OFFSET = 4;
};

/**
* @brief RecursiveRelVal represents a path in the graph and stores the corresponding rels and nodes
* of that path.
*/
class RecursiveRelVal {
public:
/**
* @return the list of nodes in the recursive rel as a Value.
*/
KUZU_API static Value* getNodes(const Value* val);

/**
* @return the list of rels in the recursive rel as a Value.
*/
KUZU_API static Value* getRels(const Value* val);

private:
static void throwIfNotRecursiveRel(const Value* val);
};

/**
* @return boolean value.
*/
Expand Down
42 changes: 2 additions & 40 deletions tools/java_api/src/jni/kuzu_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,46 +109,8 @@ internalID_t getInternalID(JNIEnv* env, jobject id) {
}

std::string dataTypeToString(const LogicalType& dataType) {
switch (dataType.getLogicalTypeID()) {
case LogicalTypeID::ANY:
return "ANY";
case LogicalTypeID::NODE:
return "NODE";
case LogicalTypeID::REL:
return "REL";
case LogicalTypeID::SERIAL:
return "SERIAL";
case LogicalTypeID::BOOL:
return "BOOL";
case LogicalTypeID::INT64:
return "INT64";
case LogicalTypeID::INT32:
return "INT32";
case LogicalTypeID::INT16:
return "INT16";
case LogicalTypeID::DOUBLE:
return "DOUBLE";
case LogicalTypeID::FLOAT:
return "FLOAT";
case LogicalTypeID::DATE:
return "DATE";
case LogicalTypeID::TIMESTAMP:
return "TIMESTAMP";
case LogicalTypeID::INTERVAL:
return "INTERVAL";
case LogicalTypeID::FIXED_LIST:
return "FIXED_LIST";
case LogicalTypeID::INTERNAL_ID:
return "INTERNAL_ID";
case LogicalTypeID::STRING:
return "STRING";
case LogicalTypeID::VAR_LIST:
return "VAR_LIST";
case LogicalTypeID::STRUCT:
return "STRUCT";
default:
throw std::invalid_argument("Unimplemented item");
}
auto typeId = dataType.getLogicalTypeID();
return LogicalTypeUtils::dataTypeToString(typeId);
}

void javaMapToCPPMap(
Expand Down
8 changes: 7 additions & 1 deletion tools/java_api/src/main/java/com/kuzudb/KuzuDataTypeID.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public enum KuzuDataTypeID {
ANY(0),
NODE(10),
REL(11),
RECURSIVE_REL(12),
SERIAL(13),
BOOL(22),
INT64(23),
INT32(24),
Expand All @@ -18,9 +20,13 @@ public enum KuzuDataTypeID {
INTERVAL(30),
FIXED_LIST(31),
INTERNAL_ID(40),
ARROW_COLUMN(41),
STRING(50),
BLOB(51),
VAR_LIST(52),
STRUCT(53);
STRUCT(53),
MAP(54),
UNION(55);

public final int value;

Expand Down
19 changes: 13 additions & 6 deletions tools/python_api/src_py/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@

class Type(Enum):
"""The type of a value in the database."""
ANY = "ANY"
NODE = "NODE"
REL = "REL"
RECURSIVE_REL = "RECURSIVE_REL"
SERIAL = "SERIAL"
BOOL = "BOOL"
INT64 = "INT64"
INT32 = "INT32"
INT16 = "INT16"
DOUBLE = "DOUBLE"
FLOAT = "FLOAT"
STRING = "STRING"
DATE = "DATE"
TIMESTAMP = "TIMESTAMP"
INTERVAL = "INTERVAL"
LIST = "LIST"
NODE = "NODE"
REL = "REL"
NODE_ID = "NODE_ID"
FIXED_LIST = "FIXED_LIST"
INTERNAL_ID = "INTERNAL_ID"
ARROW_COLUMN = "ARROW_COLUMN"
STRING = "STRING"
BLOB = "BLOB"
VAR_LIST = "VAR_LIST"
STRUCT = "STRUCT"
RECURSIVE_REL = "RECURSIVE_REL"
MAP = "MAP"
UNION = "UNION"

0 comments on commit 59763cf

Please sign in to comment.