Skip to content

Commit

Permalink
Split value.h
Browse files Browse the repository at this point in the history
This file was recompiled the most in the entire project.
  • Loading branch information
Riolku committed Sep 14, 2023
1 parent 5899d35 commit 917491a
Show file tree
Hide file tree
Showing 51 changed files with 521 additions and 347 deletions.
7 changes: 6 additions & 1 deletion src/c_api/connection.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#include "binder/bound_statement_result.h"
#include "c_api/kuzu.h"
#include "common/exception/exception.h"
#include "common/types/value.h"
#include "main/kuzu.h"
#include "planner/logical_plan/logical_plan.h"

namespace kuzu {
namespace common {
class Value;
}
} // namespace kuzu

using namespace kuzu::common;
using namespace kuzu::main;

Expand Down
1 change: 1 addition & 0 deletions src/c_api/flat_tuple.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "processor/result/flat_tuple.h"

#include "c_api/kuzu.h"
#include "common/exception/exception.h"

using namespace kuzu::common;
using namespace kuzu::processor;
Expand Down
2 changes: 1 addition & 1 deletion src/c_api/prepared_statement.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "binder/bound_statement.h"
#include "c_api/kuzu.h"
#include "common/types/value.h"
#include "common/types/value/value.h"
#include "main/kuzu.h"
#include "planner/logical_plan/logical_plan.h"

Expand Down
6 changes: 5 additions & 1 deletion src/c_api/value.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#include "common/types/value.h"
#include "common/types/value/value.h"

#include "c_api/kuzu.h"
#include "common/types/internal_id_t.h"
#include "common/types/types.h"
#include "common/types/value/nested.h"
#include "common/types/value/node.h"
#include "common/types/value/recursive_rel.h"
#include "common/types/value/rel.h"
#include "main/kuzu.h"

using namespace kuzu::common;
Expand Down
4 changes: 3 additions & 1 deletion src/common/arrow/arrow_row_batch.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "common/arrow/arrow_row_batch.h"

#include "common/types/value.h"
#include "common/types/value/node.h"
#include "common/types/value/rel.h"
#include "common/types/value/value.h"
#include "storage/storage_utils.h"

namespace kuzu {
Expand Down
7 changes: 4 additions & 3 deletions src/common/types/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
add_subdirectory(value)

add_library(kuzu_common_types
OBJECT
blob.cpp
date_t.cpp
dtime_t.cpp
internal_id_t.cpp
interval_t.cpp
ku_list.cpp
ku_string.cpp
value.cpp
timestamp_t.cpp
types.cpp
blob.cpp)
types.cpp)

set(ALL_OBJECT_FILES
${ALL_OBJECT_FILES} $<TARGET_OBJECTS:kuzu_common_types>
Expand Down
11 changes: 11 additions & 0 deletions src/common/types/value/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_library(kuzu_common_types_value
OBJECT
nested.cpp
node.cpp
recursive_rel.cpp
rel.cpp
value.cpp)

set(ALL_OBJECT_FILES
${ALL_OBJECT_FILES} $<TARGET_OBJECTS:kuzu_common_types_value>
PARENT_SCOPE)
21 changes: 21 additions & 0 deletions src/common/types/value/nested.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "common/types/value/nested.h"

#include "common/exception/runtime.h"
#include "common/types/value/value.h"

namespace kuzu {
namespace common {

uint32_t NestedVal::getChildrenSize(const Value* val) {
return val->childrenSize;
}

Value* NestedVal::getChildVal(const Value* val, uint32_t idx) {
if (idx > val->childrenSize) {
throw RuntimeException("NestedVal::getChildPointer index out of bound.");
}
return val->children[idx].get();
}

} // namespace common
} // namespace kuzu
87 changes: 87 additions & 0 deletions src/common/types/value/node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "common/types/value/node.h"

#include "common/constants.h"
#include "common/types/types.h"
#include "common/types/value/value.h"
#include "spdlog/fmt/fmt.h"

namespace kuzu {
namespace common {

std::vector<std::pair<std::string, std::unique_ptr<Value>>> NodeVal::getProperties(
const Value* val) {
throwIfNotNode(val);
std::vector<std::pair<std::string, std::unique_ptr<Value>>> properties;
auto fieldNames = StructType::getFieldNames(val->dataType.get());
for (auto i = 0u; i < val->childrenSize; ++i) {
auto currKey = fieldNames[i];
if (currKey == InternalKeyword::ID || currKey == InternalKeyword::LABEL) {
continue;
}
properties.emplace_back(currKey, val->children[i]->copy());
}
return properties;
}

uint64_t NodeVal::getNumProperties(const Value* val) {
throwIfNotNode(val);
auto fieldNames = StructType::getFieldNames(val->dataType.get());
return fieldNames.size() - OFFSET;
}

std::string NodeVal::getPropertyName(const Value* val, uint64_t index) {
throwIfNotNode(val);
auto fieldNames = StructType::getFieldNames(val->dataType.get());
if (index >= fieldNames.size() - OFFSET) {
return "";
}
return fieldNames[index + OFFSET];
}

Value* NodeVal::getPropertyVal(const Value* val, uint64_t index) {
throwIfNotNode(val);
auto fieldNames = StructType::getFieldNames(val->dataType.get());
if (index >= fieldNames.size() - OFFSET) {
return nullptr;
}
return val->children[index + OFFSET].get();
}

Value* NodeVal::getNodeIDVal(const Value* val) {
throwIfNotNode(val);
auto fieldIdx = StructType::getFieldIdx(val->dataType.get(), InternalKeyword::ID);
return val->children[fieldIdx].get();
}

Value* NodeVal::getLabelVal(const Value* val) {
throwIfNotNode(val);
auto fieldIdx = StructType::getFieldIdx(val->dataType.get(), InternalKeyword::LABEL);
return val->children[fieldIdx].get();
}

nodeID_t NodeVal::getNodeID(const Value* val) {
throwIfNotNode(val);
auto nodeIDVal = getNodeIDVal(val);
return nodeIDVal->getValue<nodeID_t>();
}

std::string NodeVal::getLabelName(const Value* val) {
throwIfNotNode(val);
auto labelVal = getLabelVal(val);
return labelVal->getValue<std::string>();
}

std::string NodeVal::toString(const Value* val) {
throwIfNotNode(val);
return val->toString();
}

void NodeVal::throwIfNotNode(const Value* val) {
if (val->dataType->getLogicalTypeID() != LogicalTypeID::NODE) {
auto actualType = LogicalTypeUtils::dataTypeToString(val->dataType->getLogicalTypeID());
throw Exception(fmt::format("Expected NODE type, but got {} type", actualType));
}
}

} // namespace common
} // namespace kuzu
29 changes: 29 additions & 0 deletions src/common/types/value/recursive_rel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "common/types/value/recursive_rel.h"

#include "common/exception/exception.h"
#include "common/types/types.h"
#include "common/types/value/value.h"
#include "spdlog/fmt/fmt.h"

namespace kuzu {
namespace common {

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

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

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

} // namespace common
} // namespace kuzu
91 changes: 91 additions & 0 deletions src/common/types/value/rel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "common/types/value/rel.h"

#include "common/constants.h"
#include "common/types/value/value.h"
#include "spdlog/fmt/fmt.h"

namespace kuzu {
namespace common {

std::vector<std::pair<std::string, std::unique_ptr<Value>>> RelVal::getProperties(
const Value* val) {
throwIfNotRel(val);
std::vector<std::pair<std::string, std::unique_ptr<Value>>> properties;
auto fieldNames = StructType::getFieldNames(val->dataType.get());
for (auto i = 0u; i < val->childrenSize; ++i) {
auto currKey = fieldNames[i];
if (currKey == InternalKeyword::ID || currKey == InternalKeyword::LABEL ||
currKey == InternalKeyword::SRC || currKey == InternalKeyword::DST) {
continue;
}
auto currVal = val->children[i]->copy();
properties.emplace_back(currKey, std::move(currVal));
}
return properties;
}

uint64_t RelVal::getNumProperties(const Value* val) {
throwIfNotRel(val);
auto fieldNames = StructType::getFieldNames(val->dataType.get());
return fieldNames.size() - OFFSET;
}

std::string RelVal::getPropertyName(const Value* val, uint64_t index) {
throwIfNotRel(val);
auto fieldNames = StructType::getFieldNames(val->dataType.get());
if (index >= fieldNames.size() - OFFSET) {
return "";
}
return fieldNames[index + OFFSET];
}

Value* RelVal::getPropertyVal(const Value* val, uint64_t index) {
throwIfNotRel(val);
auto fieldNames = StructType::getFieldNames(val->dataType.get());
if (index >= fieldNames.size() - OFFSET) {
return nullptr;
}
return val->children[index + OFFSET].get();
}

Value* RelVal::getSrcNodeIDVal(const Value* val) {
auto fieldIdx = StructType::getFieldIdx(val->dataType.get(), InternalKeyword::SRC);
return val->children[fieldIdx].get();
}

Value* RelVal::getDstNodeIDVal(const Value* val) {
auto fieldIdx = StructType::getFieldIdx(val->dataType.get(), InternalKeyword::DST);
return val->children[fieldIdx].get();
}

nodeID_t RelVal::getSrcNodeID(const Value* val) {
throwIfNotRel(val);
auto srcNodeIDVal = getSrcNodeIDVal(val);
return srcNodeIDVal->getValue<nodeID_t>();
}

nodeID_t RelVal::getDstNodeID(const Value* val) {
throwIfNotRel(val);
auto dstNodeIDVal = getDstNodeIDVal(val);
return dstNodeIDVal->getValue<nodeID_t>();
}

std::string RelVal::getLabelName(const Value* val) {
auto fieldIdx = StructType::getFieldIdx(val->dataType.get(), InternalKeyword::LABEL);
return val->children[fieldIdx]->getValue<std::string>();
}

std::string RelVal::toString(const Value* val) {
throwIfNotRel(val);
return val->toString();
}

void RelVal::throwIfNotRel(const Value* val) {
if (val->dataType->getLogicalTypeID() != LogicalTypeID::REL) {
auto actualType = LogicalTypeUtils::dataTypeToString(val->dataType->getLogicalTypeID());
throw Exception(fmt::format("Expected REL type, but got {} type", actualType));
}
}

} // namespace common
} // namespace kuzu
Loading

0 comments on commit 917491a

Please sign in to comment.