Skip to content

per-query bigint option #182

Closed
Closed

Description

@r2evans

Kirill, I'm finally catching up on the RPostgres version, thanks for your efforts on this.

I'm confident that the switch to bit64::integer64 as the default was not made hastily, and I'm glad we have the capability. Since there are many aspects of base R that deal silently-incorrectly with this type, has there been discussion on being able to switch the bigint= argument per-query or even per-column?

Use Case: pre-existing processes that may rely on significant external code that either have unknown or known-bad compatibility with bit64::integer64, but with a connection where 64-bit numbers are needed for some fields. Setting it in dbConnect would still be relevant (as the default), but it'd be nice to be able to still use 64-bits when needed.

con <- dbConnect(..., bigint="integer")
dbGetQuery(con, "select 1 as a") # uses the dbConnect option
dbGetQuery(con, "select 1 as a", bigint="integer64") # overrides

It's likely going too far, but would anyone see utility in doing it per-column? The workaround until then would be to split the query into two, but this has the potential of being rather inefficient SQL-wise.

dbGetQuery(con, "select 1 as isbig, 2 as isnotbig", bigint=c(isbig="integer64",isnotbig="integer"))

where the code might be:

convert_bigint_fields <- function (conn, ret, bigint = conn@bigint) {
    bignms <- names(bigint)
    if (is.null(bignms)) {
      bigint <- setNames(rep(bigint, ncol(ret)), colnames(ret))
    }
    # set all others as default
    bigint[ setdiff(colnames(ret), names(bigint)) ] <- 'default'
    # reorder them
    bigint <- bigint[colnames(ret)]
    is_int64 <- vlapply(ret, inherits, "integer64")
    funs <- list(integer = as.integer, numeric = as.numeric, character = as.character)
    for (funnm in names(funs)) {
      istype <- is_int64 & (bigint == funnm)
      ret[istype] <- lapply(ret[istype], funs[[funnm]])
    }
    ret
}

Activity

  1. krlmlr commented on Apr 30, 2018

    @krlmlr
    Member

    Thanks, Bill. I'm considering bigint a band-aid, more sophisticated ways are discussed in https://github.com/r-dbi/DBI/issues/137. Providing bigint per query seems straightforward and useful, but in the end perhaps it's easier if you consistently used custom wrappers around dbFetch() and dbGetQuery() that took care of the conversion?

    The way the bigint argument is currently implemented is just a post-processing as one of the last steps in dbFetch(). This does add a penalty for anything other than bigint = "integer64", but it works, and the penalty is likely relevant only for large volumes of data.

  2. r2evans commented on May 1, 2018

    @r2evans
    Author

    I can see some simplicity in doing self-recovery post-dbFetch, certainly, and it keeps things clean from your perspective. I think I fully grok the concept in DBI/137, I'll wait to see how that pans out. Thanks, Kirill.

  3. added this to the 1.2.2 milestone on Dec 26, 2020
  4. modified the milestones: 1.2.2, 1.2.3, 1.4.0 on Jan 2, 2021
  5. krlmlr commented on Sep 25, 2021

    @krlmlr
    Member

    Now that we have ALTREP it feels more prudent to focus efforts on providing an ALTREP-based view on the data returned from the database, with conversion as necessary after fetching the data.

  6. github-actions commented on Sep 26, 2022

    @github-actions
    Contributor

    This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

  7. locked and limited conversation to collaborators on Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions