substrait-validator 0.1.4

Substrait validator
Documentation
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
// SPDX-License-Identifier: Apache-2.0

//! Module for parsing/validation mask expressions.

use crate::input::proto::substrait;
use crate::output::diagnostic;
use crate::output::type_system::data;
use crate::parse::context;
use crate::util;

/// Parse a struct item.
fn parse_struct_item(
    x: &substrait::expression::mask_expression::StructItem,
    y: &mut context::Context,
    root: &data::Type,
) -> diagnostic::Result<()> {
    // Handle the struct index field.
    let data_type = proto_primitive_field!(x, y, field, super::parse_struct_field_index, root)
        .1
        .unwrap_or_default();

    // Set resulting data type.
    y.set_data_type(data_type.clone());

    // Handle child selection, if any, to recursively project the field type
    // of the selected struct field.
    if x.child.is_some() {
        let data_type = proto_required_field!(x, y, child, parse_select, &data_type)
            .0
            .data_type();

        // Update data type.
        y.set_data_type(data_type);

        // Describe node.
        describe!(y, Expression, "Struct item selection and sub-selection");
    } else {
        describe!(y, Expression, "Struct item selection");
    }

    Ok(())
}

/// Parse a struct selection, a filter/swizzle for a struct type.
fn parse_struct_select(
    x: &substrait::expression::mask_expression::StructSelect,
    y: &mut context::Context,
    root: &data::Type,
) -> diagnostic::Result<()> {
    // Struct selections can only be applied to structs.
    if !root.is_unresolved() && !root.is_struct() {
        diagnostic!(
            y,
            Error,
            TypeMismatch,
            "struct selection requires a struct type, but got a {}",
            root.class()
        );
    }

    // Parse fields.
    let fields = proto_repeated_field!(
        x,
        y,
        struct_items,
        parse_struct_item,
        |_, _, _, _, _| (),
        root
    )
    .0
    .iter()
    .map(|x| x.data_type())
    .collect::<Vec<_>>();

    // Create struct.
    y.set_data_type(data::new_struct(fields, root.nullable()));

    // Describe node.
    describe!(y, Expression, "Struct selection");
    Ok(())
}

/// Parse a list element selection.
fn parse_list_select_item_element(
    x: &substrait::expression::mask_expression::list_select::list_select_item::ListElement,
    y: &mut context::Context,
) -> diagnostic::Result<()> {
    proto_primitive_field!(x, y, field);
    describe!(
        y,
        Expression,
        "Select {} element",
        util::string::describe_index(x.field)
    );
    Ok(())
}

/// Parse a list slice selection.
fn parse_list_select_item_slice(
    x: &substrait::expression::mask_expression::list_select::list_select_item::ListSlice,
    y: &mut context::Context,
) -> diagnostic::Result<()> {
    proto_primitive_field!(x, y, start);
    proto_primitive_field!(x, y, end);

    // Raise a diagnostic if the slice is always null, and describe the slice.
    let description = if (x.start >= 0) == (x.end >= 0) && x.start < x.end {
        diagnostic!(y, Info, RedundantListSlice, "slice is always null");
        String::from("Selects an empty list slice")
    } else if x.start == 0 {
        match x.end {
            i32::MIN..=-3 => format!("Selects all but the last {} elements", -x.end - 1),
            -2 => String::from("Selects all but the last element"),
            -1 => String::from("Selects the complete list"),
            0 => String::from("Selects the first element"),
            1..=i32::MAX => format!("Selects the first {} elements", x.end + 1),
        }
    } else if x.end == -1 {
        match x.start {
            i32::MIN..=-2 => format!("Selects the last {} elements", -x.start),
            -1 => String::from("Selects the last element"),
            0 => String::from("Selects the complete list"),
            1 => String::from("Selects all but the first element"),
            2..=i32::MAX => format!("Selects all but the first {} elements", x.start),
        }
    } else {
        format!(
            "Select {} until {} element (inclusive)",
            util::string::describe_index(x.start),
            util::string::describe_index(x.end)
        )
    };
    describe!(y, Expression, "{}", description);

    // Describe the node.
    Ok(())
}

/// Parse a list selection item type.
fn parse_list_select_item_type(
    x: &substrait::expression::mask_expression::list_select::list_select_item::Type,
    y: &mut context::Context,
) -> diagnostic::Result<()> {
    match x {
        substrait::expression::mask_expression::list_select::list_select_item::Type::Item(x) => {
            parse_list_select_item_element(x, y)
        }
        substrait::expression::mask_expression::list_select::list_select_item::Type::Slice(x) => {
            parse_list_select_item_slice(x, y)
        }
    }
}

/// Parse a list selection item.
fn parse_list_select_item(
    x: &substrait::expression::mask_expression::list_select::ListSelectItem,
    y: &mut context::Context,
) -> diagnostic::Result<()> {
    proto_required_field!(x, y, r#type, parse_list_select_item_type);
    Ok(())
}

/// Parse a list selection, a filter/swizzle for a list type.
fn parse_list_select(
    x: &substrait::expression::mask_expression::ListSelect,
    y: &mut context::Context,
    root: &data::Type,
) -> diagnostic::Result<()> {
    // List selections can only be applied to lists.
    if !root.is_unresolved() && !root.is_list() {
        diagnostic!(
            y,
            Error,
            TypeMismatch,
            "list selection requires a list type, but got a {}",
            root.class()
        );
    }

    // Parse fields.
    proto_repeated_field!(x, y, selection, parse_list_select_item);

    // Set resulting data type.
    y.set_data_type(root.clone());

    // Handle child selection, if any, to recursively project the list element
    // type.
    if x.child.is_some() {
        // Get the list element type.
        let data_type = root.unwrap_list().unwrap_or_default();

        // Apply selection logic recursively.
        let data_type = proto_boxed_required_field!(x, y, child, parse_select, &data_type)
            .0
            .data_type();

        // Create the new type.
        y.set_data_type(data::new_list(data_type, root.nullable()));

        // Describe node.
        describe!(y, Expression, "List selection and sub-selection");
    } else {
        describe!(y, Expression, "List selection");
    }

    Ok(())
}

/// Parse a map single-key selection.
fn parse_map_select_key(
    _x: &substrait::expression::mask_expression::map_select::MapKey,
    y: &mut context::Context,
    _key_type: &data::Type,
) -> diagnostic::Result<()> {
    // FIXME: map keys are not necessarily strings. Why is this not a
    // primitive?
    diagnostic!(
        y,
        Error,
        NotYetImplemented,
        "map key remappings are not yet specified"
    );
    describe!(y, Expression, "Single-key map selection");
    Ok(())
}

/// Parse a map selection by means of an expression.
fn parse_map_select_expression(
    _x: &substrait::expression::mask_expression::map_select::MapKeyExpression,
    y: &mut context::Context,
    _key_type: &data::Type,
) -> diagnostic::Result<()> {
    // FIXME: in Rust vernacular, need an Fn(K) -> Option<K> here. I suppose
    // there is no structure for that yet? Or are these the regex-type things
    // that are not yet specified?
    diagnostic!(
        y,
        Error,
        NotYetImplemented,
        "map key remappings are not yet specified"
    );
    describe!(y, Expression, "Map key remapping");
    Ok(())
}

/// Parse a map selection type.
fn parse_map_select_type(
    x: &substrait::expression::mask_expression::map_select::Select,
    y: &mut context::Context,
    root: &data::Type,
) -> diagnostic::Result<()> {
    match x {
        substrait::expression::mask_expression::map_select::Select::Key(x) => {
            parse_map_select_key(x, y, root)
        }
        substrait::expression::mask_expression::map_select::Select::Expression(x) => {
            parse_map_select_expression(x, y, root)
        }
    }
}

/// Parse a map selection.
fn parse_map_select(
    x: &substrait::expression::mask_expression::MapSelect,
    y: &mut context::Context,
    root: &data::Type,
) -> diagnostic::Result<()> {
    // Map selections can only be applied to maps.
    if !root.is_unresolved() && !root.is_map() {
        diagnostic!(
            y,
            Error,
            TypeMismatch,
            "map selection requires a map type, but got a {}",
            root.class()
        );
    }

    // Parse selection field.
    if x.select.is_some() {
        proto_required_field!(
            x,
            y,
            select,
            parse_map_select_type,
            &root.unwrap_map_key().unwrap_or_default()
        );
    } else {
        comment!(y, "No select key specified: mapping is left unchanged.");
    }

    // Set resulting data type.
    y.set_data_type(root.clone());

    // Handle child selection, if any, to recursively project the map value
    // type.
    if x.child.is_some() {
        // Get the map types.
        let value_type = root.unwrap_map().unwrap_or_default();
        let key_type = root.unwrap_map_key().unwrap_or_default();

        // Apply selection logic recursively.
        let value_type = proto_boxed_required_field!(x, y, child, parse_select, &value_type)
            .0
            .data_type();

        // Create the new type.
        y.set_data_type(data::new_map(key_type, value_type, root.nullable()));

        // Describe node.
        describe!(y, Expression, "Map selection and sub-selection");
    } else {
        describe!(y, Expression, "Map selection");
    }

    Ok(())
}

/// Parse a selection type.
fn parse_select_type(
    x: &substrait::expression::mask_expression::select::Type,
    y: &mut context::Context,
    root: &data::Type,
) -> diagnostic::Result<()> {
    match x {
        substrait::expression::mask_expression::select::Type::Struct(x) => {
            parse_struct_select(x, y, root)
        }
        substrait::expression::mask_expression::select::Type::List(x) => {
            parse_list_select(x.as_ref(), y, root)
        }
        substrait::expression::mask_expression::select::Type::Map(x) => {
            parse_map_select(x.as_ref(), y, root)
        }
    }
}

fn parse_select(
    x: &substrait::expression::mask_expression::Select,
    y: &mut context::Context,
    root: &data::Type,
) -> diagnostic::Result<()> {
    let data_type = proto_required_field!(x, y, r#type, parse_select_type, root)
        .0
        .data_type();
    y.set_data_type(data_type);
    Ok(())
}

/// Parses the maintain_singular_struct field of a mask expression. is_singular
/// must specify whether the data type is actually a singular struct, while
/// struct_required must specify whether the context of the mask expression
/// requires a struct type. Returns whether the data type is a singular struct
/// and should be unwrapped.
fn parse_maintain_singular_struct(
    x: &bool,
    y: &mut context::Context,
    is_singular: bool,
    struct_required: bool,
) -> diagnostic::Result<bool> {
    let maintain = *x;
    match (is_singular, maintain, struct_required) {
        (true, true, _) => {
            // Okay: maintain struct.
            summary!(
                y,
                "Mask expression yields a singular struct, which is \
                maintained as-is."
            );
            Ok(false)
        }
        (true, false, true) => {
            // Error: request to remove struct, but context requires a struct.
            summary!(
                y,
                "Mask expression yields a singular struct, which would be \
                reduced to its element type, but its context does not allow \
                this."
            );
            diagnostic!(
                y,
                Error,
                TypeStructRequired,
                "context requires a struct type and type is a singular \
                struct, maintain_singular_struct must be set"
            );
            Ok(false)
        }
        (true, false, false) => {
            // Okay: remove singular struct wrapper.
            summary!(
                y,
                "Mask expression yields a singular struct, which is reduced \
                to its element type."
            );
            Ok(true)
        }
        (false, true, _) => {
            // Okay: not a singular struct, so there is nothing to strip.
            summary!(
                y,
                "Data type of mask expression is not a singular struct, so \
                there is nothing to strip or maintain. The explicit true is \
                redundant."
            );
            Ok(false)
        }
        (false, false, _) => {
            // Okay: not a singular struct, so there is nothing to strip.
            summary!(
                y,
                "Data type of mask expression is not a singular struct, so \
                there is nothing to strip or maintain."
            );
            Ok(false)
        }
    }
}

/// Parse a mask expression; that is, a field selection that can output a
/// nested structure. root specifies the data type being indexed, while
/// struct_required must specify whether the context of the mask expression
/// requires a struct type.
pub fn parse_mask_expression(
    x: &substrait::expression::MaskExpression,
    y: &mut context::Context,
    root: &data::Type,
    struct_required: bool,
) -> diagnostic::Result<()> {
    // Parse the struct selection and get its data type.
    let data_type = proto_required_field!(x, y, select, parse_struct_select, root)
        .0
        .data_type();

    // Determine if the data type is a singular struct (i.e. a struct with only
    // one item) and its element type if so.
    let singular_type = data_type.unwrap_singular_struct().map(|data_type| {
        if root.nullable() {
            data_type.make_nullable()
        } else {
            data_type
        }
    });

    // Handle the maintain_singular_struct field.
    let unwrap = proto_primitive_field!(
        x,
        y,
        maintain_singular_struct,
        parse_maintain_singular_struct,
        singular_type.is_some(),
        struct_required
    )
    .1
    .unwrap_or_default();

    // Set the data type.
    y.set_data_type(if unwrap {
        singular_type.unwrap()
    } else {
        data_type
    });

    // Describe node.
    describe!(
        y,
        Expression,
        "References fields into a new nested structure"
    );
    Ok(())
}