datafusion_proto_common/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#![cfg_attr(test, allow(clippy::needless_pass_by_value))]
19#![doc(
20 html_logo_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg",
21 html_favicon_url = "https://raw.githubusercontent.com/apache/datafusion/19fe44cf2f30cbdd63d4a4f52c74055163c6cc38/docs/logos/standalone_logo/logo_original.svg"
22)]
23#![cfg_attr(docsrs, feature(doc_cfg))]
24// Make sure fast / cheap clones on Arc are explicit:
25// https://github.com/apache/datafusion/issues/11143
26#![deny(clippy::clone_on_ref_ptr)]
27
28//! Serialize / Deserialize DataFusion Primitive Types to bytes
29//!
30//! This crate provides support for serializing and deserializing the
31//! following structures to and from bytes:
32//!
33//! 1. [`ScalarValue`]'s
34//!
35//! [`ScalarValue`]: datafusion_common::ScalarValue
36//!
37//! Internally, this crate is implemented by converting the common types to [protocol
38//! buffers] using [prost].
39//!
40//! [protocol buffers]: https://developers.google.com/protocol-buffers
41//! [prost]: https://docs.rs/prost/latest/prost/
42//!
43//! # Version Compatibility
44//!
45//! The serialized form are not guaranteed to be compatible across
46//! DataFusion versions. A plan serialized with one version of DataFusion
47//! may not be able to deserialized with a different version.
48//!
49//! # See Also
50//!
51//! The binary format created by this crate supports the full range of DataFusion
52//! plans, but is DataFusion specific. See [datafusion-substrait] for a crate
53//! which can encode many DataFusion plans using the [substrait.io] standard.
54//!
55//! [datafusion-substrait]: https://docs.rs/datafusion-substrait/latest/datafusion_substrait
56//! [substrait.io]: https://substrait.io
57//!
58//! # Example: Serializing [`ScalarValue`]s
59//! ```
60//! # use datafusion_common::{ScalarValue, Result};
61//! # use prost::{bytes::{Bytes, BytesMut}};
62//! # use datafusion_common::plan_datafusion_err;
63//! # use datafusion_proto_common::protobuf_common;
64//! # use prost::Message;
65//! # fn main() -> Result<()>{
66//! // Create a new ScalarValue
67//! let val = ScalarValue::UInt64(Some(3));
68//! let mut buffer = BytesMut::new();
69//! let protobuf: protobuf_common::ScalarValue = match val {
70//! ScalarValue::UInt64(Some(val)) => protobuf_common::ScalarValue {
71//! value: Some(protobuf_common::scalar_value::Value::Uint64Value(val)),
72//! },
73//! _ => unreachable!(),
74//! };
75//!
76//! protobuf
77//! .encode(&mut buffer)
78//! .map_err(|e| plan_datafusion_err!("Error encoding protobuf as bytes: {e}"))?;
79//! // Convert it to bytes (for sending over the network, etc.)
80//! let bytes: Bytes = buffer.into();
81//!
82//! let protobuf = protobuf_common::ScalarValue::decode(bytes).map_err(|e| {
83//! plan_datafusion_err!("Error decoding ScalarValue as protobuf: {e}")
84//! })?;
85//! // Decode bytes from somewhere (over network, etc.) back to ScalarValue
86//! let decoded_val: ScalarValue = match protobuf.value {
87//! Some(protobuf_common::scalar_value::Value::Uint64Value(val)) => {
88//! ScalarValue::UInt64(Some(val))
89//! }
90//! _ => unreachable!(),
91//! };
92//! assert_eq!(val, decoded_val);
93//! # Ok(())
94//! # }
95//! ```
96
97pub mod common;
98pub mod from_proto;
99pub mod generated;
100pub mod to_proto;
101
102pub use from_proto::Error as FromProtoError;
103pub use generated::datafusion_proto_common as protobuf_common;
104pub use generated::datafusion_proto_common::*;
105pub use to_proto::Error as ToProtoError;
106
107#[cfg(doctest)]
108doc_comment::doctest!("../README.md", readme_example_test);