mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-10-28 14:46:22 +00:00
feat: support HTTP&gRPC&pg set timezone (#3125)
* feat: support HTTP&gRPC&pg set timezone * chore: fix code advice * chore: fix code advice
This commit is contained in:
@@ -117,6 +117,8 @@ impl<'a> ParserContext<'a> {
|
||||
|
||||
Keyword::TRUNCATE => self.parse_truncate(),
|
||||
|
||||
Keyword::SET => self.parse_set_variables(),
|
||||
|
||||
Keyword::NoKeyword
|
||||
if w.value.to_uppercase() == tql_parser::TQL && w.quote_style.is_none() =>
|
||||
{
|
||||
|
||||
@@ -21,6 +21,7 @@ pub(crate) mod drop_parser;
|
||||
pub(crate) mod explain_parser;
|
||||
pub(crate) mod insert_parser;
|
||||
pub(crate) mod query_parser;
|
||||
pub(crate) mod set_var_parser;
|
||||
pub(crate) mod show_parser;
|
||||
pub(crate) mod tql_parser;
|
||||
pub(crate) mod truncate_parser;
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
// Copyright 2023 Greptime Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use snafu::ResultExt;
|
||||
use sqlparser::ast::Statement as SpStatement;
|
||||
|
||||
use crate::error::{self, Result};
|
||||
use crate::parser::ParserContext;
|
||||
use crate::statements::set_variables::SetVariables;
|
||||
use crate::statements::statement::Statement;
|
||||
|
||||
/// SET variables statement parser implementation
|
||||
impl<'a> ParserContext<'a> {
|
||||
pub(crate) fn parse_set_variables(&mut self) -> Result<Statement> {
|
||||
let _ = self.parser.next_token();
|
||||
let spstatement = self.parser.parse_set().context(error::SyntaxSnafu)?;
|
||||
match spstatement {
|
||||
SpStatement::SetVariable {
|
||||
variable,
|
||||
value,
|
||||
local,
|
||||
hivevar,
|
||||
} if !local && !hivevar => {
|
||||
Ok(Statement::SetVariables(SetVariables { variable, value }))
|
||||
}
|
||||
unexp => error::UnsupportedSnafu {
|
||||
sql: self.sql.to_string(),
|
||||
keyword: unexp.to_string(),
|
||||
}
|
||||
.fail(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use sqlparser::ast::{Expr, Ident, ObjectName, Value};
|
||||
|
||||
use super::*;
|
||||
use crate::dialect::GreptimeDbDialect;
|
||||
|
||||
#[test]
|
||||
pub fn test_set_timezone() {
|
||||
// mysql style
|
||||
let sql = "SET time_zone = 'UTC'";
|
||||
let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {});
|
||||
let mut stmts = result.unwrap();
|
||||
assert_eq!(
|
||||
stmts.pop().unwrap(),
|
||||
Statement::SetVariables(SetVariables {
|
||||
variable: ObjectName(vec![Ident::new("time_zone")]),
|
||||
value: vec![Expr::Value(Value::SingleQuotedString("UTC".to_string()))]
|
||||
})
|
||||
);
|
||||
// postgresql style
|
||||
let sql = "SET TIMEZONE TO 'UTC'";
|
||||
let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {});
|
||||
let mut stmts = result.unwrap();
|
||||
assert_eq!(
|
||||
stmts.pop().unwrap(),
|
||||
Statement::SetVariables(SetVariables {
|
||||
variable: ObjectName(vec![Ident::new("TIMEZONE")]),
|
||||
value: vec![Expr::Value(Value::SingleQuotedString("UTC".to_string()))],
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,9 @@ use sqlparser::tokenizer::Token;
|
||||
|
||||
use crate::error::{self, InvalidDatabaseNameSnafu, InvalidTableNameSnafu, Result};
|
||||
use crate::parser::ParserContext;
|
||||
use crate::statements::show::{ShowCreateTable, ShowDatabases, ShowKind, ShowTables};
|
||||
use crate::statements::show::{
|
||||
ShowCreateTable, ShowDatabases, ShowKind, ShowTables, ShowVariables,
|
||||
};
|
||||
use crate::statements::statement::Statement;
|
||||
|
||||
/// SHOW statement parser implementation
|
||||
@@ -43,6 +45,16 @@ impl<'a> ParserContext<'a> {
|
||||
} else {
|
||||
self.unsupported(self.peek_token_as_string())
|
||||
}
|
||||
} else if self.consume_token("VARIABLES") {
|
||||
let variable =
|
||||
self.parser
|
||||
.parse_object_name()
|
||||
.with_context(|_| error::UnexpectedSnafu {
|
||||
sql: self.sql,
|
||||
expected: "a variable name",
|
||||
actual: self.peek_token_as_string(),
|
||||
})?;
|
||||
Ok(Statement::ShowVariables(ShowVariables { variable }))
|
||||
} else {
|
||||
self.unsupported(self.peek_token_as_string())
|
||||
}
|
||||
@@ -178,6 +190,8 @@ impl<'a> ParserContext<'a> {
|
||||
mod tests {
|
||||
use std::assert_matches::assert_matches;
|
||||
|
||||
use sqlparser::ast::{Ident, ObjectName};
|
||||
|
||||
use super::*;
|
||||
use crate::dialect::GreptimeDbDialect;
|
||||
use crate::statements::show::ShowDatabases;
|
||||
@@ -387,4 +401,18 @@ mod tests {
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_show_variables() {
|
||||
let sql = "SHOW VARIABLES system_time_zone";
|
||||
let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {});
|
||||
let stmts = result.unwrap();
|
||||
assert_eq!(1, stmts.len());
|
||||
assert_eq!(
|
||||
stmts[0],
|
||||
Statement::ShowVariables(ShowVariables {
|
||||
variable: ObjectName(vec![Ident::new("system_time_zone")]),
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ pub mod explain;
|
||||
pub mod insert;
|
||||
mod option_map;
|
||||
pub mod query;
|
||||
pub mod set_variables;
|
||||
pub mod show;
|
||||
pub mod statement;
|
||||
pub mod tql;
|
||||
@@ -505,7 +506,7 @@ pub fn sql_location_to_grpc_add_column_location(
|
||||
match location {
|
||||
Some(AddColumnLocation::First) => Some(Location {
|
||||
location_type: LocationType::First.into(),
|
||||
after_column_name: "".to_string(),
|
||||
after_column_name: String::default(),
|
||||
}),
|
||||
Some(AddColumnLocation::After { column_name }) => Some(Location {
|
||||
location_type: LocationType::After.into(),
|
||||
|
||||
@@ -96,7 +96,7 @@ impl CreateTable {
|
||||
if let Some(partitions) = &self.partitions {
|
||||
format!("{}\n", partitions)
|
||||
} else {
|
||||
"".to_string()
|
||||
String::default()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ impl CreateTable {
|
||||
#[inline]
|
||||
fn format_options(&self) -> String {
|
||||
if self.options.is_empty() {
|
||||
"".to_string()
|
||||
String::default()
|
||||
} else {
|
||||
let options: Vec<&SqlOption> = self.options.iter().sorted().collect();
|
||||
let options = format_list_indent!(options);
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2023 Greptime Team
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use sqlparser::ast::{Expr, ObjectName};
|
||||
use sqlparser_derive::{Visit, VisitMut};
|
||||
|
||||
/// SET variables statement.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut)]
|
||||
pub struct SetVariables {
|
||||
pub variable: ObjectName,
|
||||
pub value: Vec<Expr>,
|
||||
}
|
||||
@@ -63,6 +63,12 @@ pub struct ShowCreateTable {
|
||||
pub table_name: ObjectName,
|
||||
}
|
||||
|
||||
/// SQL structure for `SHOW VARIABLES xxx`.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut)]
|
||||
pub struct ShowVariables {
|
||||
pub variable: ObjectName,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::assert_matches::assert_matches;
|
||||
|
||||
@@ -16,6 +16,7 @@ use datafusion_sql::parser::Statement as DfStatement;
|
||||
use sqlparser::ast::Statement as SpStatement;
|
||||
use sqlparser_derive::{Visit, VisitMut};
|
||||
|
||||
use super::show::ShowVariables;
|
||||
use crate::error::{ConvertToDfStatementSnafu, Error};
|
||||
use crate::statements::alter::AlterTable;
|
||||
use crate::statements::create::{CreateDatabase, CreateExternalTable, CreateTable};
|
||||
@@ -25,6 +26,7 @@ use crate::statements::drop::DropTable;
|
||||
use crate::statements::explain::Explain;
|
||||
use crate::statements::insert::Insert;
|
||||
use crate::statements::query::Query;
|
||||
use crate::statements::set_variables::SetVariables;
|
||||
use crate::statements::show::{ShowCreateTable, ShowDatabases, ShowTables};
|
||||
use crate::statements::tql::Tql;
|
||||
use crate::statements::truncate::TruncateTable;
|
||||
@@ -64,6 +66,10 @@ pub enum Statement {
|
||||
Tql(Tql),
|
||||
// TRUNCATE TABLE
|
||||
TruncateTable(TruncateTable),
|
||||
// SET VARIABLES
|
||||
SetVariables(SetVariables),
|
||||
// SHOW VARIABLES
|
||||
ShowVariables(ShowVariables),
|
||||
}
|
||||
|
||||
/// Comment hints from SQL.
|
||||
|
||||
Reference in New Issue
Block a user