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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
//! The `PhysicalExpr` face of the session-scoped [`ProtoDecoderRegistry`],
//! which routes extension expressions to their decoders by name:
//! [`ExtensionExprFromProto::register_proto`] and [`physical_expr_names`], all keyed on `dyn PhysicalExpr`. The store itself
//! is shared with every other extension kind, so a session carries one
//! registry.
use Arc;
use Result;
use PhysicalExpr;
pub use PhysicalExprDecodeCtx;
pub use PhysicalExprEncodeCtx;
pub use ProtoDecoderRegistry;
use PhysicalExprNode;
use ExprType;
/// The wire name of an extension [`PhysicalExpr`], and the constructor that
/// rebuilds it.
///
/// Only extension expressions implement this. A built-in expression has an
/// `ExprType` variant of its own, keeps the inherent `try_from_proto`
/// dispatched from that variant, and has no wire name to be registered under —
/// so it has no [`register`](Self::register_proto) method.
///
/// One impl block says what the expression is called and how it decodes; its
/// `try_to_proto` on [`PhysicalExpr`] writes the matching node with
/// [`extension_expr_node`]:
///
/// ```ignore
/// impl PhysicalExpr for MyExpr {
/// // ...
/// fn try_to_proto(
/// &self,
/// ctx: &PhysicalExprEncodeCtx<'_>,
/// ) -> Result<Option<PhysicalExprNode>> {
/// // Stamps `Self::NAME`, so the encoded name and the registry key
/// // cannot drift apart.
/// Ok(Some(extension_expr_node::<Self>(
/// ctx,
/// self.state.to_bytes()?,
/// self.children(),
/// )?))
/// }
/// }
///
/// impl ExtensionExprFromProto for MyExpr {
/// // Namespace the name with the owning crate, so a collision with
/// // another crate is an error at registration and not a wrong decode.
/// const NAME: &'static str = "my_crate.MyExpr";
///
/// fn try_from_proto(
/// node: &PhysicalExprNode,
/// ctx: &PhysicalExprDecodeCtx<'_>,
/// ) -> Result<Arc<dyn PhysicalExpr>> {
/// let extension = expect_expr_variant!(
/// node,
/// physical_expr_node::ExprType::Extension,
/// "Extension",
/// );
/// let state = MyState::from_bytes(&extension.expr)?;
/// let children = ctx.decode_children_expressions(&extension.inputs)?;
/// Ok(Arc::new(MyExpr::new(state, children)))
/// }
/// }
///
/// let mut config = SessionConfig::new();
/// config.update_extension(MyExpr::register_proto)?;
/// ```
/// Build the `PhysicalExprNode` for an extension expression `T`: an
/// `Extension` variant carrying `payload`, the encoded `children`, and `T`'s
/// [`NAME`](ExtensionExprFromProto::NAME).
///
/// The one supported way for an extension expression to write itself. It is a
/// free function rather than a method on [`PhysicalExprEncodeCtx`] because the
/// context lives in `datafusion-physical-expr-common`, below the crate that
/// can name [`ExtensionExprFromProto`].
/// How this facade stores a decoder in the shared registry: a function pointer
/// to the monomorphized [`ExtensionExprFromProto::try_from_proto`].
///
/// Deliberately private, and the same type on both the
/// [`ExtensionExprFromProto::register_proto`] and the [`decode_physical_expr`] side.
/// [`ExtensionExprFromProto`] is the public contract and
/// [`decode_physical_expr`] (hidden, internal to DataFusion) is the only way
/// to invoke one, so the storage
/// can change later without a breaking change.
type PhysicalExprDecoder =
fn ;
/// Decode `node` with the extension expression decoder registered under the
/// name `node` carries.
///
/// The name is read from the node's `Extension` variant rather than passed in,
/// so a caller cannot pair a node with a name it does not carry.
///
/// `None` means "this node names no extension expression decoder of ours": it
/// is not an extension node, it carries no name, or no registered name matches.
/// The caller then falls back to the `PhysicalExtensionCodec` chain.
/// `Some(Err(..))` means the decoder that *does* own the name failed, which is
/// fatal: falling back there would let a codec decode the payload wrongly, the
/// very thing the name exists to prevent.
///
/// Not intended as public API: `datafusion-proto` drives decoding and is the
/// only intended caller. It is `pub` only because that call crosses a crate
/// boundary, so it is hidden from the docs and may change without notice.
/// Every extension expression name registered in `registry`, in arbitrary
/// order.
///
/// Names registered for another kind are not included.
/// The registry name `node` was written with, if it is an extension node that
/// carries one.