1use 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};
22use crate::native_histogram::NATIVE_HISTOGRAM_FIELD;
23
24static GREPTIME_TIMESTAMP_CELL: OnceCell<String> = OnceCell::new();
26
27static GREPTIME_VALUE_CELL: OnceCell<String> = OnceCell::new();
29
30static GREPTIME_NATIVE_HISTOGRAM_CELL: OnceCell<String> = OnceCell::new();
32
33static GREPTIME_EXEMPLAR_LABELS_CELL: OnceCell<String> = OnceCell::new();
35
36pub fn set_default_prefix(prefix: Option<&str>) -> Result<()> {
37 let stripped = prefix.map(|s| {
41 s.strip_prefix('"')
42 .and_then(|s| s.strip_suffix('"'))
43 .unwrap_or(s)
44 });
45
46 match stripped {
47 None => {
48 GREPTIME_TIMESTAMP_CELL.get_or_init(|| GREPTIME_TIMESTAMP.to_string());
50 GREPTIME_VALUE_CELL.get_or_init(|| GREPTIME_VALUE.to_string());
51 GREPTIME_NATIVE_HISTOGRAM_CELL.get_or_init(|| NATIVE_HISTOGRAM_FIELD.to_string());
52 GREPTIME_EXEMPLAR_LABELS_CELL.get_or_init(|| GREPTIME_EXEMPLAR_LABELS.to_string());
53 }
54 Some(s) if s.trim().is_empty() => {
55 GREPTIME_TIMESTAMP_CELL.get_or_init(|| "timestamp".to_string());
57 GREPTIME_VALUE_CELL.get_or_init(|| "value".to_string());
58 GREPTIME_NATIVE_HISTOGRAM_CELL.get_or_init(|| "native_histogram".to_string());
59 GREPTIME_EXEMPLAR_LABELS_CELL.get_or_init(|| "exemplar_labels".to_string());
60 }
61 Some(x) => {
62 ensure!(
63 NAME_PATTERN_REG.is_match(x),
64 InvalidColumnPrefixSnafu { prefix: x }
65 );
66 GREPTIME_TIMESTAMP_CELL.get_or_init(|| format!("{}_timestamp", x));
67 GREPTIME_VALUE_CELL.get_or_init(|| format!("{}_value", x));
68 GREPTIME_NATIVE_HISTOGRAM_CELL.get_or_init(|| format!("{}_native_histogram", x));
69 GREPTIME_EXEMPLAR_LABELS_CELL.get_or_init(|| format!("{}_exemplar_labels", x));
70 }
71 }
72 Ok(())
73}
74
75pub fn greptime_timestamp() -> &'static str {
78 GREPTIME_TIMESTAMP_CELL.get_or_init(|| GREPTIME_TIMESTAMP.to_string())
79}
80
81pub fn greptime_value() -> &'static str {
84 GREPTIME_VALUE_CELL.get_or_init(|| GREPTIME_VALUE.to_string())
85}
86
87#[inline]
90pub fn greptime_native_histogram() -> &'static str {
91 GREPTIME_NATIVE_HISTOGRAM_CELL
92 .get()
93 .map_or(NATIVE_HISTOGRAM_FIELD, String::as_str)
94}
95
96#[inline]
98pub fn greptime_exemplar_labels() -> &'static str {
99 GREPTIME_EXEMPLAR_LABELS_CELL
100 .get()
101 .map_or(GREPTIME_EXEMPLAR_LABELS, String::as_str)
102}
103
104const GREPTIME_TIMESTAMP: &str = "greptime_timestamp";
106const GREPTIME_VALUE: &str = "greptime_value";
108pub const EXEMPLAR_METRIC_COLUMN: &str = "greptime_metric";
110const GREPTIME_EXEMPLAR_LABELS: &str = "greptime_exemplar_labels";
112pub const OTLP_AGGREGATION_TEMPORALITY_LABEL: &str = "otlp_aggregation_temporality";
114pub const GREPTIME_TEMPORALITY_DELTA: &str = "delta";
116pub const GREPTIME_PHYSICAL_TABLE: &str = "greptime_physical_table";
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122
123 #[test]
127 fn test_set_default_prefix_none() {
128 set_default_prefix(None).unwrap();
129 assert_eq!(greptime_timestamp(), "greptime_timestamp");
130 assert_eq!(greptime_value(), "greptime_value");
131 assert_eq!(greptime_native_histogram(), "greptime_native_histogram");
132 assert_eq!(greptime_exemplar_labels(), "greptime_exemplar_labels");
133 }
134
135 #[test]
136 fn test_set_default_prefix_empty_string() {
137 set_default_prefix(Some("")).unwrap();
138 assert_eq!(greptime_timestamp(), "timestamp");
139 assert_eq!(greptime_value(), "value");
140 assert_eq!(greptime_native_histogram(), "native_histogram");
141 assert_eq!(greptime_exemplar_labels(), "exemplar_labels");
142 }
143
144 #[test]
145 fn test_set_default_prefix_quoted_empty() {
146 set_default_prefix(Some("\"\"")).unwrap();
148 assert_eq!(greptime_timestamp(), "timestamp");
149 assert_eq!(greptime_value(), "value");
150 assert_eq!(greptime_native_histogram(), "native_histogram");
151 assert_eq!(greptime_exemplar_labels(), "exemplar_labels");
152 }
153
154 #[test]
155 fn test_set_default_prefix_custom() {
156 set_default_prefix(Some("mydb")).unwrap();
157 assert_eq!(greptime_timestamp(), "mydb_timestamp");
158 assert_eq!(greptime_value(), "mydb_value");
159 assert_eq!(greptime_native_histogram(), "mydb_native_histogram");
160 assert_eq!(greptime_exemplar_labels(), "mydb_exemplar_labels");
161 }
162
163 #[test]
164 fn test_set_default_prefix_invalid() {
165 assert!(set_default_prefix(Some("invalid prefix!")).is_err());
166 }
167}