-
Notifications
You must be signed in to change notification settings - Fork 602
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
Conversation
src/ast/mod.rs
Outdated
Print { | ||
message: Box<Expr>, | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separate struct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we can use a separate struct for repr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍
@@ -2036,3 +2036,37 @@ fn parse_mssql_merge_with_output() { | |||
OUTPUT $action, deleted.ProductID INTO dsi.temp_products"; | |||
ms_and_generic().verified_stmt(stmt); | |||
} | |||
|
|||
#[test] | |||
fn parse_print() { |
There was a problem hiding this comment.
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
src/ast/mod.rs
Outdated
Print { | ||
message: Box<Expr>, | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we can use a separate struct for repr
src/parser/mod.rs
Outdated
@@ -617,6 +617,9 @@ 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 if dialect_of!(self is MsSqlDialect) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keyword::PRINT if dialect_of!(self is MsSqlDialect) => { | |
Keyword::PRINT => { |
we can parse unconditionally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍
4b9efe8
to
4bb97d6
Compare
Rebased & updated |
tests/sqlparser_mssql.rs
Outdated
let print_stmt = ms().one_statement_parses_to(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 print_national_string = "PRINT N'Hello, ⛄️!'"; | ||
let print_stmt = ms().one_statement_parses_to(print_national_string, ""); | ||
assert_eq!( | ||
print_stmt, | ||
Statement::Print(PrintStatement { | ||
message: Box::new(Expr::Value( | ||
(Value::NationalStringLiteral("Hello, ⛄️!".to_string())).with_empty_span() | ||
)), | ||
}) | ||
); | ||
|
||
let print_variable = "PRINT @my_variable"; | ||
let print_stmt = ms().one_statement_parses_to(print_variable, ""); | ||
assert_eq!( | ||
print_stmt, | ||
Statement::Print(PrintStatement { | ||
message: Box::new(Expr::Identifier(Ident::new("@my_variable"))), | ||
}) | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh was there a requirement to use one_statement_parses_to
instead of verified_stmt
for these tests?
Also with vierified_stmt we can simplify the tests except, for the hello world example, to skip assertion on the AST
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unaware of any requirement, it's just that I don't understand the project testing conventions & I'm trying to follow the patterns already present in the file. I'll switch it over to your suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we might want to inspect the ast in the future if we do an enum of value params instead of an expr, but we can cross that bridge if/when we ever come to it. This suggestion is complete 👍
4bb97d6
to
7c224ee
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Thanks @aharpervc!
cc @alamb
Reference: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/print-transact-sql?view=sql-server-ver16
Making
message
aBox<Expr>
instead of an enum of (national) string literal, variable, or expression is slightly lazy and also seems completely acceptable for the time being.