Skip to content

Conversation

@viirya
Copy link
Member

@viirya viirya commented Sep 19, 2016

What changes were proposed in this pull request?

We substitute logical plan with CTE definitions in the analyzer rule CTESubstitution. A CTE definition can be used in the logical plan for multiple times, and its analyzed logical plan should be the same. We should not analyze CTE definitions multiple times when they are reused in the query.

By analyzing CTE definitions before substitution, we can support defining CTE in subquery.

How was this patch tested?

Jenkins tests.

@viirya viirya changed the title [SPARK-17590][SQL] Analyze CTE definitions at once [SPARK-17590][SQL] Analyze CTE definitions at once and allow CTE subquery to define CTE Sep 19, 2016
@viirya
Copy link
Member Author

viirya commented Sep 19, 2016

cc @hvanhovell @cloud-fan

@SparkQA
Copy link

SparkQA commented Sep 19, 2016

Test build #65586 has finished for PR 15146 at commit 394495d.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@SparkQA
Copy link

SparkQA commented Sep 19, 2016

Test build #65588 has finished for PR 15146 at commit 2f8e79f.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.


namedQuery
: name=identifier AS? '(' queryNoWith ')'
: name=identifier AS? '(' query ')'
Copy link
Contributor

Choose a reason for hiding this comment

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

can you explain more about this grammar change?

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently we only allow a query without With used in the CTE's named query. This change loose this limit to allow a query with With to be a named query. So we can define CTE in CTE subquery.

Copy link
Member Author

@viirya viirya Sep 19, 2016

Choose a reason for hiding this comment

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

BTW, query is defined as "ctes? queryNoWith".


test("define CTE in CTE subquery") {
checkAnswer(
sql("with t2 as (with t1 as (select 1 as b, 2 as c) select b, c from t1) " +
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 use multi-line string with proper ident here? this test is almost unreadable...

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

@cloud-fan
Copy link
Contributor

Seems good, but not quite sure, cc @hvanhovell to confirm

Copy link
Contributor

@hvanhovell hvanhovell left a comment

Choose a reason for hiding this comment

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

LGTM


test("define CTE in CTE subquery") {
checkAnswer(
sql("with t2 as (with t1 as (select 1 as b, 2 as c) select b, c from t1) " +
Copy link
Contributor

Choose a reason for hiding this comment

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

+1

@hvanhovell
Copy link
Contributor

@viirya does this improve performance?

@viirya
Copy link
Member Author

viirya commented Sep 20, 2016

@hvanhovell This is for analyzer change and adds CTE in CTE feature. I don't expect there is performance improvement.

@viirya
Copy link
Member Author

viirya commented Sep 20, 2016

I guess if using the same analyzed plan increases the chance to reuse exchange, then it may improve the performance. Anyway, it is not the purpose of this change. Because the analyzed subquery plan will be changed largely in optimization later, we cannot guarantee this.

@SparkQA
Copy link

SparkQA commented Sep 20, 2016

Test build #65630 has finished for PR 15146 at commit baf239b.

  • This patch passes all tests.
  • This patch merges cleanly.
  • This patch adds no public classes.

@hvanhovell
Copy link
Contributor

Merging to master. Thanks!

@asfgit asfgit closed this in 248922f Sep 21, 2016
@viirya
Copy link
Member Author

viirya commented Sep 22, 2016

@hvanhovell @cloud-fan Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants