Skip to content

Commit

Permalink
Merge pull request #6738 from sundy-li/fix-values-parser
Browse files Browse the repository at this point in the history
fix(query): Fix values source `skip_to_next_row`
  • Loading branch information
BohuTANG authored Jul 22, 2022
2 parents 8f6b8ee + 9794c69 commit 3719228
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
10 changes: 3 additions & 7 deletions common/ast/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ pub enum TypeName {
Float32,
Float64,
Date,
DateTime { precision: Option<u64> },
Timestamp,
Timestamp { precision: Option<u64> },
String,
Array { item_type: Option<Box<TypeName>> },
Tuple { fields_type: Vec<TypeName> },
Expand Down Expand Up @@ -526,15 +525,12 @@ impl Display for TypeName {
TypeName::Date => {
write!(f, "DATE")?;
}
TypeName::DateTime { precision } => {
write!(f, "DATETIME")?;
TypeName::Timestamp { precision } => {
write!(f, "Timestamp")?;
if let Some(precision) = precision {
write!(f, "({})", *precision)?;
}
}
TypeName::Timestamp => {
write!(f, "TIMESTAMP")?;
}
TypeName::String => {
write!(f, "STRING")?;
}
Expand Down
6 changes: 2 additions & 4 deletions common/ast/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1145,12 +1145,11 @@ pub fn type_name(i: Input) -> IResult<TypeName> {
);
let ty_date = value(TypeName::Date, rule! { DATE });
let ty_datetime = map(
rule! { DATETIME ~ ( "(" ~ #literal_u64 ~ ")" )? },
|(_, opt_precision)| TypeName::DateTime {
rule! { (DATETIME | TIMESTAMP) ~ ( "(" ~ #literal_u64 ~ ")" )? },
|(_, opt_precision)| TypeName::Timestamp {
precision: opt_precision.map(|(_, precision, _)| precision),
},
);
let ty_timestamp = value(TypeName::Timestamp, rule! { TIMESTAMP });
let ty_string = value(
TypeName::String,
rule! { ( STRING | VARCHAR ) ~ ( "(" ~ #literal_u64 ~ ")" )? },
Expand All @@ -1174,7 +1173,6 @@ pub fn type_name(i: Input) -> IResult<TypeName> {
| #ty_tuple
| #ty_date
| #ty_datetime
| #ty_timestamp
| #ty_string
| #ty_object
| #ty_variant
Expand Down
6 changes: 3 additions & 3 deletions common/ast/tests/it/testdata/statement.txt
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ DropDatabase(
---------- Input ----------
create table c(a DateTime null, b DateTime(3));
---------- Output ---------
CREATE TABLE c (a DATETIME NULL, b DATETIME(3) NOT NULL)
CREATE TABLE c (a Timestamp NULL, b Timestamp(3) NOT NULL)
---------- AST ------------
CreateTable(
CreateTableStmt {
Expand All @@ -766,7 +766,7 @@ CreateTable(
span: Ident(15..16),
},
data_type: Nullable(
DateTime {
Timestamp {
precision: None,
},
),
Expand All @@ -779,7 +779,7 @@ CreateTable(
quote: None,
span: Ident(32..33),
},
data_type: DateTime {
data_type: Timestamp {
precision: Some(
3,
),
Expand Down
9 changes: 9 additions & 0 deletions query/src/sql/planner/binder/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ pub fn skip_to_next_row<R: BufferRead>(
let _ = reader.ignore_white_spaces()?;

let mut quoted = false;
let mut escaped = false;

while balance > 0 {
let buffer = reader.fill_buf()?;
Expand All @@ -373,8 +374,15 @@ pub fn skip_to_next_row<R: BufferRead>(
let c = buffer[it];
reader.consume(it + 1);

if it == 0 && escaped {
escaped = false;
continue;
}
escaped = false;

match c {
b'\\' => {
escaped = true;
continue;
}
b'\'' => {
Expand All @@ -394,6 +402,7 @@ pub fn skip_to_next_row<R: BufferRead>(
_ => {}
}
} else {
escaped = false;
reader.consume(size);
}
}
Expand Down
3 changes: 2 additions & 1 deletion query/src/sql/statements/value_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub fn skip_to_next_row<R: BufferRead>(
let c = buffer[it];
reader.consume(it + 1);

if it == 0 && escaped && c == b'\'' {
if it == 0 && escaped {
escaped = false;
continue;
}
Expand Down Expand Up @@ -213,6 +213,7 @@ pub fn skip_to_next_row<R: BufferRead>(
_ => {}
}
} else {
escaped = false;
reader.consume(size);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ desc db2.test7;


SELECT '====CREATE TRANSIENT TABLE====';
create transient table db2.test8(tiny TINYINT, tiny_unsigned TINYINT UNSIGNED, smallint SMALLINT, smallint_unsigned SMALLINT UNSIGNED, int INT, int_unsigned INT UNSIGNED, bigint BIGINT, bigint_unsigned BIGINT UNSIGNED,float FLOAT, double DOUBLE, date DATE, datetime DATETIME, ts TIMESTAMP, str VARCHAR default '3', bool BOOLEAN, arr ARRAY, obj OBJECT, variant VARIANT);
create transient table db2.test8(tiny TINYINT, tiny_unsigned TINYINT UNSIGNED, smallint SMALLINT, smallint_unsigned SMALLINT UNSIGNED, int INT, int_unsigned INT UNSIGNED, bigint BIGINT, bigint_unsigned BIGINT UNSIGNED,float FLOAT, double DOUBLE, date DATE, datetime DATETIME, ts TIMESTAMP(6), str VARCHAR default '3', bool BOOLEAN, arr ARRAY, obj OBJECT, variant VARIANT);
desc db2.test8;

-- clean up test databases
Expand Down

1 comment on commit 3719228

@vercel
Copy link

@vercel vercel bot commented on 3719228 Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

databend – ./

databend-git-main-databend.vercel.app
databend.vercel.app
databend-databend.vercel.app
databend.rs

Please sign in to comment.