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

fix: array_slice and array_element panicked on empty args #10804

Merged
merged 2 commits into from
Jun 5, 2024
Merged
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
19 changes: 14 additions & 5 deletions datafusion/functions-array/src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility};
use std::any::Any;
use std::sync::Arc;

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

// Create static instances of ScalarUDFs for each function
make_udf_expr_and_func!(
Expand Down Expand Up @@ -97,8 +97,11 @@ impl ScalarUDFImpl for ArrayElement {
}

fn display_name(&self, args: &[Expr]) -> Result<String> {
let args_name: Vec<String> = args.iter().map(|e| e.to_string()).collect();
Ok(format!("{}[{}]", args_name[0], args_name[1]))
Ok(format!(
Copy link
Contributor

Choose a reason for hiding this comment

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

that is a nice drive by cleanup

"{}[{}]",
get_arg_name(args, 0),
get_arg_name(args, 1)
))
}

fn signature(&self) -> &Signature {
Expand Down Expand Up @@ -251,8 +254,14 @@ impl ScalarUDFImpl for ArraySlice {
}

fn display_name(&self, args: &[Expr]) -> Result<String> {
let args_name: Vec<String> = args.iter().map(|e| e.to_string()).collect();
Ok(format!("{}[{}]", args_name[0], args_name[1..].join(":")))
Ok(format!(
"{}[{}]",
get_arg_name(args, 0),
(1..args.len())
.map(|i| get_arg_name(args, i))
.collect::<Vec<String>>()
.join(":")
))
}

fn name(&self) -> &str {
Expand Down
7 changes: 6 additions & 1 deletion datafusion/functions-array/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use datafusion_common::{exec_err, plan_err, Result, ScalarValue};

use core::any::type_name;
use datafusion_common::DataFusionError;
use datafusion_expr::{ColumnarValue, ScalarFunctionImplementation};
use datafusion_expr::{ColumnarValue, Expr, ScalarFunctionImplementation};

macro_rules! downcast_arg {
($ARG:expr, $ARRAY_TYPE:ident) => {{
Expand Down Expand Up @@ -253,6 +253,11 @@ pub(crate) fn compute_array_dims(
}
}

/// Returns the name of the argument at index `i`, or an empty string if the index is out of bounds.
pub(super) fn get_arg_name(args: &[Expr], i: usize) -> String {
args.get(i).map(ToString::to_string).unwrap_or_default()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 8 additions & 0 deletions datafusion/sqllogictest/test_files/array.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,10 @@ from arrays_values_without_nulls;

## array_element (aliases: array_extract, list_extract, list_element)

# Testing with empty arguments should result in an error
query error DataFusion error: Error during planning: Error during planning: \[data_types_with_scalar_udf\] signature ArraySignature\(ArrayAndIndex\) does not support zero arguments.
select array_element();

# array_element error
query error
select array_element(1, 2);
Expand Down Expand Up @@ -1974,6 +1978,10 @@ select array_slice(a, -1, 2, 1), array_slice(a, -1, 2),
[] [] [] []
[6.0] [6.0] [] []

# Testing with empty arguments should result in an error
query error DataFusion error: Error during planning: Error during planning: \[data_types_with_scalar_udf\] signature VariadicAny does not support zero arguments.
Copy link
Contributor

Choose a reason for hiding this comment

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

It is handled by signature!

select array_slice();


# make_array with nulls
query ???????
Expand Down