Access and Query Amazon Athena via DBI/JDBC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
5.1KB

  1. #' @rdname Athena
  2. #' @keywords internal
  3. #' @export
  4. db_data_type.AthenaConnection <- function(con, fields, ...) {
  5. print("\n\n\ndb_data_type\n\n\n")
  6. data_type <- function(x) {
  7. switch(
  8. class(x)[1],
  9. integer64 = "BIGINT",
  10. logical = "BOOLEAN",
  11. integer = "INTEGER",
  12. numeric = "DOUBLE",
  13. factor = "CHARACTER",
  14. character = "CHARACTER",
  15. Date = "DATE",
  16. POSIXct = "TIMESTAMP",
  17. stop("Can't map type ", paste(class(x), collapse = "/"),
  18. " to a supported database type.")
  19. )
  20. }
  21. vapply(fields, data_type, character(1))
  22. }
  23. #' @rdname Athena
  24. #' @keywords internal
  25. #' @export
  26. sql_translate_env.AthenaConnection <- function(con) {
  27. x <- con
  28. dbplyr::sql_variant(
  29. scalar = dbplyr::sql_translator(
  30. .parent = dbplyr::base_scalar,
  31. `!=` = dbplyr::sql_infix("<>"),
  32. as.integer64 = function(x) dbplyr::build_sql("CAST(", x, "AS BIGINT)"),
  33. as.numeric = function(x) dbplyr::build_sql("CAST(", x, " AS DOUBLE)"),
  34. as.character = function(x) dbplyr::build_sql("CAST(", x, " AS CHARACTER)"),
  35. as.date = function(x) dbplyr::build_sql("CAST(", x, " AS DATE)"),
  36. as.Date = function(x) dbplyr::build_sql("CAST(", x, " AS DATE)"),
  37. as.POSIXct = function(x) dbplyr::build_sql("CAST(", x, " AS TIMESTAMP)"),
  38. as.posixct = function(x) dbplyr::build_sql("CAST(", x, " AS TIMESTAMP)"),
  39. as.logical = function(x) dbplyr::build_sql("CAST(", x, " AS BOOLEAN)"),
  40. date_part = function(x, y) dbplyr::build_sql("DATE_PART(", x, ",", y ,")"),
  41. grepl = function(x, y) dbplyr::build_sql("CONTAINS(", y, ", ", x, ")"),
  42. gsub = function(x, y, z) dbplyr::build_sql("REGEXP_REPLACE(", z, ", ", x, ",", y ,")"),
  43. trimws = function(x) dbplyr::build_sql("TRIM(both ' ' FROM ", x, ")"),
  44. cbrt = dbplyr::sql_prefix("CBRT", 1),
  45. degrees = dbplyr::sql_prefix("DEGREES", 1),
  46. e = dbplyr::sql_prefix("E", 0),
  47. row_number = dbplyr::sql_prefix("row_number", 0),
  48. lshift = dbplyr::sql_prefix("LSHIFT", 2),
  49. mod = dbplyr::sql_prefix("MOD", 2),
  50. age = dbplyr::sql_prefix("AGE", 1),
  51. negative = dbplyr::sql_prefix("NEGATIVE", 1),
  52. pi = dbplyr::sql_prefix("PI", 0),
  53. pow = dbplyr::sql_prefix("POW", 2),
  54. radians = dbplyr::sql_prefix("RADIANS", 1),
  55. rand = dbplyr::sql_prefix("RAND", 0),
  56. rshift = dbplyr::sql_prefix("RSHIFT", 2),
  57. trunc = dbplyr::sql_prefix("TRUNC", 2),
  58. contains = dbplyr::sql_prefix("CONTAINS", 2),
  59. convert_to = dbplyr::sql_prefix("CONVERT_TO", 2),
  60. convert_from = dbplyr::sql_prefix("CONVERT_FROM", 2),
  61. string_binary = dbplyr::sql_prefix("STRING_BINARY", 1),
  62. binary_string = dbplyr::sql_prefix("BINARY_STRING", 1),
  63. to_char = dbplyr::sql_prefix("TO_CHAR", 2),
  64. to_date = dbplyr::sql_prefix("TO_DATE", 2),
  65. to_number = dbplyr::sql_prefix("TO_NUMBER", 2),
  66. char_to_timestamp = dbplyr::sql_prefix("TO_TIMESTAMP", 2),
  67. double_to_timestamp = dbplyr::sql_prefix("TO_TIMESTAMP", 1),
  68. char_length = dbplyr::sql_prefix("CHAR_LENGTH", 1),
  69. flatten = dbplyr::sql_prefix("FLATTEN", 1),
  70. kvgen = dbplyr::sql_prefix("KVGEN", 1),
  71. repeated_count = dbplyr::sql_prefix("REPEATED_COUNT", 1),
  72. repeated_contains = dbplyr::sql_prefix("REPEATED_CONTAINS", 2),
  73. ilike = dbplyr::sql_prefix("ILIKE", 2),
  74. init_cap = dbplyr::sql_prefix("INIT_CAP", 1),
  75. length = dbplyr::sql_prefix("LENGTH", 1),
  76. lower = dbplyr::sql_prefix("LOWER", 1),
  77. tolower = dbplyr::sql_prefix("LOWER", 1),
  78. ltrim = dbplyr::sql_prefix("LTRIM", 2),
  79. nullif = dbplyr::sql_prefix("NULLIF", 2),
  80. position = function(x, y) dbplyr::build_sql("POSITION(", x, " IN ", y, ")"),
  81. regexp_replace = dbplyr::sql_prefix("REGEXP_REPLACE", 3),
  82. rtrim = dbplyr::sql_prefix("RTRIM", 2),
  83. rpad = dbplyr::sql_prefix("RPAD", 2),
  84. rpad_with = dbplyr::sql_prefix("RPAD", 3),
  85. lpad = dbplyr::sql_prefix("LPAD", 2),
  86. lpad_with = dbplyr::sql_prefix("LPAD", 3),
  87. strpos = dbplyr::sql_prefix("STRPOS", 2),
  88. substr = dbplyr::sql_prefix("SUBSTR", 3),
  89. trim = function(x, y, z) dbplyr::build_sql("TRIM(", x, " ", y, " FROM ", z, ")"),
  90. upper = dbplyr::sql_prefix("UPPER", 1),
  91. toupper = dbplyr::sql_prefix("UPPER", 1)
  92. ),
  93. aggregate = dbplyr::sql_translator(
  94. .parent = dbplyr::base_agg,
  95. n = function() dbplyr::sql("COUNT(*)"),
  96. cor = dbplyr::sql_prefix("CORR"),
  97. cov = dbplyr::sql_prefix("COVAR_SAMP"),
  98. sd = dbplyr::sql_prefix("STDDEV_SAMP"),
  99. var = dbplyr::sql_prefix("VAR_SAMP"),
  100. n_distinct = function(x) {
  101. dbplyr::build_sql(dbplyr::sql("COUNT(DISTINCT "), x, dbplyr::sql(")"))
  102. }
  103. ),
  104. window = dbplyr::sql_translator(
  105. .parent = dbplyr::base_win,
  106. n = function() { dbplyr::win_over(dbplyr::sql("count(*)"),
  107. partition = dbplyr::win_current_group()) },
  108. cor = dbplyr::win_recycled("corr"),
  109. cov = dbplyr::win_recycled("covar_samp"),
  110. sd = dbplyr::win_recycled("stddev_samp"),
  111. var = dbplyr::win_recycled("var_samp"),
  112. all = dbplyr::win_recycled("bool_and"),
  113. any = dbplyr::win_recycled("bool_or")
  114. )
  115. )
  116. }