hamelin_lib 0.12.0

Core library for Hamelin query language
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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
use std::error::Error;

use ordermap::OrderMap;
use vecmap::VecMap;

use crate::err::NonMergeableTypes;
use crate::sql::expression::apply::{FunctionCallApply, Lambda};
use crate::sql::expression::identifier::{CompoundIdentifier, Identifier, SimpleIdentifier};
use crate::sql::expression::literal::{ColumnReference, NullLiteral, RowLiteral};
use crate::sql::expression::Cast;
use crate::sql::expression::{Leaf, SQLExpression};
use crate::sql::query::projection::{Binding, ColumnProjection, Projection};
use crate::sql::types::SQLRowType;
use crate::tree::ast::identifier::SimpleIdentifier as AstSimpleIdentifier;
use crate::types::array::Array;
use crate::types::struct_type::Struct;
use crate::types::Type;

#[derive(Clone, Debug)]
pub enum RowLiteralEntry {
    Leaf(Type, SQLExpression),
    Node(Box<ProjectionBuilder>),
}

impl RowLiteralEntry {
    pub fn leaf(type_: Type, expression: SQLExpression) -> Self {
        RowLiteralEntry::Leaf(type_, expression)
    }
    pub fn node(builder: ProjectionBuilder) -> Self {
        RowLiteralEntry::Node(Box::new(builder))
    }
}

#[derive(Clone, Debug, Default)]
pub struct ProjectionBuilder {
    bindings: VecMap<SimpleIdentifier, RowLiteralEntry>,
}

impl ProjectionBuilder {
    /// Construct a new projection builder with items initialized to reference to the fields in the given
    /// struct. Initialize the references to the fields in this struct with the given prefix. The projection
    /// builder would be useful to be used to select the given struct by the given name.
    pub fn deep_initialize_from_struct(name: Option<Identifier>, hst: Struct) -> Self {
        let mut builder = Self::default();
        for (ast_id, typ) in hst.into_iter() {
            let id: SimpleIdentifier = ast_id.into();
            let root_ident = name
                .clone()
                .map(|n| CompoundIdentifier::from_two_idents(n, id.clone().into()).into())
                .unwrap_or(id.clone().into());
            if let Type::Struct(s) = typ {
                builder.bindings.insert(
                    id.clone(),
                    RowLiteralEntry::node(Self::deep_initialize_from_struct(Some(root_ident), s)),
                );
            } else {
                builder.bindings.insert(
                    id,
                    RowLiteralEntry::leaf(typ.clone(), ColumnReference::new(root_ident).into()),
                );
            }
        }

        builder
    }

    pub fn expand_repeated_struct(
        array: SQLExpression,
        from: &Type,
        to: &Type,
    ) -> Result<SQLExpression, ExpandError> {
        Self::expand_repeated_struct_inner(array, from, to, false)
    }

    /// When `anonymous_row` is true the lambda body emits a bare `ROW(...)`
    /// instead of `CAST(ROW(...) AS ROW(...))`.  Use this when the transform
    /// will be nested inside a parent CAST that already carries the field names
    /// (avoids Trino's PushCastIntoRow stripping them from a named inner CAST).
    fn expand_repeated_struct_inner(
        array: SQLExpression,
        from: &Type,
        to: &Type,
        anonymous_row: bool,
    ) -> Result<SQLExpression, ExpandError> {
        if let Type::Array(Array {
            element_type: from_elt,
        }) = from
        {
            if let Type::Array(Array {
                element_type: to_elt,
            }) = to
            {
                if let Type::Struct(from_struct) = &**from_elt {
                    if let Type::Struct(to_struct) = &**to_elt {
                        let e = SimpleIdentifier::new("e");
                        let expanded = Self::deep_initialize_from_struct(
                            Some(e.clone().into()),
                            from_struct.clone(),
                        )
                        .expand(to_struct.clone())?;
                        let lambda_body: SQLExpression = if anonymous_row {
                            expanded.build_literal().into()
                        } else {
                            expanded
                                .build_cast()
                                .map_err(|e| ExpandError::Fatal(e.into()))?
                                .into()
                        };
                        return Ok(FunctionCallApply::with_two(
                            "transform",
                            array,
                            Lambda::new(vec![e], lambda_body).into(),
                        )
                        .into());
                    }
                }
            }
        }

        Err(ExpandError::NonMergeable(NonMergeableTypes::new(
            from.clone(),
            to.clone(),
        )))
    }

    /// Expand this projection builder to entirely cover the given type.
    ///
    /// Where this projection builder does not define a column in the given type, it will add a null
    /// literal. It will also reorder the columns to match the given.
    ///
    /// # Arguments
    /// * `to` - The struct type to expand to
    pub fn expand(&self, to: Struct) -> Result<Self, ExpandError> {
        self.expand_inner(to, false)
    }

    /// `anonymous_array_casts`: when true, array-of-struct widening inside this
    /// expansion emits an anonymous `ROW(...)` lambda body instead of
    /// `CAST(ROW(...) AS ROW(...))`.  The parent `build_cast()` already carries
    /// the field names, and Trino's PushCastIntoRow optimizer incorrectly
    /// strips them from a named inner CAST.
    fn expand_inner(&self, to: Struct, anonymous_array_casts: bool) -> Result<Self, ExpandError> {
        let mut ret = Self::default();
        for (ast_key, to_type) in to.into_iter() {
            let to_key: SimpleIdentifier = ast_key.into();
            if let Some(from_value) = self.bindings.get(&to_key) {
                match from_value {
                    RowLiteralEntry::Leaf(t, expression) if t == &to_type => {
                        ret.bind(to_key.clone().into(), expression.clone(), t.clone());
                    }
                    RowLiteralEntry::Leaf(t, expression) => {
                        ret.bind(
                            to_key.clone().into(),
                            Self::expand_repeated_struct_inner(
                                expression.clone(),
                                t,
                                &to_type,
                                anonymous_array_casts,
                            )?,
                            to_type.clone(),
                        );
                    }
                    RowLiteralEntry::Node(nested_pb) => {
                        if let Type::Struct(to_type) = to_type {
                            if nested_pb.clone().build_hamelin_type() == to_type {
                                ret.bindings.insert(to_key.clone(), from_value.clone());
                            } else {
                                ret.bindings.insert(
                                    to_key.clone(),
                                    RowLiteralEntry::node(nested_pb.expand_inner(to_type, true)?),
                                );
                            }
                        } else {
                            return Err(ExpandError::ExpectedStruct(to_key.clone(), to_type));
                        }
                    }
                }
            } else {
                ret.bind(
                    to_key.clone().into(),
                    NullLiteral::default().into(),
                    to_type.clone(),
                );
            }
        }
        Ok(ret)
    }

    /// Bind a new column value in the list of projections.
    ///
    /// # Arguments
    /// * `id` - Bind this column. Compound identifiers are okay here. It will nest!
    /// * `value` - Bind to this expression
    /// * `type` - Assume this type. (Needed in order to construct the cast parts of a row literal.)
    pub fn bind(&mut self, id: Identifier, value: SQLExpression, type_: Type) {
        match id {
            Identifier::Simple(simple_id) => {
                self.bindings
                    .insert(simple_id.clone(), RowLiteralEntry::leaf(type_, value));
            }
            Identifier::Compound(compound_id) => {
                if let Some(RowLiteralEntry::Node(node)) =
                    self.bindings.get_mut(compound_id.first())
                {
                    node.bind(compound_id.rest(), value, type_);
                } else {
                    let mut bldr = ProjectionBuilder::default();
                    bldr.bind(compound_id.rest(), value, type_);
                    self.bindings
                        .insert(compound_id.first().clone(), RowLiteralEntry::node(bldr));
                }
            }
        }
    }

    /// Bind a new column value in the list of projections, returning a new projection builder.
    ///
    /// # Arguments
    /// * `id` - Bind this column. Compound identifiers are okay here. It will nest!
    /// * `value` - Bind to this expression
    /// * `type` - Assume this type. (Needed in order to construct the cast parts of a row literal.)
    pub fn with_binding(mut self, id: Identifier, value: SQLExpression, type_: Type) -> Self {
        self.bind(id, value, type_);
        self
    }

    /// Bind all projections from another projection builder to this one.
    ///
    /// It will bind every column in the other projection builder to this one. If a column is
    /// present in both, it will be bound to the expression in the other projection builder.
    ///
    /// # Arguments
    /// * `other` - The other projection builder to bind the projections from with this one
    ///
    /// # Returns
    /// The new projection builder containing the bindings from this and the other projection
    /// builder
    pub fn bind_all(&self, other: &Self) -> Self {
        let mut ret = self.clone();
        for (key, value) in other.bindings.iter() {
            match value {
                RowLiteralEntry::Leaf(typ, expression) => {
                    ret.bind(key.clone().into(), expression.clone(), typ.clone());
                }
                RowLiteralEntry::Node(bldr) => {
                    ret.bindings
                        .insert(key.clone(), RowLiteralEntry::node(*bldr.clone()));
                }
            }
        }
        ret
    }

    /// Bind a new column reference in the list of projections.
    ///
    /// Rather than binding a variable to a value, this binds a variable to a self-reference.
    ///
    /// # Arguments
    /// * `ident` - Bind this column
    /// * `type_` - Assume this type. (Needed in order to construct the cast parts of a row
    ///   literal.)
    pub fn bind_column_reference(&mut self, ident: SimpleIdentifier, type_: Type) {
        self.bindings.insert(
            ident.clone(),
            RowLiteralEntry::leaf(type_, ColumnReference::new(ident.into()).into()),
        );
    }

    /// Initialize a key in the list of projections from a Hamelin struct type.
    /// This will set all the expressions to self-reference column references.
    /// This is useful when wanting to construct a projection literal after
    /// having messed with the environment by removing nested elements.
    ///
    /// # Arguments
    /// * `ident` - Initialize this column
    /// * `struct_type` - Initialize the column to a self-reference literal of this type
    pub fn initialize_key(&mut self, ident: SimpleIdentifier, struct_type: Struct) {
        self.bindings.insert(
            ident.clone(),
            RowLiteralEntry::node(Self::deep_initialize_from_struct(
                Some(ident.into()),
                struct_type,
            )),
        );
    }

    /// Test whether a key is present in the list of projections.
    ///
    /// # Returns
    /// True if the key is present, false otherwise.
    pub fn is_present(&self, id: &Identifier) -> bool {
        match id {
            Identifier::Simple(simple_id) => self.bindings.contains_key(simple_id),
            Identifier::Compound(compound_id) => {
                if let Some(RowLiteralEntry::Node(node)) = self.bindings.get(compound_id.first()) {
                    node.is_present(&compound_id.rest())
                } else {
                    false
                }
            }
        }
    }

    pub fn build_literal(&self) -> RowLiteral {
        let mut entries = vec![];
        for (_, value) in self.bindings.iter() {
            match value {
                RowLiteralEntry::Leaf(_, expression) => {
                    entries.push(expression.clone());
                }
                RowLiteralEntry::Node(n) => {
                    entries.push(n.build_literal().into());
                }
            }
        }
        RowLiteral::new(entries)
    }

    pub fn build_sql_type(&self) -> anyhow::Result<SQLRowType> {
        let mut fields = OrderMap::new();
        for (key, value) in self.bindings.iter() {
            match value {
                RowLiteralEntry::Leaf(t, _) => {
                    fields.insert(key.clone(), t.clone().to_sql()?);
                }
                RowLiteralEntry::Node(n) => {
                    fields.insert(key.clone(), n.build_sql_type()?.into());
                }
            }
        }
        Ok(SQLRowType::new(fields))
    }

    pub fn build_cast(&self) -> anyhow::Result<Cast> {
        Ok(Cast::new(
            self.build_literal().into(),
            self.build_sql_type()?.into(),
        ))
    }

    pub fn build_hamelin_type(self) -> Struct {
        let mut fields: VecMap<AstSimpleIdentifier, Type> = VecMap::new();
        for (key, value) in self.bindings.into_iter() {
            let ast_key: AstSimpleIdentifier = key.into();
            match value {
                RowLiteralEntry::Leaf(t, _) => {
                    fields.insert(ast_key, t);
                }
                RowLiteralEntry::Node(n) => {
                    fields.insert(ast_key, n.build_hamelin_type().into());
                }
            }
        }
        Struct::new(fields)
    }

    pub fn build_projections(self) -> anyhow::Result<Vec<Projection>> {
        let mut projections = vec![];
        for (key, value) in self.bindings.into_iter() {
            match value {
                RowLiteralEntry::Leaf(
                    _,
                    SQLExpression::Leaf(Leaf::ColumnReference(ColumnReference { identifier })),
                ) if identifier == key.clone().into() => {
                    projections.push(ColumnProjection::new(identifier).into());
                }
                RowLiteralEntry::Leaf(_, expression) => {
                    projections.push(Binding::new(key.clone(), expression).into());
                }
                RowLiteralEntry::Node(n) => {
                    projections.push(Binding::new(key.clone(), n.build_cast()?.into()).into());
                }
            }
        }
        Ok(projections)
    }
}

#[derive(Debug, thiserror::Error)]
pub enum ExpandError {
    #[error("While expanding, expected key {0} to be struct, but found {1:?}")]
    ExpectedStruct(SimpleIdentifier, Type),
    #[error(transparent)]
    NonMergeable(#[from] NonMergeableTypes),
    #[error("While expanding, encountered fatal error: {0}")]
    Fatal(Box<dyn Error + Send + Sync>),
}

#[cfg(test)]
mod tests {
    use crate::{
        sql::{
            expression::literal::{IntegerLiteral, StringLiteral},
            types::SQLBaseType,
        },
        types::{INT, STRING},
    };

    use super::*;

    #[test]
    pub fn test_empty() {
        let pb = ProjectionBuilder::default();
        assert_eq!(pb.build_literal(), RowLiteral::new(vec![]));
        assert_eq!(
            pb.build_sql_type().unwrap(),
            SQLRowType::new(OrderMap::new())
        );
        assert_eq!(pb.clone().build_hamelin_type(), Struct::new(VecMap::new()));
        assert_eq!(pb.build_projections().unwrap(), vec![]);
    }

    /// Test that the row builder can add a few leaf nodes with simple identifiers.
    /// We'll make sure to set a few flat arguments with expressions that are of type integer and string.
    #[test]
    pub fn test_flat() {
        let mut pb = ProjectionBuilder::default();
        pb.bind(
            SimpleIdentifier::new("a").into(),
            IntegerLiteral::new("1").into(),
            INT,
        );
        pb.bind(
            SimpleIdentifier::new("b").into(),
            StringLiteral::new("hello").into(),
            STRING,
        );
        assert_eq!(
            RowLiteral::new(vec![
                IntegerLiteral::new("1").into(),
                StringLiteral::new("hello").into()
            ]),
            pb.build_literal()
        );
        assert_eq!(
            SQLRowType::default()
                .with_str("a", SQLBaseType::BigInt.into())
                .with_str("b", SQLBaseType::VarChar.into()),
            pb.build_sql_type().unwrap()
        );
        assert_eq!(
            Struct::default().with_str("a", INT).with_str("b", STRING),
            pb.build_hamelin_type()
        );
    }

    /// We'll repeat the test above, but this time we'll also insert those same two flat
    /// expressions nested inside. We'll do this by calling .bind and passing a compound
    /// identifier with two parts.
    #[test]
    pub fn test_nested() {
        let mut pb = ProjectionBuilder::default();
        pb.bind(
            SimpleIdentifier::new("a").into(),
            IntegerLiteral::new("1").into(),
            INT,
        );
        pb.bind(
            SimpleIdentifier::new("b").into(),
            StringLiteral::new("hello").into(),
            STRING,
        );
        pb.bind(
            CompoundIdentifier::from_two_str("c", "d").into(),
            IntegerLiteral::new("2").into(),
            INT,
        );
        assert_eq!(
            RowLiteral::new(vec![
                IntegerLiteral::new("1").into(),
                StringLiteral::new("hello").into(),
                RowLiteral::new(vec![IntegerLiteral::new("2").into()]).into()
            ]),
            pb.build_literal()
        );
        assert_eq!(
            SQLRowType::default()
                .with_str("a", SQLBaseType::BigInt.into())
                .with_str("b", SQLBaseType::VarChar.into())
                .with_str(
                    "c",
                    SQLRowType::default()
                        .with_str("d", SQLBaseType::BigInt.into())
                        .into()
                ),
            pb.build_sql_type().unwrap()
        );
        assert_eq!(
            Struct::default()
                .with_str("a", INT)
                .with_str("b", STRING)
                .with_str("c", Struct::default().with_str("d", INT).into()),
            pb.build_hamelin_type()
        );
    }

    /// In this test, we'll setup a nested hierarchy, like in the previous test. But, we'll also insert
    /// something that shadows an entire portion of the nested hierarchy with a simple string, and
    /// ensure the sub-hierarchy gets removed as expected
    #[test]
    pub fn test_shadow() {
        let mut pb = ProjectionBuilder::default();
        pb.bind(
            SimpleIdentifier::new("a").into(),
            IntegerLiteral::new("1").into(),
            INT,
        );
        pb.bind(
            SimpleIdentifier::new("b").into(),
            StringLiteral::new("hello").into(),
            STRING,
        );
        pb.bind(
            CompoundIdentifier::from_two_str("c", "d").into(),
            IntegerLiteral::new("2").into(),
            INT,
        );
        pb.bind(
            CompoundIdentifier::from_two_str("c", "e").into(),
            IntegerLiteral::new("3").into(),
            INT,
        );
        pb.bind(
            SimpleIdentifier::new("c").into(),
            StringLiteral::new("world").into(),
            STRING,
        );
        assert_eq!(
            RowLiteral::new(vec![
                IntegerLiteral::new("1").into(),
                StringLiteral::new("hello").into(),
                StringLiteral::new("world").into()
            ]),
            pb.build_literal()
        );
        assert_eq!(
            SQLRowType::default()
                .with_str("a", SQLBaseType::BigInt.into())
                .with_str("b", SQLBaseType::VarChar.into())
                .with_str("c", SQLBaseType::VarChar.into()),
            pb.build_sql_type().unwrap()
        );
        assert_eq!(
            Struct::default()
                .with_str("a", INT)
                .with_str("b", STRING)
                .with_str("c", STRING),
            pb.build_hamelin_type()
        );
    }

    #[test]
    pub fn test_expand() {
        let mut pb = ProjectionBuilder::default();
        pb.bind(
            SimpleIdentifier::new("a").into(),
            IntegerLiteral::new("1").into(),
            INT,
        );
        pb.bind(
            SimpleIdentifier::new("b").into(),
            StringLiteral::new("hello").into(),
            STRING,
        );

        // First, expand to a larger, covering type.
        let expanded_struct = Struct::default()
            .with_str("a", INT)
            .with_str("b", STRING)
            .with_str("c", INT);
        let expanded = pb.expand(expanded_struct.clone()).unwrap();
        assert_eq!(
            RowLiteral::new(vec![
                IntegerLiteral::new("1").into(),
                StringLiteral::new("hello").into(),
                NullLiteral::default().into()
            ]),
            expanded.build_literal()
        );
        assert_eq!(
            SQLRowType::default()
                .with_str("a", SQLBaseType::BigInt.into())
                .with_str("b", SQLBaseType::VarChar.into())
                .with_str("c", SQLBaseType::BigInt.into()),
            expanded.build_sql_type().unwrap()
        );
        assert_eq!(expanded_struct, expanded.clone().build_hamelin_type());

        // Now "contract" to a pure subtype of this type.
        let contracted_struct = Struct::default().with_str("b", STRING).with_str("c", INT);
        let contracted = expanded.expand(contracted_struct.clone()).unwrap();
        assert_eq!(
            RowLiteral::new(vec![
                StringLiteral::new("hello").into(),
                NullLiteral::default().into()
            ]),
            contracted.build_literal()
        );
        assert_eq!(
            SQLRowType::default()
                .with_str("b", SQLBaseType::VarChar.into())
                .with_str("c", SQLBaseType::BigInt.into()),
            contracted.build_sql_type().unwrap()
        );
        assert_eq!(contracted_struct, contracted.build_hamelin_type());

        // Now "expand" to a partially overlapping type, this time putting a new value at the beginning.
        let overlapping_struct = Struct::default().with_str("new", INT).with_str("b", STRING);
        let overlapping = pb.expand(overlapping_struct.clone()).unwrap();
        assert_eq!(
            RowLiteral::new(vec![
                NullLiteral::default().into(),
                StringLiteral::new("hello").into(),
            ]),
            overlapping.build_literal()
        );
        assert_eq!(
            SQLRowType::default()
                .with_str("new", SQLBaseType::BigInt.into())
                .with_str("b", SQLBaseType::VarChar.into()),
            overlapping.build_sql_type().unwrap()
        );
        assert_eq!(overlapping_struct, overlapping.build_hamelin_type());
    }
}