Skip to content

Commit ba5279f

Browse files
Gohlub2010YOUY01
authored andcommitted
feat: added clippy::needless_pass_by_value lint rule to datafusion/expr (apache#18532)
## Which issue does this PR close? - Closes apache#18504. ## Rationale for this change Followed suggestions to not update any public-facing APIs and put the lint rule in the appropriate spot. ## What changes are included in this PR? * Add `#![deny(clippy::needless_pass_by_value)]` and `#![cfg_attr(test, allow(clippy::needless_pass_by_value))]` to `lib.rs`. * Add `#[allow(clippy::needless_pass_by_value)]` to public functions * fix `rewrite_in_terms_of_projection()` and `get_exprs_except_skipped()` to use references per the lint suggestion ## Are these changes tested? Yes, though the same test failed even without changes to the public APIs: `test expr_rewriter::order_by::test::rewrite_sort_cols_by_agg_alias ... FAILED` I'll append the logs for your convenience: ``` failures: ---- expr_rewriter::order_by::test::rewrite_sort_cols_by_agg_alias stdout ---- running: 'c1 --> c1 -- column *named* c1 that came out of the projection, (not t.c1)' running: 'min(c2) --> "min(c2)" -- (column *named* "min(t.c2)"!)' thread 'expr_rewriter::order_by::test::rewrite_sort_cols_by_agg_alias' (27524241) panicked at datafusion/expr/src/expr_rewriter/order_by.rs:308:13: assertion `left == right` failed: input:Sort { expr: AggregateFunction(AggregateFunction { func: AggregateUDF { inner: Min { name: "min", signature: Signature { type_signature: VariadicAny, volatility: Immutable, parameter_names: None } } }, params: AggregateFunctionParams { args: [Column(Column { relation: None, name: "c2" })], distinct: false, filter: None, order_by: [], null_treatment: None } }), asc: true, nulls_first: true } rewritten:Sort { expr: Column(Column { relation: None, name: "min(t.c2)" }), asc: true, nulls_first: true } expected:Sort { expr: Column(Column { relation: Some(Bare { table: "min(t" }), name: "c2)" }), asc: true, nulls_first: true } left: Sort { expr: Column(Column { relation: None, name: "min(t.c2)" }), asc: true, nulls_first: true } right: Sort { expr: Column(Column { relation: Some(Bare { table: "min(t" }), name: "c2)" }), asc: true, nulls_first: true } note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace failures: expr_rewriter::order_by::test::rewrite_sort_cols_by_agg_alias ``` ## Are there any user-facing changes? No, all modification were constrained to internal APIs. --------- Co-authored-by: Yongting You <2010youy01@gmail.com>
1 parent 2b86ce5 commit ba5279f

File tree

6 files changed

+15
-6
lines changed

6 files changed

+15
-6
lines changed

datafusion/expr/src/execution_props.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl ExecutionProps {
102102
}
103103

104104
/// Returns the provider for the `var_type`, if any
105+
#[allow(clippy::needless_pass_by_value)]
105106
pub fn get_var_provider(
106107
&self,
107108
var_type: VarType,

datafusion/expr/src/expr_rewriter/order_by.rs

Lines changed: 3 additions & 3 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());

datafusion/expr/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
// Make sure fast / cheap clones on Arc are explicit:
2424
// https://github.com/apache/datafusion/issues/11143
2525
#![deny(clippy::clone_on_ref_ptr)]
26+
// https://github.com/apache/datafusion/issues/18503
27+
#![deny(clippy::needless_pass_by_value)]
28+
#![cfg_attr(test, allow(clippy::needless_pass_by_value))]
2629

2730
//! [DataFusion](https://github.com/apache/datafusion)
2831
//! is an extensible query execution framework that uses

datafusion/expr/src/literal.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ use crate::Expr;
2121
use datafusion_common::{metadata::FieldMetadata, ScalarValue};
2222

2323
/// Create a literal expression
24+
#[allow(clippy::needless_pass_by_value)]
2425
pub fn lit<T: Literal>(n: T) -> Expr {
2526
n.lit()
2627
}
2728

29+
#[allow(clippy::needless_pass_by_value)]
2830
pub fn lit_with_metadata<T: Literal>(n: T, metadata: Option<FieldMetadata>) -> Expr {
2931
let Some(metadata) = metadata else {
3032
return n.lit();
@@ -45,6 +47,7 @@ pub fn lit_with_metadata<T: Literal>(n: T, metadata: Option<FieldMetadata>) -> E
4547
}
4648

4749
/// Create a literal timestamp expression
50+
#[allow(clippy::needless_pass_by_value)]
4851
pub fn lit_timestamp_nano<T: TimestampLiteral>(n: T) -> Expr {
4952
n.lit_timestamp_nano()
5053
}

datafusion/expr/src/logical_plan/plan.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3481,6 +3481,7 @@ impl Aggregate {
34813481
///
34823482
/// This method should only be called when you are absolutely sure that the schema being
34833483
/// provided is correct for the aggregate. If in doubt, call [try_new](Self::try_new) instead.
3484+
#[allow(clippy::needless_pass_by_value)]
34843485
pub fn try_new_with_schema(
34853486
input: Arc<LogicalPlan>,
34863487
group_expr: Vec<Expr>,

datafusion/expr/src/utils.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ fn get_excluded_columns(
354354
/// Returns all `Expr`s in the schema, except the `Column`s in the `columns_to_skip`
355355
fn get_exprs_except_skipped(
356356
schema: &DFSchema,
357-
columns_to_skip: HashSet<Column>,
357+
columns_to_skip: &HashSet<Column>,
358358
) -> Vec<Expr> {
359359
if columns_to_skip.is_empty() {
360360
schema.iter().map(Expr::from).collect::<Vec<Expr>>()
@@ -419,7 +419,7 @@ pub fn expand_wildcard(
419419
};
420420
// Add each excluded `Column` to columns_to_skip
421421
columns_to_skip.extend(excluded_columns);
422-
Ok(get_exprs_except_skipped(schema, columns_to_skip))
422+
Ok(get_exprs_except_skipped(schema, &columns_to_skip))
423423
}
424424

425425
/// Resolves an `Expr::Wildcard` to a collection of qualified `Expr::Column`'s.
@@ -464,7 +464,7 @@ pub fn expand_qualified_wildcard(
464464
columns_to_skip.extend(excluded_columns);
465465
Ok(get_exprs_except_skipped(
466466
&qualified_dfschema,
467-
columns_to_skip,
467+
&columns_to_skip,
468468
))
469469
}
470470

@@ -928,6 +928,7 @@ pub fn find_valid_equijoin_key_pair(
928928
/// round(Float64)
929929
/// round(Float32)
930930
/// ```
931+
#[allow(clippy::needless_pass_by_value)]
931932
pub fn generate_signature_error_msg(
932933
func_name: &str,
933934
func_signature: Signature,

0 commit comments

Comments
 (0)