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

Conversation

aharpervc
Copy link
Contributor

Reference: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/print-transact-sql?view=sql-server-ver16

Making message a Box<Expr> instead of an enum of (national) string literal, variable, or expression is slightly lazy and also seems completely acceptable for the time being.

@aharpervc aharpervc marked this pull request as ready for review April 11, 2025 21:28
src/ast/mod.rs Outdated
Comment on lines 4058 to 4060
Print {
message: Box<Expr>,
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Separate struct?

Copy link
Contributor

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

Copy link
Contributor Author

@aharpervc aharpervc Apr 15, 2025

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() {
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

src/ast/mod.rs Outdated
Comment on lines 4058 to 4060
Print {
message: Box<Expr>,
},
Copy link
Contributor

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

@@ -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) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Keyword::PRINT if dialect_of!(self is MsSqlDialect) => {
Keyword::PRINT => {

we can parse unconditionally?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done 👍

@aharpervc aharpervc force-pushed the mssql-print-statement branch 2 times, most recently from 4b9efe8 to 4bb97d6 Compare April 15, 2025 15:23
@aharpervc aharpervc requested a review from iffyio April 15, 2025 15:25
@aharpervc
Copy link
Contributor Author

Rebased & updated

Comment on lines 2060 to 2088
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"))),
})
);
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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 👍

@aharpervc aharpervc force-pushed the mssql-print-statement branch from 4bb97d6 to 7c224ee Compare April 16, 2025 18:54
@aharpervc aharpervc requested a review from iffyio April 17, 2025 13:45
Copy link
Contributor

@iffyio iffyio left a 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

@iffyio iffyio merged commit 4a48729 into apache:main Apr 18, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants