1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// (C) Copyright 2019-2020 Hewlett Packard Enterprise Development LP

use std::fmt;

use super::expression::{BExpression, Expression};
use super::return_value::{LabelSetOp, ReturnKind, ReturnValue};
use super::misc::Span;

/// All legal binary operator types
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum OperatorKind {
  Power,
  Multiply, Divide, Modulo,
  Add, Subtract,
  Equal, NotEqual,
  LessThan, LessThanEqual,
  GreaterThan, GreaterThanEqual,
  And, Unless, Or
}

impl OperatorKind {
  pub fn as_str(self) -> &'static str {
    use OperatorKind::*;
    match self {
      Power => "^",
      Multiply => "*",
      Divide => "/",
      Modulo => "%",
      Add => "+",
      Subtract => "-",
      Equal => "==",
      NotEqual => "!=",
      LessThan => "<",
      LessThanEqual => "<=",
      GreaterThan => ">",
      GreaterThanEqual => ">=",
      And => "and",
      Unless => "unless",
      Or => "or"
    }
  }
}

impl fmt::Display for OperatorKind {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{}", self.as_str())
  }
}

/// Matching group clause types (`group_left`, `group_right`)
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum MatchingGroupOp {
  Left,
  Right
}

impl fmt::Display for MatchingGroupOp {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      MatchingGroupOp::Left => write!(f, "group_left"),
      MatchingGroupOp::Right => write!(f, "group_right")
    }
  }
}

/// A matching clause's nested grouping clause
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct MatchingGroup {
  /// The matching group's operator type (left or right)
  pub op: MatchingGroupOp,

  /// A list of labels to copy to the opposite side of the group operator, i.e.
  /// group_left(foo) copies the label `foo` from the right hand side
  pub labels: Vec<String>,

  pub span: Option<Span>
}

impl MatchingGroup {
  pub fn new(op: MatchingGroupOp) -> Self {
    MatchingGroup {
      op,
      labels: vec![],
      span: None
    }
  }

  /// Creates a new MatchingGroup with the Left op
  pub fn left() -> Self {
    MatchingGroup::new(MatchingGroupOp::Left)
  }

  /// Creates a new MatchingGroup with the Right op
  pub fn right() -> Self {
    MatchingGroup::new(MatchingGroupOp::Right)
  }

  /// Replaces this Matching's operator
  pub fn op(mut self, op: MatchingGroupOp) -> Self {
    self.op = op;
    self
  }

  /// Adds a label key to this MatchingGroup
  pub fn label<S: Into<String>>(mut self, label: S) -> Self {
    self.labels.push(label.into());
    self
  }

  /// Replaces this MatchingGroup's labels with the given set
  pub fn labels(mut self, labels: &[&str]) -> Self {
    self.labels = labels.iter().map(|l| (*l).to_string()).collect();
    self
  }

  /// Clears this MatchingGroup's set of labels
  pub fn clear_labels(mut self) -> Self {
    self.labels.clear();
    self
  }

  pub fn span<S: Into<Span>>(mut self, span: S) -> Self {
    self.span = Some(span.into());
    self
  }
}

impl fmt::Display for MatchingGroup {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{}", self.op)?;

    if !self.labels.is_empty() {
      write!(f, "(")?;

      for (i, label) in self.labels.iter().enumerate() {
        if i > 0 {
          write!(f, ", ")?;
        }

        write!(f, "{}", label)?;
      }

      write!(f, ")")?;
    }

    Ok(())
  }
}

/// A matching clause type
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum MatchingOp {
  On,
  Ignoring
}

impl fmt::Display for MatchingOp {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    match self {
      MatchingOp::On => write!(f, "on"),
      MatchingOp::Ignoring => write!(f, "ignoring")
    }
  }
}

/// An operator matching clause
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Matching {
  pub op: MatchingOp,

  /// A list of labels to which the operator is applied
  pub labels: Vec<String>,

  /// An optional grouping clause for many-to-one and one-to-many vector matches
  pub group: Option<MatchingGroup>,

  pub span: Option<Span>
}

impl Matching {
  pub fn new(op: MatchingOp) -> Self {
    Matching {
      op,
      labels: vec![],
      group: None,
      span: None
    }
  }

  /// Creates a Matching cause with the On operator
  pub fn on() -> Self {
    Matching::new(MatchingOp::On)
  }

  /// Creates a Matching clause using the Ignoring operator
  pub fn ignoring() -> Self {
    Matching::new(MatchingOp::Ignoring)
  }

  /// Replaces this Matching's operator
  pub fn op(mut self, op: MatchingOp) -> Self {
    self.op = op;
    self
  }

  /// Adds a label key to this Matching
  pub fn label<S: Into<String>>(mut self, label: S) -> Self {
    self.labels.push(label.into());
    self
  }

  /// Replaces this Matching's labels with the given set
  pub fn labels(mut self, labels: &[&str]) -> Self {
    self.labels = labels.iter().map(|l| (*l).to_string()).collect();
    self
  }

  /// Clears this Matching's set of labels
  pub fn clear_labels(mut self) -> Self {
    self.labels.clear();
    self
  }

  /// Sets or replaces this Matching's group clause
  pub fn group(mut self, group: MatchingGroup) -> Self {
    self.group = Some(group);
    self
  }

  /// Clears this Matching's group clause
  pub fn clear_group(mut self) -> Self {
    self.group = None;
    self
  }

  pub fn span<S: Into<Span>>(mut self, span: S) -> Self {
    self.span = Some(span.into());
    self
  }
}

impl fmt::Display for Matching {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{}(", self.op)?;

    for (i, label) in self.labels.iter().enumerate() {
      if i > 0 {
        write!(f, ", ")?;
      }

      write!(f, "{}", label)?;
    }

    write!(f, ")")?;

    if let Some(group) = &self.group {
      write!(f, " {}", group)?;
    }

    Ok(())
  }
}

/// A binary operator expression with left- and right-hand sides.
///
/// Operator expressions may optionally have a matching clause for more specific
/// vector matching behavior.
///
/// Note that operator precedence is accounted for at parse-time, so the
/// resulting tree (i.e. lhs/rhs expressions) should already account for
/// un-grouped expressions at the same syntax level.
#[derive(Debug, PartialEq, Clone)]
pub struct Operator {
  /// This Operator's function (multiply, divide, power, equals, etc)
  pub kind: OperatorKind,

  /// The left-hand-side expression
  pub lhs: BExpression,

  /// The right-hand-side expression
  pub rhs: BExpression,

  /// An optional matching clause for this operator (`on(...)`, `ignoring(...)`)
  pub matching: Option<Matching>,

  pub span: Option<Span>
}

impl Operator {
  pub fn new(kind: OperatorKind, lhs: Expression, rhs: Expression) -> Self {
    Operator {
      kind,
      lhs: Box::new(lhs),
      rhs: Box::new(rhs),
      matching: None,
      span: None
    }
  }

  /// Sets or replaces this Operator's Matching clause
  pub fn matching(mut self, matching: Matching) -> Self {
    self.matching = Some(matching);
    self
  }

  /// Clears this Operator's Matching clause, if any
  pub fn clear_matching(mut self) -> Self {
    self.matching = None;
    self
  }

  pub fn span<S: Into<Span>>(mut self, span: S) -> Self {
    self.span = Some(span.into());
    self
  }

  /// Wraps this Operator in an Expression
  pub fn wrap(self) -> Expression {
    Expression::Operator(self)
  }

  pub fn return_value(&self) -> ReturnValue {
    // note: largely based on the description from:
    // https://www.robustperception.io/using-group_left-to-calculate-label-proportions

    // binary operator exprs can only contain (and return) instant vectors
    let lhs_ret = self.lhs.return_value();
    let rhs_ret = self.rhs.return_value();

    // operators can only have instant vectors or scalars
    if !lhs_ret.kind.is_operator_valid() {
      return ReturnValue::unknown(
        format!("lhs return type ({:?}) is not valid in an operator", &lhs_ret.kind),
        self.clone().wrap()
      );
    }

    if !rhs_ret.kind.is_operator_valid() {
      return ReturnValue::unknown(
        format!("rhs return type ({:?}) is not valid in an operator", &rhs_ret.kind),
        self.clone().wrap()
      );
    }

    let kind;
    let mut label_ops;

    if lhs_ret.kind.is_scalar() && rhs_ret.kind.is_scalar() {
      // scalar / scalar = scalar, otherwise instant vector
      kind = ReturnKind::Scalar;
      label_ops = vec![LabelSetOp::clear(self.clone().wrap(), self.span)];
    } else if lhs_ret.kind.is_scalar() {
      // lhs is scalar, so pull labels from the rhs
      kind = ReturnKind::InstantVector;
      label_ops = rhs_ret.label_ops;
    } else if rhs_ret.kind.is_scalar() {
      // rhs is scalar, so pull labels from the lhs
      kind = ReturnKind::InstantVector;
      label_ops = lhs_ret.label_ops;
    } else {
      kind = ReturnKind::InstantVector;

      // neither side is scalar, so unless there's a matching clause with a
      // group_*, the choice is arbitrary
      // i.e. expressions without matching label sets will just return an empty
      // set of metrics
      
      // on/ignoring clauses don't affect labels themselves, but a group_* may
      if let Some(matching) = &self.matching {
        if let Some(group) = &matching.group {
          match &group.op {
            MatchingGroupOp::Left => label_ops = lhs_ret.label_ops,
            MatchingGroupOp::Right => label_ops = rhs_ret.label_ops
          };

          // any explicitly-specified labels are copied from the opposite side
          // we don't care about the value, but it does imply that the label
          // will exist in the output
          label_ops.push(LabelSetOp::append(
            self.clone().wrap(),
            group.span,
            group.labels.iter().cloned().collect(),
          ));
        } else {
          label_ops = lhs_ret.label_ops;
        }
      } else {
        label_ops = lhs_ret.label_ops;
      }
    };

    ReturnValue { kind, label_ops }
  }
}

impl fmt::Display for Operator {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "{} {}", self.lhs, self.kind)?;

    if let Some(matching) = &self.matching {
      write!(f, " {}", matching)?;
    }

    write!(f, " {}", self.rhs)?;

    Ok(())
  }
}