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

Make RPresto compatible with new Presto JSON data format and dbplyr 2.5.0 #280

Merged
merged 5 commits into from
Dec 12, 2024
Merged
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Fixed a bug whereby nested CTEs result in nested WITH. (#261)
* Fixed `paste()` and `paste0()` translation (#266)
* RPresto is now compatible with dbplyr 2.5.0 (#272, #274, #277)

# RPresto 1.4.6

Expand Down
4 changes: 3 additions & 1 deletion R/cte.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ get_tables_from_sql.lazy_select_query <- function(query) {
#' @export
get_tables_from_sql.lazy_base_remote_query <- function(query) {
if (inherits(query$x, "dbplyr_table_path")) { # dbplyr >= 2.5.0
utils::getFromNamespace("table_path_name", "dbplyr")(query$x)
sim_conn <- dbplyr::simulate_dbi("PrestoConnection")
table_name <- dbplyr::table_path_name(query$x, sim_conn)
DBI::dbUnquoteIdentifier(sim_conn, table_name)[[1]]@name
} else if (inherits(query$x, "dbplyr_table_ident")) { # dbplyr >= 2.4.0
vctrs::field(query$x, "table")
} else {
Expand Down
4 changes: 2 additions & 2 deletions R/dbExistsTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ setMethod(
FROM information_schema.columns
WHERE
table_catalog = '", conn@catalog, "' AND
table_schema = '", ifelse(!"schema" %in% names(table_name), conn@schema, table_name["schema"]), "' AND
table_name = '", table_name["table"], "'
table_schema = '", ifelse(length(table_name) == 2L, table_name[1], conn@schema), "' AND
table_name = '", table_name[length(table_name)], "'
")
)
return((res$n > 0))
Expand Down
27 changes: 27 additions & 0 deletions R/presto.field.R
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@ create.empty.tibble <- function(schema) {
return(tibble::as_tibble(rv))
}

.fromJSON.if.any <- function(x, .start) {
if (is.character(x) && length(x) == 1 && any(startsWith(x, .start))) {
jsonlite::fromJSON(x, simplifyVector = FALSE)
} else {
x
}
}

# data contains all rows of data in a list
organize_simple_type_data <- function(data, prf, keep_names) {
# primitive (simple) types -> scalars
Expand All @@ -302,6 +310,7 @@ organize_simple_type_data <- function(data, prf, keep_names) {
}
# array of primitive (simple) types -> vectors
if (prf$is_array_) {
data <- purrr::map(data, ~ .fromJSON.if.any(.x, "["))
data <- purrr::map(data, get.process.func(prf))
}
return(data)
Expand All @@ -311,6 +320,7 @@ organize_simple_type_data <- function(data, prf, keep_names) {
organize_map_type_data <- function(data, prf, keep_names) {
.organize_map_type_data <- function(data, prf, keep_names) {
if (is_simple_type(prf)) {
data <- purrr::map(data, ~ .fromJSON.if.any(.x, "{"))
data <- purrr::map(data, organize_simple_type_data, prf, keep_names)
return(data)
}
Expand All @@ -325,6 +335,14 @@ organize_map_type_data <- function(data, prf, keep_names) {
return(data)
}
if (!prf$is_parent_array_) {
data <- purrr::map(
data,
~ if (is.null(.x)) {
.x
} else {
purrr::map(.x, ~ .fromJSON.if.any(.x, c("{", "[")))
}
)
return(.organize_map_type_data(data, prf, keep_names))
} else {
return(purrr::map(data, .organize_map_type_data, prf, keep_names))
Expand Down Expand Up @@ -370,6 +388,7 @@ organize_row_type_data <- function(data, prf, keep_names) {
}
# array of rows -> tibble
if (prf$is_array_) {
data <- purrr::map(data, ~ purrr::map(.x, ~ .fromJSON.if.any(.x, "[")))
data <- purrr::map(data, ~ organize.data.by.schema(., prf$fields_))
data <- purrr::map(data, ~ tibble::as_tibble(.))
}
Expand Down Expand Up @@ -420,12 +439,20 @@ organize.data.by.schema <- function(data, schema, keep_names = TRUE) {
# map type
if (prf$is_map_) {
# process all rows
data[[col.idx]] <- purrr::map(
data[[col.idx]],
~ .fromJSON.if.any(.x, c("{", "["))
)
data[[col.idx]] <- organize_map_type_data(
data[[col.idx]], prf$fields_[[1]], keep_names
)
}
# row type
if (prf$is_row_) {
data[[col.idx]] <- purrr::map(
data[[col.idx]],
~ .fromJSON.if.any(.x, "[")
)
data[[col.idx]] <- organize_row_type_data(
data[[col.idx]], prf, keep_names
)
Expand Down
6 changes: 5 additions & 1 deletion R/sqlCreateTableAs.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ setGeneric("sqlCreateTableAs",
#' @rdname PrestoConnection-class
#' @usage NULL
.sqlCreateTableAs <- function(con, name, sql, with = NULL, ...) {
name <- DBI::dbQuoteIdentifier(con, name)
if (inherits(name, "dbplyr_table_path")) { # dbplyr >= 2.5.0
name <- dbplyr::table_path_name(name, con)
} else {
name <- DBI::dbQuoteIdentifier(con, name)
Comment on lines +34 to +37

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is still breaking for dbplyr < 2.5.0 and DBI >= 1.2.0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you be more specific about the breaking? I ran all the unit tests with dbplyr 2.4.0 and DBI 1.2.3 and all tests passed.

}

DBI::SQL(paste0(
"CREATE TABLE ", name, "\n",
Expand Down
Loading