Skip to content

Add support for SQL Server filtered indexes#5504

Closed
RamonWill wants to merge 6 commits intosqlalchemy:masterfrom
RamonWill:issue4966
Closed

Add support for SQL Server filtered indexes#5504
RamonWill wants to merge 6 commits intosqlalchemy:masterfrom
RamonWill:issue4966

Conversation

@RamonWill
Copy link
Copy Markdown
Contributor

@RamonWill RamonWill commented Aug 10, 2020

A user requested that the filter definition for Filtered Indexes (MSSQL) are reflected. This change will reflect that. The presence of these filtered definitions will mean that the index is filtered. I have also made this change for PostgreSQL (where they are called Partial Indexes).

Description

I have added a new column in the SELECT clause in Dialect.get_indexes() that will show the filter definition for the index if it exists. This will then be reflected under the dialect_options as postgresql_where or mssql_where depending on the dialect.

This pull request is:

  • A documentation / typographical error fix
    • Good to go, no issue or tests are needed
  • [x ] A short code fix
    • please include the issue number, and create an issue if none exists, which
      must include a complete example of the issue. one line code fixes without an
      issue and demonstration will not be accepted.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests. one line code fixes without tests will not be accepted.
  • A new feature implementation
    • please include the issue number, and create an issue if none exists, which must
      include a complete example of how the feature would look.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests.

Have a nice day!
Fixes: #4966

@zzzeek zzzeek requested a review from sqla-tester August 11, 2020 14:05
Copy link
Copy Markdown
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this is sqla-tester setting up my work on behalf of zzzeek to try to get revision 01d1def of this pull request into gerrit so we can run tests and reviews and stuff

@sqla-tester
Copy link
Copy Markdown
Collaborator

New Gerrit review created for change 01d1def: https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

@zzzeek
Copy link
Copy Markdown
Member

zzzeek commented Aug 11, 2020

nice, let's try it out.

Copy link
Copy Markdown
Member

@CaselIT CaselIT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@@ -0,0 +1,7 @@
.. change::
:tags: mssql, usecase
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing postgres

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted, changes now made. Thanks

Comment thread lib/sqlalchemy/dialects/mssql/base.py Outdated
"name": row["name"],
"unique": row["is_unique"] == 1,
"column_names": [],
"dialect_options": {"mssql_where": row["filter_definition"]},
Copy link
Copy Markdown
Member

@CaselIT CaselIT Aug 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't dialect_options be added only when needed like in postgres? for consistency I guess

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @CaselIT I have now amended the code to only include dialect_options when needed

@zzzeek
Copy link
Copy Markdown
Member

zzzeek commented Aug 11, 2020

hi @RamonWill you should be able to go to "reviewers" and "request a review" from "sqla-tester" - that kicks off a new build.

@RamonWill RamonWill requested a review from sqla-tester August 11, 2020 22:23
Copy link
Copy Markdown
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this is sqla-tester setting up my work on behalf of RamonWill to try to get revision 75c6caa of this pull request into gerrit so we can run tests and reviews and stuff

@sqla-tester
Copy link
Copy Markdown
Collaborator

Patchset 75c6caa added to existing Gerrit review https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

@RamonWill
Copy link
Copy Markdown
Contributor Author

My apologies the name "Add error message for lookup" was supposed to be on another issue i have just replace it. the most recent commit is the one that says: "edit changelog tag and amend to only include dialect_options when needed"

@zzzeek
Copy link
Copy Markdown
Member

zzzeek commented Aug 12, 2020

ping sqla-tester as a reviewer and the bot will squash your changes into that gerrit.

@RamonWill RamonWill requested a review from sqla-tester August 12, 2020 18:47
Copy link
Copy Markdown
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this is sqla-tester setting up my work on behalf of RamonWill to try to get revision b3018ba of this pull request into gerrit so we can run tests and reviews and stuff

@sqla-tester
Copy link
Copy Markdown
Collaborator

Patchset b3018ba added to existing Gerrit review https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

@sqla-tester
Copy link
Copy Markdown
Collaborator

Ramon Williams (RamonWill) wrote:

sorry for the noise caused by this. I believe i would have to add you manually as a reviewer

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

Copy link
Copy Markdown
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mike bayer (zzzeek) wrote:

(13 comments)

OK here's a bunch of review. thanks! looks great

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

  • /COMMIT_MSG (line 7): "Add support for SQL Server filtered indexes"
  • /COMMIT_MSG (line 14): the "### Checklist" here can be removed
  • /COMMIT_MSG (line 33): This seems to work though I'm used to seeing it like:

"Fixes: #4966"

  • lib/sqlalchemy/dialects/postgresql/base.py (line 3448): and we can remove "prd" here....
  • lib/sqlalchemy/dialects/postgresql/base.py (line 3469): ... and this conditional can be removed. unless something breaks with the sv_idx_name thing, which I'm not totally sure what that is for at the moment.

@@ -3402,6 +3402,7 @@ def get_indexes(self, connection, table_name, schema, **kw):
ix.indisunique, ix.indexprs, ix.indpred,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mike bayer (zzzeek) wrote:

we are now looking at ix.indpred in the pg_get_expr() function below, so I think we can remove ix.indpred here....

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

eq_(set(list(t2.indexes)[0].columns), set([t2.c["x col"], t2.c.y]))

@testing.provide_metadata
def test_indexes_with_filtered(self):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mike bayer (zzzeek) wrote:

there's a "connection" fixture you can use now:

def test_indexes_with_filtered(self, conneciton):

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ramon Williams (RamonWill) wrote:

Done

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

)
Index("idx_x", t1.c.x, mssql_where=t1.c.x == "test")
Index("idx_y", t1.c.y, mssql_where=t1.c.y >= 5)
metadata.create_all()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mike bayer (zzzeek) wrote:

metadata.create_all(connection)

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ramon Williams (RamonWill) wrote:

Done

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

Index("idx_x", t1.c.x, mssql_where=t1.c.x == "test")
Index("idx_y", t1.c.y, mssql_where=t1.c.y >= 5)
metadata.create_all()
with testing.db.connect() as conn:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mike bayer (zzzeek) wrote:

use the "connection" here

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ramon Williams (RamonWill) wrote:

Done

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

)

@testing.provide_metadata
def test_index_reflection_partial(self):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mike bayer (zzzeek) wrote:

use connection fixture:

def test_index_reflection_partial(self, connection):

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ramon Williams (RamonWill) wrote:

Done

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

Column("x", Integer),
)
metadata.create_all()
with testing.db.connect().execution_options(autocommit=True) as conn:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mike bayer (zzzeek) wrote:

use connection, don't need autocommit=True as that's deprecated anyway

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ramon Williams (RamonWill) wrote:

Done

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

)
metadata.create_all()
with testing.db.connect().execution_options(autocommit=True) as conn:
conn.exec_driver_sql("create index idx1 on table1 ((id || name))")
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mike bayer (zzzeek) wrote:

do we need "idx1" in this test? we don't seem to be testing the expression based portion here and there are other tests for the "skipped unsupported reflection" warning below.

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

with testing.expect_warnings(
"Skipped unsupported reflection of "
"expression-based index idx1",
"Predicate of partial index idx2 ignored during reflection",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mike bayer (zzzeek) wrote:

the "predicate" warning can be removed, because that's what we're reflecting now

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

@RamonWill RamonWill changed the title fixes #4966 Add support for SQL Server filtered indexes Aug 13, 2020
Copy link
Copy Markdown
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ramon Williams (RamonWill) wrote:

(9 comments)

Hi Mike,

I've made changes locally I just have a question in regards to the conditional statement on Line 3469 in lib/sqlalchemy/dialects/postgresql/base.py.

Thank you for the review,
Ramon

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

  • /COMMIT_MSG (line 7): Done
  • /COMMIT_MSG (line 14): Done
  • /COMMIT_MSG (line 33): Done
  • lib/sqlalchemy/dialects/postgresql/base.py (line 3469): Hey Mike,
    So I've made all the changes from this review locally and they work fine. However, the change here causes test_index_reflection (on line 882, test\dialect\postgresql\test_reflection.py) to fail, since the warning below wont be raised. Shall i amend that test to accomodate this change?

@sqla-tester
Copy link
Copy Markdown
Collaborator

mike bayer (zzzeek) wrote:

Workflow+1

i think you meant to workflow +1 this

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

@zzzeek zzzeek requested a review from sqla-tester August 20, 2020 19:05
Copy link
Copy Markdown
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this is sqla-tester setting up my work on behalf of zzzeek to try to get revision b3018ba of this pull request into gerrit so we can run tests and reviews and stuff

@sqla-tester
Copy link
Copy Markdown
Collaborator

Patchset b3018ba added to existing Gerrit review https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

Copy link
Copy Markdown
Collaborator

@sqla-tester sqla-tester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mike bayer (zzzeek) wrote:

(1 comment)

OK we can always pull whatever changes you've made into gerrit here, this is where we review them in any case.

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

  • lib/sqlalchemy/dialects/postgresql/base.py (line 3469): yes, if we are actually implementing Postgresql reflection of expression-based indexes, then we are no longer skipping them and the warning can be removed, as well as the assertions in the test that were checking for that warning.

@sqla-tester
Copy link
Copy Markdown
Collaborator

Ramon Williams (RamonWill) wrote:

Workflow+1

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

@sqla-tester
Copy link
Copy Markdown
Collaborator

mike bayer (zzzeek) wrote:

Code-Review+2 Workflow+2

View this in Gerrit at https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142

@sqla-tester
Copy link
Copy Markdown
Collaborator

Gerrit review https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2142 has been merged. Congratulations! :)

@RamonWill RamonWill deleted the issue4966 branch October 14, 2020 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding is_filtered_index for SQL Server dialect

4 participants