-
Notifications
You must be signed in to change notification settings - Fork 113
Improved PREORDER
-traversal support for recursive queries
#3616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- add tests for preorder / level traversal in YAML.
PREORDER
-traversal support for recursive queries
.../com/apple/foundationdb/record/query/plan/cascades/expressions/RecursiveUnionExpression.java
Outdated
Show resolved
Hide resolved
.../com/apple/foundationdb/record/query/plan/cascades/expressions/RecursiveUnionExpression.java
Outdated
Show resolved
Hide resolved
...n/java/com/apple/foundationdb/record/query/plan/cascades/properties/DerivationsProperty.java
Outdated
Show resolved
Hide resolved
...ain/java/com/apple/foundationdb/record/query/plan/plans/RecordQueryRecursiveDfsJoinPlan.java
Show resolved
Hide resolved
.../java/com/apple/foundationdb/record/query/plan/cascades/properties/StoredRecordProperty.java
Outdated
Show resolved
Hide resolved
.../com/apple/foundationdb/record/query/plan/cascades/rules/ImplementRecursiveDfsUnionRule.java
Outdated
Show resolved
Hide resolved
.../com/apple/foundationdb/record/query/plan/cascades/rules/ImplementRecursiveDfsUnionRule.java
Outdated
Show resolved
Hide resolved
.../com/apple/foundationdb/record/query/plan/cascades/rules/ImplementRecursiveDfsUnionRule.java
Outdated
Show resolved
Hide resolved
...ain/java/com/apple/foundationdb/record/query/plan/plans/RecordQueryRecursiveDfsJoinPlan.java
Show resolved
Hide resolved
...e/src/main/java/com/apple/foundationdb/record/query/plan/plans/RecordQueryRecursivePlan.java
Outdated
Show resolved
Hide resolved
.../com/apple/foundationdb/record/query/plan/cascades/rules/ImplementRecursiveDfsUnionRule.java
Outdated
Show resolved
Hide resolved
...a/com/apple/foundationdb/record/query/plan/cascades/rules/ImplementRecursiveDfsJoinRule.java
Outdated
Show resolved
Hide resolved
...ain/java/com/apple/foundationdb/record/query/plan/plans/RecordQueryRecursiveDfsJoinPlan.java
Outdated
Show resolved
Hide resolved
...ain/java/com/apple/foundationdb/record/query/plan/plans/RecordQueryRecursiveDfsJoinPlan.java
Outdated
Show resolved
Hide resolved
...ain/java/com/apple/foundationdb/record/query/plan/plans/RecordQueryRecursiveDfsJoinPlan.java
Outdated
Show resolved
Hide resolved
- Revert List.of to ImmutableList.of in Planner components.
📊 Metrics Diff Analysis ReportSummary
ℹ️ About this analysisThis automated analysis compares query planner metrics between the base branch and this PR. It categorizes changes into:
The last category in particular may indicate planner regressions that should be investigated. New QueriesCount of new queries by file:
Plan and Metrics ChangedThese queries experienced both plan and metrics changes. This generally indicates that there was some planner change Total: 3 queries Statistical Summary (Plan and Metrics Changed)
There were no queries with significant regressions detected. Minor Changes (Plan and Metrics Changed)In addition, there were 3 queries with minor changes. |
This PR adds query planning and execution support depth-first search traversal for recursive Common Table Expressions (CTEs), providing an efficient alternative to the existing breadth-first traversal with significantly improved continuation sizes.
Internal DFS Traversal Implementation
The core DFS algorithm is implemented through two main components:
RecursiveCursor
: A new cursor class that flattens a tree of cursors using stack-based DFS traversal. It maintains aList<RecursiveNode<T>>
stack for traversal state and implements pre-order traversal logic through therecursionLoop()
method. The cursor supports database transaction continuations and emitsRecursiveValue<T>
objects containing the original value, (in addition to depth level, and leaf status which are not exposed to customers, yet).RecordQueryRecursivePlan
: A new physical query plan that integrates theRecursiveCursor
with the query execution framework. It uses aChildCursorFunction
to bind previous result values for recursive references and maps cursor results back to standard query results.The DFS implementation provides significantly smaller continuation sizes (23-50 bytes) compared to breadth-first traversal (12KB-1MB+), making it more suitable for deep hierarchical queries.
The planner now provides two implementations, when feasible, for recursive union expressions, a recursive union (BFS) and recursive plan (DFS). The cost model considers recursive plan always a winner due to its simplicity.
Enhanced SQL Syntax:
TRAVERSAL
clause:TRAVERSAL = {PREORDER | LEVEL | ANY}
WITH RECURSIVE (TRAVERSAL = PREORDER) cte_name AS (...)
PREORDER
: Depth-first search (new)LEVEL
: Breadth-first traversal (existing)ANY
: Optimizer choice (default)SQL Usage Examples
Basic organizational hierarchy traversal:
This fixes #3043.
It also fixes #3652 by providing an implementation of
equalsWithoutChildren
toTempTableInsertPlan
fixing structural equality semantics of any plan containing that operator (currently, only theRecordQueryRecursiveLevelUnionPlan
).