sunjincheng121 commented on a change in pull request #7196: [FLINK-10974]
[table] Add support for flatMap to table API
URL: https://github.com/apache/flink/pull/7196#discussion_r276922137
##########
File path:
flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/operations/OperationTreeBuilder.scala
##########
@@ -365,6 +367,53 @@ class OperationTreeBuilder(private val tableEnv:
TableEnvironment) {
mapFunction.asInstanceOf[CallExpression].getFunctionDefinition.getType
== SCALAR_FUNCTION
}
+ def flatMap(tableFunction: Expression, child: TableOperation):
TableOperation = {
+
+ val resolver = resolverFor(tableCatalog, functionCatalog, child).build()
+ val resolvedTableFunction = resolveSingleExpression(tableFunction,
resolver)
+
+ if (!isTableFunction(resolvedTableFunction)) {
+ throw new ValidationException("Only TableFunction can be used in the
flatMap operator.")
+ }
+
+ val originFieldNames: Seq[String] =
+ resolvedTableFunction.asInstanceOf[CallExpression].getFunctionDefinition
match {
+ case tfd: TableFunctionDefinition =>
+ UserDefinedFunctionUtils.getFieldInfo(tfd.getResultType)._1
+ }
+
+ def getUniqueName(inputName: String, usedFieldNames: Seq[String]): String
= {
+ var i = 0
+ var resultName = inputName
+ while (usedFieldNames.contains(resultName)) {
+ resultName = resultName + "_" + i
+ i += 1
+ }
+ resultName
+ }
+
+ val usedFieldNames =
child.asInstanceOf[LogicalNode].output.map(_.name).toBuffer
+ val newFieldNames = originFieldNames.map({ e =>
+ val resultName = getUniqueName(e, usedFieldNames)
+ usedFieldNames.append(resultName)
+ resultName
+ })
+
+ val renamedTableFunction = ApiExpressionUtils.call(
+ BuiltInFunctionDefinitions.AS,
+ resolvedTableFunction +:
newFieldNames.map(ApiExpressionUtils.valueLiteral(_)): _*)
+ val joinNode = joinLateral(child, renamedTableFunction, JoinType.INNER,
Optional.empty())
+ val dropNode = dropColumns(
Review comment:
`dorpNode` ->`rightNode`, becaust `joinNode`=`leftNode`+`rightNode`. after
`dropCloumns()`, all columns if from `rightNode`. What do you think?
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services