mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-05-30 11:50:38 +00:00
feat: support current_timestamp and now as default constrains (#2690)
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
This commit is contained in:
@@ -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(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
tests/cases/standalone/common/create/current_timestamp.sql
Normal file
11
tests/cases/standalone/common/create/current_timestamp.sql
Normal 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;
|
||||
Reference in New Issue
Block a user