Skip to main content

frontend/
instance.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
15pub mod builder;
16mod dashboard;
17mod entity_graph;
18mod export_database;
19mod grpc;
20mod influxdb;
21mod jaeger;
22mod log_handler;
23mod logs;
24mod opentsdb;
25mod otlp;
26pub mod prom_store;
27mod promql;
28mod region_query;
29
30use std::collections::HashSet;
31use std::pin::Pin;
32use std::sync::atomic::AtomicBool;
33use std::sync::{Arc, atomic};
34use std::time::{Duration, SystemTime};
35
36use async_stream::stream;
37use async_trait::async_trait;
38use auth::{
39    PROMQL_QUERY, PermissionChecker, PermissionCheckerRef, PermissionReq, PermissionTableTarget,
40    PermissionTableTargets,
41};
42use catalog::CatalogManagerRef;
43use catalog::process_manager::{
44    ProcessManagerRef, QueryStatement as CatalogQueryStatement, SlowQueryRecorder, SlowQueryTimer,
45};
46use client::OutputData;
47use common_base::Plugins;
48use common_base::cancellation::CancellableFuture;
49use common_error::ext::{BoxedError, ErrorExt};
50use common_event_recorder::EventRecorderRef;
51use common_meta::cache::TableFlownodeSetCacheRef;
52use common_meta::cache_invalidator::CacheInvalidatorRef;
53use common_meta::key::TableMetadataManagerRef;
54use common_meta::key::table_name::TableNameKey;
55use common_meta::node_manager::NodeManagerRef;
56use common_meta::procedure_executor::ProcedureExecutorRef;
57use common_query::Output;
58use common_recordbatch::RecordBatchStreamWrapper;
59use common_recordbatch::error::StreamTimeoutSnafu;
60use common_telemetry::logging::SlowQueryOptions;
61use common_telemetry::{debug, error, tracing};
62use dashmap::DashMap;
63use datafusion::dataframe::DataFrame;
64use datafusion::physical_plan::ExecutionPlan;
65use datafusion_expr::LogicalPlan;
66use futures::{Stream, StreamExt, future};
67use lazy_static::lazy_static;
68use operator::delete::DeleterRef;
69use operator::insert::InserterRef;
70use operator::statement::{StatementExecutor, StatementExecutorRef};
71use partition::manager::PartitionRuleManagerRef;
72use pipeline::pipeline_operator::PipelineOperator;
73use prometheus::HistogramTimer;
74use promql_parser::label::Matcher;
75use query::QueryEngineRef;
76use query::metrics::OnDone;
77use query::parser::{PromQuery, QueryStatement};
78use query::query_engine::DescribeResult;
79use query::query_engine::options::{QueryOptions, validate_catalog_and_schema};
80use servers::error::{
81    self as server_error, AuthSnafu, CommonMetaSnafu, ExecuteQuerySnafu,
82    OtlpMetricModeIncompatibleSnafu, UnexpectedResultSnafu,
83};
84use servers::interceptor::{
85    PromQueryInterceptor, PromQueryInterceptorRef, SqlQueryInterceptor, SqlQueryInterceptorRef,
86};
87use servers::otlp::metrics::legacy_normalize_otlp_name;
88use servers::prometheus_handler::{
89    ParsedPromQuery, PrometheusHandler, resolve_schema_from_matchers,
90};
91use servers::query_handler::sql::SqlQueryHandler;
92use session::context::{Channel, QueryContextRef};
93use session::table_name::table_idents_to_full_name;
94use snafu::prelude::*;
95use sql::ast::ObjectNamePartExt;
96use sql::dialect::Dialect;
97use sql::parser::{ParseOptions, ParserContext};
98use sql::statements::comment::CommentObject;
99use sql::statements::copy::{CopyDatabase, CopyTable};
100use sql::statements::statement::Statement;
101use sql::statements::tql::Tql;
102use sql::util::{extract_tables_from_prom_expr_checked, extract_tables_from_statement_checked};
103use sqlparser::ast::{AnalyzeFormat, ObjectName};
104use table::requests::{OTLP_METRIC_COMPAT_KEY, OTLP_METRIC_COMPAT_PROM};
105use tracing::Span;
106
107use crate::error::{
108    self, CollectRecordbatchSnafu, Error, ExecLogicalPlanSnafu, ExecutePromqlSnafu, ExternalSnafu,
109    InvalidSqlSnafu, ParseSqlSnafu, PermissionSnafu, PlanStatementSnafu, Result,
110    SqlExecInterceptedSnafu, StatementTimeoutSnafu, TableOperationSnafu,
111};
112use crate::service_config::InfluxdbMergeMode;
113use crate::stream_wrapper::CancellableStreamWrapper;
114
115lazy_static! {
116    static ref OTLP_LEGACY_DEFAULT_VALUE: String = "legacy".to_string();
117}
118
119/// The frontend instance contains necessary components, and implements many
120/// traits, like [`servers::query_handler::grpc::GrpcQueryHandler`],
121/// [`servers::query_handler::sql::SqlQueryHandler`], etc.
122#[derive(Clone)]
123pub struct Instance {
124    frontend_peer_addr: String,
125    catalog_manager: CatalogManagerRef,
126    pipeline_operator: Arc<PipelineOperator>,
127    statement_executor: Arc<StatementExecutor>,
128    query_engine: QueryEngineRef,
129    plugins: Plugins,
130    inserter: InserterRef,
131    deleter: DeleterRef,
132    table_metadata_manager: TableMetadataManagerRef,
133    event_recorder: EventRecorderRef,
134    slow_query_recorder: EventRecorderRef,
135    process_manager: ProcessManagerRef,
136    slow_query_options: SlowQueryOptions,
137    influxdb_default_merge_mode: InfluxdbMergeMode,
138    trace_ingest_chunk_size: usize,
139    otlp_resource_info: bool,
140    suspend: Arc<AtomicBool>,
141
142    // cache for otlp metrics
143    // first layer key: db-string
144    // key: direct input metric name
145    // value: if runs in legacy mode
146    otlp_metrics_table_legacy_cache: DashMap<String, DashMap<String, bool>>,
147}
148
149impl Instance {
150    pub fn frontend_peer_addr(&self) -> &str {
151        &self.frontend_peer_addr
152    }
153
154    pub fn catalog_manager(&self) -> &CatalogManagerRef {
155        &self.catalog_manager
156    }
157
158    pub fn query_engine(&self) -> &QueryEngineRef {
159        &self.query_engine
160    }
161
162    pub fn plugins(&self) -> &Plugins {
163        &self.plugins
164    }
165
166    fn check_permission(
167        &self,
168        ctx: &QueryContextRef,
169        req: PermissionReq<'_>,
170    ) -> server_error::Result<()> {
171        self.plugins
172            .get::<PermissionCheckerRef>()
173            .as_ref()
174            .check_permission(ctx.current_user(), req)
175            .context(AuthSnafu)?;
176        Ok(())
177    }
178
179    pub fn statement_executor(&self) -> &StatementExecutorRef {
180        &self.statement_executor
181    }
182
183    pub fn table_metadata_manager(&self) -> &TableMetadataManagerRef {
184        &self.table_metadata_manager
185    }
186
187    pub fn inserter(&self) -> &InserterRef {
188        &self.inserter
189    }
190
191    pub fn process_manager(&self) -> &ProcessManagerRef {
192        &self.process_manager
193    }
194
195    /// Returns the event recorder configured for this frontend instance.
196    pub fn event_recorder(&self) -> EventRecorderRef {
197        self.event_recorder.clone()
198    }
199
200    pub fn node_manager(&self) -> &NodeManagerRef {
201        self.inserter.node_manager()
202    }
203
204    pub fn partition_manager(&self) -> &PartitionRuleManagerRef {
205        self.inserter.partition_manager()
206    }
207
208    pub fn table_flownode_set_cache(&self) -> &TableFlownodeSetCacheRef {
209        self.inserter.table_flownode_set_cache()
210    }
211
212    pub fn cache_invalidator(&self) -> &CacheInvalidatorRef {
213        self.statement_executor.cache_invalidator()
214    }
215
216    pub fn procedure_executor(&self) -> &ProcedureExecutorRef {
217        self.statement_executor.procedure_executor()
218    }
219
220    pub fn suspend_state(&self) -> Arc<AtomicBool> {
221        self.suspend.clone()
222    }
223
224    pub(crate) fn is_suspended(&self) -> bool {
225        self.suspend.load(atomic::Ordering::Relaxed)
226    }
227}
228
229fn parse_stmt(sql: &str, dialect: &(dyn Dialect + Send + Sync)) -> Result<Vec<Statement>> {
230    ParserContext::create_with_dialect(sql, dialect, ParseOptions::default()).context(ParseSqlSnafu)
231}
232
233fn is_explain_analyze_verbose(stmt: &Statement) -> bool {
234    matches!(stmt, Statement::Explain(explain) if explain.analyze && explain.verbose)
235        || matches!(stmt, Statement::Tql(Tql::Analyze(analyze)) if analyze.is_verbose)
236}
237
238fn validate_analyze_stream_statement(stmt: &mut Statement) -> Result<()> {
239    let (is_verbose, format) = match stmt {
240        Statement::Explain(explain) => (explain.analyze && explain.verbose, &mut explain.format),
241        Statement::Tql(Tql::Analyze(analyze)) => (analyze.is_verbose, &mut analyze.format),
242        _ => {
243            return InvalidSqlSnafu {
244                err_msg: "only EXPLAIN ANALYZE VERBOSE or TQL ANALYZE VERBOSE statement is supported",
245            }
246            .fail();
247        }
248    };
249
250    ensure!(
251        is_verbose,
252        InvalidSqlSnafu {
253            err_msg: "statement must be EXPLAIN ANALYZE VERBOSE or TQL ANALYZE VERBOSE"
254        }
255    );
256    match format {
257        None | Some(AnalyzeFormat::JSON) => {
258            // Keep explicit FORMAT JSON accepted, but pass JSON through
259            // QueryContext.explain_format instead of the statement to avoid
260            // the planner's current `EXPLAIN VERBOSE with FORMAT` limitation.
261            *format = None;
262            Ok(())
263        }
264        Some(_) => InvalidSqlSnafu {
265            err_msg: "only FORMAT JSON is supported for EXPLAIN ANALYZE VERBOSE or TQL ANALYZE VERBOSE",
266        }
267        .fail(),
268    }
269}
270
271impl Instance {
272    fn statement_slow_query_timer(
273        &self,
274        stmt: &Statement,
275        schema_name: String,
276    ) -> Option<SlowQueryTimer> {
277        if !stmt.is_readonly() || !self.slow_query_options.enable {
278            return None;
279        }
280
281        Some(SlowQueryTimer::new(
282            CatalogQueryStatement::Sql(stmt.clone()),
283            schema_name,
284            self.slow_query_options.threshold,
285            self.slow_query_options.sample_ratio,
286            self.slow_query_options.record_type,
287            self.slow_query_recorder.clone(),
288        ))
289    }
290
291    async fn query_statement(&self, stmt: Statement, query_ctx: QueryContextRef) -> Result<Output> {
292        check_permission(self.plugins.clone(), &stmt, &query_ctx)?;
293
294        let query_interceptor = self.plugins.get::<SqlQueryInterceptorRef<Error>>();
295        let query_interceptor = query_interceptor.as_ref();
296
297        if should_track_statement_process(&stmt) {
298            let catalog_name = query_ctx.current_catalog().to_string();
299            let schema_name = query_ctx.current_schema();
300            let slow_query_timer = self.statement_slow_query_timer(&stmt, schema_name.clone());
301            let timeout_recorder = is_explain_analyze_verbose(&stmt)
302                .then(|| slow_query_timer.as_ref().map(SlowQueryTimer::recorder))
303                .flatten();
304
305            let ticket = self.process_manager.register_query(
306                catalog_name,
307                vec![schema_name],
308                stmt.to_string(),
309                query_ctx.conn_info().to_string(),
310                Some(query_ctx.process_id()),
311                slow_query_timer,
312            );
313
314            let query_fut = self.exec_statement_with_timeout(
315                stmt,
316                query_ctx,
317                query_interceptor,
318                timeout_recorder,
319            );
320
321            CancellableFuture::new(query_fut, ticket.cancellation_handle.clone())
322                .await
323                .map_err(|_| error::CancelledSnafu.build())?
324                .map(|output| {
325                    let Output { meta, data } = output;
326
327                    let data = match data {
328                        OutputData::Stream(stream) => OutputData::Stream(Box::pin(
329                            CancellableStreamWrapper::new(stream, ticket),
330                        )),
331                        other => other,
332                    };
333                    Output { data, meta }
334                })
335        } else {
336            self.exec_statement_with_timeout(stmt, query_ctx, query_interceptor, None)
337                .await
338        }
339    }
340
341    async fn exec_statement_with_timeout(
342        &self,
343        stmt: Statement,
344        query_ctx: QueryContextRef,
345        query_interceptor: Option<&SqlQueryInterceptorRef<Error>>,
346        timeout_recorder: Option<SlowQueryRecorder>,
347    ) -> Result<Output> {
348        let timeout = derive_timeout(&stmt, &query_ctx);
349        match timeout {
350            Some(timeout) => {
351                let start = tokio::time::Instant::now();
352                let output = tokio::time::timeout(
353                    timeout,
354                    self.exec_statement(stmt, query_ctx, query_interceptor),
355                )
356                .await
357                .map_err(|_| StatementTimeoutSnafu.build())??;
358                let output = map_query_output(output)?;
359                // compute remaining timeout
360                let remaining_timeout = timeout.checked_sub(start.elapsed()).unwrap_or_default();
361                attach_timeout(output, remaining_timeout, timeout_recorder)
362            }
363            None => self
364                .exec_statement(stmt, query_ctx, query_interceptor)
365                .await
366                .and_then(map_query_output),
367        }
368    }
369
370    async fn exec_statement(
371        &self,
372        stmt: Statement,
373        query_ctx: QueryContextRef,
374        query_interceptor: Option<&SqlQueryInterceptorRef<Error>>,
375    ) -> Result<Output> {
376        match stmt {
377            Statement::Query(_) | Statement::Explain(_) | Statement::Delete(_) => {
378                // TODO: remove this when format is supported in datafusion
379                if let Statement::Explain(explain) = &stmt
380                    && let Some(format) = explain.format()
381                {
382                    query_ctx.set_explain_format(format.to_string());
383                }
384
385                self.plan_and_exec_sql(stmt, &query_ctx, query_interceptor)
386                    .await
387            }
388            Statement::Tql(tql) => {
389                self.plan_and_exec_tql(&query_ctx, query_interceptor, tql)
390                    .await
391            }
392            _ => {
393                query_interceptor.pre_execute(Some(&stmt), None, query_ctx.clone())?;
394                self.statement_executor
395                    .execute_sql(stmt, query_ctx)
396                    .await
397                    .context(TableOperationSnafu)
398            }
399        }
400    }
401
402    async fn plan_and_exec_sql(
403        &self,
404        stmt: Statement,
405        query_ctx: &QueryContextRef,
406        query_interceptor: Option<&SqlQueryInterceptorRef<Error>>,
407    ) -> Result<Output> {
408        let stmt = QueryStatement::Sql(stmt);
409        let plan = self
410            .statement_executor
411            .plan(&stmt, query_ctx.clone())
412            .await?;
413        let QueryStatement::Sql(stmt) = stmt else {
414            unreachable!()
415        };
416        query_interceptor.pre_execute(Some(&stmt), Some(&plan), query_ctx.clone())?;
417
418        self.statement_executor
419            .exec_plan(plan, query_ctx.clone())
420            .await
421            .context(TableOperationSnafu)
422    }
423
424    async fn plan_and_exec_tql(
425        &self,
426        query_ctx: &QueryContextRef,
427        query_interceptor: Option<&SqlQueryInterceptorRef<Error>>,
428        tql: Tql,
429    ) -> Result<Output> {
430        let plan = self
431            .statement_executor
432            .plan_tql(tql.clone(), query_ctx)
433            .await?;
434        query_interceptor.pre_execute(
435            Some(&Statement::Tql(tql)),
436            Some(&plan),
437            query_ctx.clone(),
438        )?;
439        self.statement_executor
440            .exec_plan(plan, query_ctx.clone())
441            .await
442            .context(TableOperationSnafu)
443    }
444
445    async fn check_otlp_legacy(
446        &self,
447        names: &[String],
448        ctx: &QueryContextRef,
449    ) -> server_error::Result<bool> {
450        let db_string = ctx.get_db_string();
451        // fast cache check
452        let cache = self
453            .otlp_metrics_table_legacy_cache
454            .entry(db_string.clone())
455            .or_default();
456        if let Some(flag) = fast_legacy_check(&cache, names)? {
457            return Ok(flag);
458        }
459        // release cache reference to avoid lock contention
460        drop(cache);
461
462        let catalog = ctx.current_catalog();
463        let schema = ctx.current_schema();
464
465        // query legacy table names
466        let normalized_names = names
467            .iter()
468            .map(|n| legacy_normalize_otlp_name(n))
469            .collect::<Vec<_>>();
470        let table_names = normalized_names
471            .iter()
472            .map(|n| TableNameKey::new(catalog, &schema, n))
473            .collect::<Vec<_>>();
474        let table_values = self
475            .table_metadata_manager()
476            .table_name_manager()
477            .batch_get(table_names)
478            .await
479            .context(CommonMetaSnafu)?;
480        let table_ids = table_values
481            .into_iter()
482            .filter_map(|v| v.map(|vi| vi.table_id()))
483            .collect::<Vec<_>>();
484
485        // means no existing table is found, use new mode
486        if table_ids.is_empty() {
487            return Ok(false);
488        }
489
490        // has existing table, check table options
491        let table_infos = self
492            .table_metadata_manager()
493            .table_info_manager()
494            .batch_get(&table_ids)
495            .await
496            .context(CommonMetaSnafu)?;
497        let options = table_infos
498            .values()
499            .map(|info| {
500                info.table_info
501                    .meta
502                    .options
503                    .extra_options
504                    .get(OTLP_METRIC_COMPAT_KEY)
505                    .unwrap_or(&OTLP_LEGACY_DEFAULT_VALUE)
506            })
507            .collect::<Vec<_>>();
508        if !options.is_empty() {
509            // check value consistency
510            let has_prom = options.iter().any(|opt| *opt == OTLP_METRIC_COMPAT_PROM);
511            let has_legacy = options
512                .iter()
513                .any(|opt| *opt == OTLP_LEGACY_DEFAULT_VALUE.as_str());
514            ensure!(!(has_prom && has_legacy), OtlpMetricModeIncompatibleSnafu);
515            Ok(has_legacy)
516        } else {
517            // no table info, use new mode
518            Ok(false)
519        }
520    }
521
522    fn cache_otlp_legacy(
523        &self,
524        names: &[String],
525        ctx: &QueryContextRef,
526        is_legacy: bool,
527    ) -> server_error::Result<()> {
528        let cache = self
529            .otlp_metrics_table_legacy_cache
530            .entry(ctx.get_db_string())
531            .or_default();
532        cache_legacy_mode(&cache, names, is_legacy)
533    }
534}
535
536fn fast_legacy_check(
537    cache: &DashMap<String, bool>,
538    names: &[String],
539) -> server_error::Result<Option<bool>> {
540    let hit_cache = names
541        .iter()
542        .filter_map(|name| cache.get(name))
543        .collect::<Vec<_>>();
544    if !hit_cache.is_empty() {
545        let hit_legacy = hit_cache.iter().any(|en| *en.value());
546        let hit_prom = hit_cache.iter().any(|en| !*en.value());
547
548        // hit but have true and false, means both legacy and new mode are used
549        // we cannot handle this case, so return error
550        // add doc links in err msg later
551        ensure!(!(hit_legacy && hit_prom), OtlpMetricModeIncompatibleSnafu);
552
553        Ok(Some(hit_legacy))
554    } else {
555        Ok(None)
556    }
557}
558
559fn cache_legacy_mode(
560    cache: &DashMap<String, bool>,
561    names: &[String],
562    is_legacy: bool,
563) -> server_error::Result<()> {
564    for name in names {
565        let cached = cache.entry(name.clone()).or_insert(is_legacy);
566        ensure!(*cached == is_legacy, OtlpMetricModeIncompatibleSnafu);
567    }
568    Ok(())
569}
570
571/// If the relevant variables are set, the timeout is enforced for all PostgreSQL statements.
572/// For MySQL, it applies only to read-only statements.
573fn derive_timeout(stmt: &Statement, query_ctx: &QueryContextRef) -> Option<Duration> {
574    let query_timeout = query_ctx.query_timeout()?;
575    if query_timeout.is_zero() {
576        return None;
577    }
578    match query_ctx.channel() {
579        Channel::Mysql if stmt.is_readonly() => Some(query_timeout),
580        Channel::Postgres => Some(query_timeout),
581        _ => None,
582    }
583}
584
585/// Derives timeout for plan execution.
586fn derive_timeout_for_plan(plan: &LogicalPlan, query_ctx: &QueryContextRef) -> Option<Duration> {
587    let query_timeout = query_ctx.query_timeout()?;
588    if query_timeout.is_zero() {
589        return None;
590    }
591    match query_ctx.channel() {
592        Channel::Mysql if is_readonly_plan(plan) => Some(query_timeout),
593        Channel::Postgres => Some(query_timeout),
594        _ => None,
595    }
596}
597
598fn record_explain_analyze_timeout(
599    recorder: Option<&SlowQueryRecorder>,
600    plan: Option<&Arc<dyn ExecutionPlan>>,
601) {
602    let Some(recorder) = recorder else {
603        return;
604    };
605    let metrics = plan
606        .and_then(|plan| query::analyze_plan_metrics_to_json_value(plan, true).ok())
607        .unwrap_or_else(|| serde_json::json!([]));
608    recorder.force_record_with_payload(serde_json::json!({
609        "timed_out": true,
610        "metrics": metrics,
611    }));
612}
613
614fn attach_timeout(
615    output: Output,
616    mut timeout: Duration,
617    timeout_recorder: Option<SlowQueryRecorder>,
618) -> Result<Output> {
619    if timeout.is_zero() {
620        return StatementTimeoutSnafu.fail();
621    }
622
623    let plan = timeout_recorder
624        .as_ref()
625        .and_then(|_| output.meta.plan.clone());
626    let output = match output.data {
627        OutputData::AffectedRows(_) | OutputData::RecordBatches(_) => output,
628        OutputData::Stream(mut stream) => {
629            let schema = stream.schema();
630            let s = Box::pin(stream! {
631                let mut start = tokio::time::Instant::now();
632                while let Some(item) = tokio::time::timeout(timeout, stream.next()).await.map_err(|_| {
633                    record_explain_analyze_timeout(timeout_recorder.as_ref(), plan.as_ref());
634                    StreamTimeoutSnafu.build()
635                })? {
636                    yield item;
637
638                    let now = tokio::time::Instant::now();
639                    timeout = timeout.checked_sub(now - start).unwrap_or(Duration::ZERO);
640                    start = now;
641                    // tokio::time::timeout may not return an error immediately when timeout is 0.
642                    if timeout.is_zero() {
643                        record_explain_analyze_timeout(timeout_recorder.as_ref(), plan.as_ref());
644                        StreamTimeoutSnafu.fail()?;
645                    }
646                }
647            }) as Pin<Box<dyn Stream<Item = _> + Send>>;
648            let stream = RecordBatchStreamWrapper {
649                schema,
650                stream: s,
651                output_ordering: None,
652                metrics: Default::default(),
653                span: Span::current(),
654            };
655            Output::new(OutputData::Stream(Box::pin(stream)), output.meta)
656        }
657    };
658
659    Ok(output)
660}
661
662impl Instance {
663    async fn check_sql_permission(
664        &self,
665        stmt: &Statement,
666        query_ctx: &QueryContextRef,
667    ) -> Result<()> {
668        self.plugins
669            .get::<PermissionCheckerRef>()
670            .as_ref()
671            .check_permission_with_context(
672                query_ctx.current_user(),
673                PermissionReq::SqlStatement(stmt),
674                Some(&query_ctx.current_schema()),
675            )
676            .context(PermissionSnafu)?;
677
678        let targets = match extract_tables_from_statement_checked(stmt) {
679            Some(tables) => PermissionTableTargets::resolved(
680                tables
681                    .map(|name| {
682                        table_idents_to_full_name(&name, query_ctx).map(
683                            |(catalog, schema, table)| {
684                                PermissionTableTarget::new(catalog, schema, table)
685                            },
686                        )
687                    })
688                    .collect::<std::result::Result<Vec<_>, _>>()
689                    .map_err(BoxedError::new)
690                    .context(ExternalSnafu)?,
691            ),
692            None => PermissionTableTargets::Unresolved,
693        };
694        let targets = self
695            .resolve_query_permission_targets(targets, query_ctx)
696            .await
697            .map_err(BoxedError::new)
698            .context(ExternalSnafu)?;
699        self.check_table_permission(query_ctx, PermissionReq::SqlStatement(stmt), targets)
700            .context(PermissionSnafu)?;
701        Ok(())
702    }
703
704    #[tracing::instrument(skip_all, name = "SqlQueryHandler::do_analyze_stream_query")]
705    async fn do_analyze_stream_query_inner(
706        &self,
707        query: &str,
708        query_ctx: QueryContextRef,
709    ) -> Result<Output> {
710        ensure!(!self.is_suspended(), error::SuspendedSnafu);
711
712        let query_interceptor_opt = self.plugins.get::<SqlQueryInterceptorRef<Error>>();
713        let query_interceptor = query_interceptor_opt.as_ref();
714        let query = query_interceptor.pre_parsing(query, query_ctx.clone())?;
715        let mut stmts = parse_stmt(query.as_ref(), query_ctx.sql_dialect())
716            .and_then(|stmts| query_interceptor.post_parsing(stmts, query_ctx.clone()))?;
717
718        ensure!(
719            stmts.len() == 1,
720            InvalidSqlSnafu {
721                err_msg: "only a single EXPLAIN ANALYZE VERBOSE or TQL ANALYZE VERBOSE statement is supported"
722            }
723        );
724        let mut stmt = stmts.remove(0);
725        validate_analyze_stream_statement(&mut stmt)?;
726        query_ctx.set_explain_format(AnalyzeFormat::JSON.to_string());
727
728        self.check_sql_permission(&stmt, &query_ctx).await?;
729        check_permission(self.plugins.clone(), &stmt, &query_ctx)?;
730        let catalog_name = query_ctx.current_catalog().to_string();
731        let schema_name = query_ctx.current_schema();
732        let slow_query_timer = self.statement_slow_query_timer(&stmt, schema_name.clone());
733        let ticket = self.process_manager.register_query(
734            catalog_name,
735            vec![schema_name],
736            stmt.to_string(),
737            query_ctx.conn_info().to_string(),
738            Some(query_ctx.process_id()),
739            slow_query_timer,
740        );
741        let query_fut =
742            self.exec_statement_with_timeout(stmt, query_ctx.clone(), query_interceptor, None);
743        let output = CancellableFuture::new(query_fut, ticket.cancellation_handle.clone())
744            .await
745            .map_err(|_| error::CancelledSnafu.build())??;
746        let Output { meta, data } = output;
747        let data = match data {
748            OutputData::Stream(stream) => OutputData::Stream(Box::pin(
749                CancellableStreamWrapper::new_cancel_on_drop(stream, ticket),
750            )),
751            other => other,
752        };
753        query_interceptor.post_execute(Output { data, meta }, query_ctx)
754    }
755
756    #[tracing::instrument(skip_all, name = "SqlQueryHandler::do_query")]
757    async fn do_query_inner(&self, query: &str, query_ctx: QueryContextRef) -> Vec<Result<Output>> {
758        if self.is_suspended() {
759            return vec![error::SuspendedSnafu {}.fail()];
760        }
761
762        let query_interceptor_opt = self.plugins.get::<SqlQueryInterceptorRef<Error>>();
763        let query_interceptor = query_interceptor_opt.as_ref();
764        let query = match query_interceptor.pre_parsing(query, query_ctx.clone()) {
765            Ok(q) => q,
766            Err(e) => return vec![Err(e)],
767        };
768
769        match parse_stmt(query.as_ref(), query_ctx.sql_dialect())
770            .and_then(|stmts| query_interceptor.post_parsing(stmts, query_ctx.clone()))
771        {
772            Ok(stmts) => {
773                if stmts.is_empty() {
774                    return vec![
775                        InvalidSqlSnafu {
776                            err_msg: "empty statements",
777                        }
778                        .fail(),
779                    ];
780                }
781
782                let mut results = Vec::with_capacity(stmts.len());
783                for stmt in stmts {
784                    if let Err(e) = self.check_sql_permission(&stmt, &query_ctx).await {
785                        results.push(Err(e));
786                        break;
787                    }
788
789                    match self.query_statement(stmt.clone(), query_ctx.clone()).await {
790                        Ok(output) => {
791                            let output_result =
792                                query_interceptor.post_execute(output, query_ctx.clone());
793                            results.push(output_result);
794                        }
795                        Err(e) => {
796                            if e.status_code().should_log_error() {
797                                error!(e; "Failed to execute query: {stmt}");
798                            } else {
799                                debug!("Failed to execute query: {stmt}, {e}");
800                            }
801                            results.push(Err(e));
802                            break;
803                        }
804                    }
805                }
806                results
807            }
808            Err(e) => {
809                vec![Err(e)]
810            }
811        }
812    }
813
814    async fn exec_plan(&self, plan: LogicalPlan, query_ctx: QueryContextRef) -> Result<Output> {
815        self.query_engine
816            .execute(plan, query_ctx)
817            .await
818            .context(ExecLogicalPlanSnafu)
819    }
820
821    async fn exec_plan_with_timeout(
822        &self,
823        plan: LogicalPlan,
824        query_ctx: QueryContextRef,
825        timeout_recorder: Option<SlowQueryRecorder>,
826    ) -> Result<Output> {
827        let timeout = derive_timeout_for_plan(&plan, &query_ctx);
828        match timeout {
829            Some(timeout) => {
830                let start = tokio::time::Instant::now();
831                let output = tokio::time::timeout(timeout, self.exec_plan(plan, query_ctx))
832                    .await
833                    .map_err(|_| StatementTimeoutSnafu.build())??;
834                let output = map_query_output(output)?;
835                let remaining_timeout = timeout.checked_sub(start.elapsed()).unwrap_or_default();
836                attach_timeout(output, remaining_timeout, timeout_recorder)
837            }
838            None => self
839                .exec_plan(plan, query_ctx)
840                .await
841                .and_then(map_query_output),
842        }
843    }
844
845    async fn do_exec_plan_inner(
846        &self,
847        plan: LogicalPlan,
848        stmt: Option<Statement>,
849        query_ctx: QueryContextRef,
850    ) -> Result<Output> {
851        ensure!(!self.is_suspended(), error::SuspendedSnafu);
852
853        let query_interceptor_opt = self.plugins.get::<SqlQueryInterceptorRef<Error>>();
854        let query_interceptor = query_interceptor_opt.as_ref();
855
856        query_interceptor.pre_execute(stmt.as_ref(), Some(&plan), query_ctx.clone())?;
857
858        // TQL EXPLAIN/ANALYZE formats are consumed from the query context at
859        // execution time (see `optimize_physical_plan`); re-apply the side
860        // effect of `plan_tql` that was lost when the plan was built during
861        // Describe. `explain_format` is per-query state, so this never
862        // overwrites anything.
863        if let Some(Statement::Tql(tql)) = &stmt {
864            let format = match tql {
865                Tql::Explain(explain) => explain.format.as_ref(),
866                Tql::Analyze(analyze) => analyze.format.as_ref(),
867                Tql::Eval(_) => None,
868            };
869            if let Some(format) = format {
870                query_ctx.set_explain_format(format.to_string());
871            }
872        }
873
874        let query = stmt
875            .as_ref()
876            .map(|s| s.to_string())
877            .unwrap_or_else(|| plan.display_indent().to_string());
878
879        let plan_is_readonly = is_readonly_plan(&plan);
880        let result = if should_track_plan_process(stmt.as_ref(), &plan) {
881            let catalog_name = query_ctx.current_catalog().to_string();
882            let schema_name = query_ctx.current_schema();
883            let slow_query_timer = if plan_is_readonly {
884                self.slow_query_options.enable.then(|| {
885                    SlowQueryTimer::new(
886                        CatalogQueryStatement::Plan(query.clone()),
887                        schema_name.clone(),
888                        self.slow_query_options.threshold,
889                        self.slow_query_options.sample_ratio,
890                        self.slow_query_options.record_type,
891                        self.slow_query_recorder.clone(),
892                    )
893                })
894            } else {
895                None
896            };
897
898            let timeout_recorder = stmt
899                .as_ref()
900                .is_some_and(is_explain_analyze_verbose)
901                .then(|| slow_query_timer.as_ref().map(SlowQueryTimer::recorder))
902                .flatten();
903            let ticket = self.process_manager.register_query(
904                catalog_name,
905                vec![schema_name],
906                query,
907                query_ctx.conn_info().to_string(),
908                Some(query_ctx.process_id()),
909                slow_query_timer,
910            );
911
912            let query_fut = self.exec_plan_with_timeout(plan, query_ctx.clone(), timeout_recorder);
913
914            CancellableFuture::new(query_fut, ticket.cancellation_handle.clone())
915                .await
916                .map_err(|_| error::CancelledSnafu.build())?
917                .map(|output| {
918                    let Output { meta, data } = output;
919
920                    let data = match data {
921                        OutputData::Stream(stream) => OutputData::Stream(Box::pin(
922                            CancellableStreamWrapper::new(stream, ticket),
923                        )),
924                        other => other,
925                    };
926                    Output { data, meta }
927                })
928        } else {
929            self.exec_plan_with_timeout(plan, query_ctx.clone(), None)
930                .await
931        };
932
933        result.and_then(|output| query_interceptor.post_execute(output, query_ctx))
934    }
935
936    #[tracing::instrument(skip_all, name = "SqlQueryHandler::do_promql_query")]
937    async fn do_promql_query_inner(
938        &self,
939        query: &PromQuery,
940        query_ctx: QueryContextRef,
941    ) -> Vec<Result<Output>> {
942        if self.is_suspended() {
943            return vec![error::SuspendedSnafu {}.fail()];
944        }
945
946        // check will be done in prometheus handler's do_query
947        let result = PrometheusHandler::do_query(self, query, query_ctx)
948            .await
949            .with_context(|_| ExecutePromqlSnafu {
950                query: format!("{query:?}"),
951            });
952        vec![result]
953    }
954
955    /// Builds the [`DataFrame`] for an information-schema-backed `SHOW`
956    /// statement; `None` for other statements. The future is boxed to keep
957    /// `do_describe_inner`'s state machine small.
958    fn show_statement_dataframe<'a>(
959        &'a self,
960        stmt: &'a Statement,
961        query_ctx: &'a QueryContextRef,
962    ) -> Pin<Box<dyn Future<Output = Option<query::error::Result<DataFrame>>> + Send + 'a>> {
963        Box::pin(async move {
964            let engine = &self.query_engine;
965            let catalog_manager = self.catalog_manager();
966            let ctx = query_ctx.clone();
967            let dataframe = match stmt {
968                Statement::ShowDatabases(show) => {
969                    query::sql::show_databases_dataframe(show, engine, catalog_manager, ctx).await
970                }
971                Statement::ShowTables(show) => {
972                    query::sql::show_tables_dataframe(show, engine, catalog_manager, ctx).await
973                }
974                Statement::ShowViews(show) => {
975                    query::sql::show_views_dataframe(show, engine, catalog_manager, ctx).await
976                }
977                Statement::ShowFlows(show) => {
978                    query::sql::show_flows_dataframe(show, engine, catalog_manager, ctx).await
979                }
980                Statement::ShowColumns(show) => {
981                    query::sql::show_columns_dataframe(show, engine, catalog_manager, ctx).await
982                }
983                Statement::ShowTableStatus(show) => {
984                    query::sql::show_table_status_dataframe(show, engine, catalog_manager, ctx)
985                        .await
986                }
987                Statement::ShowCharset(kind) => {
988                    query::sql::show_charsets_dataframe(kind, engine, catalog_manager, ctx).await
989                }
990                Statement::ShowCollation(kind) => {
991                    query::sql::show_collations_dataframe(kind, engine, catalog_manager, ctx).await
992                }
993                Statement::ShowIndex(show) => {
994                    query::sql::show_index_dataframe(show, engine, catalog_manager, ctx).await
995                }
996                Statement::ShowRegion(show) => {
997                    query::sql::show_region_dataframe(show, engine, catalog_manager, ctx).await
998                }
999                Statement::ShowProcesslist(show) => {
1000                    query::sql::show_processlist_dataframe(show, engine, catalog_manager, ctx).await
1001                }
1002                _ => return None,
1003            };
1004            Some(dataframe)
1005        })
1006    }
1007
1008    async fn do_describe_inner(
1009        &self,
1010        stmt: Statement,
1011        query_ctx: QueryContextRef,
1012    ) -> Result<Option<DescribeResult>> {
1013        ensure!(!self.is_suspended(), error::SuspendedSnafu);
1014
1015        // EXPLAIN / EXPLAIN ANALYZE wrap an inner statement; describe them when the
1016        // wrapped statement is something we already plan (so that bind parameters
1017        // in the inner query get their types inferred). See #8029.
1018        let is_inner_plannable = |s: &Statement| {
1019            matches!(
1020                s,
1021                Statement::Insert(_) | Statement::Query(_) | Statement::Delete(_)
1022            )
1023        };
1024        let plannable = is_inner_plannable(&stmt)
1025            || matches!(&stmt, Statement::Explain(explain) if is_inner_plannable(explain.statement.as_ref()));
1026
1027        if let Statement::Tql(tql) = stmt {
1028            // TQL produces a logical plan; describe it from the plan so the
1029            // extended-protocol RowDescription matches the executed DataRows.
1030            self.check_sql_permission(&Statement::Tql(tql.clone()), &query_ctx)
1031                .await?;
1032            let plan = self.statement_executor.plan_tql(tql, &query_ctx).await?;
1033            return self
1034                .query_engine
1035                .describe(plan, query_ctx)
1036                .await
1037                .map(Some)
1038                .context(error::DescribeStatementSnafu);
1039        }
1040
1041        // Describe SHOW statements from the same projection the executor builds.
1042        if let Some(dataframe) = self
1043            .show_statement_dataframe(&stmt, &query_ctx)
1044            .await
1045            .transpose()
1046            .context(PlanStatementSnafu)?
1047        {
1048            self.check_sql_permission(&stmt, &query_ctx).await?;
1049            let plan = dataframe.into_unoptimized_plan();
1050            return self
1051                .query_engine
1052                .describe(plan, query_ctx)
1053                .await
1054                .map(Some)
1055                .context(error::DescribeStatementSnafu);
1056        }
1057
1058        if plannable {
1059            self.check_sql_permission(&stmt, &query_ctx).await?;
1060
1061            let plan = self
1062                .query_engine
1063                .planner()
1064                .plan(&QueryStatement::Sql(stmt), query_ctx.clone())
1065                .await
1066                .context(PlanStatementSnafu)?;
1067            self.query_engine
1068                .describe(plan, query_ctx)
1069                .await
1070                .map(Some)
1071                .context(error::DescribeStatementSnafu)
1072        } else {
1073            Ok(None)
1074        }
1075    }
1076
1077    async fn is_valid_schema_inner(&self, catalog: &str, schema: &str) -> Result<bool> {
1078        self.catalog_manager
1079            .schema_exists(catalog, schema, None)
1080            .await
1081            .context(error::CatalogSnafu)
1082    }
1083}
1084
1085#[async_trait]
1086impl SqlQueryHandler for Instance {
1087    async fn do_query(
1088        &self,
1089        query: &str,
1090        query_ctx: QueryContextRef,
1091    ) -> Vec<server_error::Result<Output>> {
1092        self.do_query_inner(query, query_ctx)
1093            .await
1094            .into_iter()
1095            .map(|result| result.map_err(BoxedError::new).context(ExecuteQuerySnafu))
1096            .collect()
1097    }
1098
1099    async fn do_analyze_stream_query(
1100        &self,
1101        query: &str,
1102        query_ctx: QueryContextRef,
1103    ) -> server_error::Result<Output> {
1104        self.do_analyze_stream_query_inner(query, query_ctx)
1105            .await
1106            .map_err(BoxedError::new)
1107            .context(ExecuteQuerySnafu)
1108    }
1109
1110    async fn do_exec_plan(
1111        &self,
1112        plan: LogicalPlan,
1113        stmt: Option<Statement>,
1114        query_ctx: QueryContextRef,
1115    ) -> server_error::Result<Output> {
1116        self.do_exec_plan_inner(plan, stmt, query_ctx)
1117            .await
1118            .map_err(BoxedError::new)
1119            .context(server_error::ExecutePlanSnafu)
1120    }
1121
1122    async fn do_promql_query(
1123        &self,
1124        query: &PromQuery,
1125        query_ctx: QueryContextRef,
1126    ) -> Vec<server_error::Result<Output>> {
1127        self.do_promql_query_inner(query, query_ctx)
1128            .await
1129            .into_iter()
1130            .map(|result| result.map_err(BoxedError::new).context(ExecuteQuerySnafu))
1131            .collect()
1132    }
1133
1134    async fn do_describe(
1135        &self,
1136        stmt: Statement,
1137        query_ctx: QueryContextRef,
1138    ) -> server_error::Result<Option<DescribeResult>> {
1139        self.do_describe_inner(stmt, query_ctx)
1140            .await
1141            .map_err(BoxedError::new)
1142            .context(server_error::DescribeStatementSnafu)
1143    }
1144
1145    async fn is_valid_schema(&self, catalog: &str, schema: &str) -> server_error::Result<bool> {
1146        self.is_valid_schema_inner(catalog, schema)
1147            .await
1148            .map_err(BoxedError::new)
1149            .context(server_error::CheckDatabaseValiditySnafu)
1150    }
1151}
1152
1153/// Expands scan-time dictionaries only when a query result leaves the frontend.
1154pub(crate) fn map_query_output(output: Output) -> Result<Output> {
1155    output
1156        .map_dictionary_to_values()
1157        .context(CollectRecordbatchSnafu)
1158}
1159
1160/// Attaches a timer to the output and observes it once the output is exhausted.
1161pub fn attach_timer(output: Output, timer: HistogramTimer) -> Output {
1162    match output.data {
1163        OutputData::AffectedRows(_) | OutputData::RecordBatches(_) => output,
1164        OutputData::Stream(stream) => {
1165            let stream = OnDone::new(stream, move || {
1166                timer.observe_duration();
1167            });
1168            Output::new(OutputData::Stream(Box::pin(stream)), output.meta)
1169        }
1170    }
1171}
1172
1173impl Instance {
1174    fn check_prom_query_privilege(&self, query_ctx: &QueryContextRef) -> server_error::Result<()> {
1175        self.plugins
1176            .get::<PermissionCheckerRef>()
1177            .as_ref()
1178            .check_permission(
1179                query_ctx.current_user(),
1180                PermissionReq::Action(PROMQL_QUERY),
1181            )
1182            .context(AuthSnafu)?;
1183        Ok(())
1184    }
1185
1186    fn prom_expr_permission_targets(
1187        &self,
1188        expr: &promql_parser::parser::Expr,
1189        query_ctx: &QueryContextRef,
1190    ) -> server_error::Result<Option<Vec<PermissionTableTarget>>> {
1191        extract_tables_from_prom_expr_checked(expr)
1192            .map(|tables| {
1193                tables
1194                    .map(|name| {
1195                        table_idents_to_full_name(&name, query_ctx).map(
1196                            |(catalog, schema, table)| {
1197                                PermissionTableTarget::new(catalog, schema, table)
1198                            },
1199                        )
1200                    })
1201                    .collect::<std::result::Result<Vec<_>, _>>()
1202                    .map_err(BoxedError::new)
1203                    .context(ExecuteQuerySnafu)
1204            })
1205            .transpose()
1206    }
1207
1208    async fn is_physical_query_permission_target(
1209        &self,
1210        target: &PermissionTableTarget,
1211        query_ctx: &QueryContextRef,
1212    ) -> server_error::Result<bool> {
1213        self.catalog_manager
1214            .table(
1215                &target.catalog,
1216                &target.schema,
1217                &target.table,
1218                Some(query_ctx),
1219            )
1220            .await
1221            .map(|table| table.is_some_and(|table| table.table_info().is_physical_table()))
1222            .map_err(BoxedError::new)
1223            .context(ExecuteQuerySnafu)
1224    }
1225
1226    async fn resolve_query_permission_targets(
1227        &self,
1228        targets: PermissionTableTargets,
1229        query_ctx: &QueryContextRef,
1230    ) -> server_error::Result<PermissionTableTargets> {
1231        const CONCURRENCY: usize = 8;
1232
1233        let checker = self.plugins.get::<PermissionCheckerRef>();
1234        if !checker.as_ref().uses_table_targets() {
1235            return Ok(targets);
1236        }
1237
1238        let PermissionTableTargets::Resolved(mut targets) = targets else {
1239            return Ok(PermissionTableTargets::Unresolved);
1240        };
1241        if targets.len() > 1 {
1242            let mut seen = HashSet::with_capacity(targets.len());
1243            targets.retain(|target| seen.insert(target.clone()));
1244        }
1245        if let [target] = targets.as_slice() {
1246            return if self
1247                .is_physical_query_permission_target(target, query_ctx)
1248                .await?
1249            {
1250                Ok(PermissionTableTargets::Unresolved)
1251            } else {
1252                Ok(PermissionTableTargets::resolved(targets))
1253            };
1254        }
1255
1256        // Bound catalog load and inspect results in target order to preserve serial semantics.
1257        for chunk in targets.chunks(CONCURRENCY) {
1258            let results = future::join_all(
1259                chunk
1260                    .iter()
1261                    .map(|target| self.is_physical_query_permission_target(target, query_ctx)),
1262            )
1263            .await;
1264            for result in results {
1265                if result? {
1266                    return Ok(PermissionTableTargets::Unresolved);
1267                }
1268            }
1269        }
1270
1271        Ok(PermissionTableTargets::resolved(targets))
1272    }
1273
1274    fn prom_queries_permission_targets(
1275        &self,
1276        queries: &[ParsedPromQuery],
1277        query_ctx: &QueryContextRef,
1278    ) -> server_error::Result<PermissionTableTargets> {
1279        let mut targets = Vec::new();
1280        let mut resolved = true;
1281
1282        for query in queries {
1283            let QueryStatement::Promql(eval_stmt, _) = query.statement() else {
1284                unreachable!("query is parsed from promql");
1285            };
1286
1287            if let Some(query_targets) =
1288                self.prom_expr_permission_targets(&eval_stmt.expr, query_ctx)?
1289            {
1290                targets.extend(query_targets);
1291            } else {
1292                resolved = false;
1293            }
1294        }
1295
1296        Ok(if resolved {
1297            PermissionTableTargets::resolved(targets)
1298        } else {
1299            PermissionTableTargets::Unresolved
1300        })
1301    }
1302}
1303
1304#[async_trait]
1305impl PrometheusHandler for Instance {
1306    #[tracing::instrument(skip_all)]
1307    async fn do_query(
1308        &self,
1309        query: &PromQuery,
1310        query_ctx: QueryContextRef,
1311    ) -> server_error::Result<Output> {
1312        let query = ParsedPromQuery::parse(query.clone(), &query_ctx)?;
1313        self.do_query_parsed(query, query_ctx).await
1314    }
1315
1316    #[tracing::instrument(skip_all)]
1317    async fn do_query_parsed(
1318        &self,
1319        query: ParsedPromQuery,
1320        query_ctx: QueryContextRef,
1321    ) -> server_error::Result<Output> {
1322        let interceptor = self
1323            .plugins
1324            .get::<PromQueryInterceptorRef<server_error::Error>>();
1325
1326        self.check_prom_query_privilege(&query_ctx)?;
1327
1328        let targets =
1329            self.prom_queries_permission_targets(std::slice::from_ref(&query), &query_ctx)?;
1330        self.check_query_target_permission(targets, &query_ctx)
1331            .await?;
1332
1333        let requires_output_ordering = query.requires_output_ordering();
1334        let (query, stmt) = query.into_parts();
1335
1336        let QueryStatement::Promql(eval_stmt, _) = &stmt else {
1337            unreachable!("query is parsed from promql");
1338        };
1339
1340        let plan = self
1341            .statement_executor
1342            .plan(&stmt, query_ctx.clone())
1343            .await
1344            .map_err(BoxedError::new)
1345            .context(ExecuteQuerySnafu)?;
1346
1347        let plan = if requires_output_ordering {
1348            plan
1349        } else {
1350            promql::remove_output_sort(plan)
1351        };
1352
1353        interceptor.pre_execute(&query, &eval_stmt.expr, Some(&plan), query_ctx.clone())?;
1354
1355        // Take the EvalStmt from the original QueryStatement and use it to create the CatalogQueryStatement.
1356        let query_statement = if let QueryStatement::Promql(eval_stmt, alias) = stmt {
1357            CatalogQueryStatement::Promql(eval_stmt, alias)
1358        } else {
1359            // It should not happen since the query is already parsed successfully.
1360            return UnexpectedResultSnafu {
1361                reason: "The query should always be promql.".to_string(),
1362            }
1363            .fail();
1364        };
1365        let raw_query = query_statement.to_string();
1366
1367        let slow_query_timer = self.slow_query_options.enable.then(|| {
1368            SlowQueryTimer::new(
1369                query_statement,
1370                query_ctx.current_schema(),
1371                self.slow_query_options.threshold,
1372                self.slow_query_options.sample_ratio,
1373                self.slow_query_options.record_type,
1374                self.slow_query_recorder.clone(),
1375            )
1376        });
1377
1378        let ticket = self.process_manager.register_query(
1379            query_ctx.current_catalog().to_string(),
1380            vec![query_ctx.current_schema()],
1381            raw_query,
1382            query_ctx.conn_info().to_string(),
1383            Some(query_ctx.process_id()),
1384            slow_query_timer,
1385        );
1386
1387        let query_fut = self.statement_executor.exec_plan(plan, query_ctx.clone());
1388
1389        let output = CancellableFuture::new(query_fut, ticket.cancellation_handle.clone())
1390            .await
1391            .map_err(|_| servers::error::CancelledSnafu.build())?
1392            .map_err(BoxedError::new)
1393            .context(ExecuteQuerySnafu)?;
1394        let output = map_query_output(output)
1395            .map_err(BoxedError::new)
1396            .context(ExecuteQuerySnafu)?;
1397        let Output { meta, data } = output;
1398        let data = match data {
1399            OutputData::Stream(stream) => {
1400                OutputData::Stream(Box::pin(CancellableStreamWrapper::new(stream, ticket)))
1401            }
1402            other => other,
1403        };
1404        let output = Output { data, meta };
1405        Ok(interceptor.post_execute(output, query_ctx)?)
1406    }
1407
1408    async fn check_query_permission(
1409        &self,
1410        queries: &[PromQuery],
1411        query_ctx: &QueryContextRef,
1412    ) -> server_error::Result<()> {
1413        let queries = queries
1414            .iter()
1415            .cloned()
1416            .map(|query| ParsedPromQuery::parse(query, query_ctx))
1417            .collect::<server_error::Result<Vec<_>>>()?;
1418        self.check_query_permission_parsed(&queries, query_ctx)
1419            .await
1420    }
1421
1422    async fn check_query_permission_parsed(
1423        &self,
1424        queries: &[ParsedPromQuery],
1425        query_ctx: &QueryContextRef,
1426    ) -> server_error::Result<()> {
1427        self.check_prom_query_privilege(query_ctx)?;
1428        let targets = self.prom_queries_permission_targets(queries, query_ctx)?;
1429        self.check_query_target_permission(targets, query_ctx).await
1430    }
1431
1432    async fn check_query_target_permission(
1433        &self,
1434        targets: PermissionTableTargets,
1435        query_ctx: &QueryContextRef,
1436    ) -> server_error::Result<()> {
1437        let targets = self
1438            .resolve_query_permission_targets(targets, query_ctx)
1439            .await?;
1440        self.check_table_permission(query_ctx, PermissionReq::Action(PROMQL_QUERY), targets)
1441            .context(AuthSnafu)?;
1442        Ok(())
1443    }
1444
1445    async fn filter_metadata_metric_names(
1446        &self,
1447        metric_names: Vec<String>,
1448        schema: &str,
1449        query_ctx: &QueryContextRef,
1450    ) -> server_error::Result<Vec<String>> {
1451        let checker = self.plugins.get::<PermissionCheckerRef>();
1452        if !checker.as_ref().uses_table_targets() {
1453            let Some(metric) = metric_names.first() else {
1454                return Ok(metric_names);
1455            };
1456            let target =
1457                PermissionTableTarget::new(query_ctx.current_catalog(), schema, metric.as_str());
1458            let result = checker
1459                .as_ref()
1460                .check_permission_with_table_targets(
1461                    query_ctx.current_user(),
1462                    PermissionReq::Action(PROMQL_QUERY),
1463                    PermissionTableTargets::resolved(vec![target]),
1464                )
1465                .context(AuthSnafu);
1466            return match result {
1467                Ok(_) => Ok(metric_names),
1468                Err(error)
1469                    if error.status_code()
1470                        == common_error::status_code::StatusCode::PermissionDenied =>
1471                {
1472                    Ok(Vec::new())
1473                }
1474                Err(error) => Err(error),
1475            };
1476        }
1477
1478        let mut allowed = Vec::with_capacity(metric_names.len());
1479        for metric in metric_names {
1480            let target =
1481                PermissionTableTarget::new(query_ctx.current_catalog(), schema, metric.as_str());
1482            match checker
1483                .as_ref()
1484                .check_permission_with_table_targets(
1485                    query_ctx.current_user(),
1486                    PermissionReq::Action(PROMQL_QUERY),
1487                    PermissionTableTargets::resolved(vec![target]),
1488                )
1489                .context(AuthSnafu)
1490            {
1491                Ok(_) => allowed.push(metric),
1492                Err(error)
1493                    if error.status_code()
1494                        == common_error::status_code::StatusCode::PermissionDenied => {}
1495                Err(error) => return Err(error),
1496            }
1497        }
1498        Ok(allowed)
1499    }
1500
1501    async fn query_metric_names(
1502        &self,
1503        matchers: Vec<Matcher>,
1504        schema: &str,
1505        ctx: &QueryContextRef,
1506    ) -> server_error::Result<Vec<String>> {
1507        self.handle_query_metric_names(matchers, schema, ctx)
1508            .await
1509            .map_err(BoxedError::new)
1510            .context(ExecuteQuerySnafu)
1511    }
1512
1513    async fn query_metric_names_by_labels(
1514        &self,
1515        matchers: Vec<Matcher>,
1516        schema: &str,
1517        start: SystemTime,
1518        end: SystemTime,
1519        ctx: &QueryContextRef,
1520    ) -> server_error::Result<Vec<String>> {
1521        self.handle_query_metric_names_by_labels(matchers, schema, start, end, ctx)
1522            .await
1523            .map_err(BoxedError::new)
1524            .context(ExecuteQuerySnafu)
1525    }
1526
1527    async fn query_label_values(
1528        &self,
1529        metric: String,
1530        label_name: String,
1531        matchers: Vec<Matcher>,
1532        start: SystemTime,
1533        end: SystemTime,
1534        ctx: &QueryContextRef,
1535    ) -> server_error::Result<Vec<String>> {
1536        let schema =
1537            resolve_schema_from_matchers(&matchers)?.unwrap_or_else(|| ctx.current_schema());
1538        let target = PermissionTableTarget::new(ctx.current_catalog(), schema.as_str(), &metric);
1539        self.check_query_target_permission(
1540            PermissionTableTargets::resolved(vec![target.clone()]),
1541            ctx,
1542        )
1543        .await?;
1544
1545        self.handle_query_label_values(target, label_name, matchers, start, end, ctx)
1546            .await
1547            .map_err(BoxedError::new)
1548            .context(ExecuteQuerySnafu)
1549    }
1550
1551    fn catalog_manager(&self) -> CatalogManagerRef {
1552        self.catalog_manager.clone()
1553    }
1554}
1555
1556/// Validate `stmt.database` permission if it's presented.
1557macro_rules! validate_db_permission {
1558    ($stmt: expr, $query_ctx: expr) => {
1559        if let Some(database) = &$stmt.database {
1560            validate_catalog_and_schema($query_ctx.current_catalog(), database, $query_ctx)
1561                .map_err(BoxedError::new)
1562                .context(SqlExecInterceptedSnafu)?;
1563        }
1564    };
1565}
1566
1567pub fn check_permission(
1568    plugins: Plugins,
1569    stmt: &Statement,
1570    query_ctx: &QueryContextRef,
1571) -> Result<()> {
1572    let need_validate = plugins
1573        .get::<QueryOptions>()
1574        .map(|opts| opts.disallow_cross_catalog_query)
1575        .unwrap_or_default();
1576
1577    if !need_validate {
1578        return Ok(());
1579    }
1580
1581    match stmt {
1582        // Will be checked in execution.
1583        // TODO(dennis): add a hook for admin commands.
1584        Statement::Admin(_) => {}
1585        // These are executed by query engine, and will be checked there.
1586        Statement::Query(_)
1587        | Statement::Explain(_)
1588        | Statement::Tql(_)
1589        | Statement::Delete(_)
1590        | Statement::DeclareCursor(_)
1591        | Statement::Copy(sql::statements::copy::Copy::CopyQueryTo(_)) => {}
1592        // database ops won't be checked
1593        Statement::CreateDatabase(_)
1594        | Statement::ShowDatabases(_)
1595        | Statement::DropDatabase(_)
1596        | Statement::AlterDatabase(_)
1597        | Statement::DropFlow(_)
1598        | Statement::Use(_) => {}
1599        #[cfg(feature = "enterprise")]
1600        Statement::DropTrigger(_) => {}
1601        Statement::ShowCreateDatabase(stmt) => {
1602            validate_database(&stmt.database_name, query_ctx)?;
1603        }
1604        Statement::ShowCreateTable(stmt) => {
1605            validate_param(&stmt.table_name, query_ctx)?;
1606        }
1607        Statement::ShowCreateFlow(stmt) => {
1608            validate_flow(&stmt.flow_name, query_ctx)?;
1609        }
1610        #[cfg(feature = "enterprise")]
1611        Statement::ShowCreateTrigger(stmt) => {
1612            validate_param(&stmt.trigger_name, query_ctx)?;
1613        }
1614        Statement::ShowCreateView(stmt) => {
1615            validate_param(&stmt.view_name, query_ctx)?;
1616        }
1617        Statement::CreateExternalTable(stmt) => {
1618            validate_param(&stmt.name, query_ctx)?;
1619        }
1620        Statement::CreateFlow(stmt) => {
1621            // TODO: should also validate source table name here?
1622            validate_param(&stmt.sink_table_name, query_ctx)?;
1623        }
1624        #[cfg(feature = "enterprise")]
1625        Statement::CreateTrigger(stmt) => {
1626            validate_param(&stmt.trigger_name, query_ctx)?;
1627        }
1628        Statement::CreateView(stmt) => {
1629            validate_param(&stmt.name, query_ctx)?;
1630        }
1631        Statement::AlterTable(stmt) => {
1632            validate_param(stmt.table_name(), query_ctx)?;
1633        }
1634        #[cfg(feature = "enterprise")]
1635        Statement::AlterTrigger(_) => {}
1636        // set/show variable now only alter/show variable in session
1637        Statement::SetVariables(_) | Statement::ShowVariables(_) => {}
1638        // show charset and show collation won't be checked
1639        Statement::ShowCharset(_) | Statement::ShowCollation(_) => {}
1640
1641        Statement::Comment(comment) => match &comment.object {
1642            CommentObject::Table(table) => validate_param(table, query_ctx)?,
1643            CommentObject::Column { table, .. } => validate_param(table, query_ctx)?,
1644            CommentObject::Flow(flow) => validate_flow(flow, query_ctx)?,
1645        },
1646
1647        Statement::Insert(insert) => {
1648            let name = insert.table_name().context(ParseSqlSnafu)?;
1649            validate_param(name, query_ctx)?;
1650        }
1651        Statement::CreateTable(stmt) => {
1652            validate_param(&stmt.name, query_ctx)?;
1653        }
1654        Statement::CreateTableLike(stmt) => {
1655            validate_param(&stmt.table_name, query_ctx)?;
1656            validate_param(&stmt.source_name, query_ctx)?;
1657        }
1658        Statement::DropTable(drop_stmt) => {
1659            for table_name in drop_stmt.table_names() {
1660                validate_param(table_name, query_ctx)?;
1661            }
1662        }
1663        #[cfg(feature = "enterprise")]
1664        Statement::UndropTable(stmt) => {
1665            validate_param(stmt.table_name(), query_ctx)?;
1666        }
1667        Statement::DropView(stmt) => {
1668            validate_param(&stmt.view_name, query_ctx)?;
1669        }
1670        Statement::ShowTables(stmt) => {
1671            validate_db_permission!(stmt, query_ctx);
1672        }
1673        Statement::ShowTableStatus(stmt) => {
1674            validate_db_permission!(stmt, query_ctx);
1675        }
1676        Statement::ShowColumns(stmt) => {
1677            validate_db_permission!(stmt, query_ctx);
1678        }
1679        Statement::ShowIndex(stmt) => {
1680            validate_db_permission!(stmt, query_ctx);
1681        }
1682        Statement::ShowRegion(stmt) => {
1683            validate_db_permission!(stmt, query_ctx);
1684        }
1685        Statement::ShowViews(stmt) => {
1686            validate_db_permission!(stmt, query_ctx);
1687        }
1688        Statement::ShowFlows(stmt) => {
1689            validate_db_permission!(stmt, query_ctx);
1690        }
1691        Statement::ShowFlowStatus(_stmt) => {
1692            // Flow statistics are organized based on the catalog dimension and
1693            // filtered by the current catalog, so there is no need to check the
1694            // permission of the database(schema).
1695        }
1696        #[cfg(feature = "enterprise")]
1697        Statement::ShowTriggers(_stmt) => {
1698            // The trigger is organized based on the catalog dimension, so there
1699            // is no need to check the permission of the database(schema).
1700        }
1701        Statement::ShowStatus(_stmt) => {}
1702        Statement::ShowSearchPath(_stmt) => {}
1703        Statement::DescribeTable(stmt) => {
1704            validate_param(stmt.name(), query_ctx)?;
1705        }
1706        Statement::Copy(sql::statements::copy::Copy::CopyTable(stmt)) => match stmt {
1707            CopyTable::To(copy_table_to) => validate_param(&copy_table_to.table_name, query_ctx)?,
1708            CopyTable::From(copy_table_from) => {
1709                validate_param(&copy_table_from.table_name, query_ctx)?
1710            }
1711        },
1712        Statement::Copy(sql::statements::copy::Copy::CopyDatabase(copy_database)) => {
1713            match copy_database {
1714                CopyDatabase::To(stmt) => validate_database(&stmt.database_name, query_ctx)?,
1715                CopyDatabase::From(stmt) => validate_database(&stmt.database_name, query_ctx)?,
1716            }
1717        }
1718        Statement::TruncateTable(stmt) => {
1719            validate_param(stmt.table_name(), query_ctx)?;
1720        }
1721        // cursor operations are always allowed once it's created
1722        Statement::FetchCursor(_) | Statement::CloseCursor(_) => {}
1723        // User can only kill process in their own catalog.
1724        Statement::Kill(_) => {}
1725        // SHOW PROCESSLIST
1726        Statement::ShowProcesslist(_) => {}
1727    }
1728    Ok(())
1729}
1730
1731fn validate_param(name: &ObjectName, query_ctx: &QueryContextRef) -> Result<()> {
1732    let (catalog, schema, _) = table_idents_to_full_name(name, query_ctx)
1733        .map_err(BoxedError::new)
1734        .context(ExternalSnafu)?;
1735
1736    validate_catalog_and_schema(&catalog, &schema, query_ctx)
1737        .map_err(BoxedError::new)
1738        .context(SqlExecInterceptedSnafu)
1739}
1740
1741fn validate_flow(name: &ObjectName, query_ctx: &QueryContextRef) -> Result<()> {
1742    let catalog = match &name.0[..] {
1743        [_flow] => query_ctx.current_catalog().to_string(),
1744        [catalog, _flow] => catalog.to_string_unquoted(),
1745        _ => {
1746            return InvalidSqlSnafu {
1747                err_msg: format!(
1748                    "expect flow name to be <catalog>.<flow_name> or <flow_name>, actual: {name}",
1749                ),
1750            }
1751            .fail();
1752        }
1753    };
1754
1755    let schema = query_ctx.current_schema();
1756
1757    validate_catalog_and_schema(&catalog, &schema, query_ctx)
1758        .map_err(BoxedError::new)
1759        .context(SqlExecInterceptedSnafu)
1760}
1761
1762fn validate_database(name: &ObjectName, query_ctx: &QueryContextRef) -> Result<()> {
1763    let (catalog, schema) = match &name.0[..] {
1764        [schema] => (
1765            query_ctx.current_catalog().to_string(),
1766            schema.to_string_unquoted(),
1767        ),
1768        [catalog, schema] => (catalog.to_string_unquoted(), schema.to_string_unquoted()),
1769        _ => InvalidSqlSnafu {
1770            err_msg: format!(
1771                "expect database name to be <catalog>.<schema> or <schema>, actual: {name}",
1772            ),
1773        }
1774        .fail()?,
1775    };
1776
1777    validate_catalog_and_schema(&catalog, &schema, query_ctx)
1778        .map_err(BoxedError::new)
1779        .context(SqlExecInterceptedSnafu)
1780}
1781
1782fn is_readonly_plan(plan: &LogicalPlan) -> bool {
1783    !matches!(plan, LogicalPlan::Dml(_) | LogicalPlan::Ddl(_))
1784}
1785
1786fn should_track_statement_process(stmt: &Statement) -> bool {
1787    stmt.is_readonly()
1788        || matches!(stmt, Statement::Insert(insert) if insert.has_non_values_query_source())
1789}
1790
1791fn should_track_plan_process(stmt: Option<&Statement>, plan: &LogicalPlan) -> bool {
1792    is_readonly_plan(plan)
1793        || matches!(stmt, Some(Statement::Insert(insert)) if insert.has_non_values_query_source())
1794}
1795
1796#[cfg(test)]
1797mod tests {
1798    use std::any::Any;
1799    use std::collections::HashMap;
1800    use std::future::Future;
1801    use std::pin::Pin;
1802    use std::sync::Arc;
1803    use std::task::{Context, Poll};
1804    use std::time::Duration;
1805
1806    use api::prom_store::remote::label_matcher::Type as PromMatcherType;
1807    use api::prom_store::remote::{
1808        Label, LabelMatcher, Query as RemoteQuery, ReadRequest, ReadResponse, Sample,
1809    };
1810    use api::v1::greptime_request::Request;
1811    use api::v1::meta::{ProcedureDetailResponse, ReconcileRequest, ReconcileResponse};
1812    use api::v1::query_request::Query;
1813    use auth::{
1814        DASHBOARD_DELETE, DASHBOARD_QUERY, DASHBOARD_SAVE, JAEGER_QUERY, PIPELINE_DELETE,
1815        PIPELINE_INSERT, PIPELINE_QUERY, PermissionAction, PermissionResp, UserInfo, UserInfoRef,
1816    };
1817    use catalog::process_manager::{ProcessManager, QueryStatement, SlowQueryTimer};
1818    use common_base::Plugins;
1819    use common_catalog::consts::DEFAULT_PRIVATE_SCHEMA_NAME;
1820    use common_error::ext::{BoxedError, ErrorExt, PlainError};
1821    use common_error::status_code::StatusCode;
1822    use common_event_recorder::{Event, EventRecorder, EventTypeFilter, EventTypeFilterRef};
1823    use common_frontend::slow_query_event::SlowQueryEvent;
1824    use common_meta::cache::LayeredCacheRegistryBuilder;
1825    use common_meta::kv_backend::memory::MemoryKvBackend;
1826    use common_meta::procedure_executor::{ExecutorContext, ProcedureExecutor};
1827    use common_meta::rpc::ddl::{DdlTask, SubmitDdlTaskRequest, SubmitDdlTaskResponse};
1828    use common_meta::rpc::procedure::{
1829        MigrateRegionRequest, MigrateRegionResponse, ProcedureStateResponse,
1830    };
1831    use common_query::prelude::greptime_value;
1832    use common_query::{Output, OutputMeta};
1833    use common_recordbatch::{
1834        OrderOption, RecordBatch, RecordBatchStream, SendableRecordBatchStream,
1835    };
1836    use common_telemetry::logging::SlowQueriesRecordType;
1837    use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};
1838    use datafusion::physical_plan::empty::EmptyExec;
1839    use datafusion_expr::dml::InsertOp;
1840    use datafusion_expr::{LogicalPlanBuilder, LogicalTableSource};
1841    use datatypes::prelude::ConcreteDataType;
1842    use datatypes::schema::{ColumnSchema, Schema as GtSchema, SchemaRef as GtSchemaRef};
1843    use datatypes::vectors::{
1844        Float64Vector, StringVector, TimestampMillisecondVector, TimestampNanosecondVector,
1845        VectorRef,
1846    };
1847    use log_query::LogQuery;
1848    use prost::Message;
1849    use query::query_engine::options::QueryOptions;
1850    use servers::query_handler::{
1851        DashboardHandler, JaegerQueryHandler, LogQueryHandler, PipelineHandler, PipelineHandlerRef,
1852        PromStoreProtocolHandler,
1853    };
1854    use session::context::{Channel, ConnInfo, QueryContext, QueryContextBuilder};
1855    use snafu::{Location, Snafu};
1856    use sql::dialect::GreptimeDbDialect;
1857    use store_api::data_source::DataSource;
1858    use store_api::metric_engine_consts::{
1859        LOGICAL_TABLE_METADATA_KEY, METRIC_ENGINE_NAME, PHYSICAL_TABLE_METADATA_KEY,
1860    };
1861    use store_api::storage::ScanRequest;
1862    use strfmt::Format;
1863    use table::metadata::{
1864        FilterPushDownType, TableInfo, TableInfoBuilder, TableMetaBuilder, TableType,
1865    };
1866    use table::table_name::TableName;
1867    use table::test_util::{EmptyTable, MemTable};
1868    use table::{Table, TableRef};
1869    use tokio::sync::{mpsc, oneshot};
1870    use tower::ServiceExt;
1871
1872    use crate::frontend::FrontendOptions;
1873    use crate::instance::builder::FrontendBuilder;
1874    use crate::instance::*;
1875
1876    fn parse_test_sql(sql: &str) -> Vec<Statement> {
1877        parse_stmt(sql, &GreptimeDbDialect {}).unwrap()
1878    }
1879
1880    #[derive(Debug, Default)]
1881    struct RecordingSlowQueryEventRecorder {
1882        payloads: std::sync::Mutex<Vec<serde_json::Value>>,
1883    }
1884
1885    impl EventRecorder for RecordingSlowQueryEventRecorder {
1886        fn record(&self, event: Box<dyn Event>) {
1887            let event = event
1888                .as_any()
1889                .downcast_ref::<SlowQueryEvent>()
1890                .expect("expected a slow query event");
1891            self.payloads.lock().unwrap().push(event.payload.clone());
1892        }
1893
1894        fn event_type_filter(&self) -> EventTypeFilterRef {
1895            Arc::new(EventTypeFilter::All)
1896        }
1897
1898        fn close(&self) {}
1899    }
1900
1901    #[test]
1902    fn test_validate_analyze_stream_statement_strictness() {
1903        for sql in [
1904            "select 1",
1905            "explain analyze select 1",
1906            "explain analyze verbose format text select 1",
1907            "explain analyze verbose format graphviz select 1",
1908            "TQL ANALYZE (0, 10, '5s') physical_metric",
1909            "TQL EXPLAIN VERBOSE (0, 10, '5s') physical_metric",
1910            "TQL ANALYZE VERBOSE FORMAT TEXT (0, 10, '5s') physical_metric",
1911        ] {
1912            let mut stmts = parse_test_sql(sql);
1913            assert!(
1914                validate_analyze_stream_statement(&mut stmts[0]).is_err(),
1915                "{sql}"
1916            );
1917        }
1918
1919        for sql in [
1920            "explain analyze verbose select 1",
1921            "explain analyze verbose format json select 1",
1922            "TQL ANALYZE VERBOSE (0, 10, '5s') physical_metric",
1923            "TQL ANALYZE VERBOSE FORMAT JSON (0, 10, '5s') physical_metric",
1924        ] {
1925            let mut stmts = parse_test_sql(sql);
1926            assert!(
1927                validate_analyze_stream_statement(&mut stmts[0]).is_ok(),
1928                "{sql}"
1929            );
1930            match &stmts[0] {
1931                Statement::Explain(explain) => assert!(explain.format.is_none()),
1932                Statement::Tql(Tql::Analyze(analyze)) => assert!(analyze.format.is_none()),
1933                _ => unreachable!(),
1934            }
1935        }
1936
1937        assert_eq!(
1938            parse_test_sql("explain analyze verbose select 1; select 2").len(),
1939            2
1940        );
1941
1942        assert!(is_explain_analyze_verbose(
1943            &parse_test_sql("explain analyze verbose select 1")[0]
1944        ));
1945        assert!(is_explain_analyze_verbose(
1946            &parse_test_sql("TQL ANALYZE VERBOSE (0, 10, '5s') physical_metric")[0]
1947        ));
1948        for sql in [
1949            "select 1",
1950            "explain select 1",
1951            "explain analyze select 1",
1952            "explain verbose select 1",
1953            "TQL ANALYZE (0, 10, '5s') physical_metric",
1954            "TQL EXPLAIN VERBOSE (0, 10, '5s') physical_metric",
1955        ] {
1956            assert!(
1957                !is_explain_analyze_verbose(&parse_test_sql(sql)[0]),
1958                "{sql}"
1959            );
1960        }
1961    }
1962
1963    #[derive(Debug, Snafu)]
1964    enum TestError {
1965        #[snafu(display("Failed to build test cache registry"))]
1966        BuildCacheRegistry {
1967            source: cache::error::Error,
1968            #[snafu(implicit)]
1969            location: Location,
1970        },
1971
1972        #[snafu(display("Failed to build test table meta for table: {table_name}"))]
1973        BuildTableMeta {
1974            table_name: String,
1975            source: table::metadata::TableMetaBuilderError,
1976            #[snafu(implicit)]
1977            location: Location,
1978        },
1979
1980        #[snafu(display("Failed to build test table info for table: {table_name}"))]
1981        BuildTableInfo {
1982            table_name: String,
1983            source: table::metadata::TableInfoBuilderError,
1984            #[snafu(implicit)]
1985            location: Location,
1986        },
1987
1988        #[snafu(display("Failed to register test table: {table_name}"))]
1989        RegisterTable {
1990            table_name: String,
1991            source: catalog::error::Error,
1992            #[snafu(implicit)]
1993            location: Location,
1994        },
1995
1996        #[snafu(display("Failed to build test frontend instance"))]
1997        BuildFrontend {
1998            source: crate::error::Error,
1999            #[snafu(implicit)]
2000            location: Location,
2001        },
2002
2003        #[snafu(display("Expected exactly one output for SQL `{sql}`, got {actual}"))]
2004        UnexpectedOutputCount {
2005            sql: String,
2006            actual: usize,
2007            #[snafu(implicit)]
2008            location: Location,
2009        },
2010
2011        #[snafu(display("Failed to execute SQL `{sql}`"))]
2012        ExecuteSql {
2013            sql: String,
2014            source: crate::error::Error,
2015            #[snafu(implicit)]
2016            location: Location,
2017        },
2018
2019        #[snafu(display("Timed out waiting for insert-select start notification"))]
2020        InsertStartTimeout {
2021            source: tokio::time::error::Elapsed,
2022            #[snafu(implicit)]
2023            location: Location,
2024        },
2025
2026        #[snafu(display("Insert-select start notification channel closed"))]
2027        InsertStartChannelClosed {
2028            #[snafu(implicit)]
2029            location: Location,
2030        },
2031
2032        #[snafu(display("Failed to release blocking insert-select interceptor"))]
2033        ReleaseBlockedInsert {
2034            #[snafu(implicit)]
2035            location: Location,
2036        },
2037
2038        #[snafu(display("Timed out waiting for insert-select source to be polled"))]
2039        SourcePollTimeout {
2040            source: tokio::time::error::Elapsed,
2041            #[snafu(implicit)]
2042            location: Location,
2043        },
2044
2045        #[snafu(display("Insert-select source poll notification channel closed"))]
2046        SourcePollChannelClosed {
2047            source: oneshot::error::RecvError,
2048            #[snafu(implicit)]
2049            location: Location,
2050        },
2051
2052        #[snafu(display("Timed out waiting for insert task to finish"))]
2053        InsertTaskTimeout {
2054            source: tokio::time::error::Elapsed,
2055            #[snafu(implicit)]
2056            location: Location,
2057        },
2058
2059        #[snafu(display("Insert task panicked"))]
2060        InsertTaskPanic {
2061            source: tokio::task::JoinError,
2062            #[snafu(implicit)]
2063            location: Location,
2064        },
2065
2066        #[snafu(display("Expected insert-select to be cancelled"))]
2067        InsertSelectNotCancelled {
2068            #[snafu(implicit)]
2069            location: Location,
2070        },
2071    }
2072
2073    type TestResult<T> = std::result::Result<T, TestError>;
2074
2075    fn parse_one_sql(sql: &str) -> Statement {
2076        parse_stmt(sql, &GreptimeDbDialect {}).unwrap().remove(0)
2077    }
2078
2079    fn test_query_ctx(process_id: u32) -> QueryContextRef {
2080        Arc::new(
2081            QueryContextBuilder::default()
2082                .channel(Channel::Mysql)
2083                .conn_info(ConnInfo::new(None, Channel::Mysql))
2084                .process_id(process_id)
2085                .build(),
2086        )
2087    }
2088
2089    #[derive(Debug)]
2090    struct AdminUserInfo;
2091
2092    impl UserInfo for AdminUserInfo {
2093        fn as_any(&self) -> &dyn Any {
2094            self
2095        }
2096
2097        fn username(&self) -> &str {
2098            "admin"
2099        }
2100
2101        fn is_admin(&self) -> bool {
2102            true
2103        }
2104    }
2105
2106    struct RejectUnresolvedPermissionChecker;
2107
2108    impl PermissionChecker for RejectUnresolvedPermissionChecker {
2109        fn check_permission(
2110            &self,
2111            _user_info: UserInfoRef,
2112            _req: PermissionReq,
2113        ) -> auth::error::Result<PermissionResp> {
2114            Ok(PermissionResp::Allow)
2115        }
2116
2117        fn check_permission_with_table_targets(
2118            &self,
2119            _user_info: UserInfoRef,
2120            _req: PermissionReq,
2121            targets: PermissionTableTargets,
2122        ) -> auth::error::Result<PermissionResp> {
2123            let reject = match targets {
2124                PermissionTableTargets::Unresolved => true,
2125                PermissionTableTargets::Resolved(targets) => {
2126                    targets.iter().any(|target| target.table == "denied")
2127                }
2128            };
2129            Ok(if reject {
2130                PermissionResp::Reject
2131            } else {
2132                PermissionResp::Allow
2133            })
2134        }
2135    }
2136
2137    #[derive(Debug, PartialEq, Eq)]
2138    struct CheckedAction {
2139        action: PermissionAction,
2140        targets: Option<PermissionTableTargets>,
2141    }
2142
2143    #[derive(Default)]
2144    struct RejectEndpointPermissionChecker {
2145        checks: std::sync::Mutex<Vec<CheckedAction>>,
2146    }
2147
2148    impl RejectEndpointPermissionChecker {
2149        fn reject(
2150            &self,
2151            action: PermissionAction,
2152            targets: Option<PermissionTableTargets>,
2153        ) -> PermissionResp {
2154            self.checks
2155                .lock()
2156                .unwrap()
2157                .push(CheckedAction { action, targets });
2158            PermissionResp::Reject
2159        }
2160
2161        fn take_check(&self) -> CheckedAction {
2162            let mut checks = self.checks.lock().unwrap();
2163            assert_eq!(1, checks.len());
2164            checks.pop().unwrap()
2165        }
2166    }
2167
2168    impl PermissionChecker for RejectEndpointPermissionChecker {
2169        fn check_permission(
2170            &self,
2171            _user_info: UserInfoRef,
2172            req: PermissionReq,
2173        ) -> auth::error::Result<PermissionResp> {
2174            Ok(match req {
2175                PermissionReq::Action(action) => self.reject(action, None),
2176                _ => PermissionResp::Allow,
2177            })
2178        }
2179
2180        fn check_permission_with_table_targets(
2181            &self,
2182            _user_info: UserInfoRef,
2183            req: PermissionReq,
2184            targets: PermissionTableTargets,
2185        ) -> auth::error::Result<PermissionResp> {
2186            Ok(match req {
2187                PermissionReq::Action(action) => self.reject(action, Some(targets)),
2188                _ => PermissionResp::Allow,
2189            })
2190        }
2191    }
2192
2193    struct WriteOnlyPermissionChecker;
2194
2195    impl PermissionChecker for WriteOnlyPermissionChecker {
2196        fn check_permission(
2197            &self,
2198            _user_info: UserInfoRef,
2199            req: PermissionReq,
2200        ) -> auth::error::Result<PermissionResp> {
2201            Ok(if req.is_readonly() {
2202                PermissionResp::Reject
2203            } else {
2204                PermissionResp::Allow
2205            })
2206        }
2207
2208        fn check_permission_with_table_targets(
2209            &self,
2210            user_info: UserInfoRef,
2211            req: PermissionReq,
2212            _targets: PermissionTableTargets,
2213        ) -> auth::error::Result<PermissionResp> {
2214            self.check_permission(user_info, req)
2215        }
2216    }
2217
2218    #[derive(Default)]
2219    struct TargetIndependentPermissionChecker {
2220        checks: atomic::AtomicUsize,
2221    }
2222
2223    impl PermissionChecker for TargetIndependentPermissionChecker {
2224        fn check_permission(
2225            &self,
2226            _user_info: UserInfoRef,
2227            _req: PermissionReq,
2228        ) -> auth::error::Result<PermissionResp> {
2229            self.checks.fetch_add(1, atomic::Ordering::Relaxed);
2230            Ok(PermissionResp::Allow)
2231        }
2232
2233        fn uses_table_targets(&self) -> bool {
2234            false
2235        }
2236
2237        fn check_permission_with_table_targets(
2238            &self,
2239            user_info: UserInfoRef,
2240            req: PermissionReq,
2241            _targets: PermissionTableTargets,
2242        ) -> auth::error::Result<PermissionResp> {
2243            self.check_permission(user_info, req)
2244        }
2245    }
2246
2247    struct BlockingInsertSelectInterceptor {
2248        started_tx: mpsc::UnboundedSender<()>,
2249        finish_rx: std::sync::Mutex<Option<oneshot::Receiver<()>>>,
2250    }
2251
2252    impl BlockingInsertSelectInterceptor {
2253        fn new(started_tx: mpsc::UnboundedSender<()>, finish_rx: oneshot::Receiver<()>) -> Self {
2254            Self {
2255                started_tx,
2256                finish_rx: std::sync::Mutex::new(Some(finish_rx)),
2257            }
2258        }
2259    }
2260
2261    impl SqlQueryInterceptor for BlockingInsertSelectInterceptor {
2262        type Error = Error;
2263
2264        fn pre_execute(
2265            &self,
2266            statement: Option<&Statement>,
2267            _plan: Option<&LogicalPlan>,
2268            _query_ctx: QueryContextRef,
2269        ) -> Result<()> {
2270            let Some(Statement::Insert(insert)) = statement else {
2271                return Ok(());
2272            };
2273            if !insert.has_non_values_query_source() {
2274                return Ok(());
2275            }
2276
2277            let finish_rx = self.finish_rx.lock().unwrap().take().unwrap();
2278            let _ = self.started_tx.send(());
2279            tokio::task::block_in_place(|| {
2280                tokio::runtime::Handle::current()
2281                    .block_on(finish_rx)
2282                    .unwrap();
2283            });
2284            Ok(())
2285        }
2286    }
2287
2288    struct PendingRecordBatchStream {
2289        schema: GtSchemaRef,
2290        polled_tx: Option<oneshot::Sender<()>>,
2291        _finish_tx: oneshot::Sender<()>,
2292        finish_rx: Pin<Box<oneshot::Receiver<()>>>,
2293    }
2294
2295    impl RecordBatchStream for PendingRecordBatchStream {
2296        fn schema(&self) -> GtSchemaRef {
2297            self.schema.clone()
2298        }
2299
2300        fn output_ordering(&self) -> Option<&[OrderOption]> {
2301            None
2302        }
2303
2304        fn metrics(&self) -> Option<common_recordbatch::adapter::RecordBatchMetrics> {
2305            None
2306        }
2307    }
2308
2309    impl Stream for PendingRecordBatchStream {
2310        type Item = common_recordbatch::error::Result<RecordBatch>;
2311
2312        fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
2313            if let Some(polled_tx) = self.polled_tx.take() {
2314                let _ = polled_tx.send(());
2315            }
2316
2317            match self.finish_rx.as_mut().poll(cx) {
2318                Poll::Ready(_) => Poll::Ready(None),
2319                Poll::Pending => Poll::Pending,
2320            }
2321        }
2322    }
2323
2324    impl Unpin for PendingRecordBatchStream {}
2325
2326    #[test]
2327    fn test_record_explain_analyze_timeout_uses_empty_metrics_without_plan() {
2328        let event_recorder = Arc::new(RecordingSlowQueryEventRecorder::default());
2329        let timer = SlowQueryTimer::new(
2330            QueryStatement::Plan("EXPLAIN ANALYZE VERBOSE SELECT 1".to_string()),
2331            "public".to_string(),
2332            Duration::from_secs(3600),
2333            0.0,
2334            SlowQueriesRecordType::SystemTable,
2335            event_recorder.clone(),
2336        );
2337        let timeout_recorder = timer.recorder();
2338
2339        record_explain_analyze_timeout(Some(&timeout_recorder), None);
2340        drop(timer);
2341
2342        let payloads = event_recorder.payloads.lock().unwrap();
2343        assert_eq!(payloads.len(), 1);
2344        assert_eq!(payloads[0]["timed_out"], true);
2345        assert_eq!(payloads[0]["metrics"], serde_json::json!([]));
2346    }
2347
2348    #[tokio::test]
2349    async fn test_attach_timeout_records_explain_analyze_metrics() {
2350        let event_recorder = Arc::new(RecordingSlowQueryEventRecorder::default());
2351        let timer = SlowQueryTimer::new(
2352            QueryStatement::Plan("EXPLAIN ANALYZE VERBOSE SELECT 1".to_string()),
2353            "public".to_string(),
2354            Duration::from_secs(3600),
2355            0.0,
2356            SlowQueriesRecordType::SystemTable,
2357            event_recorder.clone(),
2358        );
2359        let timeout_recorder = timer.recorder();
2360        let plan: Arc<dyn ExecutionPlan> = Arc::new(EmptyExec::new(Arc::new(Schema::empty())));
2361        let (finish_tx, finish_rx) = oneshot::channel();
2362        let stream = PendingRecordBatchStream {
2363            schema: Arc::new(GtSchema::new(vec![])),
2364            polled_tx: None,
2365            _finish_tx: finish_tx,
2366            finish_rx: Box::pin(finish_rx),
2367        };
2368        let output = Output::new(
2369            OutputData::Stream(Box::pin(stream)),
2370            OutputMeta::new_with_plan(plan),
2371        );
2372        let output =
2373            attach_timeout(output, Duration::from_millis(10), Some(timeout_recorder)).unwrap();
2374        let OutputData::Stream(mut stream) = output.data else {
2375            unreachable!();
2376        };
2377
2378        let err = stream.next().await.unwrap().unwrap_err();
2379        assert_eq!(err.to_string(), "Stream timeout");
2380        drop(stream);
2381        drop(timer);
2382
2383        let payloads = event_recorder.payloads.lock().unwrap();
2384        assert_eq!(payloads.len(), 1);
2385        assert_eq!(payloads[0]["timed_out"], true);
2386        assert!(
2387            payloads[0]["metrics"]
2388                .as_array()
2389                .is_some_and(|metrics| !metrics.is_empty())
2390        );
2391    }
2392
2393    struct PendingDataSource {
2394        schema: GtSchemaRef,
2395        polled_tx: std::sync::Mutex<Option<oneshot::Sender<()>>>,
2396    }
2397
2398    impl DataSource for PendingDataSource {
2399        fn get_stream(
2400            &self,
2401            _request: ScanRequest,
2402        ) -> std::result::Result<SendableRecordBatchStream, BoxedError> {
2403            let (finish_tx, finish_rx) = oneshot::channel();
2404            let mut polled_tx = self.polled_tx.lock().map_err(|_| {
2405                BoxedError::new(PlainError::new(
2406                    "pending data source lock poisoned".to_string(),
2407                    StatusCode::Unexpected,
2408                ))
2409            })?;
2410            Ok(Box::pin(PendingRecordBatchStream {
2411                schema: self.schema.clone(),
2412                polled_tx: polled_tx.take(),
2413                _finish_tx: finish_tx,
2414                finish_rx: Box::pin(finish_rx),
2415            }))
2416        }
2417    }
2418
2419    struct NoopProcedureExecutor;
2420
2421    #[async_trait::async_trait]
2422    impl ProcedureExecutor for NoopProcedureExecutor {
2423        async fn submit_ddl_task(
2424            &self,
2425            _ctx: ExecutorContext,
2426            _request: SubmitDdlTaskRequest,
2427        ) -> common_meta::error::Result<SubmitDdlTaskResponse> {
2428            common_meta::error::UnsupportedSnafu {
2429                operation: "submit_ddl_task",
2430            }
2431            .fail()
2432        }
2433
2434        async fn migrate_region(
2435            &self,
2436            _ctx: &ExecutorContext,
2437            _request: MigrateRegionRequest,
2438        ) -> common_meta::error::Result<MigrateRegionResponse> {
2439            common_meta::error::UnsupportedSnafu {
2440                operation: "migrate_region",
2441            }
2442            .fail()
2443        }
2444
2445        async fn reconcile(
2446            &self,
2447            _ctx: &ExecutorContext,
2448            _request: ReconcileRequest,
2449        ) -> common_meta::error::Result<ReconcileResponse> {
2450            common_meta::error::UnsupportedSnafu {
2451                operation: "reconcile",
2452            }
2453            .fail()
2454        }
2455
2456        async fn query_procedure_state(
2457            &self,
2458            _ctx: &ExecutorContext,
2459            _pid: &str,
2460        ) -> common_meta::error::Result<ProcedureStateResponse> {
2461            common_meta::error::UnsupportedSnafu {
2462                operation: "query_procedure_state",
2463            }
2464            .fail()
2465        }
2466
2467        async fn list_procedures(
2468            &self,
2469            _ctx: &ExecutorContext,
2470        ) -> common_meta::error::Result<ProcedureDetailResponse> {
2471            common_meta::error::UnsupportedSnafu {
2472                operation: "list_procedures",
2473            }
2474            .fail()
2475        }
2476    }
2477
2478    /// A test [`ProcedureExecutor`] that completes create/drop DDL tasks against the
2479    /// in-memory catalog, mimicking what the meta DDL procedures do in production.
2480    /// This allows happy-path DDL requests (create/drop table/view) to be exercised
2481    /// end to end through the gRPC ingress.
2482    struct MockProcedureExecutor {
2483        catalog_manager: Arc<catalog::memory::MemoryCatalogManager>,
2484        next_table_id: std::sync::atomic::AtomicU32,
2485        submitted: std::sync::Mutex<Vec<DdlTask>>,
2486    }
2487
2488    impl MockProcedureExecutor {
2489        fn new(catalog_manager: Arc<catalog::memory::MemoryCatalogManager>) -> Self {
2490            Self {
2491                catalog_manager,
2492                next_table_id: std::sync::atomic::AtomicU32::new(1026),
2493                submitted: std::sync::Mutex::new(Vec::new()),
2494            }
2495        }
2496    }
2497
2498    #[async_trait::async_trait]
2499    impl ProcedureExecutor for MockProcedureExecutor {
2500        async fn submit_ddl_task(
2501            &self,
2502            _ctx: ExecutorContext,
2503            request: SubmitDdlTaskRequest,
2504        ) -> common_meta::error::Result<SubmitDdlTaskResponse> {
2505            self.submitted.lock().unwrap().push(request.task.clone());
2506            match request.task {
2507                DdlTask::CreateTable(task) => {
2508                    let table_id = self
2509                        .next_table_id
2510                        .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2511                    let mut table_info = task.table_info;
2512                    table_info.ident.table_id = table_id;
2513                    self.catalog_manager
2514                        .register_table_sync(catalog::RegisterTableRequest {
2515                            catalog: table_info.catalog_name.clone(),
2516                            schema: table_info.schema_name.clone(),
2517                            table_name: table_info.name.clone(),
2518                            table_id,
2519                            table: table::dist_table::DistTable::table(Arc::new(table_info)),
2520                        })
2521                        .map_err(BoxedError::new)
2522                        .context(common_meta::error::ExternalSnafu)?;
2523                    Ok(SubmitDdlTaskResponse {
2524                        key: Vec::new(),
2525                        table_ids: vec![table_id],
2526                    })
2527                }
2528                DdlTask::CreateView(task) => {
2529                    let view_id = self
2530                        .next_table_id
2531                        .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2532                    let mut view_info = task.view_info;
2533                    view_info.ident.table_id = view_id;
2534                    self.catalog_manager
2535                        .register_table_sync(catalog::RegisterTableRequest {
2536                            catalog: task.create_view.catalog_name.clone(),
2537                            schema: task.create_view.schema_name.clone(),
2538                            table_name: task.create_view.view_name.clone(),
2539                            table_id: view_id,
2540                            table: table::dist_table::DistTable::table(Arc::new(view_info)),
2541                        })
2542                        .map_err(BoxedError::new)
2543                        .context(common_meta::error::ExternalSnafu)?;
2544                    Ok(SubmitDdlTaskResponse {
2545                        key: Vec::new(),
2546                        table_ids: vec![view_id],
2547                    })
2548                }
2549                DdlTask::DropView(task) => {
2550                    self.catalog_manager
2551                        .deregister_table_sync(catalog::DeregisterTableRequest {
2552                            catalog: task.catalog.clone(),
2553                            schema: task.schema.clone(),
2554                            table_name: task.view.clone(),
2555                        })
2556                        .map_err(BoxedError::new)
2557                        .context(common_meta::error::ExternalSnafu)?;
2558                    Ok(SubmitDdlTaskResponse::default())
2559                }
2560                other => common_meta::error::UnsupportedSnafu {
2561                    operation: format!("mock submit_ddl_task: {other:?}"),
2562                }
2563                .fail(),
2564            }
2565        }
2566
2567        async fn migrate_region(
2568            &self,
2569            _ctx: &ExecutorContext,
2570            _request: MigrateRegionRequest,
2571        ) -> common_meta::error::Result<MigrateRegionResponse> {
2572            common_meta::error::UnsupportedSnafu {
2573                operation: "migrate_region",
2574            }
2575            .fail()
2576        }
2577
2578        async fn reconcile(
2579            &self,
2580            _ctx: &ExecutorContext,
2581            _request: ReconcileRequest,
2582        ) -> common_meta::error::Result<ReconcileResponse> {
2583            common_meta::error::UnsupportedSnafu {
2584                operation: "reconcile",
2585            }
2586            .fail()
2587        }
2588
2589        async fn query_procedure_state(
2590            &self,
2591            _ctx: &ExecutorContext,
2592            _pid: &str,
2593        ) -> common_meta::error::Result<ProcedureStateResponse> {
2594            common_meta::error::UnsupportedSnafu {
2595                operation: "query_procedure_state",
2596            }
2597            .fail()
2598        }
2599
2600        async fn list_procedures(
2601            &self,
2602            _ctx: &ExecutorContext,
2603        ) -> common_meta::error::Result<ProcedureDetailResponse> {
2604            common_meta::error::UnsupportedSnafu {
2605                operation: "list_procedures",
2606            }
2607            .fail()
2608        }
2609    }
2610
2611    fn test_cache_registry(
2612        kv_backend: common_meta::kv_backend::KvBackendRef,
2613    ) -> TestResult<common_meta::cache::LayeredCacheRegistryRef> {
2614        Ok(Arc::new(
2615            cache::with_default_composite_cache_registry(
2616                LayeredCacheRegistryBuilder::default()
2617                    .add_cache_registry(cache::build_fundamental_cache_registry(kv_backend)),
2618            )
2619            .context(BuildCacheRegistrySnafu)?
2620            .build(),
2621        ))
2622    }
2623
2624    fn test_table_info(table_id: u32, table_name: &str) -> TestResult<TableInfo> {
2625        let schema = Arc::new(GtSchema::new(vec![
2626            ColumnSchema::new("id", ConcreteDataType::int32_datatype(), false),
2627            ColumnSchema::new(
2628                "ts",
2629                ConcreteDataType::timestamp_millisecond_datatype(),
2630                false,
2631            )
2632            .with_time_index(true),
2633        ]));
2634        let table_meta = TableMetaBuilder::empty()
2635            .schema(schema)
2636            .primary_key_indices(vec![0])
2637            .value_indices(vec![1])
2638            .next_column_id(1024)
2639            .build()
2640            .with_context(|_| BuildTableMetaSnafu {
2641                table_name: table_name.to_string(),
2642            })?;
2643
2644        TableInfoBuilder::new(table_name, table_meta)
2645            .table_id(table_id)
2646            .build()
2647            .with_context(|_| BuildTableInfoSnafu {
2648                table_name: table_name.to_string(),
2649            })
2650    }
2651
2652    fn test_table(table_id: u32, table_name: &str) -> TestResult<table::TableRef> {
2653        let table_info = test_table_info(table_id, table_name)?;
2654        Ok(EmptyTable::from_table_info(&table_info))
2655    }
2656
2657    fn test_physical_table(table_id: u32, table_name: &str) -> TestResult<table::TableRef> {
2658        let mut table_info = test_table_info(table_id, table_name)?;
2659        table_info
2660            .meta
2661            .options
2662            .extra_options
2663            .insert(PHYSICAL_TABLE_METADATA_KEY.to_string(), String::new());
2664        Ok(EmptyTable::from_table_info(&table_info))
2665    }
2666
2667    fn test_logical_table(table_id: u32, table_name: &str) -> TestResult<table::TableRef> {
2668        let mut table_info = test_table_info(table_id, table_name)?;
2669        table_info.meta.engine = METRIC_ENGINE_NAME.to_string();
2670        table_info.meta.options.extra_options.insert(
2671            LOGICAL_TABLE_METADATA_KEY.to_string(),
2672            "physical_metric".to_string(),
2673        );
2674        Ok(EmptyTable::from_table_info(&table_info))
2675    }
2676
2677    fn test_metric_names_table() -> TableRef {
2678        let schema = Arc::new(GtSchema::new(vec![
2679            ColumnSchema::new("table_catalog", ConcreteDataType::string_datatype(), false),
2680            ColumnSchema::new("table_schema", ConcreteDataType::string_datatype(), false),
2681            ColumnSchema::new("table_name", ConcreteDataType::string_datatype(), false),
2682            ColumnSchema::new("engine", ConcreteDataType::string_datatype(), false),
2683            ColumnSchema::new("create_options", ConcreteDataType::string_datatype(), false),
2684        ]));
2685        let columns: Vec<VectorRef> = vec![
2686            Arc::new(StringVector::from(vec!["greptime", "greptime"])),
2687            Arc::new(StringVector::from(vec!["public", "public"])),
2688            Arc::new(StringVector::from(vec!["denied", "target"])),
2689            Arc::new(StringVector::from(vec!["metric", "metric"])),
2690            Arc::new(StringVector::from(vec![
2691                "on_physical_table=physical_metric",
2692                "on_physical_table=physical_metric",
2693            ])),
2694        ];
2695        let record_batch = RecordBatch::new(schema, columns).unwrap();
2696        MemTable::new_with_catalog(
2697            "tables",
2698            record_batch,
2699            2048,
2700            "greptime".to_string(),
2701            "information_schema".to_string(),
2702        )
2703    }
2704
2705    fn test_pipeline_table() -> TableRef {
2706        let schema = Arc::new(GtSchema::new(vec![
2707            ColumnSchema::new("name", ConcreteDataType::string_datatype(), false),
2708            ColumnSchema::new("schema", ConcreteDataType::string_datatype(), false),
2709            ColumnSchema::new("content_type", ConcreteDataType::string_datatype(), false),
2710            ColumnSchema::new("pipeline", ConcreteDataType::string_datatype(), false),
2711            ColumnSchema::new(
2712                "created_at",
2713                ConcreteDataType::timestamp_nanosecond_datatype(),
2714                false,
2715            )
2716            .with_time_index(true),
2717        ]));
2718        let columns: Vec<VectorRef> = vec![
2719            Arc::new(StringVector::from(vec!["pipeline"])),
2720            Arc::new(StringVector::from(vec!["public"])),
2721            Arc::new(StringVector::from(vec!["application/yaml"])),
2722            Arc::new(StringVector::from(vec![
2723                "transform:\n- field: ts\n  type: timestamp, ns\n  index: time\n",
2724            ])),
2725            Arc::new(TimestampNanosecondVector::from_values([1])),
2726        ];
2727        let record_batch = RecordBatch::new(schema, columns).unwrap();
2728        MemTable::new_with_catalog(
2729            "pipelines",
2730            record_batch,
2731            2049,
2732            "greptime".to_string(),
2733            DEFAULT_PRIVATE_SCHEMA_NAME.to_string(),
2734        )
2735    }
2736
2737    fn pending_table(
2738        table_id: u32,
2739        table_name: &str,
2740        polled_tx: oneshot::Sender<()>,
2741    ) -> TestResult<table::TableRef> {
2742        let table_info = test_table_info(table_id, table_name)?;
2743        let data_source = Arc::new(PendingDataSource {
2744            schema: table_info.meta.schema.clone(),
2745            polled_tx: std::sync::Mutex::new(Some(polled_tx)),
2746        });
2747
2748        Ok(Arc::new(Table::new(
2749            Arc::new(table_info),
2750            FilterPushDownType::Unsupported,
2751            data_source,
2752        )))
2753    }
2754
2755    async fn test_instance_with_tables(
2756        source_table: TableRef,
2757        target_table: TableRef,
2758    ) -> TestResult<Instance> {
2759        test_instance_with_plugins(source_table, target_table, Plugins::new()).await
2760    }
2761
2762    async fn test_instance_with_insert_select_interceptor(
2763        interceptor: SqlQueryInterceptorRef<Error>,
2764    ) -> TestResult<Instance> {
2765        let plugins = Plugins::new();
2766        plugins.insert::<SqlQueryInterceptorRef<Error>>(interceptor);
2767
2768        test_instance_with_plugins(
2769            test_table(1024, "source")?,
2770            test_table(1025, "target")?,
2771            plugins,
2772        )
2773        .await
2774    }
2775
2776    async fn test_instance_with_plugins(
2777        source_table: TableRef,
2778        target_table: TableRef,
2779        plugins: Plugins,
2780    ) -> TestResult<Instance> {
2781        test_instance_with_plugins_and_metric_names(source_table, target_table, plugins, None).await
2782    }
2783
2784    async fn test_instance_with_plugins_and_metric_names(
2785        source_table: TableRef,
2786        target_table: TableRef,
2787        plugins: Plugins,
2788        metric_names_table: Option<TableRef>,
2789    ) -> TestResult<Instance> {
2790        let catalog_manager = catalog::memory::MemoryCatalogManager::new_with_table(source_table);
2791        test_instance_with_catalog_manager(
2792            catalog_manager,
2793            target_table,
2794            plugins,
2795            metric_names_table,
2796            Arc::new(NoopProcedureExecutor),
2797        )
2798        .await
2799    }
2800
2801    /// Builds a test frontend `Instance` over the given (already source-registered)
2802    /// catalog manager, completing DDL tasks through `procedure_executor`.
2803    async fn test_instance_with_catalog_manager(
2804        catalog_manager: Arc<catalog::memory::MemoryCatalogManager>,
2805        target_table: TableRef,
2806        plugins: Plugins,
2807        metric_names_table: Option<TableRef>,
2808        procedure_executor: ProcedureExecutorRef,
2809    ) -> TestResult<Instance> {
2810        let kv_backend = Arc::new(MemoryKvBackend::new());
2811        let process_manager = Arc::new(ProcessManager::new("test-frontend".to_string(), None));
2812        let target_table_name = "target";
2813        catalog_manager
2814            .register_table_sync(catalog::RegisterTableRequest {
2815                catalog: "greptime".to_string(),
2816                schema: "public".to_string(),
2817                table_name: target_table_name.to_string(),
2818                table_id: 1025,
2819                table: target_table,
2820            })
2821            .with_context(|_| RegisterTableSnafu {
2822                table_name: target_table_name.to_string(),
2823            })?;
2824        if let Some(table) = metric_names_table {
2825            catalog_manager
2826                .deregister_table_sync(catalog::DeregisterTableRequest {
2827                    catalog: "greptime".to_string(),
2828                    schema: "information_schema".to_string(),
2829                    table_name: "tables".to_string(),
2830                })
2831                .unwrap();
2832            catalog_manager
2833                .register_table_sync(catalog::RegisterTableRequest {
2834                    catalog: "greptime".to_string(),
2835                    schema: "information_schema".to_string(),
2836                    table_name: "tables".to_string(),
2837                    table_id: 2048,
2838                    table,
2839                })
2840                .unwrap();
2841        }
2842        catalog_manager.register_process_list_table(process_manager.clone());
2843
2844        let cache_registry = test_cache_registry(kv_backend.clone())?;
2845
2846        FrontendBuilder::new(
2847            FrontendOptions::default(),
2848            kv_backend,
2849            cache_registry,
2850            catalog_manager,
2851            Arc::new(client::client_manager::NodeClients::default()),
2852            procedure_executor,
2853            process_manager,
2854        )
2855        .with_plugin(plugins)
2856        .try_build()
2857        .await
2858        .context(BuildFrontendSnafu)
2859    }
2860
2861    async fn execute_one_sql(
2862        instance: &Instance,
2863        sql: &str,
2864        query_ctx: QueryContextRef,
2865    ) -> TestResult<Output> {
2866        let mut results = instance.do_query_inner(sql, query_ctx).await;
2867        ensure!(
2868            results.len() == 1,
2869            UnexpectedOutputCountSnafu {
2870                sql: sql.to_string(),
2871                actual: results.len(),
2872            }
2873        );
2874        results.remove(0).with_context(|_| ExecuteSqlSnafu {
2875            sql: sql.to_string(),
2876        })
2877    }
2878
2879    fn assert_permission_denied<T>(result: servers::error::Result<T>) {
2880        let err = match result {
2881            Ok(_) => panic!("request should be rejected"),
2882            Err(err) => err,
2883        };
2884        assert_eq!(StatusCode::PermissionDenied, err.status_code());
2885    }
2886
2887    fn assert_action_checked(
2888        checker: &RejectEndpointPermissionChecker,
2889        action: PermissionAction,
2890        targets: Option<PermissionTableTargets>,
2891    ) {
2892        assert_eq!(CheckedAction { action, targets }, checker.take_check());
2893    }
2894
2895    #[tokio::test]
2896    async fn database_export_authorizes_all_tables_before_preparation() -> TestResult<()> {
2897        struct ExportAcl(std::sync::Mutex<Vec<PermissionTableTargets>>);
2898        impl PermissionChecker for ExportAcl {
2899            fn check_permission(
2900                &self,
2901                _: UserInfoRef,
2902                req: PermissionReq,
2903            ) -> auth::error::Result<PermissionResp> {
2904                assert!(matches!(
2905                    req,
2906                    PermissionReq::SqlStatement(Statement::Copy(
2907                        sql::statements::copy::Copy::CopyDatabase(
2908                            sql::statements::copy::CopyDatabase::To(_)
2909                        )
2910                    ))
2911                ));
2912                Ok(PermissionResp::Allow)
2913            }
2914            fn check_permission_with_table_targets(
2915                &self,
2916                _: UserInfoRef,
2917                req: PermissionReq,
2918                targets: PermissionTableTargets,
2919            ) -> auth::error::Result<PermissionResp> {
2920                self.0.lock().unwrap().push(targets.clone());
2921                let PermissionTableTargets::Resolved(tables) = targets else {
2922                    panic!("unresolved export")
2923                };
2924                if req.is_readonly() {
2925                    // The first table is readable; the later table has only write access.
2926                    Ok(if tables.iter().any(|t| t.table == "target") {
2927                        PermissionResp::Reject
2928                    } else {
2929                        PermissionResp::Allow
2930                    })
2931                } else {
2932                    self.check_permission(QueryContext::arc().current_user(), req)
2933                }
2934            }
2935        }
2936        let checker = Arc::new(ExportAcl(Default::default()));
2937        let plugins = Plugins::new();
2938        plugins.insert::<PermissionCheckerRef>(checker.clone());
2939        let instance = test_instance_with_plugins(
2940            test_logical_table(1024, "source")?,
2941            test_table(1025, "target")?,
2942            plugins,
2943        )
2944        .await?;
2945        let req = table::requests::CopyDatabaseRequest {
2946            catalog_name: "greptime".into(),
2947            schema_name: "public".into(),
2948            location: "invalid-destination".into(),
2949            with: Default::default(),
2950            connection: Default::default(),
2951            time_range: None,
2952        };
2953        let result = instance
2954            .export_database_for_test(
2955                req.clone(),
2956                None,
2957                &tokio_util::sync::CancellationToken::new(),
2958                QueryContext::arc(),
2959            )
2960            .await;
2961        assert!(matches!(result, Err(Error::Permission { .. })));
2962        let expected = PermissionTableTargets::resolved(vec![
2963            PermissionTableTarget::new("greptime", "public", "source"),
2964            PermissionTableTarget::new("greptime", "public", "target"),
2965        ]);
2966        assert_eq!(*checker.0.lock().unwrap(), vec![expected.clone(), expected]);
2967        // An empty selection must still check the operation privilege.
2968        instance
2969            .plugins
2970            .map_mut::<PermissionCheckerRef, _, _>(|checker| {
2971                *checker.unwrap() = Arc::new(WriteOnlyPermissionChecker)
2972            });
2973        let result = instance
2974            .export_database_for_test(
2975                req,
2976                Some(&[]),
2977                &tokio_util::sync::CancellationToken::new(),
2978                QueryContext::arc(),
2979            )
2980            .await;
2981        assert!(matches!(result, Err(Error::Permission { .. })));
2982        Ok(())
2983    }
2984
2985    #[tokio::test]
2986    async fn test_prom_remote_read_with_custom_timestamp_and_value_columns() -> TestResult<()> {
2987        let schema = Arc::new(GtSchema::new(vec![
2988            ColumnSchema::new(
2989                "custom_ts",
2990                ConcreteDataType::timestamp_millisecond_datatype(),
2991                false,
2992            )
2993            .with_time_index(true),
2994            ColumnSchema::new("custom_value", ConcreteDataType::float64_datatype(), false),
2995        ]));
2996        let recordbatch = RecordBatch::new(
2997            schema,
2998            vec![
2999                Arc::new(TimestampMillisecondVector::from_vec(vec![1000, 2000, 3000])) as VectorRef,
3000                Arc::new(Float64Vector::from_vec(vec![1.0, 2.0, 3.0])) as VectorRef,
3001            ],
3002        )
3003        .unwrap();
3004        let instance = test_instance_with_tables(
3005            MemTable::table("custom_metric", recordbatch),
3006            test_table(1025, "target")?,
3007        )
3008        .await?;
3009
3010        let response = PromStoreProtocolHandler::read(
3011            &instance,
3012            ReadRequest {
3013                queries: vec![RemoteQuery {
3014                    start_timestamp_ms: 1500,
3015                    end_timestamp_ms: 2500,
3016                    matchers: vec![LabelMatcher {
3017                        r#type: PromMatcherType::Eq as i32,
3018                        name: servers::prom_store::METRIC_NAME_LABEL.to_string(),
3019                        value: "custom_metric".to_string(),
3020                    }],
3021                    ..Default::default()
3022                }],
3023                ..Default::default()
3024            },
3025            test_query_ctx(1),
3026        )
3027        .await
3028        .unwrap();
3029        let body = servers::prom_store::snappy_decompress(&response.body).unwrap();
3030        let response = ReadResponse::decode(body.as_slice()).unwrap();
3031
3032        assert_eq!(1, response.results.len());
3033        assert_eq!(1, response.results[0].timeseries.len());
3034        let timeseries = &response.results[0].timeseries[0];
3035        assert_eq!(
3036            vec![Label {
3037                name: servers::prom_store::METRIC_NAME_LABEL.to_string(),
3038                value: "custom_metric".to_string(),
3039            }],
3040            timeseries.labels
3041        );
3042        assert_eq!(
3043            vec![Sample {
3044                value: 2.0,
3045                timestamp: 2000,
3046            }],
3047            timeseries.samples
3048        );
3049
3050        Ok(())
3051    }
3052
3053    #[tokio::test]
3054    async fn test_prom_remote_read_prefers_default_value_column() -> TestResult<()> {
3055        let schema = Arc::new(GtSchema::new(vec![
3056            ColumnSchema::new(
3057                "custom_ts",
3058                ConcreteDataType::timestamp_millisecond_datatype(),
3059                false,
3060            )
3061            .with_time_index(true),
3062            ColumnSchema::new("extra_field", ConcreteDataType::float64_datatype(), false),
3063            ColumnSchema::new(
3064                greptime_value(),
3065                ConcreteDataType::float64_datatype(),
3066                false,
3067            ),
3068        ]));
3069        let recordbatch = RecordBatch::new(
3070            schema,
3071            vec![
3072                Arc::new(TimestampMillisecondVector::from_vec(vec![1000, 2000, 3000])) as VectorRef,
3073                Arc::new(Float64Vector::from_vec(vec![99.0, 99.0, 99.0])) as VectorRef,
3074                Arc::new(Float64Vector::from_vec(vec![1.0, 2.0, 3.0])) as VectorRef,
3075            ],
3076        )
3077        .unwrap();
3078        let instance = test_instance_with_tables(
3079            MemTable::table("multi_field_metric", recordbatch),
3080            test_table(1025, "target")?,
3081        )
3082        .await?;
3083
3084        let response = PromStoreProtocolHandler::read(
3085            &instance,
3086            ReadRequest {
3087                queries: vec![RemoteQuery {
3088                    start_timestamp_ms: 1500,
3089                    end_timestamp_ms: 2500,
3090                    matchers: vec![LabelMatcher {
3091                        r#type: PromMatcherType::Eq as i32,
3092                        name: servers::prom_store::METRIC_NAME_LABEL.to_string(),
3093                        value: "multi_field_metric".to_string(),
3094                    }],
3095                    ..Default::default()
3096                }],
3097                ..Default::default()
3098            },
3099            test_query_ctx(1),
3100        )
3101        .await
3102        .unwrap();
3103        let body = servers::prom_store::snappy_decompress(&response.body).unwrap();
3104        let response = ReadResponse::decode(body.as_slice()).unwrap();
3105
3106        assert_eq!(1, response.results.len());
3107        assert_eq!(1, response.results[0].timeseries.len());
3108        let timeseries = &response.results[0].timeseries[0];
3109        assert_eq!(
3110            vec![
3111                Label {
3112                    name: servers::prom_store::METRIC_NAME_LABEL.to_string(),
3113                    value: "multi_field_metric".to_string(),
3114                },
3115                Label {
3116                    name: "extra_field".to_string(),
3117                    value: "99".to_string(),
3118                },
3119            ],
3120            timeseries.labels
3121        );
3122        assert_eq!(
3123            vec![Sample {
3124                value: 2.0,
3125                timestamp: 2000,
3126            }],
3127            timeseries.samples
3128        );
3129
3130        Ok(())
3131    }
3132
3133    #[tokio::test]
3134    async fn test_prom_remote_read_rejects_ambiguous_value_columns() -> TestResult<()> {
3135        let schema = Arc::new(GtSchema::new(vec![
3136            ColumnSchema::new(
3137                "custom_ts",
3138                ConcreteDataType::timestamp_millisecond_datatype(),
3139                false,
3140            )
3141            .with_time_index(true),
3142            ColumnSchema::new("field_a", ConcreteDataType::float64_datatype(), false),
3143            ColumnSchema::new("field_b", ConcreteDataType::float64_datatype(), false),
3144        ]));
3145        let recordbatch = RecordBatch::new(
3146            schema,
3147            vec![
3148                Arc::new(TimestampMillisecondVector::from_vec(vec![1000])) as VectorRef,
3149                Arc::new(Float64Vector::from_vec(vec![1.0])) as VectorRef,
3150                Arc::new(Float64Vector::from_vec(vec![2.0])) as VectorRef,
3151            ],
3152        )
3153        .unwrap();
3154        let instance = test_instance_with_tables(
3155            MemTable::table("ambiguous_metric", recordbatch),
3156            test_table(1025, "target")?,
3157        )
3158        .await?;
3159
3160        let err = PromStoreProtocolHandler::read(
3161            &instance,
3162            ReadRequest {
3163                queries: vec![RemoteQuery {
3164                    matchers: vec![LabelMatcher {
3165                        r#type: PromMatcherType::Eq as i32,
3166                        name: servers::prom_store::METRIC_NAME_LABEL.to_string(),
3167                        value: "ambiguous_metric".to_string(),
3168                    }],
3169                    ..Default::default()
3170                }],
3171                ..Default::default()
3172            },
3173            test_query_ctx(1),
3174        )
3175        .await
3176        .err()
3177        .expect("ambiguous value columns should fail remote read");
3178
3179        assert_eq!(StatusCode::InvalidArguments, err.status_code());
3180        assert!(format!("{err:?}").contains("Ambiguous value column"));
3181
3182        Ok(())
3183    }
3184
3185    #[tokio::test]
3186    async fn test_event_recorder_is_exposed() -> TestResult<()> {
3187        let instance =
3188            test_instance_with_tables(test_table(1024, "source")?, test_table(1025, "target")?)
3189                .await?;
3190
3191        let _event_recorder = instance.event_recorder();
3192
3193        Ok(())
3194    }
3195
3196    #[tokio::test]
3197    async fn test_restricted_endpoint_handlers_check_permissions() -> TestResult<()> {
3198        let checker = Arc::new(RejectEndpointPermissionChecker::default());
3199        let plugins = Plugins::new();
3200        plugins.insert::<PermissionCheckerRef>(checker.clone());
3201        let instance = test_instance_with_plugins(
3202            test_table(1024, "denied")?,
3203            test_table(1025, "target")?,
3204            plugins,
3205        )
3206        .await?;
3207        let mut ctx = test_query_ctx(1);
3208        Arc::get_mut(&mut ctx).unwrap().set_extension(
3209            servers::http::jaeger::JAEGER_QUERY_TABLE_NAME_KEY,
3210            "denied".to_string(),
3211        );
3212        let jaeger_targets = Some(PermissionTableTargets::resolved(vec![
3213            PermissionTableTarget::new("greptime", "public", "denied"),
3214        ]));
3215
3216        assert_permission_denied(JaegerQueryHandler::get_services(&instance, ctx.clone()).await);
3217        assert_action_checked(&checker, JAEGER_QUERY, jaeger_targets.clone());
3218        assert_permission_denied(
3219            JaegerQueryHandler::get_operations(&instance, ctx.clone(), "service", None).await,
3220        );
3221        assert_action_checked(&checker, JAEGER_QUERY, jaeger_targets.clone());
3222        assert_permission_denied(
3223            JaegerQueryHandler::get_trace(&instance, ctx.clone(), "trace", None, None, None).await,
3224        );
3225        assert_action_checked(&checker, JAEGER_QUERY, jaeger_targets.clone());
3226        assert_permission_denied(
3227            JaegerQueryHandler::find_traces(
3228                &instance,
3229                ctx.clone(),
3230                servers::http::jaeger::QueryTraceParams {
3231                    service_name: "service".to_string(),
3232                    ..Default::default()
3233                },
3234            )
3235            .await,
3236        );
3237        assert_action_checked(&checker, JAEGER_QUERY, jaeger_targets);
3238
3239        assert_permission_denied(
3240            PipelineHandler::get_pipeline_str(&instance, "pipeline", None, ctx.clone()).await,
3241        );
3242        assert_action_checked(&checker, PIPELINE_QUERY, None);
3243        assert_permission_denied(
3244            PipelineHandler::insert_pipeline(
3245                &instance,
3246                "pipeline",
3247                "application/yaml",
3248                "",
3249                ctx.clone(),
3250            )
3251            .await,
3252        );
3253        assert_action_checked(&checker, PIPELINE_INSERT, None);
3254        assert_permission_denied(
3255            PipelineHandler::delete_pipeline(&instance, "pipeline", None, ctx.clone()).await,
3256        );
3257        assert_action_checked(&checker, PIPELINE_DELETE, None);
3258        let app = axum::Router::new()
3259            .route(
3260                "/pipelines/_dryrun",
3261                axum::routing::post(servers::http::event::pipeline_dryrun),
3262            )
3263            .with_state(servers::http::event::LogState {
3264                log_handler: Arc::new(instance.clone()),
3265                log_validator: None,
3266                ingest_interceptor: None,
3267            })
3268            .layer(axum::Extension((*ctx).clone()));
3269        let response = app
3270            .oneshot(
3271                axum::http::Request::post("/pipelines/_dryrun")
3272                    .header("content-type", "application/json")
3273                    .body(axum::body::Body::from("{}"))
3274                    .unwrap(),
3275            )
3276            .await
3277            .unwrap();
3278        assert_eq!(axum::http::StatusCode::FORBIDDEN, response.status());
3279        assert_action_checked(&checker, PIPELINE_QUERY, None);
3280
3281        assert_permission_denied(
3282            DashboardHandler::save(&instance, "dashboard", "{}", ctx.clone()).await,
3283        );
3284        assert_action_checked(&checker, DASHBOARD_SAVE, None);
3285        assert_permission_denied(DashboardHandler::list(&instance, ctx.clone()).await);
3286        assert_action_checked(&checker, DASHBOARD_QUERY, None);
3287        assert_permission_denied(
3288            DashboardHandler::delete(&instance, "dashboard", ctx.clone()).await,
3289        );
3290        assert_action_checked(&checker, DASHBOARD_DELETE, None);
3291
3292        Ok(())
3293    }
3294
3295    #[tokio::test]
3296    async fn test_write_only_ingestion_loads_named_pipeline() -> TestResult<()> {
3297        let plugins = Plugins::new();
3298        plugins.insert::<PermissionCheckerRef>(Arc::new(WriteOnlyPermissionChecker));
3299        let instance = test_instance_with_plugins(
3300            test_table(1024, "source")?,
3301            test_table(1025, "target")?,
3302            plugins,
3303        )
3304        .await?;
3305        instance
3306            .catalog_manager()
3307            .as_any()
3308            .downcast_ref::<catalog::memory::MemoryCatalogManager>()
3309            .unwrap()
3310            .register_table_sync(catalog::RegisterTableRequest {
3311                catalog: "greptime".to_string(),
3312                schema: DEFAULT_PRIVATE_SCHEMA_NAME.to_string(),
3313                table_name: "pipelines".to_string(),
3314                table_id: 2049,
3315                table: test_pipeline_table(),
3316            })
3317            .with_context(|_| RegisterTableSnafu {
3318                table_name: "pipelines".to_string(),
3319            })?;
3320        let ctx = test_query_ctx(1);
3321        let handler: PipelineHandlerRef = Arc::new(instance.clone());
3322
3323        handler
3324            .get_pipeline("pipeline", None, ctx.clone())
3325            .await
3326            .unwrap();
3327        assert_permission_denied(
3328            PipelineHandler::get_pipeline_str(&instance, "pipeline", None, ctx.clone()).await,
3329        );
3330
3331        let app = axum::Router::new()
3332            .route(
3333                "/pipelines/_dryrun",
3334                axum::routing::post(servers::http::event::pipeline_dryrun),
3335            )
3336            .with_state(servers::http::event::LogState {
3337                log_handler: handler,
3338                log_validator: None,
3339                ingest_interceptor: None,
3340            })
3341            .layer(axum::Extension((*ctx).clone()));
3342        let response = app
3343            .oneshot(
3344                axum::http::Request::post("/pipelines/_dryrun")
3345                    .header("content-type", "application/json")
3346                    .body(axum::body::Body::from("{}"))
3347                    .unwrap(),
3348            )
3349            .await
3350            .unwrap();
3351        assert_eq!(axum::http::StatusCode::FORBIDDEN, response.status());
3352
3353        Ok(())
3354    }
3355
3356    #[tokio::test]
3357    async fn test_write_only_grpc_sql_is_checked_after_parsing() -> TestResult<()> {
3358        let plugins = Plugins::new();
3359        plugins.insert::<PermissionCheckerRef>(Arc::new(WriteOnlyPermissionChecker));
3360        let instance = test_instance_with_plugins(
3361            test_table(1024, "source")?,
3362            test_table(1025, "target")?,
3363            plugins,
3364        )
3365        .await?;
3366
3367        let insert = Request::Query(api::v1::QueryRequest {
3368            query: Some(Query::Sql(
3369                "INSERT INTO target SELECT * FROM source".to_string(),
3370            )),
3371        });
3372        servers::query_handler::grpc::GrpcQueryHandler::do_query(
3373            &instance,
3374            insert,
3375            QueryContext::arc(),
3376        )
3377        .await
3378        .unwrap();
3379
3380        let select = Request::Query(api::v1::QueryRequest {
3381            query: Some(Query::Sql("SELECT * FROM source".to_string())),
3382        });
3383        assert_permission_denied(
3384            servers::query_handler::grpc::GrpcQueryHandler::do_query(
3385                &instance,
3386                select,
3387                QueryContext::arc(),
3388            )
3389            .await,
3390        );
3391
3392        Ok(())
3393    }
3394
3395    #[tokio::test]
3396    async fn test_target_independent_checker_skips_target_resolution() -> TestResult<()> {
3397        let physical_table = "physical_metric";
3398        let checker = Arc::new(TargetIndependentPermissionChecker::default());
3399        let plugins = Plugins::new();
3400        plugins.insert::<PermissionCheckerRef>(checker.clone());
3401        let instance = test_instance_with_plugins(
3402            test_physical_table(1024, physical_table)?,
3403            test_table(1025, "target")?,
3404            plugins,
3405        )
3406        .await?;
3407
3408        let ctx = test_query_ctx(1);
3409        let physical_target = PermissionTableTarget::new("greptime", "public", physical_table);
3410        assert_eq!(
3411            PermissionTableTargets::Resolved(vec![physical_target.clone()]),
3412            instance
3413                .resolve_query_permission_targets(
3414                    PermissionTableTargets::resolved(vec![physical_target]),
3415                    &ctx,
3416                )
3417                .await
3418                .unwrap()
3419        );
3420        assert_eq!(
3421            vec![physical_table.to_string(), "target".to_string()],
3422            PrometheusHandler::filter_metadata_metric_names(
3423                &instance,
3424                vec![physical_table.to_string(), "target".to_string()],
3425                "public",
3426                &ctx,
3427            )
3428            .await
3429            .unwrap()
3430        );
3431        assert_eq!(1, checker.checks.load(atomic::Ordering::Relaxed));
3432
3433        Ok(())
3434    }
3435
3436    #[tokio::test]
3437    async fn test_query_permission_targets_are_deduplicated() -> TestResult<()> {
3438        let plugins = Plugins::new();
3439        plugins.insert::<PermissionCheckerRef>(Arc::new(RejectUnresolvedPermissionChecker));
3440        let instance = test_instance_with_plugins(
3441            test_table(1024, "source")?,
3442            test_table(1025, "target")?,
3443            plugins,
3444        )
3445        .await?;
3446        let ctx = test_query_ctx(1);
3447        let target = PermissionTableTarget::new("greptime", "public", "target");
3448
3449        assert_eq!(
3450            PermissionTableTargets::Resolved(vec![target.clone()]),
3451            instance
3452                .resolve_query_permission_targets(
3453                    PermissionTableTargets::resolved(vec![target.clone(), target]),
3454                    &ctx,
3455                )
3456                .await
3457                .unwrap()
3458        );
3459
3460        Ok(())
3461    }
3462
3463    #[tokio::test]
3464    async fn test_physical_query_targets_fail_closed() -> TestResult<()> {
3465        let physical_table = "physical_metric";
3466        let plugins = Plugins::new();
3467        plugins.insert::<PermissionCheckerRef>(Arc::new(RejectUnresolvedPermissionChecker));
3468        let instance = test_instance_with_plugins(
3469            test_physical_table(1024, physical_table)?,
3470            test_table(1025, "target")?,
3471            plugins,
3472        )
3473        .await?;
3474
3475        let ctx = test_query_ctx(1);
3476        let logical_target = PermissionTableTarget::new("greptime", "public", "target");
3477        assert_eq!(
3478            PermissionTableTargets::Resolved(vec![logical_target.clone()]),
3479            instance
3480                .resolve_query_permission_targets(
3481                    PermissionTableTargets::resolved(vec![logical_target.clone()]),
3482                    &ctx,
3483                )
3484                .await
3485                .unwrap()
3486        );
3487        let physical_target = PermissionTableTarget::new("greptime", "public", physical_table);
3488        assert_eq!(
3489            PermissionTableTargets::Unresolved,
3490            instance
3491                .resolve_query_permission_targets(
3492                    PermissionTableTargets::resolved(
3493                        vec![logical_target, physical_target.clone(),]
3494                    ),
3495                    &ctx,
3496                )
3497                .await
3498                .unwrap()
3499        );
3500        assert_eq!(
3501            vec!["target".to_string()],
3502            PrometheusHandler::filter_metadata_metric_names(
3503                &instance,
3504                vec!["target".to_string(), "denied".to_string()],
3505                "public",
3506                &ctx,
3507            )
3508            .await
3509            .unwrap()
3510        );
3511
3512        let query = PromQuery {
3513            query: physical_table.to_string(),
3514            ..Default::default()
3515        };
3516        let err = PrometheusHandler::check_query_target_permission(
3517            &instance,
3518            PermissionTableTargets::resolved(vec![physical_target]),
3519            &ctx,
3520        )
3521        .await
3522        .unwrap_err();
3523        assert_eq!(StatusCode::PermissionDenied, err.status_code());
3524        let err = PrometheusHandler::check_query_permission(
3525            &instance,
3526            std::slice::from_ref(&query),
3527            &ctx,
3528        )
3529        .await
3530        .unwrap_err();
3531        assert_eq!(StatusCode::PermissionDenied, err.status_code());
3532        let err = PrometheusHandler::do_query(&instance, &query, ctx.clone())
3533            .await
3534            .unwrap_err();
3535        assert_eq!(StatusCode::PermissionDenied, err.status_code());
3536
3537        for sql in [
3538            "SELECT * FROM physical_metric",
3539            "TQL EVAL (0, 10, '5s') physical_metric",
3540            "INSERT INTO target SELECT * FROM physical_metric",
3541        ] {
3542            let mut results = instance.do_query_inner(sql, ctx.clone()).await;
3543            assert_eq!(1, results.len(), "{sql}");
3544            let err = results.remove(0).unwrap_err();
3545            assert_eq!(StatusCode::PermissionDenied, err.status_code(), "{sql}");
3546        }
3547        let err = LogQueryHandler::query(
3548            &instance,
3549            LogQuery {
3550                table: TableName::new("greptime", "public", physical_table),
3551                ..Default::default()
3552            },
3553            ctx.clone(),
3554        )
3555        .await
3556        .unwrap_err();
3557        assert_eq!(StatusCode::PermissionDenied, err.status_code());
3558        let err = instance
3559            .do_describe_inner(parse_one_sql("SELECT * FROM physical_metric"), ctx.clone())
3560            .await
3561            .unwrap_err();
3562        assert_eq!(StatusCode::PermissionDenied, err.status_code());
3563
3564        let request = ReadRequest {
3565            queries: vec![RemoteQuery {
3566                matchers: vec![LabelMatcher {
3567                    r#type: PromMatcherType::Eq as i32,
3568                    name: servers::prom_store::METRIC_NAME_LABEL.to_string(),
3569                    value: physical_table.to_string(),
3570                }],
3571                ..Default::default()
3572            }],
3573            ..Default::default()
3574        };
3575        let Err(err) = PromStoreProtocolHandler::read(&instance, request, ctx.clone()).await else {
3576            panic!("physical remote-read target must be rejected");
3577        };
3578        assert_eq!(StatusCode::PermissionDenied, err.status_code());
3579
3580        let err = PrometheusHandler::query_label_values(
3581            &instance,
3582            physical_table.to_string(),
3583            "host".to_string(),
3584            vec![],
3585            SystemTime::UNIX_EPOCH,
3586            SystemTime::UNIX_EPOCH,
3587            &ctx,
3588        )
3589        .await
3590        .unwrap_err();
3591        assert_eq!(StatusCode::PermissionDenied, err.status_code());
3592
3593        Ok(())
3594    }
3595
3596    #[tokio::test]
3597    async fn test_non_exact_query_discovery_keeps_denied_targets_for_batch_check() -> TestResult<()>
3598    {
3599        let plugins = Plugins::new();
3600        plugins.insert::<PermissionCheckerRef>(Arc::new(RejectUnresolvedPermissionChecker));
3601        let instance = test_instance_with_plugins_and_metric_names(
3602            test_logical_table(1024, "denied")?,
3603            test_logical_table(1025, "target")?,
3604            plugins,
3605            Some(test_metric_names_table()),
3606        )
3607        .await?;
3608        let ctx = test_query_ctx(1);
3609
3610        let mut metric_names = PrometheusHandler::query_metric_names(
3611            &instance,
3612            vec![Matcher::new(
3613                promql_parser::label::MatchOp::NotEqual,
3614                "__name__",
3615                "",
3616            )],
3617            "public",
3618            &ctx,
3619        )
3620        .await
3621        .unwrap();
3622        metric_names.sort_unstable();
3623        assert_eq!(
3624            vec!["denied".to_string(), "target".to_string()],
3625            metric_names
3626        );
3627
3628        let queries = metric_names
3629            .into_iter()
3630            .map(|query| PromQuery {
3631                query,
3632                ..Default::default()
3633            })
3634            .collect::<Vec<_>>();
3635        let err = PrometheusHandler::check_query_permission(&instance, &queries, &ctx)
3636            .await
3637            .unwrap_err();
3638        assert_eq!(StatusCode::PermissionDenied, err.status_code());
3639
3640        Ok(())
3641    }
3642
3643    #[test]
3644    fn test_fast_legacy_check_is_read_only() {
3645        let cache = DashMap::new();
3646        cache.insert("metric1".to_string(), true);
3647
3648        let names = vec!["metric1".to_string(), "metric2".to_string()];
3649        assert_eq!(Some(true), fast_legacy_check(&cache, &names).unwrap());
3650        assert!(!cache.contains_key("metric2"));
3651
3652        cache_legacy_mode(&cache, &names, true).unwrap();
3653        assert!(*cache.get("metric2").unwrap().value());
3654        assert!(cache_legacy_mode(&cache, &names, false).is_err());
3655        assert!(*cache.get("metric2").unwrap().value());
3656
3657        let cache_incompatible = DashMap::new();
3658        cache_incompatible.insert("metric1".to_string(), true);
3659        cache_incompatible.insert("metric2".to_string(), false);
3660        assert!(fast_legacy_check(&cache_incompatible, &names).is_err());
3661    }
3662
3663    #[test]
3664    fn test_should_track_statement_process() {
3665        assert!(should_track_statement_process(&parse_one_sql(
3666            "SELECT * FROM demo"
3667        )));
3668        assert!(should_track_statement_process(&parse_one_sql(
3669            "INSERT INTO demo SELECT * FROM source"
3670        )));
3671        assert!(!should_track_statement_process(&parse_one_sql(
3672            "INSERT INTO demo VALUES (1)"
3673        )));
3674        assert!(!should_track_statement_process(&parse_one_sql(
3675            "INSERT INTO demo VALUES (now())"
3676        )));
3677    }
3678
3679    #[test]
3680    fn test_should_track_plan_process() {
3681        let select_stmt = parse_one_sql("SELECT * FROM demo");
3682        let insert_select_stmt = parse_one_sql("INSERT INTO demo SELECT * FROM source");
3683        let insert_values_stmt = parse_one_sql("INSERT INTO demo VALUES (now())");
3684
3685        let empty_plan = LogicalPlanBuilder::empty(false).build().unwrap();
3686        assert!(should_track_plan_process(Some(&select_stmt), &empty_plan));
3687        assert!(should_track_plan_process(
3688            Some(&insert_select_stmt),
3689            &insert_dml_plan()
3690        ));
3691        assert!(!should_track_plan_process(
3692            Some(&insert_values_stmt),
3693            &insert_dml_plan()
3694        ));
3695        assert!(!should_track_plan_process(None, &insert_dml_plan()));
3696    }
3697
3698    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
3699    async fn test_insert_select_is_visible_in_show_processlist() -> TestResult<()> {
3700        let insert_sql = "INSERT INTO target SELECT * FROM source";
3701        let (started_tx, mut started_rx) = mpsc::unbounded_channel();
3702        let (finish_tx, finish_rx) = oneshot::channel();
3703        let interceptor = Arc::new(BlockingInsertSelectInterceptor::new(started_tx, finish_rx));
3704        let instance = Arc::new(test_instance_with_insert_select_interceptor(interceptor).await?);
3705
3706        let insert_task = tokio::spawn({
3707            let instance = instance.clone();
3708            async move { execute_one_sql(&instance, insert_sql, test_query_ctx(4242)).await }
3709        });
3710
3711        tokio::time::timeout(Duration::from_secs(5), started_rx.recv())
3712            .await
3713            .context(InsertStartTimeoutSnafu)?
3714            .context(InsertStartChannelClosedSnafu)?;
3715
3716        let output = execute_one_sql(&instance, "SHOW PROCESSLIST", test_query_ctx(43)).await?;
3717        let process_list = output.data.pretty_print().await;
3718        assert!(
3719            process_list.contains(insert_sql),
3720            "process list did not contain running insert:\n{process_list}"
3721        );
3722
3723        finish_tx
3724            .send(())
3725            .map_err(|_| ReleaseBlockedInsertSnafu.build())?;
3726        insert_task.await.context(InsertTaskPanicSnafu)??;
3727
3728        Ok(())
3729    }
3730
3731    #[tokio::test]
3732    async fn test_show_processlist_catalog_scope() -> TestResult<()> {
3733        let instance =
3734            test_instance_with_tables(test_table(1024, "source")?, test_table(1025, "target")?)
3735                .await?;
3736        let _current_catalog = instance.process_manager().register_query(
3737            "greptime".to_string(),
3738            vec!["public".to_string()],
3739            "current_catalog_query".to_string(),
3740            String::new(),
3741            None,
3742            None,
3743        );
3744        let _other_catalog = instance.process_manager().register_query(
3745            "other".to_string(),
3746            vec!["public".to_string()],
3747            "other_catalog_query".to_string(),
3748            String::new(),
3749            None,
3750            None,
3751        );
3752
3753        for sql in ["SHOW PROCESSLIST", "SHOW FULL PROCESSLIST"] {
3754            let output = execute_one_sql(&instance, sql, test_query_ctx(43)).await?;
3755            let process_list = output.data.pretty_print().await;
3756            assert!(
3757                process_list.contains("current_catalog_query"),
3758                "{process_list}"
3759            );
3760            assert!(
3761                !process_list.contains("other_catalog_query"),
3762                "{process_list}"
3763            );
3764
3765            let admin_ctx = test_query_ctx(44);
3766            admin_ctx.set_current_user(Arc::new(AdminUserInfo));
3767            let output = execute_one_sql(&instance, sql, admin_ctx).await?;
3768            let process_list = output.data.pretty_print().await;
3769            assert!(
3770                process_list.contains("current_catalog_query"),
3771                "{process_list}"
3772            );
3773            assert!(
3774                process_list.contains("other_catalog_query"),
3775                "{process_list}"
3776            );
3777        }
3778
3779        Ok(())
3780    }
3781
3782    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
3783    async fn test_kill_query_cancels_insert_select() -> TestResult<()> {
3784        assert_kill_cancels_insert_select("KILL QUERY 4242").await
3785    }
3786
3787    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
3788    async fn test_kill_process_id_cancels_insert_select() -> TestResult<()> {
3789        assert_kill_cancels_insert_select("KILL 'test-frontend/4242'").await
3790    }
3791
3792    async fn assert_kill_cancels_insert_select(kill_sql: &str) -> TestResult<()> {
3793        let insert_sql = "INSERT INTO target SELECT * FROM source";
3794        let (source_polled_tx, source_polled_rx) = oneshot::channel();
3795        let instance = Arc::new(
3796            test_instance_with_tables(
3797                pending_table(1024, "source", source_polled_tx)?,
3798                test_table(1025, "target")?,
3799            )
3800            .await?,
3801        );
3802
3803        let insert_task = tokio::spawn({
3804            let instance = instance.clone();
3805            async move { execute_one_sql(&instance, insert_sql, test_query_ctx(4242)).await }
3806        });
3807
3808        tokio::time::timeout(Duration::from_secs(5), source_polled_rx)
3809            .await
3810            .context(SourcePollTimeoutSnafu)?
3811            .context(SourcePollChannelClosedSnafu)?;
3812
3813        let output = execute_one_sql(&instance, kill_sql, test_query_ctx(43)).await?;
3814        assert!(matches!(output.data, OutputData::AffectedRows(1)));
3815
3816        let insert_result = tokio::time::timeout(Duration::from_secs(5), insert_task)
3817            .await
3818            .context(InsertTaskTimeoutSnafu)?
3819            .context(InsertTaskPanicSnafu)?;
3820        let err = match insert_result {
3821            Ok(_) => return InsertSelectNotCancelledSnafu.fail(),
3822            Err(TestError::ExecuteSql { source, .. }) => source,
3823            Err(err) => return Err(err),
3824        };
3825        assert_eq!(StatusCode::Cancelled, err.status_code());
3826
3827        let output = execute_one_sql(&instance, "SHOW PROCESSLIST", test_query_ctx(43)).await?;
3828        let process_list = output.data.pretty_print().await;
3829        assert!(
3830            !process_list.contains(insert_sql),
3831            "process list still contains killed insert:\n{process_list}"
3832        );
3833
3834        Ok(())
3835    }
3836
3837    fn insert_dml_plan() -> LogicalPlan {
3838        let schema = SchemaRef::new(Schema::new(vec![Field::new(
3839            "value",
3840            DataType::Int64,
3841            true,
3842        )]));
3843        let target = Arc::new(LogicalTableSource::new(schema));
3844        let input = LogicalPlanBuilder::empty(false).build().unwrap();
3845
3846        LogicalPlanBuilder::insert_into(input, "demo", target, InsertOp::Append)
3847            .unwrap()
3848            .build()
3849            .unwrap()
3850    }
3851
3852    #[test]
3853    fn test_exec_validation() {
3854        let query_ctx = QueryContext::arc();
3855        let plugins: Plugins = Plugins::new();
3856        plugins.insert(QueryOptions {
3857            disallow_cross_catalog_query: true,
3858        });
3859
3860        let sql = r#"
3861        SELECT * FROM demo;
3862        EXPLAIN SELECT * FROM demo;
3863        CREATE DATABASE test_database;
3864        SHOW DATABASES;
3865        "#;
3866        let stmts = parse_stmt(sql, &GreptimeDbDialect {}).unwrap();
3867        assert_eq!(stmts.len(), 4);
3868        for stmt in stmts {
3869            let re = check_permission(plugins.clone(), &stmt, &query_ctx);
3870            re.unwrap();
3871        }
3872
3873        let sql = r#"
3874        SHOW CREATE TABLE demo;
3875        ALTER TABLE demo ADD COLUMN new_col INT;
3876        "#;
3877        let stmts = parse_stmt(sql, &GreptimeDbDialect {}).unwrap();
3878        assert_eq!(stmts.len(), 2);
3879        for stmt in stmts {
3880            let re = check_permission(plugins.clone(), &stmt, &query_ctx);
3881            re.unwrap();
3882        }
3883
3884        fn replace_test(template_sql: &str, plugins: Plugins, query_ctx: &QueryContextRef) {
3885            // test right
3886            let right = vec![("", ""), ("", "public."), ("greptime.", "public.")];
3887            for (catalog, schema) in right {
3888                let sql = do_fmt(template_sql, catalog, schema);
3889                do_test(&sql, plugins.clone(), query_ctx, true);
3890            }
3891
3892            let wrong = vec![
3893                ("wrongcatalog.", "public."),
3894                ("wrongcatalog.", "wrongschema."),
3895            ];
3896            for (catalog, schema) in wrong {
3897                let sql = do_fmt(template_sql, catalog, schema);
3898                do_test(&sql, plugins.clone(), query_ctx, false);
3899            }
3900        }
3901
3902        fn do_fmt(template: &str, catalog: &str, schema: &str) -> String {
3903            let vars = HashMap::from([
3904                ("catalog".to_string(), catalog),
3905                ("schema".to_string(), schema),
3906            ]);
3907            template.format(&vars).unwrap()
3908        }
3909
3910        fn do_test(sql: &str, plugins: Plugins, query_ctx: &QueryContextRef, is_ok: bool) {
3911            let stmt = &parse_stmt(sql, &GreptimeDbDialect {}).unwrap()[0];
3912            let re = check_permission(plugins, stmt, query_ctx);
3913            if is_ok {
3914                re.unwrap();
3915            } else {
3916                assert!(re.is_err());
3917            }
3918        }
3919
3920        // test insert
3921        let sql = "INSERT INTO {catalog}{schema}monitor(host) VALUES ('host1');";
3922        replace_test(sql, plugins.clone(), &query_ctx);
3923
3924        // test create table
3925        let sql = r#"CREATE TABLE {catalog}{schema}demo(
3926                            host STRING,
3927                            ts TIMESTAMP,
3928                            TIME INDEX (ts),
3929                            PRIMARY KEY(host)
3930                        ) engine=mito;"#;
3931        replace_test(sql, plugins.clone(), &query_ctx);
3932
3933        // test drop table
3934        let sql = "DROP TABLE {catalog}{schema}demo;";
3935        replace_test(sql, plugins.clone(), &query_ctx);
3936
3937        // test undrop table
3938        #[cfg(feature = "enterprise")]
3939        {
3940            let sql = "UNDROP TABLE {catalog}{schema}demo;";
3941            replace_test(sql, plugins.clone(), &query_ctx);
3942        }
3943
3944        // test show tables
3945        let sql = "SHOW TABLES FROM public";
3946        let stmt = parse_stmt(sql, &GreptimeDbDialect {}).unwrap();
3947        check_permission(plugins.clone(), &stmt[0], &query_ctx).unwrap();
3948
3949        let sql = "SHOW TABLES FROM private";
3950        let stmt = parse_stmt(sql, &GreptimeDbDialect {}).unwrap();
3951        let re = check_permission(plugins.clone(), &stmt[0], &query_ctx);
3952        assert!(re.is_ok());
3953
3954        // test describe table
3955        let sql = "DESC TABLE {catalog}{schema}demo;";
3956        replace_test(sql, plugins.clone(), &query_ctx);
3957
3958        let comment_flow_cases = [
3959            ("COMMENT ON FLOW my_flow IS 'comment';", true),
3960            ("COMMENT ON FLOW greptime.my_flow IS 'comment';", true),
3961            ("COMMENT ON FLOW wrongcatalog.my_flow IS 'comment';", false),
3962        ];
3963        for (sql, is_ok) in comment_flow_cases {
3964            let stmt = &parse_stmt(sql, &GreptimeDbDialect {}).unwrap()[0];
3965            let result = check_permission(plugins.clone(), stmt, &query_ctx);
3966            assert_eq!(result.is_ok(), is_ok);
3967        }
3968
3969        let show_flow_cases = [
3970            ("SHOW CREATE FLOW my_flow;", true),
3971            ("SHOW CREATE FLOW greptime.my_flow;", true),
3972            ("SHOW CREATE FLOW wrongcatalog.my_flow;", false),
3973        ];
3974        for (sql, is_ok) in show_flow_cases {
3975            let stmt = &parse_stmt(sql, &GreptimeDbDialect {}).unwrap()[0];
3976            let result = check_permission(plugins.clone(), stmt, &query_ctx);
3977            assert_eq!(result.is_ok(), is_ok);
3978        }
3979    }
3980
3981    /// A `DropView` DDL sent through the direct gRPC ingress must return an error
3982    /// (e.g. table not found) instead of panicking on `todo!()`.
3983    #[tokio::test]
3984    async fn qx_152_drop_view_via_grpc_ddl_returns_error_not_panic() -> TestResult<()> {
3985        let instance =
3986            test_instance_with_tables(test_table(1024, "source")?, test_table(1025, "target")?)
3987                .await?;
3988
3989        let request = api::v1::greptime_request::Request::Ddl(api::v1::DdlRequest {
3990            expr: Some(api::v1::ddl_request::Expr::DropView(
3991                api::v1::DropViewExpr {
3992                    catalog_name: String::new(),
3993                    schema_name: String::new(),
3994                    view_name: "non_existent_view".to_string(),
3995                    view_id: None,
3996                    drop_if_exists: false,
3997                },
3998            )),
3999        });
4000
4001        let result = servers::query_handler::grpc::GrpcQueryHandler::do_query(
4002            &instance,
4003            request,
4004            QueryContext::arc(),
4005        )
4006        .await;
4007
4008        let err = match result {
4009            Ok(_) => panic!("DropView DDL request must return an error instead of panicking"),
4010            Err(err) => err,
4011        };
4012        assert_eq!(
4013            err.status_code(),
4014            StatusCode::TableNotFound,
4015            "dropping a non-existent view without IF EXISTS must report TableNotFound, got {err}"
4016        );
4017        Ok(())
4018    }
4019
4020    /// `DROP VIEW IF EXISTS` on a missing view through the direct gRPC ingress must
4021    /// succeed with 0 affected rows (no error, no DDL task submitted), instead of
4022    /// returning `TableNotFound`.
4023    #[tokio::test]
4024    async fn qx_152_drop_view_if_exists_missing_view_via_grpc_ddl_succeeds() -> TestResult<()> {
4025        let catalog_manager =
4026            catalog::memory::MemoryCatalogManager::new_with_table(test_table(1024, "source")?);
4027        let procedure_executor = Arc::new(MockProcedureExecutor::new(catalog_manager.clone()));
4028        let instance = test_instance_with_catalog_manager(
4029            catalog_manager,
4030            test_table(1025, "target")?,
4031            Plugins::new(),
4032            None,
4033            procedure_executor.clone() as ProcedureExecutorRef,
4034        )
4035        .await?;
4036
4037        let request = api::v1::greptime_request::Request::Ddl(api::v1::DdlRequest {
4038            expr: Some(api::v1::ddl_request::Expr::DropView(
4039                api::v1::DropViewExpr {
4040                    catalog_name: String::new(),
4041                    schema_name: String::new(),
4042                    view_name: "non_existent_view".to_string(),
4043                    view_id: None,
4044                    drop_if_exists: true,
4045                },
4046            )),
4047        });
4048
4049        let result = servers::query_handler::grpc::GrpcQueryHandler::do_query(
4050            &instance,
4051            request,
4052            QueryContext::arc(),
4053        )
4054        .await;
4055
4056        let output = match result {
4057            Ok(output) => output,
4058            Err(err) => {
4059                panic!("DROP VIEW IF EXISTS on a missing view must succeed, got error: {err}")
4060            }
4061        };
4062        assert!(
4063            matches!(output.data, OutputData::AffectedRows(0)),
4064            "DROP VIEW IF EXISTS on a missing view must report 0 affected rows"
4065        );
4066        assert!(
4067            procedure_executor.submitted.lock().unwrap().is_empty(),
4068            "DROP VIEW IF EXISTS on a missing view must not submit a DDL task"
4069        );
4070        Ok(())
4071    }
4072
4073    /// A `CREATE VIEW` followed by `DROP VIEW` through the direct gRPC ingress must
4074    /// succeed end to end: the view is registered in the catalog and then removed.
4075    #[tokio::test]
4076    async fn qx_152_drop_existing_view_via_grpc_ddl_succeeds() -> TestResult<()> {
4077        let catalog_manager =
4078            catalog::memory::MemoryCatalogManager::new_with_table(test_table(1024, "source")?);
4079        let procedure_executor = Arc::new(MockProcedureExecutor::new(catalog_manager.clone()));
4080        let instance = test_instance_with_catalog_manager(
4081            catalog_manager,
4082            test_table(1025, "target")?,
4083            Plugins::new(),
4084            None,
4085            procedure_executor.clone() as ProcedureExecutorRef,
4086        )
4087        .await?;
4088
4089        // The default "greptime.public" schema must be visible to the kv-backed table
4090        // metadata manager for `CREATE VIEW`/`CREATE TABLE` to pass validation.
4091        instance
4092            .table_metadata_manager()
4093            .schema_manager()
4094            .create(
4095                common_meta::key::schema_name::SchemaNameKey::new("greptime", "public"),
4096                None,
4097                true,
4098            )
4099            .await
4100            .unwrap();
4101
4102        let create_view_request = api::v1::greptime_request::Request::Ddl(api::v1::DdlRequest {
4103            expr: Some(api::v1::ddl_request::Expr::CreateView(
4104                api::v1::CreateViewExpr {
4105                    catalog_name: String::new(),
4106                    schema_name: String::new(),
4107                    view_name: "my_view".to_string(),
4108                    logical_plan: vec![1, 2, 3],
4109                    create_if_not_exists: false,
4110                    or_replace: false,
4111                    table_names: vec![],
4112                    columns: vec![],
4113                    plan_columns: vec![],
4114                    definition: "CREATE VIEW my_view AS SELECT * FROM source".to_string(),
4115                },
4116            )),
4117        });
4118
4119        let output = match servers::query_handler::grpc::GrpcQueryHandler::do_query(
4120            &instance,
4121            create_view_request,
4122            QueryContext::arc(),
4123        )
4124        .await
4125        {
4126            Ok(output) => output,
4127            Err(err) => panic!("CREATE VIEW via gRPC DDL must succeed, got error: {err}"),
4128        };
4129        assert!(
4130            matches!(output.data, OutputData::AffectedRows(0)),
4131            "CREATE VIEW via gRPC DDL must report 0 affected rows"
4132        );
4133
4134        // The view is registered in the catalog as a view.
4135        let view = instance
4136            .catalog_manager()
4137            .table("greptime", "public", "my_view", None)
4138            .await
4139            .unwrap()
4140            .expect("view should exist after CREATE VIEW");
4141        assert_eq!(view.table_info().table_type, TableType::View);
4142
4143        let drop_view_request = api::v1::greptime_request::Request::Ddl(api::v1::DdlRequest {
4144            expr: Some(api::v1::ddl_request::Expr::DropView(
4145                api::v1::DropViewExpr {
4146                    catalog_name: String::new(),
4147                    schema_name: String::new(),
4148                    view_name: "my_view".to_string(),
4149                    view_id: None,
4150                    drop_if_exists: false,
4151                },
4152            )),
4153        });
4154
4155        let output = match servers::query_handler::grpc::GrpcQueryHandler::do_query(
4156            &instance,
4157            drop_view_request,
4158            QueryContext::arc(),
4159        )
4160        .await
4161        {
4162            Ok(output) => output,
4163            Err(err) => panic!("DROP VIEW via gRPC DDL must succeed, got error: {err}"),
4164        };
4165        assert!(
4166            matches!(output.data, OutputData::AffectedRows(0)),
4167            "DROP VIEW via gRPC DDL must report 0 affected rows"
4168        );
4169
4170        // The view is gone after the drop.
4171        assert!(
4172            instance
4173                .catalog_manager()
4174                .table("greptime", "public", "my_view", None)
4175                .await
4176                .unwrap()
4177                .is_none(),
4178            "view should be removed after DROP VIEW"
4179        );
4180
4181        let submitted = procedure_executor.submitted.lock().unwrap();
4182        assert_eq!(
4183            submitted.len(),
4184            2,
4185            "expected create and drop view tasks, got {submitted:?}"
4186        );
4187        assert!(matches!(&submitted[0], DdlTask::CreateView(_)));
4188        assert!(matches!(&submitted[1], DdlTask::DropView(_)));
4189        Ok(())
4190    }
4191
4192    /// A direct gRPC `CreateTable` whose time index column is not a timestamp must
4193    /// be rejected with `InvalidArguments` instead of panicking while building the schema.
4194    #[tokio::test]
4195    async fn qx_153_create_table_with_non_timestamp_time_index_via_grpc_returns_error()
4196    -> TestResult<()> {
4197        let instance =
4198            test_instance_with_tables(test_table(1024, "source")?, test_table(1025, "target")?)
4199                .await?;
4200
4201        let request = api::v1::greptime_request::Request::Ddl(api::v1::DdlRequest {
4202            expr: Some(api::v1::ddl_request::Expr::CreateTable(
4203                api::v1::CreateTableExpr {
4204                    catalog_name: String::new(),
4205                    schema_name: String::new(),
4206                    table_name: "demo".to_string(),
4207                    desc: String::new(),
4208                    column_defs: vec![api::v1::ColumnDef {
4209                        name: "host".to_string(),
4210                        data_type: api::v1::ColumnDataType::String as i32,
4211                        is_nullable: true,
4212                        default_constraint: vec![],
4213                        semantic_type: 0,
4214                        comment: String::new(),
4215                        datatype_extension: None,
4216                        options: None,
4217                    }],
4218                    time_index: "host".to_string(),
4219                    primary_keys: vec![],
4220                    create_if_not_exists: false,
4221                    table_options: HashMap::new(),
4222                    table_id: None,
4223                    engine: "mito".to_string(),
4224                },
4225            )),
4226        });
4227
4228        let result = servers::query_handler::grpc::GrpcQueryHandler::do_query(
4229            &instance,
4230            request,
4231            QueryContext::arc(),
4232        )
4233        .await;
4234
4235        let err = match result {
4236            Ok(_) => panic!("CreateTable with a non-timestamp time index must be rejected"),
4237            Err(err) => err,
4238        };
4239        assert_eq!(err.status_code(), StatusCode::InvalidArguments, "{err}");
4240        Ok(())
4241    }
4242
4243    /// A valid `CREATE TABLE` (timestamp time index) through the direct gRPC ingress
4244    /// must succeed, guarding that the `validate_create_expr` ingress check doesn't
4245    /// accidentally reject good requests.
4246    #[tokio::test]
4247    async fn qx_153_create_table_with_timestamp_time_index_via_grpc_succeeds() -> TestResult<()> {
4248        let catalog_manager =
4249            catalog::memory::MemoryCatalogManager::new_with_table(test_table(1024, "source")?);
4250        let procedure_executor = Arc::new(MockProcedureExecutor::new(catalog_manager.clone()));
4251        let instance = test_instance_with_catalog_manager(
4252            catalog_manager,
4253            test_table(1025, "target")?,
4254            Plugins::new(),
4255            None,
4256            procedure_executor.clone() as ProcedureExecutorRef,
4257        )
4258        .await?;
4259
4260        // The default "greptime.public" schema must be visible to the kv-backed table
4261        // metadata manager for `CREATE TABLE` to pass validation.
4262        instance
4263            .table_metadata_manager()
4264            .schema_manager()
4265            .create(
4266                common_meta::key::schema_name::SchemaNameKey::new("greptime", "public"),
4267                None,
4268                true,
4269            )
4270            .await
4271            .unwrap();
4272
4273        let request = api::v1::greptime_request::Request::Ddl(api::v1::DdlRequest {
4274            expr: Some(api::v1::ddl_request::Expr::CreateTable(
4275                api::v1::CreateTableExpr {
4276                    catalog_name: String::new(),
4277                    schema_name: String::new(),
4278                    table_name: "demo".to_string(),
4279                    desc: String::new(),
4280                    column_defs: vec![
4281                        api::v1::ColumnDef {
4282                            name: "host".to_string(),
4283                            data_type: api::v1::ColumnDataType::String as i32,
4284                            is_nullable: true,
4285                            default_constraint: vec![],
4286                            semantic_type: 0,
4287                            comment: String::new(),
4288                            datatype_extension: None,
4289                            options: None,
4290                        },
4291                        api::v1::ColumnDef {
4292                            name: "ts".to_string(),
4293                            data_type: api::v1::ColumnDataType::TimestampMillisecond as i32,
4294                            is_nullable: true,
4295                            default_constraint: vec![],
4296                            semantic_type: 0,
4297                            comment: String::new(),
4298                            datatype_extension: None,
4299                            options: None,
4300                        },
4301                    ],
4302                    time_index: "ts".to_string(),
4303                    primary_keys: vec![],
4304                    create_if_not_exists: false,
4305                    table_options: HashMap::new(),
4306                    table_id: None,
4307                    engine: "mito".to_string(),
4308                },
4309            )),
4310        });
4311
4312        let output = match servers::query_handler::grpc::GrpcQueryHandler::do_query(
4313            &instance,
4314            request,
4315            QueryContext::arc(),
4316        )
4317        .await
4318        {
4319            Ok(output) => output,
4320            Err(err) => panic!("CREATE TABLE via gRPC DDL must succeed, got error: {err}"),
4321        };
4322        assert!(
4323            matches!(output.data, OutputData::AffectedRows(0)),
4324            "CREATE TABLE via gRPC DDL must report 0 affected rows"
4325        );
4326
4327        // The table is registered in the catalog.
4328        let table = instance
4329            .catalog_manager()
4330            .table("greptime", "public", "demo", None)
4331            .await
4332            .unwrap()
4333            .expect("table should exist after CREATE TABLE");
4334        assert_eq!(table.table_info().table_type, TableType::Base);
4335
4336        let submitted = procedure_executor.submitted.lock().unwrap();
4337        assert_eq!(
4338            submitted.len(),
4339            1,
4340            "expected one create table task, got {submitted:?}"
4341        );
4342        assert!(matches!(&submitted[0], DdlTask::CreateTable(_)));
4343        Ok(())
4344    }
4345}