tremor-script 0.13.0-rc.11

Tremor Script Interpreter
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
409
410
411
412
// Copyright 2020-2021, The Tremor Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![allow(clippy::unwrap_used)]
use super::*;
use crate::{registry, CustomFn, NodeMeta};
use matches::assert_matches;
use tremor_value::Object;

fn v(s: &'static str) -> ImutExpr<'static> {
    ImutExpr::literal(NodeMeta::dummy(), Value::from(s))
}

fn p() -> ImutExpr<'static> {
    ImutExpr::Path(Path::State(StatePath {
        mid: NodeMeta::dummy(),
        segments: vec![],
    }))
}

#[test]
fn record() {
    let f1 = super::Field {
        mid: NodeMeta::dummy(),
        name: "snot".into(),
        value: v("badger"),
    };
    let f2 = super::Field {
        mid: NodeMeta::dummy(),
        name: "badger".into(),
        value: v("snot"),
    };

    let r = super::Record {
        base: crate::Object::new(),
        mid: NodeMeta::dummy(),
        fields: vec![f1, f2],
    };

    assert_eq!(r.cloned_field_expr("snot"), Some(v("badger")));
    assert_eq!(r.cloned_field_expr("nots"), None);

    let lit = r.cloned_field_literal("badger");
    assert_eq!(lit.as_str(), Some("snot"));
    assert_eq!(r.cloned_field_expr("adgerb"), None);
}

#[test]
fn as_record() {
    let i = v("snot");
    assert!(i.as_record().is_none());
    let i = ImutExpr::Record(Record {
        mid: Box::new(i.meta().clone()),
        base: Object::new(),
        fields: Fields::new(),
    });
    assert!(i.as_record().is_some());
}

#[test]
fn as_list() {
    let i = v("snot");
    assert!(i.as_list().is_none());
    let i = ImutExpr::List(List {
        mid: Box::new(i.meta().clone()),
        exprs: vec![],
    });
    assert!(i.as_list().is_some());
}
#[test]
fn as_invoke() {
    let invocable = Invocable::Tremor(CustomFn {
        name: "f".into(),
        body: Vec::new(),
        args: Vec::new(),
        open: false,
        locals: 0,
        is_const: false,
        inline: false,
    });
    assert_eq!("f", invocable.name());
    let invocable2 = Invocable::Intrinsic(
        registry()
            .find("array", "reverse")
            .expect("Expected array::reverse to exist")
            .clone(),
    );
    assert_eq!("reverse", invocable2.name());
    let i = Invoke {
        mid: NodeMeta::dummy(),
        node_id: NodeId {
            module: Vec::new(),
            id: "fun".to_string(),
            mid: NodeMeta::dummy(),
        },
        invocable,
        args: Vec::new(),
    };
    assert!(Expr::Imut(v("snut")).as_invoke().is_none());
    let e = ImutExpr::Invoke(i.clone());
    assert!(Expr::Imut(e).as_invoke().is_some());
    let e = ImutExpr::Invoke1(i.clone());
    assert!(Expr::Imut(e).as_invoke().is_some());
    let e = ImutExpr::Invoke2(i.clone());
    assert!(Expr::Imut(e).as_invoke().is_some());
    let e = ImutExpr::Invoke3(i.clone());
    assert!(Expr::Imut(e).as_invoke().is_some());

    let i2 = Invoke {
        mid: NodeMeta::dummy(),
        node_id: NodeId {
            module: Vec::new(),
            id: "fun".to_string(),
            mid: NodeMeta::dummy(),
        },
        invocable: invocable2,
        args: Vec::new(),
    };
    assert!(Expr::Imut(v("snut")).as_invoke().is_none());
    let e = ImutExpr::Invoke(i2.clone());
    assert!(Expr::Imut(e).as_invoke().is_some());
    let e = ImutExpr::Invoke1(i2.clone());
    assert!(Expr::Imut(e).as_invoke().is_some());
    let e = ImutExpr::Invoke2(i2.clone());
    assert!(Expr::Imut(e).as_invoke().is_some());
    let e = ImutExpr::Invoke3(i2.clone());
    assert!(Expr::Imut(e).as_invoke().is_some());
}

#[test]
fn as_imut() {
    let i = ImutExpr::List(List {
        mid: NodeMeta::dummy(),
        exprs: vec![],
    });
    assert!(Expr::Imut(i).as_imut().is_ok());
    assert!(Expr::Drop {
        mid: NodeMeta::dummy()
    }
    .as_imut()
    .is_err());
}

#[test]
fn try_into_value() -> crate::errors::Result<()> {
    let aggr_reg = crate::registry::aggr();
    let reg = &*crate::FN_REGISTRY.read()?;
    let helper = Helper::new(reg, &aggr_reg);

    let val = v("value");
    assert!(val.try_into_value(&helper).is_ok());

    let list = ImutExpr::List(List {
        mid: NodeMeta::dummy(),
        exprs: vec![],
    });
    assert!(list.try_into_value(&helper).is_ok());

    let path = p();
    assert!(path.try_into_value(&helper).is_err());

    Ok(())
}
#[test]
fn str_lit_as_str() {
    assert_eq!(StrLitElement::Lit("string".into()).as_str(), Some("string"));
    assert_eq!(StrLitElement::Expr(v("string")).as_str(), Some("string"));
    assert_eq!(StrLitElement::Expr(p()).as_str(), None);
}

#[test]
fn replace_last_shadow_use() {
    let path = Path::Event(EventPath {
        mid: NodeMeta::dummy(),
        segments: vec![],
    });
    let expr = Expr::Assign {
        mid: NodeMeta::dummy(),
        path,
        expr: Box::new(Expr::Imut(ImutExpr::Local {
            idx: 42,
            mid: NodeMeta::dummy(),
        })),
    };
    let pc = PredicateClause {
        mid: NodeMeta::dummy(),
        pattern: Pattern::DoNotCare,
        guard: None,
        exprs: vec![],
        last_expr: expr.clone(),
    };

    // Simple
    let mut g = ClauseGroup::Simple {
        precondition: None,
        patterns: vec![pc.clone()],
    };
    g.replace_last_shadow_use(42);
    if let ClauseGroup::Simple { patterns, .. } = g {
        assert_matches!(patterns[0].last_expr, Expr::AssignMoveLocal { idx: 42, .. });
    } else {
        unreachable!()
    };

    // Search Tree
    let mut tree = BTreeMap::new();
    tree.insert(Value::from(42u32), (vec![], expr.clone()));
    let mut g = ClauseGroup::SearchTree {
        precondition: None,
        tree,
        rest: vec![pc.clone()],
    };
    g.replace_last_shadow_use(42);
    if let ClauseGroup::SearchTree { rest, tree, .. } = g {
        let last = &tree.values().next().unwrap().1;
        assert_matches!(last, Expr::AssignMoveLocal { idx: 42, .. });
        assert_matches!(rest[0].last_expr, Expr::AssignMoveLocal { idx: 42, .. });
    } else {
        unreachable!()
    };

    // Combined
    let g1 = ClauseGroup::Simple {
        precondition: None,
        patterns: vec![pc.clone()],
    };
    let g2 = ClauseGroup::Simple {
        precondition: None,
        patterns: vec![pc.clone()],
    };
    let mut g = ClauseGroup::Combined {
        precondition: None,
        groups: vec![g1, g2],
    };
    g.replace_last_shadow_use(42);
    if let ClauseGroup::Combined { groups, .. } = g {
        if let ClauseGroup::Simple { patterns, .. } = &groups[0] {
            assert_matches!(patterns[0].last_expr, Expr::AssignMoveLocal { idx: 42, .. });
        } else {
            unreachable!()
        };
        if let ClauseGroup::Simple { patterns, .. } = &groups[1] {
            assert_matches!(patterns[0].last_expr, Expr::AssignMoveLocal { idx: 42, .. });
        } else {
            unreachable!()
        };
    } else {
        unreachable!()
    }

    // Single
    let mut g = ClauseGroup::Single {
        pattern: pc.clone(),
        precondition: None,
    };
    g.replace_last_shadow_use(42);
    if let ClauseGroup::Single { pattern, .. } = g {
        assert_matches!(pattern.last_expr, Expr::AssignMoveLocal { idx: 42, .. });
    } else {
        unreachable!()
    };
}

#[test]
#[allow(clippy::too_many_lines)]
fn pp_is_exclusive() {
    let eq = PredicatePattern::Bin {
        lhs: "k1".into(),
        key: KnownKey::from("k1"),
        rhs: ImutExpr::literal(NodeMeta::dummy(), Value::from("cake")),
        kind: BinOpKind::Eq,
    };
    let eq2 = PredicatePattern::Bin {
        lhs: "k2".into(),
        key: KnownKey::from("k2"),
        rhs: ImutExpr::literal(NodeMeta::dummy(), Value::from("cake")),
        kind: BinOpKind::Eq,
    };
    assert!(!eq.is_exclusive_to(&eq), "is not exlusive to itself");
    assert!(
        !eq.is_exclusive_to(&eq2),
        "is not exclusive to a different key"
    );
    assert!(
        eq.is_exclusive_to(&PredicatePattern::Bin {
            lhs: "k1".into(),
            key: KnownKey::from("k1"),
            rhs: ImutExpr::literal(NodeMeta::dummy(), Value::from("cookie")),
            kind: BinOpKind::Eq,
        }),
        "is exclusive to the same key and a different value"
    );

    let abs = PredicatePattern::FieldAbsent {
        lhs: "k1".into(),
        key: KnownKey::from("k1"),
    };
    let abs2 = PredicatePattern::FieldAbsent {
        lhs: "k2".into(),
        key: KnownKey::from("k2"),
    };
    assert!(
        !abs.is_exclusive_to(&abs2),
        "absent is not exclusive on different fields"
    );
    assert!(eq.is_exclusive_to(&abs), "eq is exclusive to absent");
    assert!(abs.is_exclusive_to(&eq), "absent is exclusive to eq");
    assert!(
        !eq.is_exclusive_to(&abs2),
        "eq is not exclusive to absend on a different key"
    );
    assert!(
        !abs2.is_exclusive_to(&eq),
        "absent is not exclusive to eq on a different key"
    );

    let pres = PredicatePattern::FieldPresent {
        lhs: "k1".into(),
        key: KnownKey::from("k1"),
    };
    let pres2 = PredicatePattern::FieldPresent {
        lhs: "k2".into(),
        key: KnownKey::from("k2"),
    };
    assert!(
        !pres.is_exclusive_to(&pres2),
        "present is not exclusive on different fields"
    );
    assert!(
        pres.is_exclusive_to(&abs),
        "present and absent are exclusive"
    );
    assert!(
        abs.is_exclusive_to(&pres),
        "present and absent are exclusive"
    );
    assert!(
        !pres.is_exclusive_to(&eq),
        "present and eq are not exclusive"
    );
    assert!(
        !pres.is_exclusive_to(&eq2),
        "present and eq are not exclusive on different keys"
    );
    let teq = PredicatePattern::TildeEq {
        lhs: "k1".into(),
        assign: "k".into(),
        key: "k1".into(),
        test: Box::new(TestExpr {
            mid: NodeMeta::dummy(),
            id: "suffix".into(),
            test: "ake".into(),
            extractor: Extractor::Suffix("ake".into()),
        }),
    };
    let test_eq2 = PredicatePattern::TildeEq {
        lhs: "k2".into(),
        assign: "k".into(),
        key: "k2".into(),
        test: Box::new(TestExpr {
            mid: NodeMeta::dummy(),
            id: "suffix".into(),
            test: "ookie".into(),
            extractor: Extractor::Suffix("ake".into()),
        }),
    };
    let teq3 = PredicatePattern::TildeEq {
        lhs: "k1".into(),
        assign: "k".into(),
        key: "k1".into(),
        test: Box::new(TestExpr {
            mid: NodeMeta::dummy(),
            id: "suffix".into(),
            test: "ookie".into(),
            extractor: Extractor::Suffix("ookie".into()),
        }),
    };
    assert!(
        !teq.is_exclusive_to(&test_eq2),
        "~= is not exclusive on different keys"
    );
    assert!(
        !test_eq2.is_exclusive_to(&teq),
        "~= is not exclusive on different keys"
    );
    assert!(
        teq.is_exclusive_to(&teq3),
        "is not exclusive on if the tests are exclusive"
    );
    assert!(
        teq3.is_exclusive_to(&teq),
        "is not exclusive on if the tests are exclusive"
    );
    assert!(
        !teq.is_exclusive_to(&eq),
        "~= is not exclusive to eq if the patterns are not exclusive"
    );
    assert!(
        !eq.is_exclusive_to(&teq),
        "~= is not exclusive to eq if the patterns are not exclusive"
    );
}