-
Notifications
You must be signed in to change notification settings - Fork 84
refactor!: txn-specific write_metadata_schema #1021
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
Open
zachschuermann
wants to merge
2
commits into
delta-io:main
Choose a base branch
from
zachschuermann:per-table-write-meta-schema-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use std::collections::HashSet; | ||
use std::iter; | ||
use std::sync::{Arc, LazyLock}; | ||
use std::sync::Arc; | ||
use std::time::{SystemTime, UNIX_EPOCH}; | ||
|
||
use crate::actions::SetTransaction; | ||
|
@@ -18,26 +18,6 @@ | |
const KERNEL_VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
const UNKNOWN_OPERATION: &str = "UNKNOWN"; | ||
|
||
pub(crate) static WRITE_METADATA_SCHEMA: LazyLock<SchemaRef> = LazyLock::new(|| { | ||
Arc::new(StructType::new(vec![ | ||
StructField::not_null("path", DataType::STRING), | ||
StructField::not_null( | ||
"partitionValues", | ||
MapType::new(DataType::STRING, DataType::STRING, true), | ||
), | ||
StructField::not_null("size", DataType::LONG), | ||
StructField::not_null("modificationTime", DataType::LONG), | ||
StructField::not_null("dataChange", DataType::BOOLEAN), | ||
])) | ||
}); | ||
|
||
/// Get the expected schema for engine data passed to [`add_write_metadata`]. | ||
/// | ||
/// [`add_write_metadata`]: crate::transaction::Transaction::add_write_metadata | ||
pub fn get_write_metadata_schema() -> &'static SchemaRef { | ||
&WRITE_METADATA_SCHEMA | ||
} | ||
|
||
/// A transaction represents an in-progress write to a table. After creating a transaction, changes | ||
/// to the table may be staged via the transaction methods before calling `commit` to commit the | ||
/// changes to the table. | ||
|
@@ -56,6 +36,7 @@ | |
read_snapshot: Arc<Snapshot>, | ||
operation: Option<String>, | ||
commit_info: Option<Arc<dyn EngineData>>, | ||
write_metadata_schema: SchemaRef, | ||
write_metadata: Vec<Box<dyn EngineData>>, | ||
// NB: hashmap would require either duplicating the appid or splitting SetTransaction | ||
// key/payload. HashSet requires Borrow<&str> with matching Eq, Ord, and Hash. Plus, | ||
|
@@ -93,6 +74,17 @@ | |
.table_configuration() | ||
.ensure_write_supported()?; | ||
|
||
let write_metadata_schema = Arc::new(StructType::new(vec![ | ||
StructField::not_null("path", DataType::STRING), | ||
StructField::not_null( | ||
"partitionValues", | ||
MapType::new(DataType::STRING, DataType::STRING, true), | ||
), | ||
StructField::not_null("size", DataType::LONG), | ||
StructField::not_null("modificationTime", DataType::LONG), | ||
StructField::not_null("dataChange", DataType::BOOLEAN), | ||
])); | ||
|
||
// TODO: unify all these into a (safer) `fn current_time_ms()` | ||
let commit_timestamp = SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
|
@@ -104,6 +96,7 @@ | |
read_snapshot, | ||
operation: None, | ||
commit_info: None, | ||
write_metadata_schema, | ||
write_metadata: vec![], | ||
set_transactions: vec![], | ||
commit_timestamp, | ||
|
@@ -145,7 +138,8 @@ | |
self.commit_timestamp, | ||
engine_commit_info.as_ref(), | ||
); | ||
let add_actions = generate_adds(engine, self.write_metadata.iter().map(|a| a.as_ref())); | ||
let add_actions = | ||
self.generate_adds(engine, self.write_metadata.iter().map(|a| a.as_ref())); | ||
|
||
let actions = iter::once(commit_info_actions) | ||
.chain(add_actions) | ||
|
@@ -223,42 +217,48 @@ | |
pub fn get_write_context(&self) -> WriteContext { | ||
let target_dir = self.read_snapshot.table_root(); | ||
let snapshot_schema = self.read_snapshot.schema(); | ||
let write_metadata_schema = self.write_metadata_schema.clone(); | ||
let logical_to_physical = self.generate_logical_to_physical(); | ||
WriteContext::new(target_dir.clone(), snapshot_schema, logical_to_physical) | ||
WriteContext::new( | ||
target_dir.clone(), | ||
snapshot_schema, | ||
write_metadata_schema, | ||
logical_to_physical, | ||
) | ||
} | ||
|
||
/// Add write metadata about files to include in the transaction. This API can be called | ||
/// multiple times to add multiple batches. | ||
/// | ||
/// The expected schema for `write_metadata` is given by [`get_write_metadata_schema`]. | ||
pub fn add_write_metadata(&mut self, write_metadata: Box<dyn EngineData>) { | ||
self.write_metadata.push(write_metadata); | ||
} | ||
} | ||
|
||
// convert write_metadata into add actions using an expression to transform the data in a single | ||
// pass | ||
fn generate_adds<'a>( | ||
engine: &dyn Engine, | ||
write_metadata: impl Iterator<Item = &'a dyn EngineData> + Send + 'a, | ||
) -> impl Iterator<Item = DeltaResult<Box<dyn EngineData>>> + Send + 'a { | ||
let evaluation_handler = engine.evaluation_handler(); | ||
let write_metadata_schema = get_write_metadata_schema(); | ||
let log_schema = get_log_add_schema(); | ||
|
||
write_metadata.map(move |write_metadata_batch| { | ||
let adds_expr = Expression::struct_from([Expression::struct_from( | ||
write_metadata_schema | ||
.fields() | ||
.map(|f| Expression::column([f.name()])), | ||
)]); | ||
let adds_evaluator = evaluation_handler.new_expression_evaluator( | ||
write_metadata_schema.clone(), | ||
adds_expr, | ||
log_schema.clone().into(), | ||
); | ||
adds_evaluator.evaluate(write_metadata_batch) | ||
}) | ||
// convert write_metadata into add actions using an expression to transform the data in a single | ||
// pass | ||
fn generate_adds<'a>( | ||
&'a self, | ||
engine: &dyn Engine, | ||
write_metadata: impl Iterator<Item = &'a dyn EngineData> + Send + 'a, | ||
) -> impl Iterator<Item = DeltaResult<Box<dyn EngineData>>> + Send + 'a { | ||
let evaluation_handler = engine.evaluation_handler(); | ||
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. review with whitespace hidden |
||
let log_schema = get_log_add_schema(); | ||
|
||
write_metadata.map(move |write_metadata_batch| { | ||
let adds_expr = Expression::struct_from([Expression::struct_from( | ||
self.write_metadata_schema | ||
.fields() | ||
.map(|f| Expression::column([f.name()])), | ||
)]); | ||
let adds_evaluator = evaluation_handler.new_expression_evaluator( | ||
self.write_metadata_schema.clone(), | ||
adds_expr, | ||
log_schema.clone().into(), | ||
); | ||
adds_evaluator.evaluate(write_metadata_batch) | ||
}) | ||
} | ||
} | ||
|
||
/// WriteContext is data derived from a [`Transaction`] that can be provided to writers in order to | ||
|
@@ -268,14 +268,21 @@ | |
pub struct WriteContext { | ||
target_dir: Url, | ||
schema: SchemaRef, | ||
write_metadata_schema: SchemaRef, | ||
logical_to_physical: Expression, | ||
} | ||
|
||
impl WriteContext { | ||
fn new(target_dir: Url, schema: SchemaRef, logical_to_physical: Expression) -> Self { | ||
fn new( | ||
target_dir: Url, | ||
schema: SchemaRef, | ||
write_metadata_schema: SchemaRef, | ||
logical_to_physical: Expression, | ||
) -> Self { | ||
WriteContext { | ||
target_dir, | ||
schema, | ||
write_metadata_schema, | ||
logical_to_physical, | ||
} | ||
} | ||
|
@@ -284,6 +291,13 @@ | |
&self.target_dir | ||
} | ||
|
||
/// Get the expected schema for engine data passed to [`add_write_metadata`]. | ||
/// | ||
/// [`add_write_metadata`]: crate::transaction::Transaction::add_write_metadata | ||
pub fn write_metadata_schema(&self) -> &SchemaRef { | ||
&self.write_metadata_schema | ||
} | ||
|
||
pub fn schema(&self) -> &SchemaRef { | ||
&self.schema | ||
} | ||
|
@@ -383,8 +397,11 @@ | |
mod tests { | ||
use super::*; | ||
|
||
use std::path::PathBuf; | ||
|
||
use crate::engine::arrow_data::ArrowEngineData; | ||
use crate::engine::arrow_expression::ArrowEvaluationHandler; | ||
use crate::engine::sync::SyncEngine; | ||
use crate::schema::MapType; | ||
use crate::{EvaluationHandler, JsonHandler, ParquetHandler, StorageHandler}; | ||
|
||
|
@@ -715,8 +732,14 @@ | |
} | ||
|
||
#[test] | ||
fn test_write_metadata_schema() { | ||
let schema = get_write_metadata_schema(); | ||
fn test_write_metadata_schema() -> DeltaResult<()> { | ||
let path = std::fs::canonicalize(PathBuf::from("./tests/data/basic_partitioned/"))?; | ||
let url = Url::from_directory_path(path).unwrap(); | ||
let engine = SyncEngine::new(); | ||
let snapshot = Snapshot::try_new(url, &engine, None)?; | ||
let txn = Transaction::try_new(snapshot)?; | ||
let ctx = txn.get_write_context(); | ||
let schema = ctx.write_metadata_schema(); | ||
let expected = StructType::new(vec![ | ||
StructField::not_null("path", DataType::STRING), | ||
StructField::not_null( | ||
|
@@ -728,5 +751,6 @@ | |
StructField::not_null("dataChange", DataType::BOOLEAN), | ||
]); | ||
assert_eq!(*schema, expected.into()); | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Now that we take
&'a self
, I think we can remove the named lifetimes?At worst we might need
+ '_
for the iterators?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.
Hm, the compiler is yelling that anonymous lifetimes are unstable in
impl trait
..