lyne7-sc commented on code in PR #19909:
URL: https://github.com/apache/datafusion/pull/19909#discussion_r2712625608


##########
datafusion/spark/src/function/math/unhex.rs:
##########
@@ -0,0 +1,394 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use arrow::array::{Array, ArrayRef, BinaryBuilder};
+use arrow::datatypes::DataType;
+use datafusion_common::cast::{
+    as_large_string_array, as_string_array, as_string_view_array,
+};
+use datafusion_common::types::logical_string;
+use datafusion_common::{DataFusionError, Result, ScalarValue, exec_err};
+use datafusion_expr::{
+    Coercion, ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature,
+    TypeSignatureClass, Volatility,
+};
+use std::any::Any;
+use std::sync::Arc;
+
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#unhex>
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkUnhex {
+    signature: Signature,
+    aliases: Vec<String>,
+}
+
+impl Default for SparkUnhex {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl SparkUnhex {
+    pub fn new() -> Self {
+        let string = 
Coercion::new_exact(TypeSignatureClass::Native(logical_string()));
+
+        Self {
+            signature: Signature::coercible(vec![string], 
Volatility::Immutable),
+            aliases: vec![],
+        }
+    }
+}
+
+impl ScalarUDFImpl for SparkUnhex {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn name(&self) -> &str {
+        "unhex"
+    }
+
+    fn aliases(&self) -> &[String] {
+        &self.aliases
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+        Ok(DataType::Binary)
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
+        spark_unhex(&args.args)
+    }
+}
+
+#[inline]
+fn hex_nibble(c: u8) -> Option<u8> {
+    match c {
+        b'0'..=b'9' => Some(c - b'0'),
+        b'a'..=b'f' => Some(c - b'a' + 10),
+        b'A'..=b'F' => Some(c - b'A' + 10),
+        _ => None,
+    }
+}
+
+fn unhex_common(bytes: &[u8], out: &mut Vec<u8>) -> bool {
+    if bytes.is_empty() {
+        return true;
+    }
+
+    let mut i = 0usize;
+
+    // If the hex string length is odd, implicitly left-pad with '0'.
+    if (bytes.len() & 1) == 1 {
+        match hex_nibble(bytes[0]) {
+            // Equivalent to (0 << 4) | lo
+            Some(lo) => out.push(lo),
+            None => return false,
+        }
+        i = 1;
+    }
+
+    while i + 1 < bytes.len() {
+        match (hex_nibble(bytes[i]), hex_nibble(bytes[i + 1])) {
+            (Some(hi), Some(lo)) => out.push((hi << 4) | lo),
+            _ => return false,
+        }
+        i += 2;
+    }
+
+    true
+}
+
+fn unhex_array<I, T>(iter: I, len: usize) -> Result<ArrayRef, DataFusionError>
+where
+    I: Iterator<Item = Option<T>>,
+    T: AsRef<str>,
+{
+    let mut builder = BinaryBuilder::with_capacity(len, len * 32);

Review Comment:
   It was chosen based on benchmark results and seemed like a reasonable 
initial estimate. Thanks for pointing this out, for `StringArray` and 
`LargeStringArray` we can use a more accurate capacity from `values().len()`. 
I’ve updated the code accordingly.



-- 
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]

Reply via email to