1use datafusion_common::DataFusionError;
12use datafusion_common::display::{PlanType, StringifiedPlan};
13use datafusion_common::{
14 JoinConstraint, JoinType, NullEquality, TableReference, UnnestOptions,
15};
16
17use crate::generated::datafusion_common::EmptyMessage;
18use crate::protobuf::{
19 self, AnalyzedLogicalPlanType, OptimizedLogicalPlanType, OptimizedPhysicalPlanType,
20 RecursionUnnestOption,
21 plan_type::PlanTypeEnum::{
22 AnalyzedLogicalPlan, FinalAnalyzedLogicalPlan, FinalLogicalPlan,
23 FinalPhysicalPlan, FinalPhysicalPlanWithSchema, FinalPhysicalPlanWithStats,
24 InitialLogicalPlan, InitialPhysicalPlan, InitialPhysicalPlanWithSchema,
25 InitialPhysicalPlanWithStats, OptimizedLogicalPlan, OptimizedPhysicalPlan,
26 PhysicalPlanError,
27 },
28};
29
30impl From<&UnnestOptions> for protobuf::UnnestOptions {
31 fn from(opts: &UnnestOptions) -> Self {
32 use datafusion_common::NullHandling;
33 use protobuf::unnest_options::NullHandling as ProtoNullHandling;
34 let null_handling = match opts.null_handling {
35 NullHandling::Preserve => ProtoNullHandling::Preserve,
36 NullHandling::Drop => ProtoNullHandling::Drop,
37 NullHandling::PreserveAndExpandEmpty => {
38 ProtoNullHandling::PreserveAndExpandEmpty
39 }
40 } as i32;
41 Self {
42 null_handling,
43 recursions: opts
44 .recursions
45 .iter()
46 .map(|r| RecursionUnnestOption {
47 input_column: Some((&r.input_column).into()),
48 output_column: Some((&r.output_column).into()),
49 depth: r.depth as u32,
50 })
51 .collect(),
52 }
53 }
54}
55
56impl From<&StringifiedPlan> for protobuf::StringifiedPlan {
57 fn from(stringified_plan: &StringifiedPlan) -> Self {
58 Self {
59 plan_type: match stringified_plan.clone().plan_type {
60 PlanType::InitialLogicalPlan => Some(protobuf::PlanType {
61 plan_type_enum: Some(InitialLogicalPlan(EmptyMessage {})),
62 }),
63 PlanType::AnalyzedLogicalPlan { analyzer_name } => {
64 Some(protobuf::PlanType {
65 plan_type_enum: Some(AnalyzedLogicalPlan(
66 AnalyzedLogicalPlanType { analyzer_name },
67 )),
68 })
69 }
70 PlanType::FinalAnalyzedLogicalPlan => Some(protobuf::PlanType {
71 plan_type_enum: Some(FinalAnalyzedLogicalPlan(EmptyMessage {})),
72 }),
73 PlanType::OptimizedLogicalPlan { optimizer_name } => {
74 Some(protobuf::PlanType {
75 plan_type_enum: Some(OptimizedLogicalPlan(
76 OptimizedLogicalPlanType { optimizer_name },
77 )),
78 })
79 }
80 PlanType::FinalLogicalPlan => Some(protobuf::PlanType {
81 plan_type_enum: Some(FinalLogicalPlan(EmptyMessage {})),
82 }),
83 PlanType::InitialPhysicalPlan => Some(protobuf::PlanType {
84 plan_type_enum: Some(InitialPhysicalPlan(EmptyMessage {})),
85 }),
86 PlanType::OptimizedPhysicalPlan { optimizer_name } => {
87 Some(protobuf::PlanType {
88 plan_type_enum: Some(OptimizedPhysicalPlan(
89 OptimizedPhysicalPlanType { optimizer_name },
90 )),
91 })
92 }
93 PlanType::FinalPhysicalPlan => Some(protobuf::PlanType {
94 plan_type_enum: Some(FinalPhysicalPlan(EmptyMessage {})),
95 }),
96 PlanType::InitialPhysicalPlanWithStats => Some(protobuf::PlanType {
97 plan_type_enum: Some(InitialPhysicalPlanWithStats(EmptyMessage {})),
98 }),
99 PlanType::InitialPhysicalPlanWithSchema => Some(protobuf::PlanType {
100 plan_type_enum: Some(InitialPhysicalPlanWithSchema(EmptyMessage {})),
101 }),
102 PlanType::FinalPhysicalPlanWithStats => Some(protobuf::PlanType {
103 plan_type_enum: Some(FinalPhysicalPlanWithStats(EmptyMessage {})),
104 }),
105 PlanType::FinalPhysicalPlanWithSchema => Some(protobuf::PlanType {
106 plan_type_enum: Some(FinalPhysicalPlanWithSchema(EmptyMessage {})),
107 }),
108 PlanType::PhysicalPlanError => Some(protobuf::PlanType {
109 plan_type_enum: Some(PhysicalPlanError(EmptyMessage {})),
110 }),
111 },
112 plan: stringified_plan.plan.to_string(),
113 }
114 }
115}
116
117impl From<TableReference> for protobuf::TableReference {
118 fn from(t: TableReference) -> Self {
119 use protobuf::table_reference::TableReferenceEnum;
120 let table_reference_enum = match t {
121 TableReference::Bare { table } => {
122 TableReferenceEnum::Bare(protobuf::BareTableReference {
123 table: table.to_string(),
124 })
125 }
126 TableReference::Partial { schema, table } => {
127 TableReferenceEnum::Partial(protobuf::PartialTableReference {
128 schema: schema.to_string(),
129 table: table.to_string(),
130 })
131 }
132 TableReference::Full {
133 catalog,
134 schema,
135 table,
136 } => TableReferenceEnum::Full(protobuf::FullTableReference {
137 catalog: catalog.to_string(),
138 schema: schema.to_string(),
139 table: table.to_string(),
140 }),
141 };
142
143 protobuf::TableReference {
144 table_reference_enum: Some(table_reference_enum),
145 }
146 }
147}
148
149impl From<JoinType> for protobuf::JoinType {
150 fn from(t: JoinType) -> Self {
151 match t {
152 JoinType::Inner => protobuf::JoinType::Inner,
153 JoinType::Left => protobuf::JoinType::Left,
154 JoinType::Right => protobuf::JoinType::Right,
155 JoinType::Full => protobuf::JoinType::Full,
156 JoinType::LeftSemi => protobuf::JoinType::Leftsemi,
157 JoinType::RightSemi => protobuf::JoinType::Rightsemi,
158 JoinType::LeftAnti => protobuf::JoinType::Leftanti,
159 JoinType::RightAnti => protobuf::JoinType::Rightanti,
160 JoinType::LeftMark => protobuf::JoinType::Leftmark,
161 JoinType::RightMark => protobuf::JoinType::Rightmark,
162 }
163 }
164}
165
166impl From<JoinConstraint> for protobuf::JoinConstraint {
167 fn from(t: JoinConstraint) -> Self {
168 match t {
169 JoinConstraint::On => protobuf::JoinConstraint::On,
170 JoinConstraint::Using => protobuf::JoinConstraint::Using,
171 }
172 }
173}
174
175impl From<NullEquality> for protobuf::NullEquality {
176 fn from(t: NullEquality) -> Self {
177 match t {
178 NullEquality::NullEqualsNothing => protobuf::NullEquality::NullEqualsNothing,
179 NullEquality::NullEqualsNull => protobuf::NullEquality::NullEqualsNull,
180 }
181 }
182}
183
184impl<T> TryFrom<&[T]> for protobuf::FileGroup
197where
198 for<'a> &'a T: TryInto<protobuf::PartitionedFile, Error = DataFusionError>,
199{
200 type Error = DataFusionError;
201
202 fn try_from(files: &[T]) -> Result<Self, Self::Error> {
203 Ok(protobuf::FileGroup {
204 files: files
205 .iter()
206 .map(TryInto::try_into)
207 .collect::<Result<Vec<_>, _>>()?,
208 })
209 }
210}
211
212#[cfg(test)]
213mod tests {
214 use datafusion_common::{NullHandling, RecursionUnnestOption};
215
216 use super::*;
217
218 #[test]
219 fn table_reference_roundtrip() {
220 for reference in [
221 TableReference::bare("t"),
222 TableReference::partial("s", "t"),
223 TableReference::full("c", "s", "t"),
224 ] {
225 let encoded = protobuf::TableReference::from(reference.clone());
226 let decoded = TableReference::try_from(encoded).unwrap();
227 assert_eq!(decoded, reference);
228 }
229 }
230
231 #[test]
232 fn table_reference_from_proto_rejects_missing_oneof() {
233 let proto = protobuf::TableReference {
234 table_reference_enum: None,
235 };
236 let err = TableReference::try_from(proto).unwrap_err();
237 assert!(
238 err.to_string().contains("table_reference_enum"),
239 "unexpected error: {err}"
240 );
241 }
242
243 #[test]
244 fn join_enums_roundtrip() {
245 for join_type in [
246 JoinType::Inner,
247 JoinType::Left,
248 JoinType::Right,
249 JoinType::Full,
250 JoinType::LeftSemi,
251 JoinType::RightSemi,
252 JoinType::LeftAnti,
253 JoinType::RightAnti,
254 JoinType::LeftMark,
255 JoinType::RightMark,
256 ] {
257 assert_eq!(
258 JoinType::from(protobuf::JoinType::from(join_type)),
259 join_type
260 );
261 }
262 for constraint in [JoinConstraint::On, JoinConstraint::Using] {
263 assert_eq!(
264 JoinConstraint::from(protobuf::JoinConstraint::from(constraint)),
265 constraint
266 );
267 }
268 for null_equality in [
269 NullEquality::NullEqualsNothing,
270 NullEquality::NullEqualsNull,
271 ] {
272 assert_eq!(
273 NullEquality::from(protobuf::NullEquality::from(null_equality)),
274 null_equality
275 );
276 }
277 }
278
279 #[test]
280 fn unnest_options_roundtrip() {
281 let options = UnnestOptions {
282 null_handling: NullHandling::Drop,
283 recursions: vec![RecursionUnnestOption {
284 input_column: "a".into(),
285 output_column: "b".into(),
286 depth: 2,
287 }],
288 };
289
290 let encoded = protobuf::UnnestOptions::from(&options);
291 let decoded = UnnestOptions::from(&encoded);
292
293 assert_eq!(decoded.null_handling, options.null_handling);
294 assert_eq!(decoded.recursions, options.recursions);
295 }
296
297 #[test]
298 fn stringified_plan_roundtrip() {
299 let plan = StringifiedPlan::new(
300 PlanType::OptimizedLogicalPlan {
301 optimizer_name: "push_down_filter".to_string(),
302 },
303 "some plan",
304 );
305
306 let encoded = protobuf::StringifiedPlan::from(&plan);
307 let decoded = StringifiedPlan::from(&encoded);
308
309 assert_eq!(decoded.plan_type, plan.plan_type);
310 assert_eq!(decoded.plan, plan.plan);
311 }
312}