Skip to content

Commit

Permalink
fix edge insert (#2862)
Browse files Browse the repository at this point in the history
  • Loading branch information
czpmango committed Sep 22, 2021
1 parent 371b011 commit 9a4cea3
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
37 changes: 35 additions & 2 deletions src/graph/validator/MutateValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Status InsertEdgesValidator::toPlan() {
nullptr,
spaceId_,
std::move(edges_),
std::move(propNames_),
std::move(entirePropNames_),
ifNotExists_,
useChainInsert);
root_ = doNode;
Expand Down Expand Up @@ -196,6 +196,11 @@ Status InsertEdgesValidator::prepareEdges() {
auto useToss = isoLevel == IsoLevel::TOSS;
auto size = useToss ? rows_.size() : rows_.size() * 2;
edges_.reserve(size);

size_t fieldNum = schema_->getNumFields();
for (size_t j = 0; j < fieldNum; ++j) {
entirePropNames_.emplace_back(schema_->field(j)->name());
}
for (auto i = 0u; i < rows_.size(); i++) {
auto *row = rows_[i];
if (propNames_.size() != row->values().size()) {
Expand Down Expand Up @@ -233,6 +238,34 @@ Status InsertEdgesValidator::prepareEdges() {
auto valsRet = SchemaUtil::toValueVec(row->values());
NG_RETURN_IF_ERROR(valsRet);
auto props = std::move(valsRet).value();

std::vector<Value> entirePropValues;
for (size_t j = 0; j < fieldNum; ++j) {
auto *field = schema_->field(j);
auto propName = entirePropNames_[j];
auto iter = std::find(propNames_.begin(), propNames_.end(), propName);
if (iter == propNames_.end()) {
if (field->hasDefault()) {
auto *defaultValue = field->defaultValue();
DCHECK(!!defaultValue);
auto v = defaultValue->eval(QueryExpressionContext()(nullptr));
entirePropValues.emplace_back(v);
} else {
if (!field->nullable()) {
return Status::SemanticError(
"The property `%s' is not nullable and has no default value.", field->name());
}
entirePropValues.emplace_back(Value(NullType::__NULL__));
}
} else {
auto v = props[std::distance(propNames_.begin(), iter)];
if (!field->nullable() && v.isNull()) {
return Status::SemanticError("The non-nullable property `%s' could not be NULL.",
field->name());
}
entirePropValues.emplace_back(v);
}
}
storage::cpp2::NewEdge edge;
storage::cpp2::EdgeKey key;

Expand All @@ -241,7 +274,7 @@ Status InsertEdgesValidator::prepareEdges() {
key.set_edge_type(edgeType_);
key.set_ranking(rank);
edge.set_key(key);
edge.set_props(std::move(props));
edge.set_props(std::move(entirePropValues));
edges_.emplace_back(edge);
if (!useToss) {
// inbound
Expand Down
1 change: 1 addition & 0 deletions src/graph/validator/MutateValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class InsertEdgesValidator final : public Validator {
EdgeType edgeType_{-1};
std::shared_ptr<const meta::SchemaProviderIf> schema_;
std::vector<std::string> propNames_;
std::vector<std::string> entirePropNames_;
std::vector<EdgeRowItem*> rows_;
std::vector<storage::cpp2::NewEdge> edges_;
};
Expand Down
9 changes: 8 additions & 1 deletion src/graph/validator/test/MutateValidatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,18 @@ TEST_F(MutateValidatorTest, InsertEdgeTest) {
ASSERT_FALSE(checkResult(cmd, {}));
}
// vid use function call
{
auto cmd =
"INSERT EDGE like(start, end, likeness) VALUES lower(\"Lily\")->\"Tom\":(2010, "
"2020, 90);";
ASSERT_TRUE(checkResult(cmd, {PK::kInsertEdges, PK::kStart}));
}
// vid use function call
{
auto cmd =
"INSERT EDGE like(start, end) VALUES lower(\"Lily\")->\"Tom\":(2010, "
"2020);";
ASSERT_TRUE(checkResult(cmd, {PK::kInsertEdges, PK::kStart}));
ASSERT_FALSE(checkResult(cmd, {PK::kInsertEdges, PK::kStart}));
}
}

Expand Down
40 changes: 40 additions & 0 deletions tests/tck/features/insert/InsertEdgeOnDiffParts.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) 2021 vesoft inc. All rights reserved.
#
# This source code is licensed under Apache 2.0 License,
# attached with Common Clause Condition 1.0, found in the LICENSES directory.
Feature: Insert vertex and edge with if not exists

Scenario: insert edge with default value
Given an empty graph
And create a space with following options:
| partition_num | 9 |
| replica_factor | 1 |
| vid_type | FIXED_STRING(20) |
And having executed:
"""
CREATE TAG IF NOT EXISTS V();
CREATE EDGE IF NOT EXISTS E(rank timestamp default timestamp());
"""
When try to execute query:
"""
INSERT VERTEX V() VALUES "v1":()
"""
Then the execution should be successful
When try to execute query:
"""
INSERT VERTEX V() VALUES "v2":()
"""
Then the execution should be successful
When try to execute query:
"""
INSERT EDGE E() VALUES "v1"->"v2":()
"""
Then the execution should be successful
When executing query:
"""
(GO FROM "v1" over E yield E.rank union GO FROM "v2" over E REVERSELY yield E.rank) | yield count(*) AS count
"""
Then the result should be, in any order:
| count |
| 1 |
And drop the used space
2 changes: 1 addition & 1 deletion tests/tck/features/schema/Schema.feature
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ Feature: Insert string vid of vertex and edge
"""
INSERT EDGE e() VALUES "1"->"2":()
"""
Then a ExecutionError should be raised at runtime: Storage Error: The not null field doesn't have a default value.
Then a SemanticError should be raised at runtime: The property `description' is not nullable and has no default value.
# test alter edge with timestamp default
When executing query:
"""
Expand Down

0 comments on commit 9a4cea3

Please sign in to comment.