Repository navigation
Expand file tree
/
Copy pathcodec.rs
More file actions
1602 lines (1486 loc) · 63 KB
/
Copy pathcodec.rs
File metadata and controls
1602 lines (1486 loc) · 63 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! Python-aware extension codecs.
//!
//! Datafusion-python plans can carry references to Python-defined
//! objects that the upstream protobuf codecs do not know how to
//! serialize: pure-Python scalar / aggregate / window UDFs, Python
//! query-planning extensions, and so on. Their state lives inside
//! `Py<PyAny>` callables and closures rather than being recoverable
//! from a name in the receiver's function registry. To ship a plan
//! across a process boundary (pickle, `multiprocessing`, Ray actor,
//! `datafusion-distributed`, etc.) those payloads have to be encoded
//! into the proto wire format itself.
//!
//! [`PythonLogicalCodec`] is the [`LogicalExtensionCodec`] that
//! datafusion-python parks on every `SessionContext`. It wraps a
//! chain of composable codecs and adds Python-aware in-band encoding
//! on top: when the encoder sees a Python-defined UDF, the codec
//! cloudpickles the callable + signature into the `fun_definition`
//! proto field; when the decoder sees a payload it produced, it
//! reconstructs the UDF from the bytes alone — no pre-registration on
//! the receiver. Everything the codec does not recognise is delegated
//! to the chain: each downstream FFI codec installed via
//! `SessionContext.with_logical_extension_codec(...)` is appended, and
//! encoding consults them in install order with
//! `DefaultLogicalExtensionCodec` as the terminal fallback. Decoding
//! does not walk the chain at all — a payload names the codec that
//! wrote it. See [`PythonLogicalCodec`].
//!
//! [`PythonPhysicalCodec`] is the symmetric wrapper around
//! [`PhysicalExtensionCodec`]. Logical and physical layers each have
//! a `try_encode_udf` / `try_decode_udf` pair, so a `ScalarUDF`
//! referenced inside a `LogicalPlan`, an `ExecutionPlan`, or a
//! `PhysicalExpr` must encode identically through either layer for
//! plans to survive a serialization round-trip. Both codecs share
//! the same payload framing for that reason.
//!
//! Payloads emitted by these codecs are framed as
//! `<family_magic: 7 bytes> <version: u8> <py_major: u8> <py_minor: u8> <cloudpickle blob>`.
//! The family magic identifies the UDF flavor; the version byte lets
//! the decoder reject too-new or too-old payloads with a clean error
//! instead of falling into an opaque `cloudpickle` tuple-unpack
//! failure when the tuple shape changes; the Python `(major, minor)`
//! bytes catch the cloudpickle-cross-minor-version case and raise an
//! actionable error instead of an opaque `marshal` failure on load
//! (cloudpickle payloads are not portable across Python minor
//! versions). Dispatch precedence on decode: **family match +
//! supported version + matching Python version → codec chain →
//! caller's `FunctionRegistry` fallback.**
//!
//! ## Wire-format family registry
//!
//! | Layer + kind | Family prefix |
//! | ----------------------------- | ------------- |
//! | `PythonLogicalCodec` scalar | `DFPYUDF` |
//! | `PythonLogicalCodec` agg | `DFPYUDA` |
//! | `PythonLogicalCodec` window | `DFPYUDW` |
//! | `PythonPhysicalCodec` scalar | `DFPYUDF` |
//! | `PythonPhysicalCodec` agg | `DFPYUDA` |
//! | `PythonPhysicalCodec` window | `DFPYUDW` |
//! | User FFI extension codec | user-chosen |
//! | Default codec | (none) |
//!
//! Current wire-format version is [`WIRE_VERSION_CURRENT`]; supported
//! receive range is `WIRE_VERSION_MIN_SUPPORTED..=WIRE_VERSION_CURRENT`.
//! Bump [`WIRE_VERSION_CURRENT`] whenever the cloudpickle tuple shape
//! changes; raise [`WIRE_VERSION_MIN_SUPPORTED`] when dropping support
//! for an older shape.
//!
//! Downstream FFI codecs should pick non-colliding family prefixes
//! (use a `DF` namespace plus a crate-specific suffix) and return an
//! error for *objects* they do not own — on encode, that error is the
//! chain's "not mine" signal, letting the next codec take a turn. A
//! codec that answers `Ok` for objects outside its family claims them
//! ahead of every codec installed after it.
//!
//! Rejecting foreign *payloads* is not asked of a codec, because a
//! payload is only ever handed to the codec whose id it carries. A
//! codec is free to check for a marker of its own anyway — that costs
//! nothing here and is worth keeping for hosts that dispatch by
//! position or by trial — but dispatch never depends on it. See
//! [`PythonLogicalCodec`].
use std::sync::Arc;
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
use arrow::ipc::reader::StreamReader;
use arrow::ipc::writer::StreamWriter;
use datafusion::common::{Result, TableReference};
use datafusion::datasource::TableProvider;
use datafusion::datasource::file_format::FileFormatFactory;
use datafusion::execution::TaskContext;
use datafusion::logical_expr::{
AggregateUDF, AggregateUDFImpl, Extension, LogicalPlan, ScalarUDF, ScalarUDFImpl, Signature,
TypeSignature, Volatility, WindowUDF, WindowUDFImpl,
};
use datafusion::physical_expr::PhysicalExpr;
use datafusion::physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx;
use datafusion::physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx;
use datafusion::physical_plan::ExecutionPlan;
use datafusion_proto::logical_plan::{DefaultLogicalExtensionCodec, LogicalExtensionCodec};
use datafusion_proto::physical_plan::{
DefaultPhysicalExtensionCodec, PhysicalExtensionCodec, PhysicalProtoConverterExtension,
};
use pyo3::prelude::*;
use pyo3::sync::PyOnceLock;
use pyo3::types::{PyBytes, PyTuple};
use crate::errors::to_datafusion_err;
use crate::udaf::PythonFunctionAggregateUDF;
use crate::udf::PythonFunctionScalarUDF;
use crate::udwf::PythonFunctionWindowUDF;
// Wire-format framing for inlined Python UDF payloads.
//
// Layout: `<family_magic: 7 bytes> <version: u8> <py_major: u8> <py_minor: u8> <cloudpickle blob>`.
// The family magic identifies the UDF flavor; the version byte lets
// the decoder reject too-new or too-old payloads with a clean error
// instead of falling into an opaque `cloudpickle` tuple-unpack failure
// when the tuple shape changes; the Python `(major, minor)` bytes
// catch the cloudpickle-cross-minor-version case (cloudpickle is not
// portable across Python minor versions) and raise an actionable
// error instead of an opaque `marshal` failure on load. Bump
// [`WIRE_VERSION_CURRENT`] whenever the tuple shape changes; raise
// [`WIRE_VERSION_MIN_SUPPORTED`] when dropping support for an older
// shape.
/// Family prefix for an inlined Python scalar UDF
/// (cloudpickled tuple of name, callable, input schema, return field,
/// volatility).
pub(crate) const PY_SCALAR_UDF_FAMILY: &[u8] = b"DFPYUDF";
/// Family prefix for an inlined Python aggregate UDF
/// (cloudpickled tuple of name, accumulator factory, input schema bytes,
/// return schema bytes (single-field IPC schema), state schema bytes,
/// volatility).
pub(crate) const PY_AGG_UDF_FAMILY: &[u8] = b"DFPYUDA";
/// Family prefix for an inlined Python window UDF
/// (cloudpickled tuple of name, evaluator factory, input schema bytes,
/// return schema bytes (single-field IPC schema), volatility).
pub(crate) const PY_WINDOW_UDF_FAMILY: &[u8] = b"DFPYUDW";
/// Wire-format version this build emits.
pub(crate) const WIRE_VERSION_CURRENT: u8 = 1;
/// Oldest wire-format version this build still decodes. Bump when
/// retiring support for an older payload shape.
pub(crate) const WIRE_VERSION_MIN_SUPPORTED: u8 = 1;
/// Tag `buf` with the framing header for `family` at the current
/// wire-format version, stamping `py_version` as `(major, minor)`
/// bytes. Append-only — the caller writes the cloudpickle payload
/// after.
fn write_wire_header(buf: &mut Vec<u8>, family: &[u8], py_version: (u8, u8)) {
buf.extend_from_slice(family);
buf.push(WIRE_VERSION_CURRENT);
buf.push(py_version.0);
buf.push(py_version.1);
}
/// Inspect the framing on `buf`.
///
/// * `Ok(None)` — `buf` does not carry `family`. The caller should
/// delegate to its codec chain.
/// * `Ok(Some(payload))` — `buf` carries `family` at a version this
/// build accepts and a Python `(major, minor)` matching
/// `expected_py`; `payload` is the cloudpickle blob.
/// * `Err(_)` — `buf` carries `family` but the wire-format version
/// is outside `WIRE_VERSION_MIN_SUPPORTED..=WIRE_VERSION_CURRENT`,
/// or the stamped Python `(major, minor)` does not match
/// `expected_py`. The error names the offending values so an
/// operator can diagnose sender/receiver drift instead of seeing
/// an opaque cloudpickle tuple-unpack or `marshal` failure.
fn strip_wire_header<'a>(
buf: &'a [u8],
family: &[u8],
kind: &str,
expected_py: (u8, u8),
) -> Result<Option<&'a [u8]>> {
if !buf.starts_with(family) {
return Ok(None);
}
let version_idx = family.len();
let Some(&version) = buf.get(version_idx) else {
return Err(datafusion::error::DataFusionError::Execution(format!(
"Truncated inline Python {kind} payload: missing wire-format version byte"
)));
};
if !(WIRE_VERSION_MIN_SUPPORTED..=WIRE_VERSION_CURRENT).contains(&version) {
return Err(datafusion::error::DataFusionError::Execution(format!(
"Inline Python {kind} payload wire-format version v{version}; \
this build supports v{WIRE_VERSION_MIN_SUPPORTED}..=v{WIRE_VERSION_CURRENT}. \
Align datafusion-python versions on sender and receiver."
)));
}
let py_major_idx = version_idx + 1;
let Some(&encoded_major) = buf.get(py_major_idx) else {
return Err(datafusion::error::DataFusionError::Execution(format!(
"Truncated inline Python {kind} payload: missing Python major version byte"
)));
};
let py_minor_idx = version_idx + 2;
let Some(&encoded_minor) = buf.get(py_minor_idx) else {
return Err(datafusion::error::DataFusionError::Execution(format!(
"Truncated inline Python {kind} payload: missing Python minor version byte"
)));
};
let (current_major, current_minor) = expected_py;
if encoded_major != current_major || encoded_minor != current_minor {
return Err(datafusion::error::DataFusionError::Execution(format!(
"Inline Python {kind} payload was serialized on Python \
{encoded_major}.{encoded_minor} but this process is running Python \
{current_major}.{current_minor}. cloudpickle payloads are not portable \
across Python minor versions. Align Python versions on sender and receiver."
)));
}
Ok(Some(&buf[py_minor_idx + 1..]))
}
/// Family prefix for the envelope wrapping a chained codec's payload.
///
/// A distinct magic is what makes "is this framed?" a definite test
/// rather than a speculative decode. Probing by attempting to parse the
/// envelope would reintroduce exactly the protobuf ambiguity this
/// framing exists to remove: prost skips unknown fields and defaults
/// missing ones, so a foreign payload can parse cleanly as an envelope.
pub(crate) const CHAINED_PAYLOAD_FAMILY: &[u8] = b"DFPYCHN";
/// Wire-format version for the chained-payload envelope. Independent of
/// [`WIRE_VERSION_CURRENT`], which versions the cloudpickle framing.
pub(crate) const CHAIN_WIRE_VERSION_CURRENT: u8 = 1;
/// Oldest chained-payload envelope version this build decodes.
pub(crate) const CHAIN_WIRE_VERSION_MIN_SUPPORTED: u8 = 1;
/// Prefix for the id given to a codec installed from a bare
/// `PyCapsule`.
///
/// Every capsule reports the same type, so there is nothing on it to
/// derive an id from. The rest of the id is a fresh UUID, minted when
/// the codec is installed. Plans it encodes decode on the session that
/// installed it, and on sessions cloned from that one, because cloning
/// copies the chain along with its ids. On any other session the id is
/// simply missing, and decoding says so.
///
/// Numbering the capsules instead — `anon:0`, `anon:1` — would be
/// worse. Every session starts counting at zero, so one session's
/// `anon:0` would be accepted by another session and decoded with
/// whatever codec happened to be its own first capsule.
pub(crate) const ANONYMOUS_CODEC_ID_PREFIX: &str = "anon:";
/// Prefix for the id a `SessionContext` reports when its own codec stack
/// is installed as an extension codec on another session.
///
/// An ordinary codec object takes its id from its class, which is the
/// library's import path. That does not work for a session: every
/// session is an instance of the same class, so they would all report
/// the same id. Installing two sessions as codecs on one target would
/// then collide, and a plan encoded by one would be decoded by the
/// other. A session id is unique per session and stable, so the rest
/// of the id carries that.
pub(crate) const SESSION_CODEC_ID_PREFIX: &str = "session:";
/// One installed codec plus the identity its payloads are tagged with.
///
/// The id is what makes dispatch order-independent. Keying on position
/// in the chain — as `ComposedPhysicalExtensionCodec` does upstream —
/// is sound only when both ends assemble the same list in the same
/// order. That holds for the consumers upstream was written for, whose
/// lists structurally cannot disagree: Ballista's is a compile-time
/// constant, and `datafusion-distributed` pins its own codec at index 0
/// and appends user codecs rebuilt from the same startup code on every
/// node. It does not hold here. A chain is assembled by user Python,
/// and `Expr.to_bytes(ctx1)` / `Expr.from_bytes(ctx2)` puts two
/// independently configured sessions on either end of one payload, so
/// an index would name a different codec in the decoder as soon as
/// install order differed.
struct ChainEntry<C: ?Sized> {
id: Arc<str>,
codec: Arc<C>,
}
impl<C: ?Sized> Clone for ChainEntry<C> {
fn clone(&self) -> Self {
Self {
id: Arc::clone(&self.id),
codec: Arc::clone(&self.codec),
}
}
}
impl<C: ?Sized + std::fmt::Debug> std::fmt::Debug for ChainEntry<C> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ChainEntry")
.field("id", &self.id)
.field("codec", &self.codec)
.finish()
}
}
/// Wrap `blob` in the envelope identifying the codec that produced it.
///
/// Layout: `DFPYCHN | version: u8 | id_len: u32 (LE) | id | blob`.
fn write_chained_payload(buf: &mut Vec<u8>, codec_id: &str, blob: &[u8]) {
buf.extend_from_slice(CHAINED_PAYLOAD_FAMILY);
buf.push(CHAIN_WIRE_VERSION_CURRENT);
buf.extend_from_slice(&(codec_id.len() as u32).to_le_bytes());
buf.extend_from_slice(codec_id.as_bytes());
buf.extend_from_slice(blob);
}
/// Inspect the chained-payload envelope on `buf`.
///
/// * `Ok(None)` — no envelope. The payload came from the terminal
/// codec, which writes unframed so a session with no extension
/// codecs installed produces bytes identical to a build without
/// codec chaining.
/// * `Ok(Some((codec_id, blob)))` — the owning codec's id and its
/// original bytes, byte-for-byte as it wrote them.
fn read_chained_payload(buf: &[u8]) -> Result<Option<(&str, &[u8])>> {
if !buf.starts_with(CHAINED_PAYLOAD_FAMILY) {
return Ok(None);
}
let mut idx = CHAINED_PAYLOAD_FAMILY.len();
let Some(&version) = buf.get(idx) else {
return Err(datafusion::error::DataFusionError::Execution(
"Truncated extension codec payload: missing envelope version byte".to_string(),
));
};
if !(CHAIN_WIRE_VERSION_MIN_SUPPORTED..=CHAIN_WIRE_VERSION_CURRENT).contains(&version) {
return Err(datafusion::error::DataFusionError::Execution(format!(
"Extension codec payload envelope version v{version}; this build supports \
v{CHAIN_WIRE_VERSION_MIN_SUPPORTED}..=v{CHAIN_WIRE_VERSION_CURRENT}. \
Align datafusion-python versions on sender and receiver."
)));
}
idx += 1;
let Some(len_bytes) = buf.get(idx..idx + 4) else {
return Err(datafusion::error::DataFusionError::Execution(
"Truncated extension codec payload: missing codec id length".to_string(),
));
};
let id_len = u32::from_le_bytes(len_bytes.try_into().expect("4 bytes")) as usize;
idx += 4;
let Some(id_bytes) = buf.get(idx..idx + id_len) else {
return Err(datafusion::error::DataFusionError::Execution(
"Truncated extension codec payload: codec id shorter than its declared length"
.to_string(),
));
};
let codec_id = std::str::from_utf8(id_bytes).map_err(|err| {
datafusion::error::DataFusionError::Execution(format!(
"Extension codec payload carries a non-UTF-8 codec id: {err}"
))
})?;
Ok(Some((codec_id, &buf[idx + id_len..])))
}
/// Decode `buf` with the single codec that encoded it.
///
/// Three cases:
///
/// * **Empty `buf`** — nothing was encoded, so there is no tag to
/// dispatch on and every codec is offered the empty buffer in install
/// order. See [`chain_resolve_by_name`] for why that is sound here and
/// why the case exists at all.
/// * **Framed `buf`** — the envelope names its author, so exactly one
/// codec is consulted and its error surfaces verbatim.
/// * **Unframed non-empty `buf`** — the terminal codec wrote it.
///
/// Outside the empty case nothing is ever offered to a codec that did
/// not write it, which is what stops a structurally similar prost
/// message from decoding in the wrong library.
///
/// Do not replace this with a walk that hands `buf` to each codec until
/// one returns `Ok`. That looks simpler and removes the envelope, and it
/// is unsound: protobuf carries no type identity, so a `prost` message
/// decodes cleanly from an unrelated message's bytes whenever their
/// leading field numbers and wire types line up, and an all-defaults
/// message encodes to zero bytes that decode as anything. Requiring
/// each codec to check a marker of its own does not fix it either: the
/// codecs come from libraries this crate does not control, the natural
/// implementation is `MyMessage::decode(buf)`, which has no marker to
/// check and cannot decline, and one library skipping the convention is
/// enough to lose someone else's payload. Upstream shipped that design
/// and reverted it after a Parquet payload decoded as CSV —
/// apache/datafusion#16980, fixed in #16986.
fn chain_decode<C: ?Sized, R>(
chain: &[ChainEntry<C>],
terminal: &Arc<C>,
buf: &[u8],
what: &str,
f: impl Fn(&C, &[u8]) -> Result<R>,
) -> Result<R> {
if buf.is_empty() {
return chain_resolve_by_name(chain, terminal, what, |codec| f(codec, buf));
}
let Some((codec_id, blob)) = read_chained_payload(buf)? else {
return f(terminal.as_ref(), buf);
};
let Some(entry) = chain.iter().find(|entry| &*entry.id == codec_id) else {
let installed = if chain.is_empty() {
"no extension codecs are installed on this session".to_string()
} else {
format!(
"installed: {}",
chain
.iter()
.map(|entry| entry.id.as_ref())
.collect::<Vec<_>>()
.join(", ")
)
};
let hint = if codec_id.starts_with(ANONYMOUS_CODEC_ID_PREFIX) {
". This payload was written by a codec installed from a bare PyCapsule, which \
carries no portable identity. Pass `codec_id=` when installing it if plans must \
cross sessions."
} else {
""
};
return Err(datafusion::error::DataFusionError::Execution(format!(
"{what} was encoded by extension codec '{codec_id}', which is not installed on \
this session ({installed}){hint}"
)));
};
f(entry.codec.as_ref(), blob)
}
/// Resolve an object carrying no payload, by consulting each codec.
///
/// Used only where DataFusion encodes by name: `try_encode_udf` and its
/// aggregate/window siblings return `Ok` writing nothing, and the
/// decoder then tries the `FunctionRegistry` first and the codec second
/// (`from_proto.rs`, the `None => ctx.udf(..).or_else(..)` arm). A codec
/// whose functions are reconstructible from the name alone is reached
/// through that arm and must still be offered the empty buffer.
///
/// This is the one place dispatch cannot be tagged — there are no bytes
/// to tag. It is not the hazard that tagging exists to remove: the
/// question asked here is "do you own the function named `x`", which is
/// name-scoped and answerable, not "do these bytes happen to parse as
/// your message type". Two codecs disagreeing requires them to claim
/// the same function name, which already collides in the registry.
fn chain_resolve_by_name<C: ?Sized, R>(
chain: &[ChainEntry<C>],
terminal: &Arc<C>,
what: &str,
f: impl Fn(&C) -> Result<R>,
) -> Result<R> {
let mut errors: Vec<datafusion::error::DataFusionError> = Vec::new();
for entry in chain {
match f(entry.codec.as_ref()) {
Ok(value) => return Ok(value),
Err(err) => errors.push(err),
}
}
match f(terminal.as_ref()) {
Ok(value) => Ok(value),
Err(err) => {
errors.push(err);
Err(aggregate_chain_errors(what, errors))
}
}
}
/// Collapse per-codec failures into one error. A single failure is
/// returned as-is so a session with no extension codecs behaves exactly
/// like a build without codec chaining.
fn aggregate_chain_errors(
what: &str,
mut errors: Vec<datafusion::error::DataFusionError>,
) -> datafusion::error::DataFusionError {
match errors.len() {
0 => datafusion::error::DataFusionError::Internal(format!(
"Empty extension codec chain while handling {what}"
)),
1 => errors.swap_remove(0),
_ => {
let joined = errors
.iter()
.map(|err| err.to_string())
.collect::<Vec<_>>()
.join("; ");
datafusion::error::DataFusionError::Execution(format!(
"No installed extension codec handled {what}: {joined}"
))
}
}
}
/// Encode through the chain, tagging the payload with its author.
///
/// Entries are consulted in install order and the first one to write
/// bytes wins, so installing a codec can only claim objects no
/// earlier codec claimed. Adding a library therefore never changes how
/// an already-installed library's objects encode.
///
/// Each codec encodes into a scratch buffer so a failed attempt cannot
/// leave partial bytes behind. `Ok` with an empty buffer is "no
/// opinion" rather than a claim, so the walk continues; if nothing
/// writes bytes the result is `Ok` with nothing written, which is
/// DataFusion's encode-by-name signal. Framing that empty result would
/// set `fun_definition` and permanently skip the registry lookup the
/// decoder does first.
///
/// The terminal codec writes unframed, so a session with no extension
/// codecs is byte-compatible with a build predating the chain.
fn chain_encode<C: ?Sized>(
chain: &[ChainEntry<C>],
terminal: &Arc<C>,
buf: &mut Vec<u8>,
what: &str,
f: impl Fn(&C, &mut Vec<u8>) -> Result<()>,
) -> Result<()> {
let mut saw_empty_ok = false;
let mut errors: Vec<datafusion::error::DataFusionError> = Vec::new();
for entry in chain {
let mut scratch = Vec::new();
match f(entry.codec.as_ref(), &mut scratch) {
Ok(()) if !scratch.is_empty() => {
write_chained_payload(buf, &entry.id, &scratch);
return Ok(());
}
Ok(()) => saw_empty_ok = true,
Err(err) => errors.push(err),
}
}
let mut scratch = Vec::new();
match f(terminal.as_ref(), &mut scratch) {
Ok(()) if !scratch.is_empty() => {
buf.extend_from_slice(&scratch);
return Ok(());
}
Ok(()) => saw_empty_ok = true,
Err(err) => errors.push(err),
}
if saw_empty_ok {
return Ok(());
}
Err(aggregate_chain_errors(what, errors))
}
/// `LogicalExtensionCodec` parked on every `SessionContext`. Holds
/// the Python-aware encoding hooks for logical-layer types
/// (`LogicalPlan`, `Expr`) and delegates everything it does not
/// handle to a chain of composable codecs. Each downstream FFI codec
/// installed via `SessionContext.with_logical_extension_codec(...)` is
/// appended to the chain, and `terminal` — normally
/// `DefaultLogicalExtensionCodec` — handles whatever no installed codec
/// claims.
///
/// Every payload an installed codec writes is wrapped in an envelope
/// naming that codec (see [`write_chained_payload`]), so decoding
/// consults exactly the codec that encoded it. Dispatch does not depend
/// on a codec recognizing and rejecting foreign payloads, which is not
/// something a codec can reliably do: a prost message decodes cleanly
/// from another message's bytes whenever their leading field numbers and
/// wire types line up.
///
/// Sitting at the top of the session's logical codec stack means
/// every serializer that reads `session.logical_codec()` automatically
/// picks up Python-aware encoding for free.
///
/// A codec deliberately does **not** retain the session it was built from.
/// Codecs are routinely handed to a provider that is then registered back into
/// that same session, so retaining here would close a cycle:
/// `SessionContext -> catalog -> FFI provider -> FFI codec -> here`. Keeping
/// the weak `FFI_TaskContextProvider` valid is instead a matter of never
/// replacing the session's `Arc<SessionContext>`; see
/// `PySessionContext::set_session_query_planner`.
#[derive(Debug, Clone)]
pub struct PythonLogicalCodec {
chain: Vec<ChainEntry<dyn LogicalExtensionCodec>>,
terminal: Arc<dyn LogicalExtensionCodec>,
python_udf_inlining: bool,
}
impl PythonLogicalCodec {
/// Build a codec with no installed extension codecs and `inner` as
/// the terminal fallback. `inner` is not part of the keyed chain and
/// its payloads are written unframed, so a context built this way
/// serializes byte-identically to one with no chaining at all.
pub fn new(inner: Arc<dyn LogicalExtensionCodec>) -> Self {
Self {
chain: Vec::new(),
terminal: inner,
python_udf_inlining: true,
}
}
/// Return a copy of this codec with `codec` appended to the chain
/// under `id`, preserving the Python-UDF-inlining setting.
///
/// Appending rather than prepending keeps the operation additive:
/// the new codec is consulted for encoding only after every codec
/// already installed, so it can claim objects nothing else claimed
/// but cannot take over an existing library's objects.
pub fn with_additional_codec(
&self,
id: impl Into<Arc<str>>,
codec: Arc<dyn LogicalExtensionCodec>,
) -> Self {
let mut chain = self.chain.clone();
chain.push(ChainEntry {
id: id.into(),
codec,
});
Self {
chain,
terminal: Arc::clone(&self.terminal),
python_udf_inlining: self.python_udf_inlining,
}
}
/// Ids of the installed extension codecs, in install order.
///
/// The terminal codec is not listed: it is not addressable by id
/// because its payloads are written unframed.
pub fn codec_ids(&self) -> Vec<&str> {
self.chain.iter().map(|entry| entry.id.as_ref()).collect()
}
/// Installed extension codecs paired with their ids, in install
/// order. Restores the inspection that the removed `inner()`
/// accessor provided, and exposes the id dispatch keys along with it.
pub fn codecs(&self) -> Vec<(&str, &Arc<dyn LogicalExtensionCodec>)> {
self.chain
.iter()
.map(|entry| (entry.id.as_ref(), &entry.codec))
.collect()
}
/// Terminal codec consulted when no installed codec claims an object.
pub fn terminal(&self) -> &Arc<dyn LogicalExtensionCodec> {
&self.terminal
}
/// Toggle inline encoding of Python UDFs. See
/// `SessionContext.with_python_udf_inlining` (Python) for full
/// behavior and use cases.
///
/// Security scope: strict mode (`false`) narrows only the codec
/// layer — it stops `Expr::from_bytes` from invoking
/// `cloudpickle.loads` on the inline `DFPY*` payload. It does
/// **not** make `pickle.loads(untrusted_bytes)` safe; treat every
/// `pickle.loads` on untrusted input as unsafe regardless of this
/// setting. See `docs/source/user-guide/distributing-work/expressions.md`
/// (Security section) for the full threat model, and Python's
/// [pickle module security warning][1] for why `pickle.loads` is
/// unsafe in general.
///
/// [1]: https://docs.python.org/3/library/pickle.html#module-pickle
pub fn with_python_udf_inlining(mut self, enabled: bool) -> Self {
self.python_udf_inlining = enabled;
self
}
pub fn python_udf_inlining(&self) -> bool {
self.python_udf_inlining
}
}
impl Default for PythonLogicalCodec {
fn default() -> Self {
Self::new(Arc::new(DefaultLogicalExtensionCodec {}))
}
}
impl LogicalExtensionCodec for PythonLogicalCodec {
fn try_decode(
&self,
buf: &[u8],
inputs: &[LogicalPlan],
ctx: &TaskContext,
) -> Result<Extension> {
chain_decode(
&self.chain,
&self.terminal,
buf,
"an extension logical plan node",
|codec, buf| codec.try_decode(buf, inputs, ctx),
)
}
fn try_encode(&self, node: &Extension, buf: &mut Vec<u8>) -> Result<()> {
chain_encode(
&self.chain,
&self.terminal,
buf,
"an extension logical plan node",
|codec, buf| codec.try_encode(node, buf),
)
}
fn try_decode_table_provider(
&self,
buf: &[u8],
table_ref: &TableReference,
schema: SchemaRef,
ctx: &TaskContext,
) -> Result<Arc<dyn TableProvider>> {
chain_decode(
&self.chain,
&self.terminal,
buf,
"a table provider",
|codec, buf| codec.try_decode_table_provider(buf, table_ref, Arc::clone(&schema), ctx),
)
}
fn try_encode_table_provider(
&self,
table_ref: &TableReference,
node: Arc<dyn TableProvider>,
buf: &mut Vec<u8>,
) -> Result<()> {
chain_encode(
&self.chain,
&self.terminal,
buf,
"a table provider",
|codec, buf| codec.try_encode_table_provider(table_ref, Arc::clone(&node), buf),
)
}
fn try_decode_file_format(
&self,
buf: &[u8],
ctx: &TaskContext,
) -> Result<Arc<dyn FileFormatFactory>> {
chain_decode(
&self.chain,
&self.terminal,
buf,
"a file format",
|codec, buf| codec.try_decode_file_format(buf, ctx),
)
}
fn try_encode_file_format(
&self,
buf: &mut Vec<u8>,
node: Arc<dyn FileFormatFactory>,
) -> Result<()> {
chain_encode(
&self.chain,
&self.terminal,
buf,
"a file format",
|codec, buf| codec.try_encode_file_format(buf, Arc::clone(&node)),
)
}
fn try_encode_udf(&self, node: &ScalarUDF, buf: &mut Vec<u8>) -> Result<()> {
if self.python_udf_inlining && try_encode_python_scalar_udf(node, buf)? {
return Ok(());
}
chain_encode(
&self.chain,
&self.terminal,
buf,
"a scalar UDF",
|codec, buf| codec.try_encode_udf(node, buf),
)
}
fn try_decode_udf(&self, name: &str, buf: &[u8]) -> Result<Arc<ScalarUDF>> {
if self.python_udf_inlining {
if let Some(udf) = try_decode_python_scalar_udf(buf)? {
return Ok(udf);
}
} else {
refuse_if_inline(buf, PY_SCALAR_UDF_FAMILY, "scalar UDF", name)?;
}
chain_decode(
&self.chain,
&self.terminal,
buf,
"a scalar UDF",
|codec, buf| codec.try_decode_udf(name, buf),
)
}
fn try_encode_udaf(&self, node: &AggregateUDF, buf: &mut Vec<u8>) -> Result<()> {
if self.python_udf_inlining && try_encode_python_udaf(node, buf)? {
return Ok(());
}
chain_encode(
&self.chain,
&self.terminal,
buf,
"an aggregate UDF",
|codec, buf| codec.try_encode_udaf(node, buf),
)
}
fn try_decode_udaf(&self, name: &str, buf: &[u8]) -> Result<Arc<AggregateUDF>> {
if self.python_udf_inlining {
if let Some(udaf) = try_decode_python_udaf(buf)? {
return Ok(udaf);
}
} else {
refuse_if_inline(buf, PY_AGG_UDF_FAMILY, "aggregate UDF", name)?;
}
chain_decode(
&self.chain,
&self.terminal,
buf,
"an aggregate UDF",
|codec, buf| codec.try_decode_udaf(name, buf),
)
}
fn try_encode_udwf(&self, node: &WindowUDF, buf: &mut Vec<u8>) -> Result<()> {
if self.python_udf_inlining && try_encode_python_udwf(node, buf)? {
return Ok(());
}
chain_encode(
&self.chain,
&self.terminal,
buf,
"a window UDF",
|codec, buf| codec.try_encode_udwf(node, buf),
)
}
fn try_decode_udwf(&self, name: &str, buf: &[u8]) -> Result<Arc<WindowUDF>> {
if self.python_udf_inlining {
if let Some(udwf) = try_decode_python_udwf(buf)? {
return Ok(udwf);
}
} else {
refuse_if_inline(buf, PY_WINDOW_UDF_FAMILY, "window UDF", name)?;
}
chain_decode(
&self.chain,
&self.terminal,
buf,
"a window UDF",
|codec, buf| codec.try_decode_udwf(name, buf),
)
}
}
/// Strict-mode gate: if `buf` is a well-framed inline payload for
/// `family`, return the strict-refusal error; otherwise return
/// `Ok(())` so the caller can delegate to its codec chain.
///
/// Routing through [`read_framed_payload`] (rather than a bare
/// `starts_with` probe) means malformed inline bytes — wrong
/// wire-format version, mismatched Python version, truncated header —
/// surface *their* diagnostic instead of the strict-mode message.
/// The strict message implies sender intent ("inlining is disabled"),
/// so it should fire only when the bytes really would have decoded.
///
/// Fast path: short-circuit on the family-magic prefix before
/// acquiring the GIL. Plans with many non-Python UDFs would otherwise
/// pay a GIL acquisition per decode call just to confirm "not a
/// Python UDF". `read_framed_payload` itself rejects buffers that
/// don't start with `family`, so this is purely an optimization.
fn refuse_if_inline(buf: &[u8], family: &[u8], kind: &str, name: &str) -> Result<()> {
if !buf.starts_with(family) {
return Ok(());
}
Python::attach(|py| match read_framed_payload(py, buf, family, kind)? {
Some(_) => Err(refuse_inline_payload(kind, name)),
None => Ok(()),
})
}
/// Build the error returned by a strict codec when it receives an
/// inline Python-UDF payload it has been told not to deserialize.
fn refuse_inline_payload(kind: &str, name: &str) -> datafusion::error::DataFusionError {
// `Execution`, not `Plan`: this is a wire-format decode refusal at
// codec time, not a planner-stage failure. Downstream error
// classification keys off the variant — surfacing this as a planner
// error would mis-route it into "fix your SQL" buckets.
datafusion::error::DataFusionError::Execution(format!(
"Refusing to deserialize inline Python {kind} '{name}': Python UDF \
inlining is disabled on this session. Two remediations: \
(1) ask the sender to re-encode with inlining disabled so '{name}' \
travels by name, and register '{name}' on this receiver; or \
(2) enable inlining on this receiver (accepts the cloudpickle \
execution risk on inbound payloads). Receivers cannot re-encode \
bytes they did not produce."
))
}
/// `PhysicalExtensionCodec` mirror of [`PythonLogicalCodec`] parked
/// on the same `SessionContext`. Carries the Python-aware encoding
/// hooks for physical-layer types (`ExecutionPlan`, `PhysicalExpr`)
/// and delegates the rest to the composable codec chain (see
/// [`PythonLogicalCodec`] for chain ordering and dispatch rules).
///
/// The `PhysicalExtensionCodec` trait has its own `try_encode_udf`
/// / `try_decode_udf` pair distinct from the logical one, so a
/// `ScalarUDF` referenced inside a physical plan needs Python-aware
/// encoding on this layer too — otherwise a plan with a Python UDF
/// would round-trip at the logical level but break at the physical
/// level. Both layers reuse the shared payload framing
/// ([`PY_SCALAR_UDF_FAMILY`] et al.) so the wire format is identical.
///
/// Like [`PythonLogicalCodec`], this does not retain the session it was built
/// from; see that type for why.
#[derive(Debug, Clone)]
pub struct PythonPhysicalCodec {
chain: Vec<ChainEntry<dyn PhysicalExtensionCodec>>,
terminal: Arc<dyn PhysicalExtensionCodec>,
python_udf_inlining: bool,
}
impl PythonPhysicalCodec {
/// See [`PythonLogicalCodec::new`]; `inner` is the terminal codec
/// rather than a chain entry.
pub fn new(inner: Arc<dyn PhysicalExtensionCodec>) -> Self {
Self {
chain: Vec::new(),
terminal: inner,
python_udf_inlining: true,
}
}
/// Return a copy of this codec with `codec` appended to the chain
/// under `id`. See [`PythonLogicalCodec::with_additional_codec`].
pub fn with_additional_codec(
&self,
id: impl Into<Arc<str>>,
codec: Arc<dyn PhysicalExtensionCodec>,
) -> Self {
let mut chain = self.chain.clone();
chain.push(ChainEntry {
id: id.into(),
codec,
});
Self {
chain,
terminal: Arc::clone(&self.terminal),
python_udf_inlining: self.python_udf_inlining,
}
}
/// Ids of the installed extension codecs, in install order.
pub fn codec_ids(&self) -> Vec<&str> {
self.chain.iter().map(|entry| entry.id.as_ref()).collect()
}
/// Installed extension codecs paired with their ids, in install order.
pub fn codecs(&self) -> Vec<(&str, &Arc<dyn PhysicalExtensionCodec>)> {
self.chain
.iter()
.map(|entry| (entry.id.as_ref(), &entry.codec))
.collect()
}
/// Terminal codec consulted when no installed codec claims an object.
pub fn terminal(&self) -> &Arc<dyn PhysicalExtensionCodec> {
&self.terminal
}
/// Toggle inline encoding of Python UDFs on this physical codec.
///
/// Mirrors [`PythonLogicalCodec::with_python_udf_inlining`]; see
/// that method for the full security and portability discussion.
pub fn with_python_udf_inlining(mut self, enabled: bool) -> Self {
self.python_udf_inlining = enabled;
self
}
pub fn python_udf_inlining(&self) -> bool {
self.python_udf_inlining
}
}
impl Default for PythonPhysicalCodec {
fn default() -> Self {
Self::new(Arc::new(DefaultPhysicalExtensionCodec {}))
}
}
impl PhysicalExtensionCodec for PythonPhysicalCodec {
fn try_decode(
&self,
buf: &[u8],
inputs: &[Arc<dyn ExecutionPlan>],