Skip to content
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

add set time zone sometimezone as a exception while parsing keyword::set #727

Merged
merged 4 commits into from Nov 28, 2022

Conversation

waitingkuo
Copy link
Contributor

closes #303

src/parser.rs Outdated Show resolved Hide resolved
@waitingkuo
Copy link
Contributor Author

i'd like to make sure this implement make sense first, then i'll add some test cases later

@coveralls
Copy link

coveralls commented Nov 25, 2022

Pull Request Test Coverage Report for Build 3568533098

  • 12 of 15 (80.0%) changed or added relevant lines in 3 files are covered.
  • 150 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.02%) to 86.358%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/ast/mod.rs 4 5 80.0%
src/parser.rs 3 5 60.0%
Files with Coverage Reduction New Missed Lines %
src/ast/mod.rs 1 77.48%
src/parser.rs 149 83.91%
Totals Coverage Status
Change from base Build 3523496345: 0.02%
Covered Lines: 11888
Relevant Lines: 13766

💛 - Coveralls

src/parser.rs Outdated
Comment on lines 4556 to 4559
} else if (self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO))
|| (variable == ObjectName(vec!["TIMEZONE".into()]))
{
// when the object name is TIMEZONE, we support `SET TIMEZONE 'UTC'` without Eq sign or TO
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alamb @435201823

SET TIMEZONE 'UTC' is an exceptional case in posetgresql
not sure whether it's a good idea to add it here

src/parser.rs Outdated
@@ -4553,7 +4553,11 @@ impl<'a> Parser<'a> {
charset_name,
collation_name,
})
} else if self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO) {
} else if self.consume_token(&Token::Eq)
Copy link
Contributor

Choose a reason for hiding this comment

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

Having tests would make it easier, but, tbh, I guess the SET TIME ZONE could be an individual command, no?

It is a singular command in many dialects. Having this manual conversion seems weird to me, considering that we can't have SET TIME ZONE = ... (I'm basing myself on PostgreSQLs Docs)

Copy link
Contributor Author

@waitingkuo waitingkuo left a comment

Choose a reason for hiding this comment

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

@AugustoFKL @alamb @step-baby
thank you for the comments and feedbacks

here's the update:

  1. add a new statement Statement::SetTimeZone
  2. SET <variable> [TO|=] <value> is parsed as Statement::SetVariable
  3. SET TIME ZONE [TO|=] <value> is parsed as Statement::SetVariable
  4. SET TIME ZONE <value> is parsed as Statement::SetTimeZone
  5. SET <non-time-zone-variable> <value> doesn't work

Comment on lines +1274 to +1278
/// SET TIME ZONE <value>
///
/// Note: this is a PostgreSQL-specific statements
/// SET TIME ZONE <value> is an alias for SET timezone TO <value> in PostgreSQL
SetTimeZone { local: bool, value: 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.

add a new Statement SetTimeZone to reflect the difference between SET TIME ZONE TO 'UTC' and SET TIME ZONE 'UTC'

Comment on lines 4982 to 4993
#[test]
fn parse_set_time_zone_alias() {
println!("wew");
match verified_stmt("SET TIME ZONE 'UTC'") {
Statement::SetTimeZone { local, value } => {
assert!(!local);
println!("wew");
assert_eq!(value, Expr::Value(Value::SingleQuotedString("UTC".into())));
}
_ => unreachable!(),
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

add a test case for set time zone 'utc'

Comment on lines +4576 to +4584
} else if variable.to_string().eq_ignore_ascii_case("TIMEZONE") {
// for some db (e.g. postgresql), SET TIME ZONE <value> is an alias for SET TIMEZONE [TO|=] <value>
match self.parse_expr() {
Ok(expr) => Ok(Statement::SetTimeZone {
local: modifier == Some(Keyword::LOCAL),
value: expr,
}),
_ => self.expected("timezone value", self.peek_token())?,
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this captures the SET TIME ZONE <value> case

@waitingkuo waitingkuo marked this pull request as ready for review November 28, 2022 09:48
Copy link
Collaborator

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Other than the println looks good to me -- thank you @waitingkuo

match verified_stmt("SET TIME ZONE 'UTC'") {
Statement::SetTimeZone { local, value } => {
assert!(!local);
println!("wew");
Copy link
Collaborator

Choose a reason for hiding this comment

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

is the println meant to be left in?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated, thank you @alamb

@alamb alamb merged commit bae6822 into sqlparser-rs:main Nov 28, 2022
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.

can not parse "SET TIME ZONE 'UTC'"
4 participants