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

Range Query Optimization (For sequential Vindex types) #17342

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

c-r-dev
Copy link

@c-r-dev c-r-dev commented Dec 6, 2024

Description

Queries that specify values in a range (for vindexed column) are sent to all shards, instead of just the shards which contain the values in the range.

  • For primary vindexs which are sequential and not random, a BINARY vindex for example, optimized range queries at vtgate to be sent to only shards included between the first and last keyspaceid values of the range.

Related Issue(s)

Fixes #9808

Checklist

  • "Backport to:" labels have been added if this change should be back-ported to release branches
  • If this change is to be back-ported to previous releases, a justification is included in the PR description
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on CI?
  • Documentation was added or is not required

Deployment Notes

Made the change for sequential Vindexes ( binary and numeric)

Tests Performed

  • Added unit tests module level

  • Included sample schema with two columns (c1 and c2) , c1 is vindexed (type binday) and c2 is not vindexed.

When range queried on column c1 (vindex column) :

bin % ./vtexplain --shards 4 --vschema-file ../examples/vtexplain/sample_vschema.json --schema-file ../examples/vtexplain/sample_schema.sql --sql "select * from t1 where c1 between 1 and 10"

----------------------------------------------------------------------
select * from t1 where c1 between 1 and 10

1 ks1/-40: select * from t1 where c1 between 1 and 10 limit 10001

----------------------------------------------------------------------
<nil>%                                      

When range queried on column c2 (non vindex column) :

bin % ./vtexplain --shards 4 --vschema-file ../examples/vtexplain/sample_vschema.json --schema-file ../examples/vtexplain/sample_schema.sql --sql "select * from t1 where c2 between 1 and 10"

----------------------------------------------------------------------
select * from t1 where c2 between 1 and 10

1 ks1/-40: select * from t1 where c2 between 1 and 10 limit 10001
1 ks1/40-80: select * from t1 where c2 between 1 and 10 limit 10001
1 ks1/80-c0: select * from t1 where c2 between 1 and 10 limit 10001
1 ks1/c0-: select * from t1 where c2 between 1 and 10 limit 10001

----------------------------------------------------------------------
<nil>%                                                                                                                                                                                                           

Copy link
Contributor

vitess-bot bot commented Dec 6, 2024

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsBackportReason If backport labels have been applied to a PR, a justification is required NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Dec 6, 2024
@github-actions github-actions bot added this to the v22.0.0 milestone Dec 6, 2024
@deepthi
Copy link
Member

deepthi commented Dec 8, 2024

I unchecked this, because CI has not yet run

Did the new or modified tests pass consistently locally and on CI?

@deepthi deepthi added Type: Enhancement Logical improvement (somewhere between a bug and feature) Component: Query Serving and removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Dec 8, 2024
@c-r-dev
Copy link
Author

c-r-dev commented Dec 9, 2024

I unchecked this, because CI has not yet run

Did the new or modified tests pass consistently locally and on CI?

thanks, confirm - modified tests passed consistently locally ( i have run module level unit tests )

vitess.io/vitess/go/vt/key
vitess.io/vitess/go/vt/vtgate/engine
vitess.io/vitess/go/vt/vtgate/vindexes
vitess.io/vitess/go/vt/vtgate/planbuilder/operators

Didn't get CI to run locally end to end. Currently working on the CI setup.

@deepthi deepthi marked this pull request as ready for review December 9, 2024 17:31
Copy link
Collaborator

@systay systay left a comment

Choose a reason for hiding this comment

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

Thank you so much for your contribution!
This looks really good from my POV.

I would love to see a plan test and and one end-to-end test as well. Not sure where the end-to-end test should live. @harshit-gangal probably knows.

go/vt/vtgate/planbuilder/operators/sharded_routing.go Outdated Show resolved Hide resolved
go/vt/vtgate/vindexes/binary_test.go Show resolved Hide resolved
Copy link
Member

@harshit-gangal harshit-gangal left a comment

Choose a reason for hiding this comment

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

We should add plan test that uses Between for Sequential Vindex and Non-Sequential Vindex type

It can be added to go/vt/vtgate/planbuilder/testdata/select_cases.json

go/vt/vtgate/vindexes/vindex.go Outdated Show resolved Hide resolved
go/vt/vtgate/planbuilder/operators/sharded_routing.go Outdated Show resolved Hide resolved
examples/vtexplain/sample_vschema.json Outdated Show resolved Hide resolved
go/vt/vtgate/engine/routing.go Outdated Show resolved Hide resolved
go/vt/vtgate/engine/routing.go Outdated Show resolved Hide resolved
@c-r-dev
Copy link
Author

c-r-dev commented Dec 10, 2024

thanks @systay and @harshit-gangal for reviewing and sharing feedback, let me incorporate these and get back. Will reach out in case of questions.

@c-r-dev
Copy link
Author

c-r-dev commented Dec 11, 2024

We should add plan test that uses Between for Sequential Vindex and Non-Sequential Vindex type

It can be added to go/vt/vtgate/planbuilder/testdata/select_cases.json

Thanks for the suggestion and pointing me to sample , i have added new test cases with e8593a8

@@ -948,6 +948,61 @@
]
}
}
},
"sequential": {
Copy link
Collaborator

Choose a reason for hiding this comment

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

not sure, but I'm thinking we could use an existing keyspace instead of adding a new one. feel free to add a vindex or even a table if you need to.
I'm just thinking of not adding unnecessary complexity if we don't have to

Copy link
Author

Choose a reason for hiding this comment

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

sure thing , incorporated with 5a7c8f1 , using user keyspace - added extra vindex and table

]
}
},
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice set of planner tests. could you add at least one test where the values in the between are not literal values, but instead columns from another table. something like:

select * 
from tblA 
  join tblB on tblA.foo = tbl.bar 
where tblA.X between tblB.C and tblB.D

it'd be good to make sure that this also works as expected.

Copy link
Author

Choose a reason for hiding this comment

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

thanks for suggesting this , added a test case now 0697cb0 , and also fixed missing Cost for RoutOpCode.Between.

Comment on lines 161 to 163
if !assert.Equal(t, got[0].String(), want) {
t.Errorf("RangeMap(): %+v, want %+v", got, want)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if !assert.Equal(t, got[0].String(), want) {
t.Errorf("RangeMap(): %+v, want %+v", got, want)
}
assert.Equal(t, want, got[0].String())

Copy link
Author

Choose a reason for hiding this comment

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

thanks , done with 5a7c8f1

"Query": "select cola, colb, col1 from seq_multicol_tbl where cola between 1 and 5",
"Table": "seq_multicol_tbl",
"Values": [
"(1, 5)"
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't understand why we get the values field here for a Scatter route. it doesn't look right

Copy link
Author

Choose a reason for hiding this comment

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

thanks for catching this , noticed processSingleColumnVindex has few extra early checks compared to processMultiColumnVindex. With 8a0ded0 , added checks for vindex and also routingCode before processing any futher.

This ensures Values is not set in case of Scatter route.

Please help to review if addition of these checks has any other side effects (which i may have missed) , ensured all test cases are passing.

@c-r-dev
Copy link
Author

c-r-dev commented Dec 11, 2024

thanks @systay , will check these and make next set of changes.

@c-r-dev c-r-dev requested a review from systay December 11, 2024 16:54
Copy link

codecov bot commented Dec 12, 2024

Codecov Report

Attention: Patch coverage is 44.70588% with 47 lines in your changes missing coverage. Please review.

Project coverage is 67.51%. Comparing base (45192d2) to head (1400fea).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
go/vt/vtgate/engine/routing.go 0.00% 30 Missing ⚠️
go/vt/vtgate/vindexes/numeric.go 0.00% 11 Missing ⚠️
go/vt/vtgate/vindexes/binary.go 63.63% 4 Missing ⚠️
...vt/vtgate/planbuilder/operators/sharded_routing.go 93.54% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #17342      +/-   ##
==========================================
- Coverage   67.52%   67.51%   -0.01%     
==========================================
  Files        1581     1581              
  Lines      253948   254030      +82     
==========================================
+ Hits       171480   171520      +40     
- Misses      82468    82510      +42     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@harshit-gangal harshit-gangal left a comment

Choose a reason for hiding this comment

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

Changes looks good.
For end to end test. We recently added a way to test the plan tests to also run as e2e test

Look here https://github.com/vitessio/vitess/blob/main/go/test/endtoend/vtgate/plan_tests/plan_e2e_test.go
and the PR that added it #17117

You would have to add filter_cases.json to the e2e test.

@c-r-dev
Copy link
Author

c-r-dev commented Dec 13, 2024

Changes looks good. For end to end test. We recently added a way to test the plan tests to also run as e2e test

Look here https://github.com/vitessio/vitess/blob/main/go/test/endtoend/vtgate/plan_tests/plan_e2e_test.go and the PR that added it #17117

You would have to add filter_cases.json to the e2e test.

thanks @harshit-gangal , will pull in the changes from #17117 and add test case for filer_case.json. Will reach out in case of any questions.

@c-r-dev
Copy link
Author

c-r-dev commented Dec 14, 2024

Changes looks good. For end to end test. We recently added a way to test the plan tests to also run as e2e test
Look here https://github.com/vitessio/vitess/blob/main/go/test/endtoend/vtgate/plan_tests/plan_e2e_test.go and the PR that added it #17117
You would have to add filter_cases.json to the e2e test.

thanks @harshit-gangal , will pull in the changes from #17117 and add test case for filter_case.json. Will reach out in case of any questions.

Thanks , added end-to-end test case for filter_case.json with f4a578c. Please help to review if skip_e2e is tagged correctly ( kept it similar to select_case.json - join cases (no sample data in table) and known error cases )

go/vt/vtgate/planbuilder/testdata/filter_cases.json Outdated Show resolved Hide resolved
go/vt/vtgate/planbuilder/testdata/filter_cases.json Outdated Show resolved Hide resolved
Comment on lines 5110 to 5112
},
"skip_e2e":true
}
Copy link
Member

Choose a reason for hiding this comment

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

new test should not be skipped

Copy link
Author

Choose a reason for hiding this comment

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

thanks , out of the 5 new test cases added for between , marked the final one related to join as skip_e2e.

Reason for marking this skip , is because of "OperatorType": "Join" , verifyTestExpectations for join cases verifies -> Verify that the Join primitive sees atleast 1 row on the left side. Currently we don't have any sample data in these test schema tables. (pls let me know if we can put some test data in these tables for e2e testing)

@harshit-gangal please help to let me know if I have missed any thing , or if there is a better way to write this test for "Between clause on a binary vindex field with values from a different table"

Copy link
Member

Choose a reason for hiding this comment

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

We need to add the sample data to have data on left and right side of the join.
The policy after adding these plan test is that we need to ensure that new tests does not add skip_e2e flag.
For that we have to keep modifying the sample input data.

Copy link
Author

@c-r-dev c-r-dev Dec 17, 2024

Choose a reason for hiding this comment

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

thanks @harshit-gangal , added sample data loading with 1400fea , and removed skip_e2e. All new tests are passing with out skipping.

Please help to review.

@c-r-dev
Copy link
Author

c-r-dev commented Dec 16, 2024

thanks @harshit-gangal for reviewing , will incorporate feedback and share and updated version for review.

@c-r-dev
Copy link
Author

c-r-dev commented Dec 18, 2024

Thank you so much for your contribution! This looks really good from my POV.

I would love to see a plan test and and one end-to-end test as well. Not sure where the end-to-end test should live. @harshit-gangal probably knows.

@systay , thanks for the feedback , have worked with @harshit-gangal and got end-to-end setup for filter_case.

Can you please help to review when you get a chance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Query Serving Type: Enhancement Logical improvement (somewhere between a bug and feature)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Range Query Optimization (For sequential Vindex types)
4 participants