Skip to content

Commit

Permalink
Fix insert value should put order properties #1215 (#1219)
Browse files Browse the repository at this point in the history
  • Loading branch information
laura-ding authored and dangleptr committed Nov 16, 2019
1 parent 9c857f0 commit 0bf7fc3
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 92 deletions.
25 changes: 12 additions & 13 deletions src/graph/Executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,20 @@ bool Executor::checkValueType(const nebula::cpp2::ValueType &type, const Variant
return false;
}

Status Executor::checkFieldName(std::shared_ptr<const meta::SchemaProviderIf> schema,
std::vector<std::string*> props) {
for (auto fieldIndex = 0u; fieldIndex < schema->getNumFields(); fieldIndex++) {
auto schemaFieldName = schema->getFieldName(fieldIndex);
if (UNLIKELY(nullptr == schemaFieldName)) {
return Status::Error("Invalid field index");
}
if (schemaFieldName != *props[fieldIndex]) {
LOG(ERROR) << "Field name is wrong, schema field " << schemaFieldName
<< ", input field " << *props[fieldIndex];
return Status::Error("Input field name `%s' is wrong",
props[fieldIndex]->c_str());
StatusOr<std::unordered_map<std::string, int64_t>>
Executor::checkFieldName(std::shared_ptr<const meta::SchemaProviderIf> schema,
std::vector<std::string*> props) {
std::unordered_map<std::string, int64_t> schemaIndexes;
auto pos = 0;
for (auto it : props) {
auto index = schema->getFieldIndex(*it);
if (index < 0) {
return Status::Error("Invalid field name `%s'", it->c_str());
}
schemaIndexes.emplace(*it, pos);
pos++;
}
return Status::OK();
return schemaIndexes;
}

StatusOr<int64_t> Executor::toTimestamp(const VariantType &value) {
Expand Down
5 changes: 3 additions & 2 deletions src/graph/Executor.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ class Executor : public cpp::NonCopyable, public cpp::NonMovable {

bool checkValueType(const nebula::cpp2::ValueType &type, const VariantType &value);

Status checkFieldName(std::shared_ptr<const meta::SchemaProviderIf> schema,
std::vector<std::string*> props);
StatusOr<std::unordered_map<std::string, int64_t>> checkFieldName(
std::shared_ptr<const meta::SchemaProviderIf> schema,
std::vector<std::string*> props);

StatusOr<int64_t> toTimestamp(const VariantType &value);

Expand Down
24 changes: 14 additions & 10 deletions src/graph/InsertEdgeExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ Status InsertEdgeExecutor::check() {
// Check field name
auto checkStatus = checkFieldName(schema_, props);
if (!checkStatus.ok()) {
status = checkStatus;
status = checkStatus.status();
break;
}

schemaIndexes_ = std::move(checkStatus).value();
} while (false);

if (!status.ok()) {
Expand Down Expand Up @@ -148,16 +150,18 @@ StatusOr<std::vector<storage::cpp2::Edge>> InsertEdgeExecutor::prepareEdges() {
}

RowWriter writer(schema_);
auto fieldIndex = 0u;
for (auto &value : values) {
auto iter = schema_->begin();
while (iter) {
// Check value type
auto schemaType = schema_->getFieldType(fieldIndex);
auto schemaType = iter->getType();
auto &value = values[schemaIndexes_[iter->getName()]];
if (!checkValueType(schemaType, value)) {
DCHECK(onError_);
LOG(ERROR) << "ValueType is wrong, schema type "
<< static_cast<int32_t>(schemaType.type)
<< ", input type " << value.which();
return Status::Error("ValueType is wrong");
auto *output = "ValueType is wrong, schema type [%d], "
"input type [%d], near `%s'";
auto error = folly::stringPrintf(output, static_cast<int32_t>(schemaType.type),
value.which(), iter->getName());
LOG(ERROR) << error;
return Status::Error(std::move(error));
}

if (schemaType.type == nebula::cpp2::SupportedType::TIMESTAMP) {
Expand All @@ -169,7 +173,7 @@ StatusOr<std::vector<storage::cpp2::Edge>> InsertEdgeExecutor::prepareEdges() {
} else {
writeVariantType(writer, value);
}
fieldIndex++;
++iter;
}
{
auto &out = edges[index++];
Expand Down
1 change: 1 addition & 0 deletions src/graph/InsertEdgeExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class InsertEdgeExecutor final : public Executor {
EdgeType edgeType_{0};
EdgeSchema schema_;
std::vector<EdgeRowItem*> rows_;
std::unordered_map<std::string, int64_t> schemaIndexes_;
};

} // namespace graph
Expand Down
31 changes: 19 additions & 12 deletions src/graph/InsertVertexExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ Status InsertVertexExecutor::check() {
// Check field name
auto checkStatus = checkFieldName(schema, props);
if (!checkStatus.ok()) {
LOG(ERROR) << checkStatus;
return checkStatus;
LOG(ERROR) << checkStatus.status();
return checkStatus.status();
}
schemaIndexes_.emplace_back(std::move(checkStatus).value());
}
return Status::OK();
}
Expand Down Expand Up @@ -138,17 +139,23 @@ StatusOr<std::vector<storage::cpp2::Vertex>> InsertVertexExecutor::prepareVertic
}

RowWriter writer(schema);
auto valueIndex = valuePos;
for (auto fieldIndex = 0u; fieldIndex < schema->getNumFields(); fieldIndex++) {
auto& value = values[valueIndex];

auto iter = schema->begin();
while (iter) {
// Check value type
auto schemaType = iter->getType();
uint32_t fieldIndex = schemaIndexes_[index][iter->getName()] + valuePos;
if (fieldIndex >= values.size()) {
return Status::Error("Wrong index of `%s'", iter->getName());
}
auto &value = values[fieldIndex];
// Check value type
auto schemaType = schema->getFieldType(fieldIndex);
if (!checkValueType(schemaType, value)) {
LOG(ERROR) << "ValueType is wrong, schema type "
<< static_cast<int32_t>(schemaType.type)
<< ", input type " << value.which();
return Status::Error("ValueType is wrong");
auto *output = "ValueType is wrong, schema type [%d], "
"input type [%d], near `%s'";
auto error = folly::stringPrintf(output, static_cast<int32_t>(schemaType.type),
value.which(), iter->getName());
LOG(ERROR) << error;
return Status::Error(std::move(error));
}
if (schemaType.type == nebula::cpp2::SupportedType::TIMESTAMP) {
auto timestamp = toTimestamp(value);
Expand All @@ -160,7 +167,7 @@ StatusOr<std::vector<storage::cpp2::Vertex>> InsertVertexExecutor::prepareVertic
writeVariantType(writer, value);
}

valueIndex++;
++iter;
}

tag.set_tag_id(tagId);
Expand Down
2 changes: 2 additions & 0 deletions src/graph/InsertVertexExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class InsertVertexExecutor final : public Executor {
std::vector<TagSchema> schemas_;
std::vector<std::vector<std::string*>> tagProps_;
GraphSpaceID spaceId_{-1};
using NameIndex = std::vector<std::unordered_map<std::string, int64_t>>;
NameIndex schemaIndexes_;
};

} // namespace graph
Expand Down
Loading

0 comments on commit 0bf7fc3

Please sign in to comment.