Skip to content

Commit

Permalink
fix: set local or session time_zone not work
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 committed May 28, 2024
1 parent 4aa756c commit 63f1f57
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/servers/src/mysql/federated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ static OTHER_NOT_SUPPORTED_STMT: Lazy<RegexSet> = Lazy::new(|| {
"(?i)^(SELECT \\$\\$)",

// mysqldump.
"(?i)^(SET SESSION(.*))",
"(?i)^(SET SQL_QUOTE_SHOW_CREATE(.*))",
"(?i)^(LOCK TABLES(.*))",
"(?i)^(UNLOCK TABLES(.*))",
Expand Down
24 changes: 16 additions & 8 deletions src/sql/src/parsers/set_var_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ impl<'a> ParserContext<'a> {
SpStatement::SetVariable {
variable,
value,
local,
hivevar,
} if !local && !hivevar => {
Ok(Statement::SetVariables(SetVariables { variable, value }))
}
..
} if !hivevar => Ok(Statement::SetVariables(SetVariables { variable, value })),
unexp => error::UnsupportedSnafu {
sql: self.sql.to_string(),
keyword: unexp.to_string(),
Expand All @@ -51,10 +49,7 @@ mod tests {
use crate::dialect::GreptimeDbDialect;
use crate::parser::ParseOptions;

#[test]
pub fn test_set_timezone() {
// mysql style
let sql = "SET time_zone = 'UTC'";
fn assert_mysql_parse_result(sql: &str) {
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
let mut stmts = result.unwrap();
Expand All @@ -65,6 +60,19 @@ mod tests {
value: vec![Expr::Value(Value::SingleQuotedString("UTC".to_string()))]
})
);
}

#[test]
pub fn test_set_timezone() {
// mysql style
let sql = "SET time_zone = 'UTC'";
assert_mysql_parse_result(sql);
// session or local style
let sql = "SET LOCAL time_zone = 'UTC'";
assert_mysql_parse_result(sql);
let sql = "SET SESSION time_zone = 'UTC'";
assert_mysql_parse_result(sql);

// postgresql style
let sql = "SET TIMEZONE TO 'UTC'";
let result =
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/standalone/common/system/timezone.result
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ select to_unixtime('2024-01-02 00:00:00+08:00');
+------------------------------------------------+

--- UTC-8 ---
SET TIME_ZONE = '-8:00';
SET SESSION TIME_ZONE = '-8:00';

Affected Rows: 0

Expand Down Expand Up @@ -281,7 +281,7 @@ drop table test;
Affected Rows: 0

-- revert timezone to UTC
SET TIME_ZONE = 'UTC';
SET LOCAL TIME_ZONE = 'UTC';

Affected Rows: 0

Expand Down
4 changes: 2 additions & 2 deletions tests/cases/standalone/common/system/timezone.sql
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ select to_unixtime('2024-01-02 00:00:00');
select to_unixtime('2024-01-02 00:00:00+08:00');

--- UTC-8 ---
SET TIME_ZONE = '-8:00';
SET SESSION TIME_ZONE = '-8:00';

SHOW VARIABLES time_zone;

Expand All @@ -71,7 +71,7 @@ select to_unixtime('2024-01-02 00:00:00+08:00');
drop table test;

-- revert timezone to UTC
SET TIME_ZONE = 'UTC';
SET LOCAL TIME_ZONE = 'UTC';

SHOW VARIABLES time_zone;

Expand Down
9 changes: 7 additions & 2 deletions tests/runner/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,9 @@ impl Database for GreptimeDB {

let mut client = self.client.lock().await;

if query.trim().to_lowercase().starts_with("use ") {
let query_str = query.trim().to_lowercase();

if query_str.starts_with("use ") {
// use [db]
let database = query
.split_ascii_whitespace()
Expand All @@ -447,7 +449,10 @@ impl Database for GreptimeDB {
Box::new(ResultDisplayer {
result: Ok(Output::new_with_affected_rows(0)),
}) as _
} else if query.trim().to_lowercase().starts_with("set time_zone") {
} else if query_str.starts_with("set time_zone")
|| query_str.starts_with("set session time_zone")
|| query_str.starts_with("set local time_zone")
{
// set time_zone='xxx'
let timezone = query
.split('=')
Expand Down

0 comments on commit 63f1f57

Please sign in to comment.