dtenedor commented on code in PR #44093:
URL: https://github.com/apache/spark/pull/44093#discussion_r1421247769
##########
common/utils/src/main/resources/error/error-classes.json:
##########
@@ -1005,6 +1005,12 @@
],
"sqlState" : "42702"
},
+ "EXEC_IMMEDIATE_DUPLICATE_ARGUMENT_ALIASES" : {
+ "message" : [
+ "Using statement contains multiple arguments with same alias
(<aliases>)."
Review Comment:
```suggestion
"The USING clause of this EXECUTE IMMEDIATE command contained multiple
arguments with same alias (<aliases>), which is invalid; please update the
command to specify unique aliases and then try it again."
```
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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 scala.util.{Either, Left, Right}
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.expressions.{Alias, Expression,
NamedExpression}
+import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
+import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, SetVariable}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{COMMAND,
EXECUTE_IMMEDIATE, TreePattern}
+import org.apache.spark.sql.connector.catalog.CatalogManager
+import
org.apache.spark.sql.errors.QueryCompilationErrors.unresolvedVariableError
+import org.apache.spark.sql.types.StringType
+
+/**
+ * Logical plan representing execute immediate query.
+ *
+ * @param args parameters of query
+ * @param query query string or variable
+ * @param targetVariables variables to store the result of the query
+ */
+case class ExecuteImmediateQuery(
+ args: Seq[Expression],
+ query: Either[String, UnresolvedAttribute],
+ targetVariables: Option[Seq[UnresolvedAttribute]]) extends
UnresolvedLeafNode {
+ final override val nodePatterns: Seq[TreePattern] = Seq(EXECUTE_IMMEDIATE)
+}
+
+/**
+ * This rule substitutes execute immediate query node with plan that is passed
as string literal
+ * or session parameter.
+ */
+class SubstituteExecuteImmediate(val catalogManager: CatalogManager)
+ extends Rule[LogicalPlan] with ColumnResolutionHelper {
+
+ def resolveVariable(e: Expression) : Expression = {
+ /* We know that the expression is either UnresolvedAttribute or Alias,
+ * as passed from the parser.
+ * If it is an UnresolvedAttribute, we look it up in the catalog and
return it.
+ * If it is an Alias, we resolve the child and return an Alias with the
same name.
+ */
+ e match {
+ case u: UnresolvedAttribute =>
+ lookupVariable(u.nameParts) match {
+ case Some(variable) =>
+ variable.copy(canFold = false)
+ case _ => throw unresolvedVariableError(u.nameParts, Seq("SYSTEM",
"SESSION"))
+ }
+
+ case a: Alias =>
+ Alias(resolveVariable(a.child), a.name)()
+
+ case other => throw SparkException.internalError(
+ "Unexpected variable expression in ParametrizedQuery: " + other)
+ }
+ }
+
+ def resolveArguments(expressions : Seq[Expression]): Seq[Expression] = {
+ expressions.map { exp =>
+ if (exp.resolved) {
+ exp
+ } else {
+ resolveVariable(exp)
+ }
+ }
+ }
+
+ def extractQueryString(either : Either[String, UnresolvedAttribute]) :
String = {
+ either match {
+ case Left(v) => v
+ case Right(u) =>
+ val varReference = lookupVariable(u.nameParts) match {
+ case Some(variable) => variable.copy(canFold = false)
+ case _ => throw unresolvedVariableError(u.nameParts, Seq("SYSTEM",
"SESSION"))
+ }
+
+ if (!varReference.dataType.sameType(StringType)) {
+ throw new AnalysisException(
+ errorClass = "INVALID_VARIABLE_TYPE_FOR_QUERY_EXECUTE_IMMEDIATE",
+ messageParameters = Map(
+ "varType" -> varReference.dataType.simpleString))
+ }
+
+ // call eval with null row.
+ // this is ok as this is variable and invoking eval should
+ // be independent of row
+ varReference.eval(null).toString
+ }
+ }
+
+ override def apply(plan: LogicalPlan): LogicalPlan =
plan.resolveOperatorsWithPruning(
+ _.containsPattern(EXECUTE_IMMEDIATE), ruleId) {
+ case ExecuteImmediateQuery(expressions, query, targetVariablesOpt) =>
+ val queryString = extractQueryString(query)
+ val plan = CatalystSqlParser.parsePlan(queryString);
+
+ val posNodes = plan.collect {
+ case p: LogicalPlan =>
+ p.expressions.flatMap(_.collect { case n: PosParameter => n })
+ }.flatten
+ val namedNodes = plan.collect {
+ case p: LogicalPlan =>
+ p.expressions.flatMap(_.collect{ case n: NamedParameter => n })
+ }.flatten
+
+ val queryPlan = if (expressions.isEmpty || (posNodes.isEmpty &&
namedNodes.isEmpty)) {
+ plan
+ } else if (posNodes.nonEmpty && namedNodes.nonEmpty) {
+ throw new AnalysisException(
+ errorClass =
"INVALID_QUERY_BOTH_POSITIONAL_AND_NAMED_PARAMETERS_PRESENT",
+ messageParameters = Map.empty)
+ } else {
Review Comment:
you can drop the `else` since you raise an error in the previous case,
de-denting the rest of the function.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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 scala.util.{Either, Left, Right}
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.expressions.{Alias, Expression,
NamedExpression}
+import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
+import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, SetVariable}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{COMMAND,
EXECUTE_IMMEDIATE, TreePattern}
+import org.apache.spark.sql.connector.catalog.CatalogManager
+import
org.apache.spark.sql.errors.QueryCompilationErrors.unresolvedVariableError
+import org.apache.spark.sql.types.StringType
+
+/**
+ * Logical plan representing execute immediate query.
+ *
+ * @param args parameters of query
+ * @param query query string or variable
+ * @param targetVariables variables to store the result of the query
+ */
+case class ExecuteImmediateQuery(
+ args: Seq[Expression],
+ query: Either[String, UnresolvedAttribute],
+ targetVariables: Option[Seq[UnresolvedAttribute]]) extends
UnresolvedLeafNode {
+ final override val nodePatterns: Seq[TreePattern] = Seq(EXECUTE_IMMEDIATE)
+}
+
+/**
+ * This rule substitutes execute immediate query node with plan that is passed
as string literal
+ * or session parameter.
+ */
+class SubstituteExecuteImmediate(val catalogManager: CatalogManager)
+ extends Rule[LogicalPlan] with ColumnResolutionHelper {
+
+ def resolveVariable(e: Expression) : Expression = {
+ /* We know that the expression is either UnresolvedAttribute or Alias,
+ * as passed from the parser.
+ * If it is an UnresolvedAttribute, we look it up in the catalog and
return it.
+ * If it is an Alias, we resolve the child and return an Alias with the
same name.
+ */
+ e match {
+ case u: UnresolvedAttribute =>
+ lookupVariable(u.nameParts) match {
+ case Some(variable) =>
+ variable.copy(canFold = false)
+ case _ => throw unresolvedVariableError(u.nameParts, Seq("SYSTEM",
"SESSION"))
+ }
+
+ case a: Alias =>
+ Alias(resolveVariable(a.child), a.name)()
+
+ case other => throw SparkException.internalError(
+ "Unexpected variable expression in ParametrizedQuery: " + other)
+ }
+ }
+
+ def resolveArguments(expressions : Seq[Expression]): Seq[Expression] = {
+ expressions.map { exp =>
+ if (exp.resolved) {
+ exp
+ } else {
+ resolveVariable(exp)
+ }
+ }
+ }
+
+ def extractQueryString(either : Either[String, UnresolvedAttribute]) :
String = {
+ either match {
+ case Left(v) => v
+ case Right(u) =>
+ val varReference = lookupVariable(u.nameParts) match {
+ case Some(variable) => variable.copy(canFold = false)
+ case _ => throw unresolvedVariableError(u.nameParts, Seq("SYSTEM",
"SESSION"))
Review Comment:
these strings also appear on L65 above; please deduplicate into one place?
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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 scala.util.{Either, Left, Right}
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.expressions.{Alias, Expression,
NamedExpression}
+import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
+import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, SetVariable}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{COMMAND,
EXECUTE_IMMEDIATE, TreePattern}
+import org.apache.spark.sql.connector.catalog.CatalogManager
+import
org.apache.spark.sql.errors.QueryCompilationErrors.unresolvedVariableError
+import org.apache.spark.sql.types.StringType
+
+/**
+ * Logical plan representing execute immediate query.
+ *
+ * @param args parameters of query
+ * @param query query string or variable
+ * @param targetVariables variables to store the result of the query
+ */
+case class ExecuteImmediateQuery(
+ args: Seq[Expression],
+ query: Either[String, UnresolvedAttribute],
+ targetVariables: Option[Seq[UnresolvedAttribute]]) extends
UnresolvedLeafNode {
+ final override val nodePatterns: Seq[TreePattern] = Seq(EXECUTE_IMMEDIATE)
+}
+
+/**
+ * This rule substitutes execute immediate query node with plan that is passed
as string literal
+ * or session parameter.
+ */
+class SubstituteExecuteImmediate(val catalogManager: CatalogManager)
+ extends Rule[LogicalPlan] with ColumnResolutionHelper {
+
+ def resolveVariable(e: Expression) : Expression = {
+ /* We know that the expression is either UnresolvedAttribute or Alias,
+ * as passed from the parser.
+ * If it is an UnresolvedAttribute, we look it up in the catalog and
return it.
+ * If it is an Alias, we resolve the child and return an Alias with the
same name.
+ */
+ e match {
+ case u: UnresolvedAttribute =>
+ lookupVariable(u.nameParts) match {
+ case Some(variable) =>
+ variable.copy(canFold = false)
+ case _ => throw unresolvedVariableError(u.nameParts, Seq("SYSTEM",
"SESSION"))
+ }
+
+ case a: Alias =>
+ Alias(resolveVariable(a.child), a.name)()
+
+ case other => throw SparkException.internalError(
+ "Unexpected variable expression in ParametrizedQuery: " + other)
+ }
+ }
+
+ def resolveArguments(expressions : Seq[Expression]): Seq[Expression] = {
+ expressions.map { exp =>
+ if (exp.resolved) {
+ exp
+ } else {
+ resolveVariable(exp)
+ }
+ }
+ }
+
+ def extractQueryString(either : Either[String, UnresolvedAttribute]) :
String = {
+ either match {
+ case Left(v) => v
+ case Right(u) =>
+ val varReference = lookupVariable(u.nameParts) match {
+ case Some(variable) => variable.copy(canFold = false)
+ case _ => throw unresolvedVariableError(u.nameParts, Seq("SYSTEM",
"SESSION"))
+ }
+
+ if (!varReference.dataType.sameType(StringType)) {
+ throw new AnalysisException(
+ errorClass = "INVALID_VARIABLE_TYPE_FOR_QUERY_EXECUTE_IMMEDIATE",
+ messageParameters = Map(
+ "varType" -> varReference.dataType.simpleString))
+ }
+
+ // call eval with null row.
Review Comment:
please update the comments to complete sentences (each starting with a
capital letter and ending with punctuation); here and elsewhere in the PR
##########
common/utils/src/main/resources/error/error-classes.json:
##########
@@ -2338,6 +2356,12 @@
],
"sqlState" : "42000"
},
+ "INVALID_VARIABLE_TYPE_FOR_QUERY_EXECUTE_IMMEDIATE" : {
+ "message" : [
+ "Variable type must be string type but got <varType>."
Review Comment:
```suggestion
"Failed to run the EXECUTE IMMEDIATE command because the variable type
must have string type, but instead it was <varType>."
```
##########
common/utils/src/main/resources/error/error-classes.json:
##########
@@ -2261,6 +2273,12 @@
},
"sqlState" : "42000"
},
+ "INVALID_STATEMENT_FOR_EXECUTE_INTO" : {
+ "message" : [
+ "The INTO clause of EXECUTE IMMEDIATE is only valid for queries but the
given statement isn't: <sqlString>."
Review Comment:
```suggestion
"Failed to run this EXECUTE IMMEDIATE command because the INTO clause
is only valid for queries, but the given statement is not a query: <sqlString>."
```
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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 scala.util.{Either, Left, Right}
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.expressions.{Alias, Expression,
NamedExpression}
+import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
+import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, SetVariable}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{COMMAND,
EXECUTE_IMMEDIATE, TreePattern}
+import org.apache.spark.sql.connector.catalog.CatalogManager
+import
org.apache.spark.sql.errors.QueryCompilationErrors.unresolvedVariableError
+import org.apache.spark.sql.types.StringType
+
+/**
+ * Logical plan representing execute immediate query.
+ *
+ * @param args parameters of query
+ * @param query query string or variable
+ * @param targetVariables variables to store the result of the query
+ */
+case class ExecuteImmediateQuery(
+ args: Seq[Expression],
+ query: Either[String, UnresolvedAttribute],
+ targetVariables: Option[Seq[UnresolvedAttribute]]) extends
UnresolvedLeafNode {
+ final override val nodePatterns: Seq[TreePattern] = Seq(EXECUTE_IMMEDIATE)
+}
+
+/**
+ * This rule substitutes execute immediate query node with plan that is passed
as string literal
+ * or session parameter.
+ */
+class SubstituteExecuteImmediate(val catalogManager: CatalogManager)
+ extends Rule[LogicalPlan] with ColumnResolutionHelper {
+
+ def resolveVariable(e: Expression) : Expression = {
+ /* We know that the expression is either UnresolvedAttribute or Alias,
Review Comment:
```suggestion
/**
* We know that the expression is either UnresolvedAttribute or Alias,
```
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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 scala.util.{Either, Left, Right}
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.expressions.{Alias, Expression,
NamedExpression}
+import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
+import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, SetVariable}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{COMMAND,
EXECUTE_IMMEDIATE, TreePattern}
+import org.apache.spark.sql.connector.catalog.CatalogManager
+import
org.apache.spark.sql.errors.QueryCompilationErrors.unresolvedVariableError
+import org.apache.spark.sql.types.StringType
+
+/**
+ * Logical plan representing execute immediate query.
+ *
+ * @param args parameters of query
+ * @param query query string or variable
+ * @param targetVariables variables to store the result of the query
+ */
+case class ExecuteImmediateQuery(
+ args: Seq[Expression],
+ query: Either[String, UnresolvedAttribute],
+ targetVariables: Option[Seq[UnresolvedAttribute]]) extends
UnresolvedLeafNode {
+ final override val nodePatterns: Seq[TreePattern] = Seq(EXECUTE_IMMEDIATE)
+}
+
+/**
+ * This rule substitutes execute immediate query node with plan that is passed
as string literal
+ * or session parameter.
+ */
+class SubstituteExecuteImmediate(val catalogManager: CatalogManager)
+ extends Rule[LogicalPlan] with ColumnResolutionHelper {
+
+ def resolveVariable(e: Expression) : Expression = {
+ /* We know that the expression is either UnresolvedAttribute or Alias,
+ * as passed from the parser.
+ * If it is an UnresolvedAttribute, we look it up in the catalog and
return it.
+ * If it is an Alias, we resolve the child and return an Alias with the
same name.
+ */
+ e match {
+ case u: UnresolvedAttribute =>
+ lookupVariable(u.nameParts) match {
+ case Some(variable) =>
+ variable.copy(canFold = false)
+ case _ => throw unresolvedVariableError(u.nameParts, Seq("SYSTEM",
"SESSION"))
+ }
+
+ case a: Alias =>
+ Alias(resolveVariable(a.child), a.name)()
+
+ case other => throw SparkException.internalError(
+ "Unexpected variable expression in ParametrizedQuery: " + other)
+ }
+ }
+
+ def resolveArguments(expressions : Seq[Expression]): Seq[Expression] = {
+ expressions.map { exp =>
+ if (exp.resolved) {
+ exp
+ } else {
+ resolveVariable(exp)
+ }
+ }
+ }
+
+ def extractQueryString(either : Either[String, UnresolvedAttribute]) :
String = {
Review Comment:
please update formatting to not leave a space before each `:` as specified
by the Databricks Scala style guide:
https://github.com/databricks/scala-style-guide
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/executeImmediate.scala:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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 scala.util.{Either, Left, Right}
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.expressions.{Alias, Expression,
NamedExpression}
+import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
+import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, SetVariable}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreePattern.{COMMAND,
EXECUTE_IMMEDIATE, TreePattern}
+import org.apache.spark.sql.connector.catalog.CatalogManager
+import
org.apache.spark.sql.errors.QueryCompilationErrors.unresolvedVariableError
+import org.apache.spark.sql.types.StringType
+
+/**
+ * Logical plan representing execute immediate query.
+ *
+ * @param args parameters of query
+ * @param query query string or variable
+ * @param targetVariables variables to store the result of the query
+ */
+case class ExecuteImmediateQuery(
+ args: Seq[Expression],
+ query: Either[String, UnresolvedAttribute],
+ targetVariables: Option[Seq[UnresolvedAttribute]]) extends
UnresolvedLeafNode {
+ final override val nodePatterns: Seq[TreePattern] = Seq(EXECUTE_IMMEDIATE)
+}
+
+/**
+ * This rule substitutes execute immediate query node with plan that is passed
as string literal
+ * or session parameter.
+ */
+class SubstituteExecuteImmediate(val catalogManager: CatalogManager)
+ extends Rule[LogicalPlan] with ColumnResolutionHelper {
+
+ def resolveVariable(e: Expression) : Expression = {
+ /* We know that the expression is either UnresolvedAttribute or Alias,
+ * as passed from the parser.
+ * If it is an UnresolvedAttribute, we look it up in the catalog and
return it.
+ * If it is an Alias, we resolve the child and return an Alias with the
same name.
+ */
+ e match {
+ case u: UnresolvedAttribute =>
+ lookupVariable(u.nameParts) match {
+ case Some(variable) =>
+ variable.copy(canFold = false)
+ case _ => throw unresolvedVariableError(u.nameParts, Seq("SYSTEM",
"SESSION"))
+ }
+
+ case a: Alias =>
+ Alias(resolveVariable(a.child), a.name)()
+
+ case other => throw SparkException.internalError(
+ "Unexpected variable expression in ParametrizedQuery: " + other)
+ }
+ }
+
+ def resolveArguments(expressions : Seq[Expression]): Seq[Expression] = {
+ expressions.map { exp =>
+ if (exp.resolved) {
+ exp
+ } else {
+ resolveVariable(exp)
+ }
+ }
+ }
+
+ def extractQueryString(either : Either[String, UnresolvedAttribute]) :
String = {
+ either match {
+ case Left(v) => v
Review Comment:
please fix indentation (-2 spaces here and for the rest of the `match` block)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]