-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Store example data directly inside the datafusion-examples (#19141) #19319
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 14 commits
9e35cfe
6daedb6
6603e3e
591f61c
d77de01
b02e636
7d35fcc
e6b30ac
449f3aa
7dc8a50
fb1af66
396193b
40b1e60
445dbff
2e9e563
0c2dea9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <!--- | ||
| 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. | ||
| --> | ||
|
|
||
| ## Example datasets | ||
|
|
||
| | Filename | Path | Description | | ||
| | ----------- | --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | ||
| | `cars.csv` | [`data/csv/cars.csv`](./csv/cars.csv) | Time-series–like dataset containing car identifiers, speed values, and timestamps. Used in window function and time-based query examples (e.g. ordering, window frames). | | ||
| | `regex.csv` | [`data/csv/regex.csv`](./csv/regex.csv) | Dataset for regular expression examples. Contains input values, regex patterns, replacement strings, and optional flags. Covers ASCII, Unicode, and locale-specific text processing. | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| car,speed,time | ||
| red,20.0,1996-04-12T12:05:03.000000000 | ||
| red,20.3,1996-04-12T12:05:04.000000000 | ||
| red,21.4,1996-04-12T12:05:05.000000000 | ||
| red,21.5,1996-04-12T12:05:06.000000000 | ||
| red,19.0,1996-04-12T12:05:07.000000000 | ||
| red,18.0,1996-04-12T12:05:08.000000000 | ||
| red,17.0,1996-04-12T12:05:09.000000000 | ||
| red,7.0,1996-04-12T12:05:10.000000000 | ||
| red,7.1,1996-04-12T12:05:11.000000000 | ||
| red,7.2,1996-04-12T12:05:12.000000000 | ||
| red,3.0,1996-04-12T12:05:13.000000000 | ||
| red,1.0,1996-04-12T12:05:14.000000000 | ||
| red,0.0,1996-04-12T12:05:15.000000000 | ||
| green,10.0,1996-04-12T12:05:03.000000000 | ||
| green,10.3,1996-04-12T12:05:04.000000000 | ||
| green,10.4,1996-04-12T12:05:05.000000000 | ||
| green,10.5,1996-04-12T12:05:06.000000000 | ||
| green,11.0,1996-04-12T12:05:07.000000000 | ||
| green,12.0,1996-04-12T12:05:08.000000000 | ||
| green,14.0,1996-04-12T12:05:09.000000000 | ||
| green,15.0,1996-04-12T12:05:10.000000000 | ||
| green,15.1,1996-04-12T12:05:11.000000000 | ||
| green,15.2,1996-04-12T12:05:12.000000000 | ||
| green,8.0,1996-04-12T12:05:13.000000000 | ||
| green,2.0,1996-04-12T12:05:14.000000000 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| values,patterns,replacement,flags | ||
| abc,^(a),bb\1bb,i | ||
| ABC,^(A).*,B,i | ||
| aBc,(b|d),e,i | ||
| AbC,(B|D),e, | ||
| aBC,^(b|c),d, | ||
| 4000,\b4([1-9]\d\d|\d[1-9]\d|\d\d[1-9])\b,xyz, | ||
| 4010,\b4([1-9]\d\d|\d[1-9]\d|\d\d[1-9])\b,xyz, | ||
| Düsseldorf,[\p{Letter}-]+,München, | ||
| Москва,[\p{L}-]+,Moscow, | ||
| Köln,[a-zA-Z]ö[a-zA-Z]{2},Koln, | ||
| اليوم,^\p{Arabic}+$,Today, |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,28 +17,49 @@ | |
|
|
||
| //! See `main.rs` for how to run it. | ||
|
|
||
| use std::path::PathBuf; | ||
| use std::sync::Arc; | ||
|
|
||
| use datafusion::common::DataFusionError; | ||
| use datafusion::config::{ConfigFileEncryptionProperties, 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::CsvReadOptions; | ||
| use datafusion::prelude::{ParquetReadOptions, SessionContext}; | ||
| use std::sync::Arc; | ||
| use tempfile::TempDir; | ||
| use tokio::fs::create_dir_all; | ||
|
|
||
| /// Read and write encrypted Parquet files using DataFusion | ||
| pub async fn parquet_encrypted() -> 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"); | ||
| // Load CSV into an in-memory DataFrame, then materialize it to Parquet. | ||
| // This replaces a static parquet fixture and makes the example self-contained | ||
| // without requiring DataFusion test files. | ||
| let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) | ||
|
||
| .join("data") | ||
| .join("csv") | ||
| .join("cars.csv"); | ||
| let csv_df = ctx | ||
| .read_csv(path.to_str().unwrap(), CsvReadOptions::default()) | ||
| .await?; | ||
| let tmp_source = TempDir::new()?; | ||
| let out_dir = tmp_source.path().join("parquet_source"); | ||
| create_dir_all(&out_dir).await?; | ||
| csv_df | ||
| .write_parquet( | ||
| out_dir.to_str().unwrap(), | ||
| DataFrameWriteOptions::default(), | ||
| None, | ||
| ) | ||
| .await?; | ||
|
|
||
| // Read the sample parquet file | ||
| let parquet_df = ctx | ||
| .read_parquet(filename, ParquetReadOptions::default()) | ||
| .read_parquet(out_dir.to_str().unwrap(), ParquetReadOptions::default()) | ||
| .await?; | ||
|
|
||
| // Show information from the dataframe | ||
|
|
@@ -52,27 +73,27 @@ pub async fn parquet_encrypted() -> datafusion::common::Result<()> { | |
| 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(); | ||
| let tempfile = tmp_source.path().join("cars_encrypted"); | ||
|
|
||
| // Write encrypted parquet | ||
| let mut options = TableParquetOptions::default(); | ||
| options.crypto.file_encryption = Some(ConfigFileEncryptionProperties::from(&encrypt)); | ||
| parquet_df | ||
| .write_parquet( | ||
| tempfile_str.as_str(), | ||
| tempfile.to_str().unwrap(), | ||
| DataFrameWriteOptions::new().with_single_file_output(true), | ||
| Some(options), | ||
| ) | ||
| .await?; | ||
|
|
||
| // Read encrypted parquet | ||
| // Read encrypted parquet back as a DataFrame using matching decryption config | ||
| let ctx: SessionContext = SessionContext::new(); | ||
| let read_options = | ||
| ParquetReadOptions::default().file_decryption_properties((&decrypt).into()); | ||
|
|
||
| let encrypted_parquet_df = ctx.read_parquet(tempfile_str, read_options).await?; | ||
| let encrypted_parquet_df = ctx | ||
| .read_parquet(tempfile.to_str().unwrap(), read_options) | ||
| .await?; | ||
|
|
||
| // Show information from the dataframe | ||
| println!( | ||
|
|
@@ -91,11 +112,12 @@ async fn query_dataframe(df: &DataFrame) -> Result<(), DataFusionError> { | |
| df.clone().describe().await?.show().await?; | ||
|
|
||
| // Select three columns and filter the results | ||
| // so that only rows where id > 1 are returned | ||
| // so that only rows where speed > 5 are returned | ||
| // select car, speed, time from t where speed > 5 | ||
| println!("\nSelected rows and columns:"); | ||
| df.clone() | ||
| .select_columns(&["id", "bool_col", "timestamp_col"])? | ||
| .filter(col("id").gt(lit(5)))? | ||
| .select_columns(&["car", "speed", "time"])? | ||
| .filter(col("speed").gt(lit(5)))? | ||
| .show() | ||
| .await?; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.