Access and Query Amazon Athena via DBI/JDBC
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

121 líneas
3.1KB

  1. list(
  2. "-7" = as.logical, # BIT
  3. "-6" = as.integer, # TINYINT
  4. "-5" = bit64::as.integer64, # BIGINT
  5. "-4" = as.character, # LONGVARBINARY
  6. "-3" = as.character, # VARBINARY
  7. "-2" = as.character, # BINARY
  8. "-1" = as.character, # LONGVARCHAR
  9. "0" = as.character, # NULL
  10. "1" = as.character, # CHAR
  11. "2" = as.double, # NUMERIC
  12. "3" = as.double, # DECIMAL
  13. "4" = as.integer, # INTEGER
  14. "5" = as.integer, # SMALLINT
  15. "6" = as.double, # FLOAT
  16. "7" = as.double, # REAL
  17. "8" = as.double, # DOUBLE
  18. "12" = as.character, # VARCHAR
  19. "16" = as_logical, # BOOLEAN
  20. "91" = as_date, # DATE
  21. "92" = as.character, # TIME
  22. "93" = as_posixct, # TIMESTAMP
  23. "2003" = as.character, # ARRAY
  24. "1111" = as.character # OTHER
  25. ) -> .jdbc_converters
  26. #' @export
  27. #' @keywords internal
  28. setMethod("dbGetInfo", "AthenaDriver", def=function(dbObj, ...)
  29. list(
  30. name = "AthenaJDBC",
  31. driver_version = list.files(system.file("java", package="metis.lite"), "jar$")[1],
  32. package_version = utils::packageVersion("metis.lite")
  33. )
  34. )
  35. #' @export
  36. #' @keywords internal
  37. setMethod("dbGetInfo", "AthenaConnection", def=function(dbObj, ...)
  38. list(
  39. name = "AthenaJDBC",
  40. driver_version = list.files(system.file("java", package="metis.lite"), "jar$")[1],
  41. package_version = utils::packageVersion("metis.lite")
  42. )
  43. )
  44. #' Fetch records from a previously executed query
  45. #'
  46. #' Fetch the next `n` elements (rows) from the result set and return them
  47. #' as a data.frame.
  48. #'
  49. #' @param res An object inheriting from [DBIResult-class], created by
  50. #' [dbSendQuery()].
  51. #' @param n maximum number of records to retrieve per fetch. Use `n = -1`
  52. #' or `n = Inf`
  53. #' to retrieve all pending records. Some implementations may recognize other
  54. #' special values.
  55. #' @param ... Other arguments passed on to methods.
  56. #' @export
  57. setMethod(
  58. "fetch",
  59. signature(res="AthenaResult", n="numeric"),
  60. def = function(res, n, block = 1000L, ...) {
  61. nms <- c()
  62. athena_type_convert <- list()
  63. cols <- .jcall(res@md, "I", "getColumnCount")
  64. for (i in 1:cols) {
  65. ct <- as.character(.jcall(res@md, "I", "getColumnType", i))
  66. athena_type_convert[[i]] <- .jdbc_converters[[ct]]
  67. nms <- c(nms, .jcall(res@md, "S", "getColumnLabel", i))
  68. # message(ct, "|", tail(nms, 1))
  69. }
  70. athena_type_convert <- set_names(athena_type_convert, nms)
  71. out <- callNextMethod(res = res, n = n, block = block, ...)
  72. # print(str(out))
  73. for (nm in names(athena_type_convert)) {
  74. f <- athena_type_convert[[nm]]
  75. if (length(f) == 0) f <- as.character # catchall in case AMZN is tricksy
  76. out[[nm]] <- f(out[[nm]])
  77. }
  78. out
  79. }
  80. )
  81. #' AthenaJDBC
  82. #'
  83. #' @param conn Athena connection
  84. #' @param statement SQL statement
  85. #' @param ... unused
  86. #' @importFrom rJava .jcall
  87. #' @export
  88. setMethod(
  89. "dbGetQuery",
  90. signature(conn="AthenaConnection", statement="character"),
  91. definition = function(conn, statement, ...) {
  92. r <- dbSendQuery(conn, statement, ...)
  93. on.exit(.jcall(r@stat, "V", "close"))
  94. res <- fetch(r, -1, block = conn@fetch_size)
  95. class(res) <- c("tbl_df", "tbl", "data.frame")
  96. res
  97. }
  98. )