pub struct TypeChecker<'a, C: Catalog + ?Sized> { /* private fields */ }Expand description
Type checker for SQL expressions.
Performs type inference and validation for expressions, ensuring that operations are valid for the types involved and that constraints are met.
§Examples
use alopex_sql::catalog::MemoryCatalog;
use alopex_sql::planner::type_checker::TypeChecker;
let catalog = MemoryCatalog::new();
let type_checker = TypeChecker::new(&catalog);Implementations§
Source§impl<'a, C: Catalog + ?Sized> TypeChecker<'a, C>
impl<'a, C: Catalog + ?Sized> TypeChecker<'a, C>
Sourcepub fn infer_type(
&self,
expr: &Expr,
table: &TableMetadata,
) -> Result<TypedExpr, PlannerError>
pub fn infer_type( &self, expr: &Expr, table: &TableMetadata, ) -> Result<TypedExpr, PlannerError>
Infer the type of an expression within a table context.
Recursively analyzes the expression to determine its type, resolving column references against the provided table metadata.
§Errors
Returns an error if:
- A column reference cannot be resolved
- A binary operation is invalid for the operand types
- A function call has invalid arguments
Sourcepub fn check_binary_op(
&self,
op: BinaryOp,
left: &ResolvedType,
right: &ResolvedType,
span: Span,
) -> Result<ResolvedType, PlannerError>
pub fn check_binary_op( &self, op: BinaryOp, left: &ResolvedType, right: &ResolvedType, span: Span, ) -> Result<ResolvedType, PlannerError>
Check binary operation and return the result type.
Validates that the operator is valid for the given operand types and returns the result type.
§Type Rules
- Arithmetic operators (+, -, *, /, %): Require numeric operands
- Comparison operators (=, <>, <, >, <=, >=): Require compatible types
- Logical operators (AND, OR): Require boolean operands
- String concatenation (||): Requires text operands
Sourcepub fn normalize_metric(
&self,
metric: &str,
span: Span,
) -> Result<VectorMetric, PlannerError>
pub fn normalize_metric( &self, metric: &str, span: Span, ) -> Result<VectorMetric, PlannerError>
Sourcepub fn check_function_call(
&self,
name: &str,
args: &[TypedExpr],
span: Span,
) -> Result<ResolvedType, PlannerError>
pub fn check_function_call( &self, name: &str, args: &[TypedExpr], span: Span, ) -> Result<ResolvedType, PlannerError>
Check function call and return the result type.
Validates that the function arguments have correct types and returns the result type.
Sourcepub fn check_vector_distance(
&self,
args: &[TypedExpr],
span: Span,
) -> Result<ResolvedType, PlannerError>
pub fn check_vector_distance( &self, args: &[TypedExpr], span: Span, ) -> Result<ResolvedType, PlannerError>
Check vector_distance function arguments.
Signature: vector_distance(column: Vector, vector: Vector, metric: Text) -> Double
§Requirements
- First argument must be a Vector type (column reference)
- Second argument must be a Vector type (vector literal)
- Third argument must be a Text type (metric string)
- Vector dimensions must match
Sourcepub fn check_vector_similarity(
&self,
args: &[TypedExpr],
span: Span,
) -> Result<ResolvedType, PlannerError>
pub fn check_vector_similarity( &self, args: &[TypedExpr], span: Span, ) -> Result<ResolvedType, PlannerError>
Check vector_similarity function arguments.
Signature: vector_similarity(column: Vector, vector: Vector, metric: Text) -> Double
Same validation rules as vector_distance.
Sourcepub fn check_vector_dimension(
&self,
expected: u32,
found: u32,
span: Span,
) -> Result<(), PlannerError>
pub fn check_vector_dimension( &self, expected: u32, found: u32, span: Span, ) -> Result<(), PlannerError>
Check that two vector dimensions match.
§Errors
Returns PlannerError::VectorDimensionMismatch if dimensions don’t match.
Sourcepub fn check_insert_values(
&self,
table: &TableMetadata,
columns: &[String],
values: &[Vec<Expr>],
span: Span,
) -> Result<Vec<Vec<TypedExpr>>, PlannerError>
pub fn check_insert_values( &self, table: &TableMetadata, columns: &[String], values: &[Vec<Expr>], span: Span, ) -> Result<Vec<Vec<TypedExpr>>, PlannerError>
Check INSERT values against table columns.
Validates that:
- The number of values matches the number of columns
- Each value’s type is compatible with the column type
- NOT NULL constraints are satisfied
- Vector dimensions match for vector columns
§Column Order
If columns is empty, uses TableMetadata.column_names() order (definition order).
§Errors
ColumnValueCountMismatch: Number of values doesn’t match columnsTypeMismatch: Value type incompatible with column typeNullConstraintViolation: NULL value for NOT NULL columnVectorDimensionMismatch: Vector dimension mismatch
Sourcepub fn check_assignment(
&self,
table: &TableMetadata,
column: &str,
value: &Expr,
span: Span,
) -> Result<TypedExpr, PlannerError>
pub fn check_assignment( &self, table: &TableMetadata, column: &str, value: &Expr, span: Span, ) -> Result<TypedExpr, PlannerError>
Check UPDATE assignment type compatibility.
Validates that the value’s type is compatible with the column type.
§Errors
ColumnNotFound: Column doesn’t existTypeMismatch: Value type incompatible with column typeNullConstraintViolation: NULL value for NOT NULL columnVectorDimensionMismatch: Vector dimension mismatch
Sourcepub fn check_null_constraint(
&self,
column: &ColumnMetadata,
value: &TypedExpr,
span: Span,
) -> Result<(), PlannerError>
pub fn check_null_constraint( &self, column: &ColumnMetadata, value: &TypedExpr, span: Span, ) -> Result<(), PlannerError>
Check NOT NULL constraint for a value.
§Errors
Returns PlannerError::NullConstraintViolation if the column has NOT NULL
constraint and the value is NULL.