Uh oh!
There was an error while loading. Please reload this page.
[SPARK-19799][SQL] Support WITH clause in subqueries - #24831
Conversation
dongjoon-hyun
commented
Jun 10, 2019
ok to test |
SparkQA
commented
Jun 10, 2019
Test build #106360 has finished for PR 24831 at commit
|
dongjoon-hyun
commented
Jun 10, 2019
cc @gatorsmile since this is a part of PostgreSQL feature parity. |
SparkQA
commented
Jun 13, 2019
Test build #106461 has finished for PR 24831 at commit
|
SparkQA
commented
Jun 15, 2019
Test build #106541 has finished for PR 24831 at commit
|
SparkQA
commented
Jun 22, 2019
Test build #106793 has finished for PR 24831 at commit
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
I remember now why I used lazy here. A CTE definition can be used multiple times in WITH but the call by name parameter (ctePlan = traverseAndSubstituteCTE(...)) should be executed only once.
But now I believe it is better to use lazy outside of substituteCTE than inside, please review my commit 7d69105.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
SparkQA
commented
Jul 1, 2019
Test build #107069 has finished for PR 24831 at commit
|
SparkQA
commented
Jul 1, 2019
Test build #107073 has finished for PR 24831 at commit
|
SparkQA
commented
Jul 1, 2019
Test build #107079 has finished for PR 24831 at commit
|
SparkQA
commented
Jul 1, 2019
Test build #107084 has finished for PR 24831 at commit
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Why we need this flag? Some tests would fail if we have not?
There was a problem hiding this comment.
No it would not, but I wanted to do CTE substitution in the current plan only (not in the subqueries) if it is safe. (CTE substitution will run for subqueries later anyway.)
@peter-toth Can we split this pr into two parts cleanly: 1. the sub-query support and 2. the behaviour change? Also, I think we need to update the migration guide for the part 2. cc: @gatorsmile |
@maropu, all right, I dropped the changes that relate to order of substitution and will do it in another ticket. What remained here is just the |
maropu
commented
Jul 2, 2019
Yea, thanks! |
peter-toth
commented
Jul 2, 2019
Very welcome. |
SparkQA
commented
Jul 2, 2019
Test build #107101 has finished for PR 24831 at commit
|
SparkQA
commented
Jul 2, 2019
Test build #107106 has finished for PR 24831 at commit
|
| /** | ||
| * Analyze WITH nodes and substitute child plan with CTE definitions. | ||
| */ | ||
| object CTESubstitution extends Rule[LogicalPlan] { |
There was a problem hiding this comment.
can we avoid moving the class, in order to keep the diff smaller?
There was a problem hiding this comment.
The idea of moving the rule to a separate file came from here: #24831 (comment), but I think you are right @mgaido91, because we cut the scope and split the PR since that. Maybe the other part (#25029) could extract the rule to a separate file as that one makes the rule a bit more complicated. Does that work for you @maropu?
There was a problem hiding this comment.
Yes, that was what I meant, we can move the rule in the other PR which refactors it more thoroughly.
Uh oh!
There was an error while loading. Please reload this page.
| plan resolveOperatorsUp { | ||
| case UnresolvedRelation(Seq(table)) if resolver(cteName, table) => | ||
| ctePlan | ||
| case u: UnresolvedRelation => |
There was a problem hiding this comment.
I don't think this line does anything nor UnresolvedRelation can have an expression so I thought it is safe and good idea to remove the line. Please correct me if I'm wrong.
There was a problem hiding this comment.
yes, I think you're right, I was just curious about the reason of this change
mgaido91
left a comment
There was a problem hiding this comment.
only a style comment, otherwise LGTM, thanks!
| plan resolveOperatorsUp { | ||
| case UnresolvedRelation(Seq(table)) if resolver(cteName, table) => | ||
| ctePlan | ||
| case u: UnresolvedRelation => |
There was a problem hiding this comment.
yes, I think you're right, I was just curious about the reason of this change
Uh oh!
There was an error while loading. Please reload this page.
| -- !query 20 schema | ||
| struct<> | ||
| -- !query 20 output | ||
| org.apache.spark.sql.AnalysisException |
There was a problem hiding this comment.
Just a question, is this going to be addressed in the PR which allows recursive subqueries or is this an invalid query?
There was a problem hiding this comment.
I have a WIP PR open #23531 that would add support for recursive queries (and subqueries and subquery expressions too). But these queries lack the RECURSIVE keyword and using an outer recursive reference in a subquery is not allowed (next query) according to the SQL standard so these will never become valid.
But, this PR should be accepted first then could come #25029 and #23531
Actually I think I'm removing the test WITH r AS (SELECT * FROM r) SELECT * FROM r; because there is already a similar one in cte.sql and moving the WITH r AS (SELECT (SELECT * FROM r)) SELECT * FROM r; next to the existing one.
SparkQA
commented
Jul 3, 2019
Test build #107168 has finished for PR 24831 at commit
|
SparkQA
commented
Jul 3, 2019
Test build #107175 has finished for PR 24831 at commit
|
SparkQA
commented
Jul 3, 2019
Test build #107178 has finished for PR 24831 at commit
|
| struct<1:int> | ||
| -- !query 12 output | ||
| 1 | ||
There was a problem hiding this comment.
This result is different from the pg one;
postgres=# WITH
postgres-# t AS (SELECT 1),
postgres-# t2 AS (
postgres(# WITH t AS (SELECT 2)
postgres(# SELECT * FROM t
postgres(# )
postgres-# SELECT * FROM t2;
?column? ----------
2
(1 row)
This will be address in the following #25029?
There was a problem hiding this comment.
I also agree that this is inevitable in this PR. (cc @gatorsmile ).
There was a problem hiding this comment.
Yes, after #25029 it will return 2 (https://github.com/apache/spark/pull/25029/files#diff-fc515a5db268d29b08b80f5eb8202026R145)
dongjoon-hyun
left a comment
There was a problem hiding this comment.
+1, LGTM. Thank you, @peter-toth , @maropu , @mgaido91 .
The original PR is split into two (this and #25029) according to the review comment.
This is a new feature at Spark 3.0.0 and will be consistent with PostgreSQL soon.
Merged to master to move forward.
peter-toth
commented
Jul 4, 2019
Thanks @dongjoon-hyun, @maropu, @mgaido91 for the review! I will prepare #25029 for review soon. |
What changes were proposed in this pull request?
This PR adds support of
WITHclause within a subquery so this query becomes valid:How was this patch tested?
Added new UTs.