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

Fixup/oracle dates #810

Merged
merged 7 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# odbc (development version)

* oracle: Fix writing to DATE and TIMESTAMP(n) targets using `batch_size` > 1.

* Raises "Cancelling previous query" warnings from R rather than from Rcpp when
a connection has a current result to avoid possible incorrect resource
unwinds with `options(warn = 2)` (#797).
Expand Down
2 changes: 1 addition & 1 deletion R/dbi-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ setMethod("dbAppendTable", "OdbcConnection",
)
rs <- OdbcResult(conn, sql)

if (!is.null(fieldDetails) && nrow(fieldDetails) == nparam) {
if (!is.null(fieldDetails) && nrow(fieldDetails) <= nparam) {
result_describe_parameters(rs@ptr, fieldDetails)
}

Expand Down
7 changes: 6 additions & 1 deletion R/driver-oracle.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ setMethod("odbcConnectionColumns_", c("Oracle", "character"),
)
}

dbGetQuery(conn, query)
res <- dbGetQuery(conn, query)

res$data_type <- as.numeric(res$data_type)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to revert this column back to another type after processing it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hey Simon:

Good question - I think the query can/does return all NULLs for that field, which in turn I think gives a column full of NA_character_ in the data-frame.

res[res$`field.type` == "DATE", c("data_type", "column_size")] <- c(91, 6)
res[grepl("TIMESTAMP", res$`field.type`), c("data_type", "column_size")] <- c(93, 16)
detule marked this conversation as resolved.
Show resolved Hide resolved
res
}
)

Expand Down
27 changes: 22 additions & 5 deletions tests/testthat/test-driver-oracle.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
test_that("can round columns", {
con <- test_con("ORACLE")
# - Long/outstanding issue with batch inserting
# date/datetime for Oracle. See for example
# #349, #350, #391
# - Date does not roundtrip correctly since
# their DATE data-type contains hour/min/seconds.
# To avoid loss of precision we read it in as
# POSIXct.
# - There also looks like there are issues related
# to binary elements of size zero.
# to binary elements of size zero.
# - Finally, no boolean in Oracle prior to 23
test_roundtrip(con, columns = c("time", "date", "datetime", "binary", "logical"))
test_roundtrip(con, columns = c("time", "date", "binary", "logical"))
})

test_that("can detect existence of table", {
Expand All @@ -18,3 +19,19 @@ test_that("can detect existence of table", {
tbl2 <- local_table(con, "mtcars_test", mtcars)
expect_true(dbExistsTable(con, tbl2))
})

test_that("Writing date/datetime with batch size > 1", {
# See #349, #350, #391
con <- test_con("ORACLE")

ir <- datasets::iris
values <- data.frame(
datetime = as.POSIXct(as.numeric(ir$Petal.Length * 10), origin = "2024-01-01", tz = "UTC"),
date = as.Date(as.numeric(ir$Petal.Length * 10), origin = "2024-01-01", tz = "UTC"),
integer = as.integer(ir$Petal.Width * 100),
double = ir$Sepal.Length,
varchar = ir$Species)
detule marked this conversation as resolved.
Show resolved Hide resolved
tbl <- local_table(con, "test_batched_write_w_dates", values)
res <- dbReadTable(con, "test_batched_write_w_dates")
expect_true(nrow(res) == nrow(values))
})
Loading