-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-12017][table-planner-blink] Support translation from Rank/Dedu…
…plicate to StreamTransformation.
- Loading branch information
1 parent
096c63a
commit 3c720b9
Showing
62 changed files
with
6,281 additions
and
126 deletions.
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
71 changes: 71 additions & 0 deletions
71
...k-table-planner-blink/src/main/java/org/apache/flink/table/plan/util/KeySelectorUtil.java
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,71 @@ | ||
/* | ||
* 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.plan.util; | ||
|
||
import org.apache.flink.table.api.TableConfig; | ||
import org.apache.flink.table.codegen.CodeGeneratorContext; | ||
import org.apache.flink.table.codegen.ProjectionCodeGenerator; | ||
import org.apache.flink.table.generated.GeneratedProjection; | ||
import org.apache.flink.table.runtime.keySelector.BaseRowKeySelector; | ||
import org.apache.flink.table.runtime.keySelector.BinaryRowKeySelector; | ||
import org.apache.flink.table.runtime.keySelector.NullBinaryRowKeySelector; | ||
import org.apache.flink.table.type.InternalType; | ||
import org.apache.flink.table.type.RowType; | ||
import org.apache.flink.table.typeutils.BaseRowSerializer; | ||
import org.apache.flink.table.typeutils.BaseRowTypeInfo; | ||
import org.apache.flink.table.typeutils.TypeCheckUtils; | ||
|
||
/** | ||
* Utility for KeySelector. | ||
*/ | ||
public class KeySelectorUtil { | ||
|
||
/** | ||
* Create a BaseRowKeySelector to extract keys from DataStream which type is BaseRowTypeInfo. | ||
* | ||
* @param keyFields key fields | ||
* @param rowType type of DataStream to extract keys | ||
* | ||
* @return the BaseRowKeySelector to extract keys from DataStream which type is BaseRowTypeInfo. | ||
*/ | ||
public static BaseRowKeySelector getBaseRowSelector(int[] keyFields, BaseRowTypeInfo rowType) { | ||
if (keyFields.length > 0) { | ||
InternalType[] inputFieldTypes = rowType.getInternalTypes(); | ||
String[] inputFieldNames = rowType.getFieldNames(); | ||
InternalType[] keyFieldTypes = new InternalType[keyFields.length]; | ||
String[] keyFieldNames = new String[keyFields.length]; | ||
for (int i = 0; i < keyFields.length; ++i) { | ||
keyFieldTypes[i] = inputFieldTypes[keyFields[i]]; | ||
keyFieldNames[i] = inputFieldNames[keyFields[i]]; | ||
} | ||
RowType returnType = new RowType(keyFieldTypes, keyFieldNames); | ||
RowType inputType = new RowType(inputFieldTypes, rowType.getFieldNames()); | ||
GeneratedProjection generatedProjection = ProjectionCodeGenerator.generateProjection( | ||
CodeGeneratorContext.apply(new TableConfig()), | ||
BaseRowSerializer.class.getSimpleName(), inputType, returnType, keyFields); | ||
BaseRowTypeInfo keyRowType = returnType.toTypeInfo(); | ||
// check if type implements proper equals/hashCode | ||
TypeCheckUtils.validateEqualsHashCode("grouping", keyRowType); | ||
return new BinaryRowKeySelector(keyRowType, generatedProjection); | ||
} else { | ||
return new NullBinaryRowKeySelector(); | ||
} | ||
} | ||
|
||
} |
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
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
148 changes: 148 additions & 0 deletions
148
...-planner-blink/src/main/scala/org/apache/flink/table/codegen/EqualiserCodeGenerator.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,148 @@ | ||
/* | ||
* 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.codegen | ||
|
||
import org.apache.flink.table.api.TableConfig | ||
import org.apache.flink.table.codegen.CodeGenUtils._ | ||
import org.apache.flink.table.codegen.Indenter.toISC | ||
import org.apache.flink.table.dataformat.{BaseRow, BinaryRow} | ||
import org.apache.flink.table.generated.{GeneratedRecordEqualiser, RecordEqualiser} | ||
import org.apache.flink.table.`type`.{DateType, InternalType, PrimitiveType, RowType, TimeType, TimestampType} | ||
|
||
class EqualiserCodeGenerator(fieldTypes: Seq[InternalType]) { | ||
|
||
private val BASE_ROW = className[BaseRow] | ||
private val BINARY_ROW = className[BinaryRow] | ||
private val RECORD_EQUALISER = className[RecordEqualiser] | ||
private val LEFT_INPUT = "left" | ||
private val RIGHT_INPUT = "right" | ||
|
||
def generateRecordEqualiser(name: String): GeneratedRecordEqualiser = { | ||
// ignore time zone | ||
val ctx = CodeGeneratorContext(new TableConfig) | ||
val className = newName(name) | ||
val header = | ||
s""" | ||
|if ($LEFT_INPUT.getHeader() != $RIGHT_INPUT.getHeader()) { | ||
| return false; | ||
|} | ||
""".stripMargin | ||
|
||
val codes = for (i <- fieldTypes.indices) yield { | ||
val fieldType = fieldTypes(i) | ||
val fieldTypeTerm = primitiveTypeTermForType(fieldType) | ||
val result = s"cmp$i" | ||
val leftNullTerm = "leftIsNull$" + i | ||
val rightNullTerm = "rightIsNull$" + i | ||
val leftFieldTerm = "leftField$" + i | ||
val rightFieldTerm = "rightField$" + i | ||
val equalsCode = if (isInternalPrimitive(fieldType)) { | ||
s"$leftFieldTerm == $rightFieldTerm" | ||
} else if (isBaseRow(fieldType)) { | ||
val equaliserGenerator = | ||
new EqualiserCodeGenerator(fieldType.asInstanceOf[RowType].getFieldTypes) | ||
val generatedEqualiser = equaliserGenerator | ||
.generateRecordEqualiser("field$" + i + "GeneratedEqualiser") | ||
val generatedEqualiserTerm = ctx.addReusableObject( | ||
generatedEqualiser, "field$" + i + "GeneratedEqualiser") | ||
val equaliserTypeTerm = classOf[RecordEqualiser].getCanonicalName | ||
val equaliserTerm = newName("equaliser") | ||
ctx.addReusableMember(s"private $equaliserTypeTerm $equaliserTerm = null;") | ||
ctx.addReusableInitStatement( | ||
s""" | ||
|$equaliserTerm = ($equaliserTypeTerm) | ||
| $generatedEqualiserTerm.newInstance(Thread.currentThread().getContextClassLoader()); | ||
|""".stripMargin) | ||
s"$equaliserTerm.equalsWithoutHeader($leftFieldTerm, $rightFieldTerm)" | ||
} else { | ||
s"$leftFieldTerm.equals($rightFieldTerm)" | ||
} | ||
val leftReadCode = baseRowFieldReadAccess(ctx, i, LEFT_INPUT, fieldType) | ||
val rightReadCode = baseRowFieldReadAccess(ctx, i, RIGHT_INPUT, fieldType) | ||
s""" | ||
|boolean $leftNullTerm = $LEFT_INPUT.isNullAt($i); | ||
|boolean $rightNullTerm = $RIGHT_INPUT.isNullAt($i); | ||
|boolean $result; | ||
|if ($leftNullTerm && $rightNullTerm) { | ||
| $result = true; | ||
|} else if ($leftNullTerm || $rightNullTerm) { | ||
| $result = false; | ||
|} else { | ||
| $fieldTypeTerm $leftFieldTerm = $leftReadCode; | ||
| $fieldTypeTerm $rightFieldTerm = $rightReadCode; | ||
| $result = $equalsCode; | ||
|} | ||
|if (!$result) { | ||
| return false; | ||
|} | ||
""".stripMargin | ||
} | ||
|
||
val functionCode = | ||
j""" | ||
public final class $className implements $RECORD_EQUALISER { | ||
|
||
${ctx.reuseMemberCode()} | ||
|
||
public $className(Object[] references) throws Exception { | ||
${ctx.reuseInitCode()} | ||
} | ||
|
||
@Override | ||
public boolean equals($BASE_ROW $LEFT_INPUT, $BASE_ROW $RIGHT_INPUT) { | ||
if ($LEFT_INPUT instanceof $BINARY_ROW && $RIGHT_INPUT instanceof $BINARY_ROW) { | ||
return $LEFT_INPUT.equals($RIGHT_INPUT); | ||
} else { | ||
$header | ||
${ctx.reuseLocalVariableCode()} | ||
${codes.mkString("\n")} | ||
return true; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equalsWithoutHeader($BASE_ROW $LEFT_INPUT, $BASE_ROW $RIGHT_INPUT) { | ||
if ($LEFT_INPUT instanceof $BINARY_ROW && $RIGHT_INPUT instanceof $BINARY_ROW) { | ||
return (($BINARY_ROW)$LEFT_INPUT).equalsWithoutHeader((($BINARY_ROW)$RIGHT_INPUT)); | ||
} else { | ||
${ctx.reuseLocalVariableCode()} | ||
${codes.mkString("\n")} | ||
return true; | ||
} | ||
} | ||
} | ||
""".stripMargin | ||
|
||
new GeneratedRecordEqualiser(className, functionCode, ctx.references.toArray) | ||
} | ||
|
||
private def isInternalPrimitive(t: InternalType): Boolean = t match { | ||
case _: PrimitiveType => true | ||
|
||
case _: DateType => true | ||
case TimeType.INSTANCE => true | ||
case _: TimestampType => true | ||
|
||
case _ => false | ||
} | ||
|
||
private def isBaseRow(t: InternalType): Boolean = t match { | ||
case _: RowType => true | ||
case _ => false | ||
} | ||
} |
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
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
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
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.