hamelin_lib 0.11.1

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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
use crate::write_utils::{maybe_newline, maybe_pad, newline_or_space, Indent};
use std::fmt::{Display, Formatter};
use std::rc::Rc;

use antlr_rust::token::Token;
use antlr_rust::tree::ParseTree;
use derive_more::{From, TryUnwrap};
use ordermap::OrderMap;

use crate::antlr::trinotypeparser::{
    BaseContextAttrs, ParameterizedContextAttrs, RowContextAttrs, TrinotypeContextAll,
    TypeParameterContextAttrs,
};
use crate::err::{TranslationError, TranslationErrors};
use crate::sql::expression::identifier::SimpleIdentifier;

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SQLBaseType {
    TinyInt,
    SmallInt,
    Integer,
    BigInt,
    Real,
    Double,
    Char,
    VarChar,
    Json,
    Boolean,
    VarBinary,
    Date,
    Time,
    TimeStamp,
    TimeStampTz,
    IntervalDayToSecond,
    IntervalYearToMonth,
    TimeTz,
    IpAddress,
    Unknown,
}

impl SQLBaseType {
    pub fn fmt_indented(&self, f: &mut Formatter<'_>, _indent: Indent) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                SQLBaseType::TinyInt => "TINYINT",
                SQLBaseType::SmallInt => "SMALLINT",
                SQLBaseType::Integer => "INTEGER",
                SQLBaseType::BigInt => "BIGINT",
                SQLBaseType::Real => "REAL",
                SQLBaseType::Double => "DOUBLE",
                SQLBaseType::Char => "CHAR",
                SQLBaseType::VarChar => "VARCHAR",
                SQLBaseType::Json => "JSON",
                SQLBaseType::Boolean => "BOOLEAN",
                SQLBaseType::VarBinary => "VARBINARY",
                SQLBaseType::Date => "DATE",
                SQLBaseType::Time => "TIME",
                SQLBaseType::TimeStamp => "TIMESTAMP",
                SQLBaseType::TimeStampTz => "TIMESTAMP WITH TIME ZONE",
                SQLBaseType::IntervalDayToSecond => "INTERVAL DAY TO SECOND",
                SQLBaseType::IntervalYearToMonth => "INTERVAL YEAR TO MONTH",
                SQLBaseType::TimeTz => "TIME WITH TIME ZONE",
                SQLBaseType::Unknown => "UNKNOWN",
                SQLBaseType::IpAddress => "IPADDRESS",
            }
        )
    }
}

impl Display for SQLBaseType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, Indent::default())
    }
}

#[derive(Clone, Debug, From, TryUnwrap, PartialEq, Eq)]
pub enum SQLType {
    SQLAnonRowType(SQLAnonRowType),
    SQLArrayType(SQLArrayType),
    SQLBaseType(SQLBaseType),
    SQLCharType(SQLCharType),
    SQLDecimalType(SQLDecimalType),
    SQLMapType(SQLMapType),
    SQLRowType(SQLRowType),
    SQLTimeType(SQLTimeType),
    SQLTimestampType(SQLTimestampType),
    SQLTimestampTzType(SQLTimestampTzType),
    SQLVarBinaryType(SQLVarBinaryType),
    SQLVarcharType(SQLVarcharType),
}

impl SQLType {
    pub fn from_database_reflection(
        ctx: Rc<TrinotypeContextAll<'static>>,
    ) -> Result<SQLType, TranslationErrors> {
        match ctx.as_ref() {
            TrinotypeContextAll::BaseContext(ctx) => {
                let ident_str = TranslationErrors::expect(ctx, ctx.identifier())?
                    .get_text()
                    .to_uppercase();
                let base_type = match ident_str.as_str() {
                    "TINYINT" => SQLBaseType::TinyInt,
                    "SMALLINT" => SQLBaseType::SmallInt,
                    "INTEGER" => SQLBaseType::Integer,
                    "BIGINT" => SQLBaseType::BigInt,
                    "REAL" => SQLBaseType::Real,
                    "DOUBLE" => SQLBaseType::Double,
                    "CHAR" => SQLBaseType::Char,
                    "VARCHAR" => SQLBaseType::VarChar,
                    "JSON" => SQLBaseType::Json,
                    "BOOLEAN" => SQLBaseType::Boolean,
                    "VARBINARY" => SQLBaseType::VarBinary,
                    "DATE" => SQLBaseType::Date,
                    "TIME" | "TIME WITHOUT TIME ZONE" => SQLBaseType::Time,
                    "TIME WITH TIME ZONE" | "TIMETZ" => SQLBaseType::TimeTz,
                    "TIMESTAMP" | "TIMESTAMP WITHOUT TIME ZONE" => SQLBaseType::TimeStamp,
                    "TIMESTAMPTZ" | "TIMESTAMP WITH TIME ZONE" => SQLBaseType::TimeStampTz,
                    "INTERVAL DAY TO SECOND" => SQLBaseType::IntervalDayToSecond,
                    "INTERVAL YEAR TO MONTH" => SQLBaseType::IntervalYearToMonth,
                    "UNKNOWN" => SQLBaseType::Unknown,
                    _ => return TranslationError::msg(ctx, "Unknown base type").single_result(),
                };
                Ok(base_type.into())
            }
            TrinotypeContextAll::ParameterizedContext(ctx) => {
                let ident_str = TranslationErrors::expect(ctx, ctx.identifier())?
                    .get_text()
                    .to_uppercase();
                let base_type: SQLType = match ident_str.as_str() {
                    "DECIMAL" => {
                        let precision = TranslationErrors::expect(ctx, ctx.typeParameter(0))?
                            .get_text()
                            .parse::<u64>()
                            .map_err(|_| {
                                TranslationError::msg(ctx, "Failed to parse precision").single()
                            })?;
                        let scale = TranslationErrors::expect(ctx, ctx.typeParameter(1))?
                            .get_text()
                            .parse::<u64>()
                            .map_err(|_| {
                                TranslationError::msg(ctx, "Failed to parse scale").single()
                            })?;
                        SQLDecimalType::new(precision, scale).into()
                    }
                    "CHAR" => {
                        let length = TranslationErrors::expect(ctx, ctx.typeParameter(0))?
                            .get_text()
                            .parse::<u64>()
                            .map_err(|_| {
                                TranslationError::msg(ctx, "Failed to parse length").single()
                            })?;
                        SQLCharType::new(length).into()
                    }
                    "VARCHAR" => {
                        let length = TranslationErrors::expect(ctx, ctx.typeParameter(0))?
                            .get_text()
                            .parse::<u64>()
                            .map_err(|_| {
                                TranslationError::msg(ctx, "Failed to parse length").single()
                            })?;
                        SQLVarcharType::new(length).into()
                    }
                    "VARBINARY" => {
                        let length = TranslationErrors::expect(ctx, ctx.typeParameter(0))?
                            .get_text()
                            .parse::<u64>()
                            .map_err(|_| {
                                TranslationError::msg(ctx, "Failed to parse length").single()
                            })?;
                        SQLVarBinaryType::new(length).into()
                    }
                    "ARRAY" => {
                        let element_type = Self::from_database_reflection(
                            TranslationErrors::expect(ctx, ctx.typeParameter(0))?
                                .trinotype()
                                .ok_or_else(|| {
                                    TranslationError::msg(ctx, "Expected a subtype for array()")
                                        .single()
                                })?,
                        )?;
                        SQLArrayType::new(element_type).into()
                    }
                    "MAP" => {
                        let key_type = Self::from_database_reflection(
                            TranslationErrors::expect(ctx, ctx.typeParameter(0))?
                                .trinotype()
                                .ok_or_else(|| {
                                    TranslationError::msg(ctx, "Expected a subtype for map()")
                                        .single()
                                })?,
                        )?;
                        let value_type = Self::from_database_reflection(
                            TranslationErrors::expect(ctx, ctx.typeParameter(1))?
                                .trinotype()
                                .ok_or_else(|| {
                                    TranslationError::msg(ctx, "Expected a subtype for map()")
                                        .single()
                                })?,
                        )?;
                        SQLMapType::new(key_type, value_type).into()
                    }
                    "ROW" => {
                        let elements = ctx
                            .typeParameter_all()
                            .iter()
                            .map(|tp| {
                                tp.trinotype()
                                    .ok_or_else(|| {
                                        TranslationError::msg(ctx, "Expected a subtype for row()")
                                            .single()
                                    })
                                    .and_then(|tp| Self::from_database_reflection(tp))
                            })
                            .collect::<Result<Vec<_>, TranslationErrors>>()?;
                        SQLAnonRowType::new(elements).into()
                    }
                    "TIMESTAMP" => {
                        let precision = TranslationErrors::expect(ctx, ctx.typeParameter(0))?
                            .get_text()
                            .parse::<u64>()
                            .map_err(|_| {
                                TranslationError::msg(ctx, "Failed to parse precision").single()
                            })?;
                        SQLTimestampType::new(precision).into()
                    }
                    "TIMESTAMPTZ" => {
                        let precision = TranslationErrors::expect(ctx, ctx.typeParameter(0))?
                            .get_text()
                            .parse::<u64>()
                            .map_err(|_| {
                                TranslationError::msg(ctx, "Failed to parse precision").single()
                            })?;
                        SQLTimestampTzType::new(precision).into()
                    }
                    _ => {
                        return TranslationError::msg(ctx, "Unknown parameterized type")
                            .single_result();
                    }
                };
                Ok(base_type.into())
            }
            TrinotypeContextAll::TimestampContext(_) => Ok(SQLBaseType::TimeStamp.into()),
            TrinotypeContextAll::TimestampPrecisionContext(ctx) => {
                let precision = TranslationErrors::expect(ctx, ctx.precision.clone())?
                    .get_text()
                    .parse::<u64>()
                    .map_err(|_| {
                        TranslationError::msg(ctx, "Failed to parse precision").single()
                    })?;
                Ok(SQLTimestampType::new(precision).into())
            }
            TrinotypeContextAll::TimestamptzContext(_) => Ok(SQLBaseType::TimeStampTz.into()),
            TrinotypeContextAll::TimestamptzPrecisionContext(ctx) => {
                let precision = TranslationErrors::expect(ctx, ctx.precision.clone())?
                    .get_text()
                    .parse::<u64>()
                    .map_err(|_| {
                        TranslationError::msg(ctx, "Failed to parse precision").single()
                    })?;
                Ok(SQLTimestampTzType::new(precision).into())
            }
            TrinotypeContextAll::TimeContext(_) => Ok(SQLBaseType::Time.into()),
            TrinotypeContextAll::TimePrecisionContext(ctx) => {
                let precision = TranslationErrors::expect(ctx, ctx.precision.clone())?
                    .get_text()
                    .parse::<u64>()
                    .map_err(|_| {
                        TranslationError::msg(ctx, "Failed to parse precision").single()
                    })?;
                Ok(SQLTimeType::new(precision).into())
            }
            TrinotypeContextAll::TimetzContext(_) => Ok(SQLBaseType::TimeTz.into()),
            TrinotypeContextAll::IntervalYearToMonthContext(_) => {
                Ok(SQLBaseType::IntervalYearToMonth.into())
            }
            TrinotypeContextAll::IntervalDayToSecondContext(_) => {
                Ok(SQLBaseType::IntervalDayToSecond.into())
            }
            TrinotypeContextAll::RowContext(ctx) => {
                let identifiers = ctx.identifier_all().clone();
                let types = ctx.trinotype_all().clone();
                let mut bindings = OrderMap::new();
                for (identifier, ty) in identifiers.iter().zip(types.iter()) {
                    let key = SimpleIdentifier::new(&identifier.get_text());
                    let value = Self::from_database_reflection(ty.clone())?;
                    bindings.insert(key, value);
                }
                Ok(SQLRowType::new(bindings).into())
            }
            TrinotypeContextAll::Error(ctx) => {
                TranslationError::msg(ctx, "trino type parse error").single_result()
            }
        }
    }

    pub fn fmt_indented(&self, f: &mut Formatter<'_>, indentation: Indent) -> std::fmt::Result {
        match self {
            SQLType::SQLAnonRowType(t) => t.fmt_indented(f, indentation),
            SQLType::SQLArrayType(t) => t.fmt_indented(f, indentation),
            SQLType::SQLBaseType(t) => t.fmt_indented(f, indentation),
            SQLType::SQLCharType(t) => t.fmt_indented(f, indentation),
            SQLType::SQLDecimalType(t) => t.fmt_indented(f, indentation),
            SQLType::SQLMapType(t) => t.fmt_indented(f, indentation),
            SQLType::SQLRowType(t) => t.fmt_indented(f, indentation),
            SQLType::SQLTimeType(t) => t.fmt_indented(f, indentation),
            SQLType::SQLTimestampType(t) => t.fmt_indented(f, indentation),
            SQLType::SQLTimestampTzType(t) => t.fmt_indented(f, indentation),
            SQLType::SQLVarBinaryType(t) => t.fmt_indented(f, indentation),
            SQLType::SQLVarcharType(t) => t.fmt_indented(f, indentation),
        }
    }

    pub fn subfields(&self) -> usize {
        match self {
            SQLType::SQLBaseType(_) => 0,
            SQLType::SQLCharType(_) => 0,
            SQLType::SQLDecimalType(_) => 0,
            SQLType::SQLTimeType(_) => 0,
            SQLType::SQLTimestampType(_) => 0,
            SQLType::SQLTimestampTzType(_) => 0,
            SQLType::SQLVarBinaryType(_) => 0,
            SQLType::SQLVarcharType(_) => 0,
            SQLType::SQLArrayType(t) => t.subfields(),
            SQLType::SQLMapType(t) => t.subfields(),
            SQLType::SQLRowType(t) => t.subfields(),
            SQLType::SQLAnonRowType(t) => t.subfields(),
        }
    }
}

impl Display for SQLType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, Indent::default())
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SQLAnonRowType {
    pub elements: Vec<SQLType>,
}

impl SQLAnonRowType {
    pub fn new(elements: Vec<SQLType>) -> Self {
        Self { elements }
    }

    pub fn fmt_indented(&self, f: &mut Formatter<'_>, indentation: Indent) -> std::fmt::Result {
        write!(f, "ROW(")?;
        maybe_newline(f, indentation)?;
        for (i, element) in self.elements.iter().enumerate() {
            maybe_pad(f, indentation.nested())?;
            element.fmt_indented(f, indentation.nested())?;
            if i < self.elements.len() - 1 {
                write!(f, ",")?;
                newline_or_space(f, indentation)?;
            }
        }
        maybe_newline(f, indentation)?;
        maybe_pad(f, indentation)?;
        write!(f, ")")
    }

    fn subfields(&self) -> usize {
        self.elements
            .iter()
            .map(|element| element.subfields() + 1)
            .sum()
    }
}

impl Display for SQLAnonRowType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, Indent::default())
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SQLArrayType {
    pub element_type: Box<SQLType>,
}

impl SQLArrayType {
    pub fn new(element_type: SQLType) -> Self {
        Self {
            element_type: Box::new(element_type),
        }
    }

    pub fn fmt_indented(&self, f: &mut Formatter<'_>, indentation: Indent) -> std::fmt::Result {
        write!(f, "ARRAY(")?;
        self.element_type.fmt_indented(f, indentation.nested())?;
        write!(f, ")")
    }

    fn subfields(&self) -> usize {
        1 + self.element_type.subfields()
    }
}

impl Display for SQLArrayType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, Indent::default())
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SQLCharType {
    pub length: u64,
}

impl SQLCharType {
    pub fn new(length: u64) -> Self {
        Self { length }
    }

    pub fn fmt_indented(&self, f: &mut Formatter<'_>, _indentation: Indent) -> std::fmt::Result {
        write!(f, "CHAR({})", self.length)
    }
}

impl Display for SQLCharType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, Indent::default())
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SQLDecimalType {
    pub precision: u64,
    pub scale: u64,
}

impl SQLDecimalType {
    pub fn new(precision: u64, scale: u64) -> Self {
        Self { precision, scale }
    }

    pub fn fmt_indented(&self, f: &mut Formatter<'_>, _indentation: Indent) -> std::fmt::Result {
        write!(f, "DECIMAL({}, {})", self.precision, self.scale)
    }
}

impl Display for SQLDecimalType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, Indent::default())
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SQLMapType {
    pub key_type: Box<SQLType>,
    pub value_type: Box<SQLType>,
}

impl SQLMapType {
    pub fn new(key_type: SQLType, value_type: SQLType) -> Self {
        Self {
            key_type: Box::new(key_type),
            value_type: Box::new(value_type),
        }
    }

    pub fn fmt_indented(&self, f: &mut Formatter<'_>, _indentation: Indent) -> std::fmt::Result {
        write!(f, "MAP({}, {})", self.key_type, self.value_type)
    }

    fn subfields(&self) -> usize {
        2 + self.key_type.subfields() + self.value_type.subfields()
    }
}

impl Display for SQLMapType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, Indent::default())
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SQLRowType {
    pub bindings: OrderMap<SimpleIdentifier, SQLType>,
}

impl SQLRowType {
    pub fn new(bindings: OrderMap<SimpleIdentifier, SQLType>) -> Self {
        Self { bindings }
    }

    pub fn fmt_indented(&self, f: &mut Formatter<'_>, indentation: Indent) -> std::fmt::Result {
        let indentation = match indentation {
            Indent::Pretty { .. } if self.subfields() > 3 => indentation,
            _ => Indent::Compact,
        };

        write!(f, "ROW(")?;
        maybe_newline(f, indentation)?;
        for (i, (k, v)) in self.bindings.iter().enumerate() {
            maybe_pad(f, indentation.nested())?;
            k.fmt_indented(f, indentation.nested())?;
            write!(f, " ")?;
            v.fmt_indented(f, indentation.nested())?;
            if i < self.bindings.len() - 1 {
                write!(f, ",")?;
                newline_or_space(f, indentation)?;
            }
        }
        maybe_newline(f, indentation)?;
        maybe_pad(f, indentation)?;

        write!(f, ")")
    }

    pub fn with_str(&self, identifier: &str, ty: SQLType) -> Self {
        self.with(SimpleIdentifier::new(identifier), ty)
    }

    pub fn with(&self, identifier: SimpleIdentifier, ty: SQLType) -> Self {
        let mut bindings = self.bindings.clone();
        bindings.insert(identifier, ty);
        Self { bindings }
    }

    fn subfields(&self) -> usize {
        self.bindings.values().map(|ty| ty.subfields() + 1).sum()
    }
}

impl Display for SQLRowType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, Indent::default())
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SQLTimeType {
    pub precision: u64,
}

impl SQLTimeType {
    pub fn new(precision: u64) -> Self {
        Self { precision }
    }

    pub fn fmt_indented(&self, f: &mut Formatter<'_>, _indentation: Indent) -> std::fmt::Result {
        write!(f, "TIME({})", self.precision)
    }
}

impl Display for SQLTimeType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, Indent::default())
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SQLTimestampType {
    pub precision: u64,
}

impl SQLTimestampType {
    pub fn new(precision: u64) -> Self {
        Self { precision }
    }

    pub fn fmt_indented(&self, f: &mut Formatter<'_>, _indentation: Indent) -> std::fmt::Result {
        write!(f, "TIMESTAMP({})", self.precision)
    }
}

impl Display for SQLTimestampType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, Indent::default())
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SQLTimestampTzType {
    pub precision: u64,
}

impl SQLTimestampTzType {
    pub fn new(precision: u64) -> Self {
        Self { precision }
    }

    pub fn fmt_indented(&self, f: &mut Formatter<'_>, _indentation: Indent) -> std::fmt::Result {
        write!(f, "TIMESTAMP({}) WITH TIME ZONE", self.precision)
    }
}

impl Display for SQLTimestampTzType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, Indent::default())
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SQLVarBinaryType {
    pub length: u64,
}

impl SQLVarBinaryType {
    pub fn new(length: u64) -> Self {
        Self { length }
    }

    pub fn fmt_indented(&self, f: &mut Formatter<'_>, _indentation: Indent) -> std::fmt::Result {
        write!(f, "VARBINARY({})", self.length)
    }
}

impl Display for SQLVarBinaryType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, Indent::default())
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SQLVarcharType {
    pub length: u64,
}

impl SQLVarcharType {
    pub fn new(length: u64) -> Self {
        Self { length }
    }

    pub fn fmt_indented(&self, f: &mut Formatter<'_>, _indentation: Indent) -> std::fmt::Result {
        write!(f, "VARCHAR({})", self.length)
    }
}

impl Display for SQLVarcharType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, Indent::default())
    }
}

#[cfg(test)]
mod test {
    use crate::types::decimal_type::Decimal;
    use crate::types::struct_type::Struct;
    use crate::types::{Type, DOUBLE, STRING};

    use rstest::rstest;

    use crate::parser::make_trinotype_parser_from_input;

    use super::*;

    #[rstest]
    #[case("TINYINT")]
    #[case("array(row(time_off array(row(description varchar, interval varchar))))")]
    #[case("array(row(time_off array(row(description varchar, interval varchar))))")]
    #[case("array(row(time_off array(row(time varchar, timestamp varchar))))")]
    #[case("array(row(time_off array(row(description varchar, schnitzel timestamp(5)))))")]
    #[case(
        r#"array(
            row(
                account_expiration_time timestamp(6),
                account_lockout_time timestamp(6),
                account_type varchar,
                attribute_cloud_availability_zone varchar,
                attribute_cloud_environment varchar,
                attribute_cloud_project varchar,
                attribute_cloud_vpc varchar,
                attribute_creation_time timestamp(6),
                attribute_labels map(varchar, varchar),
                attribute_last_update_time timestamp(6),
                attribute_permissions array(
                    row(
                        description varchar,
                        name varchar, type varchar
                    )
                ),
                attribute_roles array(
                    row(
                        description varchar,
                        name varchar,
                        type varchar
                    )
                ),
                company_name varchar,
                department array(varchar),
                email_addresses array(varchar),
                employee_id varchar,
                first_name varchar,
                first_seen_time timestamp(6),
                group_identifiers array(varchar),
                groupid varchar,
                hire_date timestamp(6),
                last_bad_password_attempt_time timestamp(6),
                last_login_time timestamp(6),
                last_name varchar,
                last_password_change_time timestamp(6),
                middle_name varchar,
                office_address_city varchar,
                office_address_country_or_region varchar,
                office_address_desk_name varchar,
                office_address_floor_name varchar,
                office_address_name varchar,
                office_address_region_coordinates varchar,
                office_address_region_latitude double,
                office_address_region_longitude double,
                office_address_state varchar,
                password_expiration_time timestamp(6),
                personal_address_city varchar,
                personal_address_country_or_region varchar,
                personal_address_desk_name varchar,
                personal_address_floor_name varchar,
                personal_address_name varchar,
                personal_address_region_coordinates varchar,
                personal_address_region_latitude double,
                personal_address_region_longitude double,
                personal_address_state varchar,
                phone_numbers array(varchar),
                product_object_id varchar,
                role_description varchar,
                role_name varchar,
                termination_date timestamp(6),
                time_off array(row(
                    description varchar,
                    interval varchar
                )),
                title varchar,
                user_authentication_status varchar,
                user_display_name varchar,
                user_role varchar,
                userid varchar,
                windows_sid varchar
            ))"#
    )]
    pub fn trino_type_parse_ensure_no_errors(#[case] trino_type: String) {
        let ctx = make_trinotype_parser_from_input(trino_type)
            .trinotype()
            .unwrap();

        Type::from_sql(SQLType::from_database_reflection(ctx).unwrap()).unwrap();
    }

    #[test]
    pub fn test_nested_row() -> anyhow::Result<()> {
        let problem_type = r#"row(
            device varchar,
            geographicalContext row(
                city varchar,
                country varchar,
                geolocation row(
                    lat double, long double
                ),
                postalCode varchar,
                state varchar
            ),
            id varchar,
            ipAddress varchar,
            userAgent row(
                browser varchar,
                os varchar,
                rawUserAgent varchar
            ),
            zone varchar,
            currency decimal(38, 2)
        )
    "#;

        let ctx = make_trinotype_parser_from_input(problem_type.to_owned())
            .trinotype()
            .unwrap();

        assert_eq!(
            Type::from_sql(SQLType::from_database_reflection(ctx).unwrap()).unwrap(),
            Struct::default()
                .with_str("device", STRING)
                .with_str(
                    "geographicalContext",
                    Struct::default()
                        .with_str("city", STRING)
                        .with_str("country", STRING)
                        .with_str(
                            "geolocation",
                            Struct::default()
                                .with_str("lat", DOUBLE)
                                .with_str("long", DOUBLE)
                                .into()
                        )
                        .with_str("postalCode", STRING)
                        .with_str("state", STRING)
                        .into()
                )
                .with_str("id", STRING)
                .with_str("ipAddress", STRING)
                .with_str(
                    "userAgent",
                    Struct::default()
                        .with_str("browser", STRING)
                        .with_str("os", STRING)
                        .with_str("rawUserAgent", STRING)
                        .into()
                )
                .with_str("zone", STRING)
                .with_str("currency", Decimal::new(38, 2)?.into())
                .into()
        );

        Ok(())
    }
}