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

Improve sharded query routing for tuple list #14892

Merged
merged 6 commits into from
Jan 15, 2024

Conversation

harshit-gangal
Copy link
Member

@harshit-gangal harshit-gangal commented Jan 5, 2024

Description

This PR improved the plan when filter condition has List Argument.
E.g. select 1 from user where (id, col) in ::vals
Earlier this query was send down as Scatter to all shards.
With this PR, the query will send down to the shards based on where the sharding key (here id) routes this query.

These List Arguments is currently used in DML routing for multi-table query and foreign key cascades.

Old Plan:

{
  "QueryType": "SELECT",
  "Original": "select 1 from user where (id, col) in ::vals",
  "Instructions": {
    "OperatorType": "Route",
    "Variant": "Scatter",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "FieldQuery": "select 1 from `user` where 1 != 1",
    "Query": "select 1 from `user` where (id, col) in ::vals",
    "Table": "`user`"
  },
  "TablesUsed": [
    "user.user"
  ]
}

New Plan:

{
  "QueryType": "SELECT",
  "Original": "select 1 from user where (id, col) in ::vals",
  "Instructions": {
    "OperatorType": "Route",
    "Variant": "MultiEqual",
    "Keyspace": {
      "Name": "user",
      "Sharded": true
    },
    "FieldQuery": "select 1 from `user` where 1 != 1",
    "Query": "select 1 from `user` where (id, col) in ::vals",
    "Table": "`user`",
    "Values": [
      "vals:0"
    ],
    "Vindex": "user_index"
  },
  "TablesUsed": [
    "user.user"
  ]
}

Related Issue(s)

Checklist

  • 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

@harshit-gangal harshit-gangal added Type: Enhancement Logical improvement (somewhere between a bug and feature) Component: Query Serving labels Jan 5, 2024
Copy link
Contributor

vitess-bot bot commented Jan 5, 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 Jan 5, 2024
@harshit-gangal harshit-gangal removed NeedsWebsiteDocsUpdate What it says NeedsBackportReason If backport labels have been applied to a PR, a justification is required labels Jan 5, 2024
@github-actions github-actions bot added this to the v19.0.0 milestone Jan 5, 2024
@harshit-gangal harshit-gangal force-pushed the listarg-vindex-selection branch from 30f96b6 to 6af8ec1 Compare January 12, 2024 05:19
Copy link

codecov bot commented Jan 12, 2024

Codecov Report

Attention: 114 lines in your changes are missing coverage. Please review.

Comparison is base (eddb39e) 47.29% compared to head (825ce76) 47.28%.
Report is 3 commits behind head on main.

❗ Current head 825ce76 differs from pull request most recent head b4f7288. Consider uploading reports for the commit b4f7288 to get more accurate results

Files Patch % Lines
go/vt/vtgate/evalengine/expr_tuple_bvar.go 0.00% 50 Missing ⚠️
...vt/vtgate/planbuilder/operators/sharded_routing.go 0.00% 39 Missing ⚠️
go/vt/vtgate/evalengine/cached_size.go 0.00% 10 Missing ⚠️
go/vt/vtgate/evalengine/format.go 0.00% 6 Missing ⚠️
go/vt/vtgate/evalengine/translate_simplify.go 0.00% 4 Missing ⚠️
go/vt/vtgate/planbuilder/operators/route.go 0.00% 3 Missing ⚠️
go/vt/vtgate/evalengine/expr_bvar.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #14892      +/-   ##
==========================================
- Coverage   47.29%   47.28%   -0.02%     
==========================================
  Files        1137     1138       +1     
  Lines      238684   238787     +103     
==========================================
+ Hits       112895   112912      +17     
- Misses     117168   117257      +89     
+ Partials     8621     8618       -3     

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

@harshit-gangal harshit-gangal removed NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request labels Jan 12, 2024
@harshit-gangal harshit-gangal force-pushed the listarg-vindex-selection branch from 3074bf8 to 825ce76 Compare January 12, 2024 06:24
@harshit-gangal harshit-gangal marked this pull request as ready for review January 12, 2024 06:24
}

if bvar.Type != sqltypes.Tuple {
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "query argument '%s' must be a tuple (is %s)", bv.Key, bvar.Type)
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
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "query argument '%s' must be a tuple (is %s)", bv.Key, bvar.Type)
return nil, vterrors.Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "query argument '%s' must be a tuple (is %s)", bv.Key, bvar.Type.String())

Copy link
Member Author

Choose a reason for hiding this comment

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

I can add that explicitly. Otherwise, It is not required when a String() method is implemented.

}

func (bv *TupleBindVariable) compile(c *compiler) (ctype, error) {
return ctype{}, c.unsupported(bv)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add the logic for this here? We need this also for the type resolving as well, since we depend on compile for resolving types now as well. See for example also here:

func (u *UntypedExpr) Compile(env *ExpressionEnv) (*CompiledExpr, error) {
typed, err := u.loadTypedExpression(env)
if err != nil {
return nil, err
}
return typed.compile(u.ir, u.collation, u.collationEnv, env.sqlmode)
}

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a Tuple type bind variable and I am not sure how to compile such.
I checked expr_bvar and it is not recognized there

func (bvar *BindVariable) compile(c *compiler) (ctype, error) {
.
.
.
switch tt := typ.Type; {
.
.
default:
    return ctype{}, vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "Type is not supported: %s", tt)
}
.		

Copy link
Member Author

@harshit-gangal harshit-gangal Jan 15, 2024

Choose a reason for hiding this comment

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

We store something like
(1, 'a'), (2, 'b'), (3, 'c')... in a single bind variable.
and TupleBindVariable will provide single tuple for an index.
If Index is 1 then it returns
('a'), ('b'), ('c'). The output type is still a tuple.

Signed-off-by: Harshit Gangal <[email protected]>
@harshit-gangal harshit-gangal merged commit 42afc72 into vitessio:main Jan 15, 2024
100 checks passed
@harshit-gangal harshit-gangal deleted the listarg-vindex-selection branch January 15, 2024 06:56
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.

4 participants