From: mollardthomas Date: Fri, 3 May 2019 15:31:57 +0000 (-0400) Subject: Add support for filtered indexes for mssql dialect X-Git-Tag: rel_1_3_4~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2512211e324e6c4663ad879e41406d6764453b07;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add support for filtered indexes for mssql dialect Added support for SQL Server filtered indexes, via the ``mssql_where`` parameter which works similarly to that of the ``postgresql_where`` index function in the PostgreSQL dialect. Fixes: #4657 Closes: #4658 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/4658 Pull-request-sha: cf609c19bccc74c0dba38d2fc4976df3a205f3f6 Change-Id: I9c61b97d0b0cb6f6d417da7b1875b40f8f918a3c (cherry picked from commit 174f2ae33b5235d709b17f3371946a173defaa0d) --- diff --git a/doc/build/changelog/unreleased_13/4657.rst b/doc/build/changelog/unreleased_13/4657.rst new file mode 100644 index 0000000000..b51d8652f2 --- /dev/null +++ b/doc/build/changelog/unreleased_13/4657.rst @@ -0,0 +1,11 @@ +.. change:: + :tags: feature, mssql + :tickets: 4657 + + Added support for SQL Server filtered indexes, via the ``mssql_where`` + parameter which works similarly to that of the ``postgresql_where`` index + function in the PostgreSQL dialect. + + .. seealso:: + + :ref:`mssql_index_where` \ No newline at end of file diff --git a/lib/sqlalchemy/dialects/mssql/base.py b/lib/sqlalchemy/dialects/mssql/base.py index 507fcfdcb5..d2c84f446a 100644 --- a/lib/sqlalchemy/dialects/mssql/base.py +++ b/lib/sqlalchemy/dialects/mssql/base.py @@ -540,6 +540,20 @@ names:: would render the index as ``CREATE INDEX my_index ON table (x) INCLUDE (y)`` +.. _mssql_index_where: + +Filtered Indexes +^^^^^^^^^^^^^^^^ + +The ``mssql_where`` option renders WHERE(condition) for the given string +names:: + + Index("my_index", table.c.x, mssql_where=table.c.x > 10) + +would render the index as ``CREATE INDEX my_index ON table (x) WHERE x > 10``. + +.. versionadded:: 1.3.4 + Index ordering ^^^^^^^^^^^^^^ @@ -1950,6 +1964,14 @@ class MSDDLCompiler(compiler.DDLCompiler): ), ) + whereclause = index.dialect_options["mssql"]["where"] + + if whereclause is not None: + where_compiled = self.sql_compiler.process( + whereclause, include_table=False, literal_binds=True + ) + text += " WHERE " + where_compiled + # handle other included columns if index.dialect_options["mssql"]["include"]: inclusions = [ @@ -2182,7 +2204,7 @@ class MSDialect(default.DefaultDialect): construct_arguments = [ (sa_schema.PrimaryKeyConstraint, {"clustered": None}), (sa_schema.UniqueConstraint, {"clustered": None}), - (sa_schema.Index, {"clustered": None, "include": None}), + (sa_schema.Index, {"clustered": None, "include": None, "where": None}), (sa_schema.Column, {"identity_start": 1, "identity_increment": 1}), ] diff --git a/test/dialect/mssql/test_compiler.py b/test/dialect/mssql/test_compiler.py index 339cc75907..30a11d16b2 100644 --- a/test/dialect/mssql/test_compiler.py +++ b/test/dialect/mssql/test_compiler.py @@ -1129,6 +1129,15 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): schema.CreateIndex(idx), "CREATE CLUSTERED INDEX foo ON test (id)" ) + def test_index_where(self): + metadata = MetaData() + tbl = Table("test", metadata, Column("data", Integer)) + idx = Index("test_idx_data_1", tbl.c.data, mssql_where=tbl.c.data > 1) + self.assert_compile( + schema.CreateIndex(idx), + "CREATE INDEX test_idx_data_1 ON test (data) WHERE data > 1" + ) + def test_index_ordering(self): metadata = MetaData() tbl = Table(