Skip to main content

uqa_sql/routines/resolution/production/
table.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Table calls share candidate selection and retain the selected invocation independently.
8
9use super::{
10    check_invocation_result, combined, diagnostics, matching, selected_binding, selection,
11};
12use crate::{
13    ast::{ColumnType, FunctionBinding, RoutineVariadicMode},
14    plan::copy::copy_syntax_with_control,
15    routines::resolution::{RoutineCallKind, RoutineOverloadContext},
16    type_resolution::{BuiltinFunctionOverload, ResolvedFunctionOverload, RoutineCallDescriptor},
17    SQLError,
18};
19use uqa_core::memory::{Produced, ProductionControl};
20
21impl RoutineOverloadContext<'_> {
22    /// Static table binding checks the selected routine without resolving a scalar output type.
23    pub fn resolve_static_sql_function_binding_with_control(
24        &self,
25        name: &str,
26        binding: Option<&FunctionBinding>,
27        argument_names: &[Option<String>],
28        argument_types: &[Option<ColumnType>],
29        explicit_variadic: bool,
30        control: &ProductionControl<'_>,
31    ) -> Result<Option<Produced<FunctionBinding>>, SQLError> {
32        control.check()?;
33        let kind = RoutineCallKind::Function;
34        let call = RoutineCallDescriptor {
35            argument_names,
36            argument_types,
37            explicit_variadic,
38        };
39        let matched = if let Some(binding) = binding {
40            if binding.builtin {
41                return Ok(None);
42            }
43            let function = self
44                .catalog
45                .select_bound_routine_with_control(binding, kind, control)?
46                .ok_or_else(|| {
47                    diagnostics::bound(kind, binding, control)
48                        .unwrap_or_else(std::convert::identity)
49                })?;
50            if let Some(invocation) = &binding.invocation {
51                if invocation.argument_positions.len() != argument_types.len()
52                    || matches!(
53                        invocation.variadic_mode,
54                        RoutineVariadicMode::Explicit { .. }
55                    ) != explicit_variadic
56                {
57                    return Err(diagnostics::bound(kind, binding, control)?);
58                }
59                diagnostics::kind(name, call, kind, &function.def, control)?;
60                check_invocation_result(self, invocation, control)?;
61                return selected_binding(
62                    &function,
63                    copy_syntax_with_control(invocation.as_ref(), control)?,
64                    control,
65                )
66                .map(Some);
67            }
68            matching::match_candidate(
69                &self.catalog.routine_type_snapshot(),
70                function,
71                call,
72                kind,
73                control,
74            )
75            .map_err(|error| selection::signature_error(error, control))?
76            .ok_or_else(|| {
77                diagnostics::bound(kind, binding, control).unwrap_or_else(std::convert::identity)
78            })?
79        } else {
80            let Some(matched) = selection::select_unbound(self, name, call, control)? else {
81                return Ok(None);
82            };
83            matched
84        };
85        diagnostics::kind(name, call, kind, &matched.function.def, control)?;
86        if let Some(error) = &matched.signature.default_error {
87            return Err(diagnostics::default_type(self.catalog, error, control)?);
88        }
89        check_invocation_result(self, &matched.invocation, control)?;
90        selected_binding(&matched.function, matched.invocation, control).map(Some)
91    }
92
93    pub fn resolve_table_function_overload_with_builtins_and_control(
94        &self,
95        name: &str,
96        binding: Option<&FunctionBinding>,
97        call: crate::type_resolution::RoutineCallDescriptor<'_>,
98        builtins: &[BuiltinFunctionOverload],
99        control: &ProductionControl<'_>,
100    ) -> Result<Option<Produced<ResolvedFunctionOverload>>, SQLError> {
101        let selected = combined::resolve(self, name, binding, call, builtins, control)?;
102        let Some(binding) = binding.filter(|binding| !binding.builtin) else {
103            return Ok(Some(selected));
104        };
105        // Stored table calls keep their original binding; scalar selection may regenerate it.
106        let binding = copy_syntax_with_control(binding, control)?;
107        let return_type = selected.return_type.clone_with_control(control)?;
108        let known_arguments = selected.known_arguments;
109        let precedes_pg_catalog = selected.precedes_pg_catalog;
110        drop(selected);
111        let (binding, binding_memory) = binding.into_parts();
112        let (return_type, type_memory) = return_type.into_parts();
113        Ok(Some(control.finish(
114            ResolvedFunctionOverload {
115                binding,
116                return_type,
117                exact_matches: 0,
118                known_arguments,
119                preferred_matches: 0,
120                precedes_pg_catalog,
121            },
122            control.combine(binding_memory, type_memory),
123        )?))
124    }
125}