scarlin-cloudera commented on code in PR #4442: URL: https://github.com/apache/hive/pull/4442#discussion_r1244460449
########## ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/ASTConverter.java: ########## @@ -577,6 +586,64 @@ private QueryBlockInfo convertSource(RelNode r) throws CalciteSemanticException ast = ASTBuilder.subQuery(left, sqAlias); s = new Schema((Union) r, sqAlias); } + } else if (r instanceof HiveTableFunctionScan && + !canOptimizeOutLateralView((HiveTableFunctionScan) r)) { + // In the case where the RelNode is a HiveTableFunctionScan, first we check + // to see if we can't optimize out the lateral view operator. We can optimize the + // operator out if only the udtf fields are grabbed out of the RelNode. If any + // of the base table fields need to be grabbed out, then a 'join' needs to be done + // and we need the lateral view. + TableFunctionScan tfs = ((TableFunctionScan) r); + + // retrieve the base table source. + QueryBlockInfo tableFunctionSource = convertSource(tfs.getInput(0)); + String sqAlias = tableFunctionSource.schema.get(0).table; + // the schema will contain the base table source fields + s = new Schema(tfs, sqAlias); + + // next, set up the select for the parameters of the UDTF + List<ASTNode> children = new ArrayList<>(); + RexCall call = (RexCall) tfs.getCall(); + for (RexNode rn : call.getOperands()) { + ASTNode expr = rn.accept(new RexVisitor(s, r instanceof RexLiteral, + select.getCluster().getRexBuilder())); + children.add(expr); + } + ASTNode function = buildUDTFAST(call.getOperator().getName(), children); + + // Add the function to the SELEXPR + ASTBuilder selexpr = ASTBuilder.construct(HiveParser.TOK_SELEXPR, "TOK_SELEXPR"); + selexpr.add(function); + + // Add only the table generated size columns to the select expr for the function, + // skipping over the base table columns from the input side of the join. + int i = 0; + for (ColumnInfo c : s) { + if (i++ < tableFunctionSource.schema.size()) { + continue; + } + selexpr.add(HiveParser.Identifier, c.column); + } + // add the table alias for the lateral view. + ASTBuilder tabAlias = ASTBuilder.construct(HiveParser.TOK_TABALIAS, "TOK_TABALIAS"); + tabAlias.add(HiveParser.Identifier, sqAlias); + + // add the table alias to the SEL_EXPR + selexpr.add(tabAlias.node()); + + // create the SELECT clause + ASTBuilder sel = ASTBuilder.construct(HiveParser.TOK_SELEXPR, "TOK_SELECT"); + sel.add(selexpr.node()); + + // place the SELECT clause under the LATERAL VIEW clause + ASTBuilder lateralview = ASTBuilder.construct(HiveParser.TOK_LATERAL_VIEW, "TOK_LATERAL_VIEW"); + lateralview.add(sel.node()); + + // finally, add the LATERAL VIEW clause under the left side source which is the base table. Review Comment: Added comment -- 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. To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org