uqa_sql/expr/numeric_operator/
resolved.rs1use super::{
10 ColumnType, NumericOperator, Produced, ProductionControl, ProductionVec, Result, SQLError,
11 Value,
12};
13use crate::{expr::NumericCast, type_resolution::NumericOperatorTypes};
14
15#[derive(Clone, Copy, Debug)]
17pub struct ResolvedNumericOperator {
18 operator: NumericOperator,
19 arguments: [Option<NumericCast>; 2],
20 result: NumericCast,
21}
22
23impl ResolvedNumericOperator {
24 pub(super) fn from_selection(
25 operator: NumericOperator,
26 sources: &[Option<ColumnType>],
27 selected: &NumericOperatorTypes,
28 ) -> Option<Self> {
29 let mut arguments = [None; 2];
30 for (index, (source, target)) in sources.iter().zip(&selected.arguments).enumerate() {
31 arguments[index] = Some(NumericCast::select(source.as_ref()?, target)?);
32 }
33 Some(Self {
34 operator,
35 arguments,
36 result: NumericCast::select(&selected.result, &selected.result)?,
37 })
38 }
39
40 pub fn evaluate(
41 &self,
42 arguments: &[Value],
43 control: &ProductionControl<'_>,
44 ) -> Result<Produced<Value>> {
45 control.check()?;
46 if arguments.len() != self.operator.arity() {
47 return Err(SQLError::Internal(
48 "resolved operator operand count differs".into(),
49 ));
50 }
51 let mut converted = ProductionVec::new(*control);
52 converted.reserve(arguments.len())?;
53 for (value, conversion) in arguments.iter().zip(self.arguments) {
54 converted.push_produced(
55 conversion
56 .expect("selected numeric operand")
57 .evaluate(value, control)?,
58 )?;
59 }
60 let arguments = converted.finish()?;
61 let value = super::evaluate_converted(
62 self.operator,
63 &arguments,
64 &self.result.target_type(),
65 control,
66 )?;
67 self.result.evaluate(&value, control)
69 }
70}
71
72#[cfg(test)]
73mod tests;