Skip to main content

query/
promql.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 error;
16pub mod label_values;
17pub mod native_compile;
18pub mod planner;
19
20use datafusion_common::tree_node::{TreeNode as _, TreeNodeRecursion};
21use datafusion_expr::{Extension, LogicalPlan};
22use promql::extension_plan::{
23    Absent, EmptyMetric, HistogramFold, InstantManipulate, RangeManipulate, ScalarCalculate,
24    SeriesDivide, SeriesNormalize, UnionDistinctOn,
25};
26
27/// Returns true if the plan contains PromQL-specific extension plan nodes.
28pub fn plan_contains_promql_extension(plan: &LogicalPlan) -> bool {
29    let mut found = false;
30    let _ = plan.apply(|node| {
31        if is_promql_extension_plan(node) {
32            found = true;
33            Ok(TreeNodeRecursion::Stop)
34        } else {
35            Ok(TreeNodeRecursion::Continue)
36        }
37    });
38    found
39}
40
41fn is_promql_extension_plan(plan: &LogicalPlan) -> bool {
42    let LogicalPlan::Extension(Extension { node }) = plan else {
43        return false;
44    };
45
46    node.as_any().is::<Absent>()
47        || node.as_any().is::<EmptyMetric>()
48        || node.as_any().is::<HistogramFold>()
49        || node.as_any().is::<InstantManipulate>()
50        || node.as_any().is::<RangeManipulate>()
51        || node.as_any().is::<ScalarCalculate>()
52        || node.as_any().is::<SeriesDivide>()
53        || node.as_any().is::<SeriesNormalize>()
54        || node.as_any().is::<UnionDistinctOn>()
55}
56
57#[cfg(test)]
58mod tests {
59    use std::sync::Arc;
60
61    use datafusion_common::DFSchema;
62    use datafusion_expr::{EmptyRelation, Extension, LogicalPlanBuilder, col};
63
64    use super::*;
65
66    #[test]
67    fn plan_contains_promql_extension_returns_true_for_promql_extension() {
68        let plan = empty_metric_plan();
69
70        assert!(plan_contains_promql_extension(&plan));
71    }
72
73    #[test]
74    fn plan_contains_promql_extension_returns_true_for_nested_promql_extension() {
75        let plan = LogicalPlanBuilder::from(empty_metric_plan())
76            .project(vec![col("ts")])
77            .unwrap()
78            .build()
79            .unwrap();
80
81        assert!(plan_contains_promql_extension(&plan));
82    }
83
84    #[test]
85    fn plan_contains_promql_extension_returns_false_for_non_promql_plan() {
86        let plan = LogicalPlan::EmptyRelation(EmptyRelation {
87            produce_one_row: false,
88            schema: Arc::new(DFSchema::empty()),
89        });
90
91        assert!(!plan_contains_promql_extension(&plan));
92    }
93
94    fn empty_metric_plan() -> LogicalPlan {
95        let empty_metric = EmptyMetric::new(
96            0,
97            10_000,
98            5_000,
99            "ts".to_string(),
100            "greptime_value".to_string(),
101            None,
102        )
103        .unwrap();
104
105        LogicalPlan::Extension(Extension {
106            node: Arc::new(empty_metric),
107        })
108    }
109}