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

Geo spatial: Geography #2830

Closed
wants to merge 8 commits into from
Closed
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
1 change: 1 addition & 0 deletions cmake/nebula/GeneralCompilerConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_compile_options(-Woverloaded-virtual)
add_compile_options(-Wignored-qualifiers)

add_definitions(-DS2_USE_GLOG)
add_definitions(-DS2_USE_GFLAGS)

include_directories(AFTER ${CMAKE_SOURCE_DIR}/src)
include_directories(AFTER ${CMAKE_CURRENT_BINARY_DIR}/src)
Expand Down
10 changes: 8 additions & 2 deletions src/clients/meta/MetaClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ bool MetaClient::loadSchemas(GraphSpaceID spaceId,
bool hasDef = col.default_value_ref().has_value();
auto& colType = col.get_type();
size_t len = colType.type_length_ref().has_value() ? *colType.get_type_length() : 0;
cpp2::GeoShape geoShape =
colType.geo_shape_ref().has_value() ? *colType.get_geo_shape() : cpp2::GeoShape::ANY;
bool nullable = col.nullable_ref().has_value() ? *col.get_nullable() : false;
Expression* defaultValueExpr = nullptr;
if (hasDef) {
Expand All @@ -353,8 +355,12 @@ bool MetaClient::loadSchemas(GraphSpaceID spaceId,
}
}

schema->addField(
col.get_name(), colType.get_type(), len, nullable, hasDef ? defaultValueExpr : nullptr);
schema->addField(col.get_name(),
colType.get_type(),
len,
nullable,
hasDef ? defaultValueExpr : nullptr,
geoShape);
};

for (auto& tagIt : tagItemVec) {
Expand Down
11 changes: 11 additions & 0 deletions src/codec/RowReaderV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@ Value RowReaderV2::getValueByIndex(const int64_t index) const noexcept {
dt.microsec = microsec;
return dt;
}
case meta::cpp2::PropertyType::GEOGRAPHY: {
int32_t strOffset;
int32_t strLen;
memcpy(reinterpret_cast<void*>(&strOffset), &data_[offset], sizeof(int32_t));
memcpy(reinterpret_cast<void*>(&strLen), &data_[offset + sizeof(int32_t)], sizeof(int32_t));
if (static_cast<size_t>(strOffset) == data_.size() && strLen == 0) {
return Geography();
}
CHECK_LT(strOffset, data_.size());
return Geography(std::string(&data_[strOffset], strLen));
}
case meta::cpp2::PropertyType::UNKNOWN:
break;
}
Expand Down
19 changes: 19 additions & 0 deletions src/codec/RowWriterV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ RowWriterV2::RowWriterV2(RowReader& reader) : RowWriterV2(reader.getSchema()) {
case Value::Type::DATETIME:
set(i, v.moveDateTime());
break;
case Value::Type::GEOGRAPHY:
set(i, v.moveGeography());
break;
default:
LOG(FATAL) << "Invalid data: " << v << ", type: " << v.typeName();
}
Expand Down Expand Up @@ -203,6 +206,8 @@ WriteResult RowWriterV2::setValue(ssize_t index, const Value& val) noexcept {
return write(index, val.getTime());
case Value::Type::DATETIME:
return write(index, val.getDateTime());
case Value::Type::GEOGRAPHY:
return write(index, val.getGeography());
default:
return WriteResult::TYPE_MISMATCH;
}
Expand Down Expand Up @@ -637,6 +642,7 @@ WriteResult RowWriterV2::write(ssize_t index, folly::StringPiece v) noexcept {
auto field = schema_->field(index);
auto offset = headerLen_ + numNullBytes_ + field->offset();
switch (field->type()) {
case meta::cpp2::PropertyType::GEOGRAPHY: // write wkb
case meta::cpp2::PropertyType::STRING: {
if (isSet_[index]) {
// The string value has already been set, we need to turn it
Expand Down Expand Up @@ -755,6 +761,16 @@ WriteResult RowWriterV2::write(ssize_t index, const DateTime& v) noexcept {
return WriteResult::SUCCEEDED;
}

WriteResult RowWriterV2::write(ssize_t index, const Geography& v) noexcept {
auto field = schema_->field(index);
auto geoShape = field->geoShape();
if (geoShape != meta::cpp2::GeoShape::ANY &&
folly::to<uint32_t>(geoShape) != folly::to<uint32_t>(v.shape())) {
return WriteResult::TYPE_MISMATCH;
}
return write(index, folly::StringPiece(v.wkb));
}

WriteResult RowWriterV2::checkUnsetFields() noexcept {
DefaultValueContext expCtx;
for (size_t i = 0; i < schema_->getNumFields(); i++) {
Expand Down Expand Up @@ -794,6 +810,9 @@ WriteResult RowWriterV2::checkUnsetFields() noexcept {
case Value::Type::DATETIME:
r = write(i, defVal.getDateTime());
break;
case Value::Type::GEOGRAPHY:
r = write(i, defVal.getGeography());
break;
default:
LOG(FATAL) << "Unsupported default value type: " << defVal.typeName()
<< ", default value: " << defVal
Expand Down
3 changes: 3 additions & 0 deletions src/codec/RowWriterV2.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ enum class WriteResult {
TIMESTAMP (8 bytes)
DATE (4 bytes)
DATETIME (15 bytes)
GEOGRAPHY (8 bytes) *

All except STRING typed properties are stored in-place. The STRING property
stored the offset of the string content in the first 4 bytes and the length
Expand Down Expand Up @@ -188,6 +189,8 @@ class RowWriterV2 {
WriteResult write(ssize_t index, const Date& v) noexcept;
WriteResult write(ssize_t index, const Time& v) noexcept;
WriteResult write(ssize_t index, const DateTime& v) noexcept;

WriteResult write(ssize_t index, const Geography& v) noexcept;
};

} // namespace nebula
Expand Down
1 change: 1 addition & 0 deletions src/codec/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(CODEC_TEST_LIBS
$<TARGET_OBJECTS:process_obj>
$<TARGET_OBJECTS:expression_obj>
$<TARGET_OBJECTS:function_manager_obj>
$<TARGET_OBJECTS:wkt_parser_obj>
$<TARGET_OBJECTS:agg_function_manager_obj>
$<TARGET_OBJECTS:time_utils_obj>
$<TARGET_OBJECTS:version_obj>
Expand Down
8 changes: 6 additions & 2 deletions src/codec/test/ResultSchemaProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ ResultSchemaProvider::ResultSchemaField::ResultSchemaField(std::string name,
bool nullable,
int32_t offset,
size_t nullFlagPos,
Expression* defaultValue)
Expression* defaultValue,
meta::cpp2::GeoShape geoShape)
: name_(std::move(name)),
type_(type),
size_(size),
nullable_(nullable),
offset_(offset),
nullFlagPos_(nullFlagPos),
defaultValue_(defaultValue) {}
defaultValue_(defaultValue),
geoShape_(geoShape) {}

const char* ResultSchemaProvider::ResultSchemaField::name() const { return name_.c_str(); }

Expand All @@ -50,6 +52,8 @@ size_t ResultSchemaProvider::ResultSchemaField::offset() const { return offset_;

size_t ResultSchemaProvider::ResultSchemaField::nullFlagPos() const { return nullFlagPos_; }

meta::cpp2::GeoShape ResultSchemaProvider::ResultSchemaField::geoShape() const { return geoShape_; }

/***********************************
*
* ResultSchemaProvider
Expand Down
5 changes: 4 additions & 1 deletion src/codec/test/ResultSchemaProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class ResultSchemaProvider : public meta::SchemaProviderIf {
bool nullable,
int32_t offset,
size_t nullFlagPos,
Expression* defaultValue = nullptr);
Expression* defaultValue = nullptr,
meta::cpp2::GeoShape = meta::cpp2::GeoShape::ANY);

const char* name() const override;
meta::cpp2::PropertyType type() const override;
Expand All @@ -32,6 +33,7 @@ class ResultSchemaProvider : public meta::SchemaProviderIf {
size_t size() const override;
size_t offset() const override;
size_t nullFlagPos() const override;
meta::cpp2::GeoShape geoShape() const override;

private:
std::string name_;
Expand All @@ -41,6 +43,7 @@ class ResultSchemaProvider : public meta::SchemaProviderIf {
int32_t offset_;
size_t nullFlagPos_;
Expression* defaultValue_;
meta::cpp2::GeoShape geoShape_;
};

public:
Expand Down
9 changes: 7 additions & 2 deletions src/codec/test/SchemaWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ SchemaWriter& SchemaWriter::appendCol(folly::StringPiece name,
PropertyType type,
int32_t fixedStrLen,
bool nullable,
Expression* defaultValue) noexcept {
Expression* defaultValue,
meta::cpp2::GeoShape geoShape) noexcept {
using folly::hash::SpookyHashV2;
uint64_t hash = SpookyHashV2::Hash64(name.data(), name.size(), 0);
DCHECK(nameIndex_.find(hash) == nameIndex_.end());
Expand Down Expand Up @@ -75,6 +76,9 @@ SchemaWriter& SchemaWriter::appendCol(folly::StringPiece name,
case PropertyType::DATETIME:
size = sizeof(int16_t) + 5 * sizeof(int8_t) + sizeof(int32_t);
break;
case PropertyType::GEOGRAPHY:
size = 2 * sizeof(int32_t); // as same as STRING
break;
default:
LOG(FATAL) << "Unknown column type";
}
Expand All @@ -84,7 +88,8 @@ SchemaWriter& SchemaWriter::appendCol(folly::StringPiece name,
nullFlagPos = numNullableFields_++;
}

columns_.emplace_back(name.toString(), type, size, nullable, offset, nullFlagPos, defaultValue);
columns_.emplace_back(
name.toString(), type, size, nullable, offset, nullFlagPos, defaultValue, geoShape);
nameIndex_.emplace(std::make_pair(hash, columns_.size() - 1));

return *this;
Expand Down
3 changes: 2 additions & 1 deletion src/codec/test/SchemaWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class SchemaWriter : public ResultSchemaProvider {
meta::cpp2::PropertyType type,
int32_t fixedStrLen = 0,
bool nullable = false,
Expression* defaultValue = nullptr) noexcept;
Expression* defaultValue = nullptr,
meta::cpp2::GeoShape geoShape = meta::cpp2::GeoShape::ANY) noexcept;

private:
};
Expand Down
1 change: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ nebula_add_subdirectory(graph)
nebula_add_subdirectory(plugin)
nebula_add_subdirectory(utils)
nebula_add_subdirectory(ssl)
nebula_add_subdirectory(geo)
2 changes: 1 addition & 1 deletion src/common/base/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

#include "common/base/Logging.h"

#define MUST_USE_RESULT __attribute__((warn_unused_result))
#define NG_MUST_USE_RESULT __attribute__((warn_unused_result))
#define DONT_OPTIMIZE __attribute__((optimize("O0")))

#define ALWAYS_INLINE __attribute__((always_inline))
Expand Down
25 changes: 13 additions & 12 deletions src/common/conf/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class Configuration final {
/**
* Parse from a file
*/
Status MUST_USE_RESULT parseFromFile(const std::string &filename);
Status NG_MUST_USE_RESULT parseFromFile(const std::string &filename);
/**
* Parse from a string buffer
*/
Status MUST_USE_RESULT parseFromString(const std::string &content);
Status NG_MUST_USE_RESULT parseFromString(const std::string &content);

std::string dumpToString() const;

Expand All @@ -42,19 +42,20 @@ class Configuration final {
* @key item key
* @val to hold the item value.
*/
Status MUST_USE_RESULT fetchAsInt(const char *key, int64_t &val) const;
Status MUST_USE_RESULT fetchAsDouble(const char *key, double &val) const;
Status MUST_USE_RESULT fetchAsBool(const char *key, bool &val) const;
Status MUST_USE_RESULT fetchAsString(const char *key, std::string &val) const;
Status NG_MUST_USE_RESULT fetchAsInt(const char *key, int64_t &val) const;
Status NG_MUST_USE_RESULT fetchAsDouble(const char *key, double &val) const;
Status NG_MUST_USE_RESULT fetchAsBool(const char *key, bool &val) const;
Status NG_MUST_USE_RESULT fetchAsString(const char *key, std::string &val) const;

Status MUST_USE_RESULT fetchAsIntArray(const char *key, std::vector<int64_t> &val) const;
Status MUST_USE_RESULT fetchAsDoubleArray(const char *key, std::vector<double> &val) const;
Status MUST_USE_RESULT fetchAsBoolArray(const char *key, std::vector<bool> &val) const;
Status MUST_USE_RESULT fetchAsStringArray(const char *key, std::vector<std::string> &val) const;
Status NG_MUST_USE_RESULT fetchAsIntArray(const char *key, std::vector<int64_t> &val) const;
Status NG_MUST_USE_RESULT fetchAsDoubleArray(const char *key, std::vector<double> &val) const;
Status NG_MUST_USE_RESULT fetchAsBoolArray(const char *key, std::vector<bool> &val) const;
Status NG_MUST_USE_RESULT fetchAsStringArray(const char *key,
std::vector<std::string> &val) const;

Status MUST_USE_RESULT fetchAsSubConf(const char *key, Configuration &val) const;
Status NG_MUST_USE_RESULT fetchAsSubConf(const char *key, Configuration &val) const;

Status MUST_USE_RESULT upsertStringField(const char *key, const std::string &val);
Status NG_MUST_USE_RESULT upsertStringField(const char *key, const std::string &val);

// Iterate through every key in the configuration
Status forEachKey(std::function<void(const std::string &)> processor) const;
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 @@ -14,6 +14,7 @@ nebula_add_library(
Map.cpp
List.cpp
Set.cpp
Geography.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 @@ -24,6 +24,7 @@ struct Map;
struct Set;
struct List;
struct DataSet;
struct Geography;
} // namespace nebula

namespace apache::thrift {
Expand All @@ -43,6 +44,7 @@ SPECIALIZE_CPP2OPS(nebula::Map);
SPECIALIZE_CPP2OPS(nebula::Set);
SPECIALIZE_CPP2OPS(nebula::List);
SPECIALIZE_CPP2OPS(nebula::DataSet);
SPECIALIZE_CPP2OPS(nebula::Geography);

} // namespace apache::thrift

Expand Down
Loading