dianfu 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_r239302508
########## File path: flink-libraries/flink-table/src/main/scala/org/apache/flink/table/api/table.scala ########## @@ -999,6 +1001,131 @@ class Table( new OverWindowedTable(this, overWindows.toArray) } + /** + * Performs a flatMap operation with an user-defined table function. + * + * Scala Example: + * {{{ + * class MyFlatMapFunction extends TableFunction[Row] { + * def eval(str : String) { + * if (str.contains("#")) { + * val splits = str.split("#") + * collect(Row.of(splits(0), splits(1))) + * } + * } + * + * def getResultType(signature: Array[Class[_]]): TypeInformation[_] = + * Types.ROW(Types.STRING, Types.STRING) + * } + * + * val func = new MyFlatMapFunction() + * table.flatMap(func('c)).as('a, 'b) + * }}} + * + * Java Example: + * {{{ + * class MyFlatMapFunction extends TableFunction<Row> { + * public void eval(String str) { + * if (str.contains("#")) { + * String[] splits = str.split("#"); + * collect(Row.of(splits[0], splits[1])); + * } + * } + * + * public TypeInformation getResultType(Class[] signature) { + * return Types.ROW(Types.STRING, Types.STRING); + * } + * } + * + * TableFunction func = new MyFlatMapFunction(); + * tableEnv.registerFunction("func", func); + * table.flatMap("func(c)").as("a, b"); + * }}} + */ + def flatMap(tableFunction: Expression): Table = { + unwrap(tableFunction, tableEnv) match { + case _: TableFunctionCall => + case _ => throw new ValidationException("Only TableFunction can be used in flatMap.") + } + + // rename output fields names to avoid ambiguous name + val originalCall = UserDefinedFunctionUtils.createLogicalFunctionCall(tableEnv, tableFunction) + val originalOutputFieldNames = originalCall.output.map(_.name) + val usedFieldNames: mutable.HashSet[String] = mutable.HashSet() + logicalPlan.output.map(_.name).foreach(usedFieldNames.add) Review comment: Good suggestion, done. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services