-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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: fix partial index and foreign key bugs #55702
sql: fix partial index and foreign key bugs #55702
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the quick investigation and fix!
Reviewable status: complete! 1 of 0 LGTMs obtained (waiting on @mgartner and @RaduBerinde)
pkg/sql/opt/optbuilder/select.go, line 531 at r2 (raw file):
// index predicates cannot be built because they may reference columns // not in outScope. containsAllOrdinaryTableColumns := true
[nit] We could skip the loop if we have tab.ColmnCount() columns (which I believe is the common case)
If the customer that ran into this issue creates a full index on the column (in addition to existing partial index), would it "fix" their issue? Can we tell them to workaround the problem by doing this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 6 of 6 files at r1, 3 of 3 files at r2.
Reviewable status: complete! 2 of 0 LGTMs obtained (waiting on @mgartner)
pkg/sql/opt/memo/expr.go, line 909 at r1 (raw file):
// Note that TableMeta.PartialIndexPredicates is not initialized for mutation // queries. This function will never return ok=true in the context of a mutation. func PartialIndexPredicate(
[nit] Do you need this function anymore? Doesn't seem like it's adding much on top of TableMeta.PartialIndexPredicates
...
No, they already had one (the PK). |
c7d2daf
to
3ee3d65
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @rytaft)
pkg/sql/opt/memo/expr.go, line 909 at r1 (raw file):
Previously, rytaft (Rebecca Taft) wrote…
[nit] Do you need this function anymore? Doesn't seem like it's adding much on top of
TableMeta.PartialIndexPredicates
...
Good point. It's also used far less with the recent changes to scanIndexIter
. I've removed it here, but will not remove it in the backport to minimize changes.
pkg/sql/opt/optbuilder/select.go, line 531 at r2 (raw file):
Previously, RaduBerinde wrote…
[nit] We could skip the loop if we have tab.ColmnCount() columns (which I believe is the common case)
Good idea. Done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed 3 of 6 files at r3, 3 of 3 files at r4.
Reviewable status: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @mgartner)
pkg/sql/opt/optbuilder/select.go, line 531 at r4 (raw file):
// index predicates cannot be built because they may reference columns // not in outScope. In the most common case, the outScope has the same // number of columns as the table and we can skip checking the each
[nit] the each -> that each
76456cd
to
d5d81c1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewable status: complete! 0 of 0 LGTMs obtained (and 2 stale) (waiting on @rytaft)
pkg/sql/opt/optbuilder/select.go, line 531 at r4 (raw file):
Previously, rytaft (Rebecca Taft) wrote…
[nit] the each -> that each
Done.
bors r+ |
bors r- |
Canceled. |
…n scope This commit fixes a panic induced by trying to build partial index predicate expressions without all of a table's columns in-scope. Some scans, like scans built for foreign key checks, do not include all of a table's column in their scope. For such scans, optbuilder no longer attempts to build a partial index predicate because the predicate may reference a column that is not in scope. As a result of this change, `opt.TableMeta` may not have a predicate expression for all partial indexes. The `memo.PartialIndexPredicate` function which retrieves the predicate expressions has been updated to account for this case. Fixes cockroachdb#55672 Release justification: This is a critical bug fix for a new feature, partial indexes. Release note (bug fix): An INSERT into a table with a foreign key reference to a table with a partial index no longer causes an error.
Release justification: This is a critical bug fix for a new feature, partial indexes. Release note (bug fix): Foreign keys can no longer reference columns that are only indexed by a partial unique index. A partial unique index does not guarantee uniqueness in the entire table, therefore the column indexed is not guaranteed to be a unique key.
d5d81c1
to
21fd3a6
Compare
bors r+ |
Build succeeded: |
This commit re-enables the PartialIndexMutator for sqlsmith tests. This mutator was disabled in cockroachdb#55724 after bug fixes in cockroachdb#55702 revealed invalid schemas being created in sqlsmith. By reordering the mutators, randomly generated schemas are no longer invalid. Informs cockroachdb#55718 Release note: None
This commit re-enables the PartialIndexMutator for sqlsmith tests. This mutator was disabled in cockroachdb#55724 after bug fixes in cockroachdb#55702 revealed invalid schemas being created in sqlsmith. By reordering the mutators, randomly generated schemas are no longer invalid. Informs cockroachdb#55718 Release note: None
55761: sqlsmith: enable partial index mutator r=yuzefovich a=mgartner This commit re-enables the PartialIndexMutator for sqlsmith tests. This mutator was disabled in #55724 after bug fixes in #55702 revealed invalid schemas being created in sqlsmith. By reordering the mutators, randomly generated schemas are no longer invalid. Informs #55718 Release note: None Co-authored-by: Marcus Gartner <[email protected]>
opt: build partial index predicates only when all table columns are in scope
This commit fixes a panic induced by trying to build partial index
predicate expressions without all of a table's columns in-scope. Some
scans, like scans built for foreign key checks, do not include all of a
table's column in their scope. For such scans, optbuilder no longer
attempts to build a partial index predicate because the predicate may
reference a column that is not in scope.
As a result of this change,
opt.TableMeta
may not have a predicateexpression for all partial indexes. The
memo.PartialIndexPredicate
function which retrieves the predicate expressions has been updated to
account for this case.
Fixes #55672
Release justification: This is a critical bug fix for a new feature,
partial indexes.
Release note (bug fix): An INSERT into a table with a foreign key
reference to a table with a partial index no longer causes an error.
sql: disqualify partial unique indexes as foreign key reference indexes
Release justification: This is a critical bug fix for a new feature,
partial indexes.
Release note (bug fix): Foreign keys can no longer reference columns
that are only indexed by a partial unique index. A partial unique index
does not guarantee uniqueness in the entire table, therefore the column
indexed is not guaranteed to be a unique key.