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/// Maximum decoded SQL size accepted by the logical-table batch operation.
30pub const MAX_LOGICAL_TABLE_DDL_BYTES: usize = 1024 * 1024;
31/// Maximum number of CREATE statements in one logical-table batch.
32pub const MAX_LOGICAL_TABLE_DDL_STATEMENTS: usize = 128;
33
34#[async_trait]
35pub trait SqlQueryHandler {
36    /// Whether this handler implements the bounded logical-table DDL operation.
37    fn supports_metric_batch_ddl(&self) -> bool {
38        false
39    }
40
41    /// Validates and authorizes the whole batch before submitting one procedure.
42    async fn create_logical_tables(
43        &self,
44        _query: &str,
45        _query_ctx: QueryContextRef,
46    ) -> Result<Vec<Output>> {
47        crate::error::UnsupportedOperationSnafu {
48            operation: "metric batch DDL",
49        }
50        .fail()
51    }
52
53    async fn do_query(&self, query: &str, query_ctx: QueryContextRef) -> Vec<Result<Output>>;
54
55    /// Executes an HTTP analyze-stream query.
56    ///
57    /// Implementations must validate that `query` is exactly one supported
58    /// SQL `EXPLAIN ANALYZE VERBOSE` or TQL `TQL ANALYZE VERBOSE` statement and
59    /// must return a streaming output. `OutputMeta.plan` is used by the HTTP
60    /// layer to emit metrics snapshots; when it is absent, partial metrics may
61    /// not be available. The returned stream should support cancel-on-drop
62    /// semantics (as the production frontend implementation does) so client
63    /// disconnect can best-effort cancel the underlying query.
64    async fn do_analyze_stream_query(
65        &self,
66        query: &str,
67        query_ctx: QueryContextRef,
68    ) -> Result<Output>;
69
70    async fn do_exec_plan(
71        &self,
72        plan: LogicalPlan,
73        stmt: Option<Statement>,
74        query_ctx: QueryContextRef,
75    ) -> Result<Output>;
76
77    async fn do_promql_query(
78        &self,
79        query: &PromQuery,
80        query_ctx: QueryContextRef,
81    ) -> Vec<Result<Output>>;
82
83    async fn do_describe(
84        &self,
85        stmt: Statement,
86        query_ctx: QueryContextRef,
87    ) -> Result<Option<DescribeResult>>;
88
89    async fn is_valid_schema(&self, catalog: &str, schema: &str) -> Result<bool>;
90}