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

Add support for common table expressions #14321

Merged
merged 20 commits into from
Oct 31, 2023
Merged

Add support for common table expressions #14321

merged 20 commits into from
Oct 31, 2023

Conversation

systay
Copy link
Collaborator

@systay systay commented Oct 20, 2023

Description

ANSI SQL has had support for WITH and common table expressions since SQL-99, and MySQL added support in the 8.0 release, so it's about time Vitess supports it.

This PR adds tentative support for WITH and CTEs. The approach I have taken is to rewrite the CTE into derived tables before the actual planning starts.

In this PR, only SELECT and UNION support WITH.

Related Issue(s)

Part of solving #4099

@vitess-bot
Copy link
Contributor

vitess-bot bot commented Oct 20, 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 Oct 20, 2023
@github-actions github-actions bot added this to the v19.0.0 milestone Oct 20, 2023
@systay systay force-pushed the cte branch 2 times, most recently from d9ba97f to f7bd9e0 Compare October 24, 2023 11:29
systay added 13 commits October 24, 2023 13:29
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>
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>
Signed-off-by: Andres Taylor <andres@planetscale.com>
@systay systay changed the title ... Add support for common table expressions Oct 24, 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 Oct 24, 2023
Signed-off-by: Andres Taylor <andres@planetscale.com>
@systay systay marked this pull request as ready for review October 24, 2023 12:26
Signed-off-by: Andres Taylor <andres@planetscale.com>
"Sharded": true
},
"FieldQuery": "select id, foo, weight_string(id), weight_string(foo) from (select id, foo from (select id, foo from `user` where 1 != 1) as x where 1 != 1 union select id, foo from (select id, foo from `user` where 1 != 1) as x where 1 != 1) as dt where 1 != 1",
"Query": "select id, foo, weight_string(id), weight_string(foo) from (select id, foo from (select id, foo from `user`) as x union select id, foo from (select id, foo from `user`) as x) as dt",
Copy link
Member

Choose a reason for hiding this comment

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

Can't we simplify this:

Suggested change
"Query": "select id, foo, weight_string(id), weight_string(foo) from (select id, foo from (select id, foo from `user`) as x union select id, foo from (select id, foo from `user`) as x) as dt",
"Query": "select id, foo, weight_string(id), weight_string(foo) from (select id, foo from `user`) as x union select id, foo, weight_string(id), weight_string(foo) from (select id, foo from `user`) as x"

Comment on lines 377 to 381
{
"comment": "CTEs cant use a table with the same name as the CTE alias",
"query": "with user as (select aa from user where user.id=1) select ref.col from ref join user",
"plan": "VT12001: unsupported: do not support CTE that use the CTE alias inside the CTE query"
}
Copy link
Member

Choose a reason for hiding this comment

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

Any plan on supporting this? Also, is the case where the table name is prefixed with the database identifier unsupported? (i.e.: with user as (select aa from user where user.id=1) select ref.col from ref join my_db.user)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah, long term I think we want much better support for CTEs. instead of rewriting them to derived tables they should be first hand citizens, so that the parts of a query that we push down to MySQL uses CTE if the original query used a CTE, and even supporting recursive CTEs using a new engine primitive

Copy link
Member

Choose a reason for hiding this comment

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

that makes sense!

Copy link
Member

@frouioui frouioui left a comment

Choose a reason for hiding this comment

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

Left a few questions, nits and suggestions. Approving this for now as there is nothing major blocking this for me. Really excited about this, thank you 🚀

Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Signed-off-by: Andres Taylor <andres@planetscale.com>
Copy link
Member

@frouioui frouioui left a comment

Choose a reason for hiding this comment

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

Still looks good to me after the last few changes.

Signed-off-by: Andres Taylor <andres@planetscale.com>
@@ -0,0 +1,1844 @@
[
{
"comment": "with t as (select count(*) as a from user) select a from t",
Copy link
Member

Choose a reason for hiding this comment

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

nit: can all the cases have comments about what is getting tested?

Copy link
Member

@GuptaManan100 GuptaManan100 left a comment

Choose a reason for hiding this comment

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

lgtm

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.

the right of join route can be improvement as it is using sharding key in where condition.

{
          "QueryType": "SELECT",
          "Original": "with x as (select * from authoritative) select music.id, x.col1 from music join x on music.id = x.user_id",
          "Instructions": {
            "OperatorType": "Join",
            "Variant": "Join",
            "JoinColumnIndexes": "L:0,R:0",
            "JoinVars": {
              "music_id": 0
            },
            "TableName": "music_authoritative",
            "Inputs": [
              {
                "OperatorType": "Route",
                "Variant": "Scatter",
                "Keyspace": {
                  "Name": "user",
                  "Sharded": true
                },
                "FieldQuery": "select music.id from music where 1 != 1",
                "Query": "select music.id from music",
                "Table": "music"
              },
              {
                "OperatorType": "Route",
                "Variant": "Scatter",
                "Keyspace": {
                  "Name": "user",
                  "Sharded": true
                },
                "FieldQuery": "select x.col1 from (select user_id, col1, col2 from authoritative where 1 != 1) as x where 1 != 1",
                "Query": "select x.col1 from (select user_id, col1, col2 from authoritative where user_id = :music_id) as x",
                "Table": "authoritative"
              }
            ]
          },
          "TablesUsed": [
            "user.authoritative",
            "user.music"
          ]
        }

Signed-off-by: Andres Taylor <andres@planetscale.com>
@systay systay merged commit eddb7da into vitessio:main Oct 31, 2023
115 checks passed
@systay systay deleted the cte branch October 31, 2023 06:47
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