Skip to content
Draft

Muck #315

Show file tree
Hide file tree
Changes from all 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 .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
^renv$
^renv\.lock$
^CRAN-RELEASE$
extras
man-roxygen
Expand Down
24 changes: 23 additions & 1 deletion .github/workflows/R_CMD_check_Hades.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,31 @@ jobs:

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
extra-packages: any::rcmdcheck reticulate
needs: check

- uses: actions/setup-python@v4
with:
python-version: "3.x"

- name: setup r-reticulate venv
shell: Rscript {0}
run: |

if (.Platform$OS.type == "unix" && Sys.info()["sysname"] == "Darwin") {
# macOS
python_path <- "/usr/bin/python3"
} else {
# Windows and Linux
python_path <- Sys.which("python")
}

path_to_python <- reticulate::virtualenv_create(
envname = "r-reticulate",
python = python_path,
packages = c("sqlglot")
)

- uses: r-lib/actions/check-r-package@v2
with:
args: 'c("--no-manual", "--as-cran")'
Expand Down
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ Suggests:
bigrquery,
pool,
ParallelLogger,
AzureStor
AzureStor,
reticulate
License: Apache License
VignetteBuilder: knitr
URL: https://ohdsi.github.io/DatabaseConnector/, https://github.com/OHDSI/DatabaseConnector
Expand Down
38 changes: 29 additions & 9 deletions R/Connect.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ checkIfDbmsIsSupported <- function(dbms) {
"snowflake",
"synapse",
"duckdb",
"iris"
"iris",
"muckdb"
)
deprecated <- c(
"hive",
Expand Down Expand Up @@ -292,10 +293,10 @@ connect <- function(connectionDetails = NULL,
# Using default connectionDetails
assertDetailsCanBeValidated(connectionDetails)
checkIfDbmsIsSupported(connectionDetails$dbms)

if (connectionDetails$dbms %in% c("sqlite", "sqlite extended")) {
connectSqlite(connectionDetails)
} else if (connectionDetails$dbms == "duckdb") {
} else if (connectionDetails$dbms %in% c("duckdb", "muckdb")) {
connectDuckdb(connectionDetails)
} else if (connectionDetails$dbms == "spark" && is.null(connectionDetails$connectionString())) {
connectSparkUsingOdbc(connectionDetails)
Expand Down Expand Up @@ -614,7 +615,7 @@ connectHive <- function(connectionDetails) {
inform("Connecting using Hive driver")
jarPath <- findPathToJar("^hive-jdbc-([.0-9]+-)*standalone\\.jar$", connectionDetails$pathToDriver)
driver <- getJbcDriverSingleton("org.apache.hive.jdbc.HiveDriver", jarPath)

if (is.null(connectionDetails$connectionString()) || connectionDetails$connectionString() == "") {
connectionString <- paste0("jdbc:hive2://", connectionDetails$server(), ":", connectionDetails$port(), "/")
if (!is.null(connectionDetails$extraSettings)) {
Expand Down Expand Up @@ -825,7 +826,7 @@ connectUsingDbi <- function(dbiConnectionDetails) {
dbms <- dbiConnectionDetails$dbms
dbiConnectionDetails$dbms <- NULL
dbiConnection <- do.call(DBI::dbConnect, dbiConnectionDetails)

connection <- new("DatabaseConnectorDbiConnection",
server = dbms,
dbiConnection = dbiConnection,
Expand All @@ -842,6 +843,17 @@ connectUsingDbi <- function(dbiConnectionDetails) {
connectDuckdb <- function(connectionDetails) {
inform("Connecting using DuckDB driver")
ensure_installed("duckdb")

# Use muckdb if requested
if (connectionDetails$dbms == "muckdb") {
ensure_installed("reticulate")
if (!reticulate::py_module_available("sqlglot"))
stop("Python module 'sqlglot' is required. Install via pip: pip install sqlglot or reticulate::install_python('sqlglot')")

checkIfDbmsIsSupported(connectionDetails$connectionString())
inform(paste(connectionDetails$connectionString(), "mucks like \U1f986"))
}

connection <- connectUsingDbi(
createDbiConnectionDetails(
dbms = connectionDetails$dbms,
Expand All @@ -850,24 +862,32 @@ connectDuckdb <- function(connectionDetails) {
bigint = "integer64"
)
)
# Check if ICU extension if installed, and if not, try to install it:
# Check if ICU extension is installed, and if not, try to install it:
isInstalled <- querySql(
connection = connection,
connection = connection,
sql = "SELECT installed FROM duckdb_extensions() WHERE extension_name = 'icu';"
)[1, 1]
if (!isInstalled) {
warning("The ICU extension of DuckDB is not installed. Attempting to install it.")
tryCatch(
executeSql(connection, "INSTALL icu"),
error = function(e) {
warning("Attempting to install the ICU extension of DuckDB failed.\n",
warning("Attempting to install the ICU extension of DuckDB failed.\n",
"You may need to check your internet connection.\n",
"For more detail, try 'executeSql(connection, \"INSTALL icu\")'.\n",
"Be aware that some time and date functionality will not be available.")
"Be aware that some time and date functionality will not be available.")
return(NULL)
}
)
}

# For muckdb, set dbms attribute to the target dialect for translation and attaches transpiler
if (connectionDetails$dbms == "muckdb") {
attr(connection, "dbms") <- connectionDetails$connectionString()
attr(connection, "sqlglot") <- reticulate::import("sqlglot")
attr(connection, "isMuckDb") <- TRUE
}

return(connection)
}

Expand Down
14 changes: 13 additions & 1 deletion R/Sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ executeSql <- function(connection,
if (!DBI::dbIsValid(connection)) {
abort("Connection is closed")
}


if (isTRUE(attr(connection, "isMuckDb"))) {
sqlglot <- attr(connection, "sqlglot")
sql <- sqlglot$transpile(sql, read = dbms(connection), write = "duckdb", unsupported_level = "IGNORE") |>
paste(collapse = ";\n")
}

startTime <- Sys.time()
dbms <- dbms(connection)

Expand Down Expand Up @@ -331,6 +337,12 @@ querySql <- function(connection,
if (!DBI::dbIsValid(connection)) {
abort("Connection is closed")
}

if (isTRUE(attr(connection, "isMuckDb"))) {
sqlglot <- attr(connection, "sqlglot")
sql <- sqlglot$transpile(sql, read = dbms(connection), write = "duckdb", unsupported_level = "IGNORE")
}

# Calling splitSql, because this will also strip trailing semicolons (which cause Oracle to crash).
sqlStatements <- SqlRender::splitSql(sql)
if (length(sqlStatements) > 1) {
Expand Down

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

3 changes: 2 additions & 1 deletion man/dbClearResult-DatabaseConnectorDbiResult-method.Rd

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

3 changes: 2 additions & 1 deletion man/dbClearResult-DatabaseConnectorJdbcResult-method.Rd

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

3 changes: 2 additions & 1 deletion man/dbColumnInfo-DatabaseConnectorDbiResult-method.Rd

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

3 changes: 2 additions & 1 deletion man/dbColumnInfo-DatabaseConnectorJdbcResult-method.Rd

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

3 changes: 2 additions & 1 deletion man/dbCreateTable-DatabaseConnectorConnection-method.Rd

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

3 changes: 2 additions & 1 deletion man/dbDisconnect-DatabaseConnectorConnection-method.Rd

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

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

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

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

3 changes: 2 additions & 1 deletion man/dbFetch-DatabaseConnectorDbiResult-method.Rd

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

3 changes: 2 additions & 1 deletion man/dbFetch-DatabaseConnectorJdbcResult-method.Rd

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

Loading
Loading