operator/
utils.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use common_time::Timezone;
16use session::context::{QueryContextBuilder, QueryContextRef};
17use snafu::ResultExt;
18
19use crate::error::{Error, InvalidTimezoneSnafu};
20
21pub fn to_meta_query_context(
22    query_context: QueryContextRef,
23) -> common_meta::rpc::ddl::QueryContext {
24    common_meta::rpc::ddl::QueryContext {
25        current_catalog: query_context.current_catalog().to_string(),
26        current_schema: query_context.current_schema().clone(),
27        timezone: query_context.timezone().to_string(),
28        extensions: query_context.extensions(),
29        channel: query_context.channel() as u8,
30    }
31}
32
33pub fn try_to_session_query_context(
34    value: common_meta::rpc::ddl::QueryContext,
35) -> Result<session::context::QueryContext, Error> {
36    Ok(QueryContextBuilder::default()
37        .current_catalog(value.current_catalog)
38        .current_schema(value.current_schema)
39        .timezone(
40            Timezone::from_tz_string(&value.timezone).context(InvalidTimezoneSnafu {
41                timezone: value.timezone,
42            })?,
43        )
44        .extensions(value.extensions)
45        .channel((value.channel as u32).into())
46        .build())
47}