quill_sql/expression/
mod.rs

1mod aggregate;
2mod alias;
3mod binary;
4mod cast;
5mod column;
6mod literal;
7mod util;
8
9pub use aggregate::AggregateFunction;
10pub use alias::Alias;
11pub use binary::BinaryExpr;
12pub use cast::Cast;
13pub use column::ColumnExpr;
14pub use literal::Literal;
15pub use util::*;
16
17use crate::catalog::Schema;
18use crate::catalog::{Column, DataType};
19use crate::error::QuillSQLResult;
20use crate::storage::tuple::Tuple;
21use crate::utils::scalar::ScalarValue;
22
23pub trait ExprTrait {
24    /// Get the data type of this expression, given the schema of the input
25    fn data_type(&self, input_schema: &Schema) -> QuillSQLResult<DataType>;
26
27    /// Determine whether this expression is nullable, given the schema of the input
28    fn nullable(&self, input_schema: &Schema) -> QuillSQLResult<bool>;
29
30    /// Evaluate an expression against a Tuple
31    fn evaluate(&self, tuple: &Tuple) -> QuillSQLResult<ScalarValue>;
32
33    /// convert to a column with respect to a schema
34    fn to_column(&self, input_schema: &Schema) -> QuillSQLResult<Column>;
35}
36
37#[derive(Clone, PartialEq, Eq, Debug)]
38pub enum Expr {
39    /// An expression with a specific name.
40    Alias(Alias),
41    /// A named reference to a qualified filed in a schema.
42    Column(ColumnExpr),
43    /// A constant value.
44    Literal(Literal),
45    /// A binary expression such as "age > 21"
46    Binary(BinaryExpr),
47    /// Casts the expression to a given type and will return a runtime error if the expression cannot be cast.
48    /// This expression is guaranteed to have a fixed type.
49    Cast(Cast),
50    /// Represents the call of an aggregate built-in function with arguments.
51    AggregateFunction(AggregateFunction),
52}
53
54impl ExprTrait for Expr {
55    fn data_type(&self, input_schema: &Schema) -> QuillSQLResult<DataType> {
56        match self {
57            Expr::Alias(alias) => alias.data_type(input_schema),
58            Expr::Column(column) => column.data_type(input_schema),
59            Expr::Literal(literal) => literal.data_type(input_schema),
60            Expr::Binary(binary) => binary.data_type(input_schema),
61            Expr::Cast(cast) => cast.data_type(input_schema),
62            Expr::AggregateFunction(aggr) => aggr.data_type(input_schema),
63        }
64    }
65
66    fn nullable(&self, input_schema: &Schema) -> QuillSQLResult<bool> {
67        match self {
68            Expr::Alias(alias) => alias.nullable(input_schema),
69            Expr::Column(column) => column.nullable(input_schema),
70            Expr::Literal(literal) => literal.nullable(input_schema),
71            Expr::Binary(binary) => binary.nullable(input_schema),
72            Expr::Cast(cast) => cast.nullable(input_schema),
73            Expr::AggregateFunction(aggr) => aggr.nullable(input_schema),
74        }
75    }
76
77    fn evaluate(&self, tuple: &Tuple) -> QuillSQLResult<ScalarValue> {
78        match self {
79            Expr::Alias(alias) => alias.evaluate(tuple),
80            Expr::Column(column) => column.evaluate(tuple),
81            Expr::Literal(literal) => literal.evaluate(tuple),
82            Expr::Binary(binary) => binary.evaluate(tuple),
83            Expr::Cast(cast) => cast.evaluate(tuple),
84            Expr::AggregateFunction(aggr) => aggr.evaluate(tuple),
85        }
86    }
87
88    fn to_column(&self, input_schema: &Schema) -> QuillSQLResult<Column> {
89        match self {
90            Expr::Alias(alias) => alias.to_column(input_schema),
91            Expr::Column(column) => column.to_column(input_schema),
92            Expr::Literal(literal) => literal.to_column(input_schema),
93            Expr::Binary(binary) => binary.to_column(input_schema),
94            Expr::Cast(cast) => cast.to_column(input_schema),
95            Expr::AggregateFunction(aggr) => aggr.to_column(input_schema),
96        }
97    }
98}
99
100impl std::fmt::Display for Expr {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        match self {
103            Expr::Alias(e) => write!(f, "{e}"),
104            Expr::Column(e) => write!(f, "{e}"),
105            Expr::Literal(e) => write!(f, "{e}"),
106            Expr::Binary(e) => write!(f, "{e}"),
107            Expr::Cast(e) => write!(f, "{e}"),
108            Expr::AggregateFunction(e) => write!(f, "{e}"),
109        }
110    }
111}