-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-28228][SQL] Change the default behavior for name conflict in nested WITH clause #27454
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 3 commits
53699d1
f75b26a
a55e82d
c03920a
7249404
ebd337b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
|
|
||
| package org.apache.spark.sql.catalyst.analysis | ||
|
|
||
| import org.apache.spark.sql.AnalysisException | ||
| 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 | ||
|
|
@@ -28,13 +29,55 @@ import org.apache.spark.sql.internal.SQLConf.LEGACY_CTE_PRECEDENCE_ENABLED | |
| */ | ||
| object CTESubstitution extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = { | ||
| if (SQLConf.get.getConf(LEGACY_CTE_PRECEDENCE_ENABLED)) { | ||
| val isLegacy = SQLConf.get.getConf(LEGACY_CTE_PRECEDENCE_ENABLED) | ||
| if (isLegacy.isEmpty) { | ||
xuanyuanking marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (detectConflictInNestedCTE(plan, inTraverse = false)) { | ||
| throw new AnalysisException("Name collision in nested CTE was founded in current plan, " + | ||
xuanyuanking marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| s"please select the CTE precedence via ${LEGACY_CTE_PRECEDENCE_ENABLED.key}, " + | ||
xuanyuanking marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "see more detail in SPARK-28228.") | ||
| } else { | ||
| traverseAndSubstituteCTE(plan, inTraverse = false) | ||
| } | ||
| } else if (isLegacy.get) { | ||
| legacyTraverseAndSubstituteCTE(plan) | ||
| } else { | ||
| traverseAndSubstituteCTE(plan, false) | ||
| traverseAndSubstituteCTE(plan, inTraverse = false) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check the plan to be traversed has naming conflicts in nested CTE or not, traverse through | ||
| * child, innerChildren and subquery for the current plan. | ||
| */ | ||
| private def detectConflictInNestedCTE( | ||
xuanyuanking marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| plan: LogicalPlan, | ||
| inTraverse: Boolean, | ||
| cteNames: Set[String] = Set.empty): Boolean = { | ||
| plan.foreach { | ||
| case w @ With(child, relations) => | ||
| val newNames = relations.map { | ||
| case (cteName, _) => | ||
| if (cteNames.contains(cteName)) { | ||
| return true | ||
|
||
| } else { | ||
| cteName | ||
| } | ||
| }.toSet | ||
| (w.innerChildren :+ child).foreach { p => | ||
|
||
| if (detectConflictInNestedCTE(p, inTraverse = true, cteNames ++ newNames)) return true | ||
| } | ||
|
|
||
| case other if inTraverse => | ||
| other.transformExpressions { | ||
| case e: SubqueryExpression | ||
| if detectConflictInNestedCTE(e.plan, inTraverse = true, cteNames) => return true | ||
| } | ||
|
|
||
| case _ => | ||
| } | ||
| false | ||
| } | ||
|
|
||
| private def legacyTraverseAndSubstituteCTE(plan: LogicalPlan): LogicalPlan = { | ||
| plan.resolveOperatorsUp { | ||
| case With(child, relations) => | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2079,7 +2079,7 @@ object SQLConf { | |
| .internal() | ||
| .doc("When true, outer CTE definitions takes precedence over inner definitions.") | ||
|
||
| .booleanConf | ||
| .createWithDefault(false) | ||
| .createOptional | ||
|
|
||
| val LEGACY_ARRAY_EXISTS_FOLLOWS_THREE_VALUED_LOGIC = | ||
| buildConf("spark.sql.legacy.arrayExistsFollowsThreeValuedLogic") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| create temporary view t as select * from values 0, 1, 2 as t(id); | ||
| create temporary view t2 as select * from values 0, 1 as t(id); | ||
|
|
||
| -- CTE non-legacy substitution | ||
| SET spark.sql.legacy.ctePrecedence.enabled=false; | ||
|
||
|
|
||
|
||
| -- 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 | ||
| ) | ||
| ); | ||
|
|
||
| -- Clean up | ||
| DROP VIEW IF EXISTS t; | ||
| DROP VIEW IF EXISTS t2; | ||
Uh oh!
There was an error while loading. Please reload this page.