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

Add native stringview support for RIGHT #11955

Merged
merged 6 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
65 changes: 58 additions & 7 deletions datafusion/functions/src/unicode/right.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ use std::any::Any;
use std::cmp::{max, Ordering};
use std::sync::Arc;

use arrow::array::{ArrayRef, GenericStringArray, OffsetSizeTrait};
use arrow::array::{Array, ArrayRef, GenericStringArray, OffsetSizeTrait};
use arrow::datatypes::DataType;

use datafusion_common::cast::{as_generic_string_array, as_int64_array};
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::Result;
use datafusion_expr::TypeSignature::Exact;
use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility};

use crate::utils::{make_scalar_function, utf8_to_str_type};

#[derive(Debug)]
pub struct RightFunc {
signature: Signature,
Expand All @@ -46,7 +47,11 @@ impl RightFunc {
use DataType::*;
Self {
signature: Signature::one_of(
vec![Exact(vec![Utf8, Int64]), Exact(vec![LargeUtf8, Int64])],
vec![
Exact(vec![Utf8View, Int64]),
Exact(vec![Utf8, Int64]),
Exact(vec![LargeUtf8, Int64]),
],
Volatility::Immutable,
),
}
Expand All @@ -72,9 +77,14 @@ impl ScalarUDFImpl for RightFunc {

fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
match args[0].data_type() {
DataType::Utf8 => make_scalar_function(right::<i32>, vec![])(args),
DataType::Utf8 | DataType::Utf8View => {
make_scalar_function(right::<i32>, vec![])(args)
}
DataType::LargeUtf8 => make_scalar_function(right::<i64>, vec![])(args),
other => exec_err!("Unsupported data type {other:?} for function right"),
other => exec_err!(
"Unsupported data type {other:?} for function right,\
expected Utf8View, Utf8 or LargeUtf8."
),
}
}
}
Expand All @@ -83,6 +93,47 @@ impl ScalarUDFImpl for RightFunc {
/// right('abcde', 2) = 'de'
/// The implementation uses UTF-8 code points as characters
pub fn right<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
if args[0].data_type() == &DataType::Utf8View {
string_view_right(args)
} else {
string_right::<T>(args)
}
}

// Currently the return type can only be Utf8 or LargeUtf8, to reach fully support, we need
// to edit the `get_optimal_return_type` in utils.rs to make the udfs be able to return Utf8View
// See https://github.com/apache/datafusion/issues/11790#issuecomment-2283777166
fn string_view_right(args: &[ArrayRef]) -> Result<ArrayRef> {
let string_array = as_string_view_array(&args[0])?;
let n_array = as_int64_array(&args[1])?;

let result = string_array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the advice. I couldn't come up with a proper way to combine them but now I get it :)

.iter()
.zip(n_array.iter())
.map(|(string, n)| match (string, n) {
(Some(string), Some(n)) => match n.cmp(&0) {
Ordering::Less => Some(
string
.chars()
.skip(n.unsigned_abs() as usize)
.collect::<String>(),
),
Ordering::Equal => Some("".to_string()),
Ordering::Greater => Some(
string
.chars()
.skip(max(string.chars().count() as i64 - n, 0) as usize)
.collect::<String>(),
),
},
_ => None,
})
.collect::<GenericStringArray<i32>>(); // the return type of Utf8View is currently Utf8

Ok(Arc::new(result) as ArrayRef)
}

fn string_right<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
let string_array = as_generic_string_array::<T>(&args[0])?;
let n_array = as_int64_array(&args[1])?;

Expand Down
16 changes: 14 additions & 2 deletions datafusion/sqllogictest/test_files/string_view.slt
Original file line number Diff line number Diff line change
Expand Up @@ -809,16 +809,28 @@ logical_plan
03)----TableScan: test projection=[column1_utf8view]

## Ensure no casts for RIGHT
## TODO file ticket
query TT
EXPLAIN SELECT
RIGHT(column1_utf8view, 3) as c2
FROM test;
----
logical_plan
01)Projection: right(CAST(test.column1_utf8view AS Utf8), Int64(3)) AS c2
01)Projection: right(test.column1_utf8view, Int64(3)) AS c2
02)--TableScan: test projection=[column1_utf8view]

# Test outputs of RIGHT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

query TTT
SELECT
RIGHT(column1_utf8view, 3) as c1,
RIGHT(column1_utf8view, 0) as c2,
RIGHT(column1_utf8view, -3) as c3
FROM test;
----
rew (empty) rew
eng (empty) ngpeng
ael (empty) hael
NULL NULL NULL

## Ensure no casts for RPAD
## TODO file ticket
query TT
Expand Down