Skip to main content

query/query_engine/
context.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 std::sync::Arc;
16
17use common_query::logical_plan::SubstraitPlanDecoderRef;
18use common_telemetry::tracing_context::TracingContext;
19use datafusion::execution::context::{SessionState, TaskContext};
20use session::context::QueryContextRef;
21
22use crate::query_engine::default_serializer::DefaultPlanDecoder;
23
24#[derive(Debug)]
25pub struct QueryEngineContext {
26    state: SessionState,
27    query_ctx: QueryContextRef,
28}
29
30impl QueryEngineContext {
31    pub fn new(state: SessionState, query_ctx: QueryContextRef) -> Self {
32        Self { state, query_ctx }
33    }
34
35    #[inline]
36    pub fn state(&self) -> &SessionState {
37        &self.state
38    }
39
40    #[inline]
41    pub fn query_ctx(&self) -> QueryContextRef {
42        self.query_ctx.clone()
43    }
44
45    /// The session configuration the execution task context is built from, so that a plan may
46    /// extend it before execution.
47    #[inline]
48    pub fn config_mut(&mut self) -> &mut datafusion::execution::config::SessionConfig {
49        self.state.config_mut()
50    }
51
52    pub fn build_task_ctx(&self) -> Arc<TaskContext> {
53        let dbname = self.query_ctx.get_db_string();
54        let state = &self.state;
55        let tracing_context = TracingContext::from_current_span();
56
57        // pass tracing context in session_id
58        let session_id = tracing_context.to_json();
59
60        Arc::new(TaskContext::new(
61            Some(dbname),
62            session_id,
63            state.config().clone(),
64            state.scalar_functions().clone(),
65            state.higher_order_functions().clone(),
66            state.aggregate_functions().clone(),
67            state.window_functions().clone(),
68            state.runtime_env().clone(),
69        ))
70    }
71
72    /// Creates a [`LogicalPlan`] decoder
73    pub fn new_plan_decoder(&self) -> crate::error::Result<SubstraitPlanDecoderRef> {
74        Ok(Arc::new(DefaultPlanDecoder::new(
75            self.state.clone(),
76            &self.query_ctx,
77        )?))
78    }
79
80    /// Mock an engine context for unit tests.
81    #[cfg(test)]
82    pub fn mock() -> Self {
83        use common_base::Plugins;
84        use session::context::QueryContext;
85
86        use crate::options::QueryOptions;
87        use crate::query_engine::QueryEngineState;
88
89        let state = Arc::new(QueryEngineState::new(
90            catalog::memory::new_memory_catalog_manager().unwrap(),
91            None,
92            None,
93            None,
94            None,
95            None,
96            false,
97            Plugins::default(),
98            QueryOptions::default(),
99        ));
100
101        QueryEngineContext::new(state.session_state(), QueryContext::arc())
102    }
103}