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