Skip to main content

uqa_sql/semantics/sets/
static_setness.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Catalog-only set-returning routine classification before physical traversal.
8use super::validation::builtin_returns_set;
9use crate::ast::FunctionBinding;
10use crate::SQLError;
11use uqa_core::memory::ProductionControl;
12
13pub fn function_may_return_set_statically(
14    catalog: &dyn crate::routines::RoutineResolution,
15    name: &str,
16    binding: Option<&FunctionBinding>,
17) -> bool {
18    function_may_return_set_statically_with_control(
19        catalog,
20        name,
21        binding,
22        &ProductionControl::uncontrolled(),
23    )
24    .expect("ordinary static setness uses no limited resources")
25}
26
27pub fn function_may_return_set_statically_with_control(
28    catalog: &dyn crate::routines::RoutineResolution,
29    name: &str,
30    binding: Option<&FunctionBinding>,
31    control: &ProductionControl<'_>,
32) -> Result<bool, SQLError> {
33    control.check()?;
34    if binding.and_then(|binding| binding.dispatch).is_some()
35        || binding.is_some_and(FunctionBinding::is_polymorphic_builtin_syntax)
36    {
37        return Ok(false);
38    }
39    // A call bound to a SQL routine returns a set exactly when that routine does, whatever built-in shares its name.
40    if let Some(binding) = binding.filter(|binding| !binding.builtin) {
41        return Ok(crate::routines::selected_bound_function_with_control(
42            catalog, binding, control,
43        )?
44        .is_some_and(|function| function.def.returns_set()));
45    }
46    let identity = crate::semantics::lowercase_function_name_with_control(name, control)?;
47    let builtin =
48        crate::semantics::builtin_function_dispatch_name_with_control(&identity, control)?;
49    if builtin_returns_set(&builtin)
50        || catalog.has_registered_table_function_with_control(&identity, control)?
51    {
52        return Ok(true);
53    }
54    if binding.is_some() {
55        return Ok(false);
56    }
57    match catalog.visible_function_returns_set_with_control(name, control) {
58        Ok(returns_set) => Ok(returns_set),
59        Err(error) if matches!(error.sqlstate(), Some("53200" | "57014")) => Err(error),
60        Err(_) => Ok(false),
61    }
62}
63
64/// Whether a call of a built-in routine returns a set: a call bound to a built-in, or an unbound call that no SQL routine resolved. Scalar evaluation reports such a call with `PostgreSQL`'s executor diagnostic instead of evaluating it.
65pub fn builtin_call_returns_set(name: &str, binding: Option<&FunctionBinding>) -> bool {
66    if binding.is_some_and(|binding| {
67        !binding.builtin || binding.dispatch.is_some() || binding.is_polymorphic_builtin_syntax()
68    }) {
69        return false;
70    }
71    let name = binding
72        .map_or(name, |binding| binding.name.as_str())
73        .to_ascii_lowercase();
74    builtin_returns_set(name.strip_prefix("pg_catalog.").unwrap_or(&name))
75}
76
77/// Whether `expression` contains a call that returns a set, outside its subqueries, classified from the catalog alone: a bound call by its routine, an unbound call by every visible routine of its name.
78pub fn expression_may_return_set_statically(
79    catalog: &dyn crate::routines::RoutineResolution,
80    expression: &crate::ScalarExpr,
81) -> bool {
82    expression_may_return_set_statically_with_control(
83        catalog,
84        expression,
85        &ProductionControl::uncontrolled(),
86    )
87    .expect("ordinary static setness uses no limited resources")
88}
89
90pub fn expression_may_return_set_statically_with_control(
91    catalog: &dyn crate::routines::RoutineResolution,
92    expression: &crate::ScalarExpr,
93    control: &ProductionControl<'_>,
94) -> Result<bool, SQLError> {
95    let mut returns_set = false;
96    expression.try_visit(&mut |node| {
97        control.check()?;
98        if let crate::ScalarExpr::Func { name, binding, .. } = node {
99            if !returns_set {
100                returns_set = function_may_return_set_statically_with_control(
101                    catalog,
102                    name,
103                    binding.as_ref(),
104                    control,
105                )?;
106            }
107        }
108        Ok::<_, SQLError>(true)
109    })?;
110    Ok(returns_set)
111}