-
Notifications
You must be signed in to change notification settings - Fork 59
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
Implement row group skipping for the default engine parquet readers #362
Changes from 3 commits
715f233
ef71f1a
39b8927
b5c3a52
e71571e
cbca3b3
e7d87eb
beeb6e8
519acbd
18b33cf
6c98441
0fdaf0a
8ac33f8
1cf03dc
bc8b344
0971002
375a380
6236874
9efcbf7
46d19e3
7666512
f3865d0
a4dc3da
40131db
bf65904
cce762d
c7d6bb0
4f92ed7
08a305b
e8a947e
bf1e3a8
9d632e7
4a77f3a
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
//! | ||
|
||
use std::cmp::Ordering; | ||
use std::ops::Not; | ||
use std::sync::Arc; | ||
|
||
use itertools::Itertools; | ||
|
@@ -78,15 +77,7 @@ impl LogSegment { | |
} | ||
|
||
fn read_metadata(&self, engine: &dyn Engine) -> DeltaResult<Option<(Metadata, Protocol)>> { | ||
let schema = get_log_schema().project(&[PROTOCOL_NAME, METADATA_NAME])?; | ||
// filter out log files that do not contain metadata or protocol information | ||
use Expression as Expr; | ||
let meta_predicate = Some(Expr::or( | ||
Expr::not(Expr::is_null(Expr::column("metaData.id"))), | ||
Expr::not(Expr::is_null(Expr::column("protocol.minReaderVersion"))), | ||
)); | ||
// read the same protocol and metadata schema for both commits and checkpoints | ||
let data_batches = self.replay(engine, schema.clone(), schema, meta_predicate)?; | ||
let data_batches = self.replay_for_metadata(engine)?; | ||
let mut metadata_opt: Option<Metadata> = None; | ||
let mut protocol_opt: Option<Protocol> = None; | ||
for batch in data_batches { | ||
|
@@ -109,6 +100,22 @@ impl LogSegment { | |
_ => Err(Error::MissingMetadataAndProtocol), | ||
} | ||
} | ||
|
||
// Factored out to facilitate testing | ||
fn replay_for_metadata( | ||
&self, | ||
engine: &dyn Engine, | ||
) -> DeltaResult<impl Iterator<Item = DeltaResult<(Box<dyn EngineData>, bool)>> + Send> { | ||
let schema = get_log_schema().project(&[PROTOCOL_NAME, METADATA_NAME])?; | ||
// filter out log files that do not contain metadata or protocol information | ||
use Expression as Expr; | ||
let meta_predicate = Expr::or( | ||
Expr::column("metaData.id").is_not_null(), | ||
Expr::column("protocol.minReaderVersion").is_not_null(), | ||
); | ||
// read the same protocol and metadata schema for both commits and checkpoints | ||
self.replay(engine, schema.clone(), schema, Some(meta_predicate)) | ||
} | ||
} | ||
|
||
// TODO expose methods for accessing the files of a table (with file pruning). | ||
|
@@ -175,6 +182,10 @@ impl Snapshot { | |
if let Some(version) = version { | ||
commit_files.retain(|log_path| log_path.version <= version); | ||
} | ||
// only keep commit files above the checkpoint we found | ||
if let Some(checkpoint_file) = checkpoint_files.first() { | ||
scovich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
commit_files.retain(|log_path| checkpoint_file.version < log_path.version); | ||
} | ||
|
||
// get the effective version from chosen files | ||
let version_eff = commit_files | ||
|
@@ -452,6 +463,7 @@ mod tests { | |
use crate::engine::default::filesystem::ObjectStoreFileSystemClient; | ||
use crate::engine::sync::SyncEngine; | ||
use crate::schema::StructType; | ||
use crate::Table; | ||
|
||
#[test] | ||
fn test_snapshot_read_metadata() { | ||
|
@@ -623,6 +635,37 @@ mod tests { | |
assert!(invalid.is_none()) | ||
} | ||
|
||
// NOTE: In addition to testing the meta-predicate for metadata replay, this test also verifies | ||
// that the parquet reader properly infers nullcount = rowcount for missing columns. The two | ||
// checkpoint part files that contain transaction app ids have truncated schemas that would | ||
// otherwise fail skipping due to their missing nullcount stat: | ||
// | ||
// Row group 0: count: 1 total(compressed): 111 B total(uncompressed):107 B | ||
// -------------------------------------------------------------------------------- | ||
// type nulls min / max | ||
// txn.appId BINARY 0 "3ae45b72-24e1-865a-a211-3..." / "3ae45b72-24e1-865a-a211-3..." | ||
// txn.version INT64 0 "4390" / "4390" | ||
#[test] | ||
fn test_replay_for_metadata() { | ||
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. An accidentally clever test :P 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. nice! |
||
let path = std::fs::canonicalize(PathBuf::from("./tests/data/parquet_row_group_skipping/")); | ||
let url = url::Url::from_directory_path(path.unwrap()).unwrap(); | ||
let engine = SyncEngine::new(); | ||
|
||
let table = Table::new(url); | ||
let snapshot = table.snapshot(&engine, None).unwrap(); | ||
let data: Vec<_> = snapshot | ||
.log_segment | ||
.replay_for_metadata(&engine) | ||
.unwrap() | ||
.try_collect() | ||
.unwrap(); | ||
// The checkpoint has five parts, each containing one action. The P&M come from first and | ||
// third parts, respectively. The parquet reader skips the second part; it would also skip | ||
// the last two parts, but the actual `read_metadata` will anyway skip them because it | ||
// terminates the iteration immediately after finding both P&M. | ||
assert_eq!(data.len(), 2); | ||
} | ||
|
||
#[test_log::test] | ||
fn test_read_table_with_checkpoint() { | ||
let path = std::fs::canonicalize(PathBuf::from( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::actions::visitors::TransactionVisitor; | ||
use crate::actions::{get_log_schema, TRANSACTION_NAME}; | ||
use crate::actions::{get_log_schema, Transaction, TRANSACTION_NAME}; | ||
use crate::snapshot::Snapshot; | ||
use crate::Engine; | ||
use crate::{actions::Transaction, DeltaResult}; | ||
use crate::{DeltaResult, Engine, EngineData, Expression as Expr, SchemaRef}; | ||
|
||
pub use crate::actions::visitors::TransactionMap; | ||
pub struct TransactionScanner { | ||
|
@@ -22,17 +21,11 @@ impl TransactionScanner { | |
engine: &dyn Engine, | ||
application_id: Option<&str>, | ||
) -> DeltaResult<TransactionMap> { | ||
let schema = get_log_schema().project(&[TRANSACTION_NAME])?; | ||
|
||
let schema = Self::get_txn_schema()?; | ||
let mut visitor = TransactionVisitor::new(application_id.map(|s| s.to_owned())); | ||
|
||
// when all ids are requested then a full scan of the log to the latest checkpoint is required | ||
let iter = | ||
self.snapshot | ||
.log_segment | ||
.replay(engine, schema.clone(), schema.clone(), None)?; | ||
|
||
for maybe_data in iter { | ||
// If a specific id is requested then we can terminate log replay early as soon as it was | ||
// found. If all ids are requested then we are forced to replay the entire log. | ||
for maybe_data in self.replay_for_app_ids(engine, schema.clone())? { | ||
let (txns, _) = maybe_data?; | ||
txns.extract(schema.clone(), &mut visitor)?; | ||
// if a specific id is requested and a transaction was found, then return | ||
|
@@ -44,6 +37,27 @@ impl TransactionScanner { | |
Ok(visitor.transactions) | ||
} | ||
|
||
// Factored out to facilitate testing | ||
fn get_txn_schema() -> DeltaResult<SchemaRef> { | ||
get_log_schema().project(&[TRANSACTION_NAME]) | ||
} | ||
|
||
// Factored out to facilitate testing | ||
fn replay_for_app_ids( | ||
&self, | ||
engine: &dyn Engine, | ||
schema: SchemaRef, | ||
) -> DeltaResult<impl Iterator<Item = DeltaResult<(Box<dyn EngineData>, bool)>> + Send> { | ||
// This meta-predicate should be effective because all the app ids end up in a single | ||
// checkpoint part when patitioned by `add.path` like the Delta spec requires. There's no | ||
// point filtering by a particular app id, even if we have one, because people usually query | ||
// for app ids that exist. | ||
let meta_predicate = Expr::column("txn.appId").is_not_null(); | ||
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. The code didn't previously attempt row group skipping for app ids. Now it does. |
||
self.snapshot | ||
.log_segment | ||
.replay(engine, schema.clone(), schema, Some(meta_predicate)) | ||
} | ||
|
||
/// Scan the Delta Log for the latest transaction entry of an application | ||
pub fn application_transaction( | ||
&self, | ||
|
@@ -67,6 +81,7 @@ mod tests { | |
use super::*; | ||
use crate::engine::sync::SyncEngine; | ||
use crate::Table; | ||
use itertools::Itertools; | ||
|
||
fn get_latest_transactions(path: &str, app_id: &str) -> (TransactionMap, Option<Transaction>) { | ||
let path = std::fs::canonicalize(PathBuf::from(path)).unwrap(); | ||
|
@@ -117,4 +132,24 @@ mod tests { | |
.as_ref() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_replay_for_app_ids() { | ||
let path = std::fs::canonicalize(PathBuf::from("./tests/data/parquet_row_group_skipping/")); | ||
let url = url::Url::from_directory_path(path.unwrap()).unwrap(); | ||
let engine = SyncEngine::new(); | ||
|
||
let table = Table::new(url); | ||
let snapshot = table.snapshot(&engine, None).unwrap(); | ||
let txn = TransactionScanner::new(snapshot.into()); | ||
let txn_schema = TransactionScanner::get_txn_schema().unwrap(); | ||
|
||
// The checkpoint has five parts, each containing one action. There are two app ids. | ||
let data: Vec<_> = txn | ||
.replay_for_app_ids(&engine, txn_schema.clone()) | ||
.unwrap() | ||
.try_collect() | ||
.unwrap(); | ||
assert_eq!(data.len(), 2); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
{"commitInfo":{"timestamp":1728065844007,"operation":"WRITE","operationParameters":{"mode":"Append","partitionBy":"[]"},"readVersion":0,"isolationLevel":"Serializable","isBlindAppend":true,"operationMetrics":{"numFiles":"1","numOutputRows":"5","numOutputBytes":"4959"},"engineInfo":"Apache-Spark/3.5.3 Delta-Lake/3.2.1","txnId":"d46d4bca-ab50-4075-977f-80a5b3844afa"}} | ||
{"add":{"path":"part-00000-b92e017a-50ba-4676-8322-48fc371c2b59-c000.snappy.parquet","partitionValues":{},"size":4959,"modificationTime":1728065843972,"dataChange":true,"stats":"{\"numRecords\":5}"}} | ||
{"txn":{"appId":"3ae45b72-24e1-865a-a211-34987ae02f2a","version":4390}} | ||
{"txn":{"appId":"b42b951f-f5d1-4f6e-be2a-0d11d1543029","version":1235}} |
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.
This is a new find, exposed on accident by me hacking two more parts into the checkpoint so we could test transaction app id filtering (the "checkpoint" schema was truncated, which prevented the P&M query from skipping those parts)
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.
If the
statistics()
method onColumnChunkMetadata
returnsNone
, that just means that there are no stats for that column, but doesn't necessarily imply that all values arenull
does it?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.
Oh, good catch. I didn't put the check deep enough. There are three levels of
None
here:To make things even more "fun", we have the following warning in Statistics::null_count_opt 🤦:
So I have two problems to work around now.
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.
Both fixed.