Skip to main content

uqa_sql/semantics/
runtime_scalars.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Runtime scalar argument and row-context rules.
8
9use crate::{expr::RowLookup, SQLError, ScalarExpr};
10use uqa_core::Value;
11
12pub fn deep_learn_arguments(
13    args: &[ScalarExpr],
14    evaluate: &mut dyn FnMut(&ScalarExpr) -> Result<Value, SQLError>,
15) -> Result<(String, String), SQLError> {
16    if args.len() != 2 {
17        return Err(SQLError::BadArity {
18            name: "deep_learn".into(),
19            expected: "2".into(),
20            actual: args.len(),
21        });
22    }
23    let model_name = match evaluate(&args[0])? {
24        Value::Str(s) => s,
25        other => {
26            return Err(SQLError::TypeMismatch(format!(
27                "deep_learn.model must be a string, got {other:?}"
28            )));
29        }
30    };
31    let training_source = match evaluate(&args[1])? {
32        Value::Str(s) => s,
33        other => {
34            return Err(SQLError::TypeMismatch(format!(
35                "deep_learn.training_set must be a table name or JSON string, got {other:?}"
36            )));
37        }
38    };
39    Ok((model_name, training_source))
40}
41
42pub enum DeepLearnSource<'a> {
43    Json(&'a str),
44    Table(&'a str),
45}
46pub fn deep_learn_source(source: &str) -> DeepLearnSource<'_> {
47    let trimmed = source.trim();
48    if trimmed.starts_with('{') {
49        DeepLearnSource::Json(trimmed)
50    } else {
51        DeepLearnSource::Table(source)
52    }
53}
54pub fn no_scalar_arguments(name: &str, arguments: &[Value]) -> Result<(), SQLError> {
55    if arguments.is_empty() {
56        Ok(())
57    } else {
58        Err(SQLError::BadArity {
59            name: name.into(),
60            expected: "0".into(),
61            actual: arguments.len(),
62        })
63    }
64}
65pub fn notification_arguments(arguments: &[Value]) -> Result<(&str, &str), SQLError> {
66    match arguments {
67        [channel, payload] => Ok((
68            notification_text_argument(channel, "channel")?,
69            notification_text_argument(payload, "payload")?,
70        )),
71        _ => Err(SQLError::BadArity {
72            name: "pg_notify".into(),
73            expected: "2".into(),
74            actual: arguments.len(),
75        }),
76    }
77}
78fn notification_text_argument<'a>(value: &'a Value, label: &str) -> Result<&'a str, SQLError> {
79    match value {
80        Value::Null => Ok(""),
81        Value::Str(value) => Ok(value),
82        Value::FixedChar(value) => Ok(value.trim_end_matches(' ')),
83        other => Err(SQLError::TypeMismatch(format!(
84            "pg_notify {label} must be text, got {other:?}"
85        ))),
86    }
87}
88
89pub fn merge_action_value(args: &[ScalarExpr], row: &dyn RowLookup) -> Result<Value, SQLError> {
90    if !args.is_empty() {
91        return Err(SQLError::BadArity {
92            name: "merge_action".into(),
93            expected: "0".into(),
94            actual: args.len(),
95        });
96    }
97    let action = row
98        .internal_column(super::merge_action_attribute())
99        .cloned()
100        .ok_or_else(|| {
101            SQLError::Unsupported("merge_action() is only valid in MERGE RETURNING".into())
102        })?;
103    Ok(action)
104}