Skip to content

Commit 04c9fba

Browse files
parkma99alamb
andauthored
update parse STRICT tables (#903)
Co-authored-by: Andrew Lamb <[email protected]>
1 parent 8877cba commit 04c9fba

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
@@ -1316,6 +1316,10 @@ pub enum Statement {
13161316
/// than empty (represented as ()), the latter meaning "no sorting".
13171317
/// <https://clickhouse.com/docs/en/sql-reference/statements/create/table/>
13181318
order_by: Option<Vec<Ident>>,
1319+
/// SQLite "STRICT" clause.
1320+
/// if the "STRICT" table-option keyword is added to the end, after the closing ")",
1321+
/// then strict typing rules apply to that table.
1322+
strict: bool,
13191323
},
13201324
/// SQLite's `CREATE VIRTUAL TABLE .. USING <module_name> (<module_args>)`
13211325
CreateVirtualTable {
@@ -2219,6 +2223,7 @@ impl fmt::Display for Statement {
22192223
on_commit,
22202224
on_cluster,
22212225
order_by,
2226+
strict,
22222227
} => {
22232228
// We want to allow the following options
22242229
// Empty column list, allowed by PostgreSQL:
@@ -2387,7 +2392,9 @@ impl fmt::Display for Statement {
23872392
};
23882393
write!(f, " {on_commit}")?;
23892394
}
2390-
2395+
if *strict {
2396+
write!(f, " STRICT")?;
2397+
}
23912398
Ok(())
23922399
}
23932400
Statement::CreateVirtualTable {

src/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ define_keywords!(
551551
STDOUT,
552552
STORAGE_INTEGRATION,
553553
STORED,
554+
STRICT,
554555
STRING,
555556
SUBMULTISET,
556557
SUBSTRING,

src/parser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3485,6 +3485,7 @@ impl<'a> Parser<'a> {
34853485
None
34863486
};
34873487

3488+
let strict = self.parse_keyword(Keyword::STRICT);
34883489
Ok(CreateTableBuilder::new(table_name)
34893490
.temporary(temporary)
34903491
.columns(columns)
@@ -3507,6 +3508,7 @@ impl<'a> Parser<'a> {
35073508
.collation(collation)
35083509
.on_commit(on_commit)
35093510
.on_cluster(on_cluster)
3511+
.strict(strict)
35103512
.build())
35113513
}
35123514

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)