Skip to content

Refactor signatures for lpad, rpad, left, and right #13420

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions datafusion/expr/src/type_coercion/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,12 @@ fn get_valid_types(
let logical_type: NativeType = current_type.into();
let target_logical_type = target_type.native();
if can_coerce_to(&logical_type, target_logical_type) {
let target_type =
target_logical_type.default_cast_for(current_type)?;
let target_type = if &logical_type == target_logical_type {
current_type.clone()
} else {
target_logical_type.default_cast_for(current_type)?
};
// let target_type = target_logical_type.default_cast_for(current_type)?;
new_types.push(target_type);
}
}
Expand Down
20 changes: 18 additions & 2 deletions datafusion/functions/src/string/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use arrow::array::{
ArrayRef, AsArray, GenericStringArray, GenericStringBuilder, Int64Array,
OffsetSizeTrait, StringViewArray,
};
use arrow::datatypes::DataType;
use arrow::datatypes::DataType::{LargeUtf8, Utf8, Utf8View};
use arrow::datatypes::DataType::{Dictionary, LargeUtf8, Utf8, Utf8View};
use arrow::datatypes::{DataType, Int32Type};
use datafusion_common::cast::as_int64_array;
use datafusion_common::types::{logical_int64, logical_string};
use datafusion_common::{exec_err, Result};
Expand Down Expand Up @@ -125,6 +125,22 @@ fn repeat(args: &[ArrayRef]) -> Result<ArrayRef> {
let string_array = args[0].as_string::<i64>();
repeat_impl::<i64, &GenericStringArray<i64>>(string_array, number_array)
}
Dictionary(k, v) => match &**v {
Utf8 => {
let dict_array = match &**k {
DataType::Int32 => Ok(args[0].as_dictionary::<Int32Type>()),
_ => exec_err!("Unsupported Dictionary key type {k:?}"),
}
.unwrap();
let string_array = dict_array
.values()
.as_any()
.downcast_ref::<GenericStringArray<i32>>()
.unwrap();
repeat_impl::<i32, &GenericStringArray<i32>>(string_array, number_array)
}
_ => exec_err!("Unsupported type {v:?}"),
},
other => exec_err!(
"Unsupported data type {other:?} for function repeat. \
Expected Utf8, Utf8View or LargeUtf8."
Expand Down
40 changes: 28 additions & 12 deletions datafusion/functions/src/unicode/left.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ use std::cmp::Ordering;
use std::sync::{Arc, OnceLock};

use arrow::array::{
Array, ArrayAccessor, ArrayIter, ArrayRef, GenericStringArray, Int64Array,
OffsetSizeTrait,
as_dictionary_array, Array, ArrayAccessor, ArrayIter, ArrayRef, GenericStringArray,
Int64Array, OffsetSizeTrait,
};
use arrow::datatypes::DataType;
use arrow::datatypes::{DataType, Int32Type};

use crate::utils::{make_scalar_function, utf8_to_str_type};
use datafusion_common::cast::{
as_generic_string_array, as_int64_array, as_string_view_array,
};
use datafusion_common::exec_err;
use datafusion_common::types::{logical_int64, logical_string};
use datafusion_common::Result;
use datafusion_expr::scalar_doc_sections::DOC_SECTION_STRING;
use datafusion_expr::TypeSignature::Exact;
use datafusion_expr::{
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
};
Expand All @@ -50,14 +50,9 @@ impl Default for LeftFunc {

impl LeftFunc {
pub fn new() -> Self {
use DataType::*;
Self {
signature: Signature::one_of(
vec![
Exact(vec![Utf8View, Int64]),
Exact(vec![Utf8, Int64]),
Exact(vec![LargeUtf8, Int64]),
],
signature: Signature::coercible(
vec![logical_string(), logical_int64()],
Volatility::Immutable,
),
}
Expand Down Expand Up @@ -87,6 +82,10 @@ impl ScalarUDFImpl for LeftFunc {
make_scalar_function(left::<i32>, vec![])(args)
}
DataType::LargeUtf8 => make_scalar_function(left::<i64>, vec![])(args),
DataType::Dictionary(_, v) => match &*v {
DataType::Utf8 => make_scalar_function(left::<i32>, vec![])(args),
_ => exec_err!("Unsupported data type {v:?}"),
},
other => exec_err!(
"Unsupported data type {other:?} for function left,\
expected Utf8View, Utf8 or LargeUtf8."
Expand Down Expand Up @@ -132,9 +131,26 @@ pub fn left<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
if args[0].data_type() == &DataType::Utf8View {
let string_array = as_string_view_array(&args[0])?;
left_impl::<T, _>(string_array, n_array)
} else {
} else if args[0].data_type() == &DataType::Utf8
|| args[0].data_type() == &DataType::LargeUtf8
{
let string_array = as_generic_string_array::<T>(&args[0])?;
left_impl::<T, _>(string_array, n_array)
} else {
let dict_array = match &args[0].data_type() {
DataType::Dictionary(k, _) => match **k {
DataType::Int32 => Ok(as_dictionary_array::<Int32Type>(&args[0])),
_ => exec_err!("Unsupported Dictionary key type {k:?}"),
},
_ => exec_err!("Unsupported type {:?}", &args[0].data_type()),
}
.unwrap();
let string_array = dict_array
.values()
.as_any()
.downcast_ref::<GenericStringArray<T>>()
.unwrap();
left_impl::<T, _>(string_array, n_array)
}
}

Expand Down
22 changes: 8 additions & 14 deletions datafusion/functions/src/unicode/lpad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use arrow::array::{
OffsetSizeTrait, StringViewArray,
};
use arrow::datatypes::DataType;
use datafusion_common::types::{logical_int64, logical_string};
use unicode_segmentation::UnicodeSegmentation;
use DataType::{LargeUtf8, Utf8, Utf8View};

Expand All @@ -32,7 +33,7 @@ use crate::utils::{make_scalar_function, utf8_to_str_type};
use datafusion_common::cast::as_int64_array;
use datafusion_common::{exec_err, Result};
use datafusion_expr::scalar_doc_sections::DOC_SECTION_STRING;
use datafusion_expr::TypeSignature::Exact;
use datafusion_expr::TypeSignature;
use datafusion_expr::{
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
};
Expand All @@ -50,22 +51,15 @@ impl Default for LPadFunc {

impl LPadFunc {
pub fn new() -> Self {
use DataType::*;
Self {
signature: Signature::one_of(
vec![
Exact(vec![Utf8View, Int64]),
Exact(vec![Utf8View, Int64, Utf8View]),
Exact(vec![Utf8View, Int64, Utf8]),
Exact(vec![Utf8View, Int64, LargeUtf8]),
Exact(vec![Utf8, Int64]),
Exact(vec![Utf8, Int64, Utf8View]),
Exact(vec![Utf8, Int64, Utf8]),
Exact(vec![Utf8, Int64, LargeUtf8]),
Exact(vec![LargeUtf8, Int64]),
Exact(vec![LargeUtf8, Int64, Utf8View]),
Exact(vec![LargeUtf8, Int64, Utf8]),
Exact(vec![LargeUtf8, Int64, LargeUtf8]),
TypeSignature::Coercible(vec![logical_string(), logical_int64()]),
TypeSignature::Coercible(vec![
logical_string(),
logical_int64(),
logical_string(),
]),
],
Volatility::Immutable,
),
Expand Down
39 changes: 27 additions & 12 deletions datafusion/functions/src/unicode/right.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@ use std::cmp::{max, Ordering};
use std::sync::{Arc, OnceLock};

use arrow::array::{
Array, ArrayAccessor, ArrayIter, ArrayRef, GenericStringArray, Int64Array,
OffsetSizeTrait,
as_dictionary_array, Array, ArrayAccessor, ArrayIter, ArrayRef, AsArray, GenericStringArray, Int64Array, OffsetSizeTrait
};
use arrow::datatypes::DataType;
use arrow::datatypes::{DataType, Int32Type};

use crate::utils::{make_scalar_function, utf8_to_str_type};
use datafusion_common::cast::{
as_generic_string_array, as_int64_array, as_string_view_array,
};
use datafusion_common::exec_err;
use datafusion_common::types::{logical_int64, logical_string};
use datafusion_common::Result;
use datafusion_expr::scalar_doc_sections::DOC_SECTION_STRING;
use datafusion_expr::TypeSignature::Exact;
use datafusion_expr::{
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
};
Expand All @@ -50,14 +49,9 @@ impl Default for RightFunc {

impl RightFunc {
pub fn new() -> Self {
use DataType::*;
Self {
signature: Signature::one_of(
vec![
Exact(vec![Utf8View, Int64]),
Exact(vec![Utf8, Int64]),
Exact(vec![LargeUtf8, Int64]),
],
signature: Signature::coercible(
vec![logical_string(), logical_int64()],
Volatility::Immutable,
),
}
Expand Down Expand Up @@ -87,6 +81,10 @@ impl ScalarUDFImpl for RightFunc {
make_scalar_function(right::<i32>, vec![])(args)
}
DataType::LargeUtf8 => make_scalar_function(right::<i64>, vec![])(args),
DataType::Dictionary(_, v) => match &*v {
DataType::Utf8 => make_scalar_function(right::<i32>, vec![])(args),
_ => exec_err!("Unsupported data type {v:?}"),
},
other => exec_err!(
"Unsupported data type {other:?} for function right,\
expected Utf8View, Utf8 or LargeUtf8."
Expand Down Expand Up @@ -132,10 +130,27 @@ pub fn right<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
// string_view_right(args)
let string_array = as_string_view_array(&args[0])?;
right_impl::<T, _>(&mut string_array.iter(), n_array)
} else {
} else if args[0].data_type() == &DataType::Utf8
|| args[0].data_type() == &DataType::LargeUtf8
{
// string_right::<T>(args)
let string_array = &as_generic_string_array::<T>(&args[0])?;
right_impl::<T, _>(&mut string_array.iter(), n_array)
} else {
let dict_array = match &args[0].data_type() {
DataType::Dictionary(k, _) => match **k {
DataType::Int32 => Ok(as_dictionary_array::<Int32Type>(&args[0])),
_ => exec_err!("Unsupported Dictionary key type {k:?}"),
},
_ => exec_err!("Unsupported type {:?}", &args[0].data_type()),
}
.unwrap();
let string_array = dict_array
.values()
.as_any()
.downcast_ref::<GenericStringArray<T>>()
.unwrap();
right_impl::<T, _>(&mut string_array.iter(), n_array)
}
}

Expand Down
67 changes: 51 additions & 16 deletions datafusion/functions/src/unicode/rpad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ use arrow::array::{
ArrayRef, AsArray, GenericStringArray, GenericStringBuilder, Int64Array,
OffsetSizeTrait, StringViewArray,
};
use arrow::datatypes::DataType;
use arrow::datatypes::{DataType, Int32Type};
use datafusion_common::cast::as_int64_array;
use datafusion_common::types::{logical_int64, logical_string};
use datafusion_common::DataFusionError;
use datafusion_common::{exec_err, Result};
use datafusion_expr::scalar_doc_sections::DOC_SECTION_STRING;
use datafusion_expr::TypeSignature::Exact;
use datafusion_expr::TypeSignature;
use datafusion_expr::{
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
};
use std::any::Any;
use std::fmt::Write;
use std::sync::{Arc, OnceLock};
use unicode_segmentation::UnicodeSegmentation;
use DataType::{LargeUtf8, Utf8, Utf8View};
use DataType::{Dictionary, LargeUtf8, Utf8, Utf8View};

#[derive(Debug)]
pub struct RPadFunc {
Expand All @@ -49,22 +50,15 @@ impl Default for RPadFunc {

impl RPadFunc {
pub fn new() -> Self {
use DataType::*;
Self {
signature: Signature::one_of(
vec![
Exact(vec![Utf8View, Int64]),
Exact(vec![Utf8View, Int64, Utf8View]),
Exact(vec![Utf8View, Int64, Utf8]),
Exact(vec![Utf8View, Int64, LargeUtf8]),
Exact(vec![Utf8, Int64]),
Exact(vec![Utf8, Int64, Utf8View]),
Exact(vec![Utf8, Int64, Utf8]),
Exact(vec![Utf8, Int64, LargeUtf8]),
Exact(vec![LargeUtf8, Int64]),
Exact(vec![LargeUtf8, Int64, Utf8View]),
Exact(vec![LargeUtf8, Int64, Utf8]),
Exact(vec![LargeUtf8, Int64, LargeUtf8]),
TypeSignature::Coercible(vec![logical_string(), logical_int64()]),
TypeSignature::Coercible(vec![
logical_string(),
logical_int64(),
logical_string(),
]),
],
Volatility::Immutable,
),
Expand Down Expand Up @@ -111,6 +105,14 @@ impl ScalarUDFImpl for RPadFunc {
(3, LargeUtf8, Some(Utf8 | Utf8View)) => {
make_scalar_function(rpad::<i64, i32>, vec![])(args)
}
(3, Dictionary(_, v), Some(Utf8 | Utf8View)) => match &*v {
Utf8 => make_scalar_function(rpad::<i32, i32>, vec![])(args),
_ => exec_err!("Unsupported data type {v:?}"),
},
(3, Dictionary(_, v), Some(LargeUtf8)) => match &*v {
Utf8 => make_scalar_function(rpad::<i32, i64>, vec![])(args),
_ => exec_err!("Unsupported data type {v:?}"),
},
(_, _, _) => {
exec_err!("Unsupported combination of data types for function rpad")
}
Expand Down Expand Up @@ -197,6 +199,39 @@ pub fn rpad<StringArrayLen: OffsetSizeTrait, FillArrayLen: OffsetSizeTrait>(
length_array,
Some(args[2].as_string_view()),
),
(3, Dictionary(k, _), pad_str) => {
let dict_array = match &**k {
DataType::Int32 => Ok(args[0].as_dictionary::<Int32Type>()),
_ => exec_err!("Unsupported Dictionary key type {k:?}"),
}
.unwrap();
let string_array = dict_array
.values()
.as_any()
.downcast_ref::<GenericStringArray<StringArrayLen>>()
.unwrap();
match pad_str {
Some(Utf8 | LargeUtf8) => rpad_impl::<
&GenericStringArray<StringArrayLen>,
&GenericStringArray<StringArrayLen>,
StringArrayLen,
>(
string_array,
length_array,
Some(args[2].as_string::<StringArrayLen>()),
),
Some(Utf8View) => {
rpad_impl::<
&GenericStringArray<StringArrayLen>,
&StringViewArray,
StringArrayLen,
>(
string_array, length_array, Some(args[2].as_string_view())
)
}
_ => exec_err!("Unsupported type {pad_str:?}"),
}
}
(_, _, _) => rpad_impl::<
&GenericStringArray<StringArrayLen>,
&GenericStringArray<FillArrayLen>,
Expand Down
Loading
Loading