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 4 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
37 changes: 21 additions & 16 deletions kernel/src/scan/data_skipping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,29 @@ 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,
)
.ok()?;
scovich marked this conversation as resolved.
Show resolved Hide resolved

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(),
)
.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)
.ok()?;

Some(Self {
stats_schema,
Expand Down
29 changes: 19 additions & 10 deletions kernel/src/scan/log_replay.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::clone::Clone;
use std::collections::HashSet;
use std::iter;
use std::sync::{Arc, LazyLock};

use tracing::debug;
Expand Down Expand Up @@ -241,22 +242,30 @@ 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,
table_schema: &SchemaRef,
predicate: Option<ExpressionRef>,
) -> impl Iterator<Item = DeltaResult<ScanData>> {
) -> Box<dyn Iterator<Item = DeltaResult<ScanData>>> {
scovich marked this conversation as resolved.
Show resolved Hide resolved
let mut log_scanner = LogReplayScanner::new(engine, table_schema, predicate);
let add_transform = engine.get_expression_handler().get_evaluator(
match engine.get_expression_handler().get_evaluator(
get_log_add_schema().clone(),
get_add_transform_expr(),
SCAN_ROW_DATATYPE.clone(),
);
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)))
) {
Ok(add_transform) => Box::new(
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))),
),
Err(e) => Box::new(iter::once(Err(e))),
}
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/scan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ fn transform_to_logical_internal(
read_schema,
read_expression,
global_state.logical_schema.clone().into(),
)
)?
.evaluate(data.as_ref())?;
Ok(result)
}
Expand Down
12 changes: 7 additions & 5 deletions kernel/src/table_changes/log_replay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,13 @@ impl LogReplayScanner {
.version
.try_into()
.map_err(|_| Error::generic("Failed to convert commit version to i64"))?;
let evaluator = engine.get_expression_handler().get_evaluator(
get_log_add_schema().clone(),
cdf_scan_row_expression(timestamp, commit_version),
cdf_scan_row_schema().into(),
);
let evaluator = engine
.get_expression_handler()
.get_evaluator(
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
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