Skip to content

Commit 308ab7e

Browse files
adriangbclaude
andcommitted
test(proto): guard the public From/TryFrom proto conversions
Restores `From<&protobuf::PhysicalColumn> for Column` (and adds the encoding direction, which `Column::try_to_proto` now uses) — the last of the 39 conversions 54.1.0 published. Adds `tests/cases/public_conversions.rs`, which coerces every one of those conversions to a `fn` pointer. `cargo-semver-checks` has no lint for a removed hand-written trait impl, which is why this class of break went unnoticed; a compile-time reference does catch it, and stays quiet when an impl merely moves between crates. Part of apache#24019. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent 87bb5b8 commit 308ab7e

3 files changed

Lines changed: 144 additions & 8 deletions

File tree

‎datafusion/physical-expr/src/expressions/column.rs‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,28 @@ impl PhysicalExpr for Column {
155155
use datafusion_proto_models::protobuf;
156156
Ok(Some(protobuf::PhysicalExprNode {
157157
expr_id: None,
158-
expr_type: Some(protobuf::physical_expr_node::ExprType::Column(
159-
protobuf::PhysicalColumn {
160-
name: self.name.clone(),
161-
index: self.index as u32,
162-
},
163-
)),
158+
expr_type: Some(protobuf::physical_expr_node::ExprType::Column(self.into())),
164159
}))
165160
}
166161
}
167162

163+
#[cfg(feature = "proto")]
164+
impl From<&datafusion_proto_models::protobuf::PhysicalColumn> for Column {
165+
fn from(c: &datafusion_proto_models::protobuf::PhysicalColumn) -> Self {
166+
Column::new(&c.name, c.index as usize)
167+
}
168+
}
169+
170+
#[cfg(feature = "proto")]
171+
impl From<&Column> for datafusion_proto_models::protobuf::PhysicalColumn {
172+
fn from(c: &Column) -> Self {
173+
Self {
174+
name: c.name.clone(),
175+
index: c.index as u32,
176+
}
177+
}
178+
}
179+
168180
#[cfg(feature = "proto")]
169181
impl Column {
170182
/// Reconstruct a [`Column`] from its protobuf representation.
@@ -184,12 +196,12 @@ impl Column {
184196
) -> Result<Arc<dyn PhysicalExpr>> {
185197
use datafusion_physical_expr_common::expect_expr_variant;
186198
use datafusion_proto_models::protobuf;
187-
let protobuf::PhysicalColumn { name, index } = expect_expr_variant!(
199+
let column = expect_expr_variant!(
188200
node,
189201
protobuf::physical_expr_node::ExprType::Column,
190202
"Column",
191203
);
192-
Ok(Arc::new(Column::new(name, *index as usize)))
204+
Ok(Arc::new(Column::from(column)))
193205
}
194206
}
195207

‎datafusion/proto/tests/cases/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use std::fmt::Debug;
3232
use std::hash::Hash;
3333
use std::sync::Arc;
3434

35+
mod public_conversions;
3536
mod roundtrip_logical_plan;
3637
mod roundtrip_physical_plan;
3738
mod serialize;
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//! Compile-time guard for the `From` / `TryFrom` conversions between DataFusion
19+
//! types and `datafusion_proto::protobuf` messages that downstream crates call.
20+
//!
21+
//! These impls were silently dropped once (see
22+
//! <https://github.com/apache/datafusion/issues/24019>): they were replaced by
23+
//! crate-local `FromProto` / `TryFromProto` traits, which `cargo-semver-checks`
24+
//! has no lint for, so nothing caught the break. Coercing each conversion to a
25+
//! `fn` pointer here does — moving an impl between crates is fine, removing one
26+
//! stops compiling.
27+
//!
28+
//! Only the spelling is asserted. Behaviour is covered by the round-trip tests
29+
//! next to each impl.
30+
31+
use datafusion_common::config::{
32+
CsvOptions, JsonOptions, ParquetColumnOptions, ParquetOptions, TableParquetOptions,
33+
};
34+
use datafusion_common::display::StringifiedPlan;
35+
use datafusion_common::{
36+
JoinConstraint, JoinType, NullEquality, TableReference, UnnestOptions,
37+
};
38+
use datafusion_datasource::file_groups::FileGroup;
39+
use datafusion_datasource::file_sink_config::FileSinkConfig;
40+
use datafusion_datasource::{FileRange, PartitionedFile};
41+
use datafusion_datasource_csv::file_format::CsvSink;
42+
use datafusion_datasource_json::file_format::JsonSink;
43+
use datafusion_datasource_parquet::file_format::ParquetSink;
44+
use datafusion_expr::expr::NullTreatment;
45+
use datafusion_expr::{WindowFrame, WindowFrameBound, WindowFrameUnits};
46+
use datafusion_physical_expr::expressions::Column;
47+
use datafusion_proto::physical_plan::to_proto::partitioned_files_to_proto;
48+
use datafusion_proto::protobuf;
49+
50+
/// Asserts `T: From<F>` by naming the conversion.
51+
fn assert_from<F, T: From<F>>() {
52+
let _: fn(F) -> T = From::from;
53+
}
54+
55+
/// Asserts `T: TryFrom<F>` by naming the conversion.
56+
fn assert_try_from<F, T: TryFrom<F>>() {
57+
let _: fn(F) -> Result<T, T::Error> = TryFrom::try_from;
58+
}
59+
60+
#[test]
61+
fn file_scan_conversions_are_std_traits() {
62+
assert_try_from::<&protobuf::PartitionedFile, PartitionedFile>();
63+
assert_try_from::<&PartitionedFile, protobuf::PartitionedFile>();
64+
assert_try_from::<&protobuf::FileRange, FileRange>();
65+
assert_try_from::<&FileRange, protobuf::FileRange>();
66+
assert_try_from::<&protobuf::FileGroup, FileGroup>();
67+
assert_try_from::<&FileGroup, protobuf::FileGroup>();
68+
assert_from::<&protobuf::PhysicalColumn, Column>();
69+
assert_from::<&Column, protobuf::PhysicalColumn>();
70+
71+
// The slice form has no local type to hang an impl on, so it is a function.
72+
let _: fn(&[PartitionedFile]) -> datafusion_common::Result<protobuf::FileGroup> =
73+
partitioned_files_to_proto;
74+
}
75+
76+
#[test]
77+
fn file_sink_conversions_are_std_traits() {
78+
assert_try_from::<&protobuf::FileSinkConfig, FileSinkConfig>();
79+
assert_try_from::<&FileSinkConfig, protobuf::FileSinkConfig>();
80+
assert_try_from::<&protobuf::JsonSink, JsonSink>();
81+
assert_try_from::<&JsonSink, protobuf::JsonSink>();
82+
assert_try_from::<&protobuf::CsvSink, CsvSink>();
83+
assert_try_from::<&CsvSink, protobuf::CsvSink>();
84+
assert_try_from::<&protobuf::ParquetSink, ParquetSink>();
85+
assert_try_from::<&ParquetSink, protobuf::ParquetSink>();
86+
}
87+
88+
#[test]
89+
fn window_frame_conversions_are_std_traits() {
90+
assert_try_from::<protobuf::WindowFrame, WindowFrame>();
91+
assert_try_from::<&WindowFrame, protobuf::WindowFrame>();
92+
assert_try_from::<protobuf::WindowFrameBound, WindowFrameBound>();
93+
assert_try_from::<&WindowFrameBound, protobuf::WindowFrameBound>();
94+
assert_from::<protobuf::WindowFrameUnits, WindowFrameUnits>();
95+
assert_from::<WindowFrameUnits, protobuf::WindowFrameUnits>();
96+
assert_from::<protobuf::NullTreatment, NullTreatment>();
97+
assert_from::<NullTreatment, protobuf::NullTreatment>();
98+
}
99+
100+
#[test]
101+
fn common_type_conversions_are_std_traits() {
102+
assert_from::<&protobuf::UnnestOptions, UnnestOptions>();
103+
assert_from::<&UnnestOptions, protobuf::UnnestOptions>();
104+
assert_try_from::<protobuf::TableReference, TableReference>();
105+
assert_from::<TableReference, protobuf::TableReference>();
106+
assert_from::<&protobuf::StringifiedPlan, StringifiedPlan>();
107+
assert_from::<&StringifiedPlan, protobuf::StringifiedPlan>();
108+
assert_from::<protobuf::JoinType, JoinType>();
109+
assert_from::<JoinType, protobuf::JoinType>();
110+
assert_from::<protobuf::JoinConstraint, JoinConstraint>();
111+
assert_from::<JoinConstraint, protobuf::JoinConstraint>();
112+
assert_from::<protobuf::NullEquality, NullEquality>();
113+
assert_from::<NullEquality, protobuf::NullEquality>();
114+
}
115+
116+
#[test]
117+
fn file_format_option_conversions_are_std_traits() {
118+
assert_from::<&protobuf::CsvOptions, CsvOptions>();
119+
assert_from::<&protobuf::JsonOptions, JsonOptions>();
120+
assert_try_from::<&protobuf::ParquetOptions, ParquetOptions>();
121+
assert_from::<protobuf::ParquetColumnOptions, ParquetColumnOptions>();
122+
assert_try_from::<&protobuf::TableParquetOptions, TableParquetOptions>();
123+
}

0 commit comments

Comments
 (0)