advancedxy commented on code in PR #449:
URL: https://github.com/apache/datafusion-comet/pull/449#discussion_r1607703859


##########
core/src/execution/datafusion/expressions/scalar_funcs/hex.rs:
##########
@@ -0,0 +1,191 @@
+// 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 std::sync::Arc;
+
+use arrow::array::as_string_array;
+use arrow_array::StringArray;
+use arrow_schema::DataType;
+use datafusion::logical_expr::ColumnarValue;
+use datafusion_common::{
+    cast::{as_binary_array, as_int64_array},
+    exec_err, DataFusionError, ScalarValue,
+};
+use std::fmt::Write;
+
+fn hex_bytes(bytes: &[u8]) -> Vec<u8> {
+    let length = bytes.len();
+    let mut value = vec![0; length * 2];
+    let mut i = 0;
+    while i < length {
+        value[i * 2] = (bytes[i] & 0xF0) >> 4;
+        value[i * 2 + 1] = bytes[i] & 0x0F;
+        i += 1;
+    }
+    value
+}
+
+fn hex_int64(num: i64) -> String {
+    if num >= 0 {
+        format!("{:X}", num)
+    } else {
+        format!("{:016X}", num as u64)
+    }
+}
+
+fn hex_string(s: &str) -> Vec<u8> {
+    hex_bytes(s.as_bytes())
+}
+
+fn hex_bytes_to_string(bytes: &[u8]) -> Result<String, std::fmt::Error> {
+    let mut hex_string = String::with_capacity(bytes.len() * 2);
+    for byte in bytes {
+        write!(&mut hex_string, "{:01X}", byte)?;

Review Comment:
   By the way, there's `hex_encode` in `scalar_func.rs`, maybe we should moved 
that into this hex.rs instead, so related methods are grouped together.



##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -1038,6 +1038,46 @@ class CometExpressionSuite extends CometTestBase with 
AdaptiveSparkPlanHelper {
       }
     }
   }
+  test("hex") {
+    val str_table = "string_hex_table"
+    withTable(str_table) {

Review Comment:
   Spark hex's  input types are:
   ```scala
     override def inputTypes: Seq[AbstractDataType] =
       Seq(TypeCollection(LongType, BinaryType, StringType))
   ```
   
   I think we need to test input with these types. By the way, it would be 
helpful to test with randomized input, I think you can leverage the 
`generateRows` in #451 and test hex in one sql such as:
   ```
   select hex(long_col) , hex(binary_col), hex(string_col) from $test_table
   ```



##########
core/src/execution/datafusion/expressions/scalar_funcs/hex.rs:
##########
@@ -0,0 +1,191 @@
+// 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 std::sync::Arc;
+
+use arrow::array::as_string_array;
+use arrow_array::StringArray;
+use arrow_schema::DataType;
+use datafusion::logical_expr::ColumnarValue;
+use datafusion_common::{
+    cast::{as_binary_array, as_int64_array},
+    exec_err, DataFusionError, ScalarValue,
+};
+use std::fmt::Write;
+
+fn hex_bytes(bytes: &[u8]) -> Vec<u8> {
+    let length = bytes.len();
+    let mut value = vec![0; length * 2];
+    let mut i = 0;
+    while i < length {
+        value[i * 2] = (bytes[i] & 0xF0) >> 4;
+        value[i * 2 + 1] = bytes[i] & 0x0F;
+        i += 1;
+    }
+    value
+}
+
+fn hex_int64(num: i64) -> String {
+    if num >= 0 {
+        format!("{:X}", num)
+    } else {
+        format!("{:016X}", num as u64)
+    }
+}
+
+fn hex_string(s: &str) -> Vec<u8> {
+    hex_bytes(s.as_bytes())
+}
+
+fn hex_bytes_to_string(bytes: &[u8]) -> Result<String, std::fmt::Error> {
+    let mut hex_string = String::with_capacity(bytes.len() * 2);
+    for byte in bytes {
+        write!(&mut hex_string, "{:01X}", byte)?;

Review Comment:
   hmm, I think it should be `:02X` instead?



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to