Skip to content

Commit d3b55e2

Browse files
committed
feat: added lint rule to , solved all errors that arose and updated tests to pass, solves apache#18504
1 parent a5eb912 commit d3b55e2

14 files changed

Lines changed: 138 additions & 134 deletions

File tree

‎datafusion/expr/Cargo.toml‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ rust-version = { workspace = true }
3131
[package.metadata.docs.rs]
3232
all-features = true
3333

34-
[lints]
35-
workspace = true
34+
[lints] # Overrides / extends workspace.lints
35+
[lints.clippy]
36+
# Avoid unnecessary clones for performance, okay to ignore for tests or intentional
37+
# moves.
38+
# Reference: <https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value>
39+
needless_pass_by_value = "deny"
3640

3741
[lib]
3842
name = "datafusion_expr"

‎datafusion/expr/src/arguments.rs‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ mod tests {
162162
fn test_all_positional() {
163163
let param_names = vec!["a".to_string(), "b".to_string()];
164164

165-
let args = vec![lit(1), lit("hello")];
165+
let args = vec![lit(&1), lit(&"hello")];
166166
let arg_names = vec![None, None];
167167

168168
let result =
@@ -174,7 +174,7 @@ mod tests {
174174
fn test_all_named() {
175175
let param_names = vec!["a".to_string(), "b".to_string()];
176176

177-
let args = vec![lit(1), lit("hello")];
177+
let args = vec![lit(&1), lit(&"hello")];
178178
let arg_names = vec![Some("a".to_string()), Some("b".to_string())];
179179

180180
let result = resolve_function_arguments(&param_names, args, arg_names).unwrap();
@@ -186,7 +186,7 @@ mod tests {
186186
let param_names = vec!["a".to_string(), "b".to_string(), "c".to_string()];
187187

188188
// Call with: func(c => 3.0, a => 1, b => "hello")
189-
let args = vec![lit(3.0), lit(1), lit("hello")];
189+
let args = vec![lit(&3.0), lit(&1), lit(&"hello")];
190190
let arg_names = vec![
191191
Some("c".to_string()),
192192
Some("a".to_string()),
@@ -197,34 +197,34 @@ mod tests {
197197

198198
// Should be reordered to [a, b, c] = [1, "hello", 3.0]
199199
assert_eq!(result.len(), 3);
200-
assert_eq!(result[0], lit(1));
201-
assert_eq!(result[1], lit("hello"));
202-
assert_eq!(result[2], lit(3.0));
200+
assert_eq!(result[0], lit(&1));
201+
assert_eq!(result[1], lit(&"hello"));
202+
assert_eq!(result[2], lit(&3.0));
203203
}
204204

205205
#[test]
206206
fn test_mixed_positional_and_named() {
207207
let param_names = vec!["a".to_string(), "b".to_string(), "c".to_string()];
208208

209209
// Call with: func(1, c => 3.0, b => "hello")
210-
let args = vec![lit(1), lit(3.0), lit("hello")];
210+
let args = vec![lit(&1), lit(&3.0), lit(&"hello")];
211211
let arg_names = vec![None, Some("c".to_string()), Some("b".to_string())];
212212

213213
let result = resolve_function_arguments(&param_names, args, arg_names).unwrap();
214214

215215
// Should be reordered to [a, b, c] = [1, "hello", 3.0]
216216
assert_eq!(result.len(), 3);
217-
assert_eq!(result[0], lit(1));
218-
assert_eq!(result[1], lit("hello"));
219-
assert_eq!(result[2], lit(3.0));
217+
assert_eq!(result[0], lit(&1));
218+
assert_eq!(result[1], lit(&"hello"));
219+
assert_eq!(result[2], lit(&3.0));
220220
}
221221

222222
#[test]
223223
fn test_positional_after_named_error() {
224224
let param_names = vec!["a".to_string(), "b".to_string()];
225225

226226
// Call with: func(a => 1, "hello") - ERROR
227-
let args = vec![lit(1), lit("hello")];
227+
let args = vec![lit(&1), lit(&"hello")];
228228
let arg_names = vec![Some("a".to_string()), None];
229229

230230
let result = resolve_function_arguments(&param_names, args, arg_names);
@@ -240,7 +240,7 @@ mod tests {
240240
let param_names = vec!["a".to_string(), "b".to_string()];
241241

242242
// Call with: func(x => 1, b => "hello") - ERROR
243-
let args = vec![lit(1), lit("hello")];
243+
let args = vec![lit(&1), lit(&"hello")];
244244
let arg_names = vec![Some("x".to_string()), Some("b".to_string())];
245245

246246
let result = resolve_function_arguments(&param_names, args, arg_names);
@@ -256,7 +256,7 @@ mod tests {
256256
let param_names = vec!["a".to_string(), "b".to_string()];
257257

258258
// Call with: func(a => 1, a => 2) - ERROR
259-
let args = vec![lit(1), lit(2)];
259+
let args = vec![lit(&1), lit(&2)];
260260
let arg_names = vec![Some("a".to_string()), Some("a".to_string())];
261261

262262
let result = resolve_function_arguments(&param_names, args, arg_names);
@@ -272,7 +272,7 @@ mod tests {
272272
let param_names = vec!["a".to_string(), "b".to_string(), "c".to_string()];
273273

274274
// Call with: func(a => 1, c => 3.0) - missing 'b'
275-
let args = vec![lit(1), lit(3.0)];
275+
let args = vec![lit(&1), lit(&3.0)];
276276
let arg_names = vec![Some("a".to_string()), Some("c".to_string())];
277277

278278
let result = resolve_function_arguments(&param_names, args, arg_names);

‎datafusion/expr/src/conditional_expressions.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,16 @@ mod tests {
112112

113113
#[test]
114114
fn case_when_same_literal_then_types() -> Result<()> {
115-
let _ = when(col("state").eq(lit("CO")), lit(303))
116-
.when(col("state").eq(lit("NY")), lit(212))
115+
let _ = when(col("state").eq(lit(&"CO")), lit(&303))
116+
.when(col("state").eq(lit(&"NY")), lit(&212))
117117
.end()?;
118118
Ok(())
119119
}
120120

121121
#[test]
122122
fn case_when_different_literal_then_types() {
123-
let maybe_expr = when(col("state").eq(lit("CO")), lit(303))
124-
.when(col("state").eq(lit("NY")), lit("212"))
123+
let maybe_expr = when(col("state").eq(lit(&"CO")), lit(&303))
124+
.when(col("state").eq(lit(&"NY")), lit(&"212"))
125125
.end();
126126
assert!(maybe_expr.is_err());
127127
}

‎datafusion/expr/src/execution_props.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ impl ExecutionProps {
104104
/// Returns the provider for the `var_type`, if any
105105
pub fn get_var_provider(
106106
&self,
107-
var_type: VarType,
107+
var_type: &VarType,
108108
) -> Option<Arc<dyn VarProvider + Send + Sync>> {
109109
self.var_providers
110110
.as_ref()
111-
.and_then(|var_providers| var_providers.get(&var_type).cloned())
111+
.and_then(|var_providers| var_providers.get(var_type).cloned())
112112
}
113113

114114
/// Returns the configuration properties for this execution

‎datafusion/expr/src/expr.rs‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3661,9 +3661,9 @@ mod test {
36613661
#[test]
36623662
fn format_case_when() -> Result<()> {
36633663
let expr = case(col("a"))
3664-
.when(lit(1), lit(true))
3665-
.when(lit(0), lit(false))
3666-
.otherwise(lit(ScalarValue::Null))?;
3664+
.when(lit(&1), lit(&true))
3665+
.when(lit(&0), lit(&false))
3666+
.otherwise(lit(&ScalarValue::Null))?;
36673667
let expected = "CASE a WHEN Int32(1) THEN Boolean(true) WHEN Int32(0) THEN Boolean(false) ELSE NULL END";
36683668
assert_eq!(expected, format!("{expr}"));
36693669
Ok(())
@@ -3687,9 +3687,9 @@ mod test {
36873687
fn test_partial_ord() {
36883688
// Test validates that partial ord is defined for Expr, not
36893689
// intended to exhaustively test all possibilities
3690-
let exp1 = col("a") + lit(1);
3691-
let exp2 = col("a") + lit(2);
3692-
let exp3 = !(col("a") + lit(2));
3690+
let exp1 = col("a") + lit(&1);
3691+
let exp2 = col("a") + lit(&2);
3692+
let exp3 = !(col("a") + lit(&2));
36933693

36943694
assert!(exp1 < exp2);
36953695
assert!(exp3 > exp2);
@@ -3708,7 +3708,7 @@ mod test {
37083708

37093709
// multiple columns
37103710
{
3711-
let expr = col("a") + col("b") + lit(1);
3711+
let expr = col("a") + col("b") + lit(&1);
37123712
let columns = expr.column_refs();
37133713
assert_eq!(2, columns.len());
37143714
assert!(columns.contains(&Column::from_name("a")));
@@ -3721,35 +3721,35 @@ mod test {
37213721
#[test]
37223722
fn test_logical_ops() {
37233723
assert_eq!(
3724-
format!("{}", lit(1u32).eq(lit(2u32))),
3724+
format!("{}", lit(&1u32).eq(lit(&2u32))),
37253725
"UInt32(1) = UInt32(2)"
37263726
);
37273727
assert_eq!(
3728-
format!("{}", lit(1u32).not_eq(lit(2u32))),
3728+
format!("{}", lit(&1u32).not_eq(lit(&2u32))),
37293729
"UInt32(1) != UInt32(2)"
37303730
);
37313731
assert_eq!(
3732-
format!("{}", lit(1u32).gt(lit(2u32))),
3732+
format!("{}", lit(&1u32).gt(lit(&2u32))),
37333733
"UInt32(1) > UInt32(2)"
37343734
);
37353735
assert_eq!(
3736-
format!("{}", lit(1u32).gt_eq(lit(2u32))),
3736+
format!("{}", lit(&1u32).gt_eq(lit(&2u32))),
37373737
"UInt32(1) >= UInt32(2)"
37383738
);
37393739
assert_eq!(
3740-
format!("{}", lit(1u32).lt(lit(2u32))),
3740+
format!("{}", lit(&1u32).lt(lit(&2u32))),
37413741
"UInt32(1) < UInt32(2)"
37423742
);
37433743
assert_eq!(
3744-
format!("{}", lit(1u32).lt_eq(lit(2u32))),
3744+
format!("{}", lit(&1u32).lt_eq(lit(&2u32))),
37453745
"UInt32(1) <= UInt32(2)"
37463746
);
37473747
assert_eq!(
3748-
format!("{}", lit(1u32).and(lit(2u32))),
3748+
format!("{}", lit(&1u32).and(lit(&2u32))),
37493749
"UInt32(1) AND UInt32(2)"
37503750
);
37513751
assert_eq!(
3752-
format!("{}", lit(1u32).or(lit(2u32))),
3752+
format!("{}", lit(&1u32).or(lit(&2u32))),
37533753
"UInt32(1) OR UInt32(2)"
37543754
);
37553755
}
@@ -3896,7 +3896,7 @@ mod test {
38963896
format!(
38973897
"{}",
38983898
SchemaDisplay(
3899-
&lit(1).alias_qualified("table_name".into(), "column_name")
3899+
&lit(&1).alias_qualified("table_name".into(), "column_name")
39003900
)
39013901
),
39023902
"table_name.column_name"
@@ -3908,7 +3908,7 @@ mod test {
39083908
assert_eq!(
39093909
format!(
39103910
"{}",
3911-
SchemaDisplay(&lit(1).alias_qualified(None::<&str>, "column_name"))
3911+
SchemaDisplay(&lit(&1).alias_qualified(None::<&str>, "column_name"))
39123912
),
39133913
"column_name"
39143914
);
@@ -3949,7 +3949,7 @@ mod test {
39493949
fn test_accept_exprs() {
39503950
fn accept_exprs<E: AsRef<Expr>>(_: &[E]) {}
39513951

3952-
let expr = || -> Expr { lit(1) };
3952+
let expr = || -> Expr { lit(&1) };
39533953

39543954
// Call accept_exprs with owned expressions
39553955
let owned_exprs = vec![expr(), expr()];

‎datafusion/expr/src/expr_rewriter/mod.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ mod test {
390390
} else {
391391
utf8_val
392392
};
393-
Ok(Transformed::yes(lit_with_metadata(utf8_val, metadata)))
393+
Ok(Transformed::yes(lit_with_metadata(&utf8_val, metadata)))
394394
}
395395
// otherwise, return None
396396
_ => Ok(Transformed::no(expr)),
@@ -399,19 +399,19 @@ mod test {
399399

400400
// rewrites "foo" --> "bar"
401401
let rewritten = col("state")
402-
.eq(lit("foo"))
402+
.eq(lit(&"foo"))
403403
.transform(transformer)
404404
.data()
405405
.unwrap();
406-
assert_eq!(rewritten, col("state").eq(lit("bar")));
406+
assert_eq!(rewritten, col("state").eq(lit(&"bar")));
407407

408408
// doesn't rewrite
409409
let rewritten = col("state")
410-
.eq(lit("baz"))
410+
.eq(lit(&"baz"))
411411
.transform(transformer)
412412
.data()
413413
.unwrap();
414-
assert_eq!(rewritten, col("state").eq(lit("baz")));
414+
assert_eq!(rewritten, col("state").eq(lit(&"baz")));
415415
}
416416

417417
#[test]
@@ -486,7 +486,7 @@ mod test {
486486
#[test]
487487
fn rewriter_visit() {
488488
let mut rewriter = RecordingRewriter::default();
489-
col("state").eq(lit("CO")).rewrite(&mut rewriter).unwrap();
489+
col("state").eq(lit(&"CO")).rewrite(&mut rewriter).unwrap();
490490

491491
assert_eq!(
492492
rewriter.v,
@@ -514,7 +514,7 @@ mod test {
514514
);
515515

516516
// change literal type from i32 to i64
517-
test_rewrite(col("a").add(lit(1i32)), col("a").add(lit(1i64)));
517+
test_rewrite(col("a").add(lit(&1i32)), col("a").add(lit(&1i64)));
518518

519519
// test preserve qualifier
520520
test_rewrite(

‎datafusion/expr/src/expr_rewriter/order_by.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn rewrite_sort_col_by_aggs(expr: Expr, plan: &LogicalPlan) -> Result<Expr> {
5252
// on top of them)
5353
if plan_inputs.len() == 1 {
5454
let proj_exprs = plan.expressions();
55-
rewrite_in_terms_of_projection(expr, proj_exprs, plan_inputs[0])
55+
rewrite_in_terms_of_projection(expr, &proj_exprs, plan_inputs[0])
5656
} else {
5757
Ok(expr)
5858
}
@@ -71,7 +71,7 @@ fn rewrite_sort_col_by_aggs(expr: Expr, plan: &LogicalPlan) -> Result<Expr> {
7171
/// 2. t produces an output schema with two columns "a", "b + c"
7272
fn rewrite_in_terms_of_projection(
7373
expr: Expr,
74-
proj_exprs: Vec<Expr>,
74+
proj_exprs: &[Expr],
7575
input: &LogicalPlan,
7676
) -> Result<Expr> {
7777
// assumption is that each item in exprs, such as "b + c" is
@@ -104,7 +104,7 @@ fn rewrite_in_terms_of_projection(
104104

105105
// look for the column named the same as this expr
106106
let mut found = None;
107-
for proj_expr in &proj_exprs {
107+
for proj_expr in proj_exprs {
108108
proj_expr.apply(|e| {
109109
if expr_match(&search_col, e) {
110110
found = Some(e.clone());
@@ -215,7 +215,7 @@ mod test {
215215
// projects out an expression "c1" that is different than the column "c1"
216216
.project(vec![
217217
// c1 + 1 as c1,
218-
col("c1").add(lit(1)).alias("c1"),
218+
col("c1").add(lit(&1)).alias("c1"),
219219
// min(c2)
220220
min(col("c2")),
221221
// avg("c3") as average

0 commit comments

Comments
 (0)