Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.apache.spark.SparkException
import org.apache.spark.sql.catalyst.expressions.{Alias, CreateArray, CreateMap, CreateNamedStruct, Expression, LeafExpression, Literal, MapFromArrays, MapFromEntries, SubqueryExpression, Unevaluable, VariableReference}
import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, SupervisingCommand}
import org.apache.spark.sql.catalyst.rules.Rule
import org.apache.spark.sql.catalyst.trees.TreePattern.{COMMAND, PARAMETER, PARAMETERIZED_QUERY, TreePattern, UNRESOLVED_WITH}
import org.apache.spark.sql.catalyst.trees.TreePattern.{COMMAND, PARAMETER, PARAMETERIZED_QUERY, TreePattern, UNRESOLVED_IDENTIFIER_WITH_CTE, UNRESOLVED_WITH}
import org.apache.spark.sql.errors.QueryErrorsBase
import org.apache.spark.sql.types.DataType

Expand Down Expand Up @@ -189,7 +189,8 @@ object BindParameters extends ParameterizedQueryProcessor with QueryErrorsBase {
// We should wait for `CTESubstitution` to resolve CTE before binding parameters, as CTE
// relations are not children of `UnresolvedWith`.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm thinking of a different approach: shall we explicitly match UnresolvedWith here and bind parameters in the CTE relations? Then we no longer need this hack to delay parameter binding.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can we do that in 2 steps: 1. fix the issue, 2. do the proposed refactoring? It seems less risky, so, the first patch could be backported more safely.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1

case NameParameterizedQuery(child, argNames, argValues)
if !child.containsPattern(UNRESOLVED_WITH) && argValues.forall(_.resolved) =>
if !child.containsAnyPattern(UNRESOLVED_WITH, UNRESOLVED_IDENTIFIER_WITH_CTE) &&
argValues.forall(_.resolved) =>
if (argNames.length != argValues.length) {
throw SparkException.internalError(s"The number of argument names ${argNames.length} " +
s"must be equal to the number of argument values ${argValues.length}.")
Expand All @@ -199,7 +200,8 @@ object BindParameters extends ParameterizedQueryProcessor with QueryErrorsBase {
bind(child) { case NamedParameter(name) if args.contains(name) => args(name) }

case PosParameterizedQuery(child, args)
if !child.containsPattern(UNRESOLVED_WITH) && args.forall(_.resolved) =>
if !child.containsAnyPattern(UNRESOLVED_WITH, UNRESOLVED_IDENTIFIER_WITH_CTE) &&
args.forall(_.resolved) =>
val indexedArgs = args.zipWithIndex
checkArgs(indexedArgs.map(arg => (s"_${arg._2}", arg._1)))

Expand Down
17 changes: 17 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/ParametersSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -741,4 +741,21 @@ class ParametersSuite extends QueryTest with SharedSparkSession with PlanTest {
Row("c1"))
}
}

test("SPARK-50322: parameterized identifier in a sub-query") {
withTable("tt1") {
sql("CREATE TABLE tt1 (c1 INT)")
sql("INSERT INTO tt1 VALUES (1)")
def query(p: String): String = {
s"""
|WITH v1 AS (
| SELECT * FROM tt1
| WHERE 1 = (SELECT * FROM IDENTIFIER($p))
|) SELECT * FROM v1""".stripMargin
}

checkAnswer(spark.sql(query(":tab"), args = Map("tab" -> "tt1")), Row(1))
checkAnswer(spark.sql(query("?"), args = Array("tt1")), Row(1))
}
}
}