1use std::{str::FromStr, time::Duration};
2
3use crate::function::*;
4
5#[derive(Debug)]
7pub enum Expr {
8 AggregateExpr(AggregateExpr),
9 BinaryExpr(BinaryExpr),
10 FunctionCall(FunctionCall),
11 MatrixSelector(MatrixSelector),
12 SubqueryExpr(SubqueryExpr),
13 NumberLiteral(NumberLiteral),
14 ParenExpr(ParenExpr),
15 StringLiteral(StringLiteral),
16 UnaryExpr(UnaryExpr),
17 VectorSelector(VectorSelector),
18}
19
20impl Expr {
21 pub fn get_type(&self) -> ValueType {
30 match self {
31 Expr::AggregateExpr(_) => ValueType::Vector,
32 Expr::FunctionCall(call) => call.func.return_type,
33 Expr::MatrixSelector(_) => ValueType::Matrix,
34 Expr::SubqueryExpr(_) => ValueType::Matrix,
35 Expr::NumberLiteral(_) => ValueType::Scalar,
36 Expr::ParenExpr(p) => p.expr.get_type(),
37 Expr::StringLiteral(_) => ValueType::String,
38 Expr::UnaryExpr(u) => u.rhs.get_type(),
39 Expr::VectorSelector(_) => ValueType::Vector,
40 Expr::BinaryExpr(b) => {
41 if b.lhs.get_type() == ValueType::Scalar && b.rhs.get_type() == ValueType::Scalar {
42 ValueType::Scalar
43 } else {
44 ValueType::Vector
45 }
46 }
47 }
48 }
49}
50
51#[derive(Debug, Clone, Copy, PartialEq)]
53pub enum ValueType {
54 Vector,
57 Matrix,
60 Scalar,
62 String,
64}
65
66#[derive(Debug, PartialEq)]
69pub enum AggregateOp {
70 Avg,
72 Bottomk,
74 Count,
76 CountValues,
78 Group,
80 Max,
82 Min,
84 Stddev,
86 Stdvar,
88 Quantile,
90 Sum,
92 Topk,
94}
95
96impl FromStr for AggregateOp {
97 type Err = String;
98
99 fn from_str(s: &str) -> Result<Self, Self::Err> {
100 match s {
101 "avg" => Ok(AggregateOp::Avg),
102 "bottomk" => Ok(AggregateOp::Bottomk),
103 "count" => Ok(AggregateOp::Count),
104 "count_values" => Ok(AggregateOp::CountValues),
105 "group" => Ok(AggregateOp::Group),
106 "max" => Ok(AggregateOp::Max),
107 "min" => Ok(AggregateOp::Min),
108 "stddev" => Ok(AggregateOp::Stddev),
109 "stdvar" => Ok(AggregateOp::Stdvar),
110 "quantile" => Ok(AggregateOp::Quantile),
111 "sum" => Ok(AggregateOp::Sum),
112 "topk" => Ok(AggregateOp::Topk),
113 _ => Err(format!("Unknown aggregate operator: {}", s)),
114 }
115 }
116}
117
118#[derive(Debug, PartialEq)]
121pub enum AggregateModifier {
122 None,
123 By(Vec<String>),
125 Without(Vec<String>),
128}
129
130impl Default for AggregateModifier {
131 fn default() -> Self {
132 AggregateModifier::None
133 }
134}
135
136impl AggregateModifier {
137 pub fn from_str_and_vec(s: &str, v: Vec<String>) -> Result<Self, String> {
138 match s {
139 "by" => Ok(AggregateModifier::By(v)),
140 "without" => Ok(AggregateModifier::Without(v)),
141 _ => Err(format!("Unknown aggregate modifier: {}", s)),
142 }
143 }
144}
145
146#[derive(Debug)]
148pub struct AggregateExpr {
149 pub op: AggregateOp,
150 pub expr: Box<Expr>,
151 pub param: Option<Box<Expr>>,
154 pub modifier: AggregateModifier,
155}
156
157#[derive(Debug, PartialEq)]
159pub enum BinaryOp {
160 Add,
162 Sub,
164 Mul,
166 Div,
168 Mod,
170 Pow,
172 Eq,
174 Ne,
176 Gt,
178 Lt,
180 Ge,
182 Le,
184 And,
186 Or,
188 Unless,
190 Atan2,
192}
193
194impl FromStr for BinaryOp {
195 type Err = String;
196
197 fn from_str(s: &str) -> Result<Self, Self::Err> {
198 match s {
199 "+" => Ok(BinaryOp::Add),
200 "-" => Ok(BinaryOp::Sub),
201 "*" => Ok(BinaryOp::Mul),
202 "/" => Ok(BinaryOp::Div),
203 "%" => Ok(BinaryOp::Mod),
204 "^" => Ok(BinaryOp::Pow),
205 "==" => Ok(BinaryOp::Eq),
206 "!=" => Ok(BinaryOp::Ne),
207 ">" => Ok(BinaryOp::Gt),
208 "<" => Ok(BinaryOp::Lt),
209 ">=" => Ok(BinaryOp::Ge),
210 "<=" => Ok(BinaryOp::Le),
211 "and" => Ok(BinaryOp::And),
212 "or" => Ok(BinaryOp::Or),
213 "unless" => Ok(BinaryOp::Unless),
214 "atan2" => Ok(BinaryOp::Atan2),
215 _ => Err(format!("Unknown binary operator: {}", s)),
216 }
217 }
218}
219
220#[derive(Debug, PartialEq)]
223pub enum VectorMatchCardinality {
224 OneToOne,
225 OneToMany,
226 ManyToOne,
227 ManyToMany,
228}
229
230impl Default for VectorMatchCardinality {
231 fn default() -> Self {
232 VectorMatchCardinality::OneToOne
233 }
234}
235
236impl VectorMatchCardinality {
237 pub fn from_str(s: &str) -> Result<Self, String> {
238 match s {
239 "group_right" => Ok(VectorMatchCardinality::OneToMany),
240 "group_left" => Ok(VectorMatchCardinality::ManyToOne),
241 _ => Err(format!("Unknown vector match cardinality: {}", s)),
242 }
243 }
244}
245
246#[derive(Debug, PartialEq)]
248pub enum VectorMatchGrouping {
249 None,
250 On(Vec<String>),
251 Ignoring(Vec<String>),
252}
253
254impl Default for VectorMatchGrouping {
255 fn default() -> Self {
256 VectorMatchGrouping::None
257 }
258}
259
260impl VectorMatchGrouping {
261 pub fn from_str_and_vec(s: &str, v: Vec<String>) -> Result<Self, String> {
262 match s {
263 "on" => Ok(VectorMatchGrouping::On(v)),
264 "ignoring" => Ok(VectorMatchGrouping::Ignoring(v)),
265 _ => Err(format!("Unknown vector match grouping: {}", s)),
266 }
267 }
268}
269
270#[derive(Debug, PartialEq)]
273pub struct VectorMatching {
274 pub cardinality: VectorMatchCardinality,
275 pub grouping: VectorMatchGrouping,
276 pub include: Vec<String>,
277}
278
279impl Default for VectorMatching {
280 fn default() -> Self {
281 VectorMatching {
282 cardinality: VectorMatchCardinality::default(),
283 grouping: VectorMatchGrouping::default(),
284 include: Vec::new(),
285 }
286 }
287}
288
289#[derive(Debug)]
291pub struct BinaryExpr {
292 pub op: BinaryOp,
293 pub lhs: Box<Expr>,
294 pub rhs: Box<Expr>,
295 pub return_bool: bool,
297 pub vector_matching: VectorMatching,
299}
300
301#[derive(Debug)]
303pub struct FunctionCall {
304 pub func: &'static Function,
305 pub args: Vec<Expr>,
306}
307
308#[derive(Debug)]
310pub struct MatrixSelector {
311 pub vector_selector: Box<Expr>,
312 pub range: Duration,
313}
314
315#[derive(Debug, PartialEq)]
319pub enum AtModifier {
320 None,
321 Start,
322 End,
323 Timestamp(u64),
324}
325
326#[derive(Debug)]
328pub struct SubqueryExpr {
329 pub expr: Box<Expr>,
330 pub range: Duration,
331 pub step: Duration,
332 pub offset: Duration,
333 pub at: AtModifier,
334}
335
336#[derive(Debug)]
339pub struct NumberLiteral {
340 pub value: f64,
341}
342
343#[derive(Debug)]
345pub struct ParenExpr {
346 pub expr: Box<Expr>,
347}
348
349#[derive(Debug)]
355pub struct StringLiteral {
356 pub value: String,
357}
358
359#[derive(Debug, PartialEq)]
361pub enum UnaryOp {
362 Pos,
363 Neg,
364}
365
366impl FromStr for UnaryOp {
367 type Err = String;
368
369 fn from_str(s: &str) -> Result<Self, Self::Err> {
370 match s {
371 "+" => Ok(UnaryOp::Pos),
372 "-" => Ok(UnaryOp::Neg),
373 _ => Err(format!("Unknown unary operator: {}", s)),
374 }
375 }
376}
377
378#[derive(Debug)]
380pub struct UnaryExpr {
381 pub op: UnaryOp,
382 pub rhs: Box<Expr>,
383}
384
385#[derive(Debug, PartialEq)]
388pub enum MatchOp {
389 Equal,
390 NotEqual,
391 Regex,
392 NNotRegex,
393}
394
395impl FromStr for MatchOp {
396 type Err = String;
397
398 fn from_str(s: &str) -> Result<Self, Self::Err> {
399 match s {
400 "=" => Ok(MatchOp::Equal),
401 "!=" => Ok(MatchOp::NotEqual),
402 "=~" => Ok(MatchOp::Regex),
403 "!~" => Ok(MatchOp::NNotRegex),
404 _ => Err(format!("Unknown match operator: {}", s)),
405 }
406 }
407}
408
409#[derive(Debug, PartialEq)]
411pub struct LabelMatcher {
412 pub op: MatchOp,
413 pub name: String,
414 pub value: String,
415}
416
417#[derive(Debug)]
419pub struct VectorSelector {
420 pub metric: String,
421 pub label_matchers: Vec<LabelMatcher>,
422 pub original_offset: Duration,
423 pub offset: Duration,
424 pub at: AtModifier,
425}