You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kyuubi.apache.org by fe...@apache.org on 2021/12/06 11:51:27 UTC

[incubator-kyuubi] branch master updated: [KYUUBI #1504] Support to capture console out/error into interpreter output for ExecuteScala

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

feiwang 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 920c8e5  [KYUUBI #1504] Support to capture console out/error into interpreter output for ExecuteScala
920c8e5 is described below

commit 920c8e5ad65a6160358d98d1956c2dc864c89d1b
Author: Fei Wang <fw...@ebay.com>
AuthorDate: Mon Dec 6 19:51:13 2021 +0800

    [KYUUBI #1504] Support to capture console out/error into interpreter output for ExecuteScala
    
    <!--
    Thanks for sending a pull request!
    
    Here are some tips for you:
      1. If this is your first time, please read our contributor guidelines: https://kyuubi.readthedocs.io/en/latest/community/contributions.html
      2. If the PR is related to an issue in https://github.com/apache/incubator-kyuubi/issues, add '[KYUUBI #XXXX]' in your PR title, e.g., '[KYUUBI #XXXX] Your PR title ...'.
      3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][KYUUBI #XXXX] Your PR title ...'.
    -->
    
    ### _Why are the changes needed?_
    <!--
    Please clarify why the changes are needed. For instance,
      1. If you add a feature, you can talk about the use case of it.
      2. If you fix a bug, you can clarify why it is a bug.
    -->
    Redirect console output and error when interpreting scala code, see details in #1504
    
    ### _How was this patch tested?_
    - [x] 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.readthedocs.io/en/latest/develop_tools/testing.html#running-tests) locally before make a pull request
    
    Closes #1500 from turboFei/scala_out.
    
    Closes #1504
    
    c3a7df1b [Fei Wang] refactor
    d85b6791 [Fei Wang] address comments
    f19be18f [Fei Wang] redirect output error
    
    Authored-by: Fei Wang <fw...@ebay.com>
    Signed-off-by: Fei Wang <fw...@ebay.com>
---
 .../kyuubi/engine/spark/operation/ExecuteScala.scala       |  2 +-
 .../apache/kyuubi/engine/spark/repl/KyuubiSparkILoop.scala |  8 ++++++++
 .../org/apache/kyuubi/operation/SparkQueryTests.scala      | 14 ++++++++++++++
 3 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/ExecuteScala.scala b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/ExecuteScala.scala
index 9e8ca37..0278cc6 100644
--- a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/ExecuteScala.scala
+++ b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/operation/ExecuteScala.scala
@@ -57,7 +57,7 @@ class ExecuteScala(
     try {
       spark.sparkContext.setJobGroup(statementId, statement)
       Thread.currentThread().setContextClassLoader(spark.sharedState.jarClassLoader)
-      repl.interpret(statement) match {
+      repl.interpretWithRedirectOutError(statement) match {
         case Success =>
           iter =
             if (repl.results.nonEmpty) {
diff --git a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/repl/KyuubiSparkILoop.scala b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/repl/KyuubiSparkILoop.scala
index e1da8f7..6bc1a28 100644
--- a/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/repl/KyuubiSparkILoop.scala
+++ b/externals/kyuubi-spark-sql-engine/src/main/scala/org/apache/kyuubi/engine/spark/repl/KyuubiSparkILoop.scala
@@ -93,6 +93,14 @@ private[spark] case class KyuubiSparkILoop private (
     }
   }
 
+  def interpretWithRedirectOutError(statement: String): scala.tools.nsc.interpreter.IR.Result = {
+    Console.withOut(output) {
+      Console.withErr(output) {
+        this.interpret(statement)
+      }
+    }
+  }
+
   def getOutput: String = {
     val res = output.toString.trim
     output.reset()
diff --git a/kyuubi-common/src/test/scala/org/apache/kyuubi/operation/SparkQueryTests.scala b/kyuubi-common/src/test/scala/org/apache/kyuubi/operation/SparkQueryTests.scala
index c68ed1a..44d1375 100644
--- a/kyuubi-common/src/test/scala/org/apache/kyuubi/operation/SparkQueryTests.scala
+++ b/kyuubi-common/src/test/scala/org/apache/kyuubi/operation/SparkQueryTests.scala
@@ -518,4 +518,18 @@ trait SparkQueryTests extends HiveJDBCTestHelper {
       assert(e.getMessage contains "not found: value y")
     }
   }
+
+  test("scala code with console output") {
+    withJdbcStatement() { statement =>
+      statement.execute("SET kyuubi.operation.language=scala")
+      val code = """spark.sql("SET kyuubi.operation.language").show(false)"""
+      val rs = statement.executeQuery(code)
+
+      var foundOperationLangItem = false
+      while (rs.next() && !foundOperationLangItem) {
+        foundOperationLangItem = rs.getString(1).contains("kyuubi.operation.language")
+      }
+      assert(foundOperationLangItem)
+    }
+  }
 }