Skip to content
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

sql: include partial index predicates in pg_catalog and pg builtins #53967

Conversation

mgartner
Copy link
Collaborator

@mgartner mgartner commented Sep 4, 2020

logictest: make pg_catalog 46799 regression test more robust

This commit updates the regression_46799 subtest in pg_catalog logic
tests so that hard-coded relids are not used. Previously, every time a
new relation was added in the test file, this test would have to be
rewritten with new relids. Determining the new relid was a nuisance.
Now, this test should not require changes when other relations are added
or removed elsewhere in the file.

Release justification: This is a test-only, non-production code change.

Release note: None

sql: include partial index predicate in pg_catalog.pg_indexes

This commit adds the partial index predicate expression to the formatted
indexdef column of pg_catalog.pg_indexes. Also, the predicate is now
included in the output of the pg_get_indexdef builtin function.
Including the predicate is critical for compatibility with Postgres
ORMs.

Fixes #53843

Release justification: This is a low-risk change to new functionality,
partial indexes.

Release note: None

pkg: include partial index predicate expressions in SHOW CONSTRAINTS

This commit updates pg_catalog.pg_constraint.condef to include the
predicate of unique partial index constraints. Also, the predicate is
now included in the output of SHOW CONSTRAINTS and
pg_get_constraintdef().

Fixes #53908

Release justification: This is a low-risk change to new functionality,
partial indexes.

Release note: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

----
CREATE INDEX pg_indexdef_partial_idx ON test.public.pg_indexdef_test USING btree (a ASC) WHERE a > 0

# TODO(mgartner): To match Postgres, "::" casts should be appended to constants
Copy link
Collaborator

Choose a reason for hiding this comment

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

i noticed this too, for other pg_catalog columns. i have a change here (#53965) that does it, and it should be easy to do here too

//
// TODO(mgartner): Avoid the parsing he predicate expression twice. It
// is parsed in schemaexpr.FormatExprForDisplay and again here.
formattedPred, err := schemaexpr.FormatExprForDisplayWithoutTypeAnnotations(ctx, table, index.Predicate, p.SemaCtx())
Copy link
Collaborator

Choose a reason for hiding this comment

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

just a bit confused -- why can't formattedPred be used directly as the return value?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is confusing because this function converts an IndexDescriptor to a tree.CreateIndex. The tree.CreateIndex.Predicate field is a tree.Expr not a string. So we have to parse it again.

This is definitely not an ideal solution, but I think it's the most minimal one for now. In the long-term, I think it would be prudent to consider whether this conversion back into a tree.CreateIndex AST node is the right way to go about this. At the very least, the many code paths for formatting indexes should converge as much as possible.

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

LGTM, just had a small question. also depending on if this PR or #53965 is merged first, one of us will have to accommodate the others changes. which order of merging would you prefer? also, can i rope you into reviewing mine? 😄

@mgartner
Copy link
Collaborator Author

mgartner commented Sep 4, 2020

Let's get yours in first. I can rebase afterwards. I'll review it now.

@rafiss
Copy link
Collaborator

rafiss commented Sep 8, 2020

#53965 is merged, so if you rebase now, you should be able to address some of the TODOs you made by using schemaexpr.FormatExprForDisplay(..., tree.FmtPGCatalog). i think you can also get rid of tree.FmtPGIndexDef potentially, but i'll leave it to you to verify that.

This commit updates the `regression_46799` subtest in `pg_catalog` logic
tests so that hard-coded relids are not used. Previously, every time a
new relation was added in the test file, this test would have to be
rewritten with new relids. Determining the new relid was a nuisance.
Now, this test should not require changes when other relations are added
or removed elsewhere in the file.

Release justification: This is a test-only, non-production code change.

Release note: None
@mgartner mgartner force-pushed the 53843-include-partial-index-pred-in-pg-catalog branch 2 times, most recently from 0a4cd3e to 45df2ce Compare September 8, 2020 17:52
@mgartner
Copy link
Collaborator Author

mgartner commented Sep 8, 2020

@rafiss I've updated this so that casts are now included for string types, and I've removed tree.FmtPGIndexDef. Ready for another look.

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

excellent, lgtm!

Reviewed 2 of 5 files at r5, 4 of 4 files at r6.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @mgartner and @rafiss)

@mgartner mgartner changed the title sql: include output of partial index predicates in pg_catalog and pg builtins sql: include partial index predicates in pg_catalog and pg builtins Sep 8, 2020
This commit adds the partial index predicate expression to the formatted
`indexdef` column of `pg_catalog.pg_indexes`. Also, the predicate is now
included in the output of the `pg_get_indexdef` builtin function.
Including the predicate is critical for compatibility with Postgres
ORMs.

Release justification: This is a low-risk change to new functionality,
partial indexes.

Release note: None
This commit updates `pg_catalog.pg_constraint.condef` to include the
predicate of unique partial index constraints. Also, the predicate is
now included in the output of `SHOW CONSTRAINTS` and
`pg_get_constraintdef()`.

Release justification: This is a low-risk change to new functionality,
partial indexes.

Release note: None
@mgartner mgartner force-pushed the 53843-include-partial-index-pred-in-pg-catalog branch from 45df2ce to fa8fac3 Compare September 8, 2020 19:13
@mgartner
Copy link
Collaborator Author

mgartner commented Sep 8, 2020

bors r=rafiss

@craig
Copy link
Contributor

craig bot commented Sep 9, 2020

Build succeeded:

@craig craig bot merged commit b1caad6 into cockroachdb:master Sep 9, 2020
@mgartner mgartner deleted the 53843-include-partial-index-pred-in-pg-catalog branch September 9, 2020 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants