vranes commented on code in PR #60611:
URL: https://github.com/apache/spark/pull/60611#discussion_r4635634714


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/BinByExec.scala:
##########
@@ -87,6 +89,127 @@ case class BinByExec(
   override protected def withNewChildInternal(newChild: SparkPlan): BinByExec =
     copy(child = newChild)
 
+  override def supportCodegen: Boolean = 
conf.getConf(SQLConf.ENABLE_BIN_BY_CODEGEN)
+
+  // BIN BY starts its generated stage and reads the child rows through an 
iterator.
+  override def inputRDDs(): Seq[RDD[InternalRow]] = child.execute() :: Nil
+
+  // The loop returns after each output row that reaches the stage output, so 
the row needs no copy.
+  override def needCopyResult: Boolean = false
+
+  // The bin loop reads the stage's input, so it stops at a LIMIT fused above 
it.
+  override protected def canCheckLimitNotReached: Boolean = true
+
+  /**
+   * Generates the bin loop. The current input row and the state of its next 
bin are fields, so the
+   * loop can return after any bin and continue with the next bin on the next 
call.
+   */
+  override protected def doProduce(ctx: CodegenContext): String = {
+    val input = ctx.addMutableState(
+      javaType = "scala.collection.Iterator",
+      variableName = "input",
+      initFunc = v => s"$v = inputs[0];",
+      forceInline = true
+    )
+    val row = ctx.addMutableState("InternalRow", "binByRow", forceInline = 
true)
+    val hasMoreBins =
+      ctx.addMutableState(CodeGenerator.JAVA_BOOLEAN, "hasMoreBins", 
forceInline = true)
+    val nullRange = ctx.addMutableState(CodeGenerator.JAVA_BOOLEAN, 
"nullRange", forceInline = true)
+    val start = ctx.addMutableState(CodeGenerator.JAVA_LONG, "rangeStart", 
forceInline = true)
+    val end = ctx.addMutableState(CodeGenerator.JAVA_LONG, "rangeEnd", 
forceInline = true)
+    val total = ctx.addMutableState(CodeGenerator.JAVA_LONG, "total", 
forceInline = true)
+    val index = ctx.addMutableState(CodeGenerator.JAVA_LONG, "binIndex", 
forceInline = true)
+    val nextStart = ctx.addMutableState(CodeGenerator.JAVA_LONG, "nextStart", 
forceInline = true)
+    val binsSinceKillCheck =
+      ctx.addMutableState(CodeGenerator.JAVA_INT, "binsSinceKillCheck", 
forceInline = true)
+    val rsIdx = bindOrdinal(rangeStart)
+    val reIdx = bindOrdinal(rangeEnd)
+    val zone = 
timeZoneId.map(DateTimeUtils.getZoneId).getOrElse(ZoneOffset.UTC)
+    val zoneRef = ctx.addReferenceObj(
+      objName = "zoneId", obj = zone, className = classOf[ZoneId].getName)
+    val formatter = ctx.addReferenceObj(
+      objName = "formatter",
+      obj = TimestampFormatter.getFractionFormatter(zone),
+      className = classOf[TimestampFormatter].getName
+    )
+    val taskContext = ctx.addMutableState(
+      javaType = "TaskContext",
+      variableName = "taskContext",
+      initFunc = v => s"$v = TaskContext.get();",
+      forceInline = true
+    )
+    // The loop checks for a task kill every this many bins, across input rows.
+    val killCheckInterval = 1024
+    val dateTimeUtils = DateTimeUtils.getClass.getName.stripSuffix("$")
+    val errors = QueryExecutionErrors.getClass.getName.stripSuffix("$")
+    val bucket = ctx.freshName("bucket")
+    val binStart = ctx.freshName("binStart")
+    val binEnd = ctx.freshName("binEnd")
+    val ratio = ctx.freshName("ratio")
+    val numOutput = metricTerm(ctx, "numOutputRows")
+
+    val appended = Seq(binStart, binEnd, ratio).zip(appendedAttributes).map {
+      case (value, attr) =>
+        ExprCode(JavaCode.isNullGlobal(nullRange), JavaCode.variable(value, 
attr.dataType))
+    }
+    ctx.INPUT_ROW = row
+    ctx.currentVars = Seq.fill(child.output.length)(null) ++ appended
+    val outputVars = buildOutputExpressions(childLen = child.output.length, 
ratioNullable = true)
+      .map(_.genCode(ctx))
+
+    // The state of the next bin changes before `consume`, because the parent 
code can skip the
+    // rest of the loop body with `continue`.

Review Comment:
   Thanks, replaced with a shorter version of your suggestion.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to