Skip to content

Implement the vector type and property type and serialization and deserialization for vector property. #6072

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

Open
wants to merge 1 commit into
base: vector
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/codec/RowReaderV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ Value RowReaderV2::getValueByIndex(const int64_t index) const {
return nebula::extractIntOrFloat<int32_t, Set>(data_, offset);
case PropertyType::SET_FLOAT:
return nebula::extractIntOrFloat<float, Set>(data_, offset);
case PropertyType::VECTOR:
// TODO(LZY)
break;
case PropertyType::UNKNOWN:
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/common/datatypes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ nebula_add_library(
Set.cpp
Geography.cpp
Duration.cpp
Vector.cpp
)

nebula_add_subdirectory(test)
2 changes: 2 additions & 0 deletions src/common/datatypes/CommonCpp2Ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct LineString;
struct Polygon;
struct Geography;
struct Duration;
struct Vector;
} // namespace nebula

namespace apache::thrift {
Expand All @@ -54,6 +55,7 @@ SPECIALIZE_CPP2OPS(nebula::LineString);
SPECIALIZE_CPP2OPS(nebula::Polygon);
SPECIALIZE_CPP2OPS(nebula::Geography);
SPECIALIZE_CPP2OPS(nebula::Duration);
SPECIALIZE_CPP2OPS(nebula::Vector);

} // namespace apache::thrift

Expand Down
115 changes: 115 additions & 0 deletions src/common/datatypes/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "common/datatypes/Map.h"
#include "common/datatypes/Path.h"
#include "common/datatypes/Set.h"
#include "common/datatypes/Vector.h"
#include "common/datatypes/Vertex.h"

namespace nebula {
Expand All @@ -32,6 +33,7 @@ const Value Value::kNullOverflow(NullType::ERR_OVERFLOW);
const Value Value::kNullUnknownProp(NullType::UNKNOWN_PROP);
const Value Value::kNullDivByZero(NullType::DIV_BY_ZERO);
const Value Value::kNullOutOfRange(NullType::OUT_OF_RANGE);
const Value Value::kVectorDimNotMatch(NullType::VEC_DIM_NOT_MATCH);

const uint64_t Value::kEmptyNullType = Value::Type::__EMPTY__ | Value::Type::NULLVALUE;
const uint64_t Value::kNumericType = Value::Type::INT | Value::Type::FLOAT;
Expand Down Expand Up @@ -117,6 +119,10 @@ Value::Value(const Value& rhs) : type_(Value::Type::__EMPTY__) {
setDU(rhs.value_.duVal);
break;
}
case Type::VECTOR: {
setVec(rhs.value_.vecVal);
break;
}
default: {
assert(false);
break;
Expand Down Expand Up @@ -300,6 +306,14 @@ Value::Value(std::unordered_map<std::string, Value>&& map) {
setM(std::make_unique<Map>(std::move(map)));
}

Value::Value(const Vector& v) {
setVec(std::make_unique<Vector>(v));
}

Value::Value(Vector&& v) {
setVec(std::make_unique<Vector>(std::move(v)));
}

const std::string& Value::typeName() const {
static const std::unordered_map<Type, std::string> typeNames = {
{Type::__EMPTY__, "__EMPTY__"},
Expand All @@ -320,6 +334,7 @@ const std::string& Value::typeName() const {
{Type::DATASET, "dataset"},
{Type::GEOGRAPHY, "geography"},
{Type::DURATION, "duration"},
{Type::VECTOR, "vector"},
};

static const std::unordered_map<NullType, std::string> nullTypes = {
Expand All @@ -330,6 +345,7 @@ const std::string& Value::typeName() const {
{NullType::ERR_OVERFLOW, "ERR_OVERFLOW"},
{NullType::UNKNOWN_PROP, "UNKNOWN_PROP"},
{NullType::DIV_BY_ZERO, "DIV_BY_ZERO"},
{NullType::VEC_DIM_NOT_MATCH, "VEC_DIM_NOT_MATCH"},
};

static const std::string unknownType = "__UNKNOWN__";
Expand Down Expand Up @@ -598,6 +614,21 @@ void Value::setDuration(std::unique_ptr<Duration>&& v) {
setDU(std::move(v));
}

void Value::setVector(const Vector& v) {
clear();
setVec(v);
}

void Value::setVector(Vector&& v) {
clear();
setVec(std::move(v));
}

void Value::setVector(std::unique_ptr<Vector>&& v) {
clear();
setVec(std::move(v));
}

const double& Value::getFloat() const {
CHECK_EQ(type_, Type::FLOAT);
return value_.fVal;
Expand Down Expand Up @@ -713,6 +744,16 @@ const Duration* Value::getDurationPtr() const {
return value_.duVal.get();
}

const Vector& Value::getVector() const {
CHECK_EQ(type_, Type::VECTOR);
return *(value_.vecVal);
}

const Vector* Value::getVectorPtr() const {
CHECK_EQ(type_, Type::VECTOR);
return value_.vecVal.get();
}

NullType& Value::mutableNull() {
CHECK_EQ(type_, Type::NULLVALUE);
return value_.nVal;
Expand Down Expand Up @@ -798,6 +839,11 @@ Duration& Value::mutableDuration() {
return *value_.duVal;
}

Vector& Value::mutableVector() {
CHECK_EQ(type_, Type::VECTOR);
return *(value_.vecVal);
}

NullType Value::moveNull() {
CHECK_EQ(type_, Type::NULLVALUE);
NullType v = std::move(value_.nVal);
Expand Down Expand Up @@ -917,6 +963,13 @@ Duration Value::moveDuration() {
return v;
}

Vector Value::moveVector() {
CHECK_EQ(type_, Type::VECTOR);
Vector v = std::move(*(value_.vecVal));
clear();
return v;
}

void Value::clearSlow() {
switch (type_) {
case Type::__EMPTY__: {
Expand Down Expand Up @@ -1002,6 +1055,10 @@ void Value::clearSlow() {
destruct(value_.duVal);
break;
}
case Type::VECTOR: {
destruct(value_.vecVal);
break;
}
}
type_ = Type::__EMPTY__;
}
Expand Down Expand Up @@ -1089,6 +1146,10 @@ Value& Value::operator=(Value&& rhs) noexcept {
setDU(std::move(rhs.value_.duVal));
break;
}
case Type::VECTOR: {
setVec(std::move(rhs.value_.vecVal));
break;
}
default: {
assert(false);
break;
Expand Down Expand Up @@ -1175,6 +1236,10 @@ Value& Value::operator=(const Value& rhs) {
setDU(rhs.value_.duVal);
break;
}
case Type::VECTOR: {
setVec(rhs.value_.vecVal);
break;
}
default: {
assert(false);
break;
Expand Down Expand Up @@ -1446,6 +1511,26 @@ void Value::setDU(Duration&& v) {
type_ = Type::DURATION;
}

void Value::setVec(const Vector& v) {
new (std::addressof(value_.vecVal)) std::unique_ptr<Vector>(new Vector(v));
type_ = Type::VECTOR;
}

void Value::setVec(Vector&& v) {
new (std::addressof(value_.vecVal)) std::unique_ptr<Vector>(new Vector(std::move(v)));
type_ = Type::VECTOR;
}

void Value::setVec(const std::unique_ptr<Vector>& v) {
new (std::addressof(value_.vecVal)) std::unique_ptr<Vector>(new Vector(*v));
type_ = Type::VECTOR;
}

void Value::setVec(std::unique_ptr<Vector>&& v) {
new (std::addressof(value_.vecVal)) std::unique_ptr<Vector>(std::move(v));
type_ = Type::VECTOR;
}

// Convert Nebula::Value to a value compatible with Json standard
// DATE, TIME, DATETIME will be converted to strings in UTC
// VERTEX, EDGES, PATH will be converted to objects
Expand Down Expand Up @@ -1511,6 +1596,9 @@ folly::dynamic Value::toJson() const {
}
case Value::Type::DURATION: {
return getDuration().toJson();
}
case Value::Type::VECTOR: {
return getVector().toJson();
}
// no default so the compiler will warning when lack
}
Expand Down Expand Up @@ -1588,6 +1676,8 @@ std::string Value::toString() const {
return "__NULL_UNKNOWN_PROP__";
case NullType::OUT_OF_RANGE:
return "__NULL_OUT_OF_RANGE__";
case NullType::VEC_DIM_NOT_MATCH:
return "__NULL_VEC_DIM_NOT_MATCH__";
}
DLOG(FATAL) << "Unknown Null type " << static_cast<int>(getNull());
return "__NULL_BAD_TYPE__";
Expand Down Expand Up @@ -1639,6 +1729,9 @@ std::string Value::toString() const {
}
case Value::Type::DURATION: {
return getDuration().toString();
}
case Value::Type::VECTOR: {
return getVector().toString();
}
// no default so the compiler will warning when lack
}
Expand Down Expand Up @@ -1861,6 +1954,7 @@ Value Value::lessThan(const Value& v) const {
// e.g. What is the result of `duration('P1M') < duration('P30D')`?
return kNullBadType;
}
case Value::Type::VECTOR:
case Value::Type::NULLVALUE:
case Value::Type::__EMPTY__: {
return kNullBadType;
Expand Down Expand Up @@ -1960,6 +2054,9 @@ Value Value::equal(const Value& v) const {
case Value::Type::DURATION: {
return getDuration() == v.getDuration();
}
case Value::Type::VECTOR: {
return getVector() == v.getVector();
}
case Value::Type::NULLVALUE:
case Value::Type::__EMPTY__: {
return false;
Expand Down Expand Up @@ -2054,6 +2151,9 @@ void swap(Value& a, Value& b) {
case Value::Type::DURATION: {
return "DURATION";
}
case Value::Type::VECTOR: {
return "VECTOR";
}
default: {
return "__UNKNOWN__";
}
Expand Down Expand Up @@ -2269,6 +2369,9 @@ Value operator+(const Value& lhs, const Value& rhs) {
case Value::Type::NULLVALUE: {
return Value::kNullValue;
}
case Value::Type::VECTOR: {
return Value::kNullBadType;
}
}
DLOG(FATAL) << "Unknown type: " << rhs.type();
return Value::kNullBadType;
Expand Down Expand Up @@ -2343,6 +2446,7 @@ Value operator+(const Value& lhs, const Value& rhs) {
}
}
}
case Value::Type::VECTOR:
default: {
return Value::kNullBadType;
}
Expand Down Expand Up @@ -2436,6 +2540,7 @@ Value operator-(const Value& lhs, const Value& rhs) {
}
}
}
case Value::Type::VECTOR:
default: {
return Value::kNullBadType;
}
Expand Down Expand Up @@ -2707,6 +2812,10 @@ bool operator<(const Value& lhs, const Value& rhs) {
DLOG(FATAL) << "Duration is not comparable.";
return false;
}
case Value::Type::VECTOR: {
DLOG(FATAL) << "Vector is not comparable.";
return false;
}
case Value::Type::NULLVALUE:
case Value::Type::__EMPTY__: {
return false;
Expand Down Expand Up @@ -2803,6 +2912,9 @@ bool Value::equals(const Value& rhs) const {
case Value::Type::DURATION: {
return getDuration() == rhs.getDuration();
}
case Value::Type::VECTOR: {
return getVector() == rhs.getVector();
}
case Value::Type::NULLVALUE:
case Value::Type::__EMPTY__: {
return false;
Expand Down Expand Up @@ -2865,6 +2977,9 @@ std::size_t Value::hash() const {
case Type::DURATION: {
return std::hash<Duration>()(getDuration());
}
case Type::VECTOR: {
return std::hash<Vector>()(getVector());
}
case Type::DATASET: {
DLOG(FATAL) << "Hash for DATASET has not been implemented";
break;
Expand Down
Loading