Skip to content

Commit b468ba7

Browse files
authored
remove derive(Copy) from Operator (#11132)
* remove derive Copy from Operator * fix formatting and more cloning * fix remaining case * fix docs case * fix operator borrows
1 parent 8216e32 commit b468ba7

File tree

15 files changed

+68
-48
lines changed

15 files changed

+68
-48
lines changed

datafusion/core/src/physical_optimizer/pruning.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -987,8 +987,8 @@ impl<'a> PruningExpressionBuilder<'a> {
987987
})
988988
}
989989

990-
fn op(&self) -> Operator {
991-
self.op
990+
fn op(&self) -> &Operator {
991+
&self.op
992992
}
993993

994994
fn scalar_expr(&self) -> &Arc<dyn PhysicalExpr> {
@@ -1064,7 +1064,7 @@ fn rewrite_expr_to_prunable(
10641064
scalar_expr: &PhysicalExprRef,
10651065
schema: DFSchema,
10661066
) -> Result<(PhysicalExprRef, Operator, PhysicalExprRef)> {
1067-
if !is_compare_op(op) {
1067+
if !is_compare_op(&op) {
10681068
return plan_err!("rewrite_expr_to_prunable only support compare expression");
10691069
}
10701070

@@ -1131,7 +1131,7 @@ fn rewrite_expr_to_prunable(
11311131
}
11321132
}
11331133

1134-
fn is_compare_op(op: Operator) -> bool {
1134+
fn is_compare_op(op: &Operator) -> bool {
11351135
matches!(
11361136
op,
11371137
Operator::Eq
@@ -1358,11 +1358,13 @@ fn build_predicate_expression(
13581358
.map(|e| {
13591359
Arc::new(phys_expr::BinaryExpr::new(
13601360
in_list.expr().clone(),
1361-
eq_op,
1361+
eq_op.clone(),
13621362
e.clone(),
13631363
)) as _
13641364
})
1365-
.reduce(|a, b| Arc::new(phys_expr::BinaryExpr::new(a, re_op, b)) as _)
1365+
.reduce(|a, b| {
1366+
Arc::new(phys_expr::BinaryExpr::new(a, re_op.clone(), b)) as _
1367+
})
13661368
.unwrap();
13671369
return build_predicate_expression(&change_expr, schema, required_columns);
13681370
} else {
@@ -1374,7 +1376,7 @@ fn build_predicate_expression(
13741376
if let Some(bin_expr) = expr_any.downcast_ref::<phys_expr::BinaryExpr>() {
13751377
(
13761378
bin_expr.left().clone(),
1377-
*bin_expr.op(),
1379+
bin_expr.op().clone(),
13781380
bin_expr.right().clone(),
13791381
)
13801382
} else {
@@ -1386,15 +1388,19 @@ fn build_predicate_expression(
13861388
let left_expr = build_predicate_expression(&left, schema, required_columns);
13871389
let right_expr = build_predicate_expression(&right, schema, required_columns);
13881390
// simplify boolean expression if applicable
1389-
let expr = match (&left_expr, op, &right_expr) {
1391+
let expr = match (&left_expr, &op, &right_expr) {
13901392
(left, Operator::And, _) if is_always_true(left) => right_expr,
13911393
(_, Operator::And, right) if is_always_true(right) => left_expr,
13921394
(left, Operator::Or, right)
13931395
if is_always_true(left) || is_always_true(right) =>
13941396
{
13951397
unhandled
13961398
}
1397-
_ => Arc::new(phys_expr::BinaryExpr::new(left_expr, op, right_expr)),
1399+
_ => Arc::new(phys_expr::BinaryExpr::new(
1400+
left_expr,
1401+
op.clone(),
1402+
right_expr,
1403+
)),
13981404
};
13991405
return expr;
14001406
}

datafusion/expr/src/operator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::ops;
2525
use std::ops::Not;
2626

2727
/// Operators applied to expressions
28-
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Hash)]
28+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Hash)]
2929
pub enum Operator {
3030
/// Expressions are equal
3131
Eq,

datafusion/expr/src/type_coercion/binary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,8 +1152,8 @@ mod tests {
11521152
];
11531153
for (i, input_type) in input_types.iter().enumerate() {
11541154
let expect_type = &result_types[i];
1155-
for op in comparison_op_types {
1156-
let (lhs, rhs) = get_input_types(&input_decimal, &op, input_type)?;
1155+
for op in &comparison_op_types {
1156+
let (lhs, rhs) = get_input_types(&input_decimal, op, input_type)?;
11571157
assert_eq!(expect_type, &lhs);
11581158
assert_eq!(expect_type, &rhs);
11591159
}

datafusion/expr/src/utils.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ fn split_conjunction_impl<'a>(expr: &'a Expr, mut exprs: Vec<&'a Expr>) -> Vec<&
996996
/// assert_eq!(split_conjunction_owned(expr), split);
997997
/// ```
998998
pub fn split_conjunction_owned(expr: Expr) -> Vec<Expr> {
999-
split_binary_owned(expr, Operator::And)
999+
split_binary_owned(expr, &Operator::And)
10001000
}
10011001

10021002
/// Splits an owned binary operator tree [`Expr`] such as `A <OP> B <OP> C` => `[A, B, C]`
@@ -1019,19 +1019,19 @@ pub fn split_conjunction_owned(expr: Expr) -> Vec<Expr> {
10191019
/// ];
10201020
///
10211021
/// // use split_binary_owned to split them
1022-
/// assert_eq!(split_binary_owned(expr, Operator::Plus), split);
1022+
/// assert_eq!(split_binary_owned(expr, &Operator::Plus), split);
10231023
/// ```
1024-
pub fn split_binary_owned(expr: Expr, op: Operator) -> Vec<Expr> {
1024+
pub fn split_binary_owned(expr: Expr, op: &Operator) -> Vec<Expr> {
10251025
split_binary_owned_impl(expr, op, vec![])
10261026
}
10271027

10281028
fn split_binary_owned_impl(
10291029
expr: Expr,
1030-
operator: Operator,
1030+
operator: &Operator,
10311031
mut exprs: Vec<Expr>,
10321032
) -> Vec<Expr> {
10331033
match expr {
1034-
Expr::BinaryExpr(BinaryExpr { right, op, left }) if op == operator => {
1034+
Expr::BinaryExpr(BinaryExpr { right, op, left }) if &op == operator => {
10351035
let exprs = split_binary_owned_impl(*left, operator, exprs);
10361036
split_binary_owned_impl(*right, operator, exprs)
10371037
}
@@ -1048,17 +1048,17 @@ fn split_binary_owned_impl(
10481048
/// Splits an binary operator tree [`Expr`] such as `A <OP> B <OP> C` => `[A, B, C]`
10491049
///
10501050
/// See [`split_binary_owned`] for more details and an example.
1051-
pub fn split_binary(expr: &Expr, op: Operator) -> Vec<&Expr> {
1051+
pub fn split_binary<'a>(expr: &'a Expr, op: &Operator) -> Vec<&'a Expr> {
10521052
split_binary_impl(expr, op, vec![])
10531053
}
10541054

10551055
fn split_binary_impl<'a>(
10561056
expr: &'a Expr,
1057-
operator: Operator,
1057+
operator: &Operator,
10581058
mut exprs: Vec<&'a Expr>,
10591059
) -> Vec<&'a Expr> {
10601060
match expr {
1061-
Expr::BinaryExpr(BinaryExpr { right, op, left }) if *op == operator => {
1061+
Expr::BinaryExpr(BinaryExpr { right, op, left }) if op == operator => {
10621062
let exprs = split_binary_impl(left, operator, exprs);
10631063
split_binary_impl(right, operator, exprs)
10641064
}
@@ -1612,13 +1612,13 @@ mod tests {
16121612
#[test]
16131613
fn test_split_binary_owned() {
16141614
let expr = col("a");
1615-
assert_eq!(split_binary_owned(expr.clone(), Operator::And), vec![expr]);
1615+
assert_eq!(split_binary_owned(expr.clone(), &Operator::And), vec![expr]);
16161616
}
16171617

16181618
#[test]
16191619
fn test_split_binary_owned_two() {
16201620
assert_eq!(
1621-
split_binary_owned(col("a").eq(lit(5)).and(col("b")), Operator::And),
1621+
split_binary_owned(col("a").eq(lit(5)).and(col("b")), &Operator::And),
16221622
vec![col("a").eq(lit(5)), col("b")]
16231623
);
16241624
}
@@ -1628,7 +1628,7 @@ mod tests {
16281628
let expr = col("a").eq(lit(5)).or(col("b"));
16291629
assert_eq!(
16301630
// expr is connected by OR, but pass in AND
1631-
split_binary_owned(expr.clone(), Operator::And),
1631+
split_binary_owned(expr.clone(), &Operator::And),
16321632
vec![expr]
16331633
);
16341634
}

datafusion/optimizer/src/analyzer/type_coercion.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl<'a> TypeCoercionRewriter<'a> {
146146
.map(|(lhs, rhs)| {
147147
// coerce the arguments as though they were a single binary equality
148148
// expression
149-
let (lhs, rhs) = self.coerce_binary_op(lhs, Operator::Eq, rhs)?;
149+
let (lhs, rhs) = self.coerce_binary_op(lhs, &Operator::Eq, rhs)?;
150150
Ok((lhs, rhs))
151151
})
152152
.collect::<Result<Vec<_>>>()?;
@@ -157,12 +157,12 @@ impl<'a> TypeCoercionRewriter<'a> {
157157
fn coerce_binary_op(
158158
&self,
159159
left: Expr,
160-
op: Operator,
160+
op: &Operator,
161161
right: Expr,
162162
) -> Result<(Expr, Expr)> {
163163
let (left_type, right_type) = get_input_types(
164164
&left.get_type(self.schema)?,
165-
&op,
165+
op,
166166
&right.get_type(self.schema)?,
167167
)?;
168168
Ok((
@@ -279,7 +279,7 @@ impl<'a> TreeNodeRewriter for TypeCoercionRewriter<'a> {
279279
))))
280280
}
281281
Expr::BinaryExpr(BinaryExpr { left, op, right }) => {
282-
let (left, right) = self.coerce_binary_op(*left, op, *right)?;
282+
let (left, right) = self.coerce_binary_op(*left, &op, *right)?;
283283
Ok(Transformed::yes(Expr::BinaryExpr(BinaryExpr::new(
284284
Box::new(left),
285285
op,

datafusion/optimizer/src/simplify_expressions/utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ pub static POWS_OF_TEN: [i128; 38] = [
6969
/// expressions. Such as: (A AND B) AND C
7070
pub fn expr_contains(expr: &Expr, needle: &Expr, search_op: Operator) -> bool {
7171
match expr {
72-
Expr::BinaryExpr(BinaryExpr { left, op, right }) if *op == search_op => {
73-
expr_contains(left, needle, search_op)
72+
Expr::BinaryExpr(BinaryExpr { left, op, right }) if op == &search_op => {
73+
expr_contains(left, needle, search_op.clone())
7474
|| expr_contains(right, needle, search_op)
7575
}
7676
_ => expr == needle,
@@ -88,7 +88,7 @@ pub fn delete_xor_in_complex_expr(expr: &Expr, needle: &Expr, is_left: bool) ->
8888
) -> Expr {
8989
match expr {
9090
Expr::BinaryExpr(BinaryExpr { left, op, right })
91-
if *op == Operator::BitwiseXor =>
91+
if op == &Operator::BitwiseXor =>
9292
{
9393
let left_expr = recursive_delete_xor_in_expr(left, needle, xor_counter);
9494
let right_expr = recursive_delete_xor_in_expr(right, needle, xor_counter);
@@ -102,7 +102,7 @@ pub fn delete_xor_in_complex_expr(expr: &Expr, needle: &Expr, is_left: bool) ->
102102

103103
Expr::BinaryExpr(BinaryExpr::new(
104104
Box::new(left_expr),
105-
*op,
105+
op.clone(),
106106
Box::new(right_expr),
107107
))
108108
}

datafusion/optimizer/src/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,13 @@ pub fn split_conjunction_owned(expr: Expr) -> Vec<Expr> {
177177
/// ];
178178
///
179179
/// // use split_binary_owned to split them
180-
/// assert_eq!(split_binary_owned(expr, Operator::Plus), split);
180+
/// assert_eq!(split_binary_owned(expr, &Operator::Plus), split);
181181
/// ```
182182
#[deprecated(
183183
since = "34.0.0",
184184
note = "use `datafusion_expr::utils::split_binary_owned` instead"
185185
)]
186-
pub fn split_binary_owned(expr: Expr, op: Operator) -> Vec<Expr> {
186+
pub fn split_binary_owned(expr: Expr, op: &Operator) -> Vec<Expr> {
187187
expr_utils::split_binary_owned(expr, op)
188188
}
189189

@@ -194,7 +194,7 @@ pub fn split_binary_owned(expr: Expr, op: Operator) -> Vec<Expr> {
194194
since = "34.0.0",
195195
note = "use `datafusion_expr::utils::split_binary` instead"
196196
)]
197-
pub fn split_binary(expr: &Expr, op: Operator) -> Vec<&Expr> {
197+
pub fn split_binary<'a>(expr: &'a Expr, op: &Operator) -> Vec<&'a Expr> {
198198
expr_utils::split_binary(expr, op)
199199
}
200200

datafusion/physical-expr-common/src/datum.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn apply_cmp(
6363
/// Applies a binary [`Datum`] comparison kernel `f` to `lhs` and `rhs` for nested type like
6464
/// List, FixedSizeList, LargeList, Struct, Union, Map, or a dictionary of a nested type
6565
pub fn apply_cmp_for_nested(
66-
op: Operator,
66+
op: &Operator,
6767
lhs: &ColumnarValue,
6868
rhs: &ColumnarValue,
6969
) -> Result<ColumnarValue> {
@@ -88,7 +88,7 @@ pub fn apply_cmp_for_nested(
8888

8989
/// Compare on nested type List, Struct, and so on
9090
fn compare_op_for_nested(
91-
op: Operator,
91+
op: &Operator,
9292
lhs: &dyn Datum,
9393
rhs: &dyn Datum,
9494
) -> Result<BooleanArray> {

datafusion/physical-expr/src/expressions/binary.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl PhysicalExpr for BinaryExpr {
269269
if right_data_type != left_data_type {
270270
return internal_err!("type mismatch");
271271
}
272-
return apply_cmp_for_nested(self.op, &lhs, &rhs);
272+
return apply_cmp_for_nested(&self.op, &lhs, &rhs);
273273
}
274274

275275
match self.op {
@@ -329,7 +329,7 @@ impl PhysicalExpr for BinaryExpr {
329329
) -> Result<Arc<dyn PhysicalExpr>> {
330330
Ok(Arc::new(BinaryExpr::new(
331331
children[0].clone(),
332-
self.op,
332+
self.op.clone(),
333333
children[1].clone(),
334334
)))
335335
}

datafusion/physical-expr/src/intervals/cp_solver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ pub fn propagate_arithmetic(
222222
left_child: &Interval,
223223
right_child: &Interval,
224224
) -> Result<Option<(Interval, Interval)>> {
225-
let inverse_op = get_inverse_op(*op)?;
225+
let inverse_op = get_inverse_op(op)?;
226226
match (left_child.data_type(), right_child.data_type()) {
227227
// If we have a child whose type is a time interval (i.e. DataType::Interval),
228228
// we need special handling since timestamp differencing results in a

datafusion/physical-expr/src/intervals/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn check_support(expr: &Arc<dyn PhysicalExpr>, schema: &SchemaRef) -> bool {
6363
}
6464

6565
// This function returns the inverse operator of the given operator.
66-
pub fn get_inverse_op(op: Operator) -> Result<Operator> {
66+
pub fn get_inverse_op(op: &Operator) -> Result<Operator> {
6767
match op {
6868
Operator::Plus => Ok(Operator::Minus),
6969
Operator::Minus => Ok(Operator::Plus),

datafusion/physical-expr/src/planner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub fn create_physical_expr(
196196
//
197197
// There should be no coercion during physical
198198
// planning.
199-
binary(lhs, *op, rhs, input_schema)
199+
binary(lhs, op.clone(), rhs, input_schema)
200200
}
201201
Expr::Like(Like {
202202
negated,

datafusion/physical-expr/src/utils/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use petgraph::stable_graph::StableGraph;
4444
pub fn split_conjunction(
4545
predicate: &Arc<dyn PhysicalExpr>,
4646
) -> Vec<&Arc<dyn PhysicalExpr>> {
47-
split_impl(Operator::And, predicate, vec![])
47+
split_impl(&Operator::And, predicate, vec![])
4848
}
4949

5050
/// Assume the predicate is in the form of DNF, split the predicate to a Vec of PhysicalExprs.
@@ -53,16 +53,16 @@ pub fn split_conjunction(
5353
pub fn split_disjunction(
5454
predicate: &Arc<dyn PhysicalExpr>,
5555
) -> Vec<&Arc<dyn PhysicalExpr>> {
56-
split_impl(Operator::Or, predicate, vec![])
56+
split_impl(&Operator::Or, predicate, vec![])
5757
}
5858

5959
fn split_impl<'a>(
60-
operator: Operator,
60+
operator: &Operator,
6161
predicate: &'a Arc<dyn PhysicalExpr>,
6262
mut exprs: Vec<&'a Arc<dyn PhysicalExpr>>,
6363
) -> Vec<&'a Arc<dyn PhysicalExpr>> {
6464
match predicate.as_any().downcast_ref::<BinaryExpr>() {
65-
Some(binary) if binary.op() == &operator => {
65+
Some(binary) if binary.op() == operator => {
6666
let exprs = split_impl(operator, binary.left(), exprs);
6767
split_impl(operator, binary.right(), exprs)
6868
}

datafusion/proto/src/logical_plan/from_proto.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,11 @@ pub fn parse_expr(
266266
Ok(operands
267267
.into_iter()
268268
.reduce(|left, right| {
269-
Expr::BinaryExpr(BinaryExpr::new(Box::new(left), op, Box::new(right)))
269+
Expr::BinaryExpr(BinaryExpr::new(
270+
Box::new(left),
271+
op.clone(),
272+
Box::new(right),
273+
))
270274
})
271275
.expect("Binary expression could not be reduced to a single expression."))
272276
}

datafusion/substrait/src/logical_plan/producer.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,12 @@ fn to_substrait_join_expr(
664664
extension_info,
665665
)?;
666666
// AND with existing expression
667-
exprs.push(make_binary_op_scalar_func(&l, &r, eq_op, extension_info));
667+
exprs.push(make_binary_op_scalar_func(
668+
&l,
669+
&r,
670+
eq_op.clone(),
671+
extension_info,
672+
));
668673
}
669674
let join_expr: Option<Expression> =
670675
exprs.into_iter().reduce(|acc: Expression, e: Expression| {
@@ -1154,7 +1159,12 @@ pub fn to_substrait_rex(
11541159
let l = to_substrait_rex(ctx, left, schema, col_ref_offset, extension_info)?;
11551160
let r = to_substrait_rex(ctx, right, schema, col_ref_offset, extension_info)?;
11561161

1157-
Ok(make_binary_op_scalar_func(&l, &r, *op, extension_info))
1162+
Ok(make_binary_op_scalar_func(
1163+
&l,
1164+
&r,
1165+
op.clone(),
1166+
extension_info,
1167+
))
11581168
}
11591169
Expr::Case(Case {
11601170
expr,

0 commit comments

Comments
 (0)