Skip to content

Commit

Permalink
fix: set local or session time_zone not work (#4064)
Browse files Browse the repository at this point in the history
* fix: set local or session time_zone not work

* chore: supports PostgreSQL-specific setting time zone
  • Loading branch information
killme2008 authored May 29, 2024
1 parent 4aa756c commit aafb468
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 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
44 changes: 34 additions & 10 deletions src/sql/src/parsers/set_var_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use snafu::ResultExt;
use sqlparser::ast::Statement as SpStatement;

use crate::ast::{Ident, ObjectName};
use crate::error::{self, Result};
use crate::parser::ParserContext;
use crate::statements::set_variables::SetVariables;
Expand All @@ -29,11 +30,18 @@ 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 })),

SpStatement::SetTimeZone { value, .. } => Ok(Statement::SetVariables(SetVariables {
variable: ObjectName(vec![Ident {
value: "TIMEZONE".to_string(),
quote_style: None,
}]),
value: vec![value],
})),

unexp => error::UnsupportedSnafu {
sql: self.sql.to_string(),
keyword: unexp.to_string(),
Expand All @@ -51,10 +59,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,8 +70,9 @@ mod tests {
value: vec![Expr::Value(Value::SingleQuotedString("UTC".to_string()))]
})
);
// postgresql style
let sql = "SET TIMEZONE TO 'UTC'";
}

fn assert_pg_parse_result(sql: &str) {
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
let mut stmts = result.unwrap();
Expand All @@ -78,4 +84,22 @@ mod tests {
})
);
}

#[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'";
assert_pg_parse_result(sql);
let sql = "SET TIMEZONE 'UTC'";
assert_pg_parse_result(sql);
}
}
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 aafb468

Please sign in to comment.