zabetak commented on a change in pull request #1020: [CALCITE-2812] Add algebraic operators to allow expressing recursive queries (Ruben Quesada Lopez) URL: https://github.com/apache/calcite/pull/1020#discussion_r276292360
########## File path: core/src/main/java/org/apache/calcite/tools/RelBuilder.java ########## @@ -1705,6 +1711,61 @@ public RelBuilder minus(boolean all, int n) { return setOp(all, SqlKind.EXCEPT, n); } + /** + * Creates a {@link LogicalTableScan} on a {@link TransientTable} (used e.g. to accumulate + * results in repeat union operation), using as its row type the top of the stack's row type. + * Returns this builder. + */ + @Experimental + public RelBuilder transientScan(String tableName) { + return this.transientScan(tableName, this.peek().getRowType()); + } + + /** + * Creates a {@link LogicalTableScan} on a {@link TransientTable} (used e.g. to accumulate + * results in repeat union operation). + * Returns this builder. + * @param tableName table name + * @param rowType row type of the table + */ + @Experimental + public RelBuilder transientScan(String tableName, RelDataType rowType) { + TransientTable transientTable = new TransientTable(tableName, rowType); + RelOptTable relOptTable = RelOptTableImpl.create( + relOptSchema, + rowType, + transientTable, + ImmutableList.of(tableName)); + RelNode scan = scanFactory.createScan(cluster, relOptTable); + push(scan); + rename(rowType.getFieldNames()); + return this; + } + + /** + * Creates a {@link LogicalRepeatUnion} with no maxRep, i.e. repeatUnion(tableName, -1). + * @param tableName transient table name associated to the repeat union + */ + @Experimental + public RelBuilder repeatUnion(String tableName) { + return this.repeatUnion(tableName, -1); + } + + /** + * Creates a {@link LogicalRepeatUnion} of the two most recent relational expressions + * on the stack. Warning: if these relational expressions are not correctly defined, + * this operation might lead to an infinite loop. + * Returns this builder. + * @param tableName transient table name associated to the repeat union + * @param maxRep maximum number of iterations, -1 means no limit + */ + @Experimental + public RelBuilder repeatUnion(String tableName, int maxRep) { + RelNode iter = LogicalTableSpool.create(build(), Spool.Type.LAZY, Spool.Type.LAZY, tableName); Review comment: I guess it is convenient for the end-users to not have to build separately the spool when they want to perform the classical recursive union but this limits a bit the capabilities of the builder. It may be better to provide a separate method spool to do this. Clients who are only interested for this use-case can build the utility methods themselves. ---------------------------------------------------------------- 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 With regards, Apache Git Services