Skip to content

Backtick issue for #323

@eric-fey-hus

Description

@eric-fey-hus

Issue: we are using spar/databricks backend, where we need to specify tables in the form catalog.schema.table. Our catalog names contain colons, meaning we have to escape with backticks for SQL queries and most DatabaseConnector functions to work properly. Issue is the inconsistent use of dealing with backticks in DatabaseConnector, some functions use it, some do not. Here specifically, DatabaseConnector::getTableNames caused the issue. See details and fix below.

#hotfix for DatabaseConnector::getTableNames

getTableNames <- function(connection, databaseSchema = NULL, cast = "lower") {
  errorMessages <- checkmate::makeAssertCollection()
  checkmate::assertTRUE(DBI::dbIsValid(connection))
  checkmate::assertCharacter(databaseSchema, len = 1, null.ok = TRUE, add = errorMessages)
  checkmate::assertChoice(cast, c("upper", "lower", "none"), add = errorMessages)
  checkmate::reportAssertions(collection = errorMessages)

  if (is.null(databaseSchema)) {
    tableNames <- DBI::dbListTables(connection)
  } else if (is(connection, "DatabaseConnectorConnection")) {
    #EF: here I had to fix (fors spak/databricks with jdbc driver
    tableNames <- DBI::dbListTables(conn = connection, gsub("`", "", databaseSchema))
  } else if (is(connection, "PqConnection") || is(connection, "RedshiftConnection") || is(connection, "duckdb_connection")) {
    stopifnot(length(databaseSchemaSplit) == 1)
    sql <- paste0("SELECT table_name FROM information_schema.tables WHERE table_schema = '", databaseSchema, "';")
    tableNames <- DBI::dbGetQuery(connection, sql)[["table_name"]]
  } else if (is(connection, "Microsoft SQL Server")) {
    databaseSchemaSplit <- strsplit(databaseSchema, "\\.")[[1]]
    if (!(length(databaseSchemaSplit) %in% 1:2)) rlang::abort("databaseSchema can contain at most one dot (.)")

    if (length(databaseSchemaSplit) == 1) {
      tableNames <- DBI::dbListTables(connection, schema_name = databaseSchemaSplit)
    } else {
      tableNames <- DBI::dbListTables(connection, catalog_name = databaseSchemaSplit[[1]], schema_name = databaseSchemaSplit[[2]])
    }
  } else if (is(connection, "SQLiteConnection")) {
    if (databaseSchema != "main") rlang::abort("The only schema supported on SQLite is 'main'")
    tableNames <- DBI::dbListTables(connection)
  } else {
    rlang::abort(paste(paste(class(connection), collapse = ", "), "connection not supported"))
  }

  switch(cast,
         "upper" = toupper(tableNames),
         "lower" = tolower(tableNames),
         "none" = tableNames
  )
}

assignInNamespace("getTableNames", getTableNames, ns="DatabaseConnector")
cat("\nEric: loaded runtime databricks connection hotfix for DatabaseConnector::getTableNames")
cat("\thandle backtick symbol")

So Line 13 was the offender, where I used this

tableNames <- DBI::dbListTables(conn = connection, gsub("`", "", databaseSchema))

to deal with the backticks issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions