mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-09-02 04:07:21 +00:00
Implement ASSERT statement (#226)
As supported by PostgreSQL and BigQuery (with some differences between them)
This commit is contained in:
parent
5cab18963e
commit
c24b0e01db
4 changed files with 78 additions and 0 deletions
|
@ -551,6 +551,14 @@ pub enum Statement {
|
|||
Rollback { chain: bool },
|
||||
/// CREATE SCHEMA
|
||||
CreateSchema { schema_name: ObjectName },
|
||||
|
||||
/// ASSERT <condition> [AS <message>]
|
||||
Assert {
|
||||
condition: Expr,
|
||||
// AS or ,
|
||||
separator: String,
|
||||
message: Option<Expr>,
|
||||
},
|
||||
}
|
||||
|
||||
impl fmt::Display for Statement {
|
||||
|
@ -810,6 +818,18 @@ impl fmt::Display for Statement {
|
|||
write!(f, "ROLLBACK{}", if *chain { " AND CHAIN" } else { "" },)
|
||||
}
|
||||
Statement::CreateSchema { schema_name } => write!(f, "CREATE SCHEMA {}", schema_name),
|
||||
Statement::Assert {
|
||||
condition,
|
||||
separator,
|
||||
message,
|
||||
} => {
|
||||
write!(f, "ASSERT {}", condition)?;
|
||||
|
||||
if let Some(m) = message {
|
||||
write!(f, " {} {}", separator, m)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,6 +79,7 @@ define_keywords!(
|
|||
AS,
|
||||
ASC,
|
||||
ASENSITIVE,
|
||||
ASSERT,
|
||||
ASYMMETRIC,
|
||||
AT,
|
||||
ATOMIC,
|
||||
|
|
|
@ -148,6 +148,7 @@ impl Parser {
|
|||
Keyword::BEGIN => Ok(self.parse_begin()?),
|
||||
Keyword::COMMIT => Ok(self.parse_commit()?),
|
||||
Keyword::ROLLBACK => Ok(self.parse_rollback()?),
|
||||
Keyword::ASSERT => Ok(self.parse_assert()?),
|
||||
_ => self.expected("an SQL statement", Token::Word(w)),
|
||||
},
|
||||
Token::LParen => {
|
||||
|
@ -179,6 +180,22 @@ impl Parser {
|
|||
}
|
||||
Ok(expr)
|
||||
}
|
||||
pub fn parse_assert(&mut self) -> Result<Statement, ParserError> {
|
||||
let condition = self.parse_expr()?;
|
||||
let (separator, message) = if self.consume_token(&Token::Comma) {
|
||||
(",".to_string(), Some(self.parse_expr()?))
|
||||
} else if self.parse_keyword(Keyword::AS) {
|
||||
("AS".to_string(), Some(self.parse_expr()?))
|
||||
} else {
|
||||
("".to_string(), None)
|
||||
};
|
||||
|
||||
Ok(Statement::Assert {
|
||||
condition,
|
||||
separator,
|
||||
message,
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse an expression prefix
|
||||
pub fn parse_prefix(&mut self) -> Result<Expr, ParserError> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue