common_query/
prelude.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use common_base::regex_pattern::NAME_PATTERN_REG;
16pub use datafusion_common::ScalarValue;
17use once_cell::sync::OnceCell;
18use snafu::ensure;
19
20pub use crate::columnar_value::ColumnarValue;
21use crate::error::{InvalidColumnPrefixSnafu, Result};
22
23/// Default time index column name.
24static GREPTIME_TIMESTAMP_CELL: OnceCell<String> = OnceCell::new();
25
26/// Default value column name.
27static GREPTIME_VALUE_CELL: OnceCell<String> = OnceCell::new();
28
29pub fn set_default_prefix(prefix: Option<&str>) -> Result<()> {
30    match prefix {
31        None => {
32            // use default greptime prefix
33            GREPTIME_TIMESTAMP_CELL.get_or_init(|| GREPTIME_TIMESTAMP.to_string());
34            GREPTIME_VALUE_CELL.get_or_init(|| GREPTIME_VALUE.to_string());
35        }
36        Some(s) if s.trim().is_empty() => {
37            // use "" to disable prefix
38            GREPTIME_TIMESTAMP_CELL.get_or_init(|| "timestamp".to_string());
39            GREPTIME_VALUE_CELL.get_or_init(|| "value".to_string());
40        }
41        Some(x) => {
42            ensure!(
43                NAME_PATTERN_REG.is_match(x),
44                InvalidColumnPrefixSnafu { prefix: x }
45            );
46            GREPTIME_TIMESTAMP_CELL.get_or_init(|| format!("{}_timestamp", x));
47            GREPTIME_VALUE_CELL.get_or_init(|| format!("{}_value", x));
48        }
49    }
50    Ok(())
51}
52
53/// Get the default timestamp column name.
54/// Returns the configured value, or `greptime_timestamp` if not set.
55pub fn greptime_timestamp() -> &'static str {
56    GREPTIME_TIMESTAMP_CELL.get_or_init(|| GREPTIME_TIMESTAMP.to_string())
57}
58
59/// Get the default value column name.
60/// Returns the configured value, or `greptime_value` if not set.
61pub fn greptime_value() -> &'static str {
62    GREPTIME_VALUE_CELL.get_or_init(|| GREPTIME_VALUE.to_string())
63}
64
65/// Default timestamp column name constant for backward compatibility.
66const GREPTIME_TIMESTAMP: &str = "greptime_timestamp";
67/// Default value column name constant for backward compatibility.
68const GREPTIME_VALUE: &str = "greptime_value";
69/// Default counter column name for OTLP metrics (legacy mode).
70pub const GREPTIME_COUNT: &str = "greptime_count";
71/// Default physical table name
72pub const GREPTIME_PHYSICAL_TABLE: &str = "greptime_physical_table";