From 2e3c220fcf2ac5218715bef2fabb45952220e30b Mon Sep 17 00:00:00 2001 From: "kyle.cao" Date: Tue, 14 Sep 2021 16:12:29 +0800 Subject: [PATCH] t# This is a combination of 2 commits. fix insert format fix tck --- src/graph/validator/MutateValidator.cpp | 35 +++++++++++++++- src/graph/validator/MutateValidator.h | 1 + .../validator/test/MutateValidatorTest.cpp | 9 ++++- .../insert/InsertEdgeOnDiffParts.feature | 40 +++++++++++++++++++ tests/tck/features/schema/Schema.feature | 2 +- 5 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 tests/tck/features/insert/InsertEdgeOnDiffParts.feature diff --git a/src/graph/validator/MutateValidator.cpp b/src/graph/validator/MutateValidator.cpp index a99edceadb5..e4978aaefeb 100644 --- a/src/graph/validator/MutateValidator.cpp +++ b/src/graph/validator/MutateValidator.cpp @@ -148,7 +148,7 @@ Status InsertEdgesValidator::toPlan() { nullptr, spaceId_, std::move(edges_), - std::move(propNames_), + std::move(entirePropNames_), ifNotExists_, useChainInsert); root_ = doNode; @@ -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()) { @@ -233,6 +238,32 @@ Status InsertEdgesValidator::prepareEdges() { auto valsRet = SchemaUtil::toValueVec(row->values()); NG_RETURN_IF_ERROR(valsRet); auto props = std::move(valsRet).value(); + + std::vector 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 not null field doesn't have a default value."); + } + entirePropValues.emplace_back(Value(NullType::__NULL__)); + } + } else { + auto v = props[std::distance(propNames_.begin(), iter)]; + if (!field->nullable() && v.isNull()) { + return Status::SemanticError("The not null field cannot be null"); + } + entirePropValues.emplace_back(v); + } + } storage::cpp2::NewEdge edge; storage::cpp2::EdgeKey key; @@ -241,7 +272,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 diff --git a/src/graph/validator/MutateValidator.h b/src/graph/validator/MutateValidator.h index 8ebd2a876df..58464c32b74 100644 --- a/src/graph/validator/MutateValidator.h +++ b/src/graph/validator/MutateValidator.h @@ -58,6 +58,7 @@ class InsertEdgesValidator final : public Validator { EdgeType edgeType_{-1}; std::shared_ptr schema_; std::vector propNames_; + std::vector entirePropNames_; std::vector rows_; std::vector edges_; }; diff --git a/src/graph/validator/test/MutateValidatorTest.cpp b/src/graph/validator/test/MutateValidatorTest.cpp index b60a4562b05..a0677381f4b 100644 --- a/src/graph/validator/test/MutateValidatorTest.cpp +++ b/src/graph/validator/test/MutateValidatorTest.cpp @@ -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})); } } diff --git a/tests/tck/features/insert/InsertEdgeOnDiffParts.feature b/tests/tck/features/insert/InsertEdgeOnDiffParts.feature new file mode 100644 index 00000000000..040c3c803eb --- /dev/null +++ b/tests/tck/features/insert/InsertEdgeOnDiffParts.feature @@ -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 vertex and edge if not exists test + 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 diff --git a/tests/tck/features/schema/Schema.feature b/tests/tck/features/schema/Schema.feature index 46cb9e011de..742060ba5b4 100644 --- a/tests/tck/features/schema/Schema.feature +++ b/tests/tck/features/schema/Schema.feature @@ -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 not null field doesn't have a default value. # test alter edge with timestamp default When executing query: """