Skip to content

Commit ad52c83

Browse files
parkma99alamb
authored andcommitted
update parse STRICT tables (apache#903)
Co-authored-by: Andrew Lamb <[email protected]>
1 parent f4a1fe8 commit ad52c83

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

src/ast/helpers/stmt_create_table.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub struct CreateTableBuilder {
7070
pub on_commit: Option<OnCommit>,
7171
pub on_cluster: Option<String>,
7272
pub order_by: Option<Vec<Ident>>,
73+
pub strict: bool,
7374
}
7475

7576
impl CreateTableBuilder {
@@ -100,6 +101,7 @@ impl CreateTableBuilder {
100101
on_commit: None,
101102
on_cluster: None,
102103
order_by: None,
104+
strict: false,
103105
}
104106
}
105107
pub fn or_replace(mut self, or_replace: bool) -> Self {
@@ -220,6 +222,11 @@ impl CreateTableBuilder {
220222
self
221223
}
222224

225+
pub fn strict(mut self, strict: bool) -> Self {
226+
self.strict = strict;
227+
self
228+
}
229+
223230
pub fn build(self) -> Statement {
224231
Statement::CreateTable {
225232
or_replace: self.or_replace,
@@ -247,6 +254,7 @@ impl CreateTableBuilder {
247254
on_commit: self.on_commit,
248255
on_cluster: self.on_cluster,
249256
order_by: self.order_by,
257+
strict: self.strict,
250258
}
251259
}
252260
}
@@ -284,6 +292,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
284292
on_commit,
285293
on_cluster,
286294
order_by,
295+
strict,
287296
} => Ok(Self {
288297
or_replace,
289298
temporary,
@@ -310,6 +319,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
310319
on_commit,
311320
on_cluster,
312321
order_by,
322+
strict,
313323
}),
314324
_ => Err(ParserError::ParserError(format!(
315325
"Expected create table statement, but received: {stmt}"

src/ast/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,10 @@ pub enum Statement {
14121412
/// than empty (represented as ()), the latter meaning "no sorting".
14131413
/// <https://clickhouse.com/docs/en/sql-reference/statements/create/table/>
14141414
order_by: Option<Vec<Ident>>,
1415+
/// SQLite "STRICT" clause.
1416+
/// if the "STRICT" table-option keyword is added to the end, after the closing ")",
1417+
/// then strict typing rules apply to that table.
1418+
strict: bool,
14151419
},
14161420
/// SQLite's `CREATE VIRTUAL TABLE .. USING <module_name> (<module_args>)`
14171421
CreateVirtualTable {
@@ -2409,6 +2413,7 @@ impl fmt::Display for Statement {
24092413
on_commit,
24102414
on_cluster,
24112415
order_by,
2416+
strict,
24122417
} => {
24132418
// We want to allow the following options
24142419
// Empty column list, allowed by PostgreSQL:
@@ -2577,7 +2582,9 @@ impl fmt::Display for Statement {
25772582
};
25782583
write!(f, " {on_commit}")?;
25792584
}
2580-
2585+
if *strict {
2586+
write!(f, " STRICT")?;
2587+
}
25812588
Ok(())
25822589
}
25832590
Statement::CreateVirtualTable {

src/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ define_keywords!(
563563
STDOUT,
564564
STORAGE_INTEGRATION,
565565
STORED,
566+
STRICT,
566567
STRING,
567568
SUBMULTISET,
568569
SUBSTRING,

src/parser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3521,6 +3521,7 @@ impl<'a> Parser<'a> {
35213521
None
35223522
};
35233523

3524+
let strict = self.parse_keyword(Keyword::STRICT);
35243525
Ok(CreateTableBuilder::new(table_name)
35253526
.temporary(temporary)
35263527
.columns(columns)
@@ -3543,6 +3544,7 @@ impl<'a> Parser<'a> {
35433544
.collation(collation)
35443545
.on_commit(on_commit)
35453546
.on_cluster(on_cluster)
3547+
.strict(strict)
35463548
.build())
35473549
}
35483550

tests/sqlparser_sqlite.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,15 @@ fn parse_similar_to() {
242242
chk(true);
243243
}
244244

245+
#[test]
246+
fn parse_create_table_with_strict() {
247+
let sql = "CREATE TABLE Fruits (id TEXT NOT NULL PRIMARY KEY) STRICT";
248+
if let Statement::CreateTable { name, strict, .. } = sqlite().verified_stmt(sql) {
249+
assert_eq!(name.to_string(), "Fruits");
250+
assert!(strict);
251+
}
252+
}
253+
245254
fn sqlite() -> TestedDialects {
246255
TestedDialects {
247256
dialects: vec![Box::new(SQLiteDialect {})],

0 commit comments

Comments
 (0)