Repository navigation
Expand file tree
/
Copy pathproto_test_util.rs
More file actions
128 lines (113 loc) · 4.14 KB
/
Copy pathproto_test_util.rs
File metadata and controls
128 lines (113 loc) · 4.14 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
// 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
//! Shared test helpers for proto serialization / deserialization in expression unit tests
//! without depending on `datafusion-proto` (which would create circular deps).
use std::cell::Cell;
use std::sync::Arc;
use arrow::datatypes::Schema;
use datafusion_common::{DataFusionError, Result};
use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
use datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecode;
use datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncode;
use datafusion_proto_models::protobuf::{self, PhysicalExprNode, physical_expr_node};
use crate::expressions::Column;
/// A proto node for a `Column`, useful as a stand-in child node when building
/// an expression's proto representation in tests.
pub(crate) fn column_node(name: &str) -> PhysicalExprNode {
PhysicalExprNode {
expr_id: None,
expr_type: Some(physical_expr_node::ExprType::Column(
protobuf::PhysicalColumn {
name: name.to_string(),
index: 0,
},
)),
}
}
/// Decoder stub for driving `try_from_proto`: returns a fixed `Column` for each
/// child node, optionally failing on the Nth `decode` call so the
/// `ctx.decode(..)?` error arms can be exercised.
pub(crate) struct StubDecoder {
fail_on_call: Option<usize>,
calls: Cell<usize>,
}
impl StubDecoder {
/// Always succeeds, returning a placeholder `Column` per child.
pub(crate) fn ok() -> Self {
Self {
fail_on_call: None,
calls: Cell::new(0),
}
}
/// Fails on the `call`-th invocation (1-based), succeeding otherwise.
pub(crate) fn failing_on(call: usize) -> Self {
Self {
fail_on_call: Some(call),
calls: Cell::new(0),
}
}
}
impl PhysicalExprDecode for StubDecoder {
fn decode(
&self,
_node: &PhysicalExprNode,
_schema: &Schema,
) -> Result<Arc<dyn PhysicalExpr>> {
let call = self.calls.get() + 1;
self.calls.set(call);
if Some(call) == self.fail_on_call {
return Err(DataFusionError::Internal(format!(
"stub decode failure on call {call}"
)));
}
Ok(Arc::new(Column::new("decoded", 0)))
}
}
/// Decoder that must never run: used to assert that the reject paths of a
/// `try_from_proto` (wrong node, missing child) bail out before decoding.
pub(crate) struct UnreachableDecoder;
impl PhysicalExprDecode for UnreachableDecoder {
fn decode(
&self,
_node: &PhysicalExprNode,
_schema: &Schema,
) -> Result<Arc<dyn PhysicalExpr>> {
unreachable!("decode must not be reached when the node is rejected")
}
}
/// Encoder stub for driving `try_to_proto`: emits a placeholder `Column` node
/// for each child, optionally failing on the Nth `encode` call so the
/// `ctx.encode_child(..)?` error arms can be exercised.
pub(crate) struct StubEncoder {
fail_on_call: Option<usize>,
calls: Cell<usize>,
}
impl StubEncoder {
/// Always succeeds, emitting a placeholder `Column` node per child.
pub(crate) fn ok() -> Self {
Self {
fail_on_call: None,
calls: Cell::new(0),
}
}
/// Fails on the `call`-th invocation (1-based), succeeding otherwise.
pub(crate) fn failing_on(call: usize) -> Self {
Self {
fail_on_call: Some(call),
calls: Cell::new(0),
}
}
}
impl PhysicalExprEncode for StubEncoder {
fn encode(&self, _expr: &Arc<dyn PhysicalExpr>) -> Result<PhysicalExprNode> {
let call = self.calls.get() + 1;
self.calls.set(call);
if Some(call) == self.fail_on_call {
return Err(DataFusionError::Internal(format!(
"stub encode failure on call {call}"
)));
}
Ok(column_node("child"))
}
}