-
Notifications
You must be signed in to change notification settings - Fork 13.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FLINK-12173][table] Optimize SELECT DISTINCT #25752
Open
yiyutian1
wants to merge
1
commit into
apache:master
Choose a base branch
from
yiyutian1:flink12173
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
...nk/table/planner/plan/rules/physical/stream/StreamLogicalOptimizeSelectDistinctRule.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* 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.stream | ||
import org.apache.flink.table.planner.JList | ||
import org.apache.flink.table.planner.calcite.FlinkTypeFactory | ||
import org.apache.flink.table.planner.calcite.FlinkTypeFactory.{isRowtimeIndicatorType, isTimeIndicatorType} | ||
import org.apache.flink.table.planner.plan.metadata.FlinkRelMetadataQuery | ||
import org.apache.flink.table.planner.plan.nodes.FlinkConventions | ||
import org.apache.flink.table.planner.plan.nodes.logical.{FlinkLogicalAggregate, FlinkLogicalCalc, FlinkLogicalJoin, FlinkLogicalRank} | ||
import org.apache.flink.table.planner.plan.nodes.physical.stream.{StreamPhysicalIntervalJoin, StreamPhysicalRank, StreamPhysicalTemporalSort} | ||
import org.apache.flink.table.planner.plan.utils.{RankProcessStrategy, RankUtil, WindowJoinUtil} | ||
import org.apache.flink.table.planner.plan.utils.WindowUtil.groupingContainsWindowStartEnd | ||
import org.apache.flink.table.runtime.operators.rank.{ConstantRankRange, RankType} | ||
import org.apache.flink.table.types.logical.IntType | ||
|
||
import org.apache.calcite.plan.{RelOptRule, RelOptRuleCall} | ||
import org.apache.calcite.plan.RelOptRule.{any, operand} | ||
import org.apache.calcite.rel.`type`.{RelDataType, RelDataTypeField, RelDataTypeFieldImpl, RelDataTypeSystem} | ||
import org.apache.calcite.rel.{RelCollation, RelCollations, RelFieldCollation} | ||
import org.apache.calcite.rel.RelNode | ||
import org.apache.calcite.rel.convert.ConverterRule.Config | ||
import org.apache.calcite.rel.core.JoinRelType | ||
import org.apache.calcite.rel.hint.RelHint | ||
import org.apache.calcite.rel.logical.LogicalProject | ||
import org.apache.calcite.rex.{RexInputRef, RexNode, RexProgram} | ||
import org.apache.calcite.util.ImmutableBitSet | ||
|
||
import java.util | ||
import java.util.Collections | ||
|
||
import scala.collection.convert.ImplicitConversions.`iterable AsScalaIterable` | ||
|
||
/** | ||
* Rule that matches [[FlinkLogicalAggregate]], and converts it to [[FlinkLogicalRank]] in the case | ||
* of SELECT DISTINCT queries. | ||
* | ||
* e.g. {SELECT DISTINCT a, b, c;} will be converted to [[FlinkLogicalRank]] instead of | ||
* [[FlinkLogicalAggregate]] in rowtime. | ||
*/ | ||
class StreamLogicalOptimizeSelectDistinctRule | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally it would be great to have it converted to java in this PR. |
||
extends RelOptRule(operand(classOf[FlinkLogicalAggregate], any)) { | ||
private val classLoader = Thread.currentThread().getContextClassLoader | ||
private val typeSystem = RelDataTypeSystem.DEFAULT | ||
private val typeFactory = new FlinkTypeFactory(classLoader, typeSystem) | ||
private val intType: RelDataType = | ||
typeFactory.createFieldTypeFromLogicalType(new IntType(false)) | ||
|
||
override def matches(call: RelOptRuleCall): Boolean = { | ||
val rel: FlinkLogicalAggregate = call.rel(0) | ||
// check if it's a SELECT DISTINCT query | ||
val ret = | ||
rel.getGroupSet.cardinality() == rel.getRowType.getFieldCount && rel.getAggCallList.isEmpty | ||
|
||
val mq = call.getMetadataQuery | ||
val fmq = FlinkRelMetadataQuery.reuseOrCreate(mq) | ||
val windowProperties = fmq.getRelWindowProperties(rel.getInput) | ||
val grouping = rel.getGroupSet | ||
if (groupingContainsWindowStartEnd(grouping, windowProperties)) { | ||
return false // do not match if the grouping set contains window start and end | ||
} | ||
|
||
if (ret) { | ||
rel.getGroupSet.toList.foreach( | ||
i => { | ||
val field: RelDataTypeField = rel.getInput.getRowType.getFieldList.get(i) | ||
if (isTimeIndicatorType(field.getType)) { | ||
return false | ||
} | ||
}) | ||
} | ||
ret | ||
} | ||
|
||
override def onMatch(call: RelOptRuleCall): Unit = { | ||
val agg: FlinkLogicalAggregate = call.rel(0) | ||
|
||
val input = agg.getInput | ||
val traitSet = agg.getTraitSet | ||
val cluster = agg.getCluster | ||
|
||
// Create a list of all group keys | ||
val groupKeys = agg.getGroupSet | ||
|
||
// Create a projection to select only the fields in the DISTINCT clause | ||
val projectList: JList[RexNode] = { | ||
val list = new util.ArrayList[RexNode](groupKeys.cardinality()) | ||
groupKeys.toList.foreach(i => list.add(RexInputRef.of(i, input.getRowType))) | ||
list | ||
} | ||
|
||
// Create the projected row type | ||
val projectedFields: JList[RelDataTypeField] = { | ||
val fields = new util.ArrayList[RelDataTypeField](projectList.size()) | ||
projectList.forEach { | ||
rexNode => | ||
val rexInputRef = rexNode.asInstanceOf[RexInputRef] | ||
fields.add(input.getRowType.getFieldList.get(rexInputRef.getIndex)) | ||
} | ||
fields | ||
} | ||
|
||
// Create the projected row type | ||
val projectedRowType: RelDataType = typeFactory.createStructType(projectedFields) | ||
|
||
// Create a RelCollation based on all group keys | ||
val fieldCollations: JList[RelFieldCollation] = { | ||
val collations = new util.ArrayList[RelFieldCollation](groupKeys.cardinality()) | ||
groupKeys.foreach { | ||
key => collations.add(new RelFieldCollation(key, RelFieldCollation.Direction.ASCENDING)) | ||
} | ||
collations | ||
} | ||
val collation: RelCollation = RelCollations.of(fieldCollations) | ||
|
||
// Create a projection to select only the fields in the DISTINCT clause | ||
val projection: RelNode = FlinkLogicalCalc.create( | ||
input, | ||
RexProgram.create( | ||
input.getRowType, | ||
projectList, | ||
null, | ||
projectedRowType, | ||
cluster.getRexBuilder | ||
)) | ||
|
||
val rank = new FlinkLogicalRank( | ||
cluster, | ||
traitSet, | ||
projection, | ||
groupKeys, | ||
collation, | ||
RankType.ROW_NUMBER, | ||
new ConstantRankRange(1, 1), // We only want the first row for each group | ||
new RelDataTypeFieldImpl("rk", projectedRowType.getFieldCount - 1, intType), | ||
false | ||
) | ||
try RankUtil.canConvertToDeduplicate(rank) | ||
catch { | ||
case _: Exception => | ||
return | ||
} | ||
call.transformTo(rank) | ||
} | ||
} | ||
|
||
object StreamLogicalOptimizeSelectDistinctRule { | ||
val INSTANCE = new StreamLogicalOptimizeSelectDistinctRule() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yiyutian1 and I worked on this together and we started with a Scala example.
Given that we are working on migrating the Scala rules to Java, could you take a look at migrating this rule to Java?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comment Jim!
Are we migrating from Scala->Java for converters now too, or are we mainly doing it for builtInFunctions?
My impression is that for buildInFunctions we want that migration because auto-generated Java code is hard to maintain, but that doesn't seem to be a problem here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@snuyanzin , could you provide some feedback here?
Could we try merging this ticket as is, so that the optimizer can be available soon, and then we can do the migration in a separate effort?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be great to see green ci first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CI is green! Woohoo!
Could we get some feedback? @snuyanzin
Many thanks.