Skip to content

Commit b9877e6

Browse files
committed
refactor(physical-expr): adopt new proto helpers in already-migrated expressions
Ports the existing `try_to_proto` / `try_from_proto` implementations onto the helpers introduced in the previous commit (`expect_expr_variant!`) and the wider use of `decode_required_expression` / `decode_children_expressions` / `encode_children_expressions` from #22513. Covers every expression already migrated under #22418: - `Column`, `BinaryExpr` (originally #21929) - `LikeExpr` (#22471) - `InListExpr` (#22503) - `NegativeExpr` (#22483) `BinaryExpr` additionally switches its `l`/`r` legacy-decode arms to `decode_required_expression`, removing two more hand-rolled "missing required field" strings, and runs its `operands` encode/ decode loops through `encode_children_expressions` / `decode_children_expressions`. One existing test changes assertion text — `InListExpr`'s rejected- variant message was the only one using the article "an" instead of the macro's article-free "a"; updated to match. No wire-format change; `cargo test -p datafusion-proto --test proto_integration` is green (173 / 173).
1 parent bb84358 commit b9877e6

5 files changed

Lines changed: 42 additions & 57 deletions

File tree

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

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,7 @@ impl PhysicalExpr for BinaryExpr {
638638
// Reverse so operands are ordered from left innermost to right outermost.
639639
operand_refs.reverse();
640640

641-
let operands = operand_refs
642-
.iter()
643-
.map(|e| ctx.encode_child(e))
644-
.collect::<Result<Vec<_>>>()?;
641+
let operands = ctx.encode_children_expressions(operand_refs)?;
645642

646643
Ok(Some(protobuf::PhysicalExprNode {
647644
expr_id: None,
@@ -675,11 +672,13 @@ impl BinaryExpr {
675672
node: &datafusion_proto_models::protobuf::PhysicalExprNode,
676673
ctx: &datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx<'_>,
677674
) -> Result<Arc<dyn PhysicalExpr>> {
675+
use datafusion_physical_expr_common::expect_expr_variant;
678676
use datafusion_proto_models::protobuf;
679-
let node = match &node.expr_type {
680-
Some(protobuf::physical_expr_node::ExprType::BinaryExpr(b)) => b.as_ref(),
681-
_ => return internal_err!("PhysicalExprNode is not a BinaryExpr"),
682-
};
677+
let node = expect_expr_variant!(
678+
node,
679+
protobuf::physical_expr_node::ExprType::BinaryExpr,
680+
"BinaryExpr",
681+
);
683682
let op = Operator::from_proto_name(&node.op).ok_or_else(|| {
684683
datafusion_common::DataFusionError::Internal(format!(
685684
"Unsupported binary operator '{}'",
@@ -690,17 +689,12 @@ impl BinaryExpr {
690689
if !node.operands.is_empty() {
691690
// New linearized format: reduce the flat operands list back into
692691
// a nested binary expression tree.
693-
let operands = node
694-
.operands
695-
.iter()
696-
.map(|e| ctx.decode(e))
697-
.collect::<Result<Vec<_>>>()?;
692+
let operands = ctx.decode_children_expressions(&node.operands)?;
698693

699694
if operands.len() < 2 {
700-
return Err(datafusion_common::DataFusionError::Internal(
695+
return internal_err!(
701696
"A binary expression must always have at least 2 operands"
702-
.to_string(),
703-
));
697+
);
704698
}
705699

706700
Ok(operands
@@ -711,21 +705,11 @@ impl BinaryExpr {
711705
.expect("Binary expression could not be reduced to a single expression."))
712706
} else {
713707
// Legacy format with l/r fields.
714-
let left = node.l.as_deref().ok_or_else(|| {
715-
datafusion_common::DataFusionError::Internal(
716-
"BinaryExpr is missing required field 'left'".to_string(),
717-
)
718-
})?;
719-
let right = node.r.as_deref().ok_or_else(|| {
720-
datafusion_common::DataFusionError::Internal(
721-
"BinaryExpr is missing required field 'right'".to_string(),
722-
)
723-
})?;
724-
Ok(Arc::new(BinaryExpr::new(
725-
ctx.decode(left)?,
726-
op,
727-
ctx.decode(right)?,
728-
)))
708+
let left =
709+
ctx.decode_required_expression(node.l.as_deref(), "BinaryExpr", "left")?;
710+
let right =
711+
ctx.decode_required_expression(node.r.as_deref(), "BinaryExpr", "right")?;
712+
Ok(Arc::new(BinaryExpr::new(left, op, right)))
729713
}
730714
}
731715
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,13 @@ impl Column {
182182
node: &datafusion_proto_models::protobuf::PhysicalExprNode,
183183
_ctx: &datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx<'_>,
184184
) -> Result<Arc<dyn PhysicalExpr>> {
185+
use datafusion_physical_expr_common::expect_expr_variant;
185186
use datafusion_proto_models::protobuf;
186-
let protobuf::PhysicalColumn { name, index } = match &node.expr_type {
187-
Some(protobuf::physical_expr_node::ExprType::Column(c)) => c,
188-
_ => return internal_err!("PhysicalExprNode is not a Column"),
189-
};
187+
let protobuf::PhysicalColumn { name, index } = expect_expr_variant!(
188+
node,
189+
protobuf::physical_expr_node::ExprType::Column,
190+
"Column",
191+
);
190192
Ok(Arc::new(Column::new(name, *index as usize)))
191193
}
192194
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,14 @@ impl InListExpr {
252252
node: &datafusion_proto_models::protobuf::PhysicalExprNode,
253253
ctx: &datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx<'_>,
254254
) -> Result<Arc<dyn PhysicalExpr>> {
255+
use datafusion_physical_expr_common::expect_expr_variant;
255256
use datafusion_proto_models::protobuf;
256257

257-
let node = match &node.expr_type {
258-
Some(protobuf::physical_expr_node::ExprType::InList(n)) => n,
259-
_ => {
260-
return datafusion_common::internal_err!(
261-
"PhysicalExprNode is not an InList"
262-
);
263-
}
264-
};
258+
let node = expect_expr_variant!(
259+
node,
260+
protobuf::physical_expr_node::ExprType::InList,
261+
"InList",
262+
);
265263

266264
let expr =
267265
ctx.decode_required_expression(node.expr.as_deref(), "InListExpr", "expr")?;
@@ -3981,7 +3979,7 @@ mod proto_tests {
39813979
let err = InListExpr::try_from_proto(&node, &ctx).unwrap_err();
39823980
assert!(matches!(
39833981
err,
3984-
DataFusionError::Internal(msg) if msg.contains("PhysicalExprNode is not an InList")
3982+
DataFusionError::Internal(msg) if msg.contains("PhysicalExprNode is not a InList")
39853983
));
39863984
}
39873985

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,14 @@ impl LikeExpr {
180180
node: &datafusion_proto_models::protobuf::PhysicalExprNode,
181181
ctx: &datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx<'_>,
182182
) -> Result<Arc<dyn PhysicalExpr>> {
183-
use datafusion_common::internal_err;
183+
use datafusion_physical_expr_common::expect_expr_variant;
184184
use datafusion_proto_models::protobuf;
185185

186-
let like_expr = match &node.expr_type {
187-
Some(protobuf::physical_expr_node::ExprType::LikeExpr(like_expr)) => {
188-
like_expr.as_ref()
189-
}
190-
_ => return internal_err!("PhysicalExprNode is not a LikeExpr"),
191-
};
186+
let like_expr = expect_expr_variant!(
187+
node,
188+
protobuf::physical_expr_node::ExprType::LikeExpr,
189+
"LikeExpr",
190+
);
192191

193192
Ok(Arc::new(LikeExpr::new(
194193
like_expr.negated,

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,16 @@ impl NegativeExpr {
200200
node: &datafusion_proto_models::protobuf::PhysicalExprNode,
201201
ctx: &datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx<'_>,
202202
) -> Result<Arc<dyn PhysicalExpr>> {
203+
use datafusion_physical_expr_common::expect_expr_variant;
203204
use datafusion_proto_models::protobuf;
204205

205-
let expr = match &node.expr_type {
206-
Some(protobuf::physical_expr_node::ExprType::Negative(n)) => {
207-
ctx.decode_required_expression(n.expr.as_deref(), "NegativeExpr", "expr")?
208-
}
209-
_ => return internal_err!("PhysicalExprNode is not a Negative"),
210-
};
206+
let n = expect_expr_variant!(
207+
node,
208+
protobuf::physical_expr_node::ExprType::Negative,
209+
"Negative",
210+
);
211+
let expr =
212+
ctx.decode_required_expression(n.expr.as_deref(), "NegativeExpr", "expr")?;
211213

212214
Ok(Arc::new(NegativeExpr::new(expr)))
213215
}

0 commit comments

Comments
 (0)