[TOREE-475] Resolve variables in sql statement
Closes #155
diff --git a/build.sbt b/build.sbt
index f76d466..1bafcf2 100644
--- a/build.sbt
+++ b/build.sbt
@@ -70,7 +70,6 @@
)
// 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 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.
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 @@
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()
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 @@
@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:_*)