Skip to content

Move subqueries to use the operator model#13750

Merged
systay merged 110 commits intovitessio:mainfrom
planetscale:subq-op
Sep 29, 2023
Merged

Move subqueries to use the operator model#13750
systay merged 110 commits intovitessio:mainfrom
planetscale:subq-op

Conversation

@systay
Copy link
Copy Markdown
Collaborator

@systay systay commented Aug 9, 2023

Description

This PR moves the last big piece of logic over to the operator side of query planning.

Sub query planning introduces a new step in the query planning process - "subquery settling".

For queries containing subqueries, the process is now:

  • Starting from an AST with corresponding semantic state
  • Build the initial op tree with horizons, query graphs and subquery containers (more about these terms later)
  • Iteratively, push as many operators as possible under a route, and merge routes when possible
  • Once we decide we won't be able to push/merge subqueries any more, we "settle the subqueries"
    • This means remove the subquery containers and decide how the subquery needs to be executed.
    • If we notice that we are dealing with a correlated subquery that is not a semi-join, this is where we fail
    • During settling we rewrite merged subquery expressions back into an AST expression
    • Finally, we inject a filter into the outer side if we are dealing with a WHERE clause sub query

Subquery containers

When a query has multiple subqueries, we organise them as follows:

    ┌───┐
    │SQC│
    └───┘
┌──┐ ┌──┐ ┌──┐
│O │ │I1│ │I2│
└──┘ └──┘ └──┘

In this diagram, SQC is the sub query container, O is the outer query, and I1 and I2 are the two inner queries.

The alternative way of representing the same query could be something like this:

 ┌──────────┐
 │    SQ1   │
 └──────────┘

┌──┐      ┌───┐
│O │      │SQ2│
└──┘      └───┘

       ┌──┐  ┌──┐
       │I1│  │I2│
       └──┘  └──┘

Here SQ1 has O as it's outer query, but the inner query is another subquery, SQ2. It's outer is I1 and it's inner is I2. This is the "natural" way to represent this query pattern, and this is also what the final plan looks like.

The reason for having the sub query container until the subquery settle time is to make it easier to merge the outer with any of the inner queries. It makes it easy to compare the mergability of the outer query with each sub query in an efficient way.

Subquery merging

We are now a bit better when it comes to merging subqueries together. This is due to making it possible to push subquery operators down the operator tree and thus coming close enough to another Route so we can merge them together.

Related Issue(s)

Tracking issue #11626

Checklist

  • "Backport to:" labels have been added if this change should be back-ported
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on the CI
  • Documentation was added or is not required

@vitess-bot
Copy link
Copy Markdown
Contributor

vitess-bot bot commented Aug 9, 2023

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 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 Aug 9, 2023
@github-actions github-actions bot added this to the v18.0.0 milestone Aug 9, 2023
@systay systay 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 labels Aug 9, 2023
@systay systay force-pushed the subq-op branch 3 times, most recently from 38db062 to a2b6a1c Compare August 21, 2023 12:38
@systay systay force-pushed the subq-op branch 3 times, most recently from 933f462 to fe89b8c Compare August 22, 2023 17:57
@harshit-gangal harshit-gangal force-pushed the subq-op branch 2 times, most recently from 426840e to ac78e61 Compare August 24, 2023 07:51
@systay systay force-pushed the subq-op branch 3 times, most recently from e604f9b to 3974776 Compare August 25, 2023 13:23
systay added 6 commits August 28, 2023 16:46
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
systay and others added 3 commits September 27, 2023 07:13
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Manan Gupta <manan@planetscale.com>
case *SubQueryContainer:
return pushOrMergeSubQueryContainer(ctx, in)
case *QueryGraph:
return optimizeQueryGraph(ctx, in)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is *QueryGraph being used here? It seems that the *QueryGraph type has already been handled in the previous transformToPhysical method.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Great question! This is because we might have subqueries hiding inside horizons, and these won't become available until we do horizon expansion.

I'm not really happy with how this ended up, and I think I want to clean up this part. We shouldn't produce Horizons that are hiding subqueries - these should get expanded straight away. Unfortunately, we depend on the root being a Horizon to know what columns the user originally asked for in addTruncationOrProjectionToReturnOutput. I'll work on cleaning this up in coming PRs

Signed-off-by: Manan Gupta <manan@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Comment on lines -63 to -81
utils.AssertMatches(t, mcmp.VtConn, `
select id
from t1
where exists(
select t2.id, count(*)
from t2
where t1.col = t2.tcol2
having count(*) > 0
)`,
`[[INT64(100)]]`)
utils.AssertMatches(t, mcmp.VtConn, `
select id
from t1
where exists(
select t2.id, count(*)
from t2
where t1.col = t2.tcol1
) order by id`,
`[[INT64(1)] [INT64(4)] [INT64(100)]]`)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

any reason for removing these test cases

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yeah, they are not valid. they are not following the ONLY_FULL_GROUP_BY directive, but this test doesn't change sql_mode on the connection, so the query fails.

Comment on lines +40 to +44
func Walk(visit Visit, first SQLNode, nodes ...SQLNode) error {
err := VisitSQLNode(first, visit)
if err != nil {
return err
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't see why first field is required. Won't the first element of nodes otherwise be the first to run?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

this is to avoid accidental use of this method with no nodes to visit. We found one case where this was done by accident.

Copy link
Copy Markdown
Contributor

@GuptaManan100 GuptaManan100 Sep 28, 2023

Choose a reason for hiding this comment

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

Shouldn't we then just check the length of nodes and panic/error on 0 length?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Why is that preferable to handling it like this?

Assignments map[string]sqlparser.Expr
ChangedVindexValues map[string]*engine.VindexValues
OwnedVindexQuery string
AST *sqlparser.Update
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why are we not storing AST in Update anymore, but we are doing so for Delete and Inserts?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

we probably should. do you remember, @harshit-gangal

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we want to get rid of using them for others as well, just that subquery is not supported for delete and insert queries therefore it is not changed in this PR.

Signed-off-by: Andres Taylor <andres@planetscale.com>
Comment on lines +123 to +130
func (sqc *SubQueryContainer) getRootOperator(op ops.Operator) ops.Operator {
if len(sqc.Inner) == 0 {
return op
}

sqc.Outer = op
return sqc
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

getRootOperator does not feel like a good name here, we are returning the receiver on line 129

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

wdyt would be a better name?

Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
systay and others added 4 commits September 29, 2023 07:50
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
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.

5 participants