Github user fhueske commented on a diff in the pull request: https://github.com/apache/flink/pull/2653#discussion_r88339010 --- Diff: flink-libraries/flink-table/src/main/scala/org/apache/flink/api/table/table.scala --- @@ -611,6 +612,130 @@ class Table( } /** + * The Cross Apply returns rows form the outer table (table on the left of the Apply operator) + * that produces matching values from the table-valued function (which is on the right side of + * the operator). + * + * The Cross Apply is equivalent to Inner Join, but it works with a table-valued function. + * + * Example: + * + * {{{ + * class MySplitUDTF extends TableFunction[String] { + * def eval(str: String): Unit = { + * str.split("#").foreach(collect) + * } + * } + * + * val split = new MySplitUDTF() + * table.crossApply(split('c).as('s)).select('a,'b,'c,'s) + * }}} + */ + def crossApply(udtf: TableFunctionCall[_]): Table = { + applyInternal(udtf, JoinType.INNER) + } + + /** + * The Cross Apply returns rows form the outer table (table on the left of the Apply operator) + * that produces matching values from the table-valued function (which is on the right side of + * the operator). + * + * The Cross Apply is equivalent to Inner Join, but it works with a table-valued function. + * + * Example: + * + * {{{ + * class MySplitUDTF extends TableFunction[String] { + * def eval(str: String): Unit = { + * str.split("#").foreach(collect) + * } + * } + * + * val split = new MySplitUDTF() + * table.crossApply("split('c') as (s)").select("a, b, c, s") + * }}} + */ + def crossApply(udtf: String): Table = { + applyInternal(udtf, JoinType.INNER) + } + + /** + * The Cross Apply returns rows form the outer table (table on the left of the Apply operator) + * that produces matching values from the table-valued function (which is on the right side of + * the operator). + * + * The Cross Apply is equivalent to Inner Join, but it works with a table-valued function. + * + * Example: + * + * {{{ + * class MySplitUDTF extends TableFunction[String] { + * def eval(str: String): Unit = { + * str.split("#").foreach(collect) + * } + * } + * + * val split = new MySplitUDTF() + * table.outerApply(split('c).as('s)).select('a,'b,'c,'s) + * }}} + */ + def outerApply(udtf: TableFunctionCall[_]): Table = { + applyInternal(udtf, JoinType.LEFT_OUTER) + } + + /** + * The Outer Apply returns all the rows from the outer table (table on the left of the Apply + * operator), and rows that do not matches the condition from the table-valued function (which + * is on the right side of the operator), NULL values are displayed. + * + * The Outer Apply is equivalent to Left Outer Join, but it works with a table-valued function. + * + * Example: + * + * {{{ + * val split = new MySplitUDTF() + * table.crossApply("split('c') as (s)").select("a, b, c, s") + * }}} + */ + def outerApply(udtf: String): Table = { + applyInternal(udtf, JoinType.LEFT_OUTER) + } + + private def applyInternal(udtfString: String, joinType: JoinType): Table = { + val node = ExpressionParser.parseLogicalNode(udtfString) + var alias: Option[Seq[Expression]] = None + val functionCall = node match { + case AliasNode(aliasList, child) => + alias = Some(aliasList) + child + case _ => node + } + + functionCall match { + case call @ UnresolvedTableFunctionCall(name, args) => + val udtfCall = tableEnv.getFunctionCatalog.lookupTableFunction(name, args) + if (alias.isDefined) { + applyInternal(udtfCall.as(alias.get: _*), joinType) + } else { + applyInternal(udtfCall, joinType) + } + case _ => throw new TableException("Cross/Outer Apply only accept TableFunction") + } + } + + private def applyInternal(node: LogicalNode, joinType: JoinType): Table = { + node match { + case udtf: TableFunctionCall[_] => + udtf.setChild(this.logicalPlan) + new Table( + tableEnv, + Join(this.logicalPlan, udtf.validate(tableEnv), joinType, None, + Some(relBuilder.getCluster.createCorrel())).validate(tableEnv)) --- End diff -- We kept Calcite code so far out of our `LogicalNode` (except for `construct()` of course). I think we can move the `relBuilder.getCluster.createCorrel()` call also into `Join.construct()` and change the `corId` constructor parameter of `Join` to a boolean flag that indicates whether we need to generate a `corId` or not.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---