Browse Source

dplyr bits working

master
boB Rudis 1 year ago
parent
commit
f61b24cc0b
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
3 changed files with 91 additions and 20 deletions
  1. +3
    -0
      NAMESPACE
  2. +64
    -20
      R/z-dbGetQuery.R
  3. +24
    -0
      man/fetch-AthenaResult-numeric-method.Rd

+ 3
- 0
NAMESPACE View File

@@ -11,14 +11,17 @@ exportClasses(AthenaDriver)
exportClasses(AthenaResult)
exportMethods(dbConnect)
exportMethods(dbExistsTable)
exportMethods(dbGetInfo)
exportMethods(dbGetQuery)
exportMethods(dbListFields)
exportMethods(dbListTables)
exportMethods(dbReadTable)
exportMethods(dbSendQuery)
exportMethods(fetch)
import(DBI)
import(RJDBC)
import(bit64)
import(dbplyr)
importFrom(aws.signature,read_credentials)
importFrom(aws.signature,use_credentials)
importFrom(rJava,.jcall)

+ 64
- 20
R/z-dbGetQuery.R View File

@@ -23,45 +23,89 @@ list(
"1111" = as.character # OTHER
) -> .jdbc_converters

#' AthenaJDBC
#'
#' @param conn Athena connection
#' @param statement SQL statement
#' @param ... unused
#' @importFrom rJava .jcall
#' @export
setMethod(
#' @keywords internal
setMethod("dbGetInfo", "AthenaDriver", def=function(dbObj, ...)
list(
name = "AthenaJDBC",
driver_version = list.files(system.file("java", package="metis.lite"), "jar$")[1],
package_version = utils::packageVersion("metis.lite")
)
)

"dbGetQuery",
signature(conn="AthenaConnection", statement="character"),

definition = function(conn, statement, type_convert=FALSE, ...) {
#' @export
#' @keywords internal
setMethod("dbGetInfo", "AthenaConnection", def=function(dbObj, ...)
list(
name = "AthenaJDBC",
driver_version = list.files(system.file("java", package="metis.lite"), "jar$")[1],
package_version = utils::packageVersion("metis.lite")
)
)

r <- dbSendQuery(conn, statement, ...)

on.exit(.jcall(r@stat, "V", "close"))

#message("dbGetQuery()")
#' Fetch records from a previously executed query
#'
#' Fetch the next `n` elements (rows) from the result set and return them
#' as a data.frame.
#'
#' @param res An object inheriting from [DBIResult-class], created by
#' [dbSendQuery()].
#' @param n maximum number of records to retrieve per fetch. Use `n = -1`
#' or `n = Inf`
#' to retrieve all pending records. Some implementations may recognize other
#' special values.
#' @param ... Other arguments passed on to methods.
#' @export
setMethod(
"fetch",
signature(res="AthenaResult", n="numeric"),
def = function(res, n, block = 1000L, ...) {

nms <- c()
athena_type_convert <- list()

cols <- .jcall(r@md, "I", "getColumnCount")
cols <- .jcall(res@md, "I", "getColumnCount")

for (i in 1:cols) {
ct <- as.character(.jcall(r@md, "I", "getColumnType", i))
ct <- as.character(.jcall(res@md, "I", "getColumnType", i))
athena_type_convert[[i]] <- .jdbc_converters[[ct]]
nms <- c(nms, .jcall(r@md, "S", "getColumnLabel", i))
nms <- c(nms, .jcall(res@md, "S", "getColumnLabel", i))
}

athena_type_convert <- set_names(athena_type_convert, nms)

res <- fetch(r, -1, block = 1000)
out <- callNextMethod(res = res, n = n, block = block, ...)

for (nm in names(athena_type_convert)) {
res[[nm]] <- athena_type_convert[[nm]](res[[nm]])
out[[nm]] <- athena_type_convert[[nm]](out[[nm]])
}

out

}
)

#' AthenaJDBC
#'
#' @param conn Athena connection
#' @param statement SQL statement
#' @param ... unused
#' @importFrom rJava .jcall
#' @export
setMethod(

"dbGetQuery",
signature(conn="AthenaConnection", statement="character"),

definition = function(conn, statement, type_convert=FALSE, ...) {

r <- dbSendQuery(conn, statement, ...)

on.exit(.jcall(r@stat, "V", "close"))

res <- fetch(r, -1, block = 1000L)

class(res) <- c("tbl_df", "tbl", "data.frame")

res


+ 24
- 0
man/fetch-AthenaResult-numeric-method.Rd View File

@@ -0,0 +1,24 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/z-dbGetQuery.R
\docType{methods}
\name{fetch,AthenaResult,numeric-method}
\alias{fetch,AthenaResult,numeric-method}
\title{Fetch records from a previously executed query}
\usage{
\S4method{fetch}{AthenaResult,numeric}(res, n, block = 1000L, ...)
}
\arguments{
\item{res}{An object inheriting from [DBIResult-class], created by
[dbSendQuery()].}

\item{n}{maximum number of records to retrieve per fetch. Use `n = -1`
or `n = Inf`
to retrieve all pending records. Some implementations may recognize other
special values.}

\item{...}{Other arguments passed on to methods.}
}
\description{
Fetch the next `n` elements (rows) from the result set and return them
as a data.frame.
}

Loading…
Cancel
Save