uqa_sql/routines/inlining/table/
production.rs1use super::{FunctionBinding, FunctionVolatility, RoutineInliningContext, SQLError, ScalarExpr};
10use uqa_core::memory::{Produced, ProductionControl};
11
12impl RoutineInliningContext<'_> {
13 #[expect(
14 clippy::too_many_arguments,
15 reason = "source aliases, selected invocation and production control belong to the same existing expansion decision"
16 )]
17 pub fn scalar_from_columns_with_control(
18 &self,
19 binding: &FunctionBinding,
20 arguments: &[ScalarExpr],
21 output_name: &str,
22 alias: Option<&str>,
23 columns: &[String],
24 ordinality: bool,
25 control: &ProductionControl<'_>,
26 ) -> Result<Option<Produced<Vec<String>>>, SQLError> {
27 let Some(function) =
28 crate::routines::selected_bound_function_with_control(self.routines, binding, control)?
29 else {
30 return Ok(None);
31 };
32 if function.def.returns_set()
33 || !closed_arguments_with_control(self, arguments, control)?
34 || crate::semantics::user_function_composite_result_with_control(
35 self.routines,
36 &function,
37 binding.invocation.as_deref(),
38 control,
39 )?
40 .is_some()
41 {
42 return Ok(None);
43 }
44 let names = crate::semantics::user_table_function_columns_with_control(
45 self.routines,
46 &function,
47 binding.invocation.as_deref(),
48 output_name,
49 alias,
50 columns,
51 ordinality,
52 control,
53 )?;
54 Ok((names.len() == 1 + usize::from(ordinality)).then_some(names))
55 }
56}
57
58fn closed_arguments_with_control(
59 context: &RoutineInliningContext<'_>,
60 arguments: &[ScalarExpr],
61 control: &ProductionControl<'_>,
62) -> Result<bool, SQLError> {
63 for argument in arguments {
64 let mut closed = true;
65 let mut failure = None;
66 argument.visit(&mut |node| {
67 if failure.is_some() {
68 return;
69 }
70 if let Err(error) = control.check() {
71 failure = Some(error);
72 return;
73 }
74 closed &= !matches!(
75 node,
76 ScalarExpr::Column(_)
77 | ScalarExpr::QualifiedColumn { .. }
78 | ScalarExpr::InternalColumn(_)
79 | ScalarExpr::ScalarSubquery(_)
80 | ScalarExpr::Exists { .. }
81 | ScalarExpr::InSubquery { .. }
82 );
83 });
84 if let Some(error) = failure {
85 return Err(error.into());
86 }
87 if !closed
88 || super::super::properties::volatility_with_control(
89 context,
90 argument,
91 &crate::RowSchema::empty_with_control(control)?,
92 control,
93 )? == FunctionVolatility::Volatile
94 {
95 return Ok(false);
96 }
97 }
98 control.check()?;
99 Ok(true)
100}