uqa-sql 0.6.1

PostgreSQL-compatible SQL compiler built on libpg_query
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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Ordered parameter occurrences and their independently resolved SQL types.

use crate::catalog::collations::derivation::Derivation;
use crate::{plan::UnifiedPlan, ColumnType, SQLError, SQLParam, ScalarExpr};
use std::{borrow::Cow, collections::BTreeMap, ptr::NonNull};
use uqa_core::Value;

mod call_collations;
mod collations;
mod results;
pub(super) use call_collations::CallCollations;

#[derive(Clone)]
pub(super) struct ExpressionType {
    pub(super) ty: Option<ColumnType>,
    /// Selected output and deferred collation diagnostics from the same typed children.
    pub(super) derivation: Derivation,
    /// Field selection observes a ROW input before the enclosing expression's collation assignment.
    pub(super) initial_collation: u32,
    pub(super) record_fields: Option<crate::schema::RecordFields>,
    pub(super) record_descriptor: Option<std::sync::Arc<crate::schema::RecordDescriptor>>,
    /// A record variable without a registered descriptor has a different field diagnostic from a computed record.
    pub(super) record_variable: bool,
    /// An anonymous ROW retains each analyzed input until a named cast selects its field coercions.
    pub(super) row_constructor: Option<Vec<Self>>,
    occurrence: Option<usize>,
    // SQL unknown literals and parameters accept context coercion. Native
    // callbacks with no declared result type stay unresolved until execution.
    unknown: UnknownOrigin,
    /// The text of an `unknown` string literal, which the selected type's input function reads when a context coerces it.
    literal: Option<(NonNull<ScalarExpr>, String)>,
    collation: Option<NonNull<ScalarExpr>>,
}

#[derive(Clone, Copy, PartialEq, Eq)]
enum UnknownOrigin {
    Deferred,
    Literal,
    Parameter,
    Field,
}

impl ExpressionType {
    pub(super) fn default_collation(ty: Option<&ColumnType>) -> Derivation {
        Derivation::implicit(ty.map_or(0, crate::catalog::type_metadata::pg_type_collation_oid))
    }

    pub(super) fn combined(ty: Option<ColumnType>, values: &[Self]) -> Self {
        let selected = values.iter().fold(Derivation::None, |state, value| {
            state.combine(value.derivation)
        });
        let mut result = Self::resolved(ty);
        result.derivation = selected.result(result.derivation.oid());
        result
    }

    pub(super) fn comparison(left: &Self, right: &Self) -> Derivation {
        if let (Some(left), Some(right)) = (&left.row_constructor, &right.row_constructor) {
            left.iter()
                .zip(right)
                .fold(Derivation::None, |state, (left, right)| {
                    state.combine(left.derivation.combine(right.derivation).error_only())
                })
        } else {
            left.derivation.combine(right.derivation).error_only()
        }
    }

    pub(super) fn independent(ty: Option<ColumnType>, values: &[Self]) -> Self {
        let mut result = Self::resolved(ty);
        result.derivation = values.iter().fold(result.derivation, |state, value| {
            state.combine(value.derivation.error_only())
        });
        result
    }

    pub(super) fn resolved(ty: Option<ColumnType>) -> Self {
        Self {
            derivation: Self::default_collation(ty.as_ref()),
            initial_collation: 0,
            ty,
            record_fields: None,
            record_descriptor: None,
            record_variable: false,
            row_constructor: None,
            occurrence: None,
            unknown: UnknownOrigin::Deferred,
            literal: None,
            collation: None,
        }
    }

    pub(super) fn unknown() -> Self {
        Self {
            unknown: UnknownOrigin::Literal,
            ..Self::resolved(None)
        }
    }

    /// A bare string literal, `unknown` until its context selects a type that reads it.
    pub(super) fn unknown_literal(expression: &ScalarExpr, text: String) -> Self {
        Self {
            literal: Some((NonNull::from(expression), text)),
            ..Self::unknown()
        }
    }

    pub(super) fn is_deferred(&self) -> bool {
        self.ty.is_none() && self.unknown == UnknownOrigin::Deferred
    }

    pub(super) fn is_sql_unknown(&self) -> bool {
        self.ty.is_none() && self.unknown != UnknownOrigin::Deferred
    }

    pub(super) fn is_unknown_parameter(&self) -> bool {
        self.ty.is_none() && self.unknown == UnknownOrigin::Parameter
    }

    pub(super) fn mark_unknown_field(&mut self) {
        self.unknown = UnknownOrigin::Field;
    }

    pub(super) fn leaf(ty: Option<ColumnType>) -> Self {
        let mut value = Self::resolved(ty);
        value.initial_collation =
            u32::try_from(value.derivation.oid()).expect("type collation OID");
        value
    }

    pub(super) fn record_metadata(&self) -> Option<crate::schema::RecordMetadata> {
        (self.record_fields.is_some() || self.record_descriptor.is_some()).then(|| {
            crate::schema::RecordMetadata {
                types: self.record_fields.clone(),
                descriptor: self.record_descriptor.clone(),
            }
        })
    }
}

pub(super) struct ParameterTypes<'a> {
    pub(super) collations: super::collations::PendingCollations,
    types: Vec<Option<ColumnType>>,
    fixed_values: Option<&'a [SQLParam]>,
    occurrences: Vec<(usize, Option<ColumnType>)>,
    input_constants: Option<InputConstants>,
    aliases: Option<&'a dyn crate::schema::dependencies::oid_alias::OidAliasInput>,
    enum_labels: Option<&'a dyn crate::expr::enums::EnumLabelCatalog>,
    catalog_inputs: Option<&'a dyn crate::expr::CatalogInputFunctions>,
    call_collations: Option<CallCollations>,
}

/// Original expression identities for input constants and selected fixed calls, used only while one prepared plan remains in place. No pointer is dereferenced. Analysis borrows the original expressions (including grouping aliases and table-function arguments), so neither repeated text nor CTE analysis order can conflate two inputs.
/// A constant read during analysis and retained in the executable plan.
#[derive(Debug, Clone, PartialEq)]
pub(super) struct ConvertedInput {
    pub(super) expression: ScalarExpr,
    /// The conversion read a session setting, the transaction clock or the catalog, so an ordinary message must read the text again instead of reusing the plan.
    pub(super) session_dependent: bool,
}

#[derive(Default)]
pub(super) struct InputConstants(
    BTreeMap<NonNull<ScalarExpr>, ConvertedInput>,
    BTreeMap<NonNull<ScalarExpr>, crate::ast::FunctionBinding>,
    BTreeMap<NonNull<ScalarExpr>, super::membership::MembershipAnalysis>,
    collations::CollationInputs,
    results::CollationResults,
    BTreeMap<NonNull<ScalarExpr>, ColumnType>,
    /// Each analyzed call's input collation, recorded on the binding the call keeps.
    BTreeMap<NonNull<ScalarExpr>, Option<crate::catalog::collations::CollationSelection>>,
    BTreeMap<NonNull<Option<crate::ast::FunctionBinding>>, crate::ast::FunctionBinding>,
);

impl InputConstants {
    /// Ordinary messages must repeat session/catalog-dependent input functions.
    /// Prepared definitions intentionally retain these same converted constants.
    pub(super) fn reusable_across_messages(&self) -> bool {
        self.0
            .values()
            .chain(
                self.2
                    .values()
                    .flat_map(|membership| membership.left_constants.iter().flatten()),
            )
            .all(|input| !input.session_dependent)
    }

    fn apply_node(&mut self, expression: &mut ScalarExpr) -> Result<(), SQLError> {
        let identity = NonNull::from(&*expression);
        if let Some(selected) = self.1.remove(&identity) {
            if let ScalarExpr::Func { binding, .. } = expression {
                *binding = Some(selected);
                crate::type_resolution::json_path::normalize_retained(expression)?;
            }
        }
        if let Some(collation) = self.6.remove(&identity) {
            if let ScalarExpr::Func {
                binding: Some(binding),
                ..
            } = expression
            {
                binding.input_collation = collation;
            }
        }
        if let Some(constant) = self.0.remove(&identity) {
            *expression = constant.expression;
        }
        self.3.apply(identity, expression)?;
        self.4.apply(identity, expression)?;
        if let Some(target) = self.5.remove(&identity) {
            let inner = std::mem::replace(expression, ScalarExpr::Literal(Value::Null));
            *expression = ScalarExpr::Cast {
                expr: Box::new(inner),
                ty: target.catalog_name(),
                implicit: true,
            };
        }
        Ok(())
    }

    #[cfg(test)]
    pub(super) fn apply_expression(self, expression: &mut ScalarExpr) -> Result<(), SQLError> {
        self.apply_expression_with_subqueries(expression, &mut Vec::new())
    }

    pub(super) fn apply_expression_with_subqueries(
        mut self,
        expression: &mut ScalarExpr,
        subqueries: &mut Vec<crate::plan::QueryPlan>,
    ) -> Result<(), SQLError> {
        for query in subqueries.iter_mut() {
            query.visit_sources_mut(&mut |source| self.apply_source(source));
        }
        let mut result = Ok(());
        let mut apply = |node: &mut ScalarExpr| {
            if result.is_ok() {
                result = self.apply_node(node);
            }
        };
        crate::plan::rewrite_scalar_expression(expression, &mut apply);
        for query in subqueries.iter_mut() {
            query.rewrite_scalar_expressions(&mut apply);
        }
        result?;
        if !self.2.is_empty() {
            crate::plan::subqueries::rewrite_expression_with_arena(
                expression,
                subqueries,
                &mut |node, arena| self.apply_membership(node, arena),
            )?;
        }
        if self.is_consumed() {
            Ok(())
        } else {
            Err(SQLError::Internal(
                "default input constant did not belong to the analyzed expression".into(),
            ))
        }
    }

    pub(super) fn apply(mut self, plan: &mut UnifiedPlan) -> Result<(), SQLError> {
        // Source selections are consumed before expression rewrites can move their enclosing nodes.
        plan.visit_sources_mut(&mut |source| self.apply_source(source));
        // Children are rewritten before their enclosing clauses move them; every retained identity is consumed at its original address.
        let mut result = Ok(());
        plan.rewrite_scalar_expressions(&mut |expression| {
            if result.is_ok() {
                result = self.apply_node(expression);
            }
        });
        result?;
        if !self.2.is_empty() {
            crate::plan::subqueries::rewrite_with_arenas(plan, &mut |node, arena| {
                self.apply_membership(node, arena)
            })?;
        }
        if self.is_consumed() {
            Ok(())
        } else {
            Err(SQLError::Internal(
                "prepared input constant did not belong to the analyzed plan".into(),
            ))
        }
    }

    fn is_consumed(&self) -> bool {
        self.0.is_empty()
            && self.1.is_empty()
            && self.2.is_empty()
            && self.3.is_empty()
            && self.4.is_empty()
            && self.5.is_empty()
            && self.6.is_empty()
            && self.7.is_empty()
    }

    fn apply_source(&mut self, source: &mut crate::plan::SourcePlan) {
        let mut apply = |binding: &mut Option<crate::ast::FunctionBinding>| {
            if let Some(selected) = self.7.remove(&NonNull::from(&*binding)) {
                *binding = Some(selected);
            }
        };
        match source {
            crate::plan::SourcePlan::Function { binding, .. } => apply(binding),
            crate::plan::SourcePlan::FunctionGroup { functions, .. } => {
                for function in functions {
                    apply(&mut function.binding);
                }
            }
            _ => {}
        }
    }

    fn apply_membership(
        &mut self,
        node: &mut ScalarExpr,
        arena: &mut Vec<crate::plan::QueryPlan>,
    ) -> Result<(), SQLError> {
        if let Some(analysis) = self.2.remove(&NonNull::from(&*node)) {
            analysis.apply(node, arena)?;
        }
        Ok(())
    }
}

impl<'a> ParameterTypes<'a> {
    pub(super) fn retain_input_coercion(&mut self, expression: &ScalarExpr, target: ColumnType) {
        if let Some(constants) = &mut self.input_constants {
            constants.5.insert(NonNull::from(expression), target);
        }
    }

    pub(super) fn retain_result_collations(mut self) -> Self {
        if let Some(constants) = &mut self.input_constants {
            constants.4.enabled = true;
        }
        self
    }

    pub(super) fn retain_result_collation(
        &mut self,
        expression: &ScalarExpr,
        value: &ExpressionType,
    ) -> Result<(), SQLError> {
        if let Some(constants) = &mut self.input_constants {
            constants.4.record(expression, value)?;
        }
        Ok(())
    }

    pub(super) fn take_literal(&mut self, expression: &ScalarExpr) -> Option<ConvertedInput> {
        let constants = self.input_constants.as_mut()?;
        constants.3.take_literal(expression, &mut constants.0)
    }

    pub(super) fn retain_collation(
        &mut self,
        expression: &ScalarExpr,
        value: &mut ExpressionType,
        oid: u32,
    ) {
        let origin = NonNull::from(expression);
        if let Some(constants) = &mut self.input_constants {
            constants.3.select(origin, value.collation, oid);
        }
        value.collation = Some(origin);
        value.derivation = value.derivation.explicit(i64::from(oid));
        value.initial_collation = oid;
    }

    pub(super) fn retain_membership(
        &mut self,
        expression: &ScalarExpr,
        analysis: super::membership::MembershipAnalysis,
    ) {
        if let Some(constants) = &mut self.input_constants {
            constants.2.insert(NonNull::from(expression), analysis);
        }
    }

    pub(super) fn new(types: &[Option<ColumnType>]) -> Self {
        Self {
            types: types.to_vec(),
            fixed_values: None,
            occurrences: Vec::new(),
            input_constants: None,
            collations: super::collations::PendingCollations::default(),
            aliases: None,
            enum_labels: None,
            catalog_inputs: None,
            call_collations: None,
        }
    }

    /// Output binding borrows caller parameters and preserves their explicit declarations.
    pub(super) fn fixed(values: &'a [SQLParam]) -> Self {
        Self {
            fixed_values: Some(values),
            ..Self::new(&[])
        }
    }

    /// Fixed output binding can describe a subtree after ordered statement analysis, without its enclosing SQL clause.
    pub(super) fn is_output_binding(&self) -> bool {
        self.fixed_values.is_some()
    }

    pub(super) fn with_input_constants(
        types: &[Option<ColumnType>],
        aliases: Option<&'a dyn crate::schema::dependencies::oid_alias::OidAliasInput>,
        enum_labels: Option<&'a dyn crate::expr::enums::EnumLabelCatalog>,
        catalog_inputs: Option<&'a dyn crate::expr::CatalogInputFunctions>,
    ) -> Self {
        Self {
            input_constants: Some(InputConstants::default()),
            aliases,
            enum_labels,
            catalog_inputs,
            ..Self::new(types)
        }
    }

    /// Keep the declaration selected by analysis on the original call, without
    /// copying its arguments or repeating type inference before optimization.
    pub(super) fn retain_call(
        &mut self,
        expression: &ScalarExpr,
        selected: &crate::ast::FunctionBinding,
    ) {
        if !selected.builtin
            || crate::fixed_builtin_return_type(selected).is_some()
            || crate::catalog::json_path::JsonPathFunction::lookup(&selected.name).is_some()
            || matches!(
                selected.dispatch,
                Some(crate::ast::FunctionDispatch::ArraySlicesWithBounds(_))
            )
        {
            if let Some(constants) = &mut self.input_constants {
                constants
                    .1
                    .insert(NonNull::from(expression), selected.clone());
            }
        }
    }

    /// Keep the FROM call's selected identity and invocation for planning without resolving it again outside its row scope.
    pub(super) fn retain_source_binding(
        &mut self,
        original: NonNull<Option<crate::ast::FunctionBinding>>,
        selected: crate::ast::FunctionBinding,
    ) {
        if let Some(constants) = &mut self.input_constants {
            constants.7.insert(original, selected);
        }
    }

    /// Record a call's input collation on whichever binding the call keeps after analysis.
    pub(super) fn retain_input_collation(
        &mut self,
        expression: &ScalarExpr,
        collation: Option<crate::catalog::collations::CollationSelection>,
    ) {
        if let Some(constants) = &mut self.input_constants {
            constants.6.insert(NonNull::from(expression), collation);
        } else if let Some(calls) = &mut self.call_collations {
            calls.record(expression, collation);
        }
    }

    /// Keep a built-in selected by name on a call that had no binding, so its input collation reaches execution.
    pub(super) fn retain_builtin_selection(
        &mut self,
        expression: &ScalarExpr,
        selected: crate::ast::FunctionBinding,
    ) {
        if let Some(constants) = &mut self.input_constants {
            constants.1.insert(NonNull::from(expression), selected);
        } else if let Some(calls) = &mut self.call_collations {
            calls.record_builtin(expression, selected);
        }
    }

    /// Record only the input collations of calls in an expression whose routines are already bound.
    pub(super) fn with_call_collations(mut self) -> Self {
        self.call_collations = Some(CallCollations::default());
        self
    }

    pub(super) fn take_call_collations(&mut self) -> CallCollations {
        self.call_collations.take().unwrap_or_default()
    }

    pub(super) fn take_input_constants(&mut self) -> InputConstants {
        self.input_constants.take().unwrap_or_default()
    }

    pub(super) fn reference(&mut self, number: usize) -> Result<ExpressionType, SQLError> {
        if let Some(values) = self.fixed_values {
            let parameter = ScalarExpr::Param(number);
            let ty = crate::effective_overload_argument_type_with_params(
                &parameter,
                crate::scalar_type(&parameter, &crate::RowSchema::default(), values)?,
                values,
            );
            let mut value = ExpressionType::leaf(ty);
            if number
                .checked_sub(1)
                .is_some_and(|index| index < values.len())
            {
                value.unknown = UnknownOrigin::Parameter;
            }
            return Ok(value);
        }
        let index = number
            .checked_sub(1)
            .filter(|index| *index < self.types.len())
            .ok_or_else(|| error("42P02", format!("there is no parameter ${number}")))?;
        let ty = self.types[index].clone();
        let occurrence = self.occurrences.len();
        self.occurrences.push((index, ty.clone()));
        Ok(ExpressionType {
            occurrence: Some(occurrence),
            unknown: UnknownOrigin::Parameter,
            ..ExpressionType::leaf(ty)
        })
    }

    pub(super) fn coerce_unknown(
        &mut self,
        expression: &mut ExpressionType,
        target: &ColumnType,
    ) -> Result<(), SQLError> {
        if !expression.is_sql_unknown() {
            return Ok(());
        }
        if expression.unknown == UnknownOrigin::Field {
            return Err(crate::type_resolution::field_selection::unknown_conversion_error(target));
        }
        if let Some((origin, text)) = &expression.literal {
            self.read_literal(*origin, text, target)?;
        }
        let target = target.without_type_modifiers();
        if let Some(constants) = &mut self.input_constants {
            constants.3.coerce(expression.collation, &target)?;
        }
        if let Some(occurrence) = expression.occurrence {
            let (index, observed) = &mut self.occurrences[occurrence];
            if self.types[*index].as_ref().is_some_and(|ty| *ty != target) {
                return Err(error(
                    "42P08",
                    format!("inconsistent types deduced for parameter ${}", *index + 1),
                ));
            }
            self.types[*index] = Some(target.clone());
            *observed = Some(target.clone());
        }
        expression.derivation =
            expression
                .derivation
                .result(crate::catalog::type_metadata::pg_type_collation_oid(
                    &target,
                ));
        expression.ty = Some(target);
        expression.initial_collation =
            u32::try_from(expression.derivation.oid()).expect("coerced collation OID");
        Ok(())
    }

    fn read_literal(
        &mut self,
        origin: NonNull<ScalarExpr>,
        text: &str,
        target: &ColumnType,
    ) -> Result<(), SQLError> {
        let input_type = crate::type_resolution::literal_input_type_with_control(
            target,
            &uqa_core::memory::ProductionControl::uncontrolled(),
        )?
        .into_uncontrolled()
        .expect("ordinary literal input has no reservation");
        if self.input_constants.as_ref().is_some_and(|constants| {
            matches!(
                constants.0.get(&origin),
                Some(ConvertedInput {
                    expression: ScalarExpr::TypedLiteral { bound_type: Some(ty), .. },
                    ..
                }) if *ty == input_type
            )
        }) {
            return Ok(());
        }
        if let Some(input) = self.input_value(text, &input_type)? {
            if let Some(constants) = &mut self.input_constants {
                constants.0.insert(
                    origin,
                    ConvertedInput {
                        expression: ScalarExpr::TypedLiteral {
                            collation_oid: None,
                            composite_source: None,
                            value: input.value,
                            ty: input_type.catalog_name(),
                            bound_type: Some(input_type),
                            parameter_index: None,
                        },
                        session_dependent: input.session_dependent,
                    },
                );
            }
        }
        Ok(())
    }

    fn input_value(
        &self,
        text: &str,
        input_type: &ColumnType,
    ) -> Result<Option<ReadInput>, SQLError> {
        if crate::expr::requires_catalog_constant_input(input_type) {
            if self.input_constants.is_none() {
                return Ok(None);
            }
            let catalog = self.catalog_inputs.ok_or_else(|| {
                SQLError::Internal(format!(
                    "catalog input functions are unavailable for {}",
                    input_type.sql_name()
                ))
            })?;
            return catalog
                .read_unknown_input(text, input_type)
                .map(|value| Some(ReadInput::session_dependent(value)));
        }
        if self.input_constants.is_some() && crate::expr::enums::is_enum_bearing(input_type) {
            if let Some(value) = crate::expr::enums::fold_unknown_literal(
                self.enum_labels,
                &Value::Str(text.into()),
                input_type,
            )? {
                return Ok(Some(ReadInput::session_dependent(value)));
            }
        }
        if crate::type_resolution::catalog_input_type(input_type) {
            return self.aliases.map_or(Ok(None), |catalog| {
                crate::schema::dependencies::oid_alias::read_unknown_constant(
                    catalog, input_type, text,
                )
                .map(|value| value.map(ReadInput::session_dependent))
            });
        }
        let value = crate::expr::cast_value_from(
            &Value::Str(text.into()),
            &input_type.catalog_name(),
            None,
        )?;
        let session_dependent = input_reads_session(text, input_type, &value);
        Ok(Some(ReadInput {
            value,
            session_dependent,
        }))
    }

    pub(super) fn values(&self) -> Cow<'_, [SQLParam]> {
        if let Some(values) = self.fixed_values {
            return Cow::Borrowed(values);
        }
        Cow::Owned(
            self.types
                .iter()
                .map(|ty| match ty {
                    Some(ty) => SQLParam::typed_scalar(Value::Null, ty.clone()),
                    None => SQLParam::Scalar(Value::Null),
                })
                .collect(),
        )
    }

    pub(super) fn finish(self) -> Result<Vec<Option<ColumnType>>, SQLError> {
        for (index, observed) in self.occurrences {
            if observed != self.types[index] {
                return Err(error(
                    "42P08",
                    format!("could not determine data type of parameter ${}", index + 1),
                ));
            }
        }
        if let Some(index) = self.types.iter().position(Option::is_none) {
            return Err(error(
                "42P18",
                format!("could not determine data type of parameter ${}", index + 1),
            ));
        }
        Ok(self.types)
    }
}

pub(super) fn error(sqlstate: &str, message: String) -> SQLError {
    SQLError::Routine {
        sqlstate: sqlstate.into(),
        message,
    }
}

/// A text input read by its input function, with whether an ordinary message must read it again.
struct ReadInput {
    value: Value,
    session_dependent: bool,
}

impl ReadInput {
    /// A value read through the catalog, whose descriptors a later message may see changed.
    fn session_dependent(value: Value) -> Self {
        Self {
            value,
            session_dependent: true,
        }
    }
}

/// Whether an ordinary message must read `text` again. Domain arrays and composites read the catalog; a type whose input function is stable reads a session setting or the transaction clock, unless the text reproduces `value` under every setting.
fn input_reads_session(text: &str, input_type: &ColumnType, value: &Value) -> bool {
    if crate::expr::requires_domain_array_input(input_type)
        || matches!(input_type, ColumnType::Composite(_))
    {
        return true;
    }
    if crate::type_resolution::cast_volatility(&ColumnType::Text, input_type)
        == crate::ast::FunctionVolatility::Immutable
    {
        return false;
    }
    !(temporal_input_type(input_type)
        && crate::expr::text_input_is_session_independent(text, input_type, value))
}

/// Types whose text input reads only the thread's date order, interval style and transaction clock, and arrays of them.
fn temporal_input_type(ty: &ColumnType) -> bool {
    match ty {
        ColumnType::Array(element) => temporal_input_type(element),
        ColumnType::Date
        | ColumnType::Time
        | ColumnType::TimePrecision(_)
        | ColumnType::TimeTz
        | ColumnType::TimeTzPrecision(_)
        | ColumnType::Timestamp
        | ColumnType::TimestampPrecision(_)
        | ColumnType::TimestampTz
        | ColumnType::TimestampTzPrecision(_)
        | ColumnType::Interval
        | ColumnType::IntervalWithFields { .. } => true,
        _ => false,
    }
}

#[cfg(test)]
mod tests;