From 7323d727c9adf38b26a1fc2d097f16600aaad68b Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Fri, 3 Nov 2023 16:32:02 +0800 Subject: [PATCH] feat: support current_timestamp and now as default constrains (#2690) Signed-off-by: Ruihang Xia --- src/datatypes/src/schema/constraint.rs | 14 +++++++--- .../common/create/current_timestamp.result | 28 +++++++++++++++++++ .../common/create/current_timestamp.sql | 11 ++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 tests/cases/standalone/common/create/current_timestamp.result create mode 100644 tests/cases/standalone/common/create/current_timestamp.sql diff --git a/src/datatypes/src/schema/constraint.rs b/src/datatypes/src/schema/constraint.rs index e0dacbd5f7..b9c568cab6 100644 --- a/src/datatypes/src/schema/constraint.rs +++ b/src/datatypes/src/schema/constraint.rs @@ -24,7 +24,9 @@ use crate::value::Value; use crate::vectors::operations::VectorOp; use crate::vectors::{TimestampMillisecondVector, VectorRef}; -const CURRENT_TIMESTAMP: &str = "current_timestamp()"; +const CURRENT_TIMESTAMP: &str = "current_timestamp"; +const CURRENT_TIMESTAMP_FN: &str = "current_timestamp()"; +const NOW_FN: &str = "now()"; /// Column's default constraint. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] @@ -77,7 +79,7 @@ impl ColumnDefaultConstraint { match self { ColumnDefaultConstraint::Function(expr) => { ensure!( - expr == CURRENT_TIMESTAMP, + expr == CURRENT_TIMESTAMP || expr == CURRENT_TIMESTAMP_FN || expr == NOW_FN, error::UnsupportedDefaultExprSnafu { expr } ); ensure!( @@ -130,7 +132,9 @@ impl ColumnDefaultConstraint { match &expr[..] { // TODO(dennis): we only supports current_timestamp right now, // it's better to use a expression framework in future. - CURRENT_TIMESTAMP => create_current_timestamp_vector(data_type, num_rows), + CURRENT_TIMESTAMP | CURRENT_TIMESTAMP_FN | NOW_FN => { + create_current_timestamp_vector(data_type, num_rows) + } _ => error::UnsupportedDefaultExprSnafu { expr }.fail(), } } @@ -160,7 +164,9 @@ impl ColumnDefaultConstraint { // Functions should also ensure its return value is not null when // is_nullable is true. match &expr[..] { - CURRENT_TIMESTAMP => create_current_timestamp(data_type), + CURRENT_TIMESTAMP | CURRENT_TIMESTAMP_FN | NOW_FN => { + create_current_timestamp(data_type) + } _ => error::UnsupportedDefaultExprSnafu { expr }.fail(), } } diff --git a/tests/cases/standalone/common/create/current_timestamp.result b/tests/cases/standalone/common/create/current_timestamp.result new file mode 100644 index 0000000000..b4f9c905a3 --- /dev/null +++ b/tests/cases/standalone/common/create/current_timestamp.result @@ -0,0 +1,28 @@ +create table t1 (ts timestamp time index default CURRENT_TIMESTAMP); + +Affected Rows: 0 + +create table t2 (ts timestamp time index default currEnt_tImEsTamp()); + +Affected Rows: 0 + +create table t3 (ts timestamp time index default now()); + +Affected Rows: 0 + +create table t4 (ts timestamp time index default now); + +Error: 1001(Unsupported), Unsupported expr in default constraint: Identifier(Ident { value: "now", quote_style: None }) for column: ts + +drop table t1; + +Affected Rows: 0 + +drop table t2; + +Affected Rows: 0 + +drop table t3; + +Affected Rows: 0 + diff --git a/tests/cases/standalone/common/create/current_timestamp.sql b/tests/cases/standalone/common/create/current_timestamp.sql new file mode 100644 index 0000000000..0c73ae1e73 --- /dev/null +++ b/tests/cases/standalone/common/create/current_timestamp.sql @@ -0,0 +1,11 @@ +create table t1 (ts timestamp time index default CURRENT_TIMESTAMP); + +create table t2 (ts timestamp time index default currEnt_tImEsTamp()); + +create table t3 (ts timestamp time index default now()); + +create table t4 (ts timestamp time index default now); + +drop table t1; +drop table t2; +drop table t3;