feat: support current_timestamp and now as default constrains (#2690)

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
This commit is contained in:
Ruihang Xia
2023-11-03 16:32:02 +08:00
committed by GitHub
parent 68f92ecf08
commit 7323d727c9
3 changed files with 49 additions and 4 deletions

View File

@@ -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(),
}
}

View File

@@ -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

View File

@@ -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;