Repository navigation
Bracket-quote the database name in SQL Server catalog queries - #14
Open
maharanay22 wants to merge 1 commit into
Open
maharanay22 wants to merge 1 commit into
maharanay22 wants to merge 1 commit into
Conversation
get_tables() and the meta_get_* methods put the configured database name into the query as a bare three-part-name prefix, for example FROM sqldb-prod-eastus.INFORMATION_SCHEMA.TABLES. SQL Server can't parse a bare name that contains '-' (common for Azure SQL databases), spaces or other special characters, so listing tables and loading the data catalog failed for those databases. Quote the name as a T-SQL bracket identifier, escaping ']' as ']]'.
8 tasks done
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The SQL Server handler's
get_tables()and all of the data catalog methods (meta_get_tables,meta_get_columns,meta_get_column_statistics,meta_get_primary_keys,meta_get_foreign_keys) put the configured database name into the query as a bare prefix of a three-part name:A bare T-SQL identifier can't contain
-, spaces and most other special characters. Hyphenated names are very common for Azure SQL databases (for examplesqldb-prod-eastus), and SQL Server rejects these queries with a syntax error. So for those databases, listing tables (for exampleSHOW TABLES) and loading the data catalog fail, while regular queries (which don't use the prefix) keep workingThis change quotes the database name as a T-SQL bracket identifier (
[sqldb-prod-eastus]) in all of those queries, escaping]as]]so that a name containing]can't break out of the quotes. Brackets work for every database name, including plain ones likeexample_db, so nothing changes for databases whose names were already valid bare identifiersFixes: no linked issue, because issues are turned off for this repository. The bug is described in full above
Type of change
Verification Process
To ensure the changes are working as expected:
tests/unit/handlers/test_mssql.py, new testTestMSSQLHandler::test_database_name_is_bracket_quoted(covers all six methods) and updatedtest_get_tablessqldb-prod-eastusand parse it with sqlglot's T-SQL dialect. I didn't have a SQL Server instance, so sqlglot stands in for the server's parser. The same queries withexample_dbparse fine on main, so the failures below come only from the database nameBefore (current main):
After:
A name with
]in it, such asodd]name, renders as[odd]]name], and sqlglot reads the catalog name back asodd]nameUnit tests. The new test fails on main for all six methods and passes with the fix:
ruff checkandruff format --check(0.11.11) pass on the changed files. No dependencies changedAdditional Media:
I didn't record a video or take screenshots. The before and after terminal output above shows the change instead
Checklist:
No documentation change is needed
I used an AI assistant to help investigate this and draft the change and tests, and I reviewed and tested it myself