Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[SPARK-19799][SQL] Support WITH clause in subqueries
  • Loading branch information
peter-toth committed Jul 2, 2019
commit cff4becc1179d2c980e0b5b93ea26c31100824ee
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ queryPrimary
| fromStatement #fromStmt
| TABLE multipartIdentifier #table
| inlineTable #inlineTableDefault1
| '(' queryNoWith ')' #subquery
| '(' query ')' #subquery
;

sortItem
Expand Down Expand Up @@ -583,7 +583,7 @@ identifierComment

relationPrimary
: multipartIdentifier sample? tableAlias #tableName
| '(' queryNoWith ')' sample? tableAlias #aliasedQuery
| '(' query ')' sample? tableAlias #aliasedQuery
| '(' relation ')' sample? tableAlias #aliasedRelation
| inlineTable #inlineTableDefault2
| functionTable #tableValuedFunction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Copy link
Contributor

Choose a reason for hiding this comment

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

why did you remove this?

Copy link
Contributor Author

@peter-toth peter-toth Jul 3, 2019

Choose a reason for hiding this comment

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

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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
*/
Expand Down
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] {
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 avoid moving the class, in order to keep the diff smaller?

Copy link
Contributor Author

@peter-toth peter-toth Jul 3, 2019

Choose a reason for hiding this comment

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

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?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that was what I meant, we can move the rule in the other PR which refactors it more thoroughly.

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))
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
* Create a logical plan for a sub-query.
*/
override def visitSubquery(ctx: SubqueryContext): LogicalPlan = withOrigin(ctx) {
plan(ctx.queryNoWith)
plan(ctx.query)
}

/**
Expand Down Expand Up @@ -978,7 +978,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
* }}}
*/
override def visitAliasedQuery(ctx: AliasedQueryContext): LogicalPlan = withOrigin(ctx) {
val relation = plan(ctx.queryNoWith).optionalMap(ctx.sample)(withSample)
val relation = plan(ctx.query).optionalMap(ctx.sample)(withSample)
if (ctx.tableAlias.strictIdentifier == null) {
// For un-aliased subqueries, use a default alias name that is not likely to conflict with
// normal subquery names, so that parent operators can only access the columns in subquery by
Expand Down
113 changes: 113 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/cte.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,119 @@ FROM CTE1 t1
WITH t(x) AS (SELECT 1)
SELECT * FROM t WHERE x = 1;

-- CTE in CTE definition
WITH t as (
WITH t2 AS (SELECT 1)
SELECT * FROM t2
)
SELECT * FROM t;

-- CTE in subquery
SELECT max(c) FROM (
WITH t(c) AS (SELECT 1)
SELECT * FROM t
);

-- CTE in subquery expression
SELECT (
WITH t AS (SELECT 1)
SELECT * FROM t
);

-- CTE in CTE definition shadows outer
WITH
t AS (SELECT 1),
t2 AS (
WITH t AS (SELECT 2)
SELECT * FROM t
)
SELECT * FROM t2;

-- CTE in CTE definition shadows outer 2"
WITH
t(c) AS (SELECT 1),
t2 AS (
SELECT (
SELECT max(c) FROM (
WITH t(c) AS (SELECT 2)
SELECT * FROM t
)
)
)
SELECT * FROM t2;

-- CTE in CTE definition shadows outer 3
WITH
t AS (SELECT 1),
t2 AS (
WITH t AS (SELECT 2),
t2 AS (
WITH t AS (SELECT 3)
SELECT * FROM t
)
SELECT * FROM t2
)
SELECT * FROM t2;

-- CTE in subquery shadows outer
WITH t(c) AS (SELECT 1)
SELECT max(c) FROM (
WITH t(c) AS (SELECT 2)
SELECT * FROM t
);

-- CTE in subquery shadows outer 2
WITH t(c) AS (SELECT 1)
SELECT sum(c) FROM (
SELECT max(c) AS c FROM (
WITH t(c) AS (SELECT 2)
SELECT * FROM t
)
);

-- CTE in subquery shadows outer 3
WITH t(c) AS (SELECT 1)
SELECT sum(c) FROM (
WITH t(c) AS (SELECT 2)
SELECT max(c) AS c FROM (
WITH t(c) AS (SELECT 3)
SELECT * FROM t
)
);

-- CTE in subquery expression shadows outer
WITH t AS (SELECT 1)
SELECT (
WITH t AS (SELECT 2)
SELECT * FROM t
);

-- CTE in subquery expression shadows outer 2
WITH t AS (SELECT 1)
SELECT (
SELECT (
WITH t AS (SELECT 2)
SELECT * FROM t
)
);

-- CTE in subquery expression shadows outer 3
WITH t AS (SELECT 1)
SELECT (
WITH t AS (SELECT 2)
SELECT (
WITH t AS (SELECT 3)
SELECT * FROM t
)
);

-- no infinite recursion during CTE substitution
WITH r AS (SELECT * FROM r)
SELECT * FROM r;

WITH r AS (SELECT (SELECT * FROM r))
SELECT * FROM r;

-- Clean up
DROP VIEW IF EXISTS t;
DROP VIEW IF EXISTS t2;
Loading