Skip to main content

quill_sql/expression/
cast.rs

1use crate::catalog::{Column, DataType, Schema};
2use crate::error::{QuillSQLError, QuillSQLResult};
3use crate::expression::{Expr, ExprTrait};
4use crate::storage::tuple::Tuple;
5use crate::utils::scalar::ScalarValue;
6
7/// Cast expression
8#[derive(Clone, PartialEq, Eq, Debug)]
9pub struct Cast {
10    /// The expression being cast
11    pub expr: Box<Expr>,
12    /// The `DataType` the expression will yield
13    pub data_type: DataType,
14}
15
16impl ExprTrait for Cast {
17    fn data_type(&self, _input_schema: &Schema) -> QuillSQLResult<DataType> {
18        Ok(self.data_type)
19    }
20
21    fn nullable(&self, input_schema: &Schema) -> QuillSQLResult<bool> {
22        self.expr.nullable(input_schema)
23    }
24
25    fn evaluate(&self, tuple: &Tuple) -> QuillSQLResult<ScalarValue> {
26        let value = self.expr.evaluate(tuple)?;
27        value.cast_to(&self.data_type)
28    }
29
30    fn to_column(&self, _input_schema: &Schema) -> QuillSQLResult<Column> {
31        Err(QuillSQLError::Plan(format!(
32            "expr {:?} as column not supported",
33            self
34        )))
35    }
36}
37
38impl std::fmt::Display for Cast {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        write!(f, "CAST {} AS {}", self.expr, self.data_type)
41    }
42}