Skip to main content

uqa_sql/assignment/
vectors.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Validate vector and tensor values used by physical indexes.
8
9use super::conversion::validate_vector_dimensions;
10use crate::catalog::type_metadata::column_type_name;
11use crate::expr::{value_to_tensor, value_to_vector};
12use crate::{ColumnType, SQLError};
13use uqa_core::Value;
14
15pub fn index_vectors_for_type(value: &Value, ty: &ColumnType) -> Result<Vec<Vec<f32>>, SQLError> {
16    // SQL VECTOR/TENSOR columns are nullable unless their declaration says
17    // otherwise. A NULL value therefore means that the row has no vectors to
18    // index; it is not a malformed vector. Returning an empty replacement set
19    // also clears any vectors left by an UPDATE ... SET field = NULL while
20    // retaining strict validation for every non-NULL value.
21    if matches!(value, Value::Null) {
22        return Ok(Vec::new());
23    }
24    match ty {
25        ColumnType::Vector(dim) => {
26            let vector = value_to_vector(value)?;
27            validate_vector_dimensions(*dim, vector.len())?;
28            Ok(vec![vector])
29        }
30        ColumnType::Tensor(dim) => {
31            let tensor = value_to_tensor(value)?;
32            for vector in &tensor {
33                validate_vector_dimensions(*dim, vector.len())?;
34            }
35            Ok(tensor)
36        }
37        _ => Err(SQLError::TypeMismatch(format!(
38            "{} is not vector-indexable",
39            column_type_name(ty)
40        ))),
41    }
42}