Download flink_6/solution/solution.patch from lolbench26/LoLBench: direct link, hf CLI and curl.
- Browser
- Download file 64.6 kB
-
https://huggingface.co/datasets/lolbench26/LoLBench/resolve/main/flink_6/solution/solution.patch
- Command line
-
hf download hf://datasets/lolbench26/LoLBench/flink_6/solution/solution.patch
-
curl -L -o solution.patch https://huggingface.co/datasets/lolbench26/LoLBench/resolve/main/flink_6/solution/solution.patch
64.6 kB
| diff --git a/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/config/OptimizerConfigOptions.java b/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/config/OptimizerConfigOptions.java | |
| --- a/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/config/OptimizerConfigOptions.java | |
| +++ b/flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/config/OptimizerConfigOptions.java | |
| import org.apache.flink.annotation.PublicEvolving; | |
| import org.apache.flink.annotation.docs.Documentation; | |
| import org.apache.flink.configuration.ConfigOption; | |
| +import org.apache.flink.configuration.DescribedEnum; | |
| import org.apache.flink.configuration.MemorySize; | |
| import org.apache.flink.configuration.description.Description; | |
| +import org.apache.flink.configuration.description.InlineElement; | |
| import static org.apache.flink.configuration.ConfigOptions.key; | |
| import static org.apache.flink.configuration.description.TextElement.code; | |
| +import static org.apache.flink.configuration.description.TextElement.text; | |
| /** | |
| * This class holds configuration constants used by Flink's table planner module. | |
| public class OptimizerConfigOptions { | |
| "A flag to enable or disable the runtime filter. " | |
| + "When it is true, the optimizer will try to inject a runtime filter for eligible join."); | |
| + @Documentation.TableOption(execMode = Documentation.ExecMode.BATCH) | |
| + public static final ConfigOption<AdaptiveBroadcastJoinStrategy> | |
| + TABLE_OPTIMIZER_ADAPTIVE_BROADCAST_JOIN_STRATEGY = | |
| + key("table.optimizer.adaptive-broadcast-join.strategy") | |
| + .enumType(AdaptiveBroadcastJoinStrategy.class) | |
| + .defaultValue(AdaptiveBroadcastJoinStrategy.NONE) | |
| + .withDescription( | |
| + "Flink will perform broadcast hash join optimization when the runtime " | |
| + + "statistics on one side of a join operator is less than the " | |
| + + "threshold `table.optimizer.join.broadcast-threshold`. The " | |
| + + "value of this configuration option decides when Flink should " | |
| + + "perform this optimization. AUTO means Flink will automatically " | |
| + + "choose the timing for optimization, RUNTIME_ONLY means broadcast " | |
| + + "hash join optimization is only performed at runtime, and NONE " | |
| + + "means the optimization is only carried out at compile time."); | |
| + | |
| /** | |
| * The data volume of build side needs to be under this value. If the data volume of build side | |
| * is too large, the building overhead will be too large, which may lead to a negative impact on | |
| public enum NonDeterministicUpdateStrategy { | |
| */ | |
| IGNORE | |
| } | |
| + | |
| + /** Strategies used for {@link #TABLE_OPTIMIZER_ADAPTIVE_BROADCAST_JOIN_STRATEGY}. */ | |
| + @PublicEvolving | |
| + public enum AdaptiveBroadcastJoinStrategy implements DescribedEnum { | |
| + AUTO("auto", text("Flink will automatically choose the timing for optimization")), | |
| + RUNTIME_ONLY( | |
| + "runtime_only", | |
| + text("Broadcast hash join optimization is only performed at runtime.")), | |
| + NONE("none", text("Broadcast hash join optimization is only carried out at compile time.")); | |
| + | |
| + private final String value; | |
| + | |
| + private final InlineElement description; | |
| + | |
| + AdaptiveBroadcastJoinStrategy(String value, InlineElement description) { | |
| + this.value = value; | |
| + this.description = description; | |
| + } | |
| + | |
| + @Override | |
| + public String toString() { | |
| + return value; | |
| + } | |
| + | |
| + @Override | |
| + public InlineElement getDescription() { | |
| + return description; | |
| + } | |
| + } | |
| } | |
| diff --git a/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/AdaptiveJoinExecNode.java b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/AdaptiveJoinExecNode.java | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/AdaptiveJoinExecNode.java | |
| +/* | |
| + * 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; | |
| + | |
| +import org.apache.flink.table.planner.plan.nodes.exec.batch.BatchExecAdaptiveJoin; | |
| + | |
| +/** A {@link ExecNode} which support to convert to adaptive join node. */ | |
| +public interface AdaptiveJoinExecNode { | |
| + | |
| + /** Check whether the join node can be transformed to {@link BatchExecAdaptiveJoin}. */ | |
| + boolean canBeTransformedToAdaptiveJoin(); | |
| + | |
| + /** Transform this node to {@link BatchExecAdaptiveJoin}. */ | |
| + BatchExecAdaptiveJoin toAdaptiveJoinNode(); | |
| +} | |
| diff --git a/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecAdaptiveJoin.java b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecAdaptiveJoin.java | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecAdaptiveJoin.java | |
| +/* | |
| + * 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.ReadableConfig; | |
| +import org.apache.flink.streaming.api.operators.StreamOperatorFactory; | |
| +import org.apache.flink.table.api.TableException; | |
| +import org.apache.flink.table.data.RowData; | |
| +import org.apache.flink.table.planner.adaptive.AdaptiveJoinOperatorGenerator; | |
| +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.ExecNodeBase; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.ExecNodeConfig; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.ExecNodeContext; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.InputProperty; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.SingleTransformationTranslator; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.spec.JoinSpec; | |
| +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.OperatorType; | |
| +import org.apache.flink.table.runtime.generated.GeneratedJoinCondition; | |
| +import org.apache.flink.table.runtime.operators.join.adaptive.AdaptiveJoin; | |
| +import org.apache.flink.table.runtime.operators.join.adaptive.AdaptiveJoinOperatorFactory; | |
| +import org.apache.flink.table.runtime.typeutils.InternalTypeInfo; | |
| +import org.apache.flink.table.types.logical.RowType; | |
| +import org.apache.flink.util.InstantiationUtil; | |
| + | |
| +import java.io.IOException; | |
| +import java.util.List; | |
| + | |
| +/** {@link BatchExecNode} for adaptive join. */ | |
| +public class BatchExecAdaptiveJoin extends ExecNodeBase<RowData> | |
| + implements BatchExecNode<RowData>, SingleTransformationTranslator<RowData> { | |
| + | |
| + private final JoinSpec joinSpec; | |
| + private final boolean leftIsBuild; | |
| + private final int estimatedLeftAvgRowSize; | |
| + private final int estimatedRightAvgRowSize; | |
| + private final long estimatedLeftRowCount; | |
| + private final long estimatedRightRowCount; | |
| + private final boolean tryDistinctBuildRow; | |
| + private final String description; | |
| + private final OperatorType originalJoin; | |
| + | |
| + public BatchExecAdaptiveJoin( | |
| + ReadableConfig tableConfig, | |
| + JoinSpec joinSpec, | |
| + int estimatedLeftAvgRowSize, | |
| + int estimatedRightAvgRowSize, | |
| + long estimatedLeftRowCount, | |
| + long estimatedRightRowCount, | |
| + boolean leftIsBuild, | |
| + boolean tryDistinctBuildRow, | |
| + List<InputProperty> inputProperties, | |
| + RowType outputType, | |
| + String description, | |
| + OperatorType originalJoin) { | |
| + super( | |
| + ExecNodeContext.newNodeId(), | |
| + ExecNodeContext.newContext(BatchExecAdaptiveJoin.class), | |
| + ExecNodeContext.newPersistedConfig(BatchExecAdaptiveJoin.class, tableConfig), | |
| + inputProperties, | |
| + outputType, | |
| + description); | |
| + this.joinSpec = joinSpec; | |
| + this.estimatedLeftAvgRowSize = estimatedLeftAvgRowSize; | |
| + this.estimatedRightAvgRowSize = estimatedRightAvgRowSize; | |
| + this.estimatedLeftRowCount = estimatedLeftRowCount; | |
| + this.estimatedRightRowCount = estimatedRightRowCount; | |
| + this.leftIsBuild = leftIsBuild; | |
| + this.tryDistinctBuildRow = tryDistinctBuildRow; | |
| + this.description = description; | |
| + this.originalJoin = originalJoin; | |
| + } | |
| + | |
| + @Override | |
| + @SuppressWarnings("unchecked") | |
| + protected Transformation<RowData> translateToPlanInternal( | |
| + PlannerBase planner, ExecNodeConfig config) { | |
| + ExecEdge leftInputEdge = getInputEdges().get(0); | |
| + ExecEdge rightInputEdge = getInputEdges().get(1); | |
| + | |
| + Transformation<RowData> leftInputTransform = | |
| + (Transformation<RowData>) leftInputEdge.translateToPlan(planner); | |
| + Transformation<RowData> rightInputTransform = | |
| + (Transformation<RowData>) rightInputEdge.translateToPlan(planner); | |
| + // get input types | |
| + RowType leftType = (RowType) leftInputEdge.getOutputType(); | |
| + RowType rightType = (RowType) rightInputEdge.getOutputType(); | |
| + long managedMemory = JoinUtil.getManagedMemory(joinSpec.getJoinType(), config); | |
| + GeneratedJoinCondition condFunc = | |
| + JoinUtil.generateConditionFunction( | |
| + config, | |
| + planner.getFlinkContext().getClassLoader(), | |
| + joinSpec.getNonEquiCondition().orElse(null), | |
| + leftType, | |
| + rightType); | |
| + | |
| + AdaptiveJoinOperatorGenerator adaptiveJoin = | |
| + new AdaptiveJoinOperatorGenerator( | |
| + joinSpec.getLeftKeys(), | |
| + joinSpec.getRightKeys(), | |
| + joinSpec.getJoinType(), | |
| + joinSpec.getFilterNulls(), | |
| + leftType, | |
| + rightType, | |
| + condFunc, | |
| + estimatedLeftAvgRowSize, | |
| + estimatedRightAvgRowSize, | |
| + estimatedLeftRowCount, | |
| + estimatedRightRowCount, | |
| + tryDistinctBuildRow, | |
| + managedMemory, | |
| + leftIsBuild, | |
| + originalJoin); | |
| + | |
| + return ExecNodeUtil.createTwoInputTransformation( | |
| + leftInputTransform, | |
| + rightInputTransform, | |
| + createTransformationName(config), | |
| + createTransformationDescription(config), | |
| + getAdaptiveJoinOperatorFactory(adaptiveJoin), | |
| + InternalTypeInfo.of(getOutputType()), | |
| + // Given that the probe side might be decided at runtime, we choose the larger | |
| + // parallelism here. | |
| + Math.max(leftInputTransform.getParallelism(), rightInputTransform.getParallelism()), | |
| + managedMemory, | |
| + false); | |
| + } | |
| + | |
| + private StreamOperatorFactory<RowData> getAdaptiveJoinOperatorFactory( | |
| + AdaptiveJoin adaptiveJoin) { | |
| + try { | |
| + byte[] adaptiveJoinSerialized = InstantiationUtil.serializeObject(adaptiveJoin); | |
| + return new AdaptiveJoinOperatorFactory<>(adaptiveJoinSerialized); | |
| + } catch (IOException e) { | |
| + throw new TableException("The adaptive join operator failed to serialize.", e); | |
| + } | |
| + } | |
| + | |
| + @Override | |
| + public String getDescription() { | |
| + return "AdaptiveJoin(" | |
| + + "originalJoin=[" | |
| + + originalJoin | |
| + + "], " | |
| + + description.substring(description.indexOf('(') + 1); | |
| + } | |
| +} | |
| diff --git a/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecHashJoin.java b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecHashJoin.java | |
| --- a/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecHashJoin.java | |
| +++ b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecHashJoin.java | |
| import org.apache.flink.FlinkVersion; | |
| import org.apache.flink.api.dag.Transformation; | |
| import org.apache.flink.configuration.ReadableConfig; | |
| -import org.apache.flink.streaming.api.operators.SimpleOperatorFactory; | |
| import org.apache.flink.streaming.api.operators.StreamOperatorFactory; | |
| 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.LongHashJoinGenerator; | |
| -import org.apache.flink.table.planner.codegen.ProjectionCodeGenerator; | |
| import org.apache.flink.table.planner.delegation.PlannerBase; | |
| import org.apache.flink.table.planner.plan.fusion.OpFusionCodegenSpecGenerator; | |
| import org.apache.flink.table.planner.plan.fusion.generator.TwoInputOpFusionCodegenSpecGenerator; | |
| import org.apache.flink.table.planner.plan.fusion.spec.HashJoinFusionCodegenSpec; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.AdaptiveJoinExecNode; | |
| import org.apache.flink.table.planner.plan.nodes.exec.ExecEdge; | |
| import org.apache.flink.table.planner.plan.nodes.exec.ExecNodeBase; | |
| import org.apache.flink.table.planner.plan.nodes.exec.ExecNodeConfig; | |
| import org.apache.flink.table.planner.plan.nodes.exec.SingleTransformationTranslator; | |
| import org.apache.flink.table.planner.plan.nodes.exec.spec.JoinSpec; | |
| import org.apache.flink.table.planner.plan.nodes.exec.utils.ExecNodeUtil; | |
| +import org.apache.flink.table.planner.plan.utils.HashJoinOperatorUtil; | |
| import org.apache.flink.table.planner.plan.utils.JoinUtil; | |
| -import org.apache.flink.table.planner.plan.utils.SorMergeJoinOperatorUtil; | |
| +import org.apache.flink.table.planner.plan.utils.OperatorType; | |
| import org.apache.flink.table.runtime.generated.GeneratedJoinCondition; | |
| -import org.apache.flink.table.runtime.generated.GeneratedProjection; | |
| import org.apache.flink.table.runtime.operators.join.FlinkJoinType; | |
| -import org.apache.flink.table.runtime.operators.join.HashJoinOperator; | |
| import org.apache.flink.table.runtime.operators.join.HashJoinType; | |
| -import org.apache.flink.table.runtime.operators.join.SortMergeJoinFunction; | |
| import org.apache.flink.table.runtime.typeutils.InternalTypeInfo; | |
| import org.apache.flink.table.types.logical.LogicalType; | |
| import org.apache.flink.table.types.logical.RowType; | |
| minPlanVersion = FlinkVersion.v2_0, | |
| minStateVersion = FlinkVersion.v2_0) | |
| public class BatchExecHashJoin extends ExecNodeBase<RowData> | |
| - implements BatchExecNode<RowData>, SingleTransformationTranslator<RowData> { | |
| + implements BatchExecNode<RowData>, | |
| + SingleTransformationTranslator<RowData>, | |
| + AdaptiveJoinExecNode { | |
| public static final String JOIN_TRANSFORMATION = "join"; | |
| public static final String FIELD_NAME_JOIN_SPEC = "joinSpec"; | |
| public class BatchExecHashJoin extends ExecNodeBase<RowData> | |
| public static final String FIELD_NAME_ESTIMATED_LEFT_ROW_COUNT = "estimatedLeftRowCount"; | |
| public static final String FIELD_NAME_ESTIMATED_RIGHT_ROW_COUNT = "estimatedRightRowCount"; | |
| public static final String FIELD_NAME_TRY_DISTINCT_BUILD_ROW = "tryDistinctBuildRow"; | |
| + public static final String FIELD_NAME_WITH_JOIN_STRATEGY_HINT = "withJobStrategyHint"; | |
| @JsonProperty(FIELD_NAME_JOIN_SPEC) | |
| private final JoinSpec joinSpec; | |
| public class BatchExecHashJoin extends ExecNodeBase<RowData> | |
| @JsonProperty(FIELD_NAME_ESTIMATED_RIGHT_ROW_COUNT) | |
| private final long estimatedRightRowCount; | |
| + @JsonProperty(FIELD_NAME_WITH_JOIN_STRATEGY_HINT) | |
| + private final boolean withJobStrategyHint; | |
| + | |
| @JsonProperty(FIELD_NAME_TRY_DISTINCT_BUILD_ROW) | |
| private final boolean tryDistinctBuildRow; | |
| public BatchExecHashJoin( | |
| InputProperty leftInputProperty, | |
| InputProperty rightInputProperty, | |
| RowType outputType, | |
| + boolean withJobStrategyHint, | |
| String description) { | |
| super( | |
| ExecNodeContext.newNodeId(), | |
| public BatchExecHashJoin( | |
| this.estimatedLeftRowCount = estimatedLeftRowCount; | |
| this.estimatedRightRowCount = estimatedRightRowCount; | |
| this.tryDistinctBuildRow = tryDistinctBuildRow; | |
| + this.withJobStrategyHint = withJobStrategyHint; | |
| } | |
| @JsonCreator | |
| public BatchExecHashJoin( | |
| @JsonProperty(FIELD_NAME_TRY_DISTINCT_BUILD_ROW) boolean tryDistinctBuildRow, | |
| @JsonProperty(FIELD_NAME_INPUT_PROPERTIES) List<InputProperty> inputProperties, | |
| @JsonProperty(FIELD_NAME_OUTPUT_TYPE) RowType outputType, | |
| - @JsonProperty(FIELD_NAME_DESCRIPTION) String description) { | |
| + @JsonProperty(FIELD_NAME_DESCRIPTION) String description, | |
| + @JsonProperty(FIELD_NAME_WITH_JOIN_STRATEGY_HINT) boolean withJobStrategyHint) { | |
| super(id, context, persistedConfig, inputProperties, outputType, description); | |
| checkArgument(inputProperties.size() == 2); | |
| this.joinSpec = checkNotNull(joinSpec); | |
| public BatchExecHashJoin( | |
| this.estimatedLeftRowCount = estimatedLeftRowCount; | |
| this.estimatedRightRowCount = estimatedRightRowCount; | |
| this.tryDistinctBuildRow = tryDistinctBuildRow; | |
| + this.withJobStrategyHint = withJobStrategyHint; | |
| } | |
| @Override | |
| protected Transformation<RowData> translateToPlanInternal( | |
| // get input types | |
| RowType leftType = (RowType) leftInputEdge.getOutputType(); | |
| RowType rightType = (RowType) rightInputEdge.getOutputType(); | |
| - | |
| - JoinUtil.validateJoinSpec(joinSpec, leftType, rightType, false); | |
| - | |
| - int[] leftKeys = joinSpec.getLeftKeys(); | |
| - int[] rightKeys = joinSpec.getRightKeys(); | |
| - LogicalType[] keyFieldTypes = | |
| - IntStream.of(leftKeys).mapToObj(leftType::getTypeAt).toArray(LogicalType[]::new); | |
| - RowType keyType = RowType.of(keyFieldTypes); | |
| + long managedMemory = JoinUtil.getManagedMemory(joinSpec.getJoinType(), config); | |
| GeneratedJoinCondition condFunc = | |
| JoinUtil.generateConditionFunction( | |
| protected Transformation<RowData> translateToPlanInternal( | |
| leftType, | |
| rightType); | |
| - // projection for equals | |
| - GeneratedProjection leftProj = | |
| - ProjectionCodeGenerator.generateProjection( | |
| - new CodeGeneratorContext( | |
| - config, planner.getFlinkContext().getClassLoader()), | |
| - "HashJoinLeftProjection", | |
| + StreamOperatorFactory<RowData> operator = | |
| + HashJoinOperatorUtil.generateOperatorFactory( | |
| + joinSpec.getLeftKeys(), | |
| + joinSpec.getRightKeys(), | |
| + joinSpec.getJoinType(), | |
| + joinSpec.getFilterNulls(), | |
| leftType, | |
| - keyType, | |
| - leftKeys); | |
| - GeneratedProjection rightProj = | |
| - ProjectionCodeGenerator.generateProjection( | |
| - new CodeGeneratorContext( | |
| - config, planner.getFlinkContext().getClassLoader()), | |
| - "HashJoinRightProjection", | |
| rightType, | |
| - keyType, | |
| - rightKeys); | |
| + condFunc, | |
| + leftIsBuild, | |
| + estimatedLeftAvgRowSize, | |
| + estimatedRightAvgRowSize, | |
| + estimatedLeftRowCount, | |
| + estimatedRightRowCount, | |
| + tryDistinctBuildRow, | |
| + managedMemory, | |
| + config, | |
| + planner.getFlinkContext().getClassLoader()); | |
| Transformation<RowData> buildTransform; | |
| Transformation<RowData> probeTransform; | |
| - GeneratedProjection buildProj; | |
| - GeneratedProjection probeProj; | |
| - int[] buildKeys; | |
| - int[] probeKeys; | |
| - RowType buildType; | |
| - RowType probeType; | |
| - int buildRowSize; | |
| - long buildRowCount; | |
| - long probeRowCount; | |
| - boolean reverseJoin = !leftIsBuild; | |
| if (leftIsBuild) { | |
| buildTransform = leftInputTransform; | |
| - buildProj = leftProj; | |
| - buildType = leftType; | |
| - buildRowSize = estimatedLeftAvgRowSize; | |
| - buildRowCount = estimatedLeftRowCount; | |
| - buildKeys = leftKeys; | |
| - | |
| probeTransform = rightInputTransform; | |
| - probeProj = rightProj; | |
| - probeType = rightType; | |
| - probeRowCount = estimatedRightRowCount; | |
| - probeKeys = rightKeys; | |
| } else { | |
| buildTransform = rightInputTransform; | |
| - buildProj = rightProj; | |
| - buildType = rightType; | |
| - buildRowSize = estimatedRightAvgRowSize; | |
| - buildRowCount = estimatedRightRowCount; | |
| - buildKeys = rightKeys; | |
| - | |
| probeTransform = leftInputTransform; | |
| - probeProj = leftProj; | |
| - probeType = leftType; | |
| - probeRowCount = estimatedLeftRowCount; | |
| - probeKeys = leftKeys; | |
| - } | |
| - | |
| - // operator | |
| - StreamOperatorFactory<RowData> operator; | |
| - FlinkJoinType joinType = joinSpec.getJoinType(); | |
| - HashJoinType hashJoinType = | |
| - HashJoinType.of( | |
| - leftIsBuild, | |
| - joinType.isLeftOuter(), | |
| - joinType.isRightOuter(), | |
| - joinType == FlinkJoinType.SEMI, | |
| - joinType == FlinkJoinType.ANTI); | |
| - | |
| - long externalBufferMemory = | |
| - config.get(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_EXTERNAL_BUFFER_MEMORY) | |
| - .getBytes(); | |
| - long managedMemory = getLargeManagedMemory(joinType, config); | |
| - | |
| - // sort merge join function | |
| - SortMergeJoinFunction sortMergeJoinFunction = | |
| - SorMergeJoinOperatorUtil.getSortMergeJoinFunction( | |
| - planner.getFlinkContext().getClassLoader(), | |
| - config, | |
| - joinType, | |
| - leftType, | |
| - rightType, | |
| - leftKeys, | |
| - rightKeys, | |
| - keyType, | |
| - leftIsBuild, | |
| - joinSpec.getFilterNulls(), | |
| - condFunc, | |
| - 1.0 * externalBufferMemory / managedMemory); | |
| - | |
| - boolean compressionEnabled = | |
| - config.get(ExecutionConfigOptions.TABLE_EXEC_SPILL_COMPRESSION_ENABLED); | |
| - int compressionBlockSize = | |
| - (int) | |
| - config.get(ExecutionConfigOptions.TABLE_EXEC_SPILL_COMPRESSION_BLOCK_SIZE) | |
| - .getBytes(); | |
| - if (LongHashJoinGenerator.support(hashJoinType, keyType, joinSpec.getFilterNulls())) { | |
| - operator = | |
| - LongHashJoinGenerator.gen( | |
| - config, | |
| - planner.getFlinkContext().getClassLoader(), | |
| - hashJoinType, | |
| - keyType, | |
| - buildType, | |
| - probeType, | |
| - buildKeys, | |
| - probeKeys, | |
| - buildRowSize, | |
| - buildRowCount, | |
| - reverseJoin, | |
| - condFunc, | |
| - leftIsBuild, | |
| - compressionEnabled, | |
| - compressionBlockSize, | |
| - sortMergeJoinFunction); | |
| - } else { | |
| - operator = | |
| - SimpleOperatorFactory.of( | |
| - HashJoinOperator.newHashJoinOperator( | |
| - hashJoinType, | |
| - leftIsBuild, | |
| - compressionEnabled, | |
| - compressionBlockSize, | |
| - condFunc, | |
| - reverseJoin, | |
| - joinSpec.getFilterNulls(), | |
| - buildProj, | |
| - probeProj, | |
| - tryDistinctBuildRow, | |
| - buildRowSize, | |
| - buildRowCount, | |
| - probeRowCount, | |
| - keyType, | |
| - sortMergeJoinFunction)); | |
| } | |
| return ExecNodeUtil.createTwoInputTransformation( | |
| protected Transformation<RowData> translateToPlanInternal( | |
| false); | |
| } | |
| - private long getLargeManagedMemory(FlinkJoinType joinType, ExecNodeConfig config) { | |
| - long hashJoinManagedMemory = | |
| - config.get(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_HASH_JOIN_MEMORY).getBytes(); | |
| - | |
| - // The memory used by SortMergeJoinIterator that buffer the matched rows, each side needs | |
| - // this memory if it is full outer join | |
| - long externalBufferMemory = | |
| - config.get(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_EXTERNAL_BUFFER_MEMORY) | |
| - .getBytes(); | |
| - // The memory used by BinaryExternalSorter for sort, the left and right side both need it | |
| - long sortMemory = | |
| - config.get(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_SORT_MEMORY).getBytes(); | |
| - int externalBufferNum = 1; | |
| - if (joinType == FlinkJoinType.FULL) { | |
| - externalBufferNum = 2; | |
| - } | |
| - long sortMergeJoinManagedMemory = externalBufferMemory * externalBufferNum + sortMemory * 2; | |
| - | |
| - // Due to hash join maybe fallback to sort merge join, so here managed memory choose the | |
| - // large one | |
| - return Math.max(hashJoinManagedMemory, sortMergeJoinManagedMemory); | |
| - } | |
| - | |
| @Override | |
| public boolean supportFusionCodegen() { | |
| RowType leftType = (RowType) getInputEdges().get(0).getOutputType(); | |
| protected OpFusionCodegenSpecGenerator translateToFusionCodegenSpecInternal( | |
| (int) | |
| config.get(ExecutionConfigOptions.TABLE_EXEC_SPILL_COMPRESSION_BLOCK_SIZE) | |
| .getBytes(); | |
| - long managedMemory = getLargeManagedMemory(joinSpec.getJoinType(), config); | |
| + long managedMemory = JoinUtil.getManagedMemory(joinSpec.getJoinType(), config); | |
| OpFusionCodegenSpecGenerator hashJoinGenerator = | |
| new TwoInputOpFusionCodegenSpecGenerator( | |
| leftInput, | |
| protected OpFusionCodegenSpecGenerator translateToFusionCodegenSpecInternal( | |
| rightInput.addOutput(2, hashJoinGenerator); | |
| return hashJoinGenerator; | |
| } | |
| + | |
| + @Override | |
| + public boolean canBeTransformedToAdaptiveJoin() { | |
| + return !withJobStrategyHint && joinSpec.getJoinType() != FlinkJoinType.FULL; | |
| + } | |
| + | |
| + @Override | |
| + public BatchExecAdaptiveJoin toAdaptiveJoinNode() { | |
| + return new BatchExecAdaptiveJoin( | |
| + getPersistedConfig(), | |
| + joinSpec, | |
| + estimatedLeftAvgRowSize, | |
| + estimatedRightAvgRowSize, | |
| + estimatedLeftRowCount, | |
| + estimatedRightRowCount, | |
| + leftIsBuild, | |
| + tryDistinctBuildRow, | |
| + getInputProperties(), | |
| + (RowType) getOutputType(), | |
| + getDescription(), | |
| + OperatorType.ShuffleHashJoin); | |
| + } | |
| } | |
| diff --git a/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecSortMergeJoin.java b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecSortMergeJoin.java | |
| --- a/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecSortMergeJoin.java | |
| +++ b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/batch/BatchExecSortMergeJoin.java | |
| import org.apache.flink.api.dag.Transformation; | |
| import org.apache.flink.configuration.ReadableConfig; | |
| -import org.apache.flink.streaming.api.operators.SimpleOperatorFactory; | |
| +import org.apache.flink.streaming.api.operators.StreamOperatorFactory; | |
| import org.apache.flink.table.api.config.ExecutionConfigOptions; | |
| import org.apache.flink.table.data.RowData; | |
| import org.apache.flink.table.planner.delegation.PlannerBase; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.AdaptiveJoinExecNode; | |
| import org.apache.flink.table.planner.plan.nodes.exec.ExecEdge; | |
| import org.apache.flink.table.planner.plan.nodes.exec.ExecNodeBase; | |
| import org.apache.flink.table.planner.plan.nodes.exec.ExecNodeConfig; | |
| import org.apache.flink.table.planner.plan.nodes.exec.ExecNodeContext; | |
| import org.apache.flink.table.planner.plan.nodes.exec.InputProperty; | |
| import org.apache.flink.table.planner.plan.nodes.exec.SingleTransformationTranslator; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.spec.JoinSpec; | |
| 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.OperatorType; | |
| import org.apache.flink.table.planner.plan.utils.SorMergeJoinOperatorUtil; | |
| 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.SortMergeJoinFunction; | |
| -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.calcite.rex.RexNode; | |
| import javax.annotation.Nullable; | |
| import java.util.Arrays; | |
| -import java.util.stream.IntStream; | |
| import static org.apache.flink.util.Preconditions.checkArgument; | |
| import static org.apache.flink.util.Preconditions.checkNotNull; | |
| /** {@link BatchExecNode} for Sort Merge Join. */ | |
| public class BatchExecSortMergeJoin extends ExecNodeBase<RowData> | |
| - implements BatchExecNode<RowData>, SingleTransformationTranslator<RowData> { | |
| + implements BatchExecNode<RowData>, | |
| + SingleTransformationTranslator<RowData>, | |
| + AdaptiveJoinExecNode { | |
| private final FlinkJoinType joinType; | |
| private final int[] leftKeys; | |
| private final int[] rightKeys; | |
| private final boolean[] filterNulls; | |
| private final @Nullable RexNode nonEquiCondition; | |
| + private final int estimatedLeftAvgRowSize; | |
| + private final int estimatedRightAvgRowSize; | |
| + private final long estimatedLeftRowCount; | |
| + private final long estimatedRightRowCount; | |
| private final boolean leftIsSmaller; | |
| + private final boolean withJobStrategyHint; | |
| public BatchExecSortMergeJoin( | |
| ReadableConfig tableConfig, | |
| public BatchExecSortMergeJoin( | |
| int[] rightKeys, | |
| boolean[] filterNulls, | |
| @Nullable RexNode nonEquiCondition, | |
| + int estimatedLeftAvgRowSize, | |
| + int estimatedRightAvgRowSize, | |
| + long estimatedLeftRowCount, | |
| + long estimatedRightRowCount, | |
| boolean leftIsSmaller, | |
| InputProperty leftInputProperty, | |
| InputProperty rightInputProperty, | |
| RowType outputType, | |
| + boolean withJobStrategyHint, | |
| String description) { | |
| super( | |
| ExecNodeContext.newNodeId(), | |
| public BatchExecSortMergeJoin( | |
| checkArgument(leftKeys.length == filterNulls.length); | |
| this.nonEquiCondition = nonEquiCondition; | |
| + this.estimatedLeftAvgRowSize = estimatedLeftAvgRowSize; | |
| + this.estimatedRightAvgRowSize = estimatedRightAvgRowSize; | |
| + this.estimatedLeftRowCount = estimatedLeftRowCount; | |
| + this.estimatedRightRowCount = estimatedRightRowCount; | |
| this.leftIsSmaller = leftIsSmaller; | |
| + this.withJobStrategyHint = withJobStrategyHint; | |
| } | |
| @Override | |
| protected Transformation<RowData> translateToPlanInternal( | |
| RowType leftType = (RowType) leftInputEdge.getOutputType(); | |
| RowType rightType = (RowType) rightInputEdge.getOutputType(); | |
| - LogicalType[] keyFieldTypes = | |
| - IntStream.of(leftKeys).mapToObj(leftType::getTypeAt).toArray(LogicalType[]::new); | |
| - RowType keyType = RowType.of(keyFieldTypes); | |
| - | |
| - GeneratedJoinCondition condFunc = | |
| - JoinUtil.generateConditionFunction( | |
| - config, | |
| - planner.getFlinkContext().getClassLoader(), | |
| - nonEquiCondition, | |
| - leftType, | |
| - rightType); | |
| - | |
| long externalBufferMemory = | |
| config.get(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_EXTERNAL_BUFFER_MEMORY) | |
| .getBytes(); | |
| protected Transformation<RowData> translateToPlanInternal( | |
| long managedMemory = externalBufferMemory * externalBufferNum + sortMemory * 2; | |
| - SortMergeJoinFunction sortMergeJoinFunction = | |
| - SorMergeJoinOperatorUtil.getSortMergeJoinFunction( | |
| - planner.getFlinkContext().getClassLoader(), | |
| + GeneratedJoinCondition condFunc = | |
| + JoinUtil.generateConditionFunction( | |
| config, | |
| - joinType, | |
| + planner.getFlinkContext().getClassLoader(), | |
| + nonEquiCondition, | |
| + leftType, | |
| + rightType); | |
| + | |
| + StreamOperatorFactory<RowData> sortMergeJoinOperatorFactory = | |
| + SorMergeJoinOperatorUtil.generateOperatorFactory( | |
| + condFunc, | |
| leftType, | |
| rightType, | |
| leftKeys, | |
| rightKeys, | |
| - keyType, | |
| + joinType, | |
| + config, | |
| leftIsSmaller, | |
| filterNulls, | |
| - condFunc, | |
| - 1.0 * externalBufferMemory / managedMemory); | |
| + managedMemory, | |
| + planner.getFlinkContext().getClassLoader()); | |
| Transformation<RowData> leftInputTransform = | |
| (Transformation<RowData>) leftInputEdge.translateToPlan(planner); | |
| protected Transformation<RowData> translateToPlanInternal( | |
| rightInputTransform, | |
| createTransformationName(config), | |
| createTransformationDescription(config), | |
| - SimpleOperatorFactory.of(new SortMergeJoinOperator(sortMergeJoinFunction)), | |
| + sortMergeJoinOperatorFactory, | |
| InternalTypeInfo.of(getOutputType()), | |
| rightInputTransform.getParallelism(), | |
| managedMemory, | |
| false); | |
| } | |
| + | |
| + @Override | |
| + public boolean canBeTransformedToAdaptiveJoin() { | |
| + return !withJobStrategyHint && joinType != FlinkJoinType.FULL; | |
| + } | |
| + | |
| + @Override | |
| + public BatchExecAdaptiveJoin toAdaptiveJoinNode() { | |
| + return new BatchExecAdaptiveJoin( | |
| + getPersistedConfig(), | |
| + new JoinSpec(joinType, leftKeys, rightKeys, filterNulls, nonEquiCondition), | |
| + estimatedLeftAvgRowSize, | |
| + estimatedRightAvgRowSize, | |
| + estimatedLeftRowCount, | |
| + estimatedRightRowCount, | |
| + leftIsSmaller, | |
| + false, | |
| + getInputProperties(), | |
| + (RowType) getOutputType(), | |
| + getDescription(), | |
| + OperatorType.SortMergeJoin); | |
| + } | |
| } | |
| diff --git a/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/processor/AdaptiveJoinProcessor.java b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/processor/AdaptiveJoinProcessor.java | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/processor/AdaptiveJoinProcessor.java | |
| +/* | |
| + * 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.processor; | |
| + | |
| +import org.apache.flink.configuration.JobManagerOptions; | |
| +import org.apache.flink.table.api.TableConfig; | |
| +import org.apache.flink.table.api.TableException; | |
| +import org.apache.flink.table.api.config.OptimizerConfigOptions; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.AdaptiveJoinExecNode; | |
| +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.ExecNodeGraph; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.InputProperty; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.InputProperty.DistributionType; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.batch.BatchExecAdaptiveJoin; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.batch.BatchExecExchange; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.stream.StreamExecNode; | |
| +import org.apache.flink.table.planner.plan.nodes.exec.visitor.AbstractExecNodeExactlyOnceVisitor; | |
| +import org.apache.flink.table.planner.plan.utils.OperatorType; | |
| +import org.apache.flink.table.planner.utils.TableConfigUtils; | |
| + | |
| +import java.util.ArrayList; | |
| +import java.util.List; | |
| +import java.util.stream.Collectors; | |
| + | |
| +import static org.apache.flink.table.planner.plan.nodes.exec.InputProperty.DistributionType.KEEP_INPUT_AS_IS; | |
| + | |
| +/** | |
| + * A {@link ExecNodeGraphProcessor} which replace the qualified join nodes into adaptive join nodes. | |
| + */ | |
| +public class AdaptiveJoinProcessor implements ExecNodeGraphProcessor { | |
| + | |
| + @Override | |
| + public ExecNodeGraph process(ExecNodeGraph execGraph, ProcessorContext context) { | |
| + if (execGraph.getRootNodes().get(0) instanceof StreamExecNode) { | |
| + throw new TableException("AdaptiveJoin does not support streaming jobs."); | |
| + } | |
| + if (!isAdaptiveJoinEnabled(context)) { | |
| + return execGraph; | |
| + } | |
| + | |
| + AbstractExecNodeExactlyOnceVisitor visitor = | |
| + new AbstractExecNodeExactlyOnceVisitor() { | |
| + @Override | |
| + protected void visitNode(ExecNode<?> node) { | |
| + visitInputs(node); | |
| + // AdaptiveJoin conversion should be avoided when there is a | |
| + // KEEP_INPUT_AS_IS constraint downstream. And we don't need to check all | |
| + // downstream nodes of the join, because the KEEP_INPUT_AS_IS constraint | |
| + // will be bound to BatchExecExchange, which will be the direct downstream | |
| + // node of the join. | |
| + if (shouldKeepInputAsIs(node.getInputProperties())) { | |
| + return; | |
| + } | |
| + for (int i = 0; i < node.getInputEdges().size(); ++i) { | |
| + ExecEdge edge = node.getInputEdges().get(i); | |
| + ExecNode<?> newNode = tryReplaceWithAdaptiveJoinNode(edge.getSource()); | |
| + node.replaceInputEdge( | |
| + i, | |
| + ExecEdge.builder() | |
| + .source(newNode) | |
| + .target(node) | |
| + .shuffle(edge.getShuffle()) | |
| + .exchangeMode(edge.getExchangeMode()) | |
| + .build()); | |
| + } | |
| + } | |
| + }; | |
| + | |
| + List<ExecNode<?>> newRootNodes = | |
| + execGraph.getRootNodes().stream() | |
| + .map( | |
| + node -> { | |
| + node = tryReplaceWithAdaptiveJoinNode(node); | |
| + node.accept(visitor); | |
| + return node; | |
| + }) | |
| + .collect(Collectors.toList()); | |
| + | |
| + return new ExecNodeGraph(execGraph.getFlinkVersion(), newRootNodes); | |
| + } | |
| + | |
| + private ExecNode<?> tryReplaceWithAdaptiveJoinNode(ExecNode<?> node) { | |
| + // For AdaptiveJoin to be converted, its upstream input must ensure: | |
| + // 1. Data distribution is by Hash. | |
| + // 2. No upstream nodes require KEEP_INPUT_AS_IS (achieved by inserting BatchExecExchange). | |
| + if (!(areAllInputsHashShuffle(node)) | |
| + || shouldKeepUpstreamExchangeInputAsIs(node.getInputEdges())) { | |
| + return node; | |
| + } | |
| + ExecNode<?> newNode = node; | |
| + if (node instanceof AdaptiveJoinExecNode | |
| + && ((AdaptiveJoinExecNode) node).canBeTransformedToAdaptiveJoin()) { | |
| + BatchExecAdaptiveJoin adaptiveJoin = ((AdaptiveJoinExecNode) node).toAdaptiveJoinNode(); | |
| + replaceInputEdge(adaptiveJoin, node); | |
| + newNode = adaptiveJoin; | |
| + } | |
| + | |
| + return newNode; | |
| + } | |
| + | |
| + private boolean shouldKeepInputAsIs(List<InputProperty> inputProperties) { | |
| + return inputProperties.stream() | |
| + .anyMatch( | |
| + inputProperty -> | |
| + inputProperty.getRequiredDistribution().getType() | |
| + == KEEP_INPUT_AS_IS); | |
| + } | |
| + | |
| + // If KEEP_INPUT_AS_IS constraint exists on an operator, it will always show on its upstream | |
| + // BatchExecExchange. | |
| + private boolean shouldKeepUpstreamExchangeInputAsIs(List<ExecEdge> inputEdges) { | |
| + return inputEdges.stream() | |
| + .filter(execEdge -> execEdge.getSource() instanceof BatchExecExchange) | |
| + .map(execEdge -> (BatchExecExchange) execEdge.getSource()) | |
| + .anyMatch(exchange -> shouldKeepInputAsIs(exchange.getInputProperties())); | |
| + } | |
| + | |
| + private boolean isAdaptiveJoinEnabled(ProcessorContext context) { | |
| + TableConfig tableConfig = context.getPlanner().getTableConfig(); | |
| + boolean isAdaptiveJoinEnabled = | |
| + tableConfig.get( | |
| + OptimizerConfigOptions | |
| + .TABLE_OPTIMIZER_ADAPTIVE_BROADCAST_JOIN_STRATEGY) | |
| + != OptimizerConfigOptions.AdaptiveBroadcastJoinStrategy.NONE | |
| + && !TableConfigUtils.isOperatorDisabled( | |
| + tableConfig, OperatorType.BroadcastHashJoin); | |
| + JobManagerOptions.SchedulerType schedulerType = | |
| + context.getPlanner() | |
| + .getExecEnv() | |
| + .getConfig() | |
| + .getSchedulerType() | |
| + .orElse(JobManagerOptions.SchedulerType.AdaptiveBatch); | |
| + boolean isAdaptiveBatchSchedulerEnabled = | |
| + schedulerType == JobManagerOptions.SchedulerType.AdaptiveBatch; | |
| + | |
| + return isAdaptiveJoinEnabled && isAdaptiveBatchSchedulerEnabled; | |
| + } | |
| + | |
| + private boolean areAllInputsHashShuffle(ExecNode<?> node) { | |
| + for (InputProperty inputProperty : node.getInputProperties()) { | |
| + if (inputProperty.getRequiredDistribution().getType() != DistributionType.HASH) { | |
| + return false; | |
| + } | |
| + } | |
| + return true; | |
| + } | |
| + | |
| + private void replaceInputEdge(ExecNode<?> newNode, ExecNode<?> originalNode) { | |
| + List<ExecEdge> inputEdges = new ArrayList<>(); | |
| + for (int i = 0; i < originalNode.getInputEdges().size(); ++i) { | |
| + ExecEdge edge = originalNode.getInputEdges().get(i); | |
| + inputEdges.add( | |
| + ExecEdge.builder() | |
| + .source(edge.getSource()) | |
| + .target(newNode) | |
| + .shuffle(edge.getShuffle()) | |
| + .exchangeMode(edge.getExchangeMode()) | |
| + .build()); | |
| + } | |
| + newNode.setInputEdges(inputEdges); | |
| + } | |
| +} | |
| diff --git a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/delegation/BatchPlanner.scala b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/delegation/BatchPlanner.scala | |
| --- a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/delegation/BatchPlanner.scala | |
| +++ b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/delegation/BatchPlanner.scala | |
| import org.apache.flink.table.api.config.OptimizerConfigOptions | |
| import org.apache.flink.table.catalog.{CatalogManager, FunctionCatalog} | |
| import org.apache.flink.table.delegation.{Executor, InternalPlan} | |
| import org.apache.flink.table.module.ModuleManager | |
| -import org.apache.flink.table.operations.{ModifyOperation, Operation} | |
| +import org.apache.flink.table.operations.Operation | |
| import org.apache.flink.table.planner.plan.`trait`.FlinkRelDistributionTraitDef | |
| import org.apache.flink.table.planner.plan.nodes.exec.ExecNodeGraph | |
| import org.apache.flink.table.planner.plan.nodes.exec.batch.BatchExecNode | |
| -import org.apache.flink.table.planner.plan.nodes.exec.processor.{DeadlockBreakupProcessor, DynamicFilteringDependencyProcessor, ExecNodeGraphProcessor, ForwardHashExchangeProcessor, MultipleInputNodeCreationProcessor} | |
| +import org.apache.flink.table.planner.plan.nodes.exec.processor.{AdaptiveJoinProcessor, DeadlockBreakupProcessor, DynamicFilteringDependencyProcessor, ExecNodeGraphProcessor, ForwardHashExchangeProcessor, MultipleInputNodeCreationProcessor} | |
| import org.apache.flink.table.planner.plan.nodes.exec.utils.ExecNodePlanDumper | |
| import org.apache.flink.table.planner.plan.optimize.{BatchCommonSubGraphBasedOptimizer, Optimizer} | |
| import org.apache.flink.table.planner.plan.utils.FlinkRelOptUtil | |
| class BatchPlanner( | |
| processors.add(new MultipleInputNodeCreationProcessor(false)) | |
| } | |
| processors.add(new ForwardHashExchangeProcessor) | |
| + processors.add(new AdaptiveJoinProcessor) | |
| processors | |
| } | |
| diff --git a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/nodes/physical/batch/BatchPhysicalHashJoin.scala b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/nodes/physical/batch/BatchPhysicalHashJoin.scala | |
| --- a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/nodes/physical/batch/BatchPhysicalHashJoin.scala | |
| +++ b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/nodes/physical/batch/BatchPhysicalHashJoin.scala | |
| class BatchPhysicalHashJoin( | |
| val leftIsBuild: Boolean, | |
| // true if build side is broadcast, else false | |
| val isBroadcast: Boolean, | |
| - val tryDistinctBuildRow: Boolean) | |
| + val tryDistinctBuildRow: Boolean, | |
| + val withJobStrategyHint: Boolean) | |
| extends BatchPhysicalJoinBase(cluster, traitSet, leftRel, rightRel, condition, joinType) { | |
| val hashJoinType: HashJoinType = HashJoinType.of( | |
| class BatchPhysicalHashJoin( | |
| joinType, | |
| leftIsBuild, | |
| isBroadcast, | |
| - tryDistinctBuildRow) | |
| + tryDistinctBuildRow, | |
| + withJobStrategyHint) | |
| } | |
| override def explainTerms(pw: RelWriter): RelWriter = { | |
| class BatchPhysicalHashJoin( | |
| FlinkTypeFactory.toLogicalRowType(left.getRowType), | |
| FlinkTypeFactory.toLogicalRowType(right.getRowType)) | |
| - val mq = getCluster.getMetadataQuery | |
| - val leftRowSize = Util.first(mq.getAverageRowSize(left), 24).toInt | |
| - val leftRowCount = Util.first(mq.getRowCount(left), 200000).toLong | |
| - val rightRowSize = Util.first(mq.getAverageRowSize(right), 24).toInt | |
| - val rightRowCount = Util.first(mq.getRowCount(right), 200000).toLong | |
| + val (leftRowSize, leftRowCount, rightRowSize, rightRowCount) = | |
| + JoinUtil.getEstimatedRowStats(this) | |
| val (leftEdge, rightEdge) = getInputProperties | |
| new BatchExecHashJoin( | |
| unwrapTableConfig(this), | |
| class BatchPhysicalHashJoin( | |
| leftEdge, | |
| rightEdge, | |
| FlinkTypeFactory.toLogicalRowType(getRowType), | |
| + withJobStrategyHint, | |
| getRelDetailedDescription) | |
| } | |
| diff --git a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/nodes/physical/batch/BatchPhysicalSortMergeJoin.scala b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/nodes/physical/batch/BatchPhysicalSortMergeJoin.scala | |
| --- a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/nodes/physical/batch/BatchPhysicalSortMergeJoin.scala | |
| +++ b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/nodes/physical/batch/BatchPhysicalSortMergeJoin.scala | |
| import org.apache.calcite.rel.{RelCollationTraitDef, RelNode, RelWriter} | |
| import org.apache.calcite.rel.core._ | |
| import org.apache.calcite.rel.metadata.RelMetadataQuery | |
| import org.apache.calcite.rex.RexNode | |
| +import org.apache.calcite.util.Util | |
| import scala.collection.JavaConversions._ | |
| class BatchPhysicalSortMergeJoin( | |
| // true if LHS is sorted by left join keys, else false | |
| val leftSorted: Boolean, | |
| // true if RHS is sorted by right join key, else false | |
| - val rightSorted: Boolean) | |
| + val rightSorted: Boolean, | |
| + withJobStrategyHint: Boolean) | |
| extends BatchPhysicalJoinBase(cluster, traitSet, leftRel, rightRel, condition, joinType) { | |
| protected def isMergeJoinSupportedType(joinRelType: FlinkJoinType): Boolean = { | |
| class BatchPhysicalSortMergeJoin( | |
| conditionExpr, | |
| joinType, | |
| leftSorted, | |
| - rightSorted) | |
| + rightSorted, | |
| + withJobStrategyHint) | |
| } | |
| override def explainTerms(pw: RelWriter): RelWriter = | |
| class BatchPhysicalSortMergeJoin( | |
| FlinkTypeFactory.toLogicalRowType(left.getRowType), | |
| FlinkTypeFactory.toLogicalRowType(right.getRowType)) | |
| + val (leftRowSize, leftRowCount, rightRowSize, rightRowCount) = | |
| + JoinUtil.getEstimatedRowStats(this) | |
| new BatchExecSortMergeJoin( | |
| unwrapTableConfig(this), | |
| JoinTypeUtil.getFlinkJoinType(joinType), | |
| joinSpec.getLeftKeys, | |
| joinSpec.getRightKeys, | |
| joinSpec.getFilterNulls, | |
| condition, | |
| + leftRowSize, | |
| + rightRowSize, | |
| + leftRowCount, | |
| + rightRowCount, | |
| estimateOutputSize(getLeft) < estimateOutputSize(getRight), | |
| InputProperty | |
| .builder() | |
| class BatchPhysicalSortMergeJoin( | |
| .damBehavior(InputProperty.DamBehavior.END_INPUT) | |
| .build(), | |
| FlinkTypeFactory.toLogicalRowType(getRowType), | |
| + withJobStrategyHint, | |
| getRelDetailedDescription) | |
| } | |
| diff --git a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/batch/BatchPhysicalHashJoinRule.scala b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/batch/BatchPhysicalHashJoinRule.scala | |
| --- a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/batch/BatchPhysicalHashJoinRule.scala | |
| +++ b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/batch/BatchPhysicalHashJoinRule.scala | |
| import org.apache.flink.table.planner.plan.`trait`.FlinkRelDistribution | |
| import org.apache.flink.table.planner.plan.nodes.FlinkConventions | |
| import org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalJoin | |
| import org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalHashJoin | |
| +import org.apache.flink.table.planner.plan.utils.JoinUtil | |
| import org.apache.flink.table.planner.utils.ShortcutUtils.unwrapTableConfig | |
| import org.apache.calcite.plan.{RelOptRule, RelOptRuleCall, RelTraitSet} | |
| class BatchPhysicalHashJoinRule | |
| val newLeft = RelOptRule.convert(left, leftRequiredTrait) | |
| val newRight = RelOptRule.convert(right, rightRequiredTrait) | |
| val providedTraitSet = join.getTraitSet.replace(FlinkConventions.BATCH_PHYSICAL) | |
| + val withJobStrategyHint = JoinUtil.containsJoinStrategyHint(join.getHints) | |
| val newJoin = new BatchPhysicalHashJoin( | |
| join.getCluster, | |
| class BatchPhysicalHashJoinRule | |
| join.getJoinType, | |
| isLeftToBroadcastOrBuild, | |
| isBroadcast, | |
| - tryDistinctBuildRow) | |
| + tryDistinctBuildRow, | |
| + withJobStrategyHint) | |
| call.transformTo(newJoin) | |
| } | |
| diff --git a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/batch/BatchPhysicalJoinRuleBase.scala b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/batch/BatchPhysicalJoinRuleBase.scala | |
| --- a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/batch/BatchPhysicalJoinRuleBase.scala | |
| +++ b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/batch/BatchPhysicalJoinRuleBase.scala | |
| trait BatchPhysicalJoinRuleBase { | |
| (false, false) | |
| } | |
| } else { | |
| + if ( | |
| + tableConfig.get(OptimizerConfigOptions.TABLE_OPTIMIZER_ADAPTIVE_BROADCAST_JOIN_STRATEGY) | |
| + == OptimizerConfigOptions.AdaptiveBroadcastJoinStrategy.RUNTIME_ONLY | |
| + ) { | |
| + return (false, false) | |
| + } | |
| + | |
| val leftSize = JoinUtil.binaryRowRelNodeSize(join.getLeft) | |
| val rightSize = JoinUtil.binaryRowRelNodeSize(join.getRight) | |
| diff --git a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/batch/BatchPhysicalSortMergeJoinRule.scala b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/batch/BatchPhysicalSortMergeJoinRule.scala | |
| --- a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/batch/BatchPhysicalSortMergeJoinRule.scala | |
| +++ b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/rules/physical/batch/BatchPhysicalSortMergeJoinRule.scala | |
| import org.apache.flink.table.planner.plan.`trait`.FlinkRelDistribution | |
| import org.apache.flink.table.planner.plan.nodes.FlinkConventions | |
| import org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalJoin | |
| import org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalSortMergeJoin | |
| +import org.apache.flink.table.planner.plan.utils.JoinUtil | |
| import org.apache.flink.table.planner.utils.ShortcutUtils.unwrapTableConfig | |
| import org.apache.calcite.plan.{RelOptRule, RelOptRuleCall, RelTraitSet} | |
| class BatchPhysicalSortMergeJoinRule | |
| val providedTraitSet = call.getPlanner | |
| .emptyTraitSet() | |
| .replace(FlinkConventions.BATCH_PHYSICAL) | |
| + val withJobStrategyHint = JoinUtil.containsJoinStrategyHint(join.getHints) | |
| // do not try to remove redundant sort for shorter optimization time | |
| val newJoin = new BatchPhysicalSortMergeJoin( | |
| join.getCluster, | |
| class BatchPhysicalSortMergeJoinRule | |
| join.getCondition, | |
| join.getJoinType, | |
| false, | |
| - false) | |
| + false, | |
| + withJobStrategyHint) | |
| call.transformTo(newJoin) | |
| } | |
| } | |
| diff --git a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/utils/JoinUtil.scala b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/utils/JoinUtil.scala | |
| --- a/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/utils/JoinUtil.scala | |
| +++ b/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/planner/plan/utils/JoinUtil.scala | |
| package org.apache.flink.table.planner.plan.utils | |
| import org.apache.flink.configuration.ReadableConfig | |
| import org.apache.flink.table.api.TableException | |
| +import org.apache.flink.table.api.config.ExecutionConfigOptions | |
| import org.apache.flink.table.data.RowData | |
| import org.apache.flink.table.planner.JDouble | |
| import org.apache.flink.table.planner.calcite.FlinkTypeFactory | |
| import org.apache.flink.table.planner.codegen.{CodeGeneratorContext, ExprCodeGenerator, FunctionCodeGenerator} | |
| +import org.apache.flink.table.planner.hint.JoinStrategy | |
| +import org.apache.flink.table.planner.plan.nodes.exec.ExecNodeConfig | |
| import org.apache.flink.table.planner.plan.nodes.exec.spec.JoinSpec | |
| import org.apache.flink.table.planner.plan.nodes.logical.{FlinkLogicalJoin, FlinkLogicalSnapshot} | |
| +import org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalJoinBase | |
| import org.apache.flink.table.planner.plan.utils.IntervalJoinUtil.satisfyIntervalJoin | |
| import org.apache.flink.table.planner.plan.utils.TemporalJoinUtil.satisfyTemporalJoin | |
| import org.apache.flink.table.planner.plan.utils.WindowJoinUtil.satisfyWindowJoin | |
| 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.stream.utils.JoinInputSideSpec | |
| import org.apache.flink.table.runtime.types.PlannerTypeUtils | |
| import org.apache.flink.table.runtime.typeutils.InternalTypeInfo | |
| import org.apache.flink.table.types.logical.{LogicalType, RowType} | |
| +import com.google.common.collect.ImmutableList | |
| import org.apache.calcite.plan.RelOptUtil | |
| import org.apache.calcite.rel.`type`.{RelDataType, RelDataTypeField} | |
| import org.apache.calcite.rel.RelNode | |
| import org.apache.calcite.rel.core.{Join, JoinInfo, JoinRelType} | |
| +import org.apache.calcite.rel.hint.RelHint | |
| import org.apache.calcite.rex.{RexCall, RexInputRef, RexNode, RexUtil} | |
| import org.apache.calcite.sql.validate.SqlValidatorUtil | |
| import org.apache.calcite.util.ImmutableIntList | |
| +import org.apache.calcite.util.Util | |
| import java.util | |
| import java.util.Collections | |
| object JoinUtil { | |
| rowCount * FlinkRelMdUtil.binaryRowAverageSize(relNode) | |
| } | |
| } | |
| + | |
| + /** | |
| + * Get managed memory for hash join. Because hash join may fallback to sort merge join, it takes | |
| + * larger managed memory to support a HashJoin being converted into a SortMergeJoin. | |
| + */ | |
| + def getManagedMemory(joinType: FlinkJoinType, config: ExecNodeConfig): Long = { | |
| + val hashJoinManagedMemory = | |
| + config.get(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_HASH_JOIN_MEMORY).getBytes | |
| + // The memory used by SortMergeJoinIterator that buffer the matched rows, each side needs | |
| + // this memory if it is full outer join | |
| + val externalBufferMemory = | |
| + config.get(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_EXTERNAL_BUFFER_MEMORY).getBytes | |
| + // The memory used by BinaryExternalSorter for sort, the left and right side both need it | |
| + val sortMemory = config.get(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_SORT_MEMORY).getBytes | |
| + var externalBufferNum = 1 | |
| + if (joinType eq FlinkJoinType.FULL) { | |
| + externalBufferNum = 2 | |
| + } | |
| + val sortMergeJoinManagedMemory = externalBufferMemory * externalBufferNum + sortMemory * 2 | |
| + // Due to hash join maybe fallback to sort merge join, so here managed memory choose the | |
| + // large one | |
| + Math.max(hashJoinManagedMemory, sortMergeJoinManagedMemory) | |
| + } | |
| + | |
| + /** Get estimated row stats for hash join. */ | |
| + def getEstimatedRowStats(joinBase: BatchPhysicalJoinBase): (Int, Long, Int, Long) = { | |
| + val mq = joinBase.getCluster.getMetadataQuery | |
| + val leftRowSize = Util.first(mq.getAverageRowSize(joinBase.getLeft), 24).toInt | |
| + val leftRowCount = Util.first(mq.getRowCount(joinBase.getLeft), 200000).toLong | |
| + val rightRowSize = Util.first(mq.getAverageRowSize(joinBase.getRight), 24).toInt | |
| + val rightRowCount = Util.first(mq.getRowCount(joinBase.getRight), 200000).toLong | |
| + | |
| + (leftRowSize, leftRowCount, rightRowSize, rightRowCount) | |
| + } | |
| + | |
| + def containsJoinStrategyHint(relHints: ImmutableList[RelHint]): Boolean = { | |
| + relHints.exists(relHint => JoinStrategy.isJoinStrategy(relHint.hintName)) | |
| + } | |
| } | |