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