Skip to main content

datafusion_substrait/
lib.rs

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