Plan `EXISTS (SELECT 1 FROM tbl WHERE tbl.col = outer.col)` as a semi/anti join. Extracts the correlated column from the subquery's WHERE clause.
(
subquery: &ast::Query,
negated: bool,
catalog: &dyn SqlCatalog,
functions: &FunctionRegistry,
temporal: crate::TemporalScope,
)
| 249 | /// |
| 250 | /// Extracts the correlated column from the subquery's WHERE clause. |
| 251 | fn try_plan_exists_subquery( |
| 252 | subquery: &ast::Query, |
| 253 | negated: bool, |
| 254 | catalog: &dyn SqlCatalog, |
| 255 | functions: &FunctionRegistry, |
| 256 | temporal: crate::TemporalScope, |
| 257 | ) -> Result<Option<SubqueryJoin>> { |
| 258 | let select = match &*subquery.body { |
| 259 | SetExpr::Select(s) => s, |
| 260 | _ => return Ok(None), |
| 261 | }; |
| 262 | |
| 263 | // Look for a correlated predicate in the WHERE: inner.col = outer.col |
| 264 | let (outer_col, inner_col) = match &select.selection { |
| 265 | Some(expr) => match extract_correlated_eq(expr) { |
| 266 | Some(pair) => pair, |
| 267 | None => return Ok(None), |
| 268 | }, |
| 269 | None => return Ok(None), |
| 270 | }; |
| 271 | |
| 272 | // Build a simplified subquery without the correlated predicate for planning. |
| 273 | let inner_plan = super::select::plan_query(subquery, catalog, functions, temporal)?; |
| 274 | |
| 275 | Ok(Some(SubqueryJoin { |
| 276 | outer_column: outer_col, |
| 277 | inner_plan, |
| 278 | inner_column: inner_col, |
| 279 | join_type: if negated { |
| 280 | JoinType::Anti |
| 281 | } else { |
| 282 | JoinType::Semi |
| 283 | }, |
| 284 | })) |
| 285 | } |
| 286 | |
| 287 | /// Extract a correlated equality predicate from a WHERE clause. |
| 288 | /// |
no test coverage detected