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
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
//! Module: query::plan::expr::function_semantics
//! Responsibility: planner-owned scalar function taxonomy and semantic facets.
//! Does not own: parser identifier resolution, expression lowering, or runtime evaluation.
//! Boundary: central registry for scalar function category, null behavior, determinism, and typing shape.

use crate::{
    db::{
        numeric::{
            NumericArithmeticOp, NumericEvalError, apply_decimal_arithmetic_checked,
            coerce_numeric_decimal, decimal_cbrt_checked, decimal_exp_checked, decimal_ln_checked,
            decimal_log_base_checked, decimal_log2_checked, decimal_log10_checked,
            decimal_power_checked, decimal_sign, decimal_sqrt_checked,
        },
        query::plan::expr::ast::{Expr, Function},
    },
    types::Decimal,
    value::Value,
};

///
/// NumericSubtype
///
/// NumericSubtype keeps planner numeric typing coarse while still preserving
/// the few distinctions needed by operator/function inference and aggregate
/// result shaping.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum NumericSubtype {
    Integer,
    Float,
    Decimal,
    Unknown,
}

///
/// FunctionCategory
///
/// FunctionCategory classifies the canonical scalar function taxonomy into the
/// planner-owned semantic families used by typing, capability, and audit
/// reasoning.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[remain::sorted]
pub(crate) enum FunctionCategory {
    BooleanPredicate,
    Collection,
    NullHandling,
    Numeric,
    Text,
}

///
/// FunctionNullBehavior
///
/// FunctionNullBehavior records the canonical null contract for one scalar
/// function so planner consumers do not rediscover strictness or special null
/// handling through parallel local ladders.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum FunctionNullBehavior {
    NullIgnoring,
    NullObserving,
    Strict,
}

///
/// FunctionDeterminism
///
/// FunctionDeterminism keeps execution-stability classification on the
/// planner-owned function registry even while the current surface only ships
/// deterministic scalar functions.
///

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

///
/// FunctionTypeInferenceShape
///
/// FunctionTypeInferenceShape captures the canonical planner typing contract
/// for one scalar function family so type inference can consume the shared
/// registry instead of acting as the hidden function owner.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum FunctionTypeInferenceShape {
    BoolResult {
        text_positions: &'static [usize],
    },
    CollectionContains,
    DynamicCoalesce,
    DynamicNullIf,
    NumericResult {
        text_positions: &'static [usize],
        numeric_positions: &'static [usize],
        subtype: NumericSubtype,
    },
    NumericScaleResult,
    TextResult {
        text_positions: &'static [usize],
        numeric_positions: &'static [usize],
    },
    UnaryBoolPredicate,
}

///
/// FunctionSurface
///
/// FunctionSurface records the planner-owned expression surfaces where one
/// canonical scalar function family is admitted. This keeps surface
/// eligibility on the function registry instead of parser-local or
/// lowering-local special-case ladders.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum FunctionSurface {
    AggregateInput,
    AggregateInputCondition,
    HavingCondition,
    Projection,
    ProjectionCondition,
    Where,
}

///
/// BooleanFunctionShape
///
/// BooleanFunctionShape classifies the small planner-owned scalar function
/// families that participate directly in boolean truth-condition admission and
/// boolean predicate compilation. This keeps those consumers on one shared
/// function-family owner instead of repeating the same admitted boolean
/// subsets locally.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum BooleanFunctionShape {
    CollectionContains,
    FieldPredicate,
    NullTest,
    TextPredicate,
    TruthCoalesce,
}

///
/// NullTestFunctionKind
///
/// NullTestFunctionKind keeps the small `IS NULL` versus `IS NOT NULL`
/// distinction on the function owner once a caller has already admitted the
/// broader boolean null-test family.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum NullTestFunctionKind {
    IsNotNull,
    IsNull,
}

impl NullTestFunctionKind {
    /// Return whether the null branch is the truthy branch for this null-test kind.
    #[must_use]
    pub(crate) const fn null_matches_true(self) -> bool {
        matches!(self, Self::IsNull)
    }

    /// Evaluate one admitted null test against one value.
    #[must_use]
    pub(crate) const fn eval_value(self, value: &Value) -> Value {
        Value::Bool(self.null_matches_true() == matches!(value, Value::Null))
    }
}

///
/// TextPredicateFunctionKind
///
/// TextPredicateFunctionKind preserves the finer text-predicate distinction
/// once boolean-function admission has already proven that one scalar
/// function belongs to the text-predicate family.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum TextPredicateFunctionKind {
    Contains,
    EndsWith,
    StartsWith,
}

impl TextPredicateFunctionKind {
    /// Evaluate one admitted text predicate against one text input and needle.
    #[must_use]
    pub(crate) fn eval_text(self, text: &str, needle: &str) -> Value {
        Value::Bool(match self {
            Self::Contains => text.contains(needle),
            Self::EndsWith => text.ends_with(needle),
            Self::StartsWith => text.starts_with(needle),
        })
    }
}

///
/// UnaryTextFunctionKind
///
/// UnaryTextFunctionKind preserves the finer unary text transform
/// distinction once scalar-evaluation dispatch has already proven that one
/// scalar function belongs to the unary-text family.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum UnaryTextFunctionKind {
    Length,
    Lower,
    Ltrim,
    Rtrim,
    Trim,
    Upper,
}

impl UnaryTextFunctionKind {
    /// Evaluate one admitted unary text transform against one text input.
    #[must_use]
    pub(crate) fn eval_text(self, text: &str) -> Value {
        match self {
            Self::Trim => Value::Text(text.trim().to_string()),
            Self::Ltrim => Value::Text(text.trim_start().to_string()),
            Self::Rtrim => Value::Text(text.trim_end().to_string()),
            Self::Lower => Value::Text(text.to_lowercase()),
            Self::Upper => Value::Text(text.to_uppercase()),
            Self::Length => Value::Uint(u64::try_from(text.chars().count()).unwrap_or(u64::MAX)),
        }
    }
}

///
/// UnaryNumericFunctionKind
///
/// UnaryNumericFunctionKind preserves the finer unary numeric transform
/// distinction once scalar-evaluation dispatch has already proven that one
/// scalar function belongs to the unary-numeric family.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum UnaryNumericFunctionKind {
    Abs,
    Cbrt,
    Ceiling,
    Exp,
    Floor,
    Ln,
    Log10,
    Log2,
    Sign,
    Sqrt,
}

impl UnaryNumericFunctionKind {
    /// Evaluate one admitted unary numeric transform against one decimal input.
    pub(crate) fn eval_decimal(self, decimal: Decimal) -> Result<Value, NumericEvalError> {
        let result = match self {
            Self::Abs => decimal.checked_abs().ok_or(NumericEvalError::Overflow)?,
            Self::Cbrt => decimal_cbrt_checked(decimal)?,
            Self::Ceiling => decimal.ceil_dp0(),
            Self::Exp => decimal_exp_checked(decimal)?,
            Self::Floor => decimal.floor_dp0(),
            Self::Ln => decimal_ln_checked(decimal)?,
            Self::Log10 => decimal_log10_checked(decimal)?,
            Self::Log2 => decimal_log2_checked(decimal)?,
            Self::Sign => decimal_sign(decimal),
            Self::Sqrt => decimal_sqrt_checked(decimal)?,
        };

        Ok(Value::Decimal(result))
    }
}

///
/// BinaryNumericFunctionKind
///
/// BinaryNumericFunctionKind preserves the finer binary numeric transform
/// distinction once scalar-evaluation dispatch has already proven that one
/// scalar function belongs to the binary-numeric family.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum BinaryNumericFunctionKind {
    Log,
    Mod,
    Power,
}

impl BinaryNumericFunctionKind {
    /// Evaluate one admitted binary numeric transform against decimal inputs.
    pub(crate) fn eval_decimal(
        self,
        left: Decimal,
        right: Decimal,
    ) -> Result<Value, NumericEvalError> {
        let result = match self {
            Self::Log => decimal_log_base_checked(left, right)?,
            Self::Mod => apply_decimal_arithmetic_checked(NumericArithmeticOp::Rem, left, right)?,
            Self::Power => decimal_power_checked(left, right)?,
        };

        Ok(Value::Decimal(result))
    }
}

///
/// NumericScaleFunctionKind
///
/// NumericScaleFunctionKind preserves the finer scale-taking numeric
/// transform distinction once scalar-evaluation dispatch has already proven
/// that one scalar function belongs to this family.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum NumericScaleFunctionKind {
    Round,
    Trunc,
}

impl NumericScaleFunctionKind {
    /// Evaluate one admitted scale-taking numeric transform.
    #[must_use]
    pub(crate) const fn eval_decimal(self, decimal: Decimal, scale: u32) -> Value {
        let result = match self {
            Self::Round => decimal.round_dp(scale),
            Self::Trunc => decimal.trunc_dp(scale),
        };

        Value::Decimal(result)
    }
}

///
/// LeftRightTextFunctionKind
///
/// LeftRightTextFunctionKind preserves the LEFT versus RIGHT distinction once
/// scalar-evaluation dispatch has already proven that one scalar function
/// belongs to the bounded left/right text family.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum LeftRightTextFunctionKind {
    Left,
    Right,
}

impl LeftRightTextFunctionKind {
    /// Evaluate one admitted LEFT/RIGHT transform against one text input.
    #[must_use]
    pub(crate) fn eval_text(self, text: &str, count: i64) -> Value {
        Value::Text(match self {
            Self::Left => Self::left_chars(text, count),
            Self::Right => Self::right_chars(text, count),
        })
    }

    /// Return the first N chars from one text input while keeping
    /// negative/zero lengths on the empty-string SQL boundary.
    fn left_chars(text: &str, count: i64) -> String {
        if count <= 0 {
            return String::new();
        }

        text.chars()
            .take(usize::try_from(count).unwrap_or(usize::MAX))
            .collect()
    }

    /// Return the last N chars from one text input while keeping
    /// negative/zero lengths on the empty-string SQL boundary.
    fn right_chars(text: &str, count: i64) -> String {
        if count <= 0 {
            return String::new();
        }

        let count = usize::try_from(count).unwrap_or(usize::MAX);
        let total = text.chars().count();
        let skip = total.saturating_sub(count);

        text.chars().skip(skip).collect()
    }
}

///
/// FieldPredicateFunctionKind
///
/// FieldPredicateFunctionKind preserves the finer field-state predicate
/// distinction once boolean-function admission has already proven that one
/// scalar function belongs to the field-predicate family.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum FieldPredicateFunctionKind {
    Empty,
    Missing,
    NotEmpty,
}

///
/// AggregateInputConstantFoldShape
///
/// AggregateInputConstantFoldShape classifies the scalar function families
/// whose literal-only aggregate-input forms can collapse to one deterministic
/// literal result before planner grouping and aggregate lowering continue.
/// This keeps builder-side and frontend-lowering-side constant-fold admission
/// on one enum-owned contract instead of repeating the same foldable subsets.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum AggregateInputConstantFoldShape {
    DynamicCoalesce,
    DynamicNullIf,
    BinaryNumeric,
    Round,
    UnaryNumeric,
}

///
/// ScalarEvalFunctionShape
///
/// ScalarEvalFunctionShape classifies the bounded scalar-function behavior
/// families shared by planner literal preview and executor scalar projection
/// evaluation. This keeps dispatch-family ownership on `Function` and keeps
/// the shared pure value-level transforms on the same owner types.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum ScalarEvalFunctionShape {
    DynamicCoalesce,
    DynamicNullIf,
    BinaryNumeric,
    LeftRightText,
    NonExecutableProjection,
    NullTest,
    PositionText,
    ReplaceText,
    NumericScale,
    SubstringText,
    TextPredicate,
    UnaryNumeric,
    UnaryText,
}

///
/// FunctionSpec
///
/// FunctionSpec is the planner-owned semantic registry entry for one scalar
/// function identity. It carries the canonical category, null behavior,
/// determinism, and typing shape used by downstream planner consumers.
///

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct FunctionSpec {
    pub(crate) category: FunctionCategory,
    pub(crate) null_behavior: FunctionNullBehavior,
    pub(crate) determinism: FunctionDeterminism,
    pub(crate) type_inference_shape: FunctionTypeInferenceShape,
    pub(crate) allowed_surfaces: &'static [FunctionSurface],
}

const BOOLEAN_FUNCTION_SURFACES: &[FunctionSurface] = &[
    FunctionSurface::ProjectionCondition,
    FunctionSurface::AggregateInputCondition,
    FunctionSurface::HavingCondition,
    FunctionSurface::Where,
];

const GENERAL_SCALAR_FUNCTION_SURFACES: &[FunctionSurface] = &[
    FunctionSurface::Projection,
    FunctionSurface::ProjectionCondition,
    FunctionSurface::AggregateInput,
    FunctionSurface::AggregateInputCondition,
    FunctionSurface::HavingCondition,
    FunctionSurface::Where,
];

const ROUND_FUNCTION_SURFACES: &[FunctionSurface] = &[
    FunctionSurface::Projection,
    FunctionSurface::ProjectionCondition,
    FunctionSurface::AggregateInput,
    FunctionSurface::HavingCondition,
    FunctionSurface::Where,
];

impl FunctionSpec {
    /// Build one planner-owned scalar function specification.
    #[must_use]
    pub(crate) const fn new(
        category: FunctionCategory,
        null_behavior: FunctionNullBehavior,
        determinism: FunctionDeterminism,
        type_inference_shape: FunctionTypeInferenceShape,
        allowed_surfaces: &'static [FunctionSurface],
    ) -> Self {
        Self {
            category,
            null_behavior,
            determinism,
            type_inference_shape,
            allowed_surfaces,
        }
    }

    /// Build one strict unary text-result specification.
    #[must_use]
    const fn strict_unary_text_result() -> Self {
        Self::new(
            FunctionCategory::Text,
            FunctionNullBehavior::Strict,
            FunctionDeterminism::Deterministic,
            FunctionTypeInferenceShape::TextResult {
                text_positions: &[0],
                numeric_positions: &[],
            },
            GENERAL_SCALAR_FUNCTION_SURFACES,
        )
    }

    /// Build one strict text-result specification with explicit text/numeric
    /// argument positions.
    #[must_use]
    const fn strict_text_result(
        text_positions: &'static [usize],
        numeric_positions: &'static [usize],
    ) -> Self {
        Self::new(
            FunctionCategory::Text,
            FunctionNullBehavior::Strict,
            FunctionDeterminism::Deterministic,
            FunctionTypeInferenceShape::TextResult {
                text_positions,
                numeric_positions,
            },
            GENERAL_SCALAR_FUNCTION_SURFACES,
        )
    }

    /// Build one strict text-predicate boolean specification.
    #[must_use]
    const fn strict_text_bool_result(text_positions: &'static [usize]) -> Self {
        Self::new(
            FunctionCategory::Text,
            FunctionNullBehavior::Strict,
            FunctionDeterminism::Deterministic,
            FunctionTypeInferenceShape::BoolResult { text_positions },
            GENERAL_SCALAR_FUNCTION_SURFACES,
        )
    }

    /// Build one strict numeric-result specification.
    #[must_use]
    const fn strict_numeric_result(
        text_positions: &'static [usize],
        numeric_positions: &'static [usize],
        subtype: NumericSubtype,
    ) -> Self {
        Self::new(
            FunctionCategory::Numeric,
            FunctionNullBehavior::Strict,
            FunctionDeterminism::Deterministic,
            FunctionTypeInferenceShape::NumericResult {
                text_positions,
                numeric_positions,
                subtype,
            },
            GENERAL_SCALAR_FUNCTION_SURFACES,
        )
    }

    /// Build one null-observing unary boolean predicate specification.
    #[must_use]
    const fn null_observing_unary_bool_predicate() -> Self {
        Self::new(
            FunctionCategory::BooleanPredicate,
            FunctionNullBehavior::NullObserving,
            FunctionDeterminism::Deterministic,
            FunctionTypeInferenceShape::UnaryBoolPredicate,
            BOOLEAN_FUNCTION_SURFACES,
        )
    }

    /// Return whether this function specification is admitted on the given
    /// planner-owned expression surface.
    #[must_use]
    pub(crate) fn supports_surface(self, surface: FunctionSurface) -> bool {
        let mut index = 0usize;
        while index < self.allowed_surfaces.len() {
            if matches!(self.allowed_surfaces[index], current if current == surface) {
                return true;
            }
            index = index.saturating_add(1);
        }

        false
    }
}

impl Function {
    /// Return the canonical planner-owned scalar function specification.
    #[must_use]
    pub(crate) const fn spec(self) -> FunctionSpec {
        match self {
            Self::Abs
            | Self::Cbrt
            | Self::Ceiling
            | Self::Exp
            | Self::Floor
            | Self::Ln
            | Self::Log10
            | Self::Log2
            | Self::Sign
            | Self::Sqrt => FunctionSpec::strict_numeric_result(&[], &[0], NumericSubtype::Decimal),
            Self::Coalesce => FunctionSpec::new(
                FunctionCategory::NullHandling,
                FunctionNullBehavior::NullIgnoring,
                FunctionDeterminism::Deterministic,
                FunctionTypeInferenceShape::DynamicCoalesce,
                GENERAL_SCALAR_FUNCTION_SURFACES,
            ),
            Self::CollectionContains => FunctionSpec::new(
                FunctionCategory::Collection,
                FunctionNullBehavior::NullObserving,
                FunctionDeterminism::Deterministic,
                FunctionTypeInferenceShape::CollectionContains,
                BOOLEAN_FUNCTION_SURFACES,
            ),
            Self::Contains | Self::EndsWith | Self::StartsWith => {
                FunctionSpec::strict_text_bool_result(&[0, 1])
            }
            Self::IsEmpty | Self::IsMissing | Self::IsNotEmpty | Self::IsNotNull | Self::IsNull => {
                FunctionSpec::null_observing_unary_bool_predicate()
            }
            Self::Left | Self::Right => FunctionSpec::strict_text_result(&[0], &[1]),
            Self::Length => FunctionSpec::new(
                FunctionCategory::Text,
                FunctionNullBehavior::Strict,
                FunctionDeterminism::Deterministic,
                FunctionTypeInferenceShape::NumericResult {
                    text_positions: &[0],
                    numeric_positions: &[],
                    subtype: NumericSubtype::Integer,
                },
                GENERAL_SCALAR_FUNCTION_SURFACES,
            ),
            Self::Lower | Self::Ltrim | Self::Rtrim | Self::Trim | Self::Upper => {
                FunctionSpec::strict_unary_text_result()
            }
            Self::Log | Self::Mod | Self::Power => {
                FunctionSpec::strict_numeric_result(&[], &[0, 1], NumericSubtype::Decimal)
            }
            Self::NullIf => FunctionSpec::new(
                FunctionCategory::NullHandling,
                FunctionNullBehavior::NullIgnoring,
                FunctionDeterminism::Deterministic,
                FunctionTypeInferenceShape::DynamicNullIf,
                GENERAL_SCALAR_FUNCTION_SURFACES,
            ),
            Self::Position => FunctionSpec::new(
                FunctionCategory::Text,
                FunctionNullBehavior::Strict,
                FunctionDeterminism::Deterministic,
                FunctionTypeInferenceShape::NumericResult {
                    text_positions: &[0, 1],
                    numeric_positions: &[],
                    subtype: NumericSubtype::Integer,
                },
                GENERAL_SCALAR_FUNCTION_SURFACES,
            ),
            Self::Replace => FunctionSpec::strict_text_result(&[0, 1, 2], &[]),
            Self::Round | Self::Trunc => FunctionSpec::new(
                FunctionCategory::Numeric,
                FunctionNullBehavior::Strict,
                FunctionDeterminism::Deterministic,
                FunctionTypeInferenceShape::NumericScaleResult,
                ROUND_FUNCTION_SURFACES,
            ),
            Self::Substring => FunctionSpec::strict_text_result(&[0], &[1, 2]),
        }
    }

    /// Return the planner-owned typing shape for this function.
    #[must_use]
    pub(crate) const fn type_inference_shape(self) -> FunctionTypeInferenceShape {
        self.spec().type_inference_shape
    }

    /// Return whether this canonical scalar function is admitted on the given
    /// planner-owned expression surface.
    #[must_use]
    pub(crate) fn supports_surface(self, surface: FunctionSurface) -> bool {
        self.spec().supports_surface(surface)
    }

    /// Report whether planner typing classifies this scalar function as part
    /// of the text/numeric compare-operand family consumed by canonicalization.
    #[must_use]
    pub(crate) const fn is_compare_operand_coarse_family(self) -> bool {
        matches!(
            self.type_inference_shape(),
            FunctionTypeInferenceShape::TextResult { .. }
                | FunctionTypeInferenceShape::NumericResult { .. }
                | FunctionTypeInferenceShape::NumericScaleResult
                | FunctionTypeInferenceShape::DynamicCoalesce
                | FunctionTypeInferenceShape::DynamicNullIf
        )
    }

    /// Return one fixed decimal display scale implied by this scalar function
    /// and its planner-frozen arguments, if the function family carries one.
    #[must_use]
    pub(crate) fn fixed_decimal_scale(self, args: &[Expr]) -> Option<u32> {
        if !matches!(self, Self::Round | Self::Trunc) {
            return None;
        }

        match args.get(1) {
            Some(Expr::Literal(Value::Uint(scale))) => u32::try_from(*scale).ok(),
            Some(Expr::Literal(Value::Int(scale))) if *scale >= 0 => u32::try_from(*scale).ok(),
            _ => None,
        }
    }

    /// Return the planner-owned boolean function family used by truth
    /// admission, normalization, and predicate compilation, if this function
    /// participates in that bounded boolean surface.
    #[must_use]
    pub(crate) const fn boolean_function_shape(self) -> Option<BooleanFunctionShape> {
        match self {
            Self::Coalesce => Some(BooleanFunctionShape::TruthCoalesce),
            Self::IsNull | Self::IsNotNull => Some(BooleanFunctionShape::NullTest),
            Self::StartsWith | Self::EndsWith | Self::Contains => {
                Some(BooleanFunctionShape::TextPredicate)
            }
            Self::IsMissing | Self::IsEmpty | Self::IsNotEmpty => {
                Some(BooleanFunctionShape::FieldPredicate)
            }
            Self::CollectionContains => Some(BooleanFunctionShape::CollectionContains),
            Self::Abs
            | Self::Cbrt
            | Self::Ceiling
            | Self::Exp
            | Self::Floor
            | Self::Left
            | Self::Length
            | Self::Ln
            | Self::Log
            | Self::Log10
            | Self::Log2
            | Self::Lower
            | Self::Ltrim
            | Self::Mod
            | Self::NullIf
            | Self::Position
            | Self::Power
            | Self::Replace
            | Self::Right
            | Self::Round
            | Self::Rtrim
            | Self::Sign
            | Self::Substring
            | Self::Sqrt
            | Self::Trim
            | Self::Trunc
            | Self::Upper => None,
        }
    }

    /// Return the finer null-test kind once this function has already been
    /// admitted onto the bounded boolean null-test surface.
    #[must_use]
    pub(crate) const fn boolean_null_test_kind(self) -> Option<NullTestFunctionKind> {
        match self {
            Self::IsNull => Some(NullTestFunctionKind::IsNull),
            Self::IsNotNull => Some(NullTestFunctionKind::IsNotNull),
            _ => None,
        }
    }

    /// Return the finer text-predicate kind once this function has already
    /// been admitted onto the bounded boolean text-predicate surface.
    #[must_use]
    pub(crate) const fn boolean_text_predicate_kind(self) -> Option<TextPredicateFunctionKind> {
        match self {
            Self::StartsWith => Some(TextPredicateFunctionKind::StartsWith),
            Self::EndsWith => Some(TextPredicateFunctionKind::EndsWith),
            Self::Contains => Some(TextPredicateFunctionKind::Contains),
            _ => None,
        }
    }

    /// Return the finer field-predicate kind once this function has already
    /// been admitted onto the bounded boolean field-predicate surface.
    #[must_use]
    pub(crate) const fn boolean_field_predicate_kind(self) -> Option<FieldPredicateFunctionKind> {
        match self {
            Self::IsMissing => Some(FieldPredicateFunctionKind::Missing),
            Self::IsEmpty => Some(FieldPredicateFunctionKind::Empty),
            Self::IsNotEmpty => Some(FieldPredicateFunctionKind::NotEmpty),
            _ => None,
        }
    }

    /// Return whether this canonical scalar function is one of the admitted
    /// text casefold transforms that preserve shared LOWER/UPPER wrapper
    /// semantics across planner expression surfaces.
    #[must_use]
    pub(crate) const fn is_casefold_transform(self) -> bool {
        matches!(self, Self::Lower | Self::Upper)
    }

    /// Return the finer unary text transform kind once scalar-evaluation
    /// dispatch has already proven this function belongs to that family.
    #[must_use]
    pub(crate) const fn unary_text_function_kind(self) -> Option<UnaryTextFunctionKind> {
        match self {
            Self::Trim => Some(UnaryTextFunctionKind::Trim),
            Self::Ltrim => Some(UnaryTextFunctionKind::Ltrim),
            Self::Rtrim => Some(UnaryTextFunctionKind::Rtrim),
            Self::Lower => Some(UnaryTextFunctionKind::Lower),
            Self::Upper => Some(UnaryTextFunctionKind::Upper),
            Self::Length => Some(UnaryTextFunctionKind::Length),
            _ => None,
        }
    }

    /// Return the finer unary numeric transform kind once scalar-evaluation
    /// dispatch has already proven this function belongs to that family.
    #[must_use]
    pub(crate) const fn unary_numeric_function_kind(self) -> Option<UnaryNumericFunctionKind> {
        match self {
            Self::Abs => Some(UnaryNumericFunctionKind::Abs),
            Self::Cbrt => Some(UnaryNumericFunctionKind::Cbrt),
            Self::Ceiling => Some(UnaryNumericFunctionKind::Ceiling),
            Self::Exp => Some(UnaryNumericFunctionKind::Exp),
            Self::Floor => Some(UnaryNumericFunctionKind::Floor),
            Self::Ln => Some(UnaryNumericFunctionKind::Ln),
            Self::Log10 => Some(UnaryNumericFunctionKind::Log10),
            Self::Log2 => Some(UnaryNumericFunctionKind::Log2),
            Self::Sign => Some(UnaryNumericFunctionKind::Sign),
            Self::Sqrt => Some(UnaryNumericFunctionKind::Sqrt),
            _ => None,
        }
    }

    /// Return the finer binary numeric transform kind once scalar-evaluation
    /// dispatch has already proven this function belongs to that family.
    #[must_use]
    pub(crate) const fn binary_numeric_function_kind(self) -> Option<BinaryNumericFunctionKind> {
        match self {
            Self::Log => Some(BinaryNumericFunctionKind::Log),
            Self::Mod => Some(BinaryNumericFunctionKind::Mod),
            Self::Power => Some(BinaryNumericFunctionKind::Power),
            _ => None,
        }
    }

    /// Return the finer scale-taking numeric transform kind once scalar
    /// evaluation has already proven this function belongs to that family.
    #[must_use]
    pub(crate) const fn numeric_scale_function_kind(self) -> Option<NumericScaleFunctionKind> {
        match self {
            Self::Round => Some(NumericScaleFunctionKind::Round),
            Self::Trunc => Some(NumericScaleFunctionKind::Trunc),
            _ => None,
        }
    }

    /// Return the LEFT versus RIGHT distinction once scalar-evaluation
    /// dispatch has already proven this function belongs to that family.
    #[must_use]
    pub(crate) const fn left_right_text_function_kind(self) -> Option<LeftRightTextFunctionKind> {
        match self {
            Self::Left => Some(LeftRightTextFunctionKind::Left),
            Self::Right => Some(LeftRightTextFunctionKind::Right),
            _ => None,
        }
    }

    /// Return the bounded scalar-evaluation behavior family shared by
    /// planner literal preview and executor scalar projection evaluation.
    #[must_use]
    pub(crate) const fn scalar_eval_shape(self) -> ScalarEvalFunctionShape {
        match self {
            Self::IsNull | Self::IsNotNull => ScalarEvalFunctionShape::NullTest,
            Self::IsMissing | Self::IsEmpty | Self::IsNotEmpty | Self::CollectionContains => {
                ScalarEvalFunctionShape::NonExecutableProjection
            }
            Self::Trim | Self::Ltrim | Self::Rtrim | Self::Lower | Self::Upper | Self::Length => {
                ScalarEvalFunctionShape::UnaryText
            }
            Self::Coalesce => ScalarEvalFunctionShape::DynamicCoalesce,
            Self::NullIf => ScalarEvalFunctionShape::DynamicNullIf,
            Self::Abs
            | Self::Cbrt
            | Self::Ceiling
            | Self::Exp
            | Self::Floor
            | Self::Ln
            | Self::Log10
            | Self::Log2
            | Self::Sign
            | Self::Sqrt => ScalarEvalFunctionShape::UnaryNumeric,
            Self::Log | Self::Mod | Self::Power => ScalarEvalFunctionShape::BinaryNumeric,
            Self::Left | Self::Right => ScalarEvalFunctionShape::LeftRightText,
            Self::StartsWith | Self::EndsWith | Self::Contains => {
                ScalarEvalFunctionShape::TextPredicate
            }
            Self::Position => ScalarEvalFunctionShape::PositionText,
            Self::Replace => ScalarEvalFunctionShape::ReplaceText,
            Self::Substring => ScalarEvalFunctionShape::SubstringText,
            Self::Round | Self::Trunc => ScalarEvalFunctionShape::NumericScale,
        }
    }

    /// Return the stable executor-facing projection function name used by
    /// scalar projection evaluation diagnostics and invariant messages.
    #[must_use]
    pub(crate) const fn projection_eval_name(self) -> &'static str {
        match self {
            Self::IsNull => "is_null",
            Self::IsNotNull => "is_not_null",
            Self::IsMissing => "is_missing",
            Self::IsEmpty => "is_empty",
            Self::IsNotEmpty => "is_not_empty",
            Self::Trim => "trim",
            Self::Ltrim => "ltrim",
            Self::Rtrim => "rtrim",
            Self::Coalesce => "coalesce",
            Self::NullIf => "nullif",
            Self::Abs => "abs",
            Self::Cbrt => "cbrt",
            Self::Ceiling => "ceiling",
            Self::Exp => "exp",
            Self::Floor => "floor",
            Self::Ln => "ln",
            Self::Log => "log",
            Self::Log10 => "log10",
            Self::Log2 => "log2",
            Self::Sign => "sign",
            Self::Sqrt => "sqrt",
            Self::Mod => "mod",
            Self::Power => "power",
            Self::Lower => "lower",
            Self::Upper => "upper",
            Self::Length => "length",
            Self::Left => "left",
            Self::Right => "right",
            Self::StartsWith => "starts_with",
            Self::EndsWith => "ends_with",
            Self::Contains => "contains",
            Self::CollectionContains => "collection_contains",
            Self::Position => "position",
            Self::Replace => "replace",
            Self::Substring => "substring",
            Self::Round => "round",
            Self::Trunc => "trunc",
        }
    }

    /// Return the aggregate-input constant-fold family for this scalar
    /// function when a literal-only aggregate input can collapse
    /// deterministically.
    #[must_use]
    pub(crate) const fn aggregate_input_constant_fold_shape(
        self,
    ) -> Option<AggregateInputConstantFoldShape> {
        match self {
            Self::Round | Self::Trunc => Some(AggregateInputConstantFoldShape::Round),
            Self::Coalesce => Some(AggregateInputConstantFoldShape::DynamicCoalesce),
            Self::NullIf => Some(AggregateInputConstantFoldShape::DynamicNullIf),
            Self::Log | Self::Mod | Self::Power => {
                Some(AggregateInputConstantFoldShape::BinaryNumeric)
            }
            Self::Abs
            | Self::Cbrt
            | Self::Ceiling
            | Self::Exp
            | Self::Floor
            | Self::Ln
            | Self::Log10
            | Self::Log2
            | Self::Sign
            | Self::Sqrt => Some(AggregateInputConstantFoldShape::UnaryNumeric),
            Self::IsNull
            | Self::IsNotNull
            | Self::IsMissing
            | Self::IsEmpty
            | Self::IsNotEmpty
            | Self::Trim
            | Self::Ltrim
            | Self::Rtrim
            | Self::Left
            | Self::Right
            | Self::StartsWith
            | Self::EndsWith
            | Self::Contains
            | Self::CollectionContains
            | Self::Position
            | Self::Replace
            | Self::Substring
            | Self::Lower
            | Self::Upper
            | Self::Length => None,
        }
    }

    /// Evaluate one admitted COALESCE call after caller-side arity validation.
    #[must_use]
    pub(crate) fn eval_coalesce_values(self, args: &[Value]) -> Value {
        debug_assert!(matches!(self, Self::Coalesce));

        args.iter()
            .find(|value| !matches!(value, Value::Null))
            .cloned()
            .unwrap_or(Value::Null)
    }

    /// Evaluate one admitted NULLIF result once the caller has already computed
    /// its equality outcome through the layer-owned comparison boundary.
    #[must_use]
    pub(crate) fn eval_nullif_values(self, left: &Value, right: &Value, equals: bool) -> Value {
        debug_assert!(matches!(self, Self::NullIf));

        if matches!(left, Value::Null) || matches!(right, Value::Null) {
            return left.clone();
        }

        if equals { Value::Null } else { left.clone() }
    }

    /// Evaluate one admitted POSITION call after the caller has already
    /// validated both text operands.
    #[must_use]
    pub(crate) fn eval_position_text(self, text: &str, needle: &str) -> Value {
        debug_assert!(matches!(self, Self::Position));

        Value::Uint(Self::text_position_1_based(text, needle))
    }

    /// Evaluate one admitted REPLACE call after the caller has already
    /// validated all text operands.
    #[must_use]
    pub(crate) fn eval_replace_text(self, text: &str, from: &str, to: &str) -> Value {
        debug_assert!(matches!(self, Self::Replace));

        Value::Text(text.replace(from, to))
    }

    /// Evaluate one admitted SUBSTRING call after the caller has already
    /// validated the text and integer operands.
    #[must_use]
    pub(crate) fn eval_substring_text(self, text: &str, start: i64, length: Option<i64>) -> Value {
        debug_assert!(matches!(self, Self::Substring));

        Value::Text(Self::substring_1_based(text, start, length))
    }

    /// Evaluate one admitted scale-taking numeric call after the caller has
    /// already validated the non-negative scale boundary.
    #[must_use]
    pub(crate) fn eval_numeric_scale(self, value: &Value, scale: u32) -> Option<Value> {
        debug_assert!(matches!(self, Self::Round | Self::Trunc));

        let decimal = coerce_numeric_decimal(value)?;

        Some(
            self.numeric_scale_function_kind()?
                .eval_decimal(decimal, scale),
        )
    }

    /// Convert one found substring byte offset into the stable 1-based SQL
    /// char position used by POSITION(...).
    fn text_position_1_based(haystack: &str, needle: &str) -> u64 {
        let Some(byte_index) = haystack.find(needle) else {
            return 0;
        };
        let char_offset = haystack[..byte_index].chars().count();

        u64::try_from(char_offset)
            .unwrap_or(u64::MAX)
            .saturating_add(1)
    }

    /// Slice one text input using SQL-style 1-based substring coordinates.
    fn substring_1_based(text: &str, start: i64, length: Option<i64>) -> String {
        if start <= 0 {
            return String::new();
        }
        if matches!(length, Some(inner) if inner <= 0) {
            return String::new();
        }

        let start_index = usize::try_from(start.saturating_sub(1)).unwrap_or(usize::MAX);
        let chars = text.chars().skip(start_index);

        match length {
            Some(length) => chars
                .take(usize::try_from(length).unwrap_or(usize::MAX))
                .collect(),
            None => chars.collect(),
        }
    }
}