lsyldliu commented on code in PR #22734: URL: https://github.com/apache/flink/pull/22734#discussion_r1242116589
########## flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/fusion/spec/HashJoinFusionCodegenSpec.scala: ########## @@ -0,0 +1,542 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.flink.table.planner.plan.fusion.spec + +import org.apache.flink.table.data.RowData +import org.apache.flink.table.data.binary.BinaryRowData +import org.apache.flink.table.planner.codegen.{CodeGeneratorContext, GeneratedExpression, GenerateUtils} +import org.apache.flink.table.planner.codegen.CodeGenUtils.{fieldIndices, newName, newNames, primitiveDefaultValue, primitiveTypeTermForType, BINARY_ROW, ROW_DATA} +import org.apache.flink.table.planner.codegen.LongHashJoinGenerator.{genGetLongKey, genProjection} +import org.apache.flink.table.planner.plan.fusion.{OpFusionCodegenSpecBase, OpFusionCodegenSpecGenerator, OpFusionContext} +import org.apache.flink.table.planner.plan.nodes.exec.spec.JoinSpec +import org.apache.flink.table.planner.utils.JavaScalaConversionUtil.{toJava, toScala} +import org.apache.flink.table.runtime.hashtable.LongHybridHashTable +import org.apache.flink.table.runtime.operators.join.{FlinkJoinType, HashJoinType} +import org.apache.flink.table.runtime.typeutils.BinaryRowDataSerializer +import org.apache.flink.table.runtime.util.RowIterator +import org.apache.flink.table.types.logical.{LogicalType, RowType} + +import java.util + +/** Base operator fusion codegen spec for HashJoin. */ +class HashJoinFusionCodegenSpec( + operatorCtx: CodeGeneratorContext, + isBroadcast: Boolean, + leftIsBuild: Boolean, + joinSpec: JoinSpec, + estimatedLeftAvgRowSize: Int, + estimatedRightAvgRowSize: Int, + estimatedLeftRowCount: Long, + estimatedRightRowCount: Long, + compressionEnabled: Boolean, + compressionBlockSize: Int) + extends OpFusionCodegenSpecBase(operatorCtx) { + + private lazy val joinType: FlinkJoinType = joinSpec.getJoinType + private lazy val hashJoinType: HashJoinType = HashJoinType.of( + leftIsBuild, + joinType.isLeftOuter, + joinType.isRightOuter, + joinType == FlinkJoinType.SEMI, + joinType == FlinkJoinType.ANTI) + private lazy val (buildKeys, probeKeys) = if (leftIsBuild) { + (joinSpec.getLeftKeys, joinSpec.getRightKeys) + } else { + (joinSpec.getRightKeys, joinSpec.getLeftKeys) + } + private lazy val (buildRowSize, buildRowCount) = if (leftIsBuild) { + (estimatedLeftAvgRowSize, estimatedLeftRowCount) + } else { + (estimatedRightAvgRowSize, estimatedRightRowCount) + } + private lazy val buildInputId = if (leftIsBuild) { + 1 + } else { + 2 + } + + private lazy val Seq(buildToBinaryRow, probeToBinaryRow) = + newNames("buildToBinaryRow", "probeToBinaryRow") + + private lazy val hashTableTerm: String = newName("hashTable") + + private var buildInput: OpFusionCodegenSpecGenerator = _ + private var probeInput: OpFusionCodegenSpecGenerator = _ + private var buildType: RowType = _ + private var probeType: RowType = _ + private var keyType: RowType = _ + + override def setup(opFusionContext: OpFusionContext): Unit = { + super.setup(opFusionContext) + val inputs = toScala(fusionContext.getInputs) + assert(inputs.size == 2) + if (leftIsBuild) { + buildInput = inputs.head + probeInput = inputs(1) + } else { + buildInput = inputs(1) + probeInput = inputs.head + } + + buildType = buildInput.getOutputType + probeType = probeInput.getOutputType + if (leftIsBuild) { + keyType = RowType.of(joinSpec.getLeftKeys.map(idx => buildType.getTypeAt(idx)): _*) + } else { + keyType = RowType.of(joinSpec.getLeftKeys.map(idx => probeType.getTypeAt(idx)): _*) + } + } + + override def variablePrefix: String = if (isBroadcast) { "bhj" } + else { "shj" } + + override protected def doProcessProduce(fusionCtx: CodeGeneratorContext): Unit = { + // call build side first, then call probe side + buildInput.processProduce(fusionCtx) + probeInput.processProduce(fusionCtx) + } + + override protected def doEndInputProduce(fusionCtx: CodeGeneratorContext): Unit = { + // call build side first, then call probe side + buildInput.endInputProduce(fusionCtx) + probeInput.endInputProduce(fusionCtx) + } + + override def doProcessConsume( + inputId: Int, + inputVars: util.List[GeneratedExpression], + row: GeneratedExpression): String = { + // only probe side will call the consumeProcess method to consume the output record + if (inputId == buildInputId) { + codegenBuild(toScala(inputVars), row) + } else { + codegenProbe(inputVars) + } + } + + private def codegenBuild( + inputVars: Seq[GeneratedExpression], + row: GeneratedExpression): String = { + // initialize hash table related code + if (isBroadcast) { + codegenHashTable(false) + } else { + // TODO Shuffled HashJoin support build side spill to disk Review Comment: Yes, add the jira issue to comment part. -- 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]
