MCPcopy Index your code
hub / github.com/TanStack/db / removeRedundantFromClause

Function removeRedundantFromClause

packages/db/src/query/optimizer.ts:471–502  ·  view source on GitHub ↗

* Removes redundant subqueries from a FROM clause. * * @param from - The FROM clause to process * @returns A FROM clause with redundant subqueries removed

(from: From)

Source from the content-addressed store, hash-verified

469 * @returns A FROM clause with redundant subqueries removed
470 */
471function 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
504function removeRedundantJoinFromClause(
505 from: CollectionRefClass | QueryRefClass,

Callers 2

Calls 3

isRedundantSubqueryFunction · 0.85
mapMethod · 0.45

Tested by

no test coverage detected