icydb-core 0.144.7

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
//! Module: db::sql::parser::model
//! Responsibility: reduced SQL parser-owned statement and projection model types.
//! Does not own: cursor movement, clause sequencing, or execution semantics.
//! Boundary: defines the parser output contracts re-exported by the parser root.

use crate::{
    db::{
        query::plan::{AggregateKind, expr::Function},
        sql::identifier::split_qualified_identifier,
    },
    value::Value,
};

///
/// SqlStatement
///
/// Reduced SQL statement contract accepted by the current parser baseline.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum SqlStatement {
    Select(SqlSelectStatement),
    Delete(SqlDeleteStatement),
    Insert(SqlInsertStatement),
    Update(SqlUpdateStatement),
    Explain(SqlExplainStatement),
    Describe(SqlDescribeStatement),
    ShowIndexes(SqlShowIndexesStatement),
    ShowColumns(SqlShowColumnsStatement),
    ShowEntities(SqlShowEntitiesStatement),
}

///
/// SqlProjection
///
/// Projection shape parsed from one `SELECT` statement.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum SqlProjection {
    All,
    Items(Vec<SqlSelectItem>),
}

impl SqlProjection {
    /// Return whether this parsed projection already stays within the local
    /// scalar normalization fast path.
    #[must_use]
    pub(in crate::db) fn is_already_local_scalar(&self) -> bool {
        match self {
            Self::All => true,
            Self::Items(items) => items.iter().all(SqlSelectItem::is_already_local_projection),
        }
    }
}

///
/// SqlSelectItem
///
/// One projection item parsed from one `SELECT` list.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum SqlSelectItem {
    Field(String),
    Aggregate(SqlAggregateCall),
    Expr(SqlExpr),
}

impl SqlSelectItem {
    /// Return whether one parsed select item contains any aggregate leaf.
    #[must_use]
    pub(crate) fn contains_aggregate(&self) -> bool {
        match self {
            Self::Field(_) => false,
            Self::Aggregate(_) => true,
            Self::Expr(expr) => expr.contains_aggregate(),
        }
    }

    /// Return whether this parsed select item already stays within the local
    /// projection normalization fast path.
    #[must_use]
    pub(in crate::db) fn is_already_local_projection(&self) -> bool {
        match self {
            Self::Field(field) => SqlExpr::identifier_is_already_local(field.as_str()),
            Self::Aggregate(aggregate) => aggregate.is_already_local_scalar(),
            Self::Expr(_) => false,
        }
    }
}

///
/// SqlExprUnaryOp
///
/// Parser-owned unary SQL expression operator taxonomy.
/// This keeps searched-CASE conditions and future scalar-expression widening
/// on one frontend boundary before planner lowering maps onto `Expr`.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SqlExprUnaryOp {
    Not,
}

///
/// SqlExprBinaryOp
///
/// Parser-owned binary SQL expression operator taxonomy.
/// This unifies arithmetic, comparison, and boolean operators on the SQL-side
/// expression boundary instead of scattering clause-local operator enums.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SqlExprBinaryOp {
    Or,
    And,
    Eq,
    Ne,
    Lt,
    Lte,
    Gt,
    Gte,
    Add,
    Sub,
    Mul,
    Div,
}

///
/// SqlCaseArm
///
/// Parser-owned searched-CASE branch pairing one boolean condition with the
/// value expression selected when that condition evaluates true.
/// Missing ELSE stays optional at this boundary so lowering can canonicalize
/// it to one explicit planner-owned NULL fallback.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SqlCaseArm {
    pub(crate) condition: SqlExpr,
    pub(crate) result: SqlExpr,
}

///
/// SqlExpr
///
/// Parser-owned SQL scalar expression tree shared across existing scalar
/// positions before planner lowering maps onto canonical planner expressions.
/// This keeps clause-specific parsing models from becoming the semantic owner
/// for CASE or future scalar-expression widening.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum SqlExpr {
    Field(String),
    FieldPath {
        root: String,
        segments: Vec<String>,
    },
    Aggregate(SqlAggregateCall),
    Literal(Value),
    Param {
        index: usize,
    },
    Membership {
        expr: Box<Self>,
        values: Vec<Value>,
        negated: bool,
    },
    NullTest {
        expr: Box<Self>,
        negated: bool,
    },
    Like {
        expr: Box<Self>,
        pattern: String,
        negated: bool,
        casefold: bool,
    },
    FunctionCall {
        function: SqlScalarFunction,
        args: Vec<Self>,
    },
    Unary {
        op: SqlExprUnaryOp,
        expr: Box<Self>,
    },
    Binary {
        op: SqlExprBinaryOp,
        left: Box<Self>,
        right: Box<Self>,
    },
    Case {
        arms: Vec<SqlCaseArm>,
        else_expr: Option<Box<Self>>,
    },
}

impl SqlExpr {
    /// Convert one parsed select item into the shared SQL expression tree.
    #[must_use]
    pub(crate) fn from_select_item(item: &SqlSelectItem) -> Self {
        match item {
            SqlSelectItem::Field(field) => Self::from_field_identifier(field.clone()),
            SqlSelectItem::Aggregate(aggregate) => Self::Aggregate(aggregate.clone()),
            SqlSelectItem::Expr(expr) => expr.clone(),
        }
    }

    /// Convert one possibly-dotted SQL identifier into the parser-owned field
    /// leaf that preserves nested path shape.
    #[must_use]
    pub(crate) fn from_field_identifier(identifier: String) -> Self {
        let mut parts = identifier.split('.');
        let Some(root) = parts.next() else {
            return Self::Field(identifier);
        };

        let segments = parts.map(str::to_string).collect::<Vec<_>>();
        if segments.is_empty() {
            return Self::Field(root.to_string());
        }

        Self::FieldPath {
            root: root.to_string(),
            segments,
        }
    }

    /// Return true when one SQL expression tree contains any aggregate leaf.
    #[must_use]
    pub(crate) fn contains_aggregate(&self) -> bool {
        self.any_tree_expr(&mut |expr| matches!(expr, Self::Aggregate(_)))
    }

    /// Return whether this SQL expression already fits the local scalar
    /// normalization fast path without identifier rescoping.
    #[must_use]
    pub(in crate::db) fn is_already_local_scalar(&self) -> bool {
        self.all_tree_expr(&mut |expr| match expr {
            Self::Field(field) => Self::identifier_is_already_local(field.as_str()),
            Self::Literal(_)
            | Self::Param { .. }
            | Self::NullTest { .. }
            | Self::Like { .. }
            | Self::Unary { .. }
            | Self::Binary { .. } => true,
            Self::Membership { values, .. } => values
                .iter()
                .all(|value| !matches!(value, Value::List(_) | Value::Map(_))),
            Self::FieldPath { .. }
            | Self::Aggregate(_)
            | Self::FunctionCall { .. }
            | Self::Case { .. } => false,
        })
    }

    /// Return whether every field leaf in this expression is already a local
    /// bare identifier.
    #[must_use]
    pub(in crate::db) fn fields_are_already_local(&self) -> bool {
        self.all_tree_expr(&mut |expr| match expr {
            Self::Field(field) => Self::identifier_is_already_local(field.as_str()),
            Self::FieldPath { .. } => false,
            Self::Aggregate(aggregate) => aggregate.is_already_local_scalar(),
            Self::Literal(_)
            | Self::Param { .. }
            | Self::Membership { .. }
            | Self::NullTest { .. }
            | Self::Like { .. }
            | Self::FunctionCall { .. }
            | Self::Unary { .. }
            | Self::Binary { .. }
            | Self::Case { .. } => true,
        })
    }

    /// Return whether this SQL expression tree contains any searched `CASE`
    /// arm with an omitted `ELSE`.
    #[must_use]
    pub(in crate::db) fn contains_omitted_else_case(&self) -> bool {
        self.any_tree_expr(
            &mut |expr| matches!(expr, Self::Case { else_expr, .. } if else_expr.is_none()),
        )
    }

    /// Visit every SQL expression node through the owner-local parser
    /// traversal contract.
    pub(in crate::db) fn for_each_tree_expr(&self, visit: &mut impl FnMut(&Self)) {
        visit(self);

        match self {
            Self::Field(_)
            | Self::FieldPath { .. }
            | Self::Aggregate(_)
            | Self::Literal(_)
            | Self::Param { .. } => {}
            Self::Membership { expr, .. }
            | Self::NullTest { expr, .. }
            | Self::Like { expr, .. }
            | Self::Unary { expr, .. } => expr.for_each_tree_expr(visit),
            Self::FunctionCall { args, .. } => {
                for arg in args {
                    arg.for_each_tree_expr(visit);
                }
            }
            Self::Binary { left, right, .. } => {
                left.for_each_tree_expr(visit);
                right.for_each_tree_expr(visit);
            }
            Self::Case { arms, else_expr } => {
                for arm in arms {
                    arm.condition.for_each_tree_expr(visit);
                    arm.result.for_each_tree_expr(visit);
                }
                if let Some(else_expr) = else_expr.as_ref() {
                    else_expr.for_each_tree_expr(visit);
                }
            }
        }
    }

    /// Visit every aggregate leaf owned by this SQL expression tree through
    /// the canonical parser traversal contract.
    pub(in crate::db) fn for_each_tree_aggregate(&self, visit: &mut impl FnMut(&SqlAggregateCall)) {
        self.for_each_tree_expr(&mut |expr| {
            if let Self::Aggregate(aggregate) = expr {
                visit(aggregate);
            }
        });
    }

    // Local identifiers are already in the parser/planner leaf form and do
    // not need entity-scope reduction.
    fn identifier_is_already_local(identifier: &str) -> bool {
        split_qualified_identifier(identifier).is_none()
    }

    // Walk the whole SQL expression tree and return true as soon as one node
    // matches the supplied predicate. This keeps aggregate and omitted-ELSE
    // detection on one traversal shape instead of repeating the same tree walk.
    fn any_tree_expr(&self, predicate: &mut impl FnMut(&Self) -> bool) -> bool {
        if predicate(self) {
            return true;
        }

        match self {
            Self::Field(_)
            | Self::FieldPath { .. }
            | Self::Aggregate(_)
            | Self::Literal(_)
            | Self::Param { .. } => false,
            Self::Membership { expr, .. }
            | Self::NullTest { expr, .. }
            | Self::Like { expr, .. }
            | Self::Unary { expr, .. } => expr.any_tree_expr(predicate),
            Self::FunctionCall { args, .. } => args.iter().any(|arg| arg.any_tree_expr(predicate)),
            Self::Binary { left, right, .. } => {
                left.any_tree_expr(predicate) || right.any_tree_expr(predicate)
            }
            Self::Case { arms, else_expr } => {
                arms.iter().any(|arm| {
                    arm.condition.any_tree_expr(predicate) || arm.result.any_tree_expr(predicate)
                }) || else_expr
                    .as_ref()
                    .is_some_and(|else_expr| else_expr.any_tree_expr(predicate))
            }
        }
    }

    // Walk the whole SQL expression tree and require every visited node to
    // satisfy the supplied admission rule. This keeps the local-scalar and
    // local-field checks on one recursive traversal while still letting each
    // caller define its own leaf policy.
    fn all_tree_expr(&self, predicate: &mut impl FnMut(&Self) -> bool) -> bool {
        if !predicate(self) {
            return false;
        }

        match self {
            Self::Field(_)
            | Self::FieldPath { .. }
            | Self::Aggregate(_)
            | Self::Literal(_)
            | Self::Param { .. } => true,
            Self::Membership { expr, .. }
            | Self::NullTest { expr, .. }
            | Self::Like { expr, .. }
            | Self::Unary { expr, .. } => expr.all_tree_expr(predicate),
            Self::FunctionCall { args, .. } => args.iter().all(|arg| arg.all_tree_expr(predicate)),
            Self::Binary { left, right, .. } => {
                left.all_tree_expr(predicate) && right.all_tree_expr(predicate)
            }
            Self::Case { arms, else_expr } => {
                arms.iter().all(|arm| {
                    arm.condition.all_tree_expr(predicate) && arm.result.all_tree_expr(predicate)
                }) && else_expr
                    .as_ref()
                    .is_none_or(|else_expr| else_expr.all_tree_expr(predicate))
            }
        }
    }
}

/// SqlAggregateKind
///
/// Aggregate operator taxonomy accepted by the reduced parser.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SqlAggregateKind {
    Count,
    Sum,
    Avg,
    Min,
    Max,
}

impl SqlAggregateKind {
    /// Return whether this parsed aggregate kind admits `*` as its input.
    #[must_use]
    pub(crate) const fn supports_star_input(self) -> bool {
        matches!(self, Self::Count)
    }

    /// Return whether this parsed aggregate kind lowers one field input into
    /// the shared field-target aggregate shape.
    #[must_use]
    pub(in crate::db) const fn lowers_shared_field_target_shape(self) -> bool {
        !matches!(self, Self::Count)
    }

    /// Return the canonical planner aggregate kind for this parsed SQL kind.
    #[must_use]
    pub(in crate::db) const fn aggregate_kind(self) -> AggregateKind {
        match self {
            Self::Count => AggregateKind::Count,
            Self::Sum => AggregateKind::Sum,
            Self::Avg => AggregateKind::Avg,
            Self::Min => AggregateKind::Min,
            Self::Max => AggregateKind::Max,
        }
    }
}

///
/// SqlAggregateCall
///
/// Parsed aggregate call projection item.
/// `input = None` is only valid for `COUNT(*)`.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SqlAggregateCall {
    pub(crate) kind: SqlAggregateKind,
    pub(crate) input: Option<Box<SqlExpr>>,
    pub(crate) filter_expr: Option<Box<SqlExpr>>,
    pub(crate) distinct: bool,
}

impl SqlAggregateCall {
    /// Return whether this aggregate call already stays within the local
    /// aggregate input normalization fast path.
    #[must_use]
    pub(in crate::db) fn is_already_local_scalar(&self) -> bool {
        let input_is_local = self
            .input
            .as_deref()
            .is_none_or(SqlExpr::is_already_local_scalar);

        input_is_local
            && self
                .filter_expr
                .as_deref()
                .is_none_or(SqlExpr::is_already_local_scalar)
    }
}

///
/// SqlScalarFunction
///
/// Reduced scalar-function taxonomy accepted in parsed SQL expression position.
/// This remains intentionally narrow and only carries the supported scalar
/// function family that lowers into the shared planner expression surface.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[remain::sorted]
pub(crate) enum SqlScalarFunction {
    Abs,
    Cbrt,
    Ceiling,
    Coalesce,
    Contains,
    EndsWith,
    Exp,
    Floor,
    #[cfg_attr(not(test), allow(dead_code))]
    IsEmpty,
    #[cfg_attr(not(test), allow(dead_code))]
    IsMissing,
    #[cfg_attr(not(test), allow(dead_code))]
    IsNotEmpty,
    #[cfg_attr(not(test), allow(dead_code))]
    IsNotNull,
    #[cfg_attr(not(test), allow(dead_code))]
    IsNull,
    Left,
    Length,
    Ln,
    Log,
    Log2,
    Log10,
    Lower,
    Ltrim,
    Mod,
    NullIf,
    Position,
    Power,
    Replace,
    Right,
    Round,
    Rtrim,
    Sign,
    Sqrt,
    StartsWith,
    Substring,
    Trim,
    Trunc,
    Upper,
}

///
/// SqlScalarFunctionCallShape
///
/// Parser-owned call-shape family for one supported SQL scalar function.
/// This keeps parser dispatch on one enum-owned contract so projection, WHERE,
/// and ORDER parsing do not each re-derive the same function-family ladders.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SqlScalarFunctionCallShape {
    BinaryExprArgs,
    FieldPlusLiteral,
    Position,
    Replace,
    NumericScaleSpecial,
    SharedScalarCall,
    Substring,
    UnaryExpr,
    VariadicExprArgs,
    WherePredicateExprPair,
}

impl SqlScalarFunction {
    /// Return whether this parsed SQL scalar function uses the dedicated
    /// scale-taking numeric parser/lowering path instead of the general scalar
    /// call surface.
    #[must_use]
    pub(crate) const fn uses_numeric_scale_special_case(self) -> bool {
        matches!(self, Self::Round | Self::Trunc)
    }

    /// Return the canonical planner-owned scalar function identity for this
    /// parsed SQL scalar function.
    #[must_use]
    pub(in crate::db) const fn planner_function(self) -> Function {
        match self {
            Self::Abs => Function::Abs,
            Self::Cbrt => Function::Cbrt,
            Self::Ceiling => Function::Ceiling,
            Self::Coalesce => Function::Coalesce,
            Self::Contains => Function::Contains,
            Self::EndsWith => Function::EndsWith,
            Self::Exp => Function::Exp,
            Self::Floor => Function::Floor,
            Self::IsEmpty => Function::IsEmpty,
            Self::IsMissing => Function::IsMissing,
            Self::IsNotEmpty => Function::IsNotEmpty,
            Self::IsNotNull => Function::IsNotNull,
            Self::IsNull => Function::IsNull,
            Self::Left => Function::Left,
            Self::Length => Function::Length,
            Self::Ln => Function::Ln,
            Self::Log => Function::Log,
            Self::Log10 => Function::Log10,
            Self::Log2 => Function::Log2,
            Self::Lower => Function::Lower,
            Self::Ltrim => Function::Ltrim,
            Self::Mod => Function::Mod,
            Self::NullIf => Function::NullIf,
            Self::Position => Function::Position,
            Self::Power => Function::Power,
            Self::Replace => Function::Replace,
            Self::Right => Function::Right,
            Self::Round => Function::Round,
            Self::Rtrim => Function::Rtrim,
            Self::Sign => Function::Sign,
            Self::StartsWith => Function::StartsWith,
            Self::Substring => Function::Substring,
            Self::Sqrt => Function::Sqrt,
            Self::Trim => Function::Trim,
            Self::Trunc => Function::Trunc,
            Self::Upper => Function::Upper,
        }
    }

    /// Return the parser call-shape used by non-WHERE scalar function parsing.
    #[must_use]
    pub(in crate::db) const fn non_where_call_shape(self) -> SqlScalarFunctionCallShape {
        match self {
            Self::Round | Self::Trunc => SqlScalarFunctionCallShape::NumericScaleSpecial,
            Self::Coalesce => SqlScalarFunctionCallShape::VariadicExprArgs,
            Self::NullIf | Self::Log | Self::Mod | Self::Power => {
                SqlScalarFunctionCallShape::BinaryExprArgs
            }
            Self::Trim
            | Self::Ltrim
            | Self::Rtrim
            | Self::Abs
            | Self::Cbrt
            | Self::Ceiling
            | Self::Exp
            | Self::Floor
            | Self::Ln
            | Self::Log10
            | Self::Log2
            | Self::Sign
            | Self::Sqrt
            | Self::IsEmpty
            | Self::IsMissing
            | Self::IsNotEmpty
            | Self::IsNotNull
            | Self::IsNull
            | Self::Lower
            | Self::Upper
            | Self::Length => SqlScalarFunctionCallShape::UnaryExpr,
            Self::Left | Self::Right | Self::StartsWith | Self::EndsWith | Self::Contains => {
                SqlScalarFunctionCallShape::FieldPlusLiteral
            }
            Self::Position => SqlScalarFunctionCallShape::Position,
            Self::Replace => SqlScalarFunctionCallShape::Replace,
            Self::Substring => SqlScalarFunctionCallShape::Substring,
        }
    }

    /// Return the parser call-shape used by WHERE expression parsing.
    #[must_use]
    pub(in crate::db) const fn where_call_shape(self) -> SqlScalarFunctionCallShape {
        match self.non_where_call_shape() {
            SqlScalarFunctionCallShape::NumericScaleSpecial => {
                SqlScalarFunctionCallShape::NumericScaleSpecial
            }
            SqlScalarFunctionCallShape::VariadicExprArgs => {
                SqlScalarFunctionCallShape::VariadicExprArgs
            }
            SqlScalarFunctionCallShape::BinaryExprArgs => {
                SqlScalarFunctionCallShape::BinaryExprArgs
            }
            SqlScalarFunctionCallShape::FieldPlusLiteral
                if self
                    .planner_function()
                    .boolean_text_predicate_kind()
                    .is_some() =>
            {
                SqlScalarFunctionCallShape::WherePredicateExprPair
            }
            SqlScalarFunctionCallShape::UnaryExpr
            | SqlScalarFunctionCallShape::FieldPlusLiteral
            | SqlScalarFunctionCallShape::Position
            | SqlScalarFunctionCallShape::Replace
            | SqlScalarFunctionCallShape::Substring => SqlScalarFunctionCallShape::SharedScalarCall,
            SqlScalarFunctionCallShape::SharedScalarCall
            | SqlScalarFunctionCallShape::WherePredicateExprPair => {
                SqlScalarFunctionCallShape::SharedScalarCall
            }
        }
    }

    /// Resolve one parsed SQL identifier into one supported scalar function.
    #[must_use]
    pub(crate) fn from_identifier(identifier: &str) -> Option<Self> {
        const SUPPORTED_SCALAR_FUNCTIONS: [(&str, SqlScalarFunction); 34] = [
            ("trim", SqlScalarFunction::Trim),
            ("ltrim", SqlScalarFunction::Ltrim),
            ("rtrim", SqlScalarFunction::Rtrim),
            ("round", SqlScalarFunction::Round),
            ("coalesce", SqlScalarFunction::Coalesce),
            ("nullif", SqlScalarFunction::NullIf),
            ("abs", SqlScalarFunction::Abs),
            ("cbrt", SqlScalarFunction::Cbrt),
            ("ceil", SqlScalarFunction::Ceiling),
            ("ceiling", SqlScalarFunction::Ceiling),
            ("exp", SqlScalarFunction::Exp),
            ("floor", SqlScalarFunction::Floor),
            ("ln", SqlScalarFunction::Ln),
            ("log", SqlScalarFunction::Log),
            ("log10", SqlScalarFunction::Log10),
            ("log2", SqlScalarFunction::Log2),
            ("sign", SqlScalarFunction::Sign),
            ("sqrt", SqlScalarFunction::Sqrt),
            ("mod", SqlScalarFunction::Mod),
            ("power", SqlScalarFunction::Power),
            ("pow", SqlScalarFunction::Power),
            ("trunc", SqlScalarFunction::Trunc),
            ("truncate", SqlScalarFunction::Trunc),
            ("lower", SqlScalarFunction::Lower),
            ("upper", SqlScalarFunction::Upper),
            ("length", SqlScalarFunction::Length),
            ("left", SqlScalarFunction::Left),
            ("right", SqlScalarFunction::Right),
            ("starts_with", SqlScalarFunction::StartsWith),
            ("ends_with", SqlScalarFunction::EndsWith),
            ("contains", SqlScalarFunction::Contains),
            ("position", SqlScalarFunction::Position),
            ("replace", SqlScalarFunction::Replace),
            ("substring", SqlScalarFunction::Substring),
        ];

        for (name, function) in SUPPORTED_SCALAR_FUNCTIONS {
            if identifier.eq_ignore_ascii_case(name) {
                return Some(function);
            }
        }

        None
    }
}

///
/// SqlOrderDirection
///
/// Parsed order direction for one `ORDER BY` item.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SqlOrderDirection {
    Asc,
    Desc,
}

///
/// SqlOrderTerm
///
/// Parsed `ORDER BY` expression and direction pair.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SqlOrderTerm {
    pub(crate) field: SqlExpr,
    pub(crate) direction: SqlOrderDirection,
}

impl SqlOrderTerm {
    /// Return whether this parsed order term already stays within the local
    /// supported-order fast path.
    #[must_use]
    pub(in crate::db) fn is_already_local_supported(&self) -> bool {
        self.field.fields_are_already_local()
    }

    /// Return one direct field name when this order term still targets one
    /// bare SQL field leaf.
    #[must_use]
    pub(in crate::db) const fn direct_field_name(&self) -> Option<&str> {
        match &self.field {
            SqlExpr::Field(field) => Some(field.as_str()),
            SqlExpr::FieldPath { .. }
            | SqlExpr::Aggregate(_)
            | SqlExpr::Literal(_)
            | SqlExpr::Param { .. }
            | SqlExpr::Membership { .. }
            | SqlExpr::NullTest { .. }
            | SqlExpr::Like { .. }
            | SqlExpr::FunctionCall { .. }
            | SqlExpr::Unary { .. }
            | SqlExpr::Binary { .. }
            | SqlExpr::Case { .. } => None,
        }
    }
}

///
/// SqlSelectStatement
///
/// Raw parsed `SELECT` statement shape for reduced SQL.
///
/// This contract is frontend-only and intentionally schema-agnostic. Table
/// alias syntax remains attached here so lowering, not parsing, owns
/// identifier normalization.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SqlSelectStatement {
    pub(crate) entity: String,
    pub(crate) table_alias: Option<String>,
    pub(crate) projection: SqlProjection,
    pub(crate) projection_aliases: Vec<Option<String>>,
    pub(crate) predicate: Option<SqlExpr>,
    pub(crate) distinct: bool,
    pub(crate) group_by: Vec<String>,
    pub(crate) having: Vec<SqlExpr>,
    pub(crate) order_by: Vec<SqlOrderTerm>,
    pub(crate) limit: Option<u32>,
    pub(crate) offset: Option<u32>,
}

impl SqlSelectStatement {
    /// Return whether this parsed `SELECT` already stays in the local
    /// canonical shape expected by lowering.
    #[must_use]
    pub(in crate::db) fn is_already_local_canonical(&self) -> bool {
        if self.table_alias.is_some() {
            return false;
        }
        if !self.projection_aliases.iter().all(Option::is_none) {
            return false;
        }
        if !self.having.is_empty() {
            return false;
        }
        if !self
            .group_by
            .iter()
            .all(|field| SqlExpr::identifier_is_already_local(field.as_str()))
        {
            return false;
        }
        if !self.projection.is_already_local_scalar() {
            return false;
        }
        if self
            .predicate
            .as_ref()
            .is_some_and(|predicate| !predicate.is_already_local_scalar())
        {
            return false;
        }

        self.order_by
            .iter()
            .all(SqlOrderTerm::is_already_local_supported)
    }
}

///
/// SqlReturningProjection
///
/// Narrow write-lane `RETURNING` contract accepted by reduced SQL.
/// This intentionally keeps returning projections on field lists or `*` only
/// so write-result shaping does not reopen the broader computed or aggregate
/// SELECT projection surface.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum SqlReturningProjection {
    All,
    Fields(Vec<String>),
}

///
/// SqlDeleteStatement
///
/// Raw parsed `DELETE` statement shape for reduced SQL.
///
/// This contract keeps delete-mode clause policy explicit while preserving
/// table alias syntax for lowering-owned identifier normalization.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SqlDeleteStatement {
    pub(crate) entity: String,
    pub(crate) table_alias: Option<String>,
    pub(crate) predicate: Option<SqlExpr>,
    pub(crate) order_by: Vec<SqlOrderTerm>,
    pub(crate) limit: Option<u32>,
    pub(crate) offset: Option<u32>,
    pub(crate) returning: Option<SqlReturningProjection>,
}

///
/// SqlInsertSource
///
/// Canonical parsed reduced-SQL `INSERT` source.
///
/// This keeps the current write lane narrow while still distinguishing between
/// literal tuple inserts and session-owned `INSERT ... SELECT` follow-ups.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum SqlInsertSource {
    Values(Vec<Vec<Value>>),
    Select(Box<SqlSelectStatement>),
}

///
/// SqlInsertStatement
///
/// Canonical parsed `INSERT` statement shape for reduced SQL.
///
/// This stays intentionally narrow in the current slice: one explicit column
/// list plus either one or more literal `VALUES` tuples or one scalar
/// `SELECT` source handled later at the session boundary.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SqlInsertStatement {
    pub(crate) entity: String,
    pub(crate) columns: Vec<String>,
    pub(crate) source: SqlInsertSource,
    pub(crate) returning: Option<SqlReturningProjection>,
}

///
/// SqlAssignment
///
/// One parsed `UPDATE ... SET field = literal` assignment.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SqlAssignment {
    pub(crate) field: String,
    pub(crate) value: Value,
}

///
/// SqlUpdateStatement
///
/// Raw parsed `UPDATE` statement shape for reduced SQL.
///
/// This stays intentionally narrow in the current slice: one `SET` list plus
/// one optional reduced predicate and one bounded ordered window that later
/// session policy constrains further. Table alias syntax is preserved until
/// lowering normalizes all write-lane identifiers.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SqlUpdateStatement {
    pub(crate) entity: String,
    pub(crate) table_alias: Option<String>,
    pub(crate) assignments: Vec<SqlAssignment>,
    pub(crate) predicate: Option<SqlExpr>,
    pub(crate) order_by: Vec<SqlOrderTerm>,
    pub(crate) limit: Option<u32>,
    pub(crate) offset: Option<u32>,
    pub(crate) returning: Option<SqlReturningProjection>,
}

///
/// SqlExplainMode
///
/// Reduced EXPLAIN render mode selector.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SqlExplainMode {
    Plan,
    Execution,
    Json,
}

///
/// SqlExplainTarget
///
/// Statement forms accepted behind one `EXPLAIN` prefix.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum SqlExplainTarget {
    Select(SqlSelectStatement),
    Delete(SqlDeleteStatement),
}

///
/// SqlExplainStatement
///
/// Canonical parsed `EXPLAIN` statement.
///
/// Explain remains a wrapper over one executable reduced SQL statement.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SqlExplainStatement {
    pub(crate) mode: SqlExplainMode,
    pub(crate) verbose: bool,
    pub(crate) statement: SqlExplainTarget,
}

///
/// SqlDescribeStatement
///
/// Canonical parsed `DESCRIBE` statement.
/// Carries one schema entity identifier for typed session introspection.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SqlDescribeStatement {
    pub(crate) entity: String,
}

///
/// SqlShowIndexesStatement
///
/// Canonical parsed `SHOW INDEXES` statement.
/// Carries one schema entity identifier for typed session introspection.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SqlShowIndexesStatement {
    pub(crate) entity: String,
}

///
/// SqlShowColumnsStatement
///
/// Canonical parsed `SHOW COLUMNS` statement.
/// Carries one schema entity identifier for typed session introspection.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SqlShowColumnsStatement {
    pub(crate) entity: String,
}

///
/// SqlShowEntitiesStatement
///
/// Canonical parsed `SHOW ENTITIES` statement.
/// This lane carries no entity identifier and targets SQL helper introspection.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct SqlShowEntitiesStatement;