feat: time_zone variable for mysql connections (#1607)

* feat: add timezone info to query context

* feat: parse mysql compatible time zone string

* feat: add method to timestamp for rendering timezone aware string

* feat: use timezone from session for time string rendering

* refactor: use querycontectref

* feat: implement session/timezone variable read/write

* style: resolve toml format

* test: update tests

* Apply suggestions from code review

Co-authored-by: dennis zhuang <killme2008@gmail.com>

* Update src/session/src/context.rs

Co-authored-by: dennis zhuang <killme2008@gmail.com>

* refactor: address review issues

---------

Co-authored-by: dennis zhuang <killme2008@gmail.com>
This commit is contained in:
Ning Sun
2023-05-22 18:30:23 +08:00
committed by GitHub
co-authored by dennis zhuang
parent 32ad358323
commit 067c5ee7ce
11 changed files with 380 additions and 46 deletions
+1
View File
@@ -8,3 +8,4 @@ license.workspace = true
arc-swap = "1.5"
common-catalog = { path = "../common/catalog" }
common-telemetry = { path = "../common/telemetry" }
common-time = { path = "../common/time" }
+14
View File
@@ -20,6 +20,7 @@ use arc_swap::ArcSwap;
use common_catalog::build_db_string;
use common_catalog::consts::{DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME};
use common_telemetry::debug;
use common_time::TimeZone;
pub type QueryContextRef = Arc<QueryContext>;
pub type ConnInfoRef = Arc<ConnInfo>;
@@ -28,6 +29,7 @@ pub type ConnInfoRef = Arc<ConnInfo>;
pub struct QueryContext {
current_catalog: ArcSwap<String>,
current_schema: ArcSwap<String>,
time_zone: ArcSwap<Option<TimeZone>>,
}
impl Default for QueryContext {
@@ -56,6 +58,7 @@ impl QueryContext {
Self {
current_catalog: ArcSwap::new(Arc::new(DEFAULT_CATALOG_NAME.to_string())),
current_schema: ArcSwap::new(Arc::new(DEFAULT_SCHEMA_NAME.to_string())),
time_zone: ArcSwap::new(Arc::new(None)),
}
}
@@ -63,6 +66,7 @@ impl QueryContext {
Self {
current_catalog: ArcSwap::new(Arc::new(catalog.to_string())),
current_schema: ArcSwap::new(Arc::new(schema.to_string())),
time_zone: ArcSwap::new(Arc::new(None)),
}
}
@@ -99,6 +103,16 @@ impl QueryContext {
let schema = self.current_schema();
build_db_string(&catalog, &schema)
}
#[inline]
pub fn time_zone(&self) -> Option<TimeZone> {
self.time_zone.load().as_ref().clone()
}
#[inline]
pub fn set_time_zone(&self, tz: Option<TimeZone>) {
self.time_zone.swap(Arc::new(tz));
}
}
pub const DEFAULT_USERNAME: &str = "greptime";