dragosmg commented on code in PR #13789:
URL: https://github.com/apache/arrow/pull/13789#discussion_r946529125
##########
r/R/dplyr-eval.R:
##########
@@ -121,3 +148,121 @@ format_expr <- function(x) {
}
head(out, 1)
}
+
+#' Translate a function in a given context (environment with bindings)
+#'
+#' Translates a function (if possible) with the help of existing bindings in a
+#' given environment.
+#'
+#' @param fn function to be translated
+#' @param env environment to translate against
+#'
+#' @return a translated function
+#' @keywords internal
+#' @noRd
+translate_to_arrow <- function(.fun, .env) {
+ # get the args and the body of the unknown binding
+ function_formals <- formals(.fun)
+
+ if (is.primitive(.fun)) {
+ # exit as the function can't be translated, we can only translate closures
+ stop("`", as.character(.fun[[1]]), "` is a primitive and cannot be
translated")
+ }
+
+ # TODO handle errors. `fn_body()` errors when fn is a primitive, `body()`
returns
+ # NULL so maybe we can work with that
+ function_body <- rlang::fn_body(.fun)
+
+ if (translatable(.fun, .env)) {
+ translated_function <- rlang::new_function(
+ args = function_formals,
+ body = translate_to_arrow_rec(function_body[[2]])
+ )
+ } else {
+ # TODO WIP if the function is not directly translatable, make one more try
+ # by attempting to translate the unknown calls
+ unknown_function <- setdiff(all_funs(function_body[[2]]), names(.env))
+
+ fn <- as_function(unknown_function, env = caller_env())
+ translated_function <- NULL
+ }
Review Comment:
It doesn't recurse through the call stack at the moment. Still working on it
and I will implement it as recursing only 1 level down in the first instance.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]