-
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: improve index constraints for boolean columns #19106
sql: improve index constraints for boolean columns #19106
Conversation
15db3e3
to
0e00718
Compare
Review status: 0 of 6 files reviewed at latest revision, 4 unresolved discussions. pkg/sql/analyze.go, line 133 at r1 (raw file):
[nit] perhaps add the new example here (a) -> (a = true) pkg/sql/analyze.go, line 1483 at r1 (raw file):
needs a comment. It's a little unintuitive what it does given its name, arguably "a" is simpler than "a = true" pkg/sql/parser/normalize.go, line 450 at r3 (raw file):
Is this needed? Would a NULL ever satisfy an equality? pkg/sql/parser/normalize.go, line 457 at r3 (raw file):
Is this needed? isn't NE always true if one of the operands is NULL? Comments from Reviewable |
Reviewed 4 of 4 files at r1, 4 of 4 files at r2, 4 of 4 files at r3, 3 of 3 files at r4. Comments from Reviewable |
Index selection only creates index constraints when it sees `ComparisonExprs` that it knows how to handle. This means that if it saw a query like `SELECT * FROM t WHERE b`, where `b` is a `BOOL` columns, it would previously end up scanning the entire table even if there was an index on `b`! However, `SELECT * from t WHERE b = true` would work correctly. This change fixes this by simplifying boolean `IndexedVars` into `ComparisonExprs`.
This change adds normalization and simplification rules for `NotExprs`: ``` NotExpr{NotExpr{e}} -> e ``` We technically only need to add this as a normalization rule, but until cockroachdb#2791 is addressed, I think it's better to add both to preserve symmetry with `ComparisonExpr`, `AndExpr`, and `OrExpr`. These rules allow us to select better index spans in certain situations.
This change adds new normalization rules: ``` ComparisonExpr{Is,v,d} where d != DNull -> AndExpr{ ComparisonExpr{EQ,v,d} ComparisonExpr{IsNot,v,DNull} } ComparisonExpr{IsNot,v,d} where d != DNull -> OrExpr{ ComparisonExpr{NE,v,d} ComparisonExpr{Is,v,DNull} } ``` This allows us to take advantage of obvious index constraints for queries like `SELECT * FROM t WHERE b IS TRUE`. It will also avoid complications being discussed in cockroachdb#18860 by addressing the root issue. There may be an argument to make this a simplification rule instead.
This change adds two new simplification rules: ``` ComparisonExpr{NE,v,d} where d.Max() -> ComparisonExpr{LT,v,d} ComparisonExpr{NE,v,d} where d.Min() -> ComparisonExpr{GT,v,d} ``` These inequalities can be more easily used for index selection. For datum types with large domains, this isn't particularly useful. However, for types like BOOL, this is important because it means we can use an index constraint for queries like `SELECT * FROM t WHERE b != true`.
0e00718
to
8e4acbd
Compare
Review status: 1 of 6 files reviewed at latest revision, 4 unresolved discussions. pkg/sql/analyze.go, line 133 at r1 (raw file): Previously, RaduBerinde wrote…
Done. pkg/sql/analyze.go, line 1483 at r1 (raw file): Previously, RaduBerinde wrote…
Done. pkg/sql/parser/normalize.go, line 450 at r3 (raw file): Previously, RaduBerinde wrote…
Added a comment. The gist is that pkg/sql/parser/normalize.go, line 457 at r3 (raw file): Previously, RaduBerinde wrote…
Same as above. Also, got rid of Comments from Reviewable |
8e4acbd
to
4755942
Compare
This change adds simplification rules to `simplifyNotExpr`: ``` NotExpr{ComparisonExpr{NotRegMatch,a,b}} -> ComparisonExpr{RegMatch,a,b} NotExpr{ComparisonExpr{NotRegIMatch,a,b}} -> ComparisonExpr{RegIMatch,a,b} NotExpr{ComparisonExpr{IsDistinctFrom,a,b}} -> ComparisonExpr{IsNotDistinctFrom,a,b} NotExpr{ComparisonExpr{IsNotDistinctFrom,a,b}} -> ComparisonExpr{IsDistinctFrom,a,b} NotExpr{ComparisonExpr{Is,a,b}} -> ComparisonExpr{IsNot,a,b} NotExpr{ComparisonExpr{IsNota,b}} -> ComparisonExpr{Is,a,b} ```
4755942
to
f845dd9
Compare
Comments from Reviewable |
This PR includes 4 small changes that together greatly improve the
index constraints, and by extension, the index spans that we generate
when dealing with
BOOL
columns:IndexedVars
inWHERE
clauses.
NOT NOT b
tob
, which allows us to improveindex selection when that pattern is found in
WHERE
clauses.IS
andIS NOT
expressions toEQ
andNE
when possible. This allows index selection rules that work only for
EQ
and
NE
to construct index constraints. This also simplifies things for sql: NaN = NaN (in Postgres) #18860.v != max
tov < max
andv != min
tov > min
, whichallows us to create index constraints on these expressions.
None of the following queries were able to efficiently use indexes before.
Now they all can: