|
| 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