Skip to content

Commit e41e2fd

Browse files
author
Davies Liu
committed
[SPARK-8461] [SQL] fix codegen with REPL class loader
The ExecutorClassLoader for REPL will cause Janino failed to find class for those in java.lang, so switch to use default class loader for Janino, which will also help performance. cc liancheng yhuai Author: Davies Liu <[email protected]> Closes apache#6898 from davies/fix_class_loader and squashes the following commits: 24276d4 [Davies Liu] add regression test 4ff0457 [Davies Liu] address comment, refactor 7f5ffbe [Davies Liu] fix REPL class loader with codegen
1 parent 4a462c2 commit e41e2fd

File tree

6 files changed

+29
-34
lines changed

6 files changed

+29
-34
lines changed

repl/scala-2.10/src/test/scala/org/apache/spark/repl/ReplSuite.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ class ReplSuite extends SparkFunSuite {
267267
assertDoesNotContain("Exception", output)
268268
}
269269

270+
test("SPARK-8461 SQL with codegen") {
271+
val output = runInterpreter("local",
272+
"""
273+
|val sqlContext = new org.apache.spark.sql.SQLContext(sc)
274+
|sqlContext.setConf("spark.sql.codegen", "true")
275+
|sqlContext.range(0, 100).filter('id > 50).count()
276+
""".stripMargin)
277+
assertContains("Long = 49", output)
278+
assertDoesNotContain("java.lang.ClassNotFoundException", output)
279+
}
280+
270281
test("SPARK-2632 importing a method from non serializable class and not using it.") {
271282
val output = runInterpreter("local",
272283
"""

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeGenerator.scala

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ class CodeGenContext {
203203
def isPrimitiveType(dt: DataType): Boolean = primitiveTypes.contains(dt)
204204
}
205205

206+
207+
abstract class GeneratedClass {
208+
def generate(expressions: Array[Expression]): Any
209+
}
210+
206211
/**
207212
* A base class for generators of byte code to perform expression evaluation. Includes a set of
208213
* helpers for referring to Catalyst types and building trees that perform evaluation of individual
@@ -214,11 +219,6 @@ abstract class CodeGenerator[InType <: AnyRef, OutType <: AnyRef] extends Loggin
214219
protected val mutableRowType: String = classOf[MutableRow].getName
215220
protected val genericMutableRowType: String = classOf[GenericMutableRow].getName
216221

217-
/**
218-
* Can be flipped on manually in the console to add (expensive) expression evaluation trace code.
219-
*/
220-
var debugLogging = false
221-
222222
/**
223223
* Generates a class for a given input expression. Called when there is not cached code
224224
* already available.
@@ -239,10 +239,14 @@ abstract class CodeGenerator[InType <: AnyRef, OutType <: AnyRef] extends Loggin
239239
*
240240
* It will track the time used to compile
241241
*/
242-
protected def compile(code: String): Class[_] = {
242+
protected def compile(code: String): GeneratedClass = {
243243
val startTime = System.nanoTime()
244-
val clazz = try {
245-
new ClassBodyEvaluator(code).getClazz()
244+
val evaluator = new ClassBodyEvaluator()
245+
evaluator.setParentClassLoader(getClass.getClassLoader)
246+
evaluator.setDefaultImports(Array("org.apache.spark.sql.catalyst.InternalRow"))
247+
evaluator.setExtendedClass(classOf[GeneratedClass])
248+
try {
249+
evaluator.cook(code)
246250
} catch {
247251
case e: Exception =>
248252
logError(s"failed to compile:\n $code", e)
@@ -251,7 +255,7 @@ abstract class CodeGenerator[InType <: AnyRef, OutType <: AnyRef] extends Loggin
251255
val endTime = System.nanoTime()
252256
def timeMs: Double = (endTime - startTime).toDouble / 1000000
253257
logDebug(s"Code (${code.size} bytes) compiled in $timeMs ms")
254-
clazz
258+
evaluator.getClazz().newInstance().asInstanceOf[GeneratedClass]
255259
}
256260

257261
/**

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/GenerateMutableProjection.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ object GenerateMutableProjection extends CodeGenerator[Seq[Expression], () => Mu
4747
"""
4848
}.mkString("\n")
4949
val code = s"""
50-
import org.apache.spark.sql.catalyst.InternalRow;
51-
52-
public SpecificProjection generate($exprType[] expr) {
50+
public Object generate($exprType[] expr) {
5351
return new SpecificProjection(expr);
5452
}
5553

@@ -85,10 +83,8 @@ object GenerateMutableProjection extends CodeGenerator[Seq[Expression], () => Mu
8583
logDebug(s"code for ${expressions.mkString(",")}:\n$code")
8684

8785
val c = compile(code)
88-
// fetch the only one method `generate(Expression[])`
89-
val m = c.getDeclaredMethods()(0)
9086
() => {
91-
m.invoke(c.newInstance(), ctx.references.toArray).asInstanceOf[BaseMutableProjection]
87+
c.generate(ctx.references.toArray).asInstanceOf[MutableProjection]
9288
}
9389
}
9490
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/GenerateOrdering.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ object GenerateOrdering
7676
}.mkString("\n")
7777

7878
val code = s"""
79-
import org.apache.spark.sql.catalyst.InternalRow;
80-
8179
public SpecificOrdering generate($exprType[] expr) {
8280
return new SpecificOrdering(expr);
8381
}
@@ -100,9 +98,6 @@ object GenerateOrdering
10098

10199
logDebug(s"Generated Ordering: $code")
102100

103-
val c = compile(code)
104-
// fetch the only one method `generate(Expression[])`
105-
val m = c.getDeclaredMethods()(0)
106-
m.invoke(c.newInstance(), ctx.references.toArray).asInstanceOf[BaseOrdering]
101+
compile(code).generate(ctx.references.toArray).asInstanceOf[BaseOrdering]
107102
}
108103
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/GeneratePredicate.scala

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.apache.spark.sql.catalyst.expressions.codegen
1919

20-
import org.apache.spark.sql.catalyst
2120
import org.apache.spark.sql.catalyst.expressions._
2221

2322
/**
@@ -41,8 +40,6 @@ object GeneratePredicate extends CodeGenerator[Expression, (InternalRow) => Bool
4140
val ctx = newCodeGenContext()
4241
val eval = predicate.gen(ctx)
4342
val code = s"""
44-
import org.apache.spark.sql.catalyst.InternalRow;
45-
4643
public SpecificPredicate generate($exprType[] expr) {
4744
return new SpecificPredicate(expr);
4845
}
@@ -62,10 +59,7 @@ object GeneratePredicate extends CodeGenerator[Expression, (InternalRow) => Bool
6259

6360
logDebug(s"Generated predicate '$predicate':\n$code")
6461

65-
val c = compile(code)
66-
// fetch the only one method `generate(Expression[])`
67-
val m = c.getDeclaredMethods()(0)
68-
val p = m.invoke(c.newInstance(), ctx.references.toArray).asInstanceOf[Predicate]
62+
val p = compile(code).generate(ctx.references.toArray).asInstanceOf[Predicate]
6963
(r: InternalRow) => p.eval(r)
7064
}
7165
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/GenerateProjection.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ object GenerateProjection extends CodeGenerator[Seq[Expression], Projection] {
147147
}.mkString("\n")
148148

149149
val code = s"""
150-
import org.apache.spark.sql.catalyst.InternalRow;
151-
152150
public SpecificProjection generate($exprType[] expr) {
153151
return new SpecificProjection(expr);
154152
}
@@ -220,9 +218,6 @@ object GenerateProjection extends CodeGenerator[Seq[Expression], Projection] {
220218

221219
logDebug(s"MutableRow, initExprs: ${expressions.mkString(",")} code:\n${code}")
222220

223-
val c = compile(code)
224-
// fetch the only one method `generate(Expression[])`
225-
val m = c.getDeclaredMethods()(0)
226-
m.invoke(c.newInstance(), ctx.references.toArray).asInstanceOf[Projection]
221+
compile(code).generate(ctx.references.toArray).asInstanceOf[Projection]
227222
}
228223
}

0 commit comments

Comments
 (0)