From 8f3b299a45a577b79d891f7f04457cb6f87686ff Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Tue, 14 Nov 2023 11:22:23 +0800 Subject: [PATCH] fix: Normalize default constrain fn name (#2737) * fix: normalize current_timestamp to current_timestamp() Signed-off-by: Ruihang Xia * add sqlness case Signed-off-by: Ruihang Xia * fix clippy lints Signed-off-by: Ruihang Xia --------- Signed-off-by: Ruihang Xia --- src/datatypes/src/schema.rs | 2 +- src/datatypes/src/schema/constraint.rs | 6 +-- src/sql/src/error.rs | 1 + src/sql/src/statements.rs | 8 +++- .../common/create/current_timestamp.result | 48 +++++++++++++++++++ .../common/create/current_timestamp.sql | 6 +++ 6 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/datatypes/src/schema.rs b/src/datatypes/src/schema.rs index 972b02e822..6b170e3abb 100644 --- a/src/datatypes/src/schema.rs +++ b/src/datatypes/src/schema.rs @@ -13,7 +13,7 @@ // limitations under the License. mod column_schema; -mod constraint; +pub mod constraint; mod raw; use std::collections::HashMap; diff --git a/src/datatypes/src/schema/constraint.rs b/src/datatypes/src/schema/constraint.rs index b9c568cab6..c236d09769 100644 --- a/src/datatypes/src/schema/constraint.rs +++ b/src/datatypes/src/schema/constraint.rs @@ -24,9 +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_FN: &str = "current_timestamp()"; -const NOW_FN: &str = "now()"; +pub const CURRENT_TIMESTAMP: &str = "current_timestamp"; +pub const CURRENT_TIMESTAMP_FN: &str = "current_timestamp()"; +pub const NOW_FN: &str = "now()"; /// Column's default constraint. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] diff --git a/src/sql/src/error.rs b/src/sql/src/error.rs index b78aa55c75..50df979e80 100644 --- a/src/sql/src/error.rs +++ b/src/sql/src/error.rs @@ -63,6 +63,7 @@ pub enum Error { Syntax { #[snafu(source)] error: ParserError, + location: Location, }, #[snafu(display("Missing time index constraint"))] diff --git a/src/sql/src/statements.rs b/src/sql/src/statements.rs index d7b26fb589..69d3b987ec 100644 --- a/src/sql/src/statements.rs +++ b/src/sql/src/statements.rs @@ -37,6 +37,7 @@ use common_base::bytes::Bytes; use common_query::AddColumnLocation; use common_time::Timestamp; use datatypes::prelude::ConcreteDataType; +use datatypes::schema::constraint::{CURRENT_TIMESTAMP, CURRENT_TIMESTAMP_FN}; use datatypes::schema::{ColumnDefaultConstraint, ColumnSchema, COMMENT_KEY}; use datatypes::types::cast::CastOption; use datatypes::types::{cast, TimestampType}; @@ -270,8 +271,13 @@ fn parse_column_default_constraint( ColumnDefaultConstraint::Value(sql_value_to_value(column_name, data_type, v)?) } ColumnOption::Default(Expr::Function(func)) => { + let mut func = format!("{func}").to_lowercase(); + // normalize CURRENT_TIMESTAMP to CURRENT_TIMESTAMP() + if func == CURRENT_TIMESTAMP { + func = CURRENT_TIMESTAMP_FN.to_string(); + } // Always use lowercase for function expression - ColumnDefaultConstraint::Function(format!("{func}").to_lowercase()) + ColumnDefaultConstraint::Function(func.to_lowercase()) } ColumnOption::Default(expr) => { return UnsupportedDefaultValueSnafu { diff --git a/tests/cases/standalone/common/create/current_timestamp.result b/tests/cases/standalone/common/create/current_timestamp.result index b4f9c905a3..def4fbe5dd 100644 --- a/tests/cases/standalone/common/create/current_timestamp.result +++ b/tests/cases/standalone/common/create/current_timestamp.result @@ -2,14 +2,62 @@ create table t1 (ts timestamp time index default CURRENT_TIMESTAMP); Affected Rows: 0 +show create table t1; + ++-------+-----------------------------------------------------------+ +| Table | Create Table | ++-------+-----------------------------------------------------------+ +| t1 | CREATE TABLE IF NOT EXISTS "t1" ( | +| | "ts" TIMESTAMP(3) NOT NULL DEFAULT current_timestamp(), | +| | TIME INDEX ("ts") | +| | ) | +| | | +| | ENGINE=mito | +| | WITH( | +| | regions = 1 | +| | ) | ++-------+-----------------------------------------------------------+ + create table t2 (ts timestamp time index default currEnt_tImEsTamp()); Affected Rows: 0 +show create table t2; + ++-------+-----------------------------------------------------------+ +| Table | Create Table | ++-------+-----------------------------------------------------------+ +| t2 | CREATE TABLE IF NOT EXISTS "t2" ( | +| | "ts" TIMESTAMP(3) NOT NULL DEFAULT current_timestamp(), | +| | TIME INDEX ("ts") | +| | ) | +| | | +| | ENGINE=mito | +| | WITH( | +| | regions = 1 | +| | ) | ++-------+-----------------------------------------------------------+ + create table t3 (ts timestamp time index default now()); Affected Rows: 0 +show create table t3; + ++-------+---------------------------------------------+ +| Table | Create Table | ++-------+---------------------------------------------+ +| t3 | CREATE TABLE IF NOT EXISTS "t3" ( | +| | "ts" TIMESTAMP(3) NOT NULL DEFAULT now(), | +| | TIME INDEX ("ts") | +| | ) | +| | | +| | ENGINE=mito | +| | WITH( | +| | regions = 1 | +| | ) | ++-------+---------------------------------------------+ + 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 diff --git a/tests/cases/standalone/common/create/current_timestamp.sql b/tests/cases/standalone/common/create/current_timestamp.sql index 0c73ae1e73..d73d9dceba 100644 --- a/tests/cases/standalone/common/create/current_timestamp.sql +++ b/tests/cases/standalone/common/create/current_timestamp.sql @@ -1,9 +1,15 @@ create table t1 (ts timestamp time index default CURRENT_TIMESTAMP); +show create table t1; + create table t2 (ts timestamp time index default currEnt_tImEsTamp()); +show create table t2; + create table t3 (ts timestamp time index default now()); +show create table t3; + create table t4 (ts timestamp time index default now); drop table t1;