You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by ul...@apache.org on 2022/05/09 10:00:02 UTC

[incubator-kyuubi] branch master updated: [KYUUBI #2565] Variable substitution should work in plan only mode

This is an automated email from the ASF dual-hosted git repository.

ulyssesyou pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-kyuubi.git


The following commit(s) were added to refs/heads/master by this push:
     new 7f8369bf1 [KYUUBI #2565] Variable substitution should work in plan only mode
7f8369bf1 is described below

commit 7f8369bf18f8e37b31818e55877827c725696c89
Author: Fu Chen <cf...@gmail.com>
AuthorDate: Mon May 9 17:59:50 2022 +0800

    [KYUUBI #2565] Variable substitution should work in plan only mode
    
    ### _Why are the changes needed?_
    
    close #2565
    
    ### _How was this patch tested?_
    - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible
    
    - [ ] Add screenshots for manual tests if appropriate
    
    - [x] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request
    
    Closes #2566 from cfmcgrady/kyuubi-2565.
    
    Closes #2565
    
    4de27c1d [Fu Chen] address comment
    b19a53cb [Fu Chen] remove VariableSubstitution
    ddcfb39c [Fu Chen] fix style
    78588907 [Fu Chen] address comment
    c7f12565 [Fu Chen] fix style
    d0c362f7 [Fu Chen] Variable substitution in plan only mode
    
    Authored-by: Fu Chen <cf...@gmail.com>
    Signed-off-by: ulysses-you <ul...@apache.org>
---
 .../engine/spark/operation/PlanOnlyStatement.scala | 53 ++++++++++++----------
 .../kyuubi/operation/PlanOnlyOperationSuite.scala  | 11 +++++
 2 files changed, 39 insertions(+), 25 deletions(-)

diff --git a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/PlanOnlyStatement.scala b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/PlanOnlyStatement.scala
index 314099458..dab86ed7a 100644
--- a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/PlanOnlyStatement.scala
+++ b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/PlanOnlyStatement.scala
@@ -18,6 +18,7 @@
 package org.apache.kyuubi.engine.spark.operation
 
 import org.apache.spark.sql.Row
+import org.apache.spark.sql.internal.SQLConf
 import org.apache.spark.sql.types.StructType
 
 import org.apache.kyuubi.config.KyuubiConf.OPERATION_PLAN_ONLY_EXCLUDES
@@ -50,32 +51,34 @@ class PlanOnlyStatement(
     } else result.schema
   }
 
-  override protected def runInternal(): Unit = {
+  override protected def runInternal(): Unit = withLocalProperties {
     try {
-      val parsed = spark.sessionState.sqlParser.parsePlan(statement)
-      parsed match {
-        case cmd if planExcludes.contains(cmd.getClass.getSimpleName) =>
-          result = spark.sql(statement)
-          iter = new ArrayFetchIterator(result.collect())
-        case plan => mode match {
-            case PARSE =>
-              iter = new IterableFetchIterator(Seq(Row(plan.toString())))
-            case ANALYZE =>
-              val analyzed = spark.sessionState.analyzer.execute(plan)
-              spark.sessionState.analyzer.checkAnalysis(analyzed)
-              iter = new IterableFetchIterator(Seq(Row(analyzed.toString())))
-            case OPTIMIZE =>
-              val analyzed = spark.sessionState.analyzer.execute(plan)
-              spark.sessionState.analyzer.checkAnalysis(analyzed)
-              val optimized = spark.sessionState.optimizer.execute(analyzed)
-              iter = new IterableFetchIterator(Seq(Row(optimized.toString())))
-            case PHYSICAL =>
-              val physical = spark.sql(statement).queryExecution.sparkPlan
-              iter = new IterableFetchIterator(Seq(Row(physical.toString())))
-            case EXECUTION =>
-              val executed = spark.sql(statement).queryExecution.executedPlan
-              iter = new IterableFetchIterator(Seq(Row(executed.toString())))
-          }
+      SQLConf.withExistingConf(spark.sessionState.conf) {
+        val parsed = spark.sessionState.sqlParser.parsePlan(statement)
+        parsed match {
+          case cmd if planExcludes.contains(cmd.getClass.getSimpleName) =>
+            result = spark.sql(statement)
+            iter = new ArrayFetchIterator(result.collect())
+          case plan => mode match {
+              case PARSE =>
+                iter = new IterableFetchIterator(Seq(Row(plan.toString())))
+              case ANALYZE =>
+                val analyzed = spark.sessionState.analyzer.execute(plan)
+                spark.sessionState.analyzer.checkAnalysis(analyzed)
+                iter = new IterableFetchIterator(Seq(Row(analyzed.toString())))
+              case OPTIMIZE =>
+                val analyzed = spark.sessionState.analyzer.execute(plan)
+                spark.sessionState.analyzer.checkAnalysis(analyzed)
+                val optimized = spark.sessionState.optimizer.execute(analyzed)
+                iter = new IterableFetchIterator(Seq(Row(optimized.toString())))
+              case PHYSICAL =>
+                val physical = spark.sql(statement).queryExecution.sparkPlan
+                iter = new IterableFetchIterator(Seq(Row(physical.toString())))
+              case EXECUTION =>
+                val executed = spark.sql(statement).queryExecution.executedPlan
+                iter = new IterableFetchIterator(Seq(Row(executed.toString())))
+            }
+        }
       }
     } catch {
       onError()
diff --git a/kyuubi-server/src/test/scala/org/apache/kyuubi/operation/PlanOnlyOperationSuite.scala b/kyuubi-server/src/test/scala/org/apache/kyuubi/operation/PlanOnlyOperationSuite.scala
index 3e94a47ee..8b1eaef11 100644
--- a/kyuubi-server/src/test/scala/org/apache/kyuubi/operation/PlanOnlyOperationSuite.scala
+++ b/kyuubi-server/src/test/scala/org/apache/kyuubi/operation/PlanOnlyOperationSuite.scala
@@ -103,6 +103,17 @@ class PlanOnlyOperationSuite extends WithKyuubiServer with HiveJDBCTestHelper {
     }
   }
 
+  test("kyuubi #2565: Variable substitution should work in plan only mode") {
+    withSessionConf()(Map(KyuubiConf.OPERATION_PLAN_ONLY_MODE.key -> PARSE.toString))(Map.empty) {
+      withJdbcStatement() { statement =>
+        statement.executeQuery("set x = y")
+        val resultSet = statement.executeQuery("select '${x}'")
+        assert(resultSet.next())
+        resultSet.getString(1).contains("'Project [unresolvedalias(y, None)]")
+      }
+    }
+  }
+
   private def getOperationPlanWithStatement(statement: Statement): String = {
     val resultSet = statement.executeQuery("select 1 where true")
     assert(resultSet.next())