-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: Parquet modular encryption #16351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e668b99
d38dba4
5a2b456
c972676
ec3f828
3538a27
a754992
e430672
7fcba70
d6b1fca
3353186
e4bc0e3
f52e79c
5615ac8
61bc78e
a81855f
4cf12b3
f29bec3
86fe04b
d4ea63f
0fcc4a5
86db3a5
b34441a
668d728
ec1e8da
e233408
9ffaae4
8e244e9
2871d51
c405167
506801e
3058a90
e7e521a
bbeecfe
4ceb072
c998378
7780b33
219d0b3
2682292
5914d4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Test parquet encryption and decryption in DataFusion SQL. | ||
See datafusion/common/src/config.rs for equivalent rust code | ||
*/ | ||
|
||
-- Keys are hex encoded, you can generate these via encode, e.g. | ||
select encode('0123456789012345', 'hex'); | ||
/* | ||
Expected output: | ||
+----------------------------------------------+ | ||
| encode(Utf8("0123456789012345"),Utf8("hex")) | | ||
+----------------------------------------------+ | ||
| 30313233343536373839303132333435 | | ||
+----------------------------------------------+ | ||
*/ | ||
|
||
CREATE EXTERNAL TABLE encrypted_parquet_table | ||
( | ||
double_field double, | ||
float_field float | ||
) | ||
STORED AS PARQUET LOCATION 'pq/' OPTIONS ( | ||
'format.crypto.file_encryption.encrypt_footer' 'true', | ||
'format.crypto.file_encryption.footer_key_as_hex' '30313233343536373839303132333435', -- b"0123456789012345" | ||
'format.crypto.file_encryption.column_key_as_hex::double_field' '31323334353637383930313233343530', -- b"1234567890123450" | ||
'format.crypto.file_encryption.column_key_as_hex::float_field' '31323334353637383930313233343531', -- b"1234567890123451" | ||
-- Same for decryption | ||
'format.crypto.file_decryption.footer_key_as_hex' '30313233343536373839303132333435', -- b"0123456789012345" | ||
'format.crypto.file_decryption.column_key_as_hex::double_field' '31323334353637383930313233343530', -- b"1234567890123450" | ||
'format.crypto.file_decryption.column_key_as_hex::float_field' '31323334353637383930313233343531', -- b"1234567890123451" | ||
); | ||
|
||
CREATE TABLE temp_table ( | ||
double_field double, | ||
float_field float | ||
); | ||
|
||
INSERT INTO temp_table VALUES(-1.0, -1.0); | ||
INSERT INTO temp_table VALUES(1.0, 2.0); | ||
INSERT INTO temp_table VALUES(3.0, 4.0); | ||
INSERT INTO temp_table VALUES(5.0, 6.0); | ||
|
||
INSERT INTO TABLE encrypted_parquet_table(double_field, float_field) SELECT * FROM temp_table; | ||
|
||
SELECT * FROM encrypted_parquet_table | ||
WHERE double_field > 0.0 AND float_field > 0.0; | ||
|
||
/* | ||
Expected output: | ||
+--------------+-------------+ | ||
| double_field | float_field | | ||
+--------------+-------------+ | ||
| 1.0 | 2.0 | | ||
| 5.0 | 6.0 | | ||
| 3.0 | 4.0 | | ||
+--------------+-------------+ | ||
*/ | ||
|
||
CREATE EXTERNAL TABLE parquet_table | ||
( | ||
double_field double, | ||
float_field float | ||
) | ||
STORED AS PARQUET LOCATION 'pq/'; | ||
|
||
SELECT * FROM parquet_table; | ||
/* | ||
Expected output: | ||
Parquet error: Parquet error: Parquet file has an encrypted footer but decryption properties were not provided | ||
*/ | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could use a little help here. I think it makes sense to add a CLI example for how to use encryption properties. And / or there should be a test of the CLI with these properties. However, the tests seem to only be basic SQL so this doesn't really fit. So maybe this should just be an example that I put somewhere else? |
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use datafusion::common::DataFusionError; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ran this example and it works great
|
||
use datafusion::config::TableParquetOptions; | ||
use datafusion::dataframe::{DataFrame, DataFrameWriteOptions}; | ||
use datafusion::logical_expr::{col, lit}; | ||
use datafusion::parquet::encryption::decrypt::FileDecryptionProperties; | ||
use datafusion::parquet::encryption::encrypt::FileEncryptionProperties; | ||
use datafusion::prelude::{ParquetReadOptions, SessionContext}; | ||
use tempfile::TempDir; | ||
|
||
#[tokio::main] | ||
async fn main() -> datafusion::common::Result<()> { | ||
// The SessionContext is the main high level API for interacting with DataFusion | ||
let ctx = SessionContext::new(); | ||
|
||
// Find the local path of "alltypes_plain.parquet" | ||
let testdata = datafusion::test_util::parquet_test_data(); | ||
let filename = &format!("{testdata}/alltypes_plain.parquet"); | ||
|
||
// Read the sample parquet file | ||
let parquet_df = ctx | ||
.read_parquet(filename, ParquetReadOptions::default()) | ||
.await?; | ||
|
||
// Show information from the dataframe | ||
println!( | ||
"===============================================================================" | ||
); | ||
println!("Original Parquet DataFrame:"); | ||
query_dataframe(&parquet_df).await?; | ||
|
||
// Setup encryption and decryption properties | ||
let (encrypt, decrypt) = setup_encryption(&parquet_df)?; | ||
|
||
// Create a temporary file location for the encrypted parquet file | ||
let tmp_dir = TempDir::new()?; | ||
let tempfile = tmp_dir.path().join("alltypes_plain-encrypted.parquet"); | ||
let tempfile_str = tempfile.into_os_string().into_string().unwrap(); | ||
|
||
// Write encrypted parquet | ||
let mut options = TableParquetOptions::default(); | ||
options.crypto.file_encryption = Some((&encrypt).into()); | ||
parquet_df | ||
.write_parquet( | ||
tempfile_str.as_str(), | ||
DataFrameWriteOptions::new().with_single_file_output(true), | ||
Some(options), | ||
) | ||
.await?; | ||
|
||
// Read encrypted parquet | ||
let ctx: SessionContext = SessionContext::new(); | ||
let read_options = ParquetReadOptions::default().file_decryption_properties(decrypt); | ||
|
||
let encrypted_parquet_df = ctx.read_parquet(tempfile_str, read_options).await?; | ||
|
||
// Show information from the dataframe | ||
println!("\n\n==============================================================================="); | ||
println!("Encrypted Parquet DataFrame:"); | ||
query_dataframe(&encrypted_parquet_df).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
// Show information from the dataframe | ||
async fn query_dataframe(df: &DataFrame) -> Result<(), DataFusionError> { | ||
// show its schema using 'describe' | ||
println!("Schema:"); | ||
df.clone().describe().await?.show().await?; | ||
|
||
// Select three columns and filter the results | ||
// so that only rows where id > 1 are returned | ||
println!("\nSelected rows and columns:"); | ||
df.clone() | ||
.select_columns(&["id", "bool_col", "timestamp_col"])? | ||
.filter(col("id").gt(lit(5)))? | ||
.show() | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
// Setup encryption and decryption properties | ||
fn setup_encryption( | ||
parquet_df: &DataFrame, | ||
) -> Result<(FileEncryptionProperties, FileDecryptionProperties), DataFusionError> { | ||
let schema = parquet_df.schema(); | ||
let footer_key = b"0123456789012345".to_vec(); // 128bit/16 | ||
let column_key = b"1234567890123450".to_vec(); // 128bit/16 | ||
|
||
let mut encrypt = FileEncryptionProperties::builder(footer_key.clone()); | ||
let mut decrypt = FileDecryptionProperties::builder(footer_key.clone()); | ||
|
||
for field in schema.fields().iter() { | ||
encrypt = encrypt.with_column_key(field.name().as_str(), column_key.clone()); | ||
decrypt = decrypt.with_column_key(field.name().as_str(), column_key.clone()); | ||
} | ||
|
||
let encrypt = encrypt.build()?; | ||
let decrypt = decrypt.build()?; | ||
Ok((encrypt, decrypt)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
requested by clippy