-
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 2 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,24 +17,57 @@ | |
|
|
||
| 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 | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.internal.SQLConf.LEGACY_CTE_PRECEDENCE_ENABLED | ||
|
|
||
| /** | ||
| * Analyze WITH nodes and substitute child plan with CTE definitions. | ||
| */ | ||
| object CTESubstitution extends Rule[LogicalPlan] { | ||
| def apply(plan: LogicalPlan): LogicalPlan = { | ||
| if (SQLConf.get.getConf(LEGACY_CTE_PRECEDENCE_ENABLED)) { | ||
| if (SQLConf.get.legacyCTEPrecedenceEnabled.isEmpty) { | ||
xuanyuanking marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (hasNestedCTE(plan, inTraverse = false)) { | ||
|
||
| throw new AnalysisException("The ambiguous nested CTE was founded in current plan, " + | ||
| "please select the CTE precedence via spark.sql.legacy.ctePrecedence.enabled, " + | ||
| "see more detail in SPARK-28228.") | ||
| } else { | ||
| traverseAndSubstituteCTE(plan, inTraverse = false) | ||
| } | ||
| } else if (SQLConf.get.legacyCTEPrecedenceEnabled.get) { | ||
| legacyTraverseAndSubstituteCTE(plan) | ||
| } else { | ||
| traverseAndSubstituteCTE(plan, false) | ||
| traverseAndSubstituteCTE(plan, inTraverse = false) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Check the plan to be traversed has nested CTE or not, find the nested WITH clause in | ||
| * child, innerChildren and subquery for the current plan. | ||
| */ | ||
| private def hasNestedCTE(plan: LogicalPlan, inTraverse: Boolean): Boolean = { | ||
| plan.foreach { | ||
| case _: With if inTraverse => | ||
| return true | ||
|
|
||
| case w @ With(child: LogicalPlan, _) => | ||
| (w.innerChildren :+ child).foreach { p => | ||
|
||
| if (hasNestedCTE(p, inTraverse = true)) return true | ||
| } | ||
|
|
||
| case other if inTraverse => | ||
| other.transformExpressions { | ||
| case e: SubqueryExpression if hasNestedCTE(e.plan, inTraverse = true) => | ||
| 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") | ||
|
|
@@ -2714,6 +2714,8 @@ class SQLConf extends Serializable with Logging { | |
|
|
||
| def csvFilterPushDown: Boolean = getConf(CSV_FILTER_PUSHDOWN_ENABLED) | ||
|
|
||
| def legacyCTEPrecedenceEnabled: Option[Boolean] = getConf(LEGACY_CTE_PRECEDENCE_ENABLED) | ||
xuanyuanking marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** ********************** SQLConf functionality methods ************ */ | ||
|
|
||
| /** Set Spark SQL configuration properties. */ | ||
|
|
||
| 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.