我试图使用str_detect,str_replace,str_replace_all在方法dbplyr与oracle作为beckend数据库,但似乎无法访问此方法。
这是错误:
db_tbl %>% mutate(COMMENTS_NEW = str_detect(COMMENTS,"[^[:alnum:]///' ]", "")) %>% show_query()
Run Code Online (Sandbox Code Playgroud)
Error: str_detect() is not available in this SQL variant
Run Code Online (Sandbox Code Playgroud)
我已经重新安装了所有软件包,但仍然没有用。但是,我可以看到它是在dbplyr 1.2.0see here中实现的?
尝试与grepl它转化为:
db_tbl %>% mutate(COMMENTS_NEW = grepl(COMMENTS,pattern = '[^[:alnum:]]')) %>% show_query()
Run Code Online (Sandbox Code Playgroud)
db_tbl %>% mutate(COMMENTS_NEW = grepl(COMMENTS,pattern = '[^[:alnum:]]')) %>% show_query()
Run Code Online (Sandbox Code Playgroud)
也返回错误。这是回溯:
<SQL>
Named arguments ignored for SQL greplSELECT grepl("COMMENTS", '[^[:alnum:]]' AS "pattern") AS "COMMENTS_NEW"
FROM ("schema".table)
Run Code Online (Sandbox Code Playgroud)
继承人我的会议:
20.
stop(structure(list(message = "<SQL> 'SELECT * FROM (SELECT \"COMMENTS\", \"TYPE_28\", grepl(\"COMMENTS\", '[^[:alnum:]]' AS \"pattern\") AS \"COMMENTS_NEW\"\nFROM (\"schema\".table) ) \"zzz3\" WHERE ROWNUM <= 6.0'\n nanodbc/nanodbc.cpp:1587: HY000: [Oracle][ODBC][Ora]ORA-00907: missing right parenthesis\n ", call = NULL, cppstack = NULL), class = c("odbc::odbc_error", "C++Error", "error", "condition")))
19.
new_result(connection@ptr, statement)
18.
OdbcResult(connection = conn, statement = statement)
17.
dbSendQuery(con, sql)
16.
dbSendQuery(con, sql)
15.
db_collect.DBIConnection(x$src$con, sql, n = n, warn_incomplete = warn_incomplete)
14.
db_collect(x$src$con, sql, n = n, warn_incomplete = warn_incomplete)
13.
collect.tbl_sql(x, n = n)
12.
collect(x, n = n)
11.
as.data.frame(collect(x, n = n))
10.
as.data.frame.tbl_sql(head(x, n + 1))
9.
as.data.frame(head(x, n + 1))
8.
trunc_mat(x, n = n, width = width, n_extra = n_extra)
7.
format.tbl(x, ..., n = n, width = width, n_extra = n_extra)
6.
format(x, ..., n = n, width = width, n_extra = n_extra)
5.
paste0(..., "\n")
4.
cat(paste0(..., "\n"), sep = "")
3.
cat_line(format(x, ..., n = n, width = width, n_extra = n_extra))
2.
print.tbl_sql(x)
1.
function (x, ...) UseMethod("print")(x)
Run Code Online (Sandbox Code Playgroud)
这并不是真正的答案,而只是一个简单的解决方法。
问题是dbplyr::无法创建足够的 SQL 子句(SQL 没有名称为str_detect或 的函数grepl),因此它会放弃(并出现错误)。
在这两个表达式中,您都会收到错误,因为dbplyr cannot translate neitherstringr::str_detect() norbase::grepl() to a valid SQL expression.
One way to get almost what you want is tocollect() before youfilter()`:
db_tbl %>%
mutate(COMMENTS_NEW = str_detect(COMMENTS,"[^[:alnum:]///' ]", "")) %>%
show_query()
db_tbl %>%
mutate(COMMENTS_NEW = str_detect(COMMENTS,"[^[:alnum:]///' ]", "")) %>%
collect()
db_tbl %>%
mutate(COMMENTS_NEW = grepl(COMMENTS,pattern = '[^[:alnum:]]')) %>%
show_query()
db_tbl %>%
mutate(COMMENTS_NEW = grepl(COMMENTS,pattern = '[^[:alnum:]]')) %>%
collect()
Run Code Online (Sandbox Code Playgroud)
但是,如果您collect()之前放置...
db_tbl %>%
collect() %>%
mutate(COMMENTS_NEW = str_detect(COMMENTS,"[^[:alnum:]///' ]", ""))
db_tbl %>%
collect() %>%
mutate(COMMENTS_NEW = grepl(COMMENTS,pattern = '[^[:alnum:]]'))
Run Code Online (Sandbox Code Playgroud)
您的远程表变成本地表,您可以在上面str_detect()放心地应用。
作为旁注,show_query()由于显而易见的原因,它不再有意义。