Jefffrey commented on code in PR #19738:
URL: https://github.com/apache/datafusion/pull/19738#discussion_r2684604262
##########
datafusion/spark/src/function/math/hex.rs:
##########
@@ -166,103 +215,92 @@ pub fn compute_hex(
ColumnarValue::Array(array) => match array.data_type() {
DataType::Int64 => {
let array = as_int64_array(array)?;
-
- let hexed_array: StringArray =
- array.iter().map(|v| v.map(hex_int64)).collect();
-
- Ok(ColumnarValue::Array(Arc::new(hexed_array)))
+ hex_encode_int64(array.iter(), array.len())
}
DataType::Utf8 => {
let array = as_string_array(array);
-
- let hexed: StringArray = array
- .iter()
- .map(|v| v.map(|b| hex_bytes(b, lowercase)).transpose())
- .collect::<Result<_, _>>()?;
-
- Ok(ColumnarValue::Array(Arc::new(hexed)))
+ hex_encode_bytes(array.iter(), lowercase, array.len())
}
DataType::Utf8View => {
let array = as_string_view_array(array)?;
-
- let hexed: StringArray = array
- .iter()
- .map(|v| v.map(|b| hex_bytes(b, lowercase)).transpose())
- .collect::<Result<_, _>>()?;
-
- Ok(ColumnarValue::Array(Arc::new(hexed)))
+ hex_encode_bytes(array.iter(), lowercase, array.len())
}
DataType::LargeUtf8 => {
let array = as_largestring_array(array);
-
- let hexed: StringArray = array
- .iter()
- .map(|v| v.map(|b| hex_bytes(b, lowercase)).transpose())
- .collect::<Result<_, _>>()?;
-
- Ok(ColumnarValue::Array(Arc::new(hexed)))
+ hex_encode_bytes(array.iter(), lowercase, array.len())
}
DataType::Binary => {
let array = as_binary_array(array)?;
-
- let hexed: StringArray = array
- .iter()
- .map(|v| v.map(|b| hex_bytes(b, lowercase)).transpose())
- .collect::<Result<_, _>>()?;
-
- Ok(ColumnarValue::Array(Arc::new(hexed)))
+ hex_encode_bytes(array.iter(), lowercase, array.len())
}
DataType::LargeBinary => {
let array = as_large_binary_array(array)?;
-
- let hexed: StringArray = array
- .iter()
- .map(|v| v.map(|b| hex_bytes(b, lowercase)).transpose())
- .collect::<Result<_, _>>()?;
-
- Ok(ColumnarValue::Array(Arc::new(hexed)))
+ hex_encode_bytes(array.iter(), lowercase, array.len())
}
DataType::FixedSizeBinary(_) => {
let array = as_fixed_size_binary_array(array)?;
-
- let hexed: StringArray = array
- .iter()
- .map(|v| v.map(|b| hex_bytes(b, lowercase)).transpose())
- .collect::<Result<_, _>>()?;
-
- Ok(ColumnarValue::Array(Arc::new(hexed)))
+ hex_encode_bytes(array.iter(), lowercase, array.len())
}
DataType::Dictionary(_, value_type) => {
let dict = as_dictionary_array::<Int32Type>(&array);
-
- let values = match **value_type {
- DataType::Int64 => as_int64_array(dict.values())?
- .iter()
- .map(|v| v.map(hex_int64))
- .collect::<Vec<_>>(),
- DataType::Utf8 => as_string_array(dict.values())
- .iter()
- .map(|v| v.map(|b| hex_bytes(b,
lowercase)).transpose())
- .collect::<Result<_, _>>()?,
- DataType::Binary => as_binary_array(dict.values())?
- .iter()
- .map(|v| v.map(|b| hex_bytes(b,
lowercase)).transpose())
- .collect::<Result<_, _>>()?,
- _ => exec_err!(
- "hex got an unexpected argument type: {}",
- array.data_type()
- )?,
- };
-
- let new_values: Vec<Option<String>> = dict
- .keys()
- .iter()
- .map(|key| key.map(|k| values[k as
usize].clone()).unwrap_or(None))
- .collect();
-
- let string_array_values = StringArray::from(new_values);
-
- Ok(ColumnarValue::Array(Arc::new(string_array_values)))
+ let keys = dict.keys();
+ let values = dict.values();
+
+ match **value_type {
+ DataType::Int64 => {
+ let int_values = as_int64_array(values)?;
+ hex_encode_int64(
+ keys.iter().map(|k| {
+ k.and_then(|idx| {
+ if int_values.is_valid(idx as usize) {
+ Some(int_values.value(idx as usize))
+ } else {
+ None
+ }
+ })
+ }),
+ dict.len(),
+ )
Review Comment:
More compact way of iterating over dictionaries using a
[`TypedDictionaryArray`](https://docs.rs/arrow/latest/arrow/array/struct.TypedDictionaryArray.html):
```rust
let arr =
dict.downcast_dict::<arrow::array::Int64Array>().unwrap();
hex_encode_int64(arr.into_iter(), dict.len())
```
(Alternatively could look into preserving the dictionary type as an output;
so we can apply the hex only on the values and return a dict with the same
keys, instead of expanding this dict into a regular string array)
--
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]