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

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
41 changes: 29 additions & 12 deletions datafusion/functions/src/unicode/left.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@

use std::any::Any;
use std::cmp::Ordering;
use std::string;
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, TypeSignature};
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 +51,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 +83,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 +132,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
11 changes: 3 additions & 8 deletions datafusion/functions/src/unicode/right.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ 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 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
22 changes: 8 additions & 14 deletions datafusion/functions/src/unicode/rpad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ use arrow::array::{
};
use arrow::datatypes::DataType;
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,
};
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
4 changes: 4 additions & 0 deletions datafusion/sqllogictest/test_files/jctest.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
query T
SELECT left(arrow_cast('abcde', 'Dictionary(Int32, Utf8)'), -2)
----
abc
Loading