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

fix(mssql): fix temporary table creation and implement cache #9434

Merged
merged 1 commit into from
Jun 24, 2024

Conversation

gforsyth
Copy link
Member

MSSQL has two kinds of temporary tables.

  1. Local temporary tables, created in tempdb.dbo, prefixed with a
    single # and suffixed with ~~112 underscores and a number.
  2. Global temporary tables, created in tempdb.dbo, and prefixed with a
    double #.

The suffixing on local temporary tables is to avoid name collisions
between separate user sessions trying to create the same temporary table
name.
Otherwise, the two types of temporary tables are functionally
equivalent. Users need specific access to look at temporary tables that
they didn't create, and all temp tables are cleaned up after the session
closes and all stored procedures referencing those tables have run.

Because of the slightly wacky semantics of local temporary tables, this
PR makes all temp tables created by the MSSQL backend "global". This
makes the names much more predictable.

A user creating a temporary table with the name "hithere", will find
that there is no table called "hithere" in the output of
con.list_tables(). Instead, they will find a table called "##hithere"
if they run con.list_tables(database=("tempdb", "dbo")).

The returned table reference from the create_table call works the same
as any other table reference, and you can create persistent tables from
temporary tables. The only major hiccough is the perpending of the ##
on the table name, but that is the behavior of MSSQL.

I've also added in support for passing in explicit catalog and
database to create_table, which was missing (I believe this was an
oversight, since MSSQL definitely supports this).

Resolves #9431

@gforsyth gforsyth added the mssql The Microsoft SQL Server backend label Jun 24, 2024
@cpcloud cpcloud added this to the 9.2 milestone Jun 24, 2024
MSSQL has two kinds of temporary tables.

1. Local temporary tables, created in `tempdb.dbo`, prefixed with a
single `#` and suffixed with ~~112 underscores and a number.
2. Global temporary tables, created in `tempdb.dbo`, and prefixed with a
double `#`.

The suffixing on local temporary tables is to avoid name collisions
between separate user sessions trying to create the same temporary table
name.
Otherwise, the two types of temporary tables are functionally
equivalent. Users need specific access to look at temporary tables that
they didn't create, and all temp tables are cleaned up after the session
closes and all stored procedures referencing those tables have run.

Because of the slightly wacky semantics of local temporary tables, this
PR makes all temp tables created by the MSSQL backend "global". This
makes the names much more predictable.

A user creating a temporary table with the name "hithere", will find
that there is no table called "hithere" in the output of
`con.list_tables()`. Instead, they will find a table called "##hithere"
if they run `con.list_tables(database=("tempdb", "dbo"))`.

The returned table reference from the `create_table` call works the same
as any other table reference, and you can create persistent tables from
temporary tables.  The only major hiccough is the perpending of the `##`
on the table name, but that is the behavior of MSSQL.

I've also added in support for passing in explicit `catalog` and
`database` to `create_table`, which was missing (I believe this was an
oversight, since MSSQL definitely supports this).
@cpcloud cpcloud added the bug Incorrect behavior inside of ibis label Jun 24, 2024
@cpcloud cpcloud merged commit 196d8a1 into ibis-project:main Jun 24, 2024
78 checks passed
@gforsyth gforsyth deleted the mssql_temp_times branch June 25, 2024 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect behavior inside of ibis mssql The Microsoft SQL Server backend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: mssql backend create_table with temp=True get incorrect syntax exception
2 participants