adriangb commented on code in PR #22463:
URL: https://github.com/apache/datafusion/pull/22463#discussion_r3320136053
##########
datafusion/physical-expr/src/expressions/not.rs:
##########
@@ -181,13 +183,111 @@ impl PhysicalExpr for NotExpr {
write!(f, "NOT ")?;
self.arg.fmt_sql(f)
}
+
+ #[cfg(feature = "proto")]
+ fn try_to_proto(
+ &self,
+ ctx:
&datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx<'_>,
+ ) -> Result<Option<datafusion_proto_models::protobuf::PhysicalExprNode>> {
+ use datafusion_proto_models::protobuf;
+
+ Ok(Some(protobuf::PhysicalExprNode {
+ expr_id: None,
+ expr_type:
Some(protobuf::physical_expr_node::ExprType::NotExpr(Box::new(
+ protobuf::PhysicalNot {
+ expr: Some(Box::new(ctx.encode_child(&self.arg)?)),
+ },
+ ))),
+ }))
+ }
+}
+
+#[cfg(feature = "proto")]
+impl NotExpr {
+ /// Reconstruct a [`NotExpr`] from its protobuf representation.
+ pub fn try_from_proto(
+ node: &datafusion_proto_models::protobuf::PhysicalExprNode,
+ ctx:
&datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx<'_>,
+ ) -> Result<Arc<dyn PhysicalExpr>> {
+ use datafusion_physical_expr_common::expect_expr_variant;
+ use datafusion_proto_models::protobuf;
+
+ let not_expr = expect_expr_variant!(
+ node,
+ protobuf::physical_expr_node::ExprType::NotExpr,
+ "NotExpr",
+ );
+ let expr = ctx
+ .decode_required_expression(not_expr.expr.as_deref(), "NotExpr",
"expr")
+ .map_err(|err| match err {
+ datafusion_common::DataFusionError::Internal(msg)
+ if msg.starts_with("NotExpr is missing required field
'expr'") =>
+ {
+ internal_datafusion_err!(
+ "NotExpr is missing required field 'expr' (expr_id:
{:?}, expr_type: {:?})",
+ node.expr_id,
+ &node.expr_type
+ )
+ }
+ other => other,
+ })?;
+
+ Ok(Arc::new(NotExpr::new(expr)))
+ }
}
/// Creates a unary expression NOT
pub fn not(arg: Arc<dyn PhysicalExpr>) -> Result<Arc<dyn PhysicalExpr>> {
Ok(Arc::new(NotExpr::new(arg)))
}
+#[cfg(all(test, feature = "proto"))]
+mod proto_tests {
+ use std::sync::Arc;
+
+ use super::*;
+
+ use arrow::datatypes::Schema;
+ use datafusion_common::DataFusionError;
+ use datafusion_physical_expr_common::physical_expr::proto_decode::{
+ PhysicalExprDecode, PhysicalExprDecodeCtx,
+ };
+ use datafusion_proto_models::protobuf::{
+ PhysicalExprNode, PhysicalNot, physical_expr_node,
+ };
+
+ struct NoopDecoder;
+
+ impl PhysicalExprDecode for NoopDecoder {
+ fn decode(
+ &self,
+ _node: &PhysicalExprNode,
+ _schema: &Schema,
+ ) -> Result<Arc<dyn PhysicalExpr>> {
+ unreachable!("missing child should be rejected before decoding")
+ }
+ }
+
+ #[test]
+ fn test_from_proto_missing_child() {
+ let node = PhysicalExprNode {
+ expr_id: Some(42),
+ expr_type: Some(physical_expr_node::ExprType::NotExpr(Box::new(
+ PhysicalNot { expr: None },
+ ))),
+ };
+ let schema = Schema::empty();
+ let decoder = NoopDecoder;
+ let ctx = PhysicalExprDecodeCtx::new(&schema, &decoder);
+
+ let err = NotExpr::try_from_proto(&node, &ctx).unwrap_err();
+ assert!(matches!(err, DataFusionError::Internal(msg)
+ if msg.contains("NotExpr is missing required field 'expr'")
+ && msg.contains("expr_id: Some(42)")
+ && msg.contains("expr_type: Some(NotExpr")));
+ }
+}
+
Review Comment:
maybe the tracking issue is pendantic, but I do think new code at the bottom
of module makes the most sense
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]