Skip to main content

polars_io/ipc/
pl_ipc_metadata.rs

1use polars_utils::IdxSize;
2
3pub static POLARS_IPC_METADATA_KEY: &str = "__POLARS_IPC_METADATA";
4
5#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct PlIpcMetadata {
8    /// Cumulative length including the current record batch.
9    pub record_batch_cum_len: Vec<IdxSize>,
10}
11
12impl PlIpcMetadata {
13    /// Reads the Polars metadata out of an already parsed IPC footer.
14    ///
15    /// `None` for a file that was not written by Polars.
16    pub fn from_ipc_footer(metadata: &arrow::io::ipc::read::FileMetadata) -> Option<Self> {
17        #[cfg(feature = "serde")]
18        {
19            let raw = metadata
20                .custom_metadata
21                .as_ref()?
22                .get(POLARS_IPC_METADATA_KEY)?;
23            serde_json::from_str(raw).ok()
24        }
25        #[cfg(not(feature = "serde"))]
26        {
27            let _ = metadata;
28            None
29        }
30    }
31
32    /// Total number of rows over all record batches.
33    pub fn num_rows(&self) -> Option<IdxSize> {
34        self.record_batch_cum_len.last().copied()
35    }
36}