Skip to content

Add support for PRINT statement for SQL Server #1811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4054,6 +4054,12 @@ pub enum Statement {
arguments: Vec<Expr>,
options: Vec<RaisErrorOption>,
},
/// ```sql
/// PRINT msg_str | @local_variable | string_expr
/// ```
///
/// See: <https://learn.microsoft.com/en-us/sql/t-sql/statements/print-transact-sql>
Print(PrintStatement),
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down Expand Up @@ -5745,7 +5751,7 @@ impl fmt::Display for Statement {
}
Ok(())
}

Statement::Print(s) => write!(f, "{s}"),
Statement::List(command) => write!(f, "LIST {command}"),
Statement::Remove(command) => write!(f, "REMOVE {command}"),
}
Expand Down Expand Up @@ -9211,6 +9217,19 @@ pub enum CopyIntoSnowflakeKind {
Location,
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct PrintStatement {
pub message: Box<Expr>,
}

impl fmt::Display for PrintStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "PRINT {}", self.message)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ impl Spanned for Statement {
Statement::UNLISTEN { .. } => Span::empty(),
Statement::RenameTable { .. } => Span::empty(),
Statement::RaisError { .. } => Span::empty(),
Statement::Print { .. } => Span::empty(),
Statement::List(..) | Statement::Remove(..) => Span::empty(),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ define_keywords!(
PRESERVE,
PREWHERE,
PRIMARY,
PRINT,
PRIOR,
PRIVILEGES,
PROCEDURE,
Expand Down
8 changes: 8 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ impl<'a> Parser<'a> {
}
// `COMMENT` is snowflake specific https://docs.snowflake.com/en/sql-reference/sql/comment
Keyword::COMMENT if self.dialect.supports_comment_on() => self.parse_comment(),
Keyword::PRINT => self.parse_print(),
_ => self.expected("an SQL statement", next_token),
},
Token::LParen => {
Expand Down Expand Up @@ -15058,6 +15059,13 @@ impl<'a> Parser<'a> {
}
}

/// Parse [Statement::Print]
fn parse_print(&mut self) -> Result<Statement, ParserError> {
Ok(Statement::Print(PrintStatement {
message: Box::new(self.parse_expr()?),
}))
}

/// Consume the parser and return its underlying token buffer
pub fn into_tokens(self) -> Vec<TokenWithSpan> {
self.tokens
Expand Down
17 changes: 17 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2053,3 +2053,20 @@ fn parse_drop_trigger() {
}
);
}

#[test]
fn parse_print() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a test that asserts a particular error for a bare PRINT? Not sure what the conventions are for that

let print_string_literal = "PRINT 'Hello, world!'";
let print_stmt = ms().verified_stmt(print_string_literal);
assert_eq!(
print_stmt,
Statement::Print(PrintStatement {
message: Box::new(Expr::Value(
(Value::SingleQuotedString("Hello, world!".to_string())).with_empty_span()
)),
})
);

let _ = ms().verified_stmt("PRINT N'Hello, ⛄️!'");
let _ = ms().verified_stmt("PRINT @my_variable");
}