Commit 65498bf
fix: OPTIONAL MATCH on denormalized schemas generates CTE + LEFT JOIN
Previously, OPTIONAL MATCH was completely dropped for denormalized schemas
where nodes are virtual (properties embedded in edge table). The Union
standalone node scan from MATCH was distributed into OPTIONAL MATCH
branches by UnionDistribution, losing LEFT JOIN semantics.
Three-layer fix:
1. union_distribution.rs: Skip distributing GraphRel over Union when
the GraphRel is OPTIONAL and the Union is a denormalized standalone
scan. Preserves the Union as an anchor for LEFT JOIN.
2. plan_builder.rs (GraphRel handler): When an OPTIONAL GraphRel has a
Union-of-denormalized-nodes as left, render the Union as a CTE,
use CTE as FROM, and LEFT JOIN the edge table with the correct
from_id/node_id join condition.
3. plan_builder.rs (to_render_plan_with_ctx): Detect the pattern in
GraphJoins-wrapped plans and delegate to the GraphRel handler,
overlaying outer SELECT/GROUP BY/ORDER BY.
Also:
- plan_builder_helpers.rs: is_node_denormalized now handles Union
- plan_optimizer.rs: edge join preserved via from_id_column marker
- synthesize_denorm_optional_join_condition for CartesianProduct path
Generated SQL (still needs column reference refinement in follow-up):
```sql
WITH __denorm_scan_a AS (
SELECT origin_code AS "code" FROM flights
UNION DISTINCT
SELECT dest_code AS "code" FROM flights
)
SELECT a.code, count(*) FROM __denorm_scan_a AS a
LEFT JOIN flights AS r ON a.code = r.origin_code
GROUP BY a.code
```
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 305eb9a commit 65498bf
4 files changed
Lines changed: 443 additions & 25 deletions
File tree
- src
- clickhouse_query_generator
- query_planner/analyzer
- render_plan
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2507 | 2507 | | |
2508 | 2508 | | |
2509 | 2509 | | |
| 2510 | + | |
| 2511 | + | |
| 2512 | + | |
| 2513 | + | |
| 2514 | + | |
| 2515 | + | |
| 2516 | + | |
2510 | 2517 | | |
2511 | 2518 | | |
2512 | 2519 | | |
| |||
2548 | 2555 | | |
2549 | 2556 | | |
2550 | 2557 | | |
| 2558 | + | |
| 2559 | + | |
| 2560 | + | |
| 2561 | + | |
2551 | 2562 | | |
2552 | 2563 | | |
2553 | 2564 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
142 | 146 | | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
153 | 179 | | |
154 | 180 | | |
155 | 181 | | |
| |||
195 | 221 | | |
196 | 222 | | |
197 | 223 | | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
198 | 230 | | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
211 | 242 | | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
212 | 265 | | |
213 | 266 | | |
214 | 267 | | |
| |||
0 commit comments