update parse STRICT tables (#903)

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
parkma99 2023-06-23 22:48:04 +08:00 committed by GitHub
parent 8877cbafa6
commit 04c9fbaead
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 1 deletions

View file

@ -70,6 +70,7 @@ pub struct CreateTableBuilder {
pub on_commit: Option<OnCommit>, pub on_commit: Option<OnCommit>,
pub on_cluster: Option<String>, pub on_cluster: Option<String>,
pub order_by: Option<Vec<Ident>>, pub order_by: Option<Vec<Ident>>,
pub strict: bool,
} }
impl CreateTableBuilder { impl CreateTableBuilder {
@ -100,6 +101,7 @@ impl CreateTableBuilder {
on_commit: None, on_commit: None,
on_cluster: None, on_cluster: None,
order_by: None, order_by: None,
strict: false,
} }
} }
pub fn or_replace(mut self, or_replace: bool) -> Self { pub fn or_replace(mut self, or_replace: bool) -> Self {
@ -220,6 +222,11 @@ impl CreateTableBuilder {
self self
} }
pub fn strict(mut self, strict: bool) -> Self {
self.strict = strict;
self
}
pub fn build(self) -> Statement { pub fn build(self) -> Statement {
Statement::CreateTable { Statement::CreateTable {
or_replace: self.or_replace, or_replace: self.or_replace,
@ -247,6 +254,7 @@ impl CreateTableBuilder {
on_commit: self.on_commit, on_commit: self.on_commit,
on_cluster: self.on_cluster, on_cluster: self.on_cluster,
order_by: self.order_by, order_by: self.order_by,
strict: self.strict,
} }
} }
} }
@ -284,6 +292,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
on_commit, on_commit,
on_cluster, on_cluster,
order_by, order_by,
strict,
} => Ok(Self { } => Ok(Self {
or_replace, or_replace,
temporary, temporary,
@ -310,6 +319,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
on_commit, on_commit,
on_cluster, on_cluster,
order_by, order_by,
strict,
}), }),
_ => Err(ParserError::ParserError(format!( _ => Err(ParserError::ParserError(format!(
"Expected create table statement, but received: {stmt}" "Expected create table statement, but received: {stmt}"

View file

@ -1316,6 +1316,10 @@ pub enum Statement {
/// than empty (represented as ()), the latter meaning "no sorting". /// than empty (represented as ()), the latter meaning "no sorting".
/// <https://clickhouse.com/docs/en/sql-reference/statements/create/table/> /// <https://clickhouse.com/docs/en/sql-reference/statements/create/table/>
order_by: Option<Vec<Ident>>, order_by: Option<Vec<Ident>>,
/// SQLite "STRICT" clause.
/// if the "STRICT" table-option keyword is added to the end, after the closing ")",
/// then strict typing rules apply to that table.
strict: bool,
}, },
/// SQLite's `CREATE VIRTUAL TABLE .. USING <module_name> (<module_args>)` /// SQLite's `CREATE VIRTUAL TABLE .. USING <module_name> (<module_args>)`
CreateVirtualTable { CreateVirtualTable {
@ -2219,6 +2223,7 @@ impl fmt::Display for Statement {
on_commit, on_commit,
on_cluster, on_cluster,
order_by, order_by,
strict,
} => { } => {
// We want to allow the following options // We want to allow the following options
// Empty column list, allowed by PostgreSQL: // Empty column list, allowed by PostgreSQL:
@ -2387,7 +2392,9 @@ impl fmt::Display for Statement {
}; };
write!(f, " {on_commit}")?; write!(f, " {on_commit}")?;
} }
if *strict {
write!(f, " STRICT")?;
}
Ok(()) Ok(())
} }
Statement::CreateVirtualTable { Statement::CreateVirtualTable {

View file

@ -551,6 +551,7 @@ define_keywords!(
STDOUT, STDOUT,
STORAGE_INTEGRATION, STORAGE_INTEGRATION,
STORED, STORED,
STRICT,
STRING, STRING,
SUBMULTISET, SUBMULTISET,
SUBSTRING, SUBSTRING,

View file

@ -3485,6 +3485,7 @@ impl<'a> Parser<'a> {
None None
}; };
let strict = self.parse_keyword(Keyword::STRICT);
Ok(CreateTableBuilder::new(table_name) Ok(CreateTableBuilder::new(table_name)
.temporary(temporary) .temporary(temporary)
.columns(columns) .columns(columns)
@ -3507,6 +3508,7 @@ impl<'a> Parser<'a> {
.collation(collation) .collation(collation)
.on_commit(on_commit) .on_commit(on_commit)
.on_cluster(on_cluster) .on_cluster(on_cluster)
.strict(strict)
.build()) .build())
} }

View file

@ -242,6 +242,15 @@ fn parse_similar_to() {
chk(true); chk(true);
} }
#[test]
fn parse_create_table_with_strict() {
let sql = "CREATE TABLE Fruits (id TEXT NOT NULL PRIMARY KEY) STRICT";
if let Statement::CreateTable { name, strict, .. } = sqlite().verified_stmt(sql) {
assert_eq!(name.to_string(), "Fruits");
assert!(strict);
}
}
fn sqlite() -> TestedDialects { fn sqlite() -> TestedDialects {
TestedDialects { TestedDialects {
dialects: vec![Box::new(SQLiteDialect {})], dialects: vec![Box::new(SQLiteDialect {})],