-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-19799][SQL] Support WITH clause in subqueries #24831
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -211,37 +211,6 @@ class Analyzer( | |
| CleanupAliases) | ||
| ) | ||
|
|
||
| /** | ||
| * Analyze cte definitions and substitute child plan with analyzed cte definitions. | ||
| */ | ||
| object CTESubstitution extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsUp { | ||
| case With(child, relations) => | ||
| // substitute CTE expressions right-to-left to resolve references to previous CTEs: | ||
| // with a as (select * from t), b as (select * from a) select * from b | ||
| relations.foldRight(child) { | ||
| case ((cteName, ctePlan), currentPlan) => | ||
| substituteCTE(currentPlan, cteName, ctePlan) | ||
| } | ||
| case other => other | ||
| } | ||
|
|
||
| def substituteCTE(plan: LogicalPlan, cteName: String, ctePlan: LogicalPlan): LogicalPlan = { | ||
| plan resolveOperatorsUp { | ||
| case UnresolvedRelation(Seq(table)) if resolver(cteName, table) => | ||
| ctePlan | ||
| case u: UnresolvedRelation => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why did you remove this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this line does anything nor
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I think you're right, I was just curious about the reason of this change |
||
| u | ||
| case other => | ||
| // This cannot be done in ResolveSubquery because ResolveSubquery does not know the CTE. | ||
| other transformExpressions { | ||
| case e: SubqueryExpression => | ||
| e.withNewPlan(substituteCTE(e.plan, cteName, ctePlan)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Substitute child plan with WindowSpecDefinitions. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.analysis | ||
|
|
||
| import org.apache.spark.sql.catalyst.expressions.SubqueryExpression | ||
| import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, With} | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
|
|
||
| /** | ||
| * Analyze WITH nodes and substitute child plan with CTE definitions. | ||
| */ | ||
| object CTESubstitution extends Rule[LogicalPlan] { | ||
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsUp { | ||
| case With(child, relations) => | ||
| // substitute CTE expressions right-to-left to resolve references to previous CTEs: | ||
| // with a as (select * from t), b as (select * from a) select * from b | ||
| relations.foldRight(child) { | ||
| case ((cteName, ctePlan), currentPlan) => substituteCTE(currentPlan, cteName, ctePlan) | ||
| } | ||
| case other => other | ||
| } | ||
|
|
||
| private def substituteCTE( | ||
| plan: LogicalPlan, | ||
| cteName: String, | ||
| ctePlan: LogicalPlan): LogicalPlan = { | ||
| plan resolveOperatorsUp { | ||
| case UnresolvedRelation(Seq(table)) if plan.conf.resolver(cteName, table) => ctePlan | ||
|
|
||
| case o => | ||
| // This cannot be done in ResolveSubquery because ResolveSubquery does not know the CTE. | ||
| o transformExpressions { | ||
| case e: SubqueryExpression => e.withNewPlan(substituteCTE(e.plan, cteName, ctePlan)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.