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

Correc the alter default value dont't affected. #2003

Merged
merged 17 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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
58 changes: 58 additions & 0 deletions src/graph/SchemaHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ Status SchemaHelper::alterSchema(const std::vector<AlterSchemaOptItem*>& schemaO
const std::vector<SchemaPropItem*>& schemaProps,
std::vector<nebula::meta::cpp2::AlterSchemaItem>& options,
nebula::cpp2::SchemaProp& prop) {
Getters g;
for (auto& schemaOpt : schemaOpts) {
nebula::meta::cpp2::AlterSchemaItem schemaItem;
auto opType = schemaOpt->toType();
Expand All @@ -209,6 +210,63 @@ Status SchemaHelper::alterSchema(const std::vector<AlterSchemaOptItem*>& schemaO
nebula::cpp2::ColumnDef column;
column.name = *spec->name();
column.type.type = columnTypeToSupportedType(spec->type());
if (spec->hasDefault()) {
Shylock-Hg marked this conversation as resolved.
Show resolved Hide resolved
nebula::cpp2::Value v;
switch (spec->type()) {
case ColumnType::TIMESTAMP: {
auto valStatus = spec->getIntValue(g);
if (valStatus.ok()) {
v.set_timestamp(valStatus.value());
column.set_default_value(v);
} else {
return std::move(valStatus).status();
}
break;
}
case ColumnType::INT: {
auto valStatus = spec->getIntValue(g);
if (valStatus.ok()) {
v.set_int_value(valStatus.value());
column.set_default_value(v);
} else {
return std::move(valStatus).status();
}
break;
}
case ColumnType::BOOL: {
auto valStatus = spec->getBoolValue(g);
if (valStatus.ok()) {
v.set_bool_value(valStatus.value());
column.set_default_value(v);
} else {
return std::move(valStatus).status();
}
break;
}
case ColumnType::DOUBLE: {
auto valStatus = spec->getDoubleValue(g);
if (valStatus.ok()) {
v.set_double_value(valStatus.value());
column.set_default_value(v);
} else {
return std::move(valStatus).status();
}
break;
}
case ColumnType::STRING: {
auto valStatus = spec->getStringValue(g);
if (valStatus.ok()) {
v.set_string_value(std::move(valStatus).value());
column.set_default_value(std::move(v));
} else {
return std::move(valStatus).status();
}
break;
}
default:
LOG(WARNING) << "Unkown type " << static_cast<int>(spec->type());
Shylock-Hg marked this conversation as resolved.
Show resolved Hide resolved
} // switch
}
schema.columns.emplace_back(std::move(column));
}
}
Expand Down
151 changes: 151 additions & 0 deletions src/graph/test/SchemaTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,43 @@ class SchemaTest : public TestBase {
}
};

class SchemaTestIssue1987 : public TestBase, public ::testing::WithParamInterface<std::string> {
protected:
void SetUp() override {
TestBase::SetUp();
client_ = gEnv->getClient();
ASSERT_NE(nullptr, client_);
setupSchema();
}

void TearDown() override {
{
cpp2::ExecutionResponse resp;
std::string cmd = "DROP SPACE issue1987";
auto code = client_->execute(cmd, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
TestBase::TearDown();
}

void setupSchema() {
{
cpp2::ExecutionResponse resp;
std::string cmd = "CREATE SPACE issue1987";
auto code = client_->execute(cmd, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
{
cpp2::ExecutionResponse resp;
std::string cmd = "USE issue1987";
auto code = client_->execute(cmd, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
}

std::unique_ptr<GraphClient> client_{nullptr};
};

TEST_F(SchemaTest, TestComment) {
auto client = gEnv->getClient();
ASSERT_NE(nullptr, client);
Expand Down Expand Up @@ -1074,5 +1111,119 @@ TEST_F(SchemaTest, TestTagAndEdge) {
LOG(FATAL) << "Space still exists after sleep " << retry << " seconds";
}

TEST_P(SchemaTestIssue1987, issue1987) {
{
cpp2::ExecutionResponse resp;
std::string query = "CREATE " + GetParam()
+ " t(name string default \"N/A\", age int default -1)";
auto code = client_->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
// ADD
{
cpp2::ExecutionResponse resp;
std::string query = "ALTER " + GetParam() + " t ADD (description string default \"none\");";
auto code = client_->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
::sleep(FLAGS_heartbeat_interval_secs + 1);
}
std::string ve;
std::string entity;
if (GetParam() == "TAG") {
ve = "VERTEX";
entity = "1";
} else if (GetParam() == "EDGE") {
ve = "EDGE";
entity = "1->2";
} else {
ASSERT_TRUE(false) << "Invalid parameter " << GetParam();
}
{
cpp2::ExecutionResponse resp;
std::string query = "INSERT " + ve + " t() VALUES " + entity + ":()";
auto code = client_->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
std::unordered_set<uint16_t> ignore;
if (GetParam() == "TAG") {
ignore.emplace(0);
} else if (GetParam() == "EDGE") {
ignore.emplace(0);
ignore.emplace(1);
ignore.emplace(2);
} else {
ASSERT_TRUE(false) << "Invalid parameter " << GetParam();
}
{
std::vector<std::tuple<std::string, int64_t, std::string>> result {
{"N/A", -1, "none"}
};
cpp2::ExecutionResponse resp;
std::string query = std::string() + "FETCH PROP ON " + "t " + entity;
auto code = client_->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
ASSERT_TRUE(verifyResult(resp, result, true, ignore));
}

// CHANGE
{
cpp2::ExecutionResponse resp;
std::string query = "ALTER " + GetParam()
+ " t CHANGE (description string default \"NONE\")";
auto code = client_->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
::sleep(FLAGS_heartbeat_interval_secs + 1);
}
{
cpp2::ExecutionResponse resp;
std::string query = "INSERT " + ve + " t() VALUES " + entity + ":()";
auto code = client_->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
{
std::vector<std::tuple<std::string, int64_t, std::string>> result {
{"N/A", -1, "NONE"}
};
cpp2::ExecutionResponse resp;
std::string query = std::string() + "FETCH PROP ON " + "t " + entity;
auto code = client_->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
ASSERT_TRUE(verifyResult(resp, result, true, ignore));
}

// DROP
{
cpp2::ExecutionResponse resp;
std::string query = "ALTER " + GetParam() + " t CHANGE (description string)";
auto code = client_->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
::sleep(FLAGS_heartbeat_interval_secs + 1);
}
{
cpp2::ExecutionResponse resp;
std::string query = "INSERT " + ve + " t(description) VALUES " + entity + ":(\"some one\")";
auto code = client_->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
}
{
std::vector<std::tuple<std::string, int64_t, std::string>> result {
{"N/A", -1, "some one"}
};
cpp2::ExecutionResponse resp;
std::string query = std::string() + "FETCH PROP ON " + "t " + entity;
auto code = client_->execute(query, resp);
ASSERT_EQ(cpp2::ErrorCode::SUCCEEDED, code);
ASSERT_TRUE(verifyResult(resp, result, true, ignore));
}
{
cpp2::ExecutionResponse resp;
std::string query = "INSERT " + ve + " t() VALUES " + entity + ":()";
auto code = client_->execute(query, resp);
ASSERT_NE(cpp2::ErrorCode::SUCCEEDED, code);
}
}

INSTANTIATE_TEST_CASE_P(SchemaIssue1987, SchemaTestIssue1987, ::testing::Values("TAG", "EDGE"));

} // namespace graph
} // namespace nebula
93 changes: 77 additions & 16 deletions src/meta/MetaServiceUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,68 @@ std::string MetaServiceUtils::assembleSegmentKey(const std::string& segment,

cpp2::ErrorCode MetaServiceUtils::alterColumnDefs(std::vector<nebula::cpp2::ColumnDef>& cols,
nebula::cpp2::SchemaProp& prop,
std::vector<kvstore::KV>& defaultKVs,
std::vector<std::string>& removeDefaultKeys,
const GraphSpaceID spaceId,
const TagID Id,
const nebula::cpp2::ColumnDef col,
const cpp2::AlterSchemaOp op) {
static_assert(std::is_same<TagID, EdgeType>::value, "");
auto name = col.get_name();
auto dKey = MetaServiceUtils::defaultKey(spaceId, Id, name);
std::string defaultValue;
if (col.__isset.default_value) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here can encapsulate as a function, and where need it, call it, don't need judge twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default will be refactor in #2011

auto value = col.get_default_value();
switch (col.get_type().get_type()) {
case nebula::cpp2::SupportedType::BOOL:
if (value->getType() != nebula::cpp2::Value::Type::bool_value) {
LOG(ERROR) << "Handle Column Failed: " << name
<< " type mismatch";
return cpp2::ErrorCode::E_CONFLICT;
}
defaultValue = folly::to<std::string>(value->get_bool_value());
break;
case nebula::cpp2::SupportedType::INT:
if (value->getType() != nebula::cpp2::Value::Type::int_value) {
LOG(ERROR) << "Handle Column Failed: " << name
<< " type mismatch";
return cpp2::ErrorCode::E_CONFLICT;
}
defaultValue = folly::to<std::string>(value->get_int_value());
break;
case nebula::cpp2::SupportedType::DOUBLE:
if (value->getType() != nebula::cpp2::Value::Type::double_value) {
LOG(ERROR) << "Handle Column Failed: " << name
<< " type mismatch";
return cpp2::ErrorCode::E_CONFLICT;
}
defaultValue = folly::to<std::string>(value->get_double_value());
break;
case nebula::cpp2::SupportedType::STRING:
if (value->getType() != nebula::cpp2::Value::Type::string_value) {
LOG(ERROR) << "Handle Column Failed: " << name
<< " type mismatch";
return cpp2::ErrorCode::E_CONFLICT;
}
defaultValue = folly::to<std::string>(value->get_string_value());
break;
case nebula::cpp2::SupportedType::TIMESTAMP:
if (value->getType() != nebula::cpp2::Value::Type::timestamp) {
LOG(ERROR) << "Handle Column Failed: " << name
<< " type mismatch";
return cpp2::ErrorCode::E_CONFLICT;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why return E_CONFLICT

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow the previous.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous may be an error, this data type or parameter is wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow the behavior that create the tag/edge.

}
defaultValue = folly::to<std::string>(value->get_timestamp());
break;
default:
LOG(ERROR) << "Unsupported type";
return cpp2::ErrorCode::E_CONFLICT;
}

LOG(INFO) << "Get Tag Default value: Property Name " << name
<< ", Value " << defaultValue;
}

switch (op) {
case cpp2::AlterSchemaOp::ADD:
for (auto it = cols.begin(); it != cols.end(); ++it) {
Expand All @@ -434,6 +494,9 @@ cpp2::ErrorCode MetaServiceUtils::alterColumnDefs(std::vector<nebula::cpp2::Colu
}
}
cols.emplace_back(std::move(col));
if (col.__isset.default_value) {
defaultKVs.emplace_back(std::move(dKey), std::move(defaultValue));
}
return cpp2::ErrorCode::SUCCEEDED;
case cpp2::AlterSchemaOp::CHANGE:
for (auto it = cols.begin(); it != cols.end(); ++it) {
Expand All @@ -445,6 +508,11 @@ cpp2::ErrorCode MetaServiceUtils::alterColumnDefs(std::vector<nebula::cpp2::Colu
return cpp2::ErrorCode::E_UNSUPPORTED;
}
*it = col;
if (col.__isset.default_value) {
defaultKVs.emplace_back(std::move(dKey), std::move(defaultValue));
} else {
removeDefaultKeys.emplace_back(std::move(dKey));
}
return cpp2::ErrorCode::SUCCEEDED;
}
}
Expand All @@ -459,6 +527,9 @@ cpp2::ErrorCode MetaServiceUtils::alterColumnDefs(std::vector<nebula::cpp2::Colu
prop.set_ttl_col("");
}
cols.erase(it);
if (it->__isset.default_value) {
removeDefaultKeys.emplace_back(std::move(dKey));
}
return cpp2::ErrorCode::SUCCEEDED;
}
}
Expand Down Expand Up @@ -614,26 +685,16 @@ std::string MetaServiceUtils::parseRoleStr(folly::StringPiece key) {
return role;
}

std::string MetaServiceUtils::tagDefaultKey(GraphSpaceID spaceId,
TagID tag,
const std::string& field) {
std::string MetaServiceUtils::defaultKey(GraphSpaceID spaceId,
TagID id,
const std::string& field) {
// Assume edge/tag default value key is same formated
static_assert(std::is_same<TagID, EdgeType>::value, "");
std::string key;
key.reserve(kDefaultTable.size() + sizeof(GraphSpaceID) + sizeof(TagID));
key.append(kDefaultTable.data(), kDefaultTable.size())
.append(reinterpret_cast<const char*>(&spaceId), sizeof(GraphSpaceID))
.append(reinterpret_cast<const char*>(&tag), sizeof(TagID))
.append(field);
return key;
}

std::string MetaServiceUtils::edgeDefaultKey(GraphSpaceID spaceId,
EdgeType edge,
const std::string& field) {
std::string key;
key.reserve(kDefaultTable.size() + sizeof(GraphSpaceID) + sizeof(EdgeType));
key.append(kDefaultTable.data(), kDefaultTable.size())
.append(reinterpret_cast<const char*>(&spaceId), sizeof(GraphSpaceID))
.append(reinterpret_cast<const char*>(&edge), sizeof(EdgeType))
.append(reinterpret_cast<const char*>(&id), sizeof(TagID))
.append(field);
return key;
}
Expand Down
Loading