Skip to content

Add mssql-python as an optional DBAPI driver for MsSqlHook - #74539

Open
RehanAhmad25 wants to merge 2 commits into
apache:mainfrom
RehanAhmad25:feature/74432-mssql-python-driver
Open

RehanAhmad25 wants to merge 2 commits into
apache:mainfrom
RehanAhmad25:feature/74432-mssql-python-driver

Conversation

@RehanAhmad25

Copy link
Copy Markdown

closes: #74432

What

MsSqlHook can now connect with Microsoft's mssql-python driver instead of pymssql. The driver is chosen with a new dbapi_driver connection extra (pymssql or mssql_python, case-insensitive). pymssql stays the default, so existing connections behave exactly as before. An unsupported value raises a ValueError.

How it works

With dbapi_driver set to mssql_python:

  • get_conn() builds the connection with mssql_python.connect(...). The host and port are sent as Server=host,port, and the schema, login and password map to Database, UID and PWD.
  • Other connection extras (for example Encrypt, TrustServerCertificate, Authentication) are passed to the driver as keyword arguments. The hook's own extras (dbapi_driver, sqlalchemy_scheme, placeholder and the other DbApiHook format extras) are not passed on, and empty values are dropped.
  • The SQLAlchemy scheme defaults to mssql+mssqlpython. sqlalchemy_scheme still overrides it. get_uri() strips dbapi_driver from the query string.
  • The default placeholder is ?, because mssql-python does not accept a plain %s. The placeholder extra still overrides it.
  • Result rows are converted to plain tuples, since mssql-python returns its own Row objects.
  • Autocommit goes through the base class behaviour, since the driver exposes a settable autocommit property.
  • If the package is missing, the error tells the user to install the mssql-python extra.

pymssql is now imported inside the pymssql code path, not at module level. It is still a required dependency of the provider. Making it optional would be a separate change.

Where

  • providers/microsoft/mssql/src/airflow/providers/microsoft/mssql/hooks/mssql.py: the hook changes
  • providers/microsoft/mssql/tests/unit/microsoft/mssql/hooks/test_mssql.py: new tests
  • providers/microsoft/mssql/docs/connections/mssql.rst: new "Choosing the DBAPI driver" section
  • providers/microsoft/mssql/pyproject.toml: new optional extra mssql-python (mssql-python>=1.15.0)
  • providers/microsoft/mssql/docs/index.rst: extras table, updated by the repo's hook
  • uv.lock: regenerated with uv 0.13.0 for the new extra (adds mssql-python and mssql-python-odbc)

Notes for reviewers

  • I picked >=1.15.0 as the floor because it is the version I checked the driver's behaviour against. Happy to loosen it if you prefer.
  • The SQLAlchemy dialect for mssql-python needs SQLAlchemy 2.1 or newer, and the docs say so.

Testing

I added 14 new test cases for driver selection and validation, scheme and placeholder defaults, get_uri, the arguments passed to connect, the missing-package error, autocommit and row conversion. The existing pymssql tests pass unchanged (35 passed in Breeze). The prek hooks, including ruff, mypy for providers and the provider.yaml check, pass locally.

Both drivers are mocked in the tests, so I have not run this against a real SQL Server yet. The Microsoft Entra ID Authentication options in particular are untested, so a check on a real instance would be welcome.

Related: #72000


Was generative AI tooling used to co-author this PR?
  • Yes (Claude)
    Generated-by: Claude (Anthropic) following the guidelines

Comment thread providers/microsoft/mssql/docs/connections/mssql.rst Outdated
Comment thread providers/microsoft/mssql/src/airflow/providers/microsoft/mssql/hooks/mssql.py Outdated
Comment thread providers/microsoft/mssql/tests/unit/microsoft/mssql/hooks/test_mssql.py Outdated
Comment thread providers/microsoft/mssql/tests/unit/microsoft/mssql/hooks/test_mssql.py Outdated
@RehanAhmad25
RehanAhmad25 force-pushed the feature/74432-mssql-python-driver branch from bce8d0d to dc2a3b0 Compare October 11, 2026 03:42

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add mssql_python to the mssql provider

2 participants