Skip to content

Commit cbd8c17

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 736b50e commit cbd8c17

3 files changed

Lines changed: 149 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: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 conversion traits as a stopgap during the `datafusion-proto-models`
24+
//! extraction, and `cargo-semver-checks` has no lint for a removed hand-written
25+
//! trait impl, so nothing caught the break. Coercing each conversion to a `fn`
26+
//! pointer here does — moving an impl between crates is fine, removing one stops
27+
//! compiling.
28+
//!
29+
//! Only the spelling is asserted. Behaviour is covered by the round-trip tests
30+
//! next to each impl.
31+
32+
use datafusion_common::config::{
33+
CsvOptions, JsonOptions, ParquetCdcOptions, ParquetColumnOptions, ParquetOptions,
34+
TableParquetOptions,
35+
};
36+
use datafusion_common::display::StringifiedPlan;
37+
use datafusion_common::{
38+
JoinConstraint, JoinType, NullEquality, TableReference, UnnestOptions,
39+
};
40+
use datafusion_datasource::file_groups::FileGroup;
41+
use datafusion_datasource::file_sink_config::FileSinkConfig;
42+
use datafusion_datasource::{FileRange, PartitionedFile};
43+
use datafusion_datasource_csv::file_format::{CsvFormatFactory, CsvSink};
44+
use datafusion_datasource_json::file_format::{JsonFormatFactory, JsonSink};
45+
use datafusion_datasource_parquet::file_format::{ParquetFormatFactory, ParquetSink};
46+
use datafusion_expr::dml::MergeIntoClauseKind;
47+
use datafusion_expr::expr::NullTreatment;
48+
use datafusion_expr::{WindowFrame, WindowFrameBound, WindowFrameUnits};
49+
use datafusion_physical_expr::expressions::Column;
50+
use datafusion_proto::protobuf;
51+
52+
/// Asserts `T: From<F>` by naming the conversion.
53+
fn assert_from<F, T: From<F>>() {
54+
let _: fn(F) -> T = From::from;
55+
}
56+
57+
/// Asserts `T: TryFrom<F>` by naming the conversion.
58+
fn assert_try_from<F, T: TryFrom<F>>() {
59+
let _: fn(F) -> Result<T, T::Error> = TryFrom::try_from;
60+
}
61+
62+
#[test]
63+
fn file_scan_conversions_are_std_traits() {
64+
assert_try_from::<&protobuf::PartitionedFile, PartitionedFile>();
65+
assert_try_from::<&PartitionedFile, protobuf::PartitionedFile>();
66+
assert_try_from::<&protobuf::FileRange, FileRange>();
67+
assert_try_from::<&FileRange, protobuf::FileRange>();
68+
assert_try_from::<&protobuf::FileGroup, FileGroup>();
69+
assert_try_from::<&FileGroup, protobuf::FileGroup>();
70+
assert_from::<&protobuf::PhysicalColumn, Column>();
71+
assert_from::<&Column, protobuf::PhysicalColumn>();
72+
assert_try_from::<&[PartitionedFile], protobuf::FileGroup>();
73+
}
74+
75+
#[test]
76+
fn file_sink_conversions_are_std_traits() {
77+
assert_try_from::<&protobuf::FileSinkConfig, FileSinkConfig>();
78+
assert_try_from::<&FileSinkConfig, protobuf::FileSinkConfig>();
79+
assert_try_from::<&protobuf::JsonSink, JsonSink>();
80+
assert_try_from::<&JsonSink, protobuf::JsonSink>();
81+
assert_try_from::<&protobuf::CsvSink, CsvSink>();
82+
assert_try_from::<&CsvSink, protobuf::CsvSink>();
83+
assert_try_from::<&protobuf::ParquetSink, ParquetSink>();
84+
assert_try_from::<&ParquetSink, protobuf::ParquetSink>();
85+
}
86+
87+
#[test]
88+
fn window_frame_conversions_are_std_traits() {
89+
assert_try_from::<protobuf::WindowFrame, WindowFrame>();
90+
assert_try_from::<&WindowFrame, protobuf::WindowFrame>();
91+
assert_try_from::<protobuf::WindowFrameBound, WindowFrameBound>();
92+
assert_try_from::<&WindowFrameBound, protobuf::WindowFrameBound>();
93+
assert_from::<protobuf::WindowFrameUnits, WindowFrameUnits>();
94+
assert_from::<WindowFrameUnits, protobuf::WindowFrameUnits>();
95+
assert_from::<protobuf::NullTreatment, NullTreatment>();
96+
assert_from::<NullTreatment, protobuf::NullTreatment>();
97+
assert_from::<protobuf::merge_into_clause_node::Kind, MergeIntoClauseKind>();
98+
assert_from::<MergeIntoClauseKind, protobuf::merge_into_clause_node::Kind>();
99+
}
100+
101+
#[test]
102+
fn common_type_conversions_are_std_traits() {
103+
assert_from::<&protobuf::UnnestOptions, UnnestOptions>();
104+
assert_from::<&UnnestOptions, protobuf::UnnestOptions>();
105+
assert_try_from::<protobuf::TableReference, TableReference>();
106+
assert_from::<TableReference, protobuf::TableReference>();
107+
assert_from::<&protobuf::StringifiedPlan, StringifiedPlan>();
108+
assert_from::<&StringifiedPlan, protobuf::StringifiedPlan>();
109+
assert_from::<protobuf::JoinType, JoinType>();
110+
assert_from::<JoinType, protobuf::JoinType>();
111+
assert_from::<protobuf::JoinConstraint, JoinConstraint>();
112+
assert_from::<JoinConstraint, protobuf::JoinConstraint>();
113+
assert_from::<protobuf::NullEquality, NullEquality>();
114+
assert_from::<NullEquality, protobuf::NullEquality>();
115+
}
116+
117+
#[test]
118+
fn file_format_option_conversions_are_std_traits() {
119+
assert_from::<&protobuf::CsvOptions, CsvOptions>();
120+
assert_from::<&protobuf::JsonOptions, JsonOptions>();
121+
assert_try_from::<&protobuf::ParquetOptions, ParquetOptions>();
122+
assert_from::<protobuf::ParquetColumnOptions, ParquetColumnOptions>();
123+
assert_from::<protobuf::ParquetCdcOptions, ParquetCdcOptions>();
124+
assert_try_from::<&protobuf::TableParquetOptions, TableParquetOptions>();
125+
assert_from::<&CsvFormatFactory, protobuf::CsvOptions>();
126+
assert_from::<&JsonFormatFactory, protobuf::JsonOptions>();
127+
assert_from::<&ParquetFormatFactory, protobuf::TableParquetOptions>();
128+
}

0 commit comments

Comments
 (0)