Skip to content

Conversation

@Dandandan
Copy link
Contributor

@Dandandan Dandandan commented Mar 22, 2021

This adds support for CTE syntax:

WITH 
   name AS (SELECT ...)
   [, name2 AS (SELECT ...)]
SELECT ...
FROM ...

Before this PR, the CTE syntax was ignored.

This PR supports CTEs referening a previous CTE within the same query (but no forward references)

@github-actions
Copy link

Copy link
Member

@jorgecarleitao jorgecarleitao left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @Dandandan !

@alamb
Copy link
Contributor

alamb commented Mar 23, 2021

Thank you for this @Dandandan ! I plan to review this carefully tomorrow

let logical_plan = self.query_to_plan_with_alias(
&cte.query,
Some(cte.alias.name.value.clone()),
&mut ctes.clone(),
Copy link
Contributor Author

@Dandandan Dandandan Mar 24, 2021

Choose a reason for hiding this comment

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

This clone is now relatively inefficient for very long and/or deep ctes.

Solutions I see for this problem.

  • use an immutable HashMap (O(1) clone, easier to program), example: https://docs.rs/im/15.0.0/im/struct.HashMap.html
  • use something like "frames", e.g. Vec<HashMap<String, LogicalPlan>> -> when looking up a reference first look up level 0, level-1, level -2, etc.
  • cleanup the variables before returning (removing / replacing the added references)

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the frames idea makes the most sense to me -- namely a stack

We can handle it in the future if/when it shows itself to be a problem

let logical_plan = self.query_to_plan_with_alias(
&cte.query,
Some(cte.alias.name.value.clone()),
&mut ctes.clone(),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the frames idea makes the most sense to me -- namely a stack

We can handle it in the future if/when it shows itself to be a problem

@alamb alamb closed this in 33c826e Mar 24, 2021
alamb added a commit that referenced this pull request Mar 28, 2021
… of SQL features

@Dandandan  added the feature in #9776, updating docs to keep pace

Closes #9795 from alamb/alamb/CTE

Authored-by: Andrew Lamb <[email protected]>
Signed-off-by: Andrew Lamb <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants