JingsongLi commented on a change in pull request #12996:
URL: https://github.com/apache/flink/pull/12996#discussion_r461446759



##########
File path: 
flink-table/flink-table-planner-blink/src/main/scala/org/apache/flink/table/planner/codegen/ProjectionCodeGenerator.scala
##########
@@ -50,74 +50,18 @@ object ProjectionCodeGenerator {
       inputTerm: String = DEFAULT_INPUT1_TERM,
       outRecordTerm: String = DEFAULT_OUT_RECORD_TERM,
       outRecordWriterTerm: String = DEFAULT_OUT_RECORD_WRITER_TERM,
-      reusedOutRecord: Boolean = true,
-      nullCheck: Boolean = true): GeneratedExpression = {
-
-    // we use a for loop to do all the projections for the same field type
-    // instead of generating separated code for each field.
-    // when the number of fields of the same type is large, this can improve 
performance.
-    def generateLoop(
-        fieldType: LogicalType,
-        inIdxs: mutable.ArrayBuffer[Int],
-        outIdxs: mutable.ArrayBuffer[Int]): String = {
-      // this array contains the indices of the fields
-      // whose type equals to `fieldType` in the input row
-      val inIdxArr = newName("inIdx")
-      ctx.addReusableMember(s"int[] $inIdxArr = null;")
-      ctx.addReusableInitStatement(s"$inIdxArr = new int[] 
{${inIdxs.mkString(", ")}};")
-
-      // this array contains the indices of the fields
-      // whose type equals to `fieldType` in the output row
-      val outIdxArr = newName("outIdx")
-      ctx.addReusableMember(s"int[] $outIdxArr = null;")
-      ctx.addReusableInitStatement(s"$outIdxArr = new int[] 
{${outIdxs.mkString(", ")}};")
-
-      val loopIdx = newName("i")
-
-      val fieldVal = CodeGenUtils.rowFieldReadAccess(
-        ctx, s"$inIdxArr[$loopIdx]", inputTerm, fieldType)
-
-      val inIdx = s"$inIdxArr[$loopIdx]"
-      val outIdx = s"$outIdxArr[$loopIdx]"
-      val nullTerm = s"$inputTerm.isNullAt($inIdx)"
-      s"""
-         |for (int $loopIdx = 0; $loopIdx < $inIdxArr.length; $loopIdx++) {
-         |  ${CodeGenUtils.rowSetField(ctx, outClass, outRecordTerm, outIdx,
-                GeneratedExpression(fieldVal, nullTerm, "", fieldType),
-                Some(outRecordWriterTerm))}
-         |}
-       """.stripMargin
-    }
-
+      reusedOutRecord: Boolean = true): GeneratedExpression = {
     val outFieldTypes = outType.getChildren
-    val typeIdxs = new mutable.HashMap[
-      LogicalType,
-      (mutable.ArrayBuffer[Int], mutable.ArrayBuffer[Int])]()
-
-    for (i <- 0 until outFieldTypes.size()) {
-      val (inIdxs, outIdxs) = typeIdxs.getOrElseUpdate(
-        outFieldTypes.get(i), (mutable.ArrayBuffer.empty[Int], 
mutable.ArrayBuffer.empty[Int]))
-      inIdxs.append(inputMapping(i))
-      outIdxs.append(i)
-    }
 
     val codeBuffer = mutable.ArrayBuffer.empty[String]
-    for ((fieldType, (inIdxs, outIdxs)) <- typeIdxs) {
-      if (inIdxs.length >= FOR_LOOP_FIELD_LIMIT) {
-        // for loop optimization will only be enabled
-        // if the number of fields to be project exceeds the limit
-        codeBuffer.append(generateLoop(fieldType, inIdxs, outIdxs))
-      } else {
-        // otherwise we do not use for loop
-        for (i <- inIdxs.indices) {
-          val nullTerm = s"$inputTerm.isNullAt(${inIdxs(i)})"
-          codeBuffer.append(
-            CodeGenUtils.rowSetField(ctx, outClass, outRecordTerm, 
outIdxs(i).toString,
-              GeneratedExpression(rowFieldReadAccess(
-                ctx, inIdxs(i), inputTerm, fieldType), nullTerm, "", 
fieldType),
-              Some(outRecordWriterTerm)))
-        }
-      }
+    for (i <- inputMapping.indices) {

Review comment:
       Can you just use `exprGenerator.generateResultExpression`?




----------------------------------------------------------------
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.

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


Reply via email to