weld 0.4.0

Weld is a language and runtime for improving the performance of data-intensive applications.
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
//! Pretty print Weld expression trees.
//!
//! This module prints Weld expressions. The printed expression is a valid Weld program and can be
//! re-parsed using the Weld API.

use crate::ast::ExprKind::*;
use crate::ast::*;

use crate::util::join;

/// The number of spaces used when indenting code.
const INDENT_LEVEL: i32 = 2;

/// A trait for pretty printing expression trees.
pub trait PrettyPrint {
    /// Pretty print an expression.
    ///
    /// The pretty printed expression contains newlines and is indented to be human-readable.  The
    /// printed expression is a valid, parseable Weld program that can be re-parsed via the Weld
    /// API.
    fn pretty_print(&self) -> String;

    /// Pretty print an expression with the given configuration.
    ///
    /// The printed expression is a valid, parseable Weld program that can be
    /// re-parsed via the Weld API.
    fn pretty_print_config(&self, config: &PrettyPrintConfig) -> String;
}

impl PrettyPrint for Expr {
    /// Pretty print an expression.
    fn pretty_print(&self) -> String {
        let mut config = PrettyPrintConfig::default()
            .should_indent(true)
            .show_types(false);
        to_string_impl(self, &mut config)
    }

    /// Pretty print an expression with the given configuration.
    fn pretty_print_config(&self, config: &PrettyPrintConfig) -> String {
        let config = &mut config.clone();
        config.indent = INDENT_LEVEL;
        to_string_impl(self, config)
    }
}

/// A struct used to configure pretty printing.
///
/// This struct is used with the `PrettyPrint::pretty_print_config` function to configure how the
/// Weld expression is printed.
#[derive(Clone)]
pub struct PrettyPrintConfig {
    /// Sets whether to print the type of each symbol.
    pub show_types: bool,
    /// Specifies whether to indent code.
    pub should_indent: bool,
    /// Tracks the indentation level.
    indent: i32,
    /// Specifies whether this is the top of the expression tree.
    top: bool,
}

impl std::default::Default for PrettyPrintConfig {
    /// Returns a new default `PrettyPrintConfig`.
    ///
    /// The default configuration does not show types for each symbol name and indents code.
    fn default() -> Self {
        PrettyPrintConfig {
            show_types: false,
            should_indent: true,
            indent: INDENT_LEVEL,
            top: true,
        }
    }
}

impl PrettyPrintConfig {
    /// Set whether this print configuration should show types for each symbol.
    pub fn show_types(mut self, show_types: bool) -> PrettyPrintConfig {
        self.show_types = show_types;
        self
    }

    /// Set whether this print configuration should indent code.
    pub fn should_indent(mut self, should_indent: bool) -> PrettyPrintConfig {
        self.should_indent = should_indent;
        self
    }
}

/// Returns strings used for indentation.
///
/// Specifically, this function returns three strings: one representating the indented level of the
/// current configuration, one to represent one indent level lower, and one representing a newline
/// character.
fn indentation(config: &PrettyPrintConfig) -> (String, String, String) {
    if config.should_indent {
        (
            " ".repeat((config.indent - 2) as usize),
            " ".repeat(config.indent as usize),
            "\n".to_string(),
        )
    } else {
        ("".to_string(), "".to_string(), "".to_string())
    }
}

/// Prints a list of iterators for a For loop.
fn print_iters(iters: &[Iter], config: &mut PrettyPrintConfig) -> String {
    use self::IterKind::*;
    let mut iter_strs = vec![];
    let (less_indent_str, indent_str, newline) = indentation(config);
    for iter in iters {
        match iter.kind {
            NdIter => {
                /* Need to check this first because NdIter also has iter.start */
                iter_strs.push(format!(
                    "{}({},{},{},{})",
                    iter.kind,
                    to_string_impl(iter.data.as_ref(), config),
                    to_string_impl(iter.start.as_ref().unwrap(), config),
                    to_string_impl(iter.shape.as_ref().unwrap(), config),
                    to_string_impl(iter.strides.as_ref().unwrap(), config),
                ));
            }
            _ if iter.start.is_some() => {
                iter_strs.push(format!(
                    "{}({},{},{},{})",
                    iter.kind,
                    to_string_impl(iter.data.as_ref(), config),
                    to_string_impl(iter.start.as_ref().unwrap(), config),
                    to_string_impl(iter.end.as_ref().unwrap(), config),
                    to_string_impl(iter.stride.as_ref().unwrap(), config)
                ));
            }
            ScalarIter => {
                // don't print the Iter for ScalarIter for conciseness.
                iter_strs.push(to_string_impl(iter.data.as_ref(), config));
            }
            _ => {
                iter_strs.push(format!(
                    "{}({})",
                    iter.kind,
                    to_string_impl(iter.data.as_ref(), config)
                ));
            }
        }
    }

    if iters.len() > 1 {
        format!(
            "zip({}{}{}{}{})",
            newline,
            iter_strs.join(&format!(",{}{}", newline, indent_str)),
            indent_str,
            newline,
            less_indent_str
        )
    } else {
        iter_strs[0].clone()
    }
}

/// Pretty print each expression in an AST recursively.
fn to_string_impl(expr: &Expr, config: &mut PrettyPrintConfig) -> String {
    let (less_indent_str, indent_str, newline) = indentation(config);
    let top = config.top;
    config.top = false;
    let expr_str = match expr.kind {
        Literal(ref lit) => lit.to_string(),

        Ident(ref symbol) if config.show_types => format!("{}:{}", symbol, expr.ty),

        Ident(ref symbol) => symbol.to_string(),

        BinOp {
            kind,
            ref left,
            ref right,
        } => {
            use super::ast::BinOpKind::*;
            let left = to_string_impl(left, config);
            let right = to_string_impl(right, config);
            match kind {
                Max | Min | Pow => format!("{}({},{})", kind, left, right),
                _ => format!("({}{}{})", left, kind, right),
            }
        }

        UnaryOp { kind, ref value } => format!("({}({}))", kind, to_string_impl(value, config)),

        Negate(ref e) => format!("(-{})", to_string_impl(e, config)),

        Not(ref e) => format!("(!{})", to_string_impl(e, config)),

        Assert(ref e) => format!("assert({})", to_string_impl(e, config)),

        Broadcast(ref e) => format!("broadcast({})", to_string_impl(e, config)),

        CUDF {
            ref sym_name,
            ref args,
            ref return_ty,
        } => {
            let args = join(
                "(",
                ",",
                ")",
                args.iter().map(|e| to_string_impl(e, config)),
            );
            format!("cudf[{},{}]{}", sym_name, return_ty, args)
        }

        Serialize(ref e) => format!("serialize({})", to_string_impl(e, config)),

        Deserialize {
            ref value,
            ref value_ty,
        } => format!(
            "deserialize[{}]({})",
            value_ty,
            to_string_impl(value, config)
        ),

        Cast {
            kind,
            ref child_expr,
        } => format!("({}({}))", kind, to_string_impl(child_expr, config)),

        ToVec { ref child_expr } => format!("tovec({})", to_string_impl(child_expr, config)),

        Let {
            ref name,
            ref value,
            ref body,
        } => {
            let value_str = to_string_impl(value, config);
            let body = to_string_impl(body, config);
            if config.show_types {
                format!(
                    "(let {}:{}=({});{}{}{})",
                    name, value.ty, value_str, newline, indent_str, body
                )
            } else {
                format!(
                    "(let {}=({});{}{}{})",
                    name, value_str, newline, indent_str, body
                )
            }
        }

        MakeStruct { ref elems } => join(
            "{",
            ",",
            "}",
            elems.iter().map(|e| to_string_impl(e, config)),
        ),

        MakeVector { ref elems } => join(
            "[",
            ",",
            "]",
            elems.iter().map(|e| to_string_impl(e, config)),
        ),

        Zip { ref vectors } => {
            let begin = &format!("zip({}{}", newline, indent_str);
            let newlines = &format!(",{}{}", newline, indent_str);
            join(
                begin,
                newlines,
                ")",
                vectors.iter().map(&mut |e| {
                    config.indent += INDENT_LEVEL;
                    let result = to_string_impl(e, config);
                    config.indent -= INDENT_LEVEL;
                    result
                }),
            )
        }

        GetField { ref expr, index } => {
            // If the expression is a symbol, don't show its type in a GetField.
            if config.show_types {
                if let Ident(ref symbol) = expr.kind {
                    return format!("{}.${}", symbol, index);
                }
            }
            format!("{}.${}", to_string_impl(expr, config), index)
        }

        Length { ref data } => format!("len({})", to_string_impl(data, config)),

        Lookup {
            ref data,
            ref index,
        } => format!(
            "lookup({},{})",
            to_string_impl(data, config),
            to_string_impl(index, config)
        ),

        OptLookup {
            ref data,
            ref index,
        } => format!(
            "optlookup({},{})",
            to_string_impl(data, config),
            to_string_impl(index, config)
        ),

        KeyExists { ref data, ref key } => format!(
            "keyexists({},{})",
            to_string_impl(data, config),
            to_string_impl(key, config)
        ),

        Slice {
            ref data,
            ref index,
            ref size,
        } => format!(
            "slice({},{},{})",
            to_string_impl(data, config),
            to_string_impl(index, config),
            to_string_impl(size, config)
        ),

        Sort {
            ref data,
            ref cmpfunc,
        } => format!(
            "sort({},{})",
            to_string_impl(data, config),
            to_string_impl(cmpfunc, config)
        ),

        Lambda {
            ref params,
            ref body,
        } => {
            // Print types for a top-level Lambda even if show_types is disabled - this allows
            // type inference to infer the remaining types in the program.
            let mut res = join(
                "|",
                ",",
                "|",
                params.iter().map(|e| {
                    if top || config.show_types {
                        e.to_string()
                    } else {
                        e.name.to_string()
                    }
                }),
            );
            config.indent += INDENT_LEVEL;
            let body = to_string_impl(body, config);
            config.indent -= INDENT_LEVEL;
            res.push_str(&format!("{}{}{}", newline, indent_str, body));
            res
        }

        NewBuilder(ref arg) => match *arg {
            Some(ref e) => format!("{}({})", expr.ty, to_string_impl(e, config)),
            None => expr.ty.to_string(),
        },

        Res { ref builder } => {
            config.indent += INDENT_LEVEL;
            let builder = to_string_impl(builder, config);
            config.indent -= INDENT_LEVEL;
            format!(
                "result({}{}{}{}{})",
                newline, indent_str, builder, newline, less_indent_str
            )
        }

        Merge {
            ref builder,
            ref value,
        } => format!(
            "merge({},{})",
            to_string_impl(builder, config),
            to_string_impl(value, config)
        ),

        For {
            ref iters,
            ref builder,
            ref func,
        } => {
            config.indent += INDENT_LEVEL;
            let res = format!(
                "for({}{}{},{}{}{},{}{}{}{}{})",
                newline,
                indent_str,
                print_iters(iters, config),
                newline,
                indent_str,
                to_string_impl(builder, config),
                newline,
                indent_str,
                to_string_impl(func, config),
                newline,
                less_indent_str
            );
            config.indent -= INDENT_LEVEL;
            res
        }

        If {
            ref cond,
            ref on_true,
            ref on_false,
        } => {
            config.indent += INDENT_LEVEL;
            let res = format!(
                "if({}{}{},{}{}{},{}{}{}{}{})",
                newline,
                indent_str,
                to_string_impl(cond, config),
                newline,
                indent_str,
                to_string_impl(on_true, config),
                newline,
                indent_str,
                to_string_impl(on_false, config),
                newline,
                less_indent_str
            );
            config.indent -= INDENT_LEVEL;
            res
        }

        Iterate {
            ref initial,
            ref update_func,
        } => {
            config.indent += INDENT_LEVEL;
            let res = format!(
                "iterate({}{}{},{}{}{}{}{})",
                newline,
                indent_str,
                to_string_impl(initial, config),
                newline,
                indent_str,
                to_string_impl(update_func, config),
                newline,
                less_indent_str
            );
            config.indent -= INDENT_LEVEL;
            res
        }

        Select {
            ref cond,
            ref on_true,
            ref on_false,
        } => {
            config.indent += INDENT_LEVEL;
            let res = format!(
                "select({}{}{},{}{}{},{}{}{}{}{})",
                newline,
                indent_str,
                to_string_impl(cond, config),
                newline,
                indent_str,
                to_string_impl(on_true, config),
                newline,
                indent_str,
                to_string_impl(on_false, config),
                newline,
                less_indent_str
            );
            config.indent -= INDENT_LEVEL;
            res
        }

        Apply {
            ref func,
            ref params,
        } => {
            let mut res = format!("({})", to_string_impl(func, config));
            let params = params.iter().map(|e| to_string_impl(e, config));
            res.push_str(&join("(", ",", ")", params));
            res
        }
    };
    format!("{}{}", expr.annotations, expr_str)
}