Skip to main content

uqa_sql/expr/numeric_operator/
resolved.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Fixed-size numeric overloads retain typed coercions, never input values or invocation controls.
8
9use super::{
10    ColumnType, NumericOperator, Produced, ProductionControl, ProductionVec, Result, SQLError,
11    Value,
12};
13use crate::{expr::NumericCast, type_resolution::NumericOperatorTypes};
14
15/// A selected builtin numeric operation. Every evaluation still converts all operands in order and uses the invoking memory and cancellation controls.
16#[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(self.operator, &arguments, control)?;
62        // The shared value carrier does not encode int2/int4 or real width.
63        self.result.evaluate(&value, control)
64    }
65}
66
67#[cfg(test)]
68mod tests;