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

SQL Server fails to reuse temporary tables across sessions #719

Closed
krlmlr opened this issue Jan 8, 2024 · 4 comments
Closed

SQL Server fails to reuse temporary tables across sessions #719

krlmlr opened this issue Jan 8, 2024 · 4 comments
Labels
bug an unexpected problem or unintended behavior mssql Microsoft SQL Server

Comments

@krlmlr
Copy link
Member

krlmlr commented Jan 8, 2024

Issue Description and Expected Result

On SQL Server, dbWriteTable() fails to write a temporary table with the same name from two sessions. The cause is that dbExistsTable() shows temporary tables from other sessions. Fixing dbExistsTable() also fixes this problem.

On a side note, dbCreateTable() needs dbExecute(immediate = TRUE) .

Database

SQL Server

Reproducible Example

r_1 <- callr::r_session$new()
r_2 <- callr::r_session$new()

r_1$run_with_output(function() {
  .GlobalEnv$conn <- DBI::dbConnect(
    odbc::odbc(),
    driver = "ODBC Driver 18 for SQL Server",
    server = Sys.getenv("DM_TEST_DOCKER_HOST"),
    database = "test",
    uid = "SA",
    pwd = "YourStrong!Passw0rd",
    port = 1433,
    TrustServerCertificate = "yes"
  )
  print(.GlobalEnv$conn)
  NULL
})$stdout |> writeLines()
#> <OdbcConnection> dbo@df80b6ed0a1a
#>   Database: test
#>   Microsoft SQL Server Version: 16.00.1000

r_2$run_with_output(function() {
  .GlobalEnv$conn <- DBI::dbConnect(
    odbc::odbc(),
    driver = "ODBC Driver 18 for SQL Server",
    server = Sys.getenv("DM_TEST_DOCKER_HOST"),
    database = "test",
    uid = "SA",
    pwd = "YourStrong!Passw0rd",
    port = 1433,
    TrustServerCertificate = "yes"
  )
  print(.GlobalEnv$conn)
  NULL
})$stdout |> writeLines()
#> <OdbcConnection> dbo@df80b6ed0a1a
#>   Database: test
#>   Microsoft SQL Server Version: 16.00.1000

# Baseline
r_1$run(function() {
  DBI::dbExistsTable(.GlobalEnv$conn, "#test")
})
#> [1] FALSE

r_2$run(function() {
  DBI::dbExistsTable(.GlobalEnv$conn, "#test")
})
#> [1] FALSE

# Create table in first session
r_1$run(function() {
  DBI::dbWriteTable(.GlobalEnv$conn, "#test", data.frame(a = 1))
})
#> [1] TRUE

# Create table in second session fails
r_2$run_with_output(function() {
  DBI::dbWriteTable(.GlobalEnv$conn, "#test", data.frame(a = 2))
})$error
#> <callr_error/rlib_error_3_0/rlib_error/error>
#> Error: 
#> ! in callr subprocess.
#> Caused by error: 
#> ! Table #test exists in database, and both overwrite and append are FALSE
#> ---
#> Subprocess backtrace:
#> 1. DBI::dbWriteTable(.GlobalEnv$conn, "#test", data.frame(a = 2))
#> 2. DBI::dbWriteTable(.GlobalEnv$conn, "#test", data.frame(a = 2)) at 13-dbWriteTable.R:49:9
#> 3. local .local(conn, name, value, ...)
#> 4. base::stop("Table ", toString(name), " exists in database, and both overwrite an…
#> 5. | base::.handleSimpleError(function (e) …
#> 6. global h(simpleError(msg, call))

# Cause of failure (but not the core reason)
r_2$run(function() {
  DBI::dbExistsTable(.GlobalEnv$conn, "#test")
})
#> [1] TRUE

# Succeeds but no effect (need immediate = TRUE)
r_2$run(function() {
  DBI::dbCreateTable(.GlobalEnv$conn, "#test", data.frame(a = 2))
  DBI::dbCreateTable(.GlobalEnv$conn, "#test", data.frame(a = 2))
})
#> [1] TRUE

# Succeeds, finally
r_2$run(function() {
  DBI::dbExecute(.GlobalEnv$conn, "CREATE TABLE #test (a NUMERIC)", immediate = TRUE)
})
#> [1] 0

r_2$run(function() {
  DBI::dbAppendTable(.GlobalEnv$conn, "#test", data.frame(a = 2))
})
#> [1] NA

r_1$run(function() {
  DBI::dbReadTable(.GlobalEnv$conn, "#test")
})
#>   a
#> 1 1

r_2$run(function() {
  DBI::dbReadTable(.GlobalEnv$conn, "#test")
})
#>   a
#> 1 2

Created on 2024-01-08 with reprex v2.0.2

@krlmlr krlmlr added bug an unexpected problem or unintended behavior mssql Microsoft SQL Server labels Jan 8, 2024
@meztez
Copy link
Contributor

meztez commented Jan 11, 2024

We encountered the same issue today. @krlmlr before I submit a fix using the session id at the end of the pattern, were you working on a fix on your side?

Thanks

@krlmlr
Copy link
Member Author

krlmlr commented Jan 11, 2024

Not working on a fix, happy to review a PR.

@meztez
Copy link
Contributor

meztez commented Jan 11, 2024

@krlmlr beside "the fix works on my pc", let me know if this could be a possible solution.

@hadley
Copy link
Member

hadley commented Jan 12, 2024

Fixed by #724

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior mssql Microsoft SQL Server
Projects
None yet
Development

No branches or pull requests

3 participants