* Removes redundant subqueries from a FROM clause. * * @param from - The FROM clause to process * @returns A FROM clause with redundant subqueries removed
(from: From)
| 469 | * @returns A FROM clause with redundant subqueries removed |
| 470 | */ |
| 471 | function removeRedundantFromClause(from: From): From { |
| 472 | if (from.type === `unionFrom`) { |
| 473 | return new UnionFromClass( |
| 474 | from.sources.map((source) => removeRedundantFromClause(source) as any), |
| 475 | ) |
| 476 | } |
| 477 | |
| 478 | if (from.type === `unionAll`) { |
| 479 | return new UnionAllClass( |
| 480 | from.queries.map((branch) => removeRedundantSubqueries(branch)), |
| 481 | ) |
| 482 | } |
| 483 | |
| 484 | if (from.type === `collectionRef`) { |
| 485 | return from |
| 486 | } |
| 487 | |
| 488 | const processedQuery = removeRedundantSubqueries(from.query) |
| 489 | |
| 490 | // Check if this subquery is redundant |
| 491 | if (isRedundantSubquery(processedQuery)) { |
| 492 | // Return the inner query's FROM clause with this alias |
| 493 | const innerFrom = removeRedundantFromClause(processedQuery.from) |
| 494 | if (innerFrom.type === `collectionRef`) { |
| 495 | return new CollectionRefClass(innerFrom.collection, from.alias) |
| 496 | } else if (innerFrom.type === `queryRef`) { |
| 497 | return new QueryRefClass(innerFrom.query, from.alias) |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | return new QueryRefClass(processedQuery, from.alias) |
| 502 | } |
| 503 | |
| 504 | function removeRedundantJoinFromClause( |
| 505 | from: CollectionRefClass | QueryRefClass, |
no test coverage detected