fix: set local or session time_zone not work (#4064)

* fix: set local or session time_zone not work

* chore: supports PostgreSQL-specific setting time zone
This commit is contained in:
dennis zhuang
2024-05-28 17:06:13 -07:00
committed by GitHub
parent 4aa756c896
commit aafb468547
5 changed files with 45 additions and 17 deletions

View File

@@ -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(.*))",

View File

@@ -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;
@@ -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(),
@@ -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();
@@ -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();
@@ -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);
}
}