Skip to content

Commit

Permalink
fix: fix default value cannot accept negative number (#3217)
Browse files Browse the repository at this point in the history
* fix: fix default value cannot accept negative number

* chore: apply suggestions from CR
  • Loading branch information
WenyXu authored Jan 23, 2024
1 parent 364754a commit 007b63d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/sql/src/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use datatypes::types::{cast, TimestampType};
use datatypes::value::{OrderedF32, OrderedF64, Value};
pub use option_map::OptionMap;
use snafu::{ensure, OptionExt, ResultExt};
use sqlparser::ast::ExactNumberInfo;
use sqlparser::ast::{ExactNumberInfo, UnaryOperator};
pub use transform::{get_data_type_by_alias_name, transform_statements};

use crate::ast::{
Expand Down Expand Up @@ -302,6 +302,16 @@ fn parse_column_default_constraint(
ColumnDefaultConstraint::Function(func.to_lowercase())
}
ColumnOption::Default(expr) => {
if let Expr::UnaryOp { op, expr } = expr {
if let (UnaryOperator::Minus, Expr::Value(SqlValue::Number(n, _))) =
(op, expr.as_ref())
{
return Ok(Some(ColumnDefaultConstraint::Value(sql_number_to_value(
data_type,
&format!("-{n}"),
)?)));
}
}
return UnsupportedDefaultValueSnafu {
column_name,
expr: expr.clone(),
Expand Down
17 changes: 17 additions & 0 deletions tests/cases/standalone/common/create/create.result
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,20 @@ CREATE TABLE test_multiple_inline_pk_definitions ("timestamp" TIMESTAMP TIME IND

Error: 1004(InvalidArguments), Illegal primary keys definition: not allowed to inline multiple primary keys in columns options

CREATE TABLE neg_default_value(i INT DEFAULT -1024, ts TIMESTAMP TIME INDEX);

Affected Rows: 0

desc TABLE neg_default_value;

+--------+----------------------+-----+------+---------+---------------+
| Column | Type | Key | Null | Default | Semantic Type |
+--------+----------------------+-----+------+---------+---------------+
| i | Int32 | | YES | -1024 | FIELD |
| ts | TimestampMillisecond | PRI | NO | | TIMESTAMP |
+--------+----------------------+-----+------+---------+---------------+

DROP TABLE neg_default_value;

Affected Rows: 0

5 changes: 5 additions & 0 deletions tests/cases/standalone/common/create/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,8 @@ CREATE TABLE test_multiple_pk_definitions ("timestamp" TIMESTAMP TIME INDEX, hos

CREATE TABLE test_multiple_inline_pk_definitions ("timestamp" TIMESTAMP TIME INDEX, host STRING PRIMARY KEY, "value" DOUBLE PRIMARY KEY);

CREATE TABLE neg_default_value(i INT DEFAULT -1024, ts TIMESTAMP TIME INDEX);

desc TABLE neg_default_value;

DROP TABLE neg_default_value;
20 changes: 20 additions & 0 deletions tests/cases/standalone/common/insert/insert.result
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,23 @@ DROP TABLE presentations;

Affected Rows: 0

CREATE TABLE neg_default_value(i INT DEFAULT -1024, ts TIMESTAMP TIME INDEX);

Affected Rows: 0

INSERT INTO neg_default_value(ts) values ('2000-01-01 00:00:00+00:00');

Affected Rows: 1

SELECT * FROM neg_default_value;

+-------+---------------------+
| i | ts |
+-------+---------------------+
| -1024 | 2000-01-01T00:00:00 |
+-------+---------------------+

DROP TABLE neg_default_value;

Affected Rows: 0

8 changes: 8 additions & 0 deletions tests/cases/standalone/common/insert/insert.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,11 @@ insert into presentations values (1, 'Patrick Damme', 'Analytical Query Processi
DROP TABLE integers;

DROP TABLE presentations;

CREATE TABLE neg_default_value(i INT DEFAULT -1024, ts TIMESTAMP TIME INDEX);

INSERT INTO neg_default_value(ts) values ('2000-01-01 00:00:00+00:00');

SELECT * FROM neg_default_value;

DROP TABLE neg_default_value;

0 comments on commit 007b63d

Please sign in to comment.