godfreyhe commented on a change in pull request #14516: URL: https://github.com/apache/flink/pull/14516#discussion_r553721184
########## File path: flink-table/flink-table-planner-blink/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecSortMergeJoin.java ########## @@ -0,0 +1,186 @@ +/* + * 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.nodes.exec.batch; + +import org.apache.flink.api.dag.Transformation; +import org.apache.flink.configuration.MemorySize; +import org.apache.flink.streaming.api.operators.SimpleOperatorFactory; +import org.apache.flink.streaming.api.transformations.TwoInputTransformation; +import org.apache.flink.table.api.TableConfig; +import org.apache.flink.table.api.config.ExecutionConfigOptions; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.planner.codegen.CodeGeneratorContext; +import org.apache.flink.table.planner.codegen.ProjectionCodeGenerator; +import org.apache.flink.table.planner.codegen.sort.SortCodeGenerator; +import org.apache.flink.table.planner.delegation.PlannerBase; +import org.apache.flink.table.planner.plan.nodes.exec.ExecEdge; +import org.apache.flink.table.planner.plan.nodes.exec.ExecNode; +import org.apache.flink.table.planner.plan.nodes.exec.ExecNodeBase; +import org.apache.flink.table.planner.plan.nodes.exec.utils.ExecNodeUtil; +import org.apache.flink.table.planner.plan.utils.JoinUtil; +import org.apache.flink.table.planner.plan.utils.SortUtil; +import org.apache.flink.table.runtime.generated.GeneratedJoinCondition; +import org.apache.flink.table.runtime.operators.join.FlinkJoinType; +import org.apache.flink.table.runtime.operators.join.SortMergeJoinOperator; +import org.apache.flink.table.runtime.typeutils.InternalTypeInfo; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.util.Preconditions; + +import org.apache.calcite.rex.RexNode; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.IntStream; + +import scala.Tuple3; + +/** Batch {@link ExecNode} for Sort Merge Join. */ +public class BatchExecSortMergeJoin extends ExecNodeBase<RowData> + implements BatchExecNode<RowData> { + + private final FlinkJoinType joinType; + private final int[] leftKeys; + private final int[] rightKeys; + private final boolean[] filterNulls; + private final RexNode nonEquiConditions; + private final boolean leftIsSmaller; + + public BatchExecSortMergeJoin( + FlinkJoinType joinType, + int[] leftKeys, + int[] rightKeys, + boolean[] filterNulls, + RexNode nonEquiConditions, + boolean leftIsSmaller, + List<ExecEdge> inputEdges, + LogicalType outputType, + String description) { + super(inputEdges, outputType, description); + this.joinType = joinType; + + Preconditions.checkArgument(leftKeys.length == rightKeys.length); + Preconditions.checkArgument(leftKeys.length == filterNulls.length); + this.leftKeys = leftKeys; + this.rightKeys = rightKeys; + this.filterNulls = filterNulls; + this.nonEquiConditions = nonEquiConditions; + this.leftIsSmaller = leftIsSmaller; + } + + @Override + @SuppressWarnings("Unchecked") + protected Transformation<RowData> translateToPlanInternal(PlannerBase planner) { + TableConfig config = planner.getTableConfig(); + + Transformation<RowData> lInput = + (Transformation<RowData>) getInputNodes().get(0).translateToPlan(planner); + Transformation<RowData> rInput = + (Transformation<RowData>) getInputNodes().get(1).translateToPlan(planner); + + // get type + RowType lType = (RowType) getInputNodes().get(0).getOutputType(); + RowType rType = (RowType) getInputNodes().get(1).getOutputType(); + + LogicalType[] keyFieldTypes = + IntStream.of(leftKeys).mapToObj(lType::getTypeAt).toArray(LogicalType[]::new); + RowType keyType = RowType.of(keyFieldTypes); + + GeneratedJoinCondition condFunc = + JoinUtil.generateConditionFunction(config, nonEquiConditions, lType, rType); + + long externalBufferMemory = + MemorySize.parse( + config.getConfiguration() + .getString( + ExecutionConfigOptions + .TABLE_EXEC_RESOURCE_EXTERNAL_BUFFER_MEMORY)) + .getBytes(); + long sortMemory = + MemorySize.parse( + config.getConfiguration() + .getString( + ExecutionConfigOptions + .TABLE_EXEC_RESOURCE_SORT_MEMORY)) + .getBytes(); + int externalBufferNum = 1; + if (joinType == FlinkJoinType.FULL) { + externalBufferNum = 2; + } + + long managedMemory = externalBufferMemory * externalBufferNum + sortMemory * 2; + + SortCodeGenerator leftSortGen = newSortGen(config, leftKeys, lType); + SortCodeGenerator rightSortGen = newSortGen(config, rightKeys, rType); + + int[] keyPositions = IntStream.range(0, leftKeys.length).toArray(); + SortMergeJoinOperator operator = + new SortMergeJoinOperator( + 1.0 * externalBufferMemory / managedMemory, + joinType, + leftIsSmaller, + condFunc, + ProjectionCodeGenerator.generateProjection( + new CodeGeneratorContext(config), + "SMJProjection", + lType, + keyType, + leftKeys), + ProjectionCodeGenerator.generateProjection( + new CodeGeneratorContext(config), + "SMJProjection", + rType, + keyType, + rightKeys), + leftSortGen.generateNormalizedKeyComputer("LeftComputer"), + leftSortGen.generateRecordComparator("LeftComparator"), + rightSortGen.generateNormalizedKeyComputer("RightComputer"), + rightSortGen.generateRecordComparator("RightComparator"), + newSortGen(config, keyPositions, keyType) + .generateRecordComparator("KeyComparator"), + filterNulls); + + TwoInputTransformation<RowData, RowData, RowData> ret = + ExecNodeUtil.createTwoInputTransformation( + lInput, + rInput, + getDesc(), + SimpleOperatorFactory.of(operator), + InternalTypeInfo.of(getOutputType()), + rInput.getParallelism(), + managedMemory); + + if (inputsContainSingleton()) { + ret.setParallelism(1); + ret.setMaxParallelism(1); + } + return ret; + } + + private SortCodeGenerator newSortGen(TableConfig config, int[] originalKeys, RowType type) { + boolean[] originalOrders = new boolean[originalKeys.length]; + Arrays.fill(originalOrders, true); + boolean[] nullsIsLast = SortUtil.getNullDefaultOrders(originalOrders); + Tuple3<int[], boolean[], boolean[]> ret = + SortUtil.deduplicateSortKeys(originalKeys, originalOrders, nullsIsLast); Review comment: return `SortSpec` directly ? even we can use `SortSpec` in `SortCodeGenerator` ? ---------------------------------------------------------------- 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: us...@infra.apache.org