vldpyatkov commented on code in PR #5370:
URL: https://github.com/apache/calcite/pull/5370#discussion_r4530363323
##########
core/src/main/java/org/apache/calcite/sql/validate/RecursiveWithRewriter.java:
##########
@@ -110,25 +114,98 @@ static SqlWithItem rewrite(SqlValidatorImpl validator,
SqlValidatorScope scope,
* The added WHERE condition stops expanding rows already marked as cycles.
* The recursive output keys are taken after the user's projection. A row
* closing a cycle is therefore returned with its extended path, but cannot
- * feed the next recursive step. The original UNION [ALL] is retained.
- * The returned WITH item declares the two additional columns and no longer
- * has a CYCLE clause. */
+ * feed the next recursive step.
+ *
+ * <p>For {@code SEARCH DEPTH FIRST BY k1, k2 SET searchOrder}, the
+ * transformation is schematically:
+ *
+ * <pre>{@code
+ * seed output:
+ * original columns,
+ * ARRAY[ROW(k1, k2)] AS searchOrder
+ * recursive branch:
+ * SELECT original output columns,
+ * append(previousOrder, ROW(k1, k2)) AS searchOrder
+ * FROM (
+ * SELECT original projection, previous.searchOrder AS previousOrder
+ * FROM original sources
+ * WHERE original condition
+ * ) AS step
+ * }</pre>
+ *
+ * <p>The generated array contains the key tuples along the path, including
+ * the current row. Lexicographic ordering of these arrays places a parent
+ * before its descendants. For distinct sibling keys, a node's descendants
+ * precede the next sibling.
+ * The keys within each tuple follow the order of the BY list. Even a single
+ * BY column is wrapped in ROW, so the generated column is always an array
+ * of rows. The internal {@link SqlInternalOperators#RECURSIVE_PATH_APPEND}
+ * operator implements append.
+ *
+ * <p>For {@code SEARCH BREADTH FIRST BY k1, k2 SET searchOrder}, the
+ * transformation is schematically:
+ *
+ * <pre>{@code
+ * seed output:
+ * original columns,
+ * ROW(0, k1, k2) AS searchOrder
+ * recursive branch:
+ * SELECT original output columns,
+ * ROW(previousOrder.$DEPTH + 1, k1, k2) AS searchOrder
+ * FROM (
+ * SELECT original projection, previous.searchOrder AS previousOrder
+ * FROM original sources
+ * WHERE original condition
+ * ) AS step
+ * }</pre>
+ *
+ * <p>Here {@code $DEPTH} denotes the first field of the generated row. It is
Review Comment:
Test added RecursiveCteTest#testSearchBreadthFirstDepthNameCollision.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]