Skip to content

MSSQL dialect incorrectly detects temp tables from different sessions. #6910

Closed
@ewallace-RI

Description

@ewallace-RI

Describe the bug

The MSDialect.has_table method handles temp tables with a query like

 select from tempdb.INFORMATION_SCHEMA.columns where table_name like '{table_name}___%'

If there is another active session using a temp table of the same name, this version of has_table() returns True, even though there is no such table available to this session.

To Reproduce

con1 = engine.connect()
con1.execute('create table #myveryveryuniquetemptablename (a int)')
con2 = engine.connect()
print(con2.dialect.has_table(con2, '#myveryveryuniquetemptablename'))
con1.close()
con2.close()

Error

Above snippet prints True. Correct behavior would be False.

Versions

  • OS: Windows
  • Python: 3.9
  • SQLAlchemy: 1.4.23
  • Database: MS SQL Server
  • DBAPI: pyodbc 4.0.31

Additional context

No response

Activity

ewallace-RI

ewallace-RI commented on Aug 19, 2021

@ewallace-RI
Author

Introduced by the fix for #5597.

CaselIT

CaselIT commented on Aug 19, 2021

@CaselIT
Member

Thanks for reporting.

Do you have a suggestion regarding what's the correct query to do instead?

added
bugSomething isn't working
reflectionreflection of tables, columns, constraints, defaults, sequences, views, everything else
SQL ServerMicrosoft SQL Server, e.g. mssql
and removed
requires triageNew issue that requires categorization
on Aug 19, 2021
CaselIT

CaselIT commented on Aug 19, 2021

@CaselIT
Member
ewallace-RI

ewallace-RI commented on Aug 19, 2021

@ewallace-RI
Author

Sorry, should have included the suggested fix. I use something like this, which works for both temp tables and physical tables.

    db = 'tempdb.' if tname.startswith('#') else ''
    t_id, = con.execute(f"select object_id('{dbname}dbo.{tname}')").fetchone()
    return t_id is not None

Not sure if there's some other preferred method.

self-assigned this
on Aug 19, 2021
sqla-tester

sqla-tester commented on Aug 20, 2021

@sqla-tester
Collaborator

Gord Thompson has proposed a fix for this issue in the master branch:

Fix has_table() to exclude other sessions' temp tables https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/3014

zzzeek

zzzeek commented on Aug 20, 2021

@zzzeek
Member

I noted on the gerrit that the above fix should not apply to global temporary tables that start with "##".

ewallace-RI

ewallace-RI commented on Aug 20, 2021

@ewallace-RI
Author

@zzzeek I think the attached fix works for global temp tables. Temp tables from the current session and ##global temp tables will both have non-null object_ids, so this version of has_table will return True for both of them, as expected. And the _temp_table_name_like_pattern logic is unaffected.

@gordthompson I also noticed when looking into this that the _temp_table_name_like_pattern doesn't seem to be quite right. The unquoted underscore is a single-character wildcard in a like pattern, so '#tmp___%' will also match a table named '#tmp123'. The object_id logic should do the right thing, so I think the proposed fix also takes care of that, just something to be aware of.

zzzeek

zzzeek commented on Aug 20, 2021

@zzzeek
Member

@zzzeek I think the attached fix works for global temp tables. Temp tables from the current session and ##global temp tables will both have non-null object_ids, so this version of has_table will return True for both of them, as expected. And the _temp_table_name_like_pattern logic is unaffected.

oh, good idea.

gordthompson

gordthompson commented on Aug 20, 2021

@gordthompson
Member

The unquoted underscore is a single-character wildcard in a like pattern

Good point. Thanks for mentioning it!

8 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

SQL ServerMicrosoft SQL Server, e.g. mssqlbugSomething isn't workingreflectionreflection of tables, columns, constraints, defaults, sequences, views, everything else

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @zzzeek@sqla-tester@gordthompson@CaselIT@marzlevane

      Issue actions

        MSSQL dialect incorrectly detects temp tables from different sessions. · Issue #6910 · sqlalchemy/sqlalchemy