datafusion_substrait/lib.rs
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#![doc(
19 html_logo_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg",
20 html_favicon_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg"
21)]
22#![cfg_attr(docsrs, feature(doc_cfg))]
23// Make sure fast / cheap clones on Arc are explicit:
24// https://github.com/apache/datafusion/issues/11143
25#![cfg_attr(not(test), deny(clippy::clone_on_ref_ptr))]
26
27//! Serialize / Deserialize DataFusion Plans to [Substrait.io]
28//!
29//! This crate provides support for serializing and deserializing both DataFusion
30//! [`LogicalPlan`] and [`ExecutionPlan`] to and from the generated types in
31//! [substrait::proto] from the [substrait] crate.
32//!
33//! [Substrait.io] provides a cross-language serialization format for relational
34//! algebra (e.g. query plans and expressions), based on protocol buffers.
35//!
36//! [Substrait.io]: https://substrait.io/
37//!
38//! [`LogicalPlan`]: datafusion::logical_expr::LogicalPlan
39//! [`ExecutionPlan`]: datafusion::physical_plan::ExecutionPlan
40//!
41//! Potential uses of this crate:
42//! * Use DataFusion to run Substrait plans created by other systems (e.g. Apache Calcite)
43//! * Use DataFusion to create plans to run on other systems
44//! * Pass query plans over FFI boundaries, such as from Python to Rust
45//! * Pass query plans across node boundaries
46//!
47//! # See Also
48//!
49//! Substrait does not (yet) support the full range of plans and expressions
50//! that DataFusion offers. See the [datafusion-proto] crate for a DataFusion
51//! specific format that does support of the full range.
52//!
53//! [datafusion-proto]: https://docs.rs/datafusion-proto/latest/datafusion_proto
54//!
55//! Note that generated types such as [`substrait::proto::Plan`] and
56//! [`substrait::proto::Rel`] can be serialized / deserialized to bytes, JSON and
57//! other formats using [prost] and the rest of the Rust protobuf ecosystem.
58//!
59//! # Example: Serializing [`LogicalPlan`]s
60//! ```
61//! # use datafusion::prelude::*;
62//! # use datafusion::error::Result;
63//! # #[tokio::main(flavor = "current_thread")]
64//! # async fn main() -> Result<()>{
65//! # use std::sync::Arc;
66//! # use datafusion::arrow::array::{Int32Array, RecordBatch};
67//! # use datafusion_substrait::logical_plan;
68//! // Create a plan that scans table 't'
69//! let ctx = SessionContext::new();
70//! let batch = RecordBatch::try_from_iter(vec![(
71//! "x",
72//! Arc::new(Int32Array::from(vec![42])) as _,
73//! )])?;
74//! ctx.register_batch("t", batch)?;
75//! let df = ctx.sql("SELECT x from t").await?;
76//! let plan = df.into_optimized_plan()?;
77//!
78//! // Convert the plan into a substrait (protobuf) Plan
79//! let substrait_plan = logical_plan::producer::to_substrait_plan(&plan, &ctx.state())?;
80//!
81//! // Receive a substrait protobuf from somewhere, and turn it into a LogicalPlan
82//! let logical_round_trip =
83//! logical_plan::consumer::from_substrait_plan(&ctx.state(), &substrait_plan)
84//! .await?;
85//! let logical_round_trip = ctx.state().optimize(&logical_round_trip)?;
86//! assert_eq!(format!("{:?}", plan), format!("{:?}", logical_round_trip));
87//! # Ok(())
88//! # }
89//! ```
90pub mod extensions;
91pub mod logical_plan;
92#[cfg(feature = "physical")]
93pub mod physical_plan;
94pub mod serializer;
95pub mod variation_const;
96
97// Re-export substrait crate
98pub use substrait;