You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by al...@apache.org on 2022/12/02 21:17:51 UTC

[arrow-datafusion] branch master updated: Replace `&Option` with `Option<&T>` (#4458)

This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-datafusion.git


The following commit(s) were added to refs/heads/master by this push:
     new 27dc29503 Replace `&Option<T>` with `Option<&T>` (#4458)
27dc29503 is described below

commit 27dc295039454e6110f485b43e9db37826706aaa
Author: askoa <11...@users.noreply.github.com>
AuthorDate: Fri Dec 2 16:17:46 2022 -0500

    Replace `&Option<T>` with `Option<&T>` (#4458)
    
    Co-authored-by: askoa <as...@local>
---
 datafusion/jit/src/jit.rs                          | 11 ++-
 datafusion/physical-expr/src/crypto_expressions.rs | 10 +--
 datafusion/proto/src/from_proto.rs                 | 16 +++--
 datafusion/proto/src/physical_plan/from_proto.rs   |  6 +-
 datafusion/proto/src/to_proto.rs                   | 83 +++++++++++++---------
 5 files changed, 76 insertions(+), 50 deletions(-)

diff --git a/datafusion/jit/src/jit.rs b/datafusion/jit/src/jit.rs
index 21b0d44fb..9c28940fe 100644
--- a/datafusion/jit/src/jit.rs
+++ b/datafusion/jit/src/jit.rs
@@ -205,8 +205,13 @@ impl JIT {
         builder.seal_block(entry_block);
 
         // Walk the AST and declare all variables.
-        let variables =
-            declare_variables(&mut builder, &params, &the_return, &stmts, entry_block);
+        let variables = declare_variables(
+            &mut builder,
+            &params,
+            the_return.as_ref(),
+            &stmts,
+            entry_block,
+        );
 
         // Now translate the statements of the function body.
         let mut trans = FunctionTranslator {
@@ -652,7 +657,7 @@ fn typed_zero(typ: JITType, builder: &mut FunctionBuilder) -> Value {
 fn declare_variables(
     builder: &mut FunctionBuilder,
     params: &[(String, JITType)],
-    the_return: &Option<(String, JITType)>,
+    the_return: Option<&(String, JITType)>,
     stmts: &[Stmt],
     entry_block: Block,
 ) -> HashMap<String, Variable> {
diff --git a/datafusion/physical-expr/src/crypto_expressions.rs b/datafusion/physical-expr/src/crypto_expressions.rs
index 228e6a196..07ebdbfd1 100644
--- a/datafusion/physical-expr/src/crypto_expressions.rs
+++ b/datafusion/physical-expr/src/crypto_expressions.rs
@@ -72,10 +72,12 @@ fn digest_process(
             ))),
         },
         ColumnarValue::Scalar(scalar) => match scalar {
-            ScalarValue::Utf8(a) | ScalarValue::LargeUtf8(a) => Ok(digest_algorithm
-                .digest_scalar(&a.as_ref().map(|s: &String| s.as_bytes()))),
+            ScalarValue::Utf8(a) | ScalarValue::LargeUtf8(a) => {
+                Ok(digest_algorithm
+                    .digest_scalar(a.as_ref().map(|s: &String| s.as_bytes())))
+            }
             ScalarValue::Binary(a) | ScalarValue::LargeBinary(a) => Ok(digest_algorithm
-                .digest_scalar(&a.as_ref().map(|v: &Vec<u8>| v.as_slice()))),
+                .digest_scalar(a.as_ref().map(|v: &Vec<u8>| v.as_slice()))),
             other => Err(DataFusionError::Internal(format!(
                 "Unsupported data type {:?} for function {}",
                 other, digest_algorithm,
@@ -112,7 +114,7 @@ macro_rules! digest_to_scalar {
 
 impl DigestAlgorithm {
     /// digest an optional string to its hash value, null values are returned as is
-    fn digest_scalar(self, value: &Option<&[u8]>) -> ColumnarValue {
+    fn digest_scalar(self, value: Option<&[u8]>) -> ColumnarValue {
         ColumnarValue::Scalar(match self {
             Self::Md5 => digest_to_scalar!(Md5, value),
             Self::Sha224 => digest_to_scalar!(Sha224, value),
diff --git a/datafusion/proto/src/from_proto.rs b/datafusion/proto/src/from_proto.rs
index a30e7b323..1496050d2 100644
--- a/datafusion/proto/src/from_proto.rs
+++ b/datafusion/proto/src/from_proto.rs
@@ -894,10 +894,16 @@ pub fn parse_expr(
                 .when_then_expr
                 .iter()
                 .map(|e| {
-                    let when_expr =
-                        parse_required_expr_inner(&e.when_expr, registry, "when_expr")?;
-                    let then_expr =
-                        parse_required_expr_inner(&e.then_expr, registry, "then_expr")?;
+                    let when_expr = parse_required_expr_inner(
+                        e.when_expr.as_ref(),
+                        registry,
+                        "when_expr",
+                    )?;
+                    let then_expr = parse_required_expr_inner(
+                        e.then_expr.as_ref(),
+                        registry,
+                        "then_expr",
+                    )?;
                     Ok((Box::new(when_expr), Box::new(then_expr)))
                 })
                 .collect::<Result<Vec<(Box<Expr>, Box<Expr>)>, Error>>()?;
@@ -1352,7 +1358,7 @@ fn parse_required_expr(
 }
 
 fn parse_required_expr_inner(
-    p: &Option<protobuf::LogicalExprNode>,
+    p: Option<&protobuf::LogicalExprNode>,
     registry: &dyn FunctionRegistry,
     field: impl Into<String>,
 ) -> Result<Expr, Error> {
diff --git a/datafusion/proto/src/physical_plan/from_proto.rs b/datafusion/proto/src/physical_plan/from_proto.rs
index c61339a31..825399825 100644
--- a/datafusion/proto/src/physical_plan/from_proto.rs
+++ b/datafusion/proto/src/physical_plan/from_proto.rs
@@ -144,13 +144,13 @@ pub(crate) fn parse_physical_expr(
                 .map(|e| {
                     Ok((
                         parse_required_physical_expr(
-                            &e.when_expr,
+                            e.when_expr.as_ref(),
                             registry,
                             "when_expr",
                             input_schema,
                         )?,
                         parse_required_physical_expr(
-                            &e.then_expr,
+                            e.then_expr.as_ref(),
                             registry,
                             "then_expr",
                             input_schema,
@@ -237,7 +237,7 @@ fn parse_required_physical_box_expr(
 }
 
 fn parse_required_physical_expr(
-    expr: &Option<protobuf::PhysicalExprNode>,
+    expr: Option<&protobuf::PhysicalExprNode>,
     registry: &dyn FunctionRegistry,
     field: &str,
     input_schema: &Schema,
diff --git a/datafusion/proto/src/to_proto.rs b/datafusion/proto/src/to_proto.rs
index a73565ae6..92e4214b8 100644
--- a/datafusion/proto/src/to_proto.rs
+++ b/datafusion/proto/src/to_proto.rs
@@ -912,43 +912,53 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
         let data_type = val.get_datatype();
         match val {
             scalar::ScalarValue::Boolean(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::BoolValue(*s))
+                create_proto_scalar(val.as_ref(), &data_type, |s| Value::BoolValue(*s))
             }
             scalar::ScalarValue::Float32(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::Float32Value(*s))
+                create_proto_scalar(val.as_ref(), &data_type, |s| Value::Float32Value(*s))
             }
             scalar::ScalarValue::Float64(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::Float64Value(*s))
+                create_proto_scalar(val.as_ref(), &data_type, |s| Value::Float64Value(*s))
             }
             scalar::ScalarValue::Int8(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::Int8Value(*s as i32))
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
+                    Value::Int8Value(*s as i32)
+                })
             }
             scalar::ScalarValue::Int16(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::Int16Value(*s as i32))
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
+                    Value::Int16Value(*s as i32)
+                })
             }
             scalar::ScalarValue::Int32(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::Int32Value(*s))
+                create_proto_scalar(val.as_ref(), &data_type, |s| Value::Int32Value(*s))
             }
             scalar::ScalarValue::Int64(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::Int64Value(*s))
+                create_proto_scalar(val.as_ref(), &data_type, |s| Value::Int64Value(*s))
             }
             scalar::ScalarValue::UInt8(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::Uint8Value(*s as u32))
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
+                    Value::Uint8Value(*s as u32)
+                })
             }
             scalar::ScalarValue::UInt16(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::Uint16Value(*s as u32))
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
+                    Value::Uint16Value(*s as u32)
+                })
             }
             scalar::ScalarValue::UInt32(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::Uint32Value(*s))
+                create_proto_scalar(val.as_ref(), &data_type, |s| Value::Uint32Value(*s))
             }
             scalar::ScalarValue::UInt64(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::Uint64Value(*s))
+                create_proto_scalar(val.as_ref(), &data_type, |s| Value::Uint64Value(*s))
             }
             scalar::ScalarValue::Utf8(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::Utf8Value(s.to_owned()))
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
+                    Value::Utf8Value(s.to_owned())
+                })
             }
             scalar::ScalarValue::LargeUtf8(val) => {
-                create_proto_scalar(val, &data_type, |s| {
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
                     Value::LargeUtf8Value(s.to_owned())
                 })
             }
@@ -977,10 +987,10 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
                 })
             }
             datafusion::scalar::ScalarValue::Date32(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::Date32Value(*s))
+                create_proto_scalar(val.as_ref(), &data_type, |s| Value::Date32Value(*s))
             }
             datafusion::scalar::ScalarValue::TimestampMicrosecond(val, tz) => {
-                create_proto_scalar(val, &data_type, |s| {
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
                     Value::TimestampValue(protobuf::ScalarTimestampValue {
                         timezone: tz.as_ref().unwrap_or(&"".to_string()).clone(),
                         value: Some(
@@ -992,7 +1002,7 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
                 })
             }
             datafusion::scalar::ScalarValue::TimestampNanosecond(val, tz) => {
-                create_proto_scalar(val, &data_type, |s| {
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
                     Value::TimestampValue(protobuf::ScalarTimestampValue {
                         timezone: tz.as_ref().unwrap_or(&"".to_string()).clone(),
                         value: Some(
@@ -1022,10 +1032,10 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
                 }),
             },
             datafusion::scalar::ScalarValue::Date64(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::Date64Value(*s))
+                create_proto_scalar(val.as_ref(), &data_type, |s| Value::Date64Value(*s))
             }
             datafusion::scalar::ScalarValue::TimestampSecond(val, tz) => {
-                create_proto_scalar(val, &data_type, |s| {
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
                     Value::TimestampValue(protobuf::ScalarTimestampValue {
                         timezone: tz.as_ref().unwrap_or(&"".to_string()).clone(),
                         value: Some(
@@ -1035,7 +1045,7 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
                 })
             }
             datafusion::scalar::ScalarValue::TimestampMillisecond(val, tz) => {
-                create_proto_scalar(val, &data_type, |s| {
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
                     Value::TimestampValue(protobuf::ScalarTimestampValue {
                         timezone: tz.as_ref().unwrap_or(&"".to_string()).clone(),
                         value: Some(
@@ -1047,27 +1057,31 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
                 })
             }
             datafusion::scalar::ScalarValue::IntervalYearMonth(val) => {
-                create_proto_scalar(val, &data_type, |s| {
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
                     Value::IntervalYearmonthValue(*s)
                 })
             }
             datafusion::scalar::ScalarValue::IntervalDayTime(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::IntervalDaytimeValue(*s))
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
+                    Value::IntervalDaytimeValue(*s)
+                })
             }
             datafusion::scalar::ScalarValue::Null => Ok(protobuf::ScalarValue {
                 value: Some(Value::NullValue((&data_type).try_into()?)),
             }),
 
             scalar::ScalarValue::Binary(val) => {
-                create_proto_scalar(val, &data_type, |s| Value::BinaryValue(s.to_owned()))
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
+                    Value::BinaryValue(s.to_owned())
+                })
             }
             scalar::ScalarValue::LargeBinary(val) => {
-                create_proto_scalar(val, &data_type, |s| {
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
                     Value::LargeBinaryValue(s.to_owned())
                 })
             }
             scalar::ScalarValue::FixedSizeBinary(length, val) => {
-                create_proto_scalar(val, &data_type, |s| {
+                create_proto_scalar(val.as_ref(), &data_type, |s| {
                     Value::FixedSizeBinaryValue(protobuf::ScalarFixedSizeBinary {
                         values: s.to_owned(),
                         length: *length,
@@ -1076,7 +1090,7 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
             }
 
             datafusion::scalar::ScalarValue::Time32Second(v) => {
-                create_proto_scalar(v, &data_type, |v| {
+                create_proto_scalar(v.as_ref(), &data_type, |v| {
                     Value::Time32Value(protobuf::ScalarTime32Value {
                         value: Some(
                             protobuf::scalar_time32_value::Value::Time32SecondValue(*v),
@@ -1086,7 +1100,7 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
             }
 
             datafusion::scalar::ScalarValue::Time32Millisecond(v) => {
-                create_proto_scalar(v, &data_type, |v| {
+                create_proto_scalar(v.as_ref(), &data_type, |v| {
                     Value::Time32Value(protobuf::ScalarTime32Value {
                         value: Some(
                             protobuf::scalar_time32_value::Value::Time32MillisecondValue(
@@ -1098,7 +1112,7 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
             }
 
             datafusion::scalar::ScalarValue::Time64Microsecond(v) => {
-                create_proto_scalar(v, &data_type, |v| {
+                create_proto_scalar(v.as_ref(), &data_type, |v| {
                     Value::Time64Value(protobuf::ScalarTime64Value {
                         value: Some(
                             protobuf::scalar_time64_value::Value::Time64MicrosecondValue(
@@ -1110,7 +1124,7 @@ impl TryFrom<&ScalarValue> for protobuf::ScalarValue {
             }
 
             datafusion::scalar::ScalarValue::Time64Nanosecond(v) => {
-                create_proto_scalar(v, &data_type, |v| {
+                create_proto_scalar(v.as_ref(), &data_type, |v| {
                     Value::Time64Value(protobuf::ScalarTime64Value {
                         value: Some(
                             protobuf::scalar_time64_value::Value::Time64NanosecondValue(
@@ -1286,16 +1300,15 @@ impl From<&IntervalUnit> for protobuf::IntervalUnit {
 /// Creates a scalar protobuf value from an optional value (T), and
 /// encoding None as the appropriate datatype
 fn create_proto_scalar<I, T: FnOnce(&I) -> protobuf::scalar_value::Value>(
-    v: &Option<I>,
+    v: Option<&I>,
     null_arrow_type: &DataType,
     constructor: T,
 ) -> Result<protobuf::ScalarValue, Error> {
-    let value =
-        v.as_ref()
-            .map(constructor)
-            .unwrap_or(protobuf::scalar_value::Value::NullValue(
-                null_arrow_type.try_into()?,
-            ));
+    let value = v
+        .map(constructor)
+        .unwrap_or(protobuf::scalar_value::Value::NullValue(
+            null_arrow_type.try_into()?,
+        ));
 
     Ok(protobuf::ScalarValue { value: Some(value) })
 }