Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit ee02c09

Browse files
authored
Fix: merge group (#114)
`self.merged_groups.insert` has to be executed after group exprs migration because in group exprs migration it is using reduced group id. If it is executed before, all the exprs in the to be merged group will lost. Signed-off-by: AveryQi115 <[email protected]>
1 parent af92dca commit ee02c09

File tree

2 files changed

+51
-50
lines changed

2 files changed

+51
-50
lines changed

optd-core/src/cascades/memo.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ impl<T: RelNodeTyp> Memo<T> {
117117
if group_a == group_b {
118118
return group_a;
119119
}
120-
self.merged_groups
121-
.insert(group_a.as_group_id(), group_b.as_group_id());
122120

123121
// Copy all expressions from group a to group b
124122
let group_a_exprs = self.get_all_exprs_in_group(group_a.as_group_id());
@@ -127,6 +125,9 @@ impl<T: RelNodeTyp> Memo<T> {
127125
self.add_expr_to_group(expr_id, group_b, expr_node.as_ref().clone());
128126
}
129127

128+
self.merged_groups
129+
.insert(group_a.as_group_id(), group_b.as_group_id());
130+
130131
// Remove all expressions from group a (so we don't accidentally access it)
131132
self.clear_exprs_in_group(group_a);
132133

optd-sqlplannertest/tests/tpch.planner.sql

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,55 +1863,55 @@ PhysicalSort
18631863
├── exprs:SortOrder { order: Asc }
18641864
│ └── #0
18651865
└── PhysicalProjection { exprs: [ #0, #1, #2, #3, #4 ] }
1866-
└── PhysicalProjection { exprs: [ #0, #1, #2, #3, #5 ] }
1867-
└── PhysicalHashJoin { join_type: Inner, left_keys: [ #0 ], right_keys: [ #0 ] }
1868-
├── PhysicalProjection { exprs: [ #0, #1, #2, #4 ] }
1869-
│ └── PhysicalScan { table: supplier }
1870-
└── PhysicalHashJoin { join_type: Inner, left_keys: [ #1 ], right_keys: [ #0 ] }
1871-
├── PhysicalProjection { exprs: [ #0, #1 ] }
1872-
│ └── PhysicalAgg
1873-
│ ├── aggrs:Agg(Sum)
1874-
│ │ └── Mul
1875-
│ │ ├── #1
1876-
│ │ └── Sub
1877-
│ │ ├── 1
1878-
│ │ └── #2
1879-
│ ├── groups: [ #0 ]
1880-
│ └── PhysicalProjection { exprs: [ #0, #1, #2 ] }
1881-
│ └── PhysicalFilter
1882-
│ ├── cond:And
1883-
│ │ ├── Geq
1884-
│ │ │ ├── #3
1885-
│ │ │ └── 8401
1886-
│ │ └── Lt
1887-
│ │ ├── #3
1888-
│ │ └── 8491
1889-
│ └── PhysicalProjection { exprs: [ #2, #5, #6, #10 ] }
1890-
│ └── PhysicalScan { table: lineitem }
1866+
└── PhysicalHashJoin { join_type: Inner, left_keys: [ #4 ], right_keys: [ #0 ] }
1867+
├── PhysicalProjection { exprs: [ #0, #1, #2, #3, #5 ] }
1868+
│ └── PhysicalHashJoin { join_type: Inner, left_keys: [ #0 ], right_keys: [ #0 ] }
1869+
│ ├── PhysicalProjection { exprs: [ #0, #1, #2, #4 ] }
1870+
│ │ └── PhysicalScan { table: supplier }
1871+
│ └── PhysicalProjection { exprs: [ #0, #1 ] }
1872+
│ └── PhysicalAgg
1873+
│ ├── aggrs:Agg(Sum)
1874+
│ │ └── Mul
1875+
│ │ ├── #1
1876+
│ │ └── Sub
1877+
│ │ ├── 1
1878+
│ │ └── #2
1879+
│ ├── groups: [ #0 ]
1880+
│ └── PhysicalProjection { exprs: [ #0, #1, #2 ] }
1881+
│ └── PhysicalFilter
1882+
│ ├── cond:And
1883+
│ │ ├── Geq
1884+
│ │ │ ├── #3
1885+
│ │ │ └── 8401
1886+
│ │ └── Lt
1887+
│ │ ├── #3
1888+
│ │ └── 8491
1889+
│ └── PhysicalProjection { exprs: [ #2, #5, #6, #10 ] }
1890+
│ └── PhysicalScan { table: lineitem }
1891+
└── PhysicalAgg
1892+
├── aggrs:Agg(Max)
1893+
│ └── [ #0 ]
1894+
├── groups: []
1895+
└── PhysicalProjection { exprs: [ #1 ] }
18911896
└── PhysicalAgg
1892-
├── aggrs:Agg(Max)
1893-
│ └── [ #0 ]
1894-
├── groups: []
1895-
└── PhysicalProjection { exprs: [ #1 ] }
1896-
└── PhysicalAgg
1897-
├── aggrs:Agg(Sum)
1898-
│ └── Mul
1899-
│ ├── #1
1900-
│ └── Sub
1901-
│ ├── 1
1902-
│ └── #2
1903-
├── groups: [ #0 ]
1904-
└── PhysicalProjection { exprs: [ #0, #1, #2 ] }
1905-
└── PhysicalFilter
1906-
├── cond:And
1907-
│ ├── Geq
1908-
│ │ ├── #3
1909-
│ │ └── 8401
1910-
│ └── Lt
1911-
│ ├── #3
1912-
│ └── 8491
1913-
└── PhysicalProjection { exprs: [ #2, #5, #6, #10 ] }
1914-
└── PhysicalScan { table: lineitem }
1897+
├── aggrs:Agg(Sum)
1898+
│ └── Mul
1899+
│ ├── #1
1900+
│ └── Sub
1901+
│ ├── 1
1902+
│ └── #2
1903+
├── groups: [ #0 ]
1904+
└── PhysicalProjection { exprs: [ #0, #1, #2 ] }
1905+
└── PhysicalFilter
1906+
├── cond:And
1907+
│ ├── Geq
1908+
│ │ ├── #3
1909+
│ │ └── 8401
1910+
│ └── Lt
1911+
│ ├── #3
1912+
│ └── 8491
1913+
└── PhysicalProjection { exprs: [ #2, #5, #6, #10 ] }
1914+
└── PhysicalScan { table: lineitem }
19151915
*/
19161916

19171917
-- TPC-H Q17

0 commit comments

Comments
 (0)