Skip to content
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

feat: make ExpressionHandler::get_evaluator fallible #577

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions kernel/src/engine/arrow_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,12 @@ impl ExpressionHandler for ArrowExpressionHandler {
schema: SchemaRef,
expression: Expression,
output_type: DataType,
) -> Arc<dyn ExpressionEvaluator> {
Arc::new(DefaultExpressionEvaluator {
) -> DeltaResult<Arc<dyn ExpressionEvaluator>> {
Ok(Arc::new(DefaultExpressionEvaluator {
input_schema: schema,
expression: Box::new(expression),
output_type,
})
}))
}
}

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/engine/default/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl<E: TaskExecutor> DefaultEngine<E> {
input_schema.into(),
transform.clone(),
output_schema.clone().into(),
);
)?;
let physical_data = logical_to_physical_expr.evaluate(data)?;
self.parquet
.write_parquet_file(
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ pub trait ExpressionHandler: AsAny {
schema: SchemaRef,
expression: Expression,
output_type: DataType,
) -> Arc<dyn ExpressionEvaluator>;
) -> DeltaResult<Arc<dyn ExpressionEvaluator>>;
}

/// Provides file system related functionalities to Delta Kernel.
Expand Down
42 changes: 25 additions & 17 deletions kernel/src/scan/data_skipping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::cmp::Ordering;
use std::sync::{Arc, LazyLock};

use tracing::debug;
use tracing::{debug, warn};

use crate::actions::get_log_add_schema;
use crate::actions::visitors::SelectionVectorVisitor;
Expand Down Expand Up @@ -99,24 +99,32 @@ impl DataSkippingFilter {
//
// 3. The selection evaluator does DISTINCT(col(predicate), 'false') to produce true (= keep) when
// the predicate is true/null and false (= skip) when the predicate is false.
let select_stats_evaluator = engine.get_expression_handler().get_evaluator(
// safety: kernel is very broken if we don't have the schema for Add actions
get_log_add_schema().clone(),
STATS_EXPR.clone(),
DataType::STRING,
);
let select_stats_evaluator = engine
.get_expression_handler()
.get_evaluator(
// safety: kernel is very broken if we don't have the schema for Add actions
get_log_add_schema().clone(),
STATS_EXPR.clone(),
DataType::STRING,
)
.inspect_err(|e| warn!("Failed to create stats selector evaluator: {}", e))
.ok()?;

let skipping_evaluator = engine.get_expression_handler().get_evaluator(
stats_schema.clone(),
Expr::struct_from([as_data_skipping_predicate(&predicate, false)?]),
PREDICATE_SCHEMA.clone(),
);
let skipping_evaluator = engine
.get_expression_handler()
.get_evaluator(
stats_schema.clone(),
Expr::struct_from([as_data_skipping_predicate(&predicate, false)?]),
PREDICATE_SCHEMA.clone(),
)
.inspect_err(|e| warn!("Failed to create skipping evaluator: {}", e))
.ok()?;

let filter_evaluator = engine.get_expression_handler().get_evaluator(
stats_schema.clone(),
FILTER_EXPR.clone(),
DataType::BOOLEAN,
);
let filter_evaluator = engine
.get_expression_handler()
.get_evaluator(stats_schema.clone(), FILTER_EXPR.clone(), DataType::BOOLEAN)
.inspect_err(|e| warn!("Failed to create filter evaluator: {}", e))
.ok()?;

Some(Self {
stats_schema,
Expand Down
19 changes: 12 additions & 7 deletions kernel/src/scan/log_replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,24 @@ impl LogReplayScanner {
/// indicates whether the record batch is a log or checkpoint batch.
pub fn scan_action_iter(
engine: &dyn Engine,
action_iter: impl Iterator<Item = DeltaResult<(Box<dyn EngineData>, bool)>>,
action_iter: impl Iterator<Item = DeltaResult<(Box<dyn EngineData>, bool)>> + 'static,
physical_predicate: Option<(ExpressionRef, SchemaRef)>,
) -> impl Iterator<Item = DeltaResult<ScanData>> {
) -> DeltaResult<impl Iterator<Item = DeltaResult<ScanData>>> {
let mut log_scanner = LogReplayScanner::new(engine, physical_predicate);
let add_transform = engine.get_expression_handler().get_evaluator(
get_log_add_schema().clone(),
get_add_transform_expr(),
SCAN_ROW_DATATYPE.clone(),
);
action_iter
)?;

let actions = action_iter
.map(move |action_res| {
let (batch, is_log_batch) = action_res?;
log_scanner.process_scan_batch(add_transform.as_ref(), batch.as_ref(), is_log_batch)
})
.filter(|res| res.as_ref().map_or(true, |(_, sv)| sv.contains(&true)))
.filter(|res| res.as_ref().map_or(true, |(_, sv)| sv.contains(&true)));

Ok(actions)
}

#[cfg(test)]
Expand Down Expand Up @@ -291,7 +294,8 @@ mod tests {
&[true, false],
(),
validate_simple,
);
)
.unwrap();
}

#[test]
Expand All @@ -301,6 +305,7 @@ mod tests {
&[false, false, true, false],
(),
validate_simple,
);
)
.unwrap();
}
}
20 changes: 11 additions & 9 deletions kernel/src/scan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,13 @@ impl Scan {
PhysicalPredicate::Some(predicate, schema) => Some((predicate, schema)),
PhysicalPredicate::None => None,
};

let it = scan_action_iter(
engine,
self.replay_for_scan_data(engine)?,
physical_predicate,
);
)?;

Ok(Some(it).into_iter().flatten())
}

Expand Down Expand Up @@ -687,7 +689,7 @@ fn transform_to_logical_internal(
physical_schema,
read_expression,
global_state.logical_schema.clone().into(),
)
)?
.evaluate(data.as_ref())?;
Ok(result)
}
Expand All @@ -707,7 +709,7 @@ pub(crate) mod test_utils {
sync::{json::SyncJsonHandler, SyncEngine},
},
scan::log_replay::scan_action_iter,
EngineData, JsonHandler,
DeltaResult, EngineData, JsonHandler,
};

use super::state::ScanCallback;
Expand Down Expand Up @@ -759,26 +761,26 @@ pub(crate) mod test_utils {
expected_sel_vec: &[bool],
context: T,
validate_callback: ScanCallback<T>,
) {
) -> DeltaResult<()> {
let iter = scan_action_iter(
&SyncEngine::new(),
batch.into_iter().map(|batch| Ok((batch as _, true))),
None,
);
)?;
let mut batch_count = 0;
for res in iter {
let (batch, sel) = res.unwrap();
assert_eq!(sel, expected_sel_vec);
let (batch, sel) = res?;
assert_eq!(sel.as_slice(), expected_sel_vec);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why now as_slice()?

crate::scan::state::visit_scan_files(
batch.as_ref(),
&sel,
context.clone(),
validate_callback,
)
.unwrap();
)?;
batch_count += 1;
}
assert_eq!(batch_count, 1);
Ok(())
}
}

Expand Down
3 changes: 2 additions & 1 deletion kernel/src/scan/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ mod tests {
&[true, false],
context,
validate_visit,
);
)
.unwrap();
}
}
3 changes: 1 addition & 2 deletions kernel/src/table_changes/log_replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ impl LogReplayScanner {
get_log_add_schema().clone(),
cdf_scan_row_expression(timestamp, commit_version),
cdf_scan_row_schema().into(),
);

)?;
let result = action_iter.map(move |actions| -> DeltaResult<_> {
let actions = actions?;

Expand Down
3 changes: 2 additions & 1 deletion kernel/src/table_changes/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ fn read_scan_file(
physical_schema.clone(),
physical_to_logical_expr,
global_state.logical_schema.clone().into(),
);
)?;

// Determine if the scan file was derived from a deletion vector pair
let is_dv_resolved_pair = scan_file.remove_dv.is_some();

Expand Down
4 changes: 2 additions & 2 deletions kernel/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ fn generate_adds<'a>(
write_metadata_schema.clone(),
adds_expr,
log_schema.clone().into(),
);
)?;
adds_evaluator.evaluate(write_metadata_batch)
})
}
Expand Down Expand Up @@ -321,7 +321,7 @@ fn generate_commit_info(
engine_commit_info_schema.into(),
commit_info_expr,
commit_info_empty_struct_schema.into(),
);
)?;

commit_info_evaluator.evaluate(engine_commit_info)
}
Expand Down
Loading