-
Notifications
You must be signed in to change notification settings - Fork 292
Support for Partial Indexes (Filtered Indexes) #5749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Partial Indexes (Filtered Indexes) #5749
Conversation
af7a275 to
f59ae96
Compare
|
@jacek-prisma Could you please review this PR? |
6f861f6 to
f4647c7
Compare
|
@jkomyno Hi, Could you please review this PR? |
|
This PR has passed all tests that can be run locally, including tests against the real database and the shadow database. Clippy and fmt have both been applied. I’d like to run the GitHub Actions workflow. |
|
@aqrln could you review this PR? I hope it pass review |
Merging this PR will not alter performance
Comparing Footnotes
|
|
Okay, I can see that some tests have failed. I’ll fix the problem! |
|
@jacek-prisma Added missing predicate: None to sql-schema-describer test snapshots. The Index struct gained a new field in the partial index commit, which required snapshot updates. Please re-run the tests! Thank you 👍 |
2ce8b62 to
9c97582
Compare
|
I have also added tests to verify partial indexes (filtered indexes) are correctly described for PostgreSQL, SQLite, MSSQL, and CockroachDB. |
schema-engine/connectors/sql-schema-connector/src/flavour/mssql/schema_calculator.rs
Outdated
Show resolved
Hide resolved
schema-engine/connectors/sql-schema-connector/src/introspection/introspection_pair/index.rs
Outdated
Show resolved
Hide resolved
59a313b to
53adf6c
Compare
|
@jacek-prisma The tests I previously added to Thanks! |
I've pushed to my branch and all CI tests are passing now, including the Schema Engine tests. |
schema-engine/connectors/sql-schema-connector/src/flavour/mssql/schema_calculator.rs
Outdated
Show resolved
Hide resolved
|
Thanks for addressing my comments @jay-l-e-e , I left 2 additional comments about 2 edge cases that I think still need to be handled |
|
@jacek-prisma Hi, I've addressed all the review comments you left. I also added unit tests for the changes and verified they pass. Would you mind running the CI workflow when you get a chance? Thanks. |
Add support for partial indexes with WHERE clauses in Prisma Schema Language. Features: - New raw() syntax for WHERE clauses: @@unique([email], where: raw("status = 'active'")) - Object literal syntax for conditions: @@unique([email], where: { active: true }) - Supports: boolean (true/false), null, string, and number values - Conditions: equals, not equals (!=), IS NULL, IS NOT NULL - Support for PostgreSQL, SQLite, SQL Server (filtered indexes), and CockroachDB - Preview feature flag: partialIndexes - Auto-injection of preview feature during introspection Schema Engine: - SQL generation for partial indexes across all supported databases - Migration diffing with predicate comparison - Introspection of existing partial indexes - CockroachDB limitation handling (cannot introspect predicate text) SQL Server specifics: - Filtered unique indexes created via CREATE INDEX (not table constraints) - Predicate normalization for consistent diffing Tests: - Comprehensive migration tests for all supported databases - Introspection tests for PostgreSQL, SQLite, SQL Server, and CockroachDB
Add missing 'predicate: None' field to Index struct expectations in sql-schema-describer tests. This field was added in the partial index support commit but the test snapshots were not updated.
… databases Added tests to verify partial indexes (filtered indexes) are correctly described for PostgreSQL, SQLite, MSSQL, and CockroachDB.
…QL identifier replacement & fmt
…d name in where_clause_as_sql
…al index where clauses
… and move SQL rendering to walker_ext_traits
…d use structural assertions in PSL tests
c791e88 to
bac37d2
Compare
|
Hi @jay-l-e-e |
|
Hi @jacek-prisma, I’m really glad to hear that the PR may be merged soon. Thank you very much. |
|
@jacek-prisma Hmm, a new PR was just merged and it caused a conflict. It seems to be because the version of sqlparser in the PR you recently merged was bumped to 0.60. While I was working, I had considered upgrading sqlparser to 0.60 as well (since version 0.60 includes support for predicates), but I ended up not proceeding with it. Would you like me to update my changes to align with version 0.60? |
|
Oh, I see you’re already working on resolving it. I’ll step back and leave it to you. |
…support Add comprehensive documentation for the new `partialIndexes` preview feature, which enables `where` argument on `@@index`, `@@unique`, and `@unique` attributes for PostgreSQL, SQLite, SQL Server, and CockroachDB. Ref: prisma/prisma-engines#5749, prisma/prisma#6974
…support Add comprehensive documentation for the new `partialIndexes` preview feature, which enables `where` argument on `@@index`, `@@unique`, and `@unique` attributes for PostgreSQL, SQLite, SQL Server, and CockroachDB. Ref: prisma/prisma-engines#5749, prisma/prisma#6974
Summary
This PR implements support for partial indexes (also known as filtered indexes) in Prisma Schema Language. Partial indexes allow creating indexes that only include rows matching a specific condition, reducing index size and improving query performance.
There was an issue related to this PR that was 5 years old. 😱
New Syntax
Raw SQL Syntax (all supported databases)
Object Literal Syntax (type-safe alternative)
Preview Feature
This feature is gated behind the
partialIndexespreview feature:Supported Databases
CREATE INDEXCockroachDB Limitation
CockroachDB supports creating partial indexes but cannot introspect predicate text. This means:
add/modify/removepredicate migrations don't workThe differ skips predicate comparison for CockroachDB to prevent false-positive migrations.
Changes
PSL Parser (
psl/)object_expressionandobject_memberrulesExpression::Objectvariant andObjectMemberstructWhereClause,WhereCondition,WhereFieldCondition,WhereValueenumspartial_index_is_supportedvalidation requiring preview featurePartialIndexconnector capability for PostgreSQL, SQLite, CockroachDB, SQL ServerSchema Engine (
schema-engine/)CREATE INDEX ... WHEREfor all supported databasespredicates_match()trait method toSqlSchemaDifferFlavourpartialIndexespreview feature when partial indexes detectedSQL Server Specifics
CREATE INDEX(not as table constraints)render_create_table_asto exclude filtered unique indexes from constraintsfilter_definitioncolumn to index introspection querySQL DDL (
libs/sql-ddl/)WhereClausesupport toCreateIndexfor PostgreSQLCreatePartialIndexfor SQLiteTest Coverage
PSL Unit Tests (
psl/psl/tests/attributes/partial_index.rs)@@uniqueand@@index{ not: true/false/null }syntaxMigration Tests (
schema-engine/sql-migration-tests/)Total: 21 migration tests
Introspection Tests (
schema-engine/sql-introspection-tests/)@uniquewithwhereclauseGenerated SQL Examples
PostgreSQL
SQLite
SQL Server
CockroachDB
Key Files
psl/schema-ast/src/parser/datamodel.pestpsl/schema-ast/src/ast/expression.rsExpression::ObjectAST typepsl/parser-database/src/attributes.rswhereclause parsing logicpsl/parser-database/src/walkers/index.rspsl/psl-core/src/common/preview_features.rsPartialIndexesfeature flagpsl/psl-core/src/builtin_connectors/mssql_datamodel_connector.rsPartialIndexcapabilityschema-engine/.../sql_schema_calculator.rsschema-engine/.../sql_schema_differ/table.rsschema-engine/.../sql_schema_differ_flavour.rspredicates_match()trait methodschema-engine/.../flavour/postgres/schema_differ.rsschema-engine/.../flavour/mssql/renderer.rsschema-engine/.../flavour/mssql/schema_differ.rsschema-engine/sql-schema-describer/src/mssql.rsschema-engine/.../introspection/Related
Checklist