1use std::fmt::{Debug, Display};
5use std::hash::Hash;
6use std::ops::Deref;
7
8use vortex_array::{ArrayRef, DeserializeMetadata, SerializeMetadata};
9use vortex_dtype::DType;
10use vortex_error::VortexResult;
11
12use crate::{
13 AnalysisExpr, ExprEncoding, ExprEncodingRef, ExprId, ExprRef, IntoExpr, Scope, VortexExpr,
14};
15
16pub trait VTable: 'static + Sized + Send + Sync + Debug {
17 type Expr: 'static
18 + Send
19 + Sync
20 + Clone
21 + Debug
22 + Display
23 + PartialEq
24 + Hash
25 + Deref<Target = dyn VortexExpr>
26 + IntoExpr
27 + AnalysisExpr;
28 type Encoding: 'static + Send + Sync + Deref<Target = dyn ExprEncoding>;
29 type Metadata: SerializeMetadata + DeserializeMetadata + Debug;
30
31 fn id(encoding: &Self::Encoding) -> ExprId;
33
34 fn encoding(expr: &Self::Expr) -> ExprEncodingRef;
36
37 fn metadata(expr: &Self::Expr) -> Option<Self::Metadata>;
40
41 fn children(expr: &Self::Expr) -> Vec<&ExprRef>;
43
44 fn with_children(expr: &Self::Expr, children: Vec<ExprRef>) -> VortexResult<Self::Expr>;
50
51 fn build(
53 encoding: &Self::Encoding,
54 metadata: &<Self::Metadata as DeserializeMetadata>::Output,
55 children: Vec<ExprRef>,
56 ) -> VortexResult<Self::Expr>;
57
58 fn evaluate(expr: &Self::Expr, scope: &Scope) -> VortexResult<ArrayRef>;
60
61 fn return_dtype(expr: &Self::Expr, scope: &DType) -> VortexResult<DType>;
63}
64
65#[macro_export]
66macro_rules! vtable {
67 ($V:ident) => {
68 $crate::aliases::paste::paste! {
69 #[derive(Debug)]
70 pub struct [<$V VTable>];
71
72 impl AsRef<dyn $crate::VortexExpr> for [<$V Expr>] {
73 fn as_ref(&self) -> &dyn $crate::VortexExpr {
74 unsafe { &*(self as *const [<$V Expr>] as *const $crate::ExprAdapter<[<$V VTable>]>) }
76 }
77 }
78
79 impl std::ops::Deref for [<$V Expr>] {
80 type Target = dyn $crate::VortexExpr;
81
82 fn deref(&self) -> &Self::Target {
83 unsafe { &*(self as *const [<$V Expr>] as *const $crate::ExprAdapter<[<$V VTable>]>) }
85 }
86 }
87
88 impl $crate::IntoExpr for [<$V Expr>] {
89 fn into_expr(self) -> $crate::ExprRef {
90 std::sync::Arc::new(unsafe { std::mem::transmute::<[<$V Expr>], $crate::ExprAdapter::<[<$V VTable>]>>(self) })
92 }
93 }
94
95 impl From<[<$V Expr>]> for $crate::ExprRef {
96 fn from(value: [<$V Expr>]) -> $crate::ExprRef {
97 use $crate::IntoExpr;
98 value.into_expr()
99 }
100 }
101
102 impl AsRef<dyn $crate::ExprEncoding> for [<$V ExprEncoding>] {
103 fn as_ref(&self) -> &dyn $crate::ExprEncoding {
104 unsafe { &*(self as *const [<$V ExprEncoding>] as *const $crate::ExprEncodingAdapter<[<$V VTable>]>) }
106 }
107 }
108
109 impl std::ops::Deref for [<$V ExprEncoding>] {
110 type Target = dyn $crate::ExprEncoding;
111
112 fn deref(&self) -> &Self::Target {
113 unsafe { &*(self as *const [<$V ExprEncoding>] as *const $crate::ExprEncodingAdapter<[<$V VTable>]>) }
115 }
116 }
117 }
118 };
119}
120
121#[cfg(test)]
122mod tests {
123
124 use rstest::{fixture, rstest};
125
126 use super::*;
127 use crate::proto::{ExprSerializeProtoExt, deserialize_expr_proto};
128 use crate::*;
129
130 #[fixture]
131 #[once]
132 fn registry() -> ExprRegistry {
133 ExprRegistry::default()
134 }
135
136 #[rstest]
137 #[case(root())]
139 #[case(select(["hello", "world"], root()))]
140 #[case(select_exclude(["world", "hello"], root()))]
141 #[case(lit(42i32))]
143 #[case(lit(std::f64::consts::PI))]
144 #[case(lit(true))]
145 #[case(lit("hello"))]
146 #[case(col("column_name"))]
148 #[case(get_item("field", root()))]
149 #[case(eq(col("a"), lit(10)))]
151 #[case(not_eq(col("a"), lit(10)))]
152 #[case(gt(col("a"), lit(10)))]
153 #[case(gt_eq(col("a"), lit(10)))]
154 #[case(lt(col("a"), lit(10)))]
155 #[case(lt_eq(col("a"), lit(10)))]
156 #[case(and(col("a"), col("b")))]
158 #[case(or(col("a"), col("b")))]
159 #[case(not(col("a")))]
160 #[case(checked_add(col("a"), lit(5)))]
162 #[case(is_null(col("nullable_col")))]
164 #[case(cast(
166 col("a"),
167 DType::Primitive(vortex_dtype::PType::I64, vortex_dtype::Nullability::NonNullable)
168 ))]
169 #[case(between(col("a"), lit(10), lit(20), vortex_array::compute::BetweenOptions { lower_strict: vortex_array::compute::StrictComparison::NonStrict, upper_strict: vortex_array::compute::StrictComparison::NonStrict }))]
171 #[case(list_contains(col("list_col"), lit("item")))]
173 #[case(pack([("field1", col("a")), ("field2", col("b"))], vortex_dtype::Nullability::NonNullable))]
175 #[case(merge([col("struct1"), col("struct2")], vortex_dtype::Nullability::NonNullable))]
177 #[case(and(gt(col("a"), lit(0)), lt(col("a"), lit(100))))]
179 #[case(or(is_null(col("a")), eq(col("a"), lit(0))))]
180 #[case(not(and(eq(col("status"), lit("active")), gt(col("age"), lit(18)))))]
181 fn text_expr_serde_round_trip(
182 registry: &ExprRegistry,
183 #[case] expr: ExprRef,
184 ) -> anyhow::Result<()> {
185 let serialized_pb = expr.serialize_proto()?;
186 let deserialized_expr = deserialize_expr_proto(&serialized_pb, registry)?;
187
188 assert_eq!(&expr, &deserialized_expr);
189
190 Ok(())
191 }
192}