Skip to main content

servers/query_handler/
sql.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 async_trait::async_trait;
18use common_query::Output;
19use datafusion_expr::LogicalPlan;
20use query::parser::PromQuery;
21use query::query_engine::DescribeResult;
22use session::context::QueryContextRef;
23use sql::statements::statement::Statement;
24
25use crate::error::Result;
26
27pub type ServerSqlQueryHandlerRef = Arc<dyn SqlQueryHandler + Send + Sync>;
28
29#[async_trait]
30pub trait SqlQueryHandler {
31    async fn do_query(&self, query: &str, query_ctx: QueryContextRef) -> Vec<Result<Output>>;
32
33    /// Executes the experimental HTTP analyze-stream query path.
34    ///
35    /// Implementations must validate that `query` is exactly one explicit
36    /// `EXPLAIN ANALYZE VERBOSE` statement and must return a streaming output.
37    /// `OutputMeta.plan` is used by the HTTP layer to emit metrics snapshots;
38    /// when it is absent, partial metrics may not be available. The returned
39    /// stream should support cancel-on-drop semantics (as the production
40    /// frontend implementation does) so client disconnect can best-effort cancel
41    /// the underlying query.
42    async fn do_analyze_stream_query(
43        &self,
44        query: &str,
45        query_ctx: QueryContextRef,
46    ) -> Result<Output>;
47
48    async fn do_exec_plan(
49        &self,
50        plan: LogicalPlan,
51        stmt: Option<Statement>,
52        query_ctx: QueryContextRef,
53    ) -> Result<Output>;
54
55    async fn do_promql_query(
56        &self,
57        query: &PromQuery,
58        query_ctx: QueryContextRef,
59    ) -> Vec<Result<Output>>;
60
61    async fn do_describe(
62        &self,
63        stmt: Statement,
64        query_ctx: QueryContextRef,
65    ) -> Result<Option<DescribeResult>>;
66
67    async fn is_valid_schema(&self, catalog: &str, schema: &str) -> Result<bool>;
68}