Skip to content

Commit

Permalink
Migrate BatchPhysicalConstantTableFunctionScanRule to Java
Browse files Browse the repository at this point in the history
  • Loading branch information
snuyanzin committed Mar 3, 2024
1 parent de38468 commit 4f9d95a
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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.rules.physical.batch;

import org.apache.flink.table.planner.plan.nodes.FlinkConventions;
import org.apache.flink.table.planner.plan.nodes.logical.FlinkLogicalTableFunctionScan;
import org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalCorrelate;
import org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalValues;

import com.google.common.collect.ImmutableList;
import org.apache.calcite.plan.RelOptCluster;
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.plan.RelRule;
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.rel.core.JoinRelType;
import org.apache.calcite.rex.RexUtil;
import org.immutables.value.Value;

import scala.Option;

/**
* Converts [[FlinkLogicalTableFunctionScan]] with constant RexCall to {{{
* [[BatchPhysicalCorrelate]] / \ empty [[BatchPhysicalValuesRule]]
* [[FlinkLogicalTableFunctionScan]] }}}.
*
* <p>Add the rule to support select from a UDF directly, such as the following SQL: SELECT * FROM
* LATERAL TABLE(func()) as T(c)
*
* <p>Note: [[BatchPhysicalCorrelateRule]] is responsible for converting a reasonable physical plan
* for the normal correlate query, such as the following SQL: example1: SELECT * FROM T, LATERAL
* TABLE(func()) as T(c) example2: SELECT a, c FROM T, LATERAL TABLE(func(a)) as T(c)
*/
@Value.Enclosing
public class BatchPhysicalConstantTableFunctionScanRule
extends RelRule<
BatchPhysicalConstantTableFunctionScanRule
.BatchPhysicalConstantTableFunctionScanRuleConfig> {

/**
* Creates a RelRule.
*
* @param config
*/
protected BatchPhysicalConstantTableFunctionScanRule(
BatchPhysicalConstantTableFunctionScanRuleConfig config) {
super(config);
}

public boolean matches(RelOptRuleCall call) {
FlinkLogicalTableFunctionScan scan = call.rel(0);
return RexUtil.isConstant(scan.getCall()) && scan.getInputs().isEmpty();
}

public void onMatch(RelOptRuleCall call) {
FlinkLogicalTableFunctionScan scan = call.rel(0);

// create correlate left
RelOptCluster cluster = scan.getCluster();
RelTraitSet traitSet =
call.getPlanner().emptyTraitSet().replace(FlinkConventions.BATCH_PHYSICAL());
BatchPhysicalValues values =
new BatchPhysicalValues(
cluster,
traitSet,
ImmutableList.of(),
cluster.getTypeFactory()
.createStructType(ImmutableList.of(), ImmutableList.of()));

BatchPhysicalCorrelate correlate =
new BatchPhysicalCorrelate(
cluster,
traitSet,
values,
scan,
Option.empty(),
scan.getRowType(),
JoinRelType.INNER);
call.transformTo(correlate);
}

/** Configuration for {@link BatchPhysicalConstantTableFunctionScanRule}. */
@Value.Immutable(singleton = false)
public interface BatchPhysicalConstantTableFunctionScanRuleConfig extends RelRule.Config {
BatchPhysicalConstantTableFunctionScanRule.BatchPhysicalConstantTableFunctionScanRuleConfig
DEFAULT =
ImmutableBatchPhysicalConstantTableFunctionScanRule
.BatchPhysicalConstantTableFunctionScanRuleConfig.builder()
.build()
.withOperandSupplier(
b0 ->
b0.operand(FlinkLogicalTableFunctionScan.class)
.anyInputs())
.withDescription("BatchPhysicalConstantTableFunctionScanRule")
.as(
BatchPhysicalConstantTableFunctionScanRule
.BatchPhysicalConstantTableFunctionScanRuleConfig
.class);

@Override
default BatchPhysicalConstantTableFunctionScanRule toRule() {
return new BatchPhysicalConstantTableFunctionScanRule(this);
}
}

/** Holder for BatchPhysicalConstantTableFunctionScanRule. */
public static class BatchPhysicalConstantTableFunctionScanRuleHolder {
public static final BatchPhysicalConstantTableFunctionScanRule INSTANCE =
BatchPhysicalConstantTableFunctionScanRuleConfig.DEFAULT.toRule();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ object FlinkBatchRuleSets {
// CEP
BatchPhysicalMatchRule.INSTANCE,
// correlate
BatchPhysicalConstantTableFunctionScanRule.INSTANCE,
BatchPhysicalConstantTableFunctionScanRule.BatchPhysicalConstantTableFunctionScanRuleHolder.INSTANCE,
BatchPhysicalCorrelateRule.INSTANCE,
BatchPhysicalPythonCorrelateRule.INSTANCE,
// sink
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion tools/maven/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ under the License.

<!-- Have to use guava directly -->
<suppress
files="OverConvertRule.java|InConverter.java|SymbolUtil.java|RexNodeJsonDeserializer.java|RexNodeJsonSerializer.java|RexNodeJsonSerdeTest.java|FlinkAggregateProjectMergeRule.java"
files="OverConvertRule.java|InConverter.java|SymbolUtil.java|RexNodeJsonDeserializer.java|RexNodeJsonSerializer.java|RexNodeJsonSerdeTest.java|FlinkAggregateProjectMergeRule.java|BatchPhysicalConstantTableFunctionScanRule.java"
checks="IllegalImport"/>
<!-- Classes copied from AWS -->
<suppress
Expand Down

0 comments on commit 4f9d95a

Please sign in to comment.