From 9919fbc3f90ffe08457c8ea00b4cd376abf3cf1a Mon Sep 17 00:00:00 2001 From: schuemie Date: Tue, 28 Nov 2023 08:27:46 +0100 Subject: [PATCH] Fix bulk import for Postgres on MacOs. --- .settings/org.eclipse.jdt.core.prefs | 3 +++ NEWS.md | 2 ++ R/BulkLoad.R | 12 +++++------- R/InsertTable.R | 20 +++++++++++++------- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index a698e596..0fee6a9c 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -8,5 +8,8 @@ org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=disabled org.eclipse.jdt.core.compiler.source=1.8 diff --git a/NEWS.md b/NEWS.md index c10ccc61..dafc8a10 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,8 @@ Bugfixes: 1. Fixed `dbFetch()` for DBI drivers, no longer ignoring `n` argument. +2. Fix bulk import for Postgres on MacOs. + DatabaseConnector 6.3.0 ======================= diff --git a/R/BulkLoad.R b/R/BulkLoad.R index 9eb677b8..e441ecee 100644 --- a/R/BulkLoad.R +++ b/R/BulkLoad.R @@ -286,7 +286,6 @@ bulkLoadHive <- function(connection, sqlTableName, sqlFieldNames, data) { ) } - bulkLoadPostgres <- function(connection, sqlTableName, sqlFieldNames, sqlDataTypes, data) { logTrace(sprintf("Inserting %d rows into table '%s' using PostgreSQL bulk load", nrow(data), sqlTableName)) startTime <- Sys.time() @@ -306,13 +305,12 @@ bulkLoadPostgres <- function(connection, sqlTableName, sqlFieldNames, sqlDataTyp password <- attr(connection, "password")() if (.Platform$OS.type == "windows") { - winPsqlPath <- Sys.getenv("POSTGRES_PATH") - command <- file.path(winPsqlPath, "psql.exe") - if (!file.exists(command)) { - abort(paste("Could not find psql.exe in ", winPsqlPath)) - } + command <- file.path(Sys.getenv("POSTGRES_PATH"), "psql.exe") } else { - command <- "psql" + command <- file.path(Sys.getenv("POSTGRES_PATH"), "psql") + } + if (!file.exists(command)) { + abort(paste("Could not find psql.exe in ", Sys.getenv("POSTGRES_PATH"))) } headers <- paste0("(", sqlFieldNames, ")") if (is.null(port)) { diff --git a/R/InsertTable.R b/R/InsertTable.R index fde3c62f..21fb0c7a 100644 --- a/R/InsertTable.R +++ b/R/InsertTable.R @@ -130,7 +130,8 @@ validateInt64Insert <- function() { #' #' PostgreSQL: #' Uses the 'psql' executable to upload. Set the POSTGRES_PATH environment variable to the Postgres -#' binary path, e.g. 'C:/Program Files/PostgreSQL/11/bin'. +#' binary path, e.g. 'C:/Program Files/PostgreSQL/11/bin' on Windows or '/Library/PostgreSQL/16/bin' +#' on MacOs. #' #' @examples #' \dontrun{ @@ -233,21 +234,26 @@ insertTable.default <- function(connection, if (!is.null(databaseSchema)) { tableName <- paste(databaseSchema, tableName, sep = ".") } + if (Andromeda::isAndromedaTable(data)) { + warn("Batch-wise uploading of Andromeda tables currently not supported. Loading entire table in memory.", + .frequency = "regularly", + .frequency_id = "useMppBulkLoad" + ) + data <- collect(data) + } if (is.vector(data) && !is.list(data)) { data <- data.frame(x = data) } - if (length(data) < 1) { + if (ncol(data) < 1) { abort("data must have at least one column") } if (is.null(names(data))) { names(data) <- paste("V", 1:length(data), sep = "") } - if (length(data[[1]]) > 0) { - if (!is.data.frame(data)) { + if (!is.data.frame(data)) { + if (nrow(data) > 0) { data <- as.data.frame(data, row.names = 1:length(data[[1]])) - } - } else { - if (!is.data.frame(data)) { + } else { data <- as.data.frame(data) } }