TL;DR Because your code doesn't use dplyr::if_else
at all.
sparklyr
, when used as in the example, treats Spark as yet another database and issues queries using dbplyr
SQL translation layer.
In this context if_else
is no treated as a function, but an identifier which is converted to SQL primitives:
dbplyr::translate_sql(if_else(PaymentHistory < 0, 0,if_else(PaymentHistory > 1,1, PaymentHistory)))
# <SQL> CASE WHEN ("PaymentHistory" < 0.0) THEN (0.0) WHEN NOT("PaymentHistory" < 0.0) THEN (CASE WHEN ("PaymentHistory" > 1.0) THEN (1.0) WHEN NOT("PaymentHistory" > 1.0) THEN ("PaymentHistory") END) END
However if you pass a fully qualified named, it will circumvent this mechanism, try to evaluate the function, and ultimately fail, because the database columns are not in the scope.
I'm afraid that the name in dplyr will clash with some of the user-defined code.
As you see, there is no need for dplyr to be in scope here at all - functions called in sparklyr
pipelines are either translated to corresponding SQL constructs, or if there is no specific translation rule in place, passed as-is and resolved by Spark SQL engine (this path is used to invoke Spark functions).
Of course this mechanism is not specific to sparklyr
and you're likely to see the same behavior using other table backed by a database:
library(magrittr)
db <- dplyr::src_sqlite(":memory:", TRUE)
dplyr::copy_to(db, mtcars)
db %>% dplyr::tbl("mtcars") %>% dplyr::mutate(dplyr::if_else(mpg < 20, 1, 0))
Error in dplyr::if_else(mpg < 20, 1, 0) : object 'mpg' not found
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…