forked from delta-io/delta-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_files.rs
More file actions
343 lines (302 loc) · 11.6 KB
/
find_files.rs
File metadata and controls
343 lines (302 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::Arc;
use arrow_array::{Array, RecordBatch, StringArray};
use arrow_schema::{ArrowError, DataType as ArrowDataType, Field, Schema as ArrowSchema};
use datafusion::catalog::Session;
use datafusion::common::tree_node::{TreeNode, TreeNodeRecursion, TreeNodeVisitor};
use datafusion::datasource::MemTable;
use datafusion::execution::context::{SessionContext, TaskContext};
use datafusion::logical_expr::{Expr, Volatility, col};
use datafusion::physical_plan::ExecutionPlan;
use datafusion::physical_plan::filter::FilterExec;
use datafusion::physical_plan::limit::LocalLimitExec;
use itertools::Itertools;
use percent_encoding::percent_decode_str;
use tracing::*;
use crate::delta_datafusion::{
DeltaScanBuilder, DeltaScanConfigBuilder, PATH_COLUMN, df_logical_schema, get_path_column,
};
use crate::errors::{DeltaResult, DeltaTableError};
use crate::kernel::{Add, EagerSnapshot};
use crate::logstore::LogStoreRef;
#[derive(Debug, Hash, Eq, PartialEq)]
/// Representing the result of the [find_files] function.
pub(crate) struct FindFiles {
/// A list of `Add` objects that match the given predicate
pub candidates: Vec<Add>,
/// Was a physical read to the datastore required to determine the candidates
pub partition_scan: bool,
}
/// Finds files in a snapshot that match the provided predicate.
#[instrument(
skip_all,
fields(
version = snapshot.version(),
has_predicate = predicate.is_some(),
partition_scan = field::Empty,
candidate_count = field::Empty
)
)]
pub(crate) async fn find_files(
snapshot: &EagerSnapshot,
log_store: LogStoreRef,
session: &dyn Session,
predicate: Option<Expr>,
) -> DeltaResult<FindFiles> {
let current_metadata = snapshot.metadata();
match &predicate {
Some(predicate) => {
// Validate the Predicate and determine if it only contains partition columns
let mut expr_properties = FindFilesExprProperties {
partition_only: true,
partition_columns: current_metadata.partition_columns().clone(),
result: Ok(()),
};
TreeNode::visit(predicate, &mut expr_properties)?;
expr_properties.result?;
if expr_properties.partition_only {
let candidates = scan_memory_table(snapshot, predicate).await?;
let result = FindFiles {
candidates,
partition_scan: true,
};
Span::current().record("partition_scan", result.partition_scan);
Span::current().record("candidate_count", result.candidates.len());
Ok(result)
} else {
let candidates =
find_files_scan(snapshot, log_store, session, predicate.to_owned()).await?;
let result = FindFiles {
candidates,
partition_scan: false,
};
Span::current().record("partition_scan", result.partition_scan);
Span::current().record("candidate_count", result.candidates.len());
Ok(result)
}
}
None => {
let result = FindFiles {
candidates: snapshot.log_data().iter().map(|f| f.add_action()).collect(),
partition_scan: true,
};
Span::current().record("partition_scan", result.partition_scan);
Span::current().record("candidate_count", result.candidates.len());
Ok(result)
}
}
}
struct FindFilesExprProperties {
pub partition_columns: Vec<String>,
pub partition_only: bool,
pub result: DeltaResult<()>,
}
/// Ensure only expressions that make sense are accepted, check for
/// non-deterministic functions, and determine if the expression only contains
/// partition columns
impl TreeNodeVisitor<'_> for FindFilesExprProperties {
type Node = Expr;
fn f_down(&mut self, expr: &Self::Node) -> datafusion::common::Result<TreeNodeRecursion> {
// TODO: We can likely relax the volatility to STABLE. Would require further
// research to confirm the same value is generated during the scan and
// rewrite phases.
match expr {
Expr::Column(c) => {
if !self.partition_columns.contains(&c.name) {
self.partition_only = false;
}
}
Expr::ScalarVariable(_, _)
| Expr::Literal(_, _)
| Expr::Alias(_)
| Expr::BinaryExpr(_)
| Expr::Like(_)
| Expr::SimilarTo(_)
| Expr::Not(_)
| Expr::IsNotNull(_)
| Expr::IsNull(_)
| Expr::IsTrue(_)
| Expr::IsFalse(_)
| Expr::IsUnknown(_)
| Expr::IsNotTrue(_)
| Expr::IsNotFalse(_)
| Expr::IsNotUnknown(_)
| Expr::Negative(_)
| Expr::InList { .. }
| Expr::Between(_)
| Expr::Case(_)
| Expr::Cast(_)
| Expr::TryCast(_) => (),
Expr::ScalarFunction(scalar_function) => {
match scalar_function.func.signature().volatility {
Volatility::Immutable => (),
_ => {
self.result = Err(DeltaTableError::Generic(format!(
"Find files predicate contains nondeterministic function {}",
scalar_function.func.name()
)));
return Ok(TreeNodeRecursion::Stop);
}
}
}
_ => {
self.result = Err(DeltaTableError::Generic(format!(
"Find files predicate contains unsupported expression {expr}"
)));
return Ok(TreeNodeRecursion::Stop);
}
}
Ok(TreeNodeRecursion::Continue)
}
}
fn join_batches_with_add_actions(
batches: Vec<RecordBatch>,
mut actions: HashMap<String, Add>,
path_column: &str,
dict_array: bool,
) -> DeltaResult<Vec<Add>> {
// Given RecordBatches that contains `__delta_rs_path` perform a hash join
// with actions to obtain original add actions
let mut files = Vec::with_capacity(batches.iter().map(|batch| batch.num_rows()).sum());
for batch in batches {
let err = || DeltaTableError::Generic("Unable to obtain Delta-rs path column".to_string());
let iter: Box<dyn Iterator<Item = Option<&str>>> = if dict_array {
let array = get_path_column(&batch, path_column)?;
Box::new(array.into_iter())
} else {
let array = batch
.column_by_name(path_column)
.ok_or_else(err)?
.as_any()
.downcast_ref::<StringArray>()
.ok_or_else(err)?;
Box::new(array.into_iter())
};
for path in iter {
let path = path.ok_or(DeltaTableError::Generic(format!(
"{path_column} cannot be null"
)))?;
let path = percent_decode_str(&path).decode_utf8_lossy();
match actions.remove(path.as_ref()) {
Some(action) => files.push(action),
None => {
return Err(DeltaTableError::Generic(
"Unable to map __delta_rs_path to action.".to_owned(),
));
}
}
}
}
Ok(files)
}
/// Determine which files contain a record that satisfies the predicate
#[instrument(
skip_all,
fields(
version = snapshot.version(),
total_files = field::Empty,
matching_files = field::Empty
)
)]
async fn find_files_scan(
snapshot: &EagerSnapshot,
log_store: LogStoreRef,
session: &dyn Session,
expression: Expr,
) -> DeltaResult<Vec<Add>> {
let candidate_map: HashMap<String, Add> = snapshot
.log_data()
.iter()
.map(|f| f.add_action())
.map(|add| {
let path = add.path.clone();
(path, add)
})
.collect();
Span::current().record("total_files", candidate_map.len());
let scan_config = DeltaScanConfigBuilder::default()
.with_file_column(true)
.build(snapshot)?;
let logical_schema = df_logical_schema(snapshot, &scan_config.file_column_name, None)?;
// Identify which columns we need to project
let mut used_columns = expression
.column_refs()
.into_iter()
.map(|column| logical_schema.index_of(&column.name))
.collect::<Result<Vec<usize>, ArrowError>>()?;
// Add path column
used_columns.push(logical_schema.index_of(scan_config.file_column_name.as_ref().unwrap())?);
let scan = DeltaScanBuilder::new(snapshot, log_store, session)
.with_filter(Some(expression.clone()))
.with_projection(Some(&used_columns))
.with_scan_config(scan_config)
.build()
.await?;
let scan = Arc::new(scan);
let config = &scan.config;
let input_dfschema = scan.logical_schema.as_ref().to_owned().try_into()?;
let predicate_expr = session
.create_physical_expr(Expr::IsTrue(Box::new(expression.clone())), &input_dfschema)?;
let filter: Arc<dyn ExecutionPlan> =
Arc::new(FilterExec::try_new(predicate_expr, scan.clone())?);
let limit: Arc<dyn ExecutionPlan> = Arc::new(LocalLimitExec::new(filter, 1));
let task_ctx = Arc::new(TaskContext::from(session));
let path_batches = datafusion::physical_plan::collect(limit, task_ctx).await?;
let result = join_batches_with_add_actions(
path_batches,
candidate_map,
config.file_column_name.as_ref().unwrap(),
true,
)?;
Span::current().record("matching_files", result.len());
Ok(result)
}
async fn scan_memory_table(snapshot: &EagerSnapshot, predicate: &Expr) -> DeltaResult<Vec<Add>> {
let actions = snapshot
.log_data()
.iter()
.map(|f| f.add_action())
.collect_vec();
let batch = snapshot.add_actions_table(true)?;
let schema = batch.schema();
let mut arrays = Vec::with_capacity(schema.fields().len());
let mut fields = Vec::with_capacity(schema.fields().len());
arrays.push(
batch
.column_by_name("path")
.ok_or(DeltaTableError::Generic(
"Column with name `path` does not exist".to_owned(),
))?
.to_owned(),
);
fields.push(Field::new(PATH_COLUMN, ArrowDataType::Utf8, false));
for field in schema.fields() {
if field.name().starts_with("partition.") {
let name = field.name().strip_prefix("partition.").unwrap();
arrays.push(batch.column_by_name(field.name()).unwrap().to_owned());
fields.push(Field::new(
name,
field.data_type().to_owned(),
field.is_nullable(),
));
}
}
let schema = Arc::new(ArrowSchema::new(fields));
let batch = RecordBatch::try_new(schema, arrays)?;
let mem_table = MemTable::try_new(batch.schema(), vec![vec![batch]])?;
let ctx = SessionContext::new();
let mut df = ctx.read_table(Arc::new(mem_table))?;
df = df
.filter(predicate.to_owned())?
.select(vec![col(PATH_COLUMN)])?;
let batches = df.collect().await?;
let map = actions
.into_iter()
.map(|action| {
let path = action.path.clone();
(path, action)
})
.collect::<HashMap<String, Add>>();
join_batches_with_add_actions(batches, map, PATH_COLUMN, false)
}