toree-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lrese...@apache.org
Subject incubator-toree git commit: [TOREE-475] Resolve variables in sql statement
Date Fri, 28 Sep 2018 14:48:46 GMT
Repository: incubator-toree
Updated Branches:
  refs/heads/master 33b545740 -> 106d6f11e


[TOREE-475] Resolve variables in sql statement

Closes #155


Project: http://git-wip-us.apache.org/repos/asf/incubator-toree/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-toree/commit/106d6f11
Tree: http://git-wip-us.apache.org/repos/asf/incubator-toree/tree/106d6f11
Diff: http://git-wip-us.apache.org/repos/asf/incubator-toree/diff/106d6f11

Branch: refs/heads/master
Commit: 106d6f11e8f87d99a42b1fea25f93f0d4b4b8b8d
Parents: 33b5457
Author: huafengw <fvunicorn@gmail.com>
Authored: Thu May 24 15:50:35 2018 +0800
Committer: Luciano Resende <lresende@apache.org>
Committed: Fri Sep 28 07:47:36 2018 -0700

----------------------------------------------------------------------
 build.sbt                                       |  3 +--
 .../scala/ScalaInterpreterSpecific.scala        |  2 +-
 .../org/apache/toree/magic/builtin/Sql.scala    | 25 +++++++++++++-------
 3 files changed, 19 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/106d6f11/build.sbt
----------------------------------------------------------------------
diff --git a/build.sbt b/build.sbt
index f76d466..1bafcf2 100644
--- a/build.sbt
+++ b/build.sbt
@@ -70,7 +70,6 @@ javaOptions in ThisBuild ++= Seq(
 )
 // Add additional test option to show time taken per test
 testOptions in (ThisBuild, Test) += Tests.Argument("-oDF")
-
 // Build-wide dependencies
 resolvers in ThisBuild  ++= Seq(
   "Apache Snapshots" at "http://repository.apache.org/snapshots/",
@@ -186,7 +185,7 @@ lazy val scalaInterpreter = (project in file("scala-interpreter"))
 */
 lazy val sqlInterpreter = (project in file("sql-interpreter"))
   .settings(name := "toree-sql-interpreter")
-  .dependsOn(plugins, protocol, kernelApi)
+  .dependsOn(plugins, protocol, kernelApi, scalaInterpreter)
 
 /**
 * Project represents the Python interpreter used by the Spark Kernel.

http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/106d6f11/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala
----------------------------------------------------------------------
diff --git a/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala
b/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala
index fa667b9..1bde367 100644
--- a/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala
+++ b/scala-interpreter/src/main/scala-2.11/org/apache/toree/kernel/interpreter/scala/ScalaInterpreterSpecific.scala
@@ -31,7 +31,7 @@ import scala.util.Try
 trait ScalaInterpreterSpecific extends SettingsProducerLike { this: ScalaInterpreter =>
   private val ExecutionExceptionName = "lastException"
 
-  private var iMain: IMain = _
+  private[toree] var iMain: IMain = _
   private var completer: PresentationCompilerCompleter = _
   private val exceptionHack = new ExceptionHack()
 

http://git-wip-us.apache.org/repos/asf/incubator-toree/blob/106d6f11/sql-interpreter/src/main/scala/org/apache/toree/magic/builtin/Sql.scala
----------------------------------------------------------------------
diff --git a/sql-interpreter/src/main/scala/org/apache/toree/magic/builtin/Sql.scala b/sql-interpreter/src/main/scala/org/apache/toree/magic/builtin/Sql.scala
index 3dcf7e7..1d96494 100644
--- a/sql-interpreter/src/main/scala/org/apache/toree/magic/builtin/Sql.scala
+++ b/sql-interpreter/src/main/scala/org/apache/toree/magic/builtin/Sql.scala
@@ -16,9 +16,10 @@
  */
 package org.apache.toree.magic.builtin
 
-import org.apache.toree.interpreter.{ExecuteError, ExecuteAborted}
-import org.apache.toree.kernel.interpreter.sql.{SqlInterpreter, SqlException}
-import org.apache.toree.magic.{MagicOutput, CellMagic}
+import org.apache.toree.interpreter.{ExecuteAborted, ExecuteError}
+import org.apache.toree.kernel.interpreter.scala.ScalaInterpreter
+import org.apache.toree.kernel.interpreter.sql.{SqlException, SqlInterpreter}
+import org.apache.toree.magic.{CellMagic, MagicOutput}
 import org.apache.toree.magic.dependencies.IncludeKernel
 import org.apache.toree.plugins.annotations.Event
 
@@ -29,14 +30,22 @@ class Sql extends CellMagic with IncludeKernel {
 
   @Event(name = "sql")
   override def execute(code: String): MagicOutput = {
-    val sparkSQL = kernel.interpreter("SQL")
+    val sparkSql = kernel.interpreter("SQL")
 
-    if (sparkSQL.isEmpty || sparkSQL.get == null)
+    if (sparkSql.isEmpty || sparkSql.get == null)
       throw new SqlException("SQL is not available!")
 
-    sparkSQL.get match {
-      case sparkSQLInterpreter: SqlInterpreter =>
-        val (_, output) = sparkSQLInterpreter.interpret(code)
+    val scala = kernel.interpreter("Scala")
+    val evaluated = if (scala.nonEmpty && scala.get != null) {
+      val scalaInterpreter = scala.get.asInstanceOf[ScalaInterpreter]
+      scalaInterpreter.iMain.eval("s\"" + code.replace("\n", " ") + "\"").asInstanceOf[String]
+    } else {
+      code
+    }
+
+    sparkSql.get match {
+      case sqlInterpreter: SqlInterpreter =>
+        val (_, output) = sqlInterpreter.interpret(evaluated)
         output match {
           case Left(executeOutput) =>
             MagicOutput(executeOutput.toSeq:_*)


Mime
View raw message