Skip to content
Open
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
26 changes: 25 additions & 1 deletion r/R/udf.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
#' for functions with more than one argument. This signature will be used
#' to determine if this function is appropriate for a given set of arguments.
#' If this function is appropriate for more than one signature, pass a
#' `list()` of the above.
#' `list()` of the above. Arguments are passed to `fun` by position, so if
#' the schema (or field) is named, the names must match the argument names
#' of `fun` (after `context`).
#' @param out_type A [DataType] of the output type or a function accepting
#' a single argument (`types`), which is a `list()` of [DataType]s. If a
#' function it must return a [DataType].
Expand Down Expand Up @@ -157,6 +159,28 @@ arrow_scalar_function <- function(fun, in_type, out_type, auto_convert = FALSE)
)
}

# Arguments are passed to fun by position, so if the user named the
# fields in in_type, make sure those names line up with fun's arguments
# rather than silently ignoring them (GH-37761)
if (!fun_formals_have_dots) {
fun_arg_names <- names(formals(fun))[-1]
in_type_names <- lapply(in_type, names)
mismatch <- vapply(
in_type_names,
function(nms) {
isTRUE(any(nzchar(nms) & nms != fun_arg_names[seq_along(nms)]))
},
logical(1)
)
if (any(mismatch)) {
abort(c(
"Names in `in_type` must match the argument names of `fun` (after `context`)",
x = paste0("`in_type` names: ", oxford_paste(unlist(in_type_names))),
x = paste0("`fun` argument names: ", oxford_paste(fun_arg_names))
))
}
}

structure(
list(
wrapper_fun = wrapper_fun,
Expand Down
4 changes: 3 additions & 1 deletion r/man/register_scalar_function.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions r/tests/testthat/_snaps/udf.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@

fun is not a function

# arrow_scalar_function() checks in_type names against fun arguments

Names in `in_type` must match the argument names of `fun` (after `context`)
x `in_type` names: "blah" and "aj"
x `fun` argument names: "x" and "y"

55 changes: 55 additions & 0 deletions r/tests/testthat/test-udf.R
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,58 @@ test_that("head() on exec plan containing user-defined functions", {

expect_equal(nrow(result), 11)
})

test_that("arrow_scalar_function() checks in_type names against fun arguments", {
# named schema with a different name than the argument
expect_snapshot_error(
arrow_scalar_function(
function(context, x, y) x,
schema(blah = int64(), aj = int64()),
int32()
)
)

# named field with a different name than the argument
expect_error(
arrow_scalar_function(
function(context, x) x,
field("blah", int64()),
int32()
),
"must match the argument names"
)

# partial mismatch across multiple arguments
expect_error(
arrow_scalar_function(
function(context, x, y) x,
schema(x = int32(), b = int32()),
int32()
),
"must match the argument names"
)

# mismatch in a later kernel when registering several at once
expect_error(
arrow_scalar_function(
function(context, x) x,
list(schema(x = int32()), schema(y = int32())),
int32()
),
"must match the argument names"
)

# matching names, unnamed types, and `...` are all still accepted
expect_s3_class(
arrow_scalar_function(function(context, x) x, schema(x = int32()), int32()),
"arrow_scalar_function"
)
expect_s3_class(
arrow_scalar_function(function(context, anything) anything, int32(), int32()),
"arrow_scalar_function"
)
expect_s3_class(
arrow_scalar_function(function(...) NULL, schema(blah = int32()), int32()),
"arrow_scalar_function"
)
})
Loading