Skip to content

Commit bde3c91

Browse files
askoaaskoa
andauthored
Replace &Option<T> with Option<&T> (#4446)
* Replace &Option<T> with Option<&T> * empty commit to start build * Change &Option<Vec> to Option<&Vec> Co-authored-by: askoa <askoa@local>
1 parent 38e9f30 commit bde3c91

File tree

22 files changed

+62
-61
lines changed

22 files changed

+62
-61
lines changed

datafusion-examples/examples/custom_datasource.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Debug for CustomDataSource {
118118
impl CustomDataSource {
119119
pub(crate) async fn create_physical_plan(
120120
&self,
121-
projections: &Option<Vec<usize>>,
121+
projections: Option<&Vec<usize>>,
122122
schema: SchemaRef,
123123
) -> Result<Arc<dyn ExecutionPlan>> {
124124
Ok(Arc::new(CustomExec::new(projections, schema, self.clone())))
@@ -177,7 +177,7 @@ impl TableProvider for CustomDataSource {
177177
async fn scan(
178178
&self,
179179
_state: &SessionState,
180-
projection: &Option<Vec<usize>>,
180+
projection: Option<&Vec<usize>>,
181181
// filters and limit can be used here to inject some push-down operations if needed
182182
_filters: &[Expr],
183183
_limit: Option<usize>,
@@ -194,11 +194,11 @@ struct CustomExec {
194194

195195
impl CustomExec {
196196
fn new(
197-
projections: &Option<Vec<usize>>,
197+
projections: Option<&Vec<usize>>,
198198
schema: SchemaRef,
199199
db: CustomDataSource,
200200
) -> Self {
201-
let projected_schema = project_schema(&schema, projections.as_ref()).unwrap();
201+
let projected_schema = project_schema(&schema, projections).unwrap();
202202
Self {
203203
db,
204204
projected_schema,

datafusion/common/src/scalar.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,7 +2160,7 @@ impl ScalarValue {
21602160
fn eq_array_decimal(
21612161
array: &ArrayRef,
21622162
index: usize,
2163-
value: &Option<i128>,
2163+
value: Option<&i128>,
21642164
precision: u8,
21652165
scale: i8,
21662166
) -> Result<bool> {
@@ -2196,8 +2196,14 @@ impl ScalarValue {
21962196
pub fn eq_array(&self, array: &ArrayRef, index: usize) -> bool {
21972197
match self {
21982198
ScalarValue::Decimal128(v, precision, scale) => {
2199-
ScalarValue::eq_array_decimal(array, index, v, *precision, *scale)
2200-
.unwrap()
2199+
ScalarValue::eq_array_decimal(
2200+
array,
2201+
index,
2202+
v.as_ref(),
2203+
*precision,
2204+
*scale,
2205+
)
2206+
.unwrap()
22012207
}
22022208
ScalarValue::Boolean(val) => {
22032209
eq_array_primitive!(array, index, BooleanArray, val)

datafusion/core/src/dataframe.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,12 +795,11 @@ impl TableProvider for DataFrame {
795795
async fn scan(
796796
&self,
797797
_ctx: &SessionState,
798-
projection: &Option<Vec<usize>>,
798+
projection: Option<&Vec<usize>>,
799799
filters: &[Expr],
800800
limit: Option<usize>,
801801
) -> Result<Arc<dyn ExecutionPlan>> {
802802
let mut expr = projection
803-
.as_ref()
804803
// construct projections
805804
.map_or_else(
806805
|| {

datafusion/core/src/datasource/datasource.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub trait TableProvider: Sync + Send {
6161
async fn scan(
6262
&self,
6363
ctx: &SessionState,
64-
projection: &Option<Vec<usize>>,
64+
projection: Option<&Vec<usize>>,
6565
filters: &[Expr],
6666
// limit can be used to reduce the amount scanned
6767
// from the datasource as a performance optimization.

datafusion/core/src/datasource/empty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ impl TableProvider for EmptyTable {
6969
async fn scan(
7070
&self,
7171
_ctx: &SessionState,
72-
projection: &Option<Vec<usize>>,
72+
projection: Option<&Vec<usize>>,
7373
_filters: &[Expr],
7474
_limit: Option<usize>,
7575
) -> Result<Arc<dyn ExecutionPlan>> {
7676
// even though there is no data, projections apply
77-
let projected_schema = project_schema(&self.schema, projection.as_ref())?;
77+
let projected_schema = project_schema(&self.schema, projection)?;
7878
Ok(Arc::new(
7979
EmptyExec::new(false, projected_schema).with_partitions(self.partitions),
8080
))

datafusion/core/src/datasource/listing/table.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ impl TableProvider for ListingTable {
523523
async fn scan(
524524
&self,
525525
ctx: &SessionState,
526-
projection: &Option<Vec<usize>>,
526+
projection: Option<&Vec<usize>>,
527527
filters: &[Expr],
528528
limit: Option<usize>,
529529
) -> Result<Arc<dyn ExecutionPlan>> {
@@ -533,7 +533,7 @@ impl TableProvider for ListingTable {
533533
// if no files need to be read, return an `EmptyExec`
534534
if partitioned_file_lists.is_empty() {
535535
let schema = self.schema();
536-
let projected_schema = project_schema(&schema, projection.as_ref())?;
536+
let projected_schema = project_schema(&schema, projection)?;
537537
return Ok(Arc::new(EmptyExec::new(false, projected_schema)));
538538
}
539539

@@ -562,7 +562,7 @@ impl TableProvider for ListingTable {
562562
file_schema: Arc::clone(&self.file_schema),
563563
file_groups: partitioned_file_lists,
564564
statistics,
565-
projection: projection.clone(),
565+
projection: projection.cloned(),
566566
limit,
567567
output_ordering: self.try_create_output_ordering()?,
568568
table_partition_cols,
@@ -686,7 +686,7 @@ mod tests {
686686
let table = load_table(&ctx, "alltypes_plain.parquet").await?;
687687
let projection = None;
688688
let exec = table
689-
.scan(&ctx.state(), &projection, &[], None)
689+
.scan(&ctx.state(), projection, &[], None)
690690
.await
691691
.expect("Scan table");
692692

@@ -716,7 +716,7 @@ mod tests {
716716
.with_schema(schema);
717717
let table = ListingTable::try_new(config)?;
718718

719-
let exec = table.scan(&state, &None, &[], None).await?;
719+
let exec = table.scan(&state, None, &[], None).await?;
720720
assert_eq!(exec.statistics().num_rows, Some(8));
721721
assert_eq!(exec.statistics().total_byte_size, Some(671));
722722

@@ -855,7 +855,7 @@ mod tests {
855855
let filter = Expr::not_eq(col("p1"), lit("v1"));
856856

857857
let scan = table
858-
.scan(&ctx.state(), &None, &[filter], None)
858+
.scan(&ctx.state(), None, &[filter], None)
859859
.await
860860
.expect("Empty execution plan");
861861

datafusion/core/src/datasource/memory.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl MemTable {
6969
ctx: &SessionState,
7070
) -> Result<Self> {
7171
let schema = t.schema();
72-
let exec = t.scan(ctx, &None, &[], None).await?;
72+
let exec = t.scan(ctx, None, &[], None).await?;
7373
let partition_count = exec.output_partitioning().partition_count();
7474

7575
let tasks = (0..partition_count)
@@ -136,14 +136,14 @@ impl TableProvider for MemTable {
136136
async fn scan(
137137
&self,
138138
_ctx: &SessionState,
139-
projection: &Option<Vec<usize>>,
139+
projection: Option<&Vec<usize>>,
140140
_filters: &[Expr],
141141
_limit: Option<usize>,
142142
) -> Result<Arc<dyn ExecutionPlan>> {
143143
Ok(Arc::new(MemoryExec::try_new(
144144
&self.batches.clone(),
145145
self.schema(),
146-
projection.clone(),
146+
projection.cloned(),
147147
)?))
148148
}
149149
}
@@ -184,7 +184,7 @@ mod tests {
184184

185185
// scan with projection
186186
let exec = provider
187-
.scan(&session_ctx.state(), &Some(vec![2, 1]), &[], None)
187+
.scan(&session_ctx.state(), Some(&vec![2, 1]), &[], None)
188188
.await?;
189189

190190
let mut it = exec.execute(0, task_ctx)?;
@@ -218,9 +218,7 @@ mod tests {
218218

219219
let provider = MemTable::try_new(schema, vec![vec![batch]])?;
220220

221-
let exec = provider
222-
.scan(&session_ctx.state(), &None, &[], None)
223-
.await?;
221+
let exec = provider.scan(&session_ctx.state(), None, &[], None).await?;
224222
let mut it = exec.execute(0, task_ctx)?;
225223
let batch1 = it.next().await.unwrap()?;
226224
assert_eq!(3, batch1.schema().fields().len());
@@ -253,7 +251,7 @@ mod tests {
253251
let projection: Vec<usize> = vec![0, 4];
254252

255253
match provider
256-
.scan(&session_ctx.state(), &Some(projection), &[], None)
254+
.scan(&session_ctx.state(), Some(&projection), &[], None)
257255
.await
258256
{
259257
Err(DataFusionError::ArrowError(ArrowError::SchemaError(e))) => {
@@ -381,9 +379,7 @@ mod tests {
381379
let provider =
382380
MemTable::try_new(Arc::new(merged_schema), vec![vec![batch1, batch2]])?;
383381

384-
let exec = provider
385-
.scan(&session_ctx.state(), &None, &[], None)
386-
.await?;
382+
let exec = provider.scan(&session_ctx.state(), None, &[], None).await?;
387383
let mut it = exec.execute(0, task_ctx)?;
388384
let batch1 = it.next().await.unwrap()?;
389385
assert_eq!(3, batch1.schema().fields().len());

datafusion/core/src/datasource/view.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ impl ViewTable {
6161
}
6262

6363
/// Get definition ref
64-
pub fn definition(&self) -> &Option<String> {
65-
&self.definition
64+
pub fn definition(&self) -> Option<&String> {
65+
self.definition.as_ref()
6666
}
6767

6868
/// Get logical_plan ref
@@ -104,7 +104,7 @@ impl TableProvider for ViewTable {
104104
async fn scan(
105105
&self,
106106
state: &SessionState,
107-
projection: &Option<Vec<usize>>,
107+
projection: Option<&Vec<usize>>,
108108
filters: &[Expr],
109109
limit: Option<usize>,
110110
) -> Result<Arc<dyn ExecutionPlan>> {

datafusion/core/src/physical_optimizer/join_selection.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ fn swap_reverting_projection(
181181
}
182182

183183
/// Swaps join sides for filter column indices and produces new JoinFilter
184-
fn swap_join_filter(filter: &Option<JoinFilter>) -> Option<JoinFilter> {
185-
filter.as_ref().map(|filter| {
184+
fn swap_join_filter(filter: Option<&JoinFilter>) -> Option<JoinFilter> {
185+
filter.map(|filter| {
186186
let column_indices = filter
187187
.column_indices()
188188
.iter()
@@ -334,7 +334,7 @@ fn try_collect_left(
334334
Arc::clone(left),
335335
Arc::clone(right),
336336
hash_join.on().to_vec(),
337-
hash_join.filter().clone(),
337+
hash_join.filter().cloned(),
338338
hash_join.join_type(),
339339
PartitionMode::CollectLeft,
340340
hash_join.null_equals_null(),
@@ -345,7 +345,7 @@ fn try_collect_left(
345345
Arc::clone(left),
346346
Arc::clone(right),
347347
hash_join.on().to_vec(),
348-
hash_join.filter().clone(),
348+
hash_join.filter().cloned(),
349349
hash_join.join_type(),
350350
PartitionMode::CollectLeft,
351351
hash_join.null_equals_null(),
@@ -377,7 +377,7 @@ fn partitioned_hash_join(hash_join: &HashJoinExec) -> Result<Arc<dyn ExecutionPl
377377
Arc::clone(left),
378378
Arc::clone(right),
379379
hash_join.on().to_vec(),
380-
hash_join.filter().clone(),
380+
hash_join.filter().cloned(),
381381
hash_join.join_type(),
382382
PartitionMode::Partitioned,
383383
hash_join.null_equals_null(),

datafusion/core/src/physical_plan/joins/hash_join.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,8 @@ impl HashJoinExec {
248248
}
249249

250250
/// Filters applied before join output
251-
pub fn filter(&self) -> &Option<JoinFilter> {
252-
&self.filter
251+
pub fn filter(&self) -> Option<&JoinFilter> {
252+
self.filter.as_ref()
253253
}
254254

255255
/// How the join is performed
@@ -698,7 +698,7 @@ fn build_join_indices(
698698
left_data: &JoinLeftData,
699699
on_left: &[Column],
700700
on_right: &[Column],
701-
filter: &Option<JoinFilter>,
701+
filter: Option<&JoinFilter>,
702702
random_state: &RandomState,
703703
null_equals_null: &bool,
704704
) -> Result<(UInt64Array, UInt32Array)> {
@@ -1363,7 +1363,7 @@ impl HashJoinStream {
13631363
left_data,
13641364
&self.on_left,
13651365
&self.on_right,
1366-
&self.filter,
1366+
self.filter.as_ref(),
13671367
&self.random_state,
13681368
&self.null_equals_null,
13691369
);

0 commit comments

Comments
 (0)