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