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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| def generateLookupFunction( config: TableConfig, typeFactory: FlinkTypeFactory, inputType: LogicalType, returnType: LogicalType, tableReturnTypeInfo: TypeInformation[_], lookupKeyInOrder: Array[Int], allLookupFields: Map[Int, LookupKey], lookupFunction: TableFunction[_], enableObjectReuse: Boolean) : GeneratedFunction[FlatMapFunction[RowData, RowData]] = {
val ctx = CodeGeneratorContext(config) val batchJoinEnabled = config.getConfiguration.getBoolean( "temporal.table.exec.mini-batch.enabled", false)
val logicalType = RowType.of(DataTypes.RAW(TypeInformation.of(new TypeHint[MiniBatchJoinType] {})).getLogicalType) val (prepareCode, parameters, nullInParameters) = if (batchJoinEnabled) { prepareParameters( ctx, typeFactory, logicalType, lookupKeyInOrder, allLookupFields, tableReturnTypeInfo.isInstanceOf[RowTypeInfo], enableObjectReuse) } else { prepareParameters( ctx, typeFactory, inputType, lookupKeyInOrder, allLookupFields, tableReturnTypeInfo.isInstanceOf[RowTypeInfo], enableObjectReuse) }
val lookupFunctionTerm = ctx.addReusableFunction(lookupFunction) val setCollectorCode = tableReturnTypeInfo match { case rt: RowTypeInfo => val converterCollector = new RowToRowDataCollector(rt) val term = ctx.addReusableObject(converterCollector, "collector") s""" |$term.setCollector($DEFAULT_COLLECTOR_TERM); |$lookupFunctionTerm.setCollector($term); """.stripMargin case _ => s"$lookupFunctionTerm.setCollector($DEFAULT_COLLECTOR_TERM);" }
val elseCode = if (batchJoinEnabled) { s"""$lookupFunctionTerm.batchEval($parameters);""" } else { s"""$lookupFunctionTerm.eval($parameters);""" }
val body = s""" |$prepareCode |$setCollectorCode |if ($nullInParameters) { | return; |} else { | $elseCode | } """.stripMargin
if (batchJoinEnabled) { FunctionCodeGenerator.generateFunction( ctx, "LookupFunction", classOf[FlatMapFunction[RowData, RowData]], body, returnType, logicalType) } else { FunctionCodeGenerator.generateFunction( ctx, "LookupFunction", classOf[FlatMapFunction[RowData, RowData]], body, returnType, inputType) } }
private def prepareParameters( ctx: CodeGeneratorContext, typeFactory: FlinkTypeFactory, inputType: LogicalType, lookupKeyInOrder: Array[Int], allLookupFields: Map[Int, LookupKey], isExternalArgs: Boolean, fieldCopy: Boolean): (String, String, String) = { val batchJoinEnabled = ctx.tableConfig.getConfiguration.getBoolean( "temporal.table.exec.mini-batch.enabled", false) val inputFieldExprs = if (batchJoinEnabled) { val expression = generateInputAccess( ctx, inputType, DEFAULT_INPUT1_TERM, 0, nullableInput = false, fieldCopy) Array(expression) } else { for (i <- lookupKeyInOrder) yield { allLookupFields.get(i) match { case Some(ConstantLookupKey(dataType, literal)) => generateLiteral(ctx, dataType, literal.getValue3) case Some(FieldRefLookupKey(index)) => generateInputAccess( ctx, inputType, DEFAULT_INPUT1_TERM, index, nullableInput = false, fieldCopy) case None => throw new CodeGenException("This should never happen!") } } } val codeAndArg = inputFieldExprs .map { e => val dataType = fromLogicalTypeToDataType(e.resultType) val bType = if (isExternalArgs) { typeTerm(dataType.getConversionClass) } else { boxedTypeTermForType(e.resultType) } val assign = if (isExternalArgs) { CodeGenUtils.genToExternalConverter(ctx, dataType, e.resultTerm) } else { e.resultTerm } val newTerm = newName("arg") val code = s""" |$bType $newTerm = null; |if (!${e.nullTerm}) { | $newTerm = $assign; |} """.stripMargin (code, newTerm, e.nullTerm) } ( codeAndArg.map(_._1).mkString("\n"), codeAndArg.map(_._2).mkString(", "), codeAndArg.map(_._3).mkString("|| ")) }
|