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

DRAFT: example fix to make stride optional in array_slice UDF #10450

Closed
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
49 changes: 42 additions & 7 deletions datafusion/functions-array/src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,48 @@ make_udf_function!(
array_element_udf
);

make_udf_function!(
ArraySlice,
array_slice,
array begin end stride,
"returns a slice of the array.",
array_slice_udf
);
// make_udf_function!(
// ArraySlice,
// array_slice,
// array begin end stride,
// "returns a slice of the array.",
// array_slice_udf
// );


#[doc = "returns a slice of the array."]
pub fn array_slice(array: Expr, begin: Expr, end: Expr, stride: Option<Expr>) -> Expr {
if let Some(stride) = stride {
Expr::ScalarFunction(ScalarFunction::new_udf(
array_slice_udf(),
vec![array, begin, end, stride],
))
} else {
Expr::ScalarFunction(ScalarFunction::new_udf(
array_slice_udf(),
vec![array, begin, end],
))
}
}

#[doc = r" Singleton instance of [`$UDF`], ensures the UDF is only created once"]
#[doc = r" named STATIC_$(UDF). For example `STATIC_ArrayToString`"]
#[allow(non_upper_case_globals)]
static STATIC_ArraySlice: std::sync::OnceLock<
std::sync::Arc<datafusion_expr::ScalarUDF>,
> = std::sync::OnceLock::new();
#[doc = r" ScalarFunction that returns a [`ScalarUDF`] for [`$UDF`]"]
#[doc = r""]
#[doc = r" [`ScalarUDF`]: datafusion_expr::ScalarUDF"]
pub fn array_slice_udf() -> std::sync::Arc<datafusion_expr::ScalarUDF> {
STATIC_ArraySlice
.get_or_init(|| {
std::sync::Arc::new(datafusion_expr::ScalarUDF::new_from_impl(
<ArraySlice>::new(),
))
})
.clone()
}

make_udf_function!(
ArrayPopFront,
Expand Down
2 changes: 1 addition & 1 deletion datafusion/functions-array/src/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl FunctionRewrite for ArrayFunctionRewriter {
stop,
stride,
},
}) => Transformed::yes(array_slice(*expr, *start, *stop, *stride)),
}) => Transformed::yes(array_slice(*expr, *start, *stop, Some(*stride))),

_ => Transformed::no(expr),
};
Expand Down
2 changes: 1 addition & 1 deletion datafusion/proto/tests/cases/roundtrip_logical_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ async fn roundtrip_expr_api() -> Result<()> {
make_array(vec![lit(1), lit(2), lit(3)]),
lit(1),
lit(2),
lit(1),
Some(lit(1)),
),
array_pop_front(make_array(vec![lit(1), lit(2), lit(3)])),
array_pop_back(make_array(vec![lit(1), lit(2), lit(3)])),
Expand Down