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

Support duration default value. #3610

Merged
Merged
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
290 changes: 148 additions & 142 deletions src/meta/processors/schema/SchemaUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "meta/processors/schema/SchemaUtil.h"

#include "common/base/ObjectPool.h"
#include "common/expression/ConstantExpression.h"
#include "common/time/TimeUtils.h"
#include "common/utils/DefaultValueContext.h"
Expand Down Expand Up @@ -38,152 +37,159 @@ bool SchemaUtil::checkType(std::vector<cpp2::ColumnDef>& columns) {
}
continue;
}
switch (column.get_type().get_type()) {
case PropertyType::BOOL:
if (!value.isBool()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type();
return false;
}
break;
case PropertyType::INT8: {
if (!value.isInt()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type();
return false;
}
if (!checkType(pool, name, column, value)) {
return false;
}
}
}
return true;
}

auto v = value.getInt();
if (v > std::numeric_limits<int8_t>::max() || v < std::numeric_limits<int8_t>::min()) {
LOG(ERROR) << "`" << name << "' out of rang";
return false;
}
break;
}
case PropertyType::INT16: {
if (!value.isInt()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type();
return false;
}
/*static*/ bool SchemaUtil::checkType(ObjectPool* pool,
const std::string& name,
cpp2::ColumnDef& column,
const Value& value) {
switch (column.get_type().get_type()) {
case PropertyType::BOOL:
if (!value.isBool()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}
return true;
case PropertyType::INT8: {
if (!value.isInt()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}

auto v = value.getInt();
if (v > std::numeric_limits<int16_t>::max() || v < std::numeric_limits<int16_t>::min()) {
LOG(ERROR) << "`" << name << "' out of rang";
return false;
}
break;
}
case PropertyType::INT32: {
if (!value.isInt()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type();
return false;
}
auto v = value.getInt();
if (v > std::numeric_limits<int8_t>::max() || v < std::numeric_limits<int8_t>::min()) {
LOG(ERROR) << "`" << name << "' out of rang";
return false;
}
return true;
}
case PropertyType::INT16: {
if (!value.isInt()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}

auto v = value.getInt();
if (v > std::numeric_limits<int32_t>::max() || v < std::numeric_limits<int32_t>::min()) {
LOG(ERROR) << "`" << name << "' out of rang";
return false;
}
break;
}
case PropertyType::INT64:
if (!value.isInt()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type();
return false;
}
break;
case PropertyType::FLOAT:
case PropertyType::DOUBLE:
if (!value.isFloat()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type();
return false;
}
break;
case PropertyType::STRING:
if (!value.isStr()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type();
return false;
}
break;
case PropertyType::FIXED_STRING: {
if (!value.isStr()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type();
return false;
}
auto& colType = column.get_type();
size_t typeLen = colType.type_length_ref().value_or(0);
if (value.getStr().size() > typeLen) {
const auto trimStr = value.getStr().substr(0, typeLen);
value.setStr(trimStr);
const auto& fixedValue = *ConstantExpression::make(pool, value);
column.default_value_ref() = Expression::encode(fixedValue);
}
break;
}
case PropertyType::TIMESTAMP: {
if (!value.isInt()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type();
return false;
}
auto ret = time::TimeUtils::toTimestamp(value);
if (!ret.ok()) {
LOG(ERROR) << ret.status();
return false;
}
break;
}
case PropertyType::DATE:
if (!value.isDate()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type();
return false;
}
break;
case PropertyType::TIME:
if (!value.isTime()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type();
return false;
}
break;
case PropertyType::DATETIME:
if (!value.isDateTime()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type();
return false;
}
break;
case PropertyType::GEOGRAPHY: {
if (!value.isGeography()) { // TODO(jie)
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type();
return false;
}
meta::cpp2::GeoShape columnGeoShape =
column.get_type().geo_shape_ref().value_or(meta::cpp2::GeoShape::ANY);
GeoShape defaultExprGeoShape = value.getGeography().shape();
if (columnGeoShape != meta::cpp2::GeoShape::ANY &&
folly::to<uint32_t>(columnGeoShape) != folly::to<uint32_t>(defaultExprGeoShape)) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is "
<< value.type() << ", geo shape is " << defaultExprGeoShape;
return false;
}
break;
}
default:
LOG(ERROR) << "Unsupported type";
return false;
auto v = value.getInt();
if (v > std::numeric_limits<int16_t>::max() || v < std::numeric_limits<int16_t>::min()) {
LOG(ERROR) << "`" << name << "' out of rang";
return false;
}
return true;
}
}
return true;
case PropertyType::INT32: {
if (!value.isInt()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}

auto v = value.getInt();
if (v > std::numeric_limits<int32_t>::max() || v < std::numeric_limits<int32_t>::min()) {
LOG(ERROR) << "`" << name << "' out of rang";
return false;
}
return true;
}
case PropertyType::INT64:
if (!value.isInt()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}
return true;
case PropertyType::FLOAT:
case PropertyType::DOUBLE:
if (!value.isFloat()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}
return true;
case PropertyType::STRING:
if (!value.isStr()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}
return true;
case PropertyType::FIXED_STRING: {
if (!value.isStr()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}
auto& colType = column.get_type();
size_t typeLen = colType.type_length_ref().value_or(0);
if (value.getStr().size() > typeLen) {
const auto trimStr = value.getStr().substr(0, typeLen);
const auto& fixedValue = *ConstantExpression::make(pool, Value(trimStr));
column.default_value_ref() = Expression::encode(fixedValue);
}
return true;
}
case PropertyType::TIMESTAMP: {
if (!value.isInt()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}
auto ret = time::TimeUtils::toTimestamp(value);
if (!ret.ok()) {
LOG(ERROR) << ret.status();
return false;
}
return true;
}
case PropertyType::DATE:
if (!value.isDate()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}
return true;
case PropertyType::TIME:
if (!value.isTime()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}
return true;
case PropertyType::DATETIME:
if (!value.isDateTime()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}
return true;
case PropertyType::DURATION:
if (!value.isDuration()) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}
return true;
case PropertyType::GEOGRAPHY: {
if (!value.isGeography()) { // TODO(jie)
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type();
return false;
}
meta::cpp2::GeoShape columnGeoShape =
column.get_type().geo_shape_ref().value_or(meta::cpp2::GeoShape::ANY);
GeoShape defaultExprGeoShape = value.getGeography().shape();
if (columnGeoShape != meta::cpp2::GeoShape::ANY &&
folly::to<uint32_t>(columnGeoShape) != folly::to<uint32_t>(defaultExprGeoShape)) {
LOG(ERROR) << "Invalid default value for ` " << name << "', value type is " << value.type()
<< ", geo shape is " << defaultExprGeoShape;
return false;
}
return true;
}
case PropertyType::UNKNOWN:
case PropertyType::VID:
DLOG(ERROR) << "Don't supported type "
<< apache::thrift::util::enumNameSafe(column.get_type().get_type());
return false;
// no default so compiler will warning when lack
} // switch
DLOG(FATAL) << "Unknown property type " << static_cast<int>(column.get_type().get_type());
return false;
}

} // namespace meta
} // namespace nebula
7 changes: 7 additions & 0 deletions src/meta/processors/schema/SchemaUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef META_SCHEMAUTIL_H_
#define META_SCHEMAUTIL_H_

#include "common/base/ObjectPool.h"
#include "meta/processors/BaseProcessor.h"

namespace nebula {
Expand All @@ -17,6 +18,12 @@ class SchemaUtil final {

public:
static bool checkType(std::vector<cpp2::ColumnDef> &columns);

private:
static bool checkType(ObjectPool *pool,
const std::string &name,
cpp2::ColumnDef &column,
const Value &value);
};

} // namespace meta
Expand Down
23 changes: 23 additions & 0 deletions tests/tck/features/bugfix/CreateDurationAsDefaultValue.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2021 vesoft inc. All rights reserved.
#
# This source code is licensed under Apache 2.0 License.
Feature: Create duration as default value

Scenario: Create duration as default value
Given an empty graph
And create a space with following options:
| partition_num | 9 |
| replica_factor | 1 |
| vid_type | FIXED_STRING(30) |
| charset | utf8 |
| collate | utf8_bin |
When executing query:
"""
create tag ddl_tag1(col1 DURATION DEFAULT duration({years: 3, months: 2}));
"""
Then the execution should be successful
When executing query:
"""
create edge ddl_edge1(col1 DURATION DEFAULT duration({years: 3, months: 2}));
"""
Then the execution should be successful