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
use super::{Expression, ExpressionKind, LitKind, Offset, TimeUnit};
use crate::ast::Literal;
use num::rational::Rational64 as Rational;
use num::traits::{CheckedMul, Inv, Pow};
use num::{BigInt, BigRational, FromPrimitive, One, Signed, ToPrimitive};
use std::num::ParseIntError;
use std::str::FromStr;
use uom::si::frequency::hertz;
use uom::si::rational64::Frequency as UOM_Frequency;
use uom::si::rational64::Time as UOM_Time;
use uom::si::time::second;

type RationalType = i64;

impl Expression {
    pub(crate) fn parse_offset(&self) -> Result<Offset, String> {
        if let Some(val) = self.parse_literal::<i16>() {
            Ok(Offset::Discrete(val))
        } else {
            // has to be a real-time expression
            let (val, unit) = match &self.kind {
                ExpressionKind::Lit(l) => match &l.kind {
                    LitKind::Numeric(val, Some(unit)) => (val, unit),
                    _ => return Err(format!("expected numeric value with unit, found `{}`", l)),
                },
                _ => return Err(format!("expected numeric value with unit, found `{}`", self)),
            };
            Ok(Offset::RealTime(parse_rational(val)?, TimeUnit::from_str(unit)?))
        }
    }

    pub(crate) fn parse_discrete_duration(&self) -> Result<u64, String> {
        match &self.kind {
            ExpressionKind::Lit(l) => match &l.kind {
                LitKind::Numeric(val, None) => val.parse().map_err(|err: ParseIntError| err.to_string()),
                _ => Err(format!("expected numeric value without unit, found `{}`", l)),
            },
            _ => Err(format!("expected numeric value without unit, found `{}`", self)),
        }
    }

    pub(crate) fn parse_duration(&self) -> Result<UOM_Time, String> {
        let (val, unit) = match &self.kind {
            ExpressionKind::Lit(l) => match &l.kind {
                LitKind::Numeric(val, Some(unit)) => (parse_rational(val)?, unit),
                _ => return Err(format!("expected numeric value with unit, found `{}`", l)),
            },
            _ => return Err(format!("expected numeric value with unit, found `{}`", self)),
        };

        match unit.as_str() {
            "ns" | "μs" | "us" | "ms" | "s" | "min" | "h" | "d" | "w" | "a" => {
                use uom::si::time::*;
                let factor = match unit.as_str() {
                    "ns" => UOM_Time::new::<nanosecond>(Rational::one()),
                    "μs" | "us" => UOM_Time::new::<microsecond>(Rational::one()),
                    "ms" => UOM_Time::new::<millisecond>(Rational::one()),
                    "s" => UOM_Time::new::<second>(Rational::one()),
                    "min" => UOM_Time::new::<minute>(Rational::one()),
                    "h" => UOM_Time::new::<hour>(Rational::one()),
                    "d" => UOM_Time::new::<day>(Rational::one()),
                    "w" => UOM_Time::new::<day>(Rational::from_u64(7).unwrap()),
                    "a" => UOM_Time::new::<day>(Rational::from_u64(365).unwrap()),
                    u => unreachable!("'{}' should not have been catched by outer match", u),
                };
                let factor = factor.get::<second>();
                let duration = match val.checked_mul(&factor) {
                    Some(d) => d,
                    _ => {
                        return Err(format!(
                            "parsing duration failed: rational {}*{} does not fit into Rational64",
                            val, factor
                        ))
                    }
                };
                Ok(UOM_Time::new::<second>(duration))
            }
            u => Err(format!("expected duration unit, found `{}`", u)),
        }
    }

    pub(crate) fn parse_frequency(&self) -> Result<UOM_Frequency, String> {
        let (val, unit) = match &self.kind {
            ExpressionKind::Lit(l) => match &l.kind {
                LitKind::Numeric(val, Some(unit)) => (parse_rational(val)?, unit),
                _ => return Err(format!("expected numeric value with unit, found `{}`", l)),
            },
            _ => return Err(format!("expected numeric value with unit, found `{}`", self)),
        };

        if !val.is_positive() {
            return Err("frequencies have to be positive".to_string());
        }

        assert!(val.is_positive());

        match unit.as_str() {
            "μHz" | "uHz" | "mHz" | "Hz" | "kHz" | "MHz" | "GHz" => {
                use uom::si::frequency::*;
                let factor = match unit.as_str() {
                    "μHz" | "uHz" => UOM_Frequency::new::<microhertz>(Rational::one()),
                    "mHz" => UOM_Frequency::new::<millihertz>(Rational::one()),
                    "Hz" => UOM_Frequency::new::<hertz>(Rational::one()),
                    "kHz" => UOM_Frequency::new::<kilohertz>(Rational::one()),
                    "MHz" => UOM_Frequency::new::<megahertz>(Rational::one()),
                    "GHz" => UOM_Frequency::new::<gigahertz>(Rational::one()),
                    u => unreachable!("'{}' should not have been catched by outer match", u),
                };
                let factor = factor.get::<hertz>();
                let freq = match val.checked_mul(&factor) {
                    Some(f) => f,
                    _ => {
                        return Err(format!(
                            "parsing frequency failed: rational {}*{} does not fit into Rational64",
                            val, factor
                        ))
                    }
                };
                Ok(UOM_Frequency::new::<hertz>(freq))
            }
            u => Err(format!("expected frequency unit, found `{}`", u)),
        }
    }

    pub fn parse_freqspec(&self) -> Result<UOM_Frequency, String> {
        if let Ok(freq) = self.parse_frequency() {
            Ok(freq)
        } else if let Ok(period) = self.parse_duration() {
            let seconds = period.get::<second>();
            if seconds.is_positive() {
                Ok(UOM_Frequency::new::<hertz>(seconds.inv()))
            } else {
                Err(format!("duration of periodic stream specification must be positiv, found `{:#?}`", period))
            }
        } else {
            Err(format!("expected frequency or duration, found `{}`", self))
        }
    }
}

fn parse_rational(repr: &str) -> Result<Rational, String> {
    // precondition: repr is a valid floating point literal
    debug_assert!(repr.parse::<f64>().is_ok());

    macro_rules! split_at {
        ($s:expr, $c:literal) => {{
            let (prefix, suffix) = $s.split_at($s.find($c).unwrap_or($s.len()));
            let suffix = if suffix.len() > 0 { &suffix[1..] } else { suffix };
            (prefix, suffix)
        }};
    }

    let (int_digits, suffix) = split_at!(repr, '.'); // actually sign + int_digits
    let (dec_digits, exp_str) = split_at!(suffix, 'e');

    let digits = int_digits.to_string() + dec_digits; // actually sign + digits
    let integer = match BigInt::from_str(digits.as_str()) {
        Ok(i) => i,
        Err(e) => return Err(format!("parsing rational '{}' failed: {}", repr, e)),
    };
    let mut r = BigRational::from(integer);
    if !dec_digits.is_empty() {
        // divide by 10 for each decimal place
        r /= BigInt::from_u8(10).unwrap().pow(dec_digits.len());
    }

    if !exp_str.is_empty() {
        let exp = match BigInt::from_str(exp_str) {
            Ok(i) => i,
            Err(e) => return Err(format!("parsing rational '{}' failed: {}", repr, e)),
        };
        let exp = match exp.to_i16() {
            Some(i) => i,
            None => {
                return Err(format!("parsing rational '{}' failed: e exponent {} does not fit into i16", repr, exp))
            }
        };
        let factor = BigInt::from_u8(10).unwrap().pow(exp.abs() as u16);
        if exp.is_negative() {
            r /= factor;
        } else {
            r *= factor;
        }
    }

    let p = match (r.numer().to_i64(), r.denom().to_i64()) {
        (Some(n), Some(d)) => (n, d),
        _ => return Err(format!("parsing rational failed: rational {} does not fit into Rational64", r)),
    };
    Ok(Rational::from(p))
}

impl Expression {
    /// Attempts to extract the numeric, constant, unit-less value out of an `Expression::Lit`.
    pub(crate) fn parse_literal<T>(&self) -> Option<T>
    where
        T: FromStr,
    {
        match &self.kind {
            ExpressionKind::Lit(l) => l.parse_numeric(),
            _ => None,
        }
    }
}

impl Literal {
    pub(crate) fn parse_numeric<T>(&self) -> Option<T>
    where
        T: FromStr,
    {
        match &self.kind {
            LitKind::Numeric(val, unit) => {
                if unit.is_some() {
                    return None;
                }
                val.parse::<T>().ok()
            }
            _ => None,
        }
    }
}

impl Offset {
    pub(crate) fn to_uom_time(&self) -> Option<UOM_Time> {
        match self {
            Offset::Discrete(_) => None,
            Offset::RealTime(val, unit) => {
                let seconds = val * unit.to_uom_time().get::<second>();
                Some(UOM_Time::new::<second>(seconds))
            }
        }
    }
}

impl FromStr for TimeUnit {
    type Err = String;
    fn from_str(unit: &str) -> Result<Self, Self::Err> {
        match unit {
            "ns" => Ok(TimeUnit::Nanosecond),
            "μs" | "us" => Ok(TimeUnit::Microsecond),
            "ms" => Ok(TimeUnit::Millisecond),
            "s" => Ok(TimeUnit::Second),
            "min" => Ok(TimeUnit::Minute),
            "h" => Ok(TimeUnit::Hour),
            "d" => Ok(TimeUnit::Day),
            "w" => Ok(TimeUnit::Week),
            "a" => Ok(TimeUnit::Year),
            _ => Err(format!("unknown time unit `{}`", unit)),
        }
    }
}

impl TimeUnit {
    fn to_uom_time(self) -> UOM_Time {
        let f = match self {
            TimeUnit::Nanosecond => {
                Rational::new(RationalType::from_u64(1).unwrap(), RationalType::from_u64(10_u64.pow(9)).unwrap())
            }
            TimeUnit::Microsecond => {
                Rational::new(RationalType::from_u64(1).unwrap(), RationalType::from_u64(10_u64.pow(6)).unwrap())
            }
            TimeUnit::Millisecond => {
                Rational::new(RationalType::from_u64(1).unwrap(), RationalType::from_u64(10_u64.pow(3)).unwrap())
            }
            TimeUnit::Second => Rational::from_u64(1).unwrap(),
            TimeUnit::Minute => Rational::from_u64(60).unwrap(),
            TimeUnit::Hour => Rational::from_u64(60 * 60).unwrap(),
            TimeUnit::Day => Rational::from_u64(60 * 60 * 24).unwrap(),
            TimeUnit::Week => Rational::from_u64(60 * 60 * 24 * 7).unwrap(),
            TimeUnit::Year => Rational::from_u64(60 * 60 * 24 * 365).unwrap(),
        };
        UOM_Time::new::<second>(f)
    }
}

impl Expression {
    /// Tries to resolve a tuple index access
    pub(crate) fn get_expr_from_tuple(&self, idx: usize) -> Option<&Expression> {
        use ExpressionKind::*;
        match &self.kind {
            Tuple(entries) => Some(entries[idx].as_ref()),
            _ => None,
        }
    }

    /// A recursive iterator over an `Expression` tree
    /// Inspired by https://amos.me/blog/2019/recursive-iterators-rust/
    pub(crate) fn iter<'a>(&'a self) -> Box<dyn Iterator<Item = &Expression> + 'a> {
        use ExpressionKind::*;
        match &self.kind {
            Lit(_) | Ident(_) | MissingExpression => Box::new(std::iter::once(self)),
            Unary(_, inner)
            | Field(inner, _)
            | StreamAccess(inner, _)
            | Offset(inner, _)
            | ParenthesizedExpression(_, inner, _) => Box::new(std::iter::once(self).chain(inner.iter())),
            Binary(_, left, right)
            | Default(left, right)
            | DiscreteWindowAggregation { expr: left, duration: right, .. }
            | SlidingWindowAggregation { expr: left, duration: right, .. } => {
                Box::new(std::iter::once(self).chain(left.iter()).chain(right.iter()))
            }
            Ite(cond, normal, alternative) => {
                Box::new(std::iter::once(self).chain(cond.iter()).chain(normal.iter()).chain(alternative.iter()))
            }
            Tuple(entries) | Function(_, _, entries) => {
                Box::new(std::iter::once(self).chain(entries.iter().map(|entry| entry.iter()).flatten()))
            }
            Method(base, _, _, arguments) => Box::new(
                std::iter::once(self).chain(base.iter()).chain(arguments.iter().map(|entry| entry.iter()).flatten()),
            ),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::{Literal, Span};
    use crate::parse::NodeId;
    use num::ToPrimitive;
    use std::time::Duration;

    #[test]
    fn test_parse_rational() {
        macro_rules! check_on {
            ($f:expr) => {
                let f_string = format!("{}", $f);
                let f = f_string.parse::<f64>().unwrap();
                let was = parse_rational(f_string.as_str()).unwrap_or_else(|e| panic!("parsing failed: {}", e));
                assert_eq!(was, Rational::from_f64(f).unwrap());
            };
        };
        check_on!(0);
        check_on!(42);
        check_on!(-1);
        check_on!(0.1);
        check_on!(42.12);
        check_on!(-1.123);
        check_on!(0.1e-0);
        check_on!(42.12e+1);
        check_on!(-1.123e-2);
    }

    fn time_spec_int(val: &str, unit: &str) -> Duration {
        let expr = Expression::new(
            NodeId::new(32),
            ExpressionKind::Lit(Literal::new_numeric(NodeId::new(24), val, Some(unit.to_string()), Span::unknown())),
            Span::unknown(),
        );
        let freq = expr.parse_freqspec().unwrap();
        let period = UOM_Time::new::<second>(freq.get::<hertz>().inv());
        Duration::from_nanos(period.get::<uom::si::time::nanosecond>().to_integer().to_u64().unwrap())
    }

    #[test]
    fn test_time_spec_to_duration_conversion() {
        assert_eq!(time_spec_int("1", "s"), Duration::new(1, 0));
        assert_eq!(time_spec_int("2", "min"), Duration::new(2 * 60, 0));
        assert_eq!(time_spec_int("33", "h"), Duration::new(33 * 60 * 60, 0));
        assert_eq!(time_spec_int("12354", "ns"), Duration::from_nanos(12354));
        assert_eq!(time_spec_int("90351", "us"), Duration::from_nanos(90351 * 1_000));
        assert_eq!(time_spec_int("248", "ms"), Duration::from_nanos(248 * 1_000_000));
        assert_eq!(time_spec_int("29489232", "ms"), Duration::from_nanos(29_489_232 * 1_000_000));
    }

    #[test]
    fn test_frequency_to_duration_conversion() {
        assert_eq!(time_spec_int("1", "Hz"), Duration::new(1, 0));
        assert_eq!(time_spec_int("10", "Hz"), Duration::new(0, 100_000_000));
        assert_eq!(time_spec_int("400", "uHz"), Duration::new(2_500, 0));
        assert_eq!(time_spec_int("20", "mHz"), Duration::new(50, 0));
    }
}