Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dictionary data type in array_to_string #10908

Merged
merged 10 commits into from
Jun 23, 2024
29 changes: 27 additions & 2 deletions datafusion/functions-array/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! [`ScalarUDFImpl`] definitions for array_to_string and string_to_array functions.

use arrow::array::{
Array, ArrayRef, BooleanArray, Float32Array, Float64Array, GenericListArray,
Array, ArrayRef, AsArray, BooleanArray, Float32Array, Float64Array, GenericListArray,
Int16Array, Int32Array, Int64Array, Int8Array, LargeStringArray, ListBuilder,
OffsetSizeTrait, StringArray, StringBuilder, UInt16Array, UInt32Array, UInt64Array,
UInt8Array,
Expand All @@ -31,7 +31,7 @@ use datafusion_common::{plan_err, DataFusionError, Result};
use std::any::{type_name, Any};

use crate::utils::{downcast_arg, make_scalar_function};
use arrow_schema::DataType::{FixedSizeList, LargeList, LargeUtf8, List, Null, Utf8};
use arrow_schema::DataType::{FixedSizeList, LargeList, LargeUtf8, List, Null, Utf8, Dictionary};
use datafusion_common::cast::{
as_generic_string_array, as_large_list_array, as_list_array, as_string_array,
};
Expand Down Expand Up @@ -281,6 +281,31 @@ pub(super) fn array_to_string_inner(args: &[ArrayRef]) -> Result<ArrayRef> {

Ok(arg)
}
Dictionary(..) => {
let any_dict_array = arr
.as_any_dictionary_opt()
.ok_or_else( || {
DataFusionError::Internal(
format!("could not cast {} to AnyDictionaryArray", arr.data_type())
)
})?;
macro_rules! array_function {
($ARRAY_TYPE:ident) => {
to_string!(
arg,
any_dict_array.values(),
&delimiter,
&null_string,
with_null_string,
$ARRAY_TYPE
)
};
}
call_array_function!(
any_dict_array.values().data_type(),
false
)
EduardoVega marked this conversation as resolved.
Show resolved Hide resolved
}
Null => Ok(arg),
data_type => {
macro_rules! array_function {
Expand Down
16 changes: 16 additions & 0 deletions datafusion/sqllogictest/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -3769,6 +3769,22 @@ select array_to_string(make_array(), ',')
----
(empty)

# array to string dictionary
statement ok
CREATE TABLE table1 AS VALUES (1, arrow_cast('foo', 'Dictionary(Int32, Utf8)')), (2, arrow_cast('bar', 'Dictionary(Int32, Utf8)'));

query T
SELECT array_to_string(array_agg(column2),',') FROM (SELECT column2 FROM table1);
----
foo,bar

statement ok
CREATE TABLE table2 AS VALUES (1, arrow_cast(1, 'Dictionary(Int32, Int32)')), (2, arrow_cast(2, 'Dictionary(Int32, Int32)'));

query T
SELECT array_to_string(array_agg(column2),'-') FROM (SELECT column2 FROM table2);
----
1-2

## array_union (aliases: `list_union`)

Expand Down