Skip to content

Commit 5cf7bff

Browse files
authored
refactor: move arrow integer hex dispatch to datafusion-common (apache#23917)
## Which issue does this PR close? - Closes apache#23811. ## Rationale for this change Share Arrow integer-to-hex dispatch between `to_hex` and Spark `hex`. ## What changes are included in this PR? move tohex and integer implementations to common ## Are these changes tested? Existing `to_hex` and spark `hex` tests cover both ## Are there any user-facing changes? No behavior change but ToHex is public now
1 parent 9f1c5c9 commit 5cf7bff

3 files changed

Lines changed: 45 additions & 45 deletions

File tree

‎datafusion/common/src/utils/hex.rs‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
//! integer, trimming leading zeros. All four take a [`HexCase`] to choose
2424
//! between lowercase and uppercase digits.
2525
26+
use arrow::datatypes::ArrowNativeType;
27+
2628
use crate::Result;
2729
use crate::error::_internal_err;
2830

@@ -72,6 +74,44 @@ impl HexCase {
7274
}
7375
}
7476

77+
/// Trait for converting integer types to hexadecimal in a buffer
78+
pub trait ToHex: ArrowNativeType {
79+
/// Writes the hex representation into `buf` and returns the written
80+
/// subslice. Digits are right-aligned with leading zeros trimmed.
81+
fn write_hex(self, case: HexCase, buf: &mut [u8; 16]) -> &[u8];
82+
}
83+
84+
macro_rules! impl_to_hex_signed {
85+
($ty:ty) => {
86+
impl ToHex for $ty {
87+
#[inline(always)]
88+
fn write_hex(self, case: HexCase, buf: &mut [u8; 16]) -> &[u8] {
89+
encode_u64(self as i64 as u64, case, buf)
90+
}
91+
}
92+
};
93+
}
94+
95+
macro_rules! impl_to_hex_unsigned {
96+
($ty:ty) => {
97+
impl ToHex for $ty {
98+
#[inline(always)]
99+
fn write_hex(self, case: HexCase, buf: &mut [u8; 16]) -> &[u8] {
100+
encode_u64(self as u64, case, buf)
101+
}
102+
}
103+
};
104+
}
105+
106+
impl_to_hex_signed!(i8);
107+
impl_to_hex_signed!(i16);
108+
impl_to_hex_signed!(i32);
109+
impl_to_hex_signed!(i64);
110+
impl_to_hex_unsigned!(u8);
111+
impl_to_hex_unsigned!(u16);
112+
impl_to_hex_unsigned!(u32);
113+
impl_to_hex_unsigned!(u64);
114+
75115
/// Appends the hex encoding of `bytes` to `out`.
76116
///
77117
/// Allocates only through `out`'s own growth. Callers that must bound or guard

‎datafusion/functions/src/string/to_hex.rs‎

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use arrow::datatypes::{
2424
Int64Type, UInt8Type, UInt16Type, UInt32Type, UInt64Type,
2525
};
2626
use datafusion_common::cast::as_primitive_array;
27-
use datafusion_common::utils::hex::{HexCase, encode_u64};
27+
use datafusion_common::utils::hex::{HexCase, ToHex};
2828
use datafusion_common::{Result, ScalarValue, exec_err, internal_err};
2929
use datafusion_expr::{
3030
Coercion, ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
@@ -57,7 +57,7 @@ where
5757
// Process all values directly (including null slots - we write empty strings for nulls)
5858
// The null bitmap will mark which entries are actually null
5959
for value in integer_array.values() {
60-
values.extend_from_slice(value.write_hex(&mut hex_buffer));
60+
values.extend_from_slice(value.write_hex(HexCase::Lower, &mut hex_buffer));
6161
offsets.push(values.len() as i32);
6262
}
6363

@@ -76,51 +76,11 @@ where
7676
#[inline]
7777
fn to_hex_scalar<T: ToHex>(value: T) -> String {
7878
let mut hex_buffer = [0u8; 16];
79-
let hex = value.write_hex(&mut hex_buffer);
79+
let hex = value.write_hex(HexCase::Lower, &mut hex_buffer);
8080
// SAFETY: hex holds only ASCII hex digits.
8181
unsafe { std::str::from_utf8_unchecked(hex).to_string() }
8282
}
8383

84-
/// Trait for converting integer types to hexadecimal in a buffer
85-
trait ToHex: ArrowNativeType {
86-
/// Writes the hex representation into `buf` and returns the written
87-
/// subslice. Digits are right-aligned in `buf` with leading zeros trimmed.
88-
fn write_hex(self, buf: &mut [u8; 16]) -> &[u8];
89-
}
90-
91-
/// Signed values use their two's complement representation, matching a cast to
92-
/// the corresponding unsigned type.
93-
macro_rules! impl_to_hex_signed {
94-
($ty:ty) => {
95-
impl ToHex for $ty {
96-
#[inline]
97-
fn write_hex(self, buf: &mut [u8; 16]) -> &[u8] {
98-
encode_u64(self as i64 as u64, HexCase::Lower, buf)
99-
}
100-
}
101-
};
102-
}
103-
104-
macro_rules! impl_to_hex_unsigned {
105-
($ty:ty) => {
106-
impl ToHex for $ty {
107-
#[inline]
108-
fn write_hex(self, buf: &mut [u8; 16]) -> &[u8] {
109-
encode_u64(self as u64, HexCase::Lower, buf)
110-
}
111-
}
112-
};
113-
}
114-
115-
impl_to_hex_signed!(i8);
116-
impl_to_hex_signed!(i16);
117-
impl_to_hex_signed!(i32);
118-
impl_to_hex_signed!(i64);
119-
impl_to_hex_unsigned!(u8);
120-
impl_to_hex_unsigned!(u16);
121-
impl_to_hex_unsigned!(u32);
122-
impl_to_hex_unsigned!(u64);
123-
12484
#[user_doc(
12585
doc_section(label = "String Functions"),
12686
description = "Converts an integer to a hexadecimal string.",

‎datafusion/spark/src/function/math/hex.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use arrow::{
2828
use datafusion_common::cast::as_large_binary_array;
2929
use datafusion_common::cast::as_string_view_array;
3030
use datafusion_common::types::{NativeType, logical_int64, logical_string};
31-
use datafusion_common::utils::hex::{HexCase, encode_bytes_into, encode_u64};
31+
use datafusion_common::utils::hex::{HexCase, ToHex, encode_bytes_into};
3232
use datafusion_common::utils::take_function_args;
3333
use datafusion_common::{
3434
DataFusionError,
@@ -198,7 +198,7 @@ fn hex_encode_int64(
198198
for v in iter {
199199
if let Some(num) = v {
200200
let mut temp = [0u8; 16];
201-
let slice = encode_u64(num as u64, HexCase::Upper, &mut temp);
201+
let slice = num.write_hex(HexCase::Upper, &mut temp);
202202
// SAFETY: slice contains only ASCII hex digests, which are valid UTF-8
203203
unsafe {
204204
builder.append_value(from_utf8_unchecked(slice));

0 commit comments

Comments
 (0)