Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -302,37 +302,30 @@ class CodegenContext {
}

/**
* Instantiates all nested, private sub-classes as objects to the `OuterClass`
* Declares all function code. If the added functions are too many, split them into nested
* sub-classes to avoid hitting Java compiler constant pool limitation.
*/
private[sql] def initNestedClasses(): String = {
def declareAddedFunctions(): String = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we can keep this as private[sql] because all previous three functions are.

val inlinedFunctions = classFunctions(outerClassName).values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Would it be better to move this line after the comment at line 311-312? This is because this output is declared in Line 312 (i.e. inlined fuction into OuterClass).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are different. This line inline pure functions which are not in the nested classes, and the next line inline these nested classes containing other functions.

Copy link
Member

@kiszk kiszk Jul 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Sorry, I made a mistake. outerClassName is used for the non-nested class.


// Nested, private sub-classes have no mutable state (though they do reference the outer class'
// mutable state), so we declare and initialize them inline to the OuterClass.
classes.filter(_._1 != outerClassName).map {
val initNestedClasses = classes.filter(_._1 != outerClassName).map {
case (className, classInstance) =>
s"private $className $classInstance = new $className();"
}.mkString("\n")
}

/**
* Declares all function code that should be inlined to the `OuterClass`.
*/
private[sql] def declareAddedFunctions(): String = {
classFunctions(outerClassName).values.mkString("\n")
}
}

/**
* Declares all nested, private sub-classes and the function code that should be inlined to them.
*/
private[sql] def declareNestedClasses(): String = {
classFunctions.filterKeys(_ != outerClassName).map {
val declareNestedClasses = classFunctions.filterKeys(_ != outerClassName).map {
case (className, functions) =>
s"""
|private class $className {
| ${functions.values.mkString("\n")}
|}
""".stripMargin
}
}.mkString("\n")

(inlinedFunctions ++ initNestedClasses ++ declareNestedClasses).mkString("\n")
}

final val JAVA_BOOLEAN = "boolean"
final val JAVA_BYTE = "byte"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ object GenerateMutableProjection extends CodeGenerator[Seq[Expression], MutableP
${ctx.initPartition()}
}

${ctx.declareAddedFunctions()}

public ${classOf[BaseMutableProjection].getName} target(InternalRow row) {
mutableRow = row;
return this;
Expand All @@ -136,8 +134,7 @@ object GenerateMutableProjection extends CodeGenerator[Seq[Expression], MutableP
return mutableRow;
}

${ctx.initNestedClasses()}
${ctx.declareNestedClasses()}
${ctx.declareAddedFunctions()}
}
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,12 @@ object GenerateOrdering extends CodeGenerator[Seq[SortOrder], Ordering[InternalR
${ctx.initMutableStates()}
}

${ctx.declareAddedFunctions()}

public int compare(InternalRow a, InternalRow b) {
$comparisons
return 0;
}

${ctx.initNestedClasses()}
${ctx.declareNestedClasses()}
${ctx.declareAddedFunctions()}
}"""

val code = CodeFormatter.stripOverlappingComments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,12 @@ object GeneratePredicate extends CodeGenerator[Expression, Predicate] {
${ctx.initPartition()}
}

${ctx.declareAddedFunctions()}

public boolean eval(InternalRow ${ctx.INPUT_ROW}) {
${eval.code}
return !${eval.isNull} && ${eval.value};
}

${ctx.initNestedClasses()}
${ctx.declareNestedClasses()}
${ctx.declareAddedFunctions()}
}"""

val code = CodeFormatter.stripOverlappingComments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,13 @@ object GenerateSafeProjection extends CodeGenerator[Seq[Expression], Projection]
${ctx.initPartition()}
}

${ctx.declareAddedFunctions()}

public java.lang.Object apply(java.lang.Object _i) {
InternalRow ${ctx.INPUT_ROW} = (InternalRow) _i;
$allExpressions
return mutableRow;
}

${ctx.initNestedClasses()}
${ctx.declareNestedClasses()}
${ctx.declareAddedFunctions()}
}
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,6 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro
${ctx.initPartition()}
}

${ctx.declareAddedFunctions()}

// Scala.Function1 need this
public java.lang.Object apply(java.lang.Object row) {
return apply((InternalRow) row);
Expand All @@ -403,8 +401,7 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro
return ${eval.value};
}

${ctx.initNestedClasses()}
${ctx.declareNestedClasses()}
${ctx.declareAddedFunctions()}
}
"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,11 @@ case class WholeStageCodegenExec(child: SparkPlan) extends UnaryExecNode with Co
${ctx.initPartition()}
}

${ctx.declareAddedFunctions()}

protected void processNext() throws java.io.IOException {
${code.trim}
}

${ctx.initNestedClasses()}
${ctx.declareNestedClasses()}
${ctx.declareAddedFunctions()}
}
""".trim

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ object GenerateColumnAccessor extends CodeGenerator[Seq[DataType], ColumnarItera
this.columnIndexes = columnIndexes;
}

${ctx.declareAddedFunctions()}

public boolean hasNext() {
if (currentRow < numRowsInBatch) {
return true;
Expand Down Expand Up @@ -222,8 +220,7 @@ object GenerateColumnAccessor extends CodeGenerator[Seq[DataType], ColumnarItera
return unsafeRow;
}

${ctx.initNestedClasses()}
${ctx.declareNestedClasses()}
${ctx.declareAddedFunctions()}
}"""

val code = CodeFormatter.stripOverlappingComments(
Expand Down