Skip to main content

parquet/file/
column_crypto_metadata.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//! Column chunk encryption metadata
6
7use std::io::Write;
8
9use crate::errors::{ParquetError, Result};
10use crate::file::metadata::HeapSize;
11use crate::parquet_thrift::{
12    ElementType, FieldType, ReadThrift, ThriftCompactInputProtocol, ThriftCompactOutputProtocol,
13    WriteThrift, WriteThriftField, read_thrift_vec,
14};
15use crate::{thrift_struct, thrift_union};
16
17// define this and ColumnCryptoMetadata here so they're only defined when
18// the encryption feature is enabled
19
20thrift_struct!(
21/// Encryption metadata for a column chunk encrypted with a column-specific key
22pub struct EncryptionWithColumnKey {
23  /// Path to the column in the Parquet schema
24  1: required list<string> path_in_schema
25
26  /// Path to the column in the Parquet schema
27  2: optional binary key_metadata
28}
29);
30
31impl HeapSize for EncryptionWithColumnKey {
32    fn heap_size(&self) -> usize {
33        self.path_in_schema.heap_size() + self.key_metadata.heap_size()
34    }
35}
36
37thrift_union!(
38/// ColumnCryptoMetadata for a column chunk
39union ColumnCryptoMetaData {
40  1: ENCRYPTION_WITH_FOOTER_KEY
41  2: (EncryptionWithColumnKey) ENCRYPTION_WITH_COLUMN_KEY
42}
43);
44
45impl HeapSize for ColumnCryptoMetaData {
46    fn heap_size(&self) -> usize {
47        match self {
48            Self::ENCRYPTION_WITH_FOOTER_KEY => 0,
49            Self::ENCRYPTION_WITH_COLUMN_KEY(path) => path.heap_size(),
50        }
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57    use crate::parquet_thrift::tests::test_roundtrip;
58
59    #[test]
60    fn test_column_crypto_roundtrip() {
61        test_roundtrip(ColumnCryptoMetaData::ENCRYPTION_WITH_FOOTER_KEY);
62
63        let path_in_schema = vec!["foo".to_owned(), "bar".to_owned(), "really".to_owned()];
64        let key_metadata = vec![1u8; 32];
65        test_roundtrip(ColumnCryptoMetaData::ENCRYPTION_WITH_COLUMN_KEY(
66            EncryptionWithColumnKey {
67                path_in_schema: path_in_schema.clone(),
68                key_metadata: None,
69            },
70        ));
71        test_roundtrip(ColumnCryptoMetaData::ENCRYPTION_WITH_COLUMN_KEY(
72            EncryptionWithColumnKey {
73                path_in_schema,
74                key_metadata: Some(key_metadata),
75            },
76        ));
77    }
78}