comphead commented on code in PR #20044:
URL: https://github.com/apache/datafusion/pull/20044#discussion_r2743853149
##########
datafusion/spark/src/function/math/hex.rs:
##########
@@ -101,63 +97,81 @@ impl ScalarUDFImpl for SparkHex {
})
}
- fn invoke_with_args(
- &self,
- args: ScalarFunctionArgs,
- ) -> datafusion_common::Result<ColumnarValue> {
- spark_hex(&args.args)
- }
-
- fn aliases(&self) -> &[String] {
- &self.aliases
+ fn invoke_with_args(&self, args: ScalarFunctionArgs) ->
Result<ColumnarValue> {
+ make_scalar_function(compute_hex,
vec![Hint::AcceptsSingular])(&args.args)
}
}
-/// Hex encoding lookup tables for fast byte-to-hex conversion
-const HEX_CHARS_LOWER: &[u8; 16] = b"0123456789abcdef";
-const HEX_CHARS_UPPER: &[u8; 16] = b"0123456789ABCDEF";
-
-#[inline]
-fn hex_int64(num: i64, buffer: &mut [u8; 16]) -> &[u8] {
- if num == 0 {
- return b"0";
+fn compute_hex(args: &[ArrayRef]) -> Result<ArrayRef> {
+ let [array] = take_function_args("hex", args)?;
+ downcast_dictionary_array! {
+ array => {
+ let values = hex_values(array.values())?;
+ Ok(Arc::new(array.with_values(values)))
+ },
+ _ => {
+ hex_values(array)
+ }
}
+}
- let mut n = num as u64;
- let mut i = 16;
- while n != 0 {
- i -= 1;
- buffer[i] = HEX_CHARS_UPPER[(n & 0xF) as usize];
- n >>= 4;
+fn hex_values(array: &ArrayRef) -> Result<ArrayRef> {
+ match array.data_type() {
+ DataType::Int64 => {
+ let array = as_int64_array(array)?;
+ hex_encode_int64(array.iter())
+ }
+ DataType::Utf8 => {
+ let array = as_string_array(array)?;
+ hex_encode_bytes(array.iter())
+ }
+ DataType::Utf8View => {
+ let array = as_string_view_array(array)?;
+ hex_encode_bytes(array.iter())
+ }
+ DataType::LargeUtf8 => {
+ let array = as_large_string_array(array)?;
+ hex_encode_bytes(array.iter())
+ }
+ DataType::Binary => {
+ let array = as_binary_array(array)?;
+ hex_encode_bytes(array.iter())
+ }
+ DataType::LargeBinary => {
+ let array = as_large_binary_array(array)?;
+ hex_encode_bytes(array.iter())
+ }
+ DataType::BinaryView => {
+ let array = as_binary_view_array(array)?;
+ hex_encode_bytes(array.iter())
+ }
+ DataType::FixedSizeBinary(_) => {
+ let array = as_fixed_size_binary_array(array)?;
+ hex_encode_bytes(array.iter())
+ }
+ dt => internal_err!("Unexpected data type for hex: {dt}"),
}
- &buffer[i..]
}
+/// Hex encoding lookup tables for fast byte-to-hex conversion
+const HEX_CHARS_UPPER: &[u8; 16] = b"0123456789ABCDEF";
Review Comment:
👍
--
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]