-
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: try harder to trim the filter expression after processing scanNodes. #13373
Conversation
52df551
to
4ba336b
Compare
Take your time, it's not too trivial I realize. (But so far I know only Peter and yourself understand this code.) |
Great stuff! Review status: 0 of 6 files reviewed at latest revision, 8 unresolved discussions, all commit checks successful. pkg/sql/index_selection.go, line 218 at r1 (raw file):
[nit] easier to read the flow if we do pkg/sql/index_selection.go, line 1426 at r1 (raw file):
constraints pkg/sql/index_selection.go, line 1464 at r1 (raw file):
this can just be pkg/sql/index_selection.go, line 1465 at r1 (raw file):
This is not true, it implies pkg/sql/index_selection.go, line 1522 at r1 (raw file):
s/evaluate/simplify? pkg/sql/index_selection.go, line 1571 at r1 (raw file):
[nit] would still help to explain what the arguments are. Also it would help if the datum and t were together and cOp and cdatum were together. pkg/sql/index_selection.go, line 1576 at r1 (raw file):
This code is very tedious, a mistake could easily sneak in (and I doubt that we have tests that cover all the combinations). I have an idea to avoid having 5*5 cases for the comparison ops (NE excluded). Feel free to ignore if though if you don't agree it helps. We could represent a constraint as a closed interval on the integer axis. Say we pick -100 to represent -infinity, +100 as +infinity. 0 represents the smaller of cdatum and datum (both if they are equal), 10 represents the larger (unused if they are equal). First set Then e.g. if The other is similar, we just set Then it's simply a matter of detecting if the two intervals are disjoint (return False) or if c is inside t (return True), which are very simple conditions. pkg/sql/index_selection.go, line 1774 at r1 (raw file):
VisitPost is usually next to VisitPre Comments from Reviewable |
4ba336b
to
e695de8
Compare
Review status: 0 of 6 files reviewed at latest revision, 8 unresolved discussions, all commit checks successful. pkg/sql/index_selection.go, line 218 at r1 (raw file): Previously, RaduBerinde wrote…
Done. pkg/sql/index_selection.go, line 1426 at r1 (raw file): Previously, RaduBerinde wrote…
Done. pkg/sql/index_selection.go, line 1464 at r1 (raw file): Previously, RaduBerinde wrote…
Done. pkg/sql/index_selection.go, line 1465 at r1 (raw file): Previously, RaduBerinde wrote…
Done. pkg/sql/index_selection.go, line 1522 at r1 (raw file): Previously, RaduBerinde wrote…
Done. pkg/sql/index_selection.go, line 1571 at r1 (raw file): Previously, RaduBerinde wrote…
Done. pkg/sql/index_selection.go, line 1576 at r1 (raw file): Previously, RaduBerinde wrote…
"Simply a matter of ..." hahaha Nevertheless the idea was good, so I implemented it. pkg/sql/index_selection.go, line 1774 at r1 (raw file): Previously, RaduBerinde wrote…
Done. Comments from Reviewable |
9a3baa0
to
6bd530f
Compare
Review status: 0 of 7 files reviewed at latest revision, 8 unresolved discussions, some commit checks failed. pkg/sql/index_selection.go, line 1576 at r1 (raw file): Previously, knz (kena) wrote…
I don't think my proposal quite made it across. An interval would (in the code, literally) be represented by two integers instead of datums and inclusive/exclusive flags. The actual datums are not important, just the relationship between them ( I'll take an example. Say datum < cdatum. and both constraints are LT tx=0 (as defined above); interval t would be Another example: datum > cdatum, t is LT, c is GE Once we have these intervals, yes, it's simply a matter of comparing the integers:
The simplification is coming from the fact that we reduce the various combinations of cases (nil vs non-nil, inclusive vs exclusive) to a single case: inclusive intervals with comparable values at both ends. Comments from Reviewable |
Review status: 0 of 7 files reviewed at latest revision, 8 unresolved discussions, some commit checks failed. pkg/sql/index_selection.go, line 1576 at r1 (raw file): Previously, RaduBerinde wrote…
Something along the lines of: makeInterval := func(op Operator, largerDatum bool) (bool, int, int) {
x := 0
if largerDatum {
x = 10
}
switch op {
case parser.EQ
return true, x, x
case parser.LE:
return true, -100, x
case parser.LT:
return true, -100, x-1
case parser.GE:
return true, x, 100
case parser.GT:
return true, x+1, 100
case parser.NE:
return false, x, x
default:
return false, 0, 0
}
}
cmp := datum.Compare(cDatum)
tOk, tStart, tEnd := makeInterval(t.Operator, cmp > 0)
cOk, cStart, cEnd := makeInterval(c.Operator, cmp < 0)
if tOk && cOk {
if cStart > tEnd || tStart > cEnd {
return MadeDBool(false)
}
if tStart <= cStart && cEnd <= tEnd {
return MakeDBool(true)
}
return t
}
// BOOM: just handled 5*5 combinations of operators!
// If one is NE and the other is an interval, we can still use the intervals
// (hence the NE case above).
if tOk && c.Operator == NE {
if cStart < tStart || cStart > tEnd {
return MakeDBool(true)
}
return t
} Comments from Reviewable |
Review status: 0 of 7 files reviewed at latest revision, 8 unresolved discussions, some commit checks failed. pkg/sql/index_selection.go, line 1576 at r1 (raw file): Previously, RaduBerinde wrote…
Now I understand how it works, thanks for the explanation and the example code. I changed the patch accordingly. Comments from Reviewable |
6bd530f
to
c63218b
Compare
Review status: 0 of 7 files reviewed at latest revision, 7 unresolved discussions, all commit checks successful. pkg/sql/index_selection.go, line 1576 at r1 (raw file): Previously, knz (kena) wrote…
Thanks! I'll take a final look over the entire change by Monday. Haha, I don't know how I came up with it.. I just hate formulas that have cornercases. I prefer to first apply some transformation if it eliminates checking for special cases. Some examples:
pkg/sql/index_selection.go, line 1711 at r2 (raw file):
I wrote down something that might be better for this comment (we can leave out some of the details that can be easily gleamed from the code, and just explain the approach):
Comments from Reviewable |
@nvanbenschoten for checking whether I'm not doing anything here that conceptually conflicts with #13444. |
Reviewed 1 of 6 files at r1, 2 of 4 files at r2. pkg/sql/index_selection.go, line 210 at r2 (raw file):
nit: Extend pkg/sql/index_selection.go, line 1448 at r2 (raw file):
After #13444 goes in this will have to be pkg/sql/index_selection.go, line 1525 at r2 (raw file):
You passed the Fizzbuzz test! 👏 pkg/sql/index_selection.go, line 1592 at r2 (raw file):
nit: it's a little confusing that between pkg/sql/index_selection.go, line 1711 at r2 (raw file): Previously, RaduBerinde wrote…
Love the diagrams! pkg/sql/index_selection.go, line 1734 at r2 (raw file):
Let's pull these two conditions into pkg/sql/index_selection.go, line 1743 at r2 (raw file):
nit: qualify which "one is NE", since this matters pkg/sql/index_selection.go, line 1744 at r2 (raw file):
pkg/sql/index_selection.go, line 1745 at r2 (raw file):
I must be visualizing this incorrectly, but I feel like this should be pkg/sql/index_selection.go, line 1755 at r2 (raw file):
nit: is the boolean return value documented anywhere? double nit: usually triple nit: Comments from Reviewable |
pkg/sql/index_selection.go, line 1745 at r2 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
I definitely got confused as to which is which, maybe we need to give them better names. Comments from Reviewable |
c63218b
to
ec76cb9
Compare
Quite a few things to fix with the rebase and the bug y'all have found, but I got there in the end. PTAL! Review status: 3 of 7 files reviewed at latest revision, 16 unresolved discussions, some commit checks failed. pkg/sql/index_selection.go, line 210 at r2 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/sql/index_selection.go, line 1448 at r2 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/sql/index_selection.go, line 1592 at r2 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Agreed, fixed. pkg/sql/index_selection.go, line 1711 at r2 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/sql/index_selection.go, line 1734 at r2 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
done pkg/sql/index_selection.go, line 1743 at r2 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/sql/index_selection.go, line 1744 at r2 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/sql/index_selection.go, line 1745 at r2 (raw file): Previously, RaduBerinde wrote…
Done. Also added a test, since this was not caught earlier. pkg/sql/index_selection.go, line 1755 at r2 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. Comments from Reviewable |
Review status: 1 of 6 files reviewed at latest revision, 16 unresolved discussions, some commit checks pending. Comments from Reviewable |
Ok let's pray this is not going to break everything. TFYR! |
Fixes #3473.
Fixes #13351.
cc @andreimatei.
This change is