tremor-script 0.12.4

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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// 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.

// We want to keep the names here
#![allow(clippy::module_name_repetitions)]

use super::{
    BaseExpr, ConnectStmt, ConnectorDefinition, CreateStmt, CreateTargetDefinition, DeployEndpoint,
    DeployFlow, FlowDefinition, Value,
};
use crate::{
    ast::{
        base_expr::Ranged,
        docs::{FlowDoc, ModDoc},
        error_generic,
        node_id::NodeId,
        query::raw::{
            ConfigRaw, CreationalWithRaw, DefinitionalArgsRaw, DefinitionalArgsWithRaw,
            PipelineDefinitionRaw,
        },
        raw::{IdentRaw, UseRaw},
        visitors::ConstFolder,
        walkers::{ImutExprWalker, QueryWalker},
        Deploy, DeployStmt, Helper, NodeMeta, Script, Upable,
    },
    errors::{Error, Kind as ErrorKind, Result},
    impl_expr,
    module::Manager,
    AggrType, EventContext, Return,
};
use beef::Cow;
use halfbrown::HashMap;
use tremor_common::time::nanotime;
use tremor_value::literal;

/// Evaluate a script expression at compile time with an empty state context
/// for use during compile time reduction
/// # Errors
/// If evaluation of the script fails, or a legal value cannot be evaluated by result
pub fn run_script<'script>(expr: &Script<'script>) -> Result<Value<'script>> {
    // We duplicate these here as it simplifies use of the macro externally
    let ctx = EventContext::new(nanotime(), None);
    let mut event = literal!({}).into_static();
    let mut state = literal!({}).into_static();
    let mut meta = literal!({}).into_static();

    match expr.run(&ctx, AggrType::Emit, &mut event, &mut state, &mut meta) {
        Ok(Return::Emit { value, .. }) => Ok(value),
        _otherwise => error_generic(
            expr,
            expr,
            &"Failed to evaluate script at compile time".to_string(),
        ),
    }
}

#[derive(Debug, PartialEq, Serialize)]
pub struct DeployRaw<'script> {
    pub(crate) config: ConfigRaw<'script>,
    pub(crate) stmts: DeployStmtsRaw<'script>,
    pub(crate) doc: Option<Vec<Cow<'script, str>>>,
}
impl<'script> DeployRaw<'script> {
    pub(crate) fn up_script<'registry>(
        self,
        mut helper: &mut Helper<'script, 'registry>,
    ) -> Result<Deploy<'script>> {
        let mut stmts: Vec<DeployStmt<'script>> = vec![];
        for (_i, stmt) in self.stmts.into_iter().enumerate() {
            if let Some(stmt) = stmt.up(helper)? {
                stmts.push(stmt);
            }
        }

        helper.docs.module = Some(ModDoc {
            name: "self".into(),
            doc: self
                .doc
                .map(|d| d.iter().map(|l| l.trim()).collect::<Vec<_>>().join("\n")),
        });

        let mut config = HashMap::new();
        for (k, mut v) in self.config.up(helper)? {
            ConstFolder::new(helper).walk_expr(&mut v)?;
            config.insert(k.to_string(), v.try_into_value(helper)?);
        }
        Ok(Deploy {
            config,
            stmts,
            scope: helper.scope.clone(),
            docs: helper.docs.clone(),
        })
    }
}

/// we're forced to make this pub because of lalrpop
#[derive(Clone, Debug, PartialEq, Serialize)]
pub enum DeployStmtRaw<'script> {
    /// we're forced to make this pub because of lalrpop
    DeployFlow(DeployFlowRaw<'script>),
    /// we're forced to make this pub because of lalrpop
    FlowDefinition(FlowDefinitionRaw<'script>),
    /// we're forced to make this pub because of lalrpop
    Use(UseRaw),
}

impl<'script> Upable<'script> for DeployStmtRaw<'script> {
    type Target = Option<DeployStmt<'script>>;
    fn up<'registry>(self, helper: &mut Helper<'script, 'registry>) -> Result<Self::Target> {
        match self {
            DeployStmtRaw::Use(UseRaw { alias, module, mid }) => {
                let range = mid.range;
                let module_id = Manager::load(&module).map_err(|err| match err {
                    Error(ErrorKind::ModuleNotFound(_, _, p, exp), state) => Error(
                        ErrorKind::ModuleNotFound(range.expand_lines(2), range, p, exp),
                        state,
                    ),
                    _ => err,
                })?;

                let alias = alias.unwrap_or_else(|| module.id.clone());
                helper.scope().add_module_alias(alias, module_id);
                Ok(None)
            }
            DeployStmtRaw::FlowDefinition(stmt) => {
                helper.docs.flows.push(stmt.doc());
                let stmt: FlowDefinition<'script> = stmt.up(helper)?;
                helper.scope.insert_flow(stmt)?;
                Ok(None)
            }
            DeployStmtRaw::DeployFlow(stmt) => {
                let stmt: DeployFlow = stmt.up(helper)?;
                helper
                    .instances
                    .insert(stmt.instance_alias.clone(), stmt.clone());
                Ok(Some(DeployStmt::DeployFlowStmt(Box::new(stmt))))
            }
        }
    }
}

pub type DeployStmtsRaw<'script> = Vec<DeployStmtRaw<'script>>;

/// we're forced to make this pub because of lalrpop
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct ConnectorDefinitionRaw<'script> {
    pub(crate) id: String,
    pub(crate) kind: IdentRaw<'script>,
    pub(crate) params: DefinitionalArgsWithRaw<'script>,
    pub(crate) docs: Option<Vec<Cow<'script, str>>>,
    pub(crate) mid: Box<NodeMeta>,
}
impl_expr!(ConnectorDefinitionRaw);

impl<'script> Upable<'script> for ConnectorDefinitionRaw<'script> {
    type Target = ConnectorDefinition<'script>;
    fn up<'registry>(self, helper: &mut Helper<'script, 'registry>) -> Result<Self::Target> {
        // verify supported parameters
        for (ident, _) in &self.params.with.exprs {
            let key: &str = ident.id.as_ref();
            if !ConnectorDefinition::AVAILABLE_PARAMS.contains(&key) {
                let range = ident.mid.range;
                return Err(ErrorKind::InvalidDefinitionalWithParam(
                    range.expand_lines(2),
                    range,
                    format!("connector \"{}\"", self.id),
                    ident.id.to_string(),
                    &ConnectorDefinition::AVAILABLE_PARAMS,
                )
                .into());
            }
        }

        let query_defn = ConnectorDefinition {
            config: Value::const_null(),
            mid: self.mid.box_with_name(&self.id),
            params: self.params.up(helper)?,
            builtin_kind: self.kind.to_string(),
            id: self.id,
            docs: self
                .docs
                .map(|d| d.iter().map(|l| l.trim()).collect::<Vec<_>>().join("\n")),
        };

        Ok(query_defn)
    }
}

#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) struct DeployEndpointRaw<'script> {
    pub(crate) alias: IdentRaw<'script>,
    pub(crate) port: IdentRaw<'script>,
    pub(crate) mid: Box<NodeMeta>,
}

impl<'script> Upable<'script> for DeployEndpointRaw<'script> {
    type Target = DeployEndpoint;
    fn up<'registry>(self, _helper: &mut Helper<'script, 'registry>) -> Result<Self::Target> {
        Ok(DeployEndpoint {
            alias: self.alias.to_string(),
            port: self.port.to_string(),
            mid: self.mid,
        })
    }
}

#[derive(Clone, Debug, PartialEq, Serialize)]
/// we're forced to make this pub because of lalrpop
pub(crate) enum ConnectStmtRaw<'script> {
    ConnectorToPipeline {
        /// The instance we're connecting to
        from: DeployEndpointRaw<'script>,
        /// The instance being connected
        to: DeployEndpointRaw<'script>,
        /// The instance we're connecting to
        mid: Box<NodeMeta>,
    },
    PipelineToConnector {
        /// The instance we're connecting to
        from: DeployEndpointRaw<'script>,
        /// The instance being connected
        to: DeployEndpointRaw<'script>,
        /// The instance we're connecting to
        mid: Box<NodeMeta>,
    },
    PipelineToPipeline {
        /// The instance we're connecting to
        from: DeployEndpointRaw<'script>,
        /// The instance being connected
        to: DeployEndpointRaw<'script>,
        /// The instance we're connecting to
        mid: Box<NodeMeta>,
    },
}
impl<'script> Upable<'script> for ConnectStmtRaw<'script> {
    type Target = ConnectStmt;
    fn up<'registry>(self, helper: &mut Helper<'script, 'registry>) -> Result<Self::Target> {
        match self {
            ConnectStmtRaw::ConnectorToPipeline { mid, from, to } => {
                Ok(ConnectStmt::ConnectorToPipeline {
                    mid,
                    from: from.up(helper)?,
                    to: to.up(helper)?,
                })
            }
            ConnectStmtRaw::PipelineToConnector { mid, from, to } => {
                Ok(ConnectStmt::PipelineToConnector {
                    mid,
                    from: from.up(helper)?,
                    to: to.up(helper)?,
                })
            }
            ConnectStmtRaw::PipelineToPipeline { mid, from, to } => {
                Ok(ConnectStmt::PipelineToPipeline {
                    mid,
                    from: from.up(helper)?,
                    to: to.up(helper)?,
                })
            }
        }
    }
}

/// we're forced to make this pub because of lalrpop
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct FlowDefinitionRaw<'script> {
    pub(crate) id: String,
    pub(crate) params: DefinitionalArgsRaw<'script>,
    pub(crate) doc: Option<Vec<Cow<'script, str>>>,
    pub(crate) stmts: Vec<FlowStmtRaw<'script>>,
    pub(crate) mid: Box<NodeMeta>,
}

impl<'script> FlowDefinitionRaw<'script> {
    fn doc(&self) -> FlowDoc {
        FlowDoc {
            name: self.id.clone(),
            doc: self
                .doc
                .clone()
                .map(|d| d.iter().map(|l| l.trim()).collect::<Vec<_>>().join("\n")),
        }
    }
}
impl_expr!(FlowDefinitionRaw);

impl<'script> Upable<'script> for FlowDefinitionRaw<'script> {
    type Target = FlowDefinition<'script>;
    fn up<'registry>(self, helper: &mut Helper<'script, 'registry>) -> Result<Self::Target> {
        // NOTE As we can have module aliases and/or nested modules within script definitions
        // that are private to or inline with the script - multiple script definitions in the
        // same module scope can share the same relative function/const module paths.
        //
        // We add the script name to the scope as a means to distinguish these orthogonal
        // definitions. This is achieved with the push/pop pointcut around the up() call
        // below. The actual function registration occurs in the up() call in the usual way.

        helper.enter_scope();

        let mut connections = Vec::new();
        let mut creates = Vec::new();
        for stmt in self.stmts {
            match stmt {
                FlowStmtRaw::Use(UseRaw { alias, module, mid }) => {
                    let range = mid.range;
                    let module_id = Manager::load(&module).map_err(|err| match err {
                        Error(ErrorKind::ModuleNotFound(_, _, p, exp), state) => Error(
                            ErrorKind::ModuleNotFound(range.expand_lines(2), range, p, exp),
                            state,
                        ),
                        _ => err,
                    })?;
                    let alias = alias.unwrap_or_else(|| module.id.clone());
                    helper.scope().add_module_alias(alias, module_id);
                }
                FlowStmtRaw::ConnectorDefinition(stmt) => {
                    let stmt = stmt.up(helper)?;
                    helper.scope.insert_connector(stmt)?;
                }
                FlowStmtRaw::PipelineDefinition(stmt) => {
                    let stmt = stmt.up(helper)?;
                    helper.scope.insert_pipeline(stmt)?;
                }
                FlowStmtRaw::Connect(connect) => {
                    connections.push(connect.up(helper)?);
                }
                FlowStmtRaw::Create(stmt) => {
                    creates.push(stmt.up(helper)?);
                }
            }
        }
        let mid = self.mid.box_with_name(&self.id);
        let docs = self
            .doc
            .map(|d| d.iter().map(|l| l.trim()).collect::<Vec<_>>().join("\n"));
        let params = self.params.up(helper)?;
        helper.leave_scope()?;

        let flow_defn = FlowDefinition {
            mid,
            id: self.id,
            params,
            connections,
            creates,
            docs,
        };
        Ok(flow_defn)
    }
}

pub(crate) type FlowStmtsRaw<'script> = Vec<FlowStmtRaw<'script>>;

#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) enum FlowStmtRaw<'script> {
    ConnectorDefinition(ConnectorDefinitionRaw<'script>),
    PipelineDefinition(PipelineDefinitionRaw<'script>),
    Connect(ConnectStmtRaw<'script>),
    Create(CreateStmtRaw<'script>),
    Use(UseRaw),
}

/// we're forced to make this pub because of lalrpop
#[derive(Clone, Debug, PartialEq, Serialize)]
pub enum CreateKind {
    /// Reference to a connector definition
    Connector,
    /// Reference to a pipeline definition
    Pipeline,
}

/// we're forced to make this pub because of lalrpop
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct CreateStmtRaw<'script> {
    pub(crate) id: IdentRaw<'script>,
    pub(crate) params: CreationalWithRaw<'script>,
    /// Id of the definition
    pub target: NodeId,
    /// Module of the definition
    pub(crate) kind: CreateKind,
    pub(crate) mid: Box<NodeMeta>,
}
impl_expr!(CreateStmtRaw);

impl<'script> Upable<'script> for CreateStmtRaw<'script> {
    type Target = CreateStmt<'script>;
    fn up<'registry>(self, helper: &mut Helper<'script, 'registry>) -> Result<Self::Target> {
        let target = self.target.clone();
        let outer = self.extent();
        let inner = self.id.extent();

        let defn = match self.kind {
            CreateKind::Connector => {
                if let Some(artefact) = helper.get(&target)? {
                    CreateTargetDefinition::Connector(artefact)
                } else {
                    return Err(ErrorKind::DeployArtefactNotDefined(
                        outer,
                        inner,
                        target.to_string(),
                        vec![],
                    )
                    .into());
                }
            }
            CreateKind::Pipeline => {
                if let Some(artefact) = helper.get(&target)? {
                    CreateTargetDefinition::Pipeline(Box::new(artefact))
                } else {
                    return Err(ErrorKind::DeployArtefactNotDefined(
                        outer,
                        inner,
                        target.to_string(),
                        vec![],
                    )
                    .into());
                }
            }
        };
        let args = match defn {
            CreateTargetDefinition::Connector(ref conn) => &conn.params.args.0,
            CreateTargetDefinition::Pipeline(ref pipe) => &pipe.params.args.0,
        };
        for (ident, _) in &self.params.with.exprs {
            if !args.iter().any(|(args_ident, _)| ident.id == args_ident.id) {
                let range = ident.extent();
                let available_args = args
                    .iter()
                    .map(|(ident, _)| ident.id.to_string())
                    .collect::<Vec<String>>();
                return Err(ErrorKind::WithParamNoArg(
                    range.expand_lines(2),
                    range,
                    ident.id.to_string(),
                    self.id.id.to_string(),
                    available_args,
                )
                .into());
            }
        }

        let create_stmt = CreateStmt {
            mid: self.mid.box_with_name(&self.id.id),
            with: self.params.up(helper)?,
            instance_alias: self.id.id.to_string(),
            from_target: target,
            defn,
        };

        Ok(create_stmt)
    }
}

/// we're forced to make this pub because of lalrpop
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct DeployFlowRaw<'script> {
    pub(crate) id: IdentRaw<'script>,
    pub(crate) params: CreationalWithRaw<'script>,
    /// Id of the definition
    pub target: NodeId,
    pub(crate) docs: Option<Vec<Cow<'script, str>>>,
    pub(crate) mid: Box<NodeMeta>,
}
impl_expr!(DeployFlowRaw);

impl<'script> Upable<'script> for DeployFlowRaw<'script> {
    type Target = DeployFlow<'script>;
    fn up<'registry>(self, helper: &mut Helper<'script, 'registry>) -> Result<Self::Target> {
        // TODO check that names across pipeline/flow/connector definitions are unique or else hygienic error
        let target = self.target.clone();
        let mut defn = if let Some(artefact) = helper.get::<FlowDefinition>(&target)? {
            artefact.clone()
        } else {
            // TODO: smarter error when using a module target
            let defined_flows = helper
                .scope
                .content
                .flows
                .keys()
                .map(ToString::to_string)
                .collect();
            return Err(ErrorKind::DeployArtefactNotDefined(
                self.extent(),
                self.id.extent(),
                target.to_string(),
                defined_flows,
            )
            .into());
        };
        let upped_params = self.params.up(helper)?;
        defn.params.ingest_creational_with(&upped_params)?;
        ConstFolder::new(helper).walk_definitional_args(&mut defn.params)?;
        let defn_args = defn.params.render()?;

        for c in &mut defn.creates {
            c.with.substitute_args(&defn_args, helper)?;
        }

        let create_stmt = DeployFlow {
            mid: self.mid.box_with_name(&self.id.id),
            instance_alias: self.id.id.to_string(),
            from_target: self.target,
            defn,
            docs: self
                .docs
                .map(|d| d.iter().map(|l| l.trim()).collect::<Vec<_>>().join("\n")),
        };

        Ok(create_stmt)
    }
}