amaliujia commented on a change in pull request #12169: URL: https://github.com/apache/beam/pull/12169#discussion_r450517162
########## File path: sdks/java/extensions/sql/zetasql/src/main/java/org/apache/beam/sdk/extensions/sql/zetasql/translation/TVFScanConverter.java ########## @@ -42,45 +42,57 @@ @Override public RelNode convert(ResolvedTVFScan zetaNode, List<RelNode> inputs) { RelNode input = inputs.get(0); + RexCall call = + getExpressionConverter() + .convertTableValuedFunction( + input, + zetaNode.getTvf(), + zetaNode.getArgumentList(), + zetaNode.getArgumentList().get(0).getScan() != null + ? zetaNode.getArgumentList().get(0).getScan().getColumnList() + : Collections.emptyList()); RelNode tableFunctionScan = LogicalTableFunctionScan.create( - getCluster(), - inputs, - getExpressionConverter() - .convertTableValuedFunction( - input, - zetaNode.getTvf(), - zetaNode.getArgumentList(), - zetaNode.getArgumentList().get(0).getScan().getColumnList()), - null, - createRowTypeWithWindowStartAndEnd(input.getRowType()), - Collections.EMPTY_SET); + getCluster(), inputs, call, null, call.getType(), Collections.EMPTY_SET); + // Pure SQL UDF's language body is built bottom up, so FunctionArgumentRefMapping should be + // already consumed thus it can be cleared now. + context.clearFunctionArgumentRefMapping(); Review comment: The SQL plan building called from top, but built from bottom. In this case, TVFScanConverter will be called first, where it knows what values of function parameters used in later SELECT, so we add those parameters to Context. Then it goes down to inputs of TVFScanConverter. For pure SQL UDTVF, per definition, inputs of TVFScanConverter are UDTVF's SQL language body, which are also ResolvedNode, and FuncitonArugmentRefMapping is consumed. After the conversion goes back to TVFScanConverter, which means its input has finished conversion (thus has consumed FuncitonArugmentRefMapping), thus `FuncitonArugmentRefMapping` can be cleared. The current implementation in this PR is still fragile. For example, if there are more than one TVFScanConverter, I am thinking current implementation will break. So my nest step is to improve testing coverage and catch more complicated cases. ---------------------------------------------------------------- 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: us...@infra.apache.org