Skip to content

Commit

Permalink
feat: support current_timestamp and now as default constrains (#2690)
Browse files Browse the repository at this point in the history
Signed-off-by: Ruihang Xia <[email protected]>
  • Loading branch information
waynexia authored Nov 3, 2023
1 parent 68f92ec commit 7323d72
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/datatypes/src/schema/constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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(),
}
}
Expand Down Expand Up @@ -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(),
}
}
Expand Down
28 changes: 28 additions & 0 deletions tests/cases/standalone/common/create/current_timestamp.result
Original file line number Diff line number Diff line change
@@ -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

11 changes: 11 additions & 0 deletions tests/cases/standalone/common/create/current_timestamp.sql
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 7323d72

Please sign in to comment.