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_cluster: Option<String>,
pub order_by: Option<Vec<Ident>>,
pub strict: bool,
}
impl CreateTableBuilder {
@ -100,6 +101,7 @@ impl CreateTableBuilder {
on_commit: None,
on_cluster: None,
order_by: None,
strict: false,
}
}
pub fn or_replace(mut self, or_replace: bool) -> Self {
@ -220,6 +222,11 @@ impl CreateTableBuilder {
self
}
pub fn strict(mut self, strict: bool) -> Self {
self.strict = strict;
self
}
pub fn build(self) -> Statement {
Statement::CreateTable {
or_replace: self.or_replace,
@ -247,6 +254,7 @@ impl CreateTableBuilder {
on_commit: self.on_commit,
on_cluster: self.on_cluster,
order_by: self.order_by,
strict: self.strict,
}
}
}
@ -284,6 +292,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
on_commit,
on_cluster,
order_by,
strict,
} => Ok(Self {
or_replace,
temporary,
@ -310,6 +319,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
on_commit,
on_cluster,
order_by,
strict,
}),
_ => Err(ParserError::ParserError(format!(
"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".
/// <https://clickhouse.com/docs/en/sql-reference/statements/create/table/>
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>)`
CreateVirtualTable {
@ -2219,6 +2223,7 @@ impl fmt::Display for Statement {
on_commit,
on_cluster,
order_by,
strict,
} => {
// We want to allow the following options
// Empty column list, allowed by PostgreSQL:
@ -2387,7 +2392,9 @@ impl fmt::Display for Statement {
};
write!(f, " {on_commit}")?;
}
if *strict {
write!(f, " STRICT")?;
}
Ok(())
}
Statement::CreateVirtualTable {

View file

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

View file

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

View file

@ -242,6 +242,15 @@ fn parse_similar_to() {
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 {
TestedDialects {
dialects: vec![Box::new(SQLiteDialect {})],