Skip to content

Apache DataFusion SQL Query Engine

Add support `UInt64` and other integer data types for `to_hex` (#16335)

Tai Le Manh1d61f31d1216
diff --git a/datafusion/functions/src/string/to_hex.rs b/datafusion/functions/src/string/to_hex.rs
index a3a1acfcf1..a739a4dfb2 100644
--- a/datafusion/functions/src/string/to_hex.rs
+++ b/datafusion/functions/src/string/to_hex.rs
@@ -19,25 +19,29 @@ use std::any::Any;
use std::fmt::Write;
use std::sync::Arc;
-use arrow::array::{ArrayRef, GenericStringBuilder, OffsetSizeTrait};
+use crate::utils::make_scalar_function;
+use arrow::array::{ArrayRef, GenericStringBuilder};
+use arrow::datatypes::DataType::{
+ Int16, Int32, Int64, Int8, UInt16, UInt32, UInt64, UInt8, Utf8,
+};
use arrow::datatypes::{
- ArrowNativeType, ArrowPrimitiveType, DataType, Int32Type, Int64Type,
+ ArrowNativeType, ArrowPrimitiveType, DataType, Int16Type, Int32Type, Int64Type,
+ Int8Type, UInt16Type, UInt32Type, UInt64Type, UInt8Type,
};
-
-use crate::utils::make_scalar_function;
use datafusion_common::cast::as_primitive_array;
use datafusion_common::Result;
use datafusion_common::{exec_err, plan_err};
use datafusion_expr::{ColumnarValue, Documentation};
use datafusion_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility};
+use datafusion_expr_common::signature::TypeSignature::Exact;
use datafusion_macros::user_doc;
/// Converts the number to its equivalent hexadecimal representation.
/// to_hex(2147483647) = '7fffffff'
pub fn to_hex<T: ArrowPrimitiveType>(args: &[ArrayRef]) -> Result<ArrayRef>
where
- T::Native: OffsetSizeTrait,
+ T::Native: std::fmt::LowerHex,
{
let integer_array = as_primitive_array::<T>(&args[0])?;
@@ -96,9 +100,20 @@ impl Default for ToHexFunc {
impl ToHexFunc {
pub fn new() -> Self {
- use DataType::*;
Self {
- signature: Signature::uniform(1, vec![Int64], Volatility::Immutable),
+ signature: Signature::one_of(
+ vec![
+ Exact(vec![Int8]),
+ Exact(vec![Int16]),
+ Exact(vec![Int32]),
+ Exact(vec![Int64]),
+ Exact(vec![UInt8]),
+ Exact(vec![UInt16]),
+ Exact(vec![UInt32]),
+ Exact(vec![UInt64]),
+ ],
+ Volatility::Immutable,
+ ),
}
}
}
@@ -117,10 +132,8 @@ impl ScalarUDFImpl for ToHexFunc {
}
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
- use DataType::*;
-
Ok(match arg_types[0] {
- Int8 | Int16 | Int32 | Int64 => Utf8,
+ Int8 | Int16 | Int32 | Int64 | UInt8 | UInt16 | UInt32 | UInt64 => Utf8,
_ => {
return plan_err!("The to_hex function can only accept integers.");
}
@@ -129,12 +142,14 @@ impl ScalarUDFImpl for ToHexFunc {
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
match args.args[0].data_type() {
- DataType::Int32 => {
- make_scalar_function(to_hex::<Int32Type>, vec![])(&args.args)
- }
- DataType::Int64 => {
- make_scalar_function(to_hex::<Int64Type>, vec![])(&args.args)
- }
+ Int64 => make_scalar_function(to_hex::<Int64Type>, vec![])(&args.args),
+ UInt64 => make_scalar_function(to_hex::<UInt64Type>, vec![])(&args.args),
+ Int32 => make_scalar_function(to_hex::<Int32Type>, vec![])(&args.args),
+ UInt32 => make_scalar_function(to_hex::<UInt32Type>, vec![])(&args.args),
+ Int16 => make_scalar_function(to_hex::<Int16Type>, vec![])(&args.args),
+ UInt16 => make_scalar_function(to_hex::<UInt16Type>, vec![])(&args.args),
+ Int8 => make_scalar_function(to_hex::<Int8Type>, vec![])(&args.args),
+ UInt8 => make_scalar_function(to_hex::<UInt8Type>, vec![])(&args.args),
other => exec_err!("Unsupported data type {other:?} for function to_hex"),
}
}
@@ -146,48 +161,92 @@ impl ScalarUDFImpl for ToHexFunc {
#[cfg(test)]
mod tests {
- use arrow::array::{Int32Array, StringArray};
-
+ use arrow::array::{
+ Int16Array, Int32Array, Int64Array, Int8Array, StringArray, UInt16Array,
+ UInt32Array, UInt64Array, UInt8Array,
+ };
use datafusion_common::cast::as_string_array;
use super::*;
- #[test]
- // Test to_hex function for zero
- fn to_hex_zero() -> Result<()> {
- let array = vec![0].into_iter().collect::<Int32Array>();
- let array_ref = Arc::new(array);
- let hex_value_arc = to_hex::<Int32Type>(&[array_ref])?;
- let hex_value = as_string_array(&hex_value_arc)?;
- let expected = StringArray::from(vec![Some("0")]);
- assert_eq!(&expected, hex_value);
-
- Ok(())
+ macro_rules! test_to_hex_type {
+ // Default test with standard input/output
+ ($name:ident, $arrow_type:ty, $array_type:ty) => {
+ test_to_hex_type!(
+ $name,
+ $arrow_type,
+ $array_type,
+ vec![Some(100), Some(0), None],
+ vec![Some("64"), Some("0"), None]
+ );
+ };
+
+ // Custom test with custom input/output (eg: positive number)
+ ($name:ident, $arrow_type:ty, $array_type:ty, $input:expr, $expected:expr) => {
+ #[test]
+ fn $name() -> Result<()> {
+ let input = $input;
+ let expected = $expected;
+
+ let array = <$array_type>::from(input);
+ let array_ref = Arc::new(array);
+ let hex_result = to_hex::<$arrow_type>(&[array_ref])?;
+ let hex_array = as_string_array(&hex_result)?;
+ let expected_array = StringArray::from(expected);
+
+ assert_eq!(&expected_array, hex_array);
+ Ok(())
+ }
+ };
}
- #[test]
- // Test to_hex function for positive number
- fn to_hex_positive_number() -> Result<()> {
- let array = vec![100].into_iter().collect::<Int32Array>();
- let array_ref = Arc::new(array);
- let hex_value_arc = to_hex::<Int32Type>(&[array_ref])?;
- let hex_value = as_string_array(&hex_value_arc)?;
- let expected = StringArray::from(vec![Some("64")]);
- assert_eq!(&expected, hex_value);
-
- Ok(())
- }
+ test_to_hex_type!(
+ to_hex_int8,
+ Int8Type,
+ Int8Array,
+ vec![Some(100), Some(0), None, Some(-1)],
+ vec![Some("64"), Some("0"), None, Some("ffffffffffffffff")]
+ );
+ test_to_hex_type!(
+ to_hex_int16,
+ Int16Type,
+ Int16Array,
+ vec![Some(100), Some(0), None, Some(-1)],
+ vec![Some("64"), Some("0"), None, Some("ffffffffffffffff")]
+ );
+ test_to_hex_type!(
+ to_hex_int32,
+ Int32Type,
+ Int32Array,
+ vec![Some(100), Some(0), None, Some(-1)],
+ vec![Some("64"), Some("0"), None, Some("ffffffffffffffff")]
+ );
+ test_to_hex_type!(
+ to_hex_int64,
+ Int64Type,
+ Int64Array,
+ vec![Some(100), Some(0), None, Some(-1)],
+ vec![Some("64"), Some("0"), None, Some("ffffffffffffffff")]
+ );
- #[test]
- // Test to_hex function for negative number
- fn to_hex_negative_number() -> Result<()> {
- let array = vec![-1].into_iter().collect::<Int32Array>();
- let array_ref = Arc::new(array);
- let hex_value_arc = to_hex::<Int32Type>(&[array_ref])?;
- let hex_value = as_string_array(&hex_value_arc)?;
- let expected = StringArray::from(vec![Some("ffffffffffffffff")]);
- assert_eq!(&expected, hex_value);
-
- Ok(())
- }
+ test_to_hex_type!(to_hex_uint8, UInt8Type, UInt8Array);
+ test_to_hex_type!(to_hex_uint16, UInt16Type, UInt16Array);
+ test_to_hex_type!(to_hex_uint32, UInt32Type, UInt32Array);
+ test_to_hex_type!(to_hex_uint64, UInt64Type, UInt64Array);
+
+ test_to_hex_type!(
+ to_hex_large_signed,
+ Int64Type,
+ Int64Array,
+ vec![Some(i64::MAX), Some(i64::MIN)],
+ vec![Some("7fffffffffffffff"), Some("8000000000000000")]
+ );
+
+ test_to_hex_type!(
+ to_hex_large_unsigned,
+ UInt64Type,
+ UInt64Array,
+ vec![Some(u64::MAX), Some(u64::MIN)],
+ vec![Some("ffffffffffffffff"), Some("0")]
+ );
}
diff --git a/datafusion/sqllogictest/test_files/expr.slt b/datafusion/sqllogictest/test_files/expr.slt
index e4d0b72338..67a2af1187 100644
--- a/datafusion/sqllogictest/test_files/expr.slt
+++ b/datafusion/sqllogictest/test_files/expr.slt
@@ -698,6 +698,11 @@ SELECT to_hex(2147483647)
----
7fffffff
+query T
+SELECT to_hex(CAST(2147483647 as BIGINT UNSIGNED))
+----
+7fffffff
+
query T
SELECT to_hex(9223372036854775807)
----