Skip to content
Merged
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
39 changes: 36 additions & 3 deletions datafusion/functions/src/unicode/strpos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use arrow::array::{
ArrayRef, ArrowPrimitiveType, AsArray, PrimitiveArray, StringArrayType,
};
use arrow::datatypes::{ArrowNativeType, DataType, Int32Type, Int64Type};
use datafusion_common::{exec_err, Result};
use datafusion_common::{exec_err, internal_err, Result};
use datafusion_expr::{
ColumnarValue, Documentation, ScalarUDFImpl, Signature, Volatility,
};
Expand Down Expand Up @@ -79,8 +79,17 @@ impl ScalarUDFImpl for StrposFunc {
&self.signature
}

fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
utf8_to_int_type(&arg_types[0], "strpos/instr/position")
fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
internal_err!("return_type_from_args should be used instead")
}

fn return_type_from_args(
&self,
args: datafusion_expr::ReturnTypeArgs,
) -> Result<datafusion_expr::ReturnInfo> {
utf8_to_int_type(&args.arg_types[0], "strpos/instr/position").map(|data_type| {
datafusion_expr::ReturnInfo::new(data_type, args.nullables.iter().any(|x| *x))
})
}

fn invoke_with_args(
Expand Down Expand Up @@ -201,6 +210,7 @@ mod tests {
use arrow::array::{Array, Int32Array, Int64Array};
use arrow::datatypes::DataType::{Int32, Int64};

use arrow::datatypes::DataType;
use datafusion_common::{Result, ScalarValue};
use datafusion_expr::{ColumnarValue, ScalarUDFImpl};

Expand Down Expand Up @@ -288,4 +298,27 @@ mod tests {
test_strpos!("", "" -> 1; Utf8View LargeUtf8 i32 Int32 Int32Array);
test_strpos!("ДатаФусион数据融合📊🔥", "📊" -> 15; Utf8View LargeUtf8 i32 Int32 Int32Array);
}

#[test]
fn nullable_return_type() {
fn get_nullable(string_array_nullable: bool, substring_nullable: bool) -> bool {
let strpos = StrposFunc::new();
let args = datafusion_expr::ReturnTypeArgs {
arg_types: &[DataType::Utf8, DataType::Utf8],
nullables: &[string_array_nullable, substring_nullable],
scalar_arguments: &[None::<&ScalarValue>, None::<&ScalarValue>],
};

let (_, nullable) = strpos.return_type_from_args(args).unwrap().into_parts();

nullable
}

assert!(!get_nullable(false, false));

// If any of the arguments is nullable, the result is nullable
assert!(get_nullable(true, false));
assert!(get_nullable(false, true));
assert!(get_nullable(true, true));
}
}