iffyio commented on code in PR #1791: URL: https://github.com/apache/datafusion-sqlparser-rs/pull/1791#discussion_r2025392312
########## src/ast/mod.rs: ########## @@ -2145,116 +2149,189 @@ impl fmt::Display for CaseStatement { } if let Some(else_block) = else_block { - write!(f, " ELSE ")?; - format_statement_list(f, else_block)?; + write!(f, " {else_block}")?; } write!(f, " END")?; - if *has_end_case { - write!(f, " CASE")?; + + if let Token::Word(w) = &end.token { + if w.keyword == Keyword::CASE { + write!(f, " CASE")?; + } } Ok(()) } } /// An `IF` statement. -/// -/// Examples: -/// ```sql -/// IF TRUE THEN -/// SELECT 1; -/// SELECT 2; -/// ELSEIF TRUE THEN -/// SELECT 3; -/// ELSE -/// SELECT 4; -/// END IF -/// ``` -/// -/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#if) -/// [Snowflake](https://docs.snowflake.com/en/sql-reference/snowflake-scripting/if) #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] -pub struct IfStatement { - pub if_block: ConditionalStatements, - pub elseif_blocks: Vec<ConditionalStatements>, - pub else_block: Option<Vec<Statement>>, +pub enum IfStatement { + /// An `IF ... THEN [ELSE[IF] ...] END IF` statement. + /// + /// Example: + /// ```sql + /// IF TRUE THEN + /// SELECT 1; + /// SELECT 2; + /// ELSEIF TRUE THEN + /// SELECT 3; + /// ELSE + /// SELECT 4; + /// END IF + /// ``` + /// + /// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#if) + /// [Snowflake](https://docs.snowflake.com/en/sql-reference/snowflake-scripting/if) + IfThenElseEnd { + /// The `IF` token that starts the statement. + if_token: AttachedToken, + if_block: ConditionalStatements, + elseif_blocks: Vec<ConditionalStatements>, + else_block: Option<ConditionalStatements>, + /// The `IF` token that ends the statement. + end_if_token: AttachedToken, + }, + /// An MSSQL `IF ... ELSE ...` statement. + /// + /// Example: + /// ```sql + /// IF 1=1 SELECT 1 ELSE SELECT 2 + /// ``` + /// + /// [MSSQL](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/if-else-transact-sql?view=sql-server-ver16) + MsSqlIfElse { Review Comment: Would it be possible to merge the representation to be shared by both? Something like this roughly I'm thinking ```rust enum StatementBlock { Expr(Expr), Statements(Vec<Statements>), Begin(Vec<Statements>) } pub struct IfStatement { pub condition: Expr, pub if_block: StatementBlock, pub elseif_blocks: Vec<StatementBlock>, pub else_block: Option<StatementBlock>, } ``` Ideally we avoid introducing dialect specific nodes in the AST -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org