Skip to content

Commit

Permalink
Removed unnecessary code, roxygenised.
Browse files Browse the repository at this point in the history
  • Loading branch information
mratford committed Feb 16, 2022
1 parent 2442a1f commit 2ae0bf5
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 101 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(convert_athena_type_to_arrow)
export(create_temp_table)
export(delete_database_and_data)
export(delete_partitions_and_data)
Expand Down
77 changes: 0 additions & 77 deletions R/read.R
Original file line number Diff line number Diff line change
@@ -1,45 +1,3 @@
#' Convert an Athena type to an Arrow type
#'
#' @param t A string giving an Athena type
#'
#' @return An Arrow type
#' @export
#'
#' @seealso https://docs.aws.amazon.com/athena/latest/ug/data-types.html
#' @seealso https://arrow.apache.org/docs/r/reference/data-type.html
convert_athena_type_to_arrow <- function(t) {
# Regular expression matches either e.g. decimal(10) or decimal(10, 5)
decimal_match <- stringr::str_match(
t,
"decimal\\(([:digit:]+)(\\s*,\\s*([:digit:]+))?\\)"
)
if (!is.na(decimal_match[1])) {
precision <- as.numeric(decimal_match[2])
# Set scale to default 0 if not present
scale <- ifelse(is.na(decimal_match[4]), 0, as.numeric(decimal_match[4]))
return(arrow::decimal(precision, scale))
}

switch(
t,
"boolean" = arrow::bool(),
"tinyint" = arrow::int8(),
"smallint" = arrow::int16(),
"int" = arrow::int32(),
"integer" = arrow::int32(),
"bigint" = arrow::int64(),
"double" = arrow::float64(),
"float" = arrow::float32(),
"char" = arrow::string(),
"string" = arrow::string(),
"binary" = arrow::binary(),
"date" = arrow::date32(),
"timestamp" = arrow::timestamp(unit="ms", timezone="UTC"),
arrow::string()
) %>% return
}


#' Send an SQL query to Athena and receive a dataframe.
#'
#' @param sql An SQL query
Expand All @@ -50,41 +8,6 @@ convert_athena_type_to_arrow <- function(t) {
#' @examples
#' `df <- dbtools::read_sql_query('select * from my_db.my_table')`
read_sql_query <- function(sql) {
# This approach doesn't work because for some reason pydbtools and boto3
# stopped playing well together under reticulate, with boto3 throwing a
# permissions error which doesn't occur in pure Python. Leaving this code here
# in case it gets fixed as it would be much quicker.
#
# query_id <- dbtools.env$pydb$start_query_execution(sql)
# dbtools.env$pydb$wait_query(query_id)
# athena_status <- dbtools.env$pydb$get_query_execution(query_id)
#
# if (athena_status$Status$State == 'FAILED') {
# stop('SQL query failed with response error;\n',
# athena_status$Status$StateChangeReason)
# }
#
# response <- dbtools.env$boto3$client('athena')$get_query_results(
# QueryExecutionId=athena_status$QueryExecutionId
# )
#
# # Create arrow schema as a list of arrow::Fields
# schema <- list()
# for (col_info in response$ResultSet$ResultSetMetadata$ColumnInfo) {
# schema <- append(
# schema,
# list(arrow::field(col_info$Name,
# convert_athena_type_to_arrow(col_info$Type)))
# )
# }
#
# df <- arrow::read_csv_arrow(
# athena_status$ResultConfiguration$OutputLocation,
# schema=schema,
# convert_options=arrow::CsvConvertOptions$create(strings_can_be_null=TRUE)
# )


# Download the dataframe result to a parquet temporary file as pandas and
# reticulate are frequently incompatible, and load the data into R using
# arrow.
Expand Down
22 changes: 0 additions & 22 deletions man/convert_athena_type_to_arrow.Rd

This file was deleted.

3 changes: 3 additions & 0 deletions man/get_query_execution.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/read_sql_query.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/start_query_execution.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/start_query_execution_and_wait.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/wait_query.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2ae0bf5

Please sign in to comment.