CodegenContext
CodegenContext is…FIXME
CodegenContext takes no input parameters.
|
1 2 3 4 5 6 |
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenContext val ctx = new CodegenContext |
CodegenContext is created when:
-
WholeStageCodegenExecphysical operator is requested to generate a Java source code for the child operator (whenWholeStageCodegenExecis executed) -
CodeGeneratoris requested for a new CodegenContext -
GenerateUnsafeRowJoineris requested for aUnsafeRowJoiner
CodegenContext stores expressions that don’t support codegen.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
Example of CodegenContext.subexpressionElimination (through CodegenContext.generateExpressions) import org.apache.spark.sql.catalyst.expressions.codegen.CodegenContext val ctx = new CodegenContext // Use Catalyst DSL import org.apache.spark.sql.catalyst.dsl.expressions._ val expressions = "hello".expr.as("world") :: "hello".expr.as("world") :: Nil // FIXME Use a real-life query to extract the expressions // CodegenContext.subexpressionElimination (where the elimination all happens) is a private method // It is used exclusively in CodegenContext.generateExpressions which is public // and does the elimination when it is enabled // Note the doSubexpressionElimination flag is on // Triggers the subexpressionElimination private method ctx.generateExpressions(expressions, doSubexpressionElimination = true) // subexpressionElimination private method uses ctx.equivalentExpressions val commonExprs = ctx.equivalentExpressions.getAllEquivalentExprs assert(commonExprs.length > 0, "No common expressions found") |
| Name | Description |
|---|---|
|
|
Mutable Scala New entries are added when Used when |
|
|
Expressions are added and then fetched as equivalent sets when |
|
|
|
|
|
|
|
|
Placeholders and their comments Used when…FIXME |
|
|
|
|
|
Used when…FIXME |
|
|
Generating Java Source Code For Code-Generated Evaluation of Multiple Expressions (With Optional Subexpression Elimination) — generateExpressions Method
|
1 2 3 4 5 6 7 |
generateExpressions( expressions: Seq[Expression], doSubexpressionElimination: Boolean = false): Seq[ExprCode] |
(only with subexpression elimination enabled) generateExpressions does subexpressionElimination of the input expressions.
In the end, generateExpressions requests every expressions to generate the Java source code for code-generated (non-interpreted) expression evaluation.
|
Note
|
|
addReferenceObj Method
|
1 2 3 4 5 |
addReferenceObj(objName: String, obj: Any, className: String = null): String |
addReferenceObj…FIXME
|
Note
|
addReferenceObj is used when…FIXME
|
subexpressionEliminationForWholeStageCodegen Method
|
1 2 3 4 5 |
subexpressionEliminationForWholeStageCodegen(expressions: Seq[Expression]): SubExprCodes |
subexpressionEliminationForWholeStageCodegen…FIXME
|
Note
|
subexpressionEliminationForWholeStageCodegen is used exclusively when HashAggregateExec is requested to generate a Java source code for whole-stage consume path (with grouping keys or not).
|
Adding Function to Generated Class — addNewFunction Method
|
1 2 3 4 5 6 7 8 |
addNewFunction( funcName: String, funcCode: String, inlineToOuterClass: Boolean = false): String |
addNewFunction…FIXME
|
Note
|
addNewFunction is used when…FIXME
|
subexpressionElimination Internal Method
|
1 2 3 4 5 |
subexpressionElimination(expressions: Seq[Expression]): Unit |
subexpressionElimination requests EquivalentExpressions to addExprTree for every expression (in the input expressions).
subexpressionElimination requests EquivalentExpressions for the equivalent sets of expressions with at least two equivalent expressions (aka common expressions).
For every equivalent expression set, subexpressionElimination does the following:
-
Takes the first expression and requests it to generate a Java source code for the expression tree
-
addNewFunction and adds it to subexprFunctions
-
Creates a
SubExprEliminationStateand adds it with every common expression in the equivalent expression set to subExprEliminationExprs
|
Note
|
subexpressionElimination is used exclusively when CodegenContext is requested to generateExpressions (with subexpression elimination enabled).
|
Adding Mutable State — addMutableState Method
|
1 2 3 4 5 6 7 8 9 10 |
addMutableState( javaType: String, variableName: String, initFunc: String => String = _ => "", forceInline: Boolean = false, useFreshName: Boolean = true): String |
addMutableState…FIXME
|
1 2 3 4 5 |
val input = ctx.addMutableState("scala.collection.Iterator", "input", v => s"$v = inputs[0];") |
|
Note
|
addMutableState is used when…FIXME
|
Adding Immutable State (Unless Exists Already) — addImmutableStateIfNotExists Method
|
1 2 3 4 5 6 7 8 |
addImmutableStateIfNotExists( javaType: String, variableName: String, initFunc: String => String = _ => ""): Unit |
addImmutableStateIfNotExists…FIXME
|
1 2 3 4 5 6 7 |
val ctx: CodegenContext = ??? val partitionMaskTerm = "partitionMask" ctx.addImmutableStateIfNotExists(ctx.JAVA_LONG, partitionMaskTerm) |
|
Note
|
addImmutableStateIfNotExists is used when…FIXME
|
freshName Method
|
1 2 3 4 5 |
freshName(name: String): String |
freshName…FIXME
|
Note
|
freshName is used when…FIXME
|
addNewFunctionToClass Internal Method
|
1 2 3 4 5 6 7 8 |
addNewFunctionToClass( funcName: String, funcCode: String, className: String): mutable.Map[String, mutable.Map[String, String]] |
addNewFunctionToClass…FIXME
|
Note
|
addNewFunctionToClass is used when…FIXME
|
addClass Internal Method
|
1 2 3 4 5 |
addClass(className: String, classInstance: String): Unit |
addClass…FIXME
|
Note
|
addClass is used when…FIXME
|
declareAddedFunctions Method
|
1 2 3 4 5 |
declareAddedFunctions(): String |
declareAddedFunctions…FIXME
|
Note
|
declareAddedFunctions is used when…FIXME
|
declareMutableStates Method
|
1 2 3 4 5 |
declareMutableStates(): String |
declareMutableStates…FIXME
|
Note
|
declareMutableStates is used when…FIXME
|
initMutableStates Method
|
1 2 3 4 5 |
initMutableStates(): String |
initMutableStates…FIXME
|
Note
|
initMutableStates is used when…FIXME
|
initPartition Method
|
1 2 3 4 5 |
initPartition(): String |
initPartition…FIXME
|
Note
|
initPartition is used when…FIXME
|
spark技术分享