Skip to main content

vortex_array/expr/
proto.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use itertools::Itertools;
5use vortex_error::VortexResult;
6use vortex_error::vortex_ensure;
7use vortex_error::vortex_err;
8use vortex_session::VortexSession;
9
10use crate::expr::Expression;
11use crate::expr::expression::unbound_variable;
12use crate::proto::expr as pb;
13use crate::scalar_fn::ForeignScalarFnVTable;
14use crate::scalar_fn::ScalarFnId;
15use crate::scalar_fn::session::ScalarFnSessionExt;
16
17pub trait ExprSerializeProtoExt {
18    /// Serialize the expression to its protobuf representation.
19    fn serialize_proto(&self) -> VortexResult<pb::Expr>;
20}
21
22/// The wire id for [`Expression::Root`], retained from when `Root` was a scalar function so that
23/// already-serialized expressions keep round-tripping.
24pub(crate) const ROOT_ID: &str = "vortex.root";
25
26impl ExprSerializeProtoExt for Expression {
27    fn serialize_proto(&self) -> VortexResult<pb::Expr> {
28        if let Expression::Variable(name) = self {
29            return Err(unbound_variable(name));
30        }
31        let Some(scalar_fn) = self.as_scalar() else {
32            return Ok(pb::Expr {
33                id: ROOT_ID.to_string(),
34                children: vec![],
35                metadata: Some(vec![]),
36            });
37        };
38
39        let children = self
40            .children()
41            .iter()
42            .map(|child| child.serialize_proto())
43            .try_collect()?;
44
45        let metadata = scalar_fn.options().serialize()?.ok_or_else(|| {
46            vortex_err!(
47                "Expression '{}' is not serializable: {}",
48                scalar_fn.id(),
49                self
50            )
51        })?;
52
53        Ok(pb::Expr {
54            id: scalar_fn.id().to_string(),
55            children,
56            metadata: Some(metadata),
57        })
58    }
59}
60
61impl Expression {
62    pub fn from_proto(expr: &pb::Expr, session: &VortexSession) -> VortexResult<Expression> {
63        // Root is not a registered scalar fn, so it must be resolved before the registry lookup.
64        if expr.id == ROOT_ID {
65            vortex_ensure!(
66                expr.children.is_empty(),
67                "root expression must have no children, got {}",
68                expr.children.len()
69            );
70            return Ok(Expression::Root);
71        }
72
73        #[expect(clippy::disallowed_methods, reason = "interning a dynamic id")]
74        let expr_id = ScalarFnId::new(expr.id.as_str());
75        let children = expr
76            .children
77            .iter()
78            .map(|e| Expression::from_proto(e, session))
79            .collect::<VortexResult<Vec<_>>>()?;
80
81        let scalar_fn = if let Some(vtable) = session.scalar_fns().registry().get(&expr_id) {
82            vtable.deserialize(expr.metadata(), session)?
83        } else if session.allows_unknown() {
84            ForeignScalarFnVTable::make_scalar_fn(expr_id, expr.metadata().to_vec(), children.len())
85        } else {
86            return Err(vortex_err!("unknown expression id: {}", expr_id));
87        };
88
89        Expression::try_new(scalar_fn, children)
90    }
91}
92
93/// Deserialize a [`Expression`] from the protobuf representation.
94#[deprecated(note = "Use Expression::from_proto instead")]
95pub fn deserialize_expr_proto(
96    expr: &pb::Expr,
97    session: &VortexSession,
98) -> VortexResult<Expression> {
99    Expression::from_proto(expr, session)
100}
101
102#[cfg(test)]
103mod tests {
104    use std::sync::LazyLock;
105
106    use prost::Message;
107    use vortex_session::VortexSession;
108
109    use super::ExprSerializeProtoExt;
110    use crate::expr::Expression;
111    use crate::expr::and;
112    use crate::expr::between;
113    use crate::expr::eq;
114    use crate::expr::get_item;
115    use crate::expr::lit;
116    use crate::expr::or;
117    use crate::expr::root;
118    use crate::proto::expr as pb;
119    use crate::scalar_fn::fns::between::BetweenOptions;
120    use crate::scalar_fn::fns::between::StrictComparison;
121    use crate::scalar_fn::session::ScalarFnSession;
122
123    static SESSION: LazyLock<VortexSession> = LazyLock::new(|| {
124        let session = VortexSession::empty().with::<ScalarFnSession>();
125        session.allow_unknown();
126        session
127    });
128
129    #[test]
130    fn expression_serde() {
131        let expr: Expression = or(
132            and(
133                between(
134                    lit(1),
135                    root(),
136                    get_item("a", root()),
137                    BetweenOptions {
138                        lower_strict: StrictComparison::Strict,
139                        upper_strict: StrictComparison::Strict,
140                    },
141                ),
142                lit(1),
143            ),
144            eq(lit(1), root()),
145        );
146
147        let s_expr = expr.serialize_proto().unwrap();
148        let buf = s_expr.encode_to_vec();
149        let s_expr = pb::Expr::decode(buf.as_slice()).unwrap();
150        let deser_expr = Expression::from_proto(&s_expr, &VortexSession::empty()).unwrap();
151
152        assert_eq!(&deser_expr, &expr);
153    }
154
155    #[test]
156    fn unknown_expression_id_allow_unknown() {
157        let expr_proto = pb::Expr {
158            id: "vortex.test.foreign_scalar_fn".to_string(),
159            metadata: Some(vec![1, 2, 3, 4]),
160            children: vec![root().serialize_proto().unwrap()],
161        };
162
163        let expr = Expression::from_proto(&expr_proto, &SESSION).unwrap();
164        assert_eq!(
165            expr.as_scalar().map(|f| f.id().as_ref().to_string()),
166            Some("vortex.test.foreign_scalar_fn".to_string())
167        );
168
169        let roundtrip = expr.serialize_proto().unwrap();
170        assert_eq!(roundtrip.id, expr_proto.id);
171        assert_eq!(roundtrip.metadata(), expr_proto.metadata());
172        assert_eq!(roundtrip.children, expr_proto.children);
173    }
174}