Skip to content

tyneq


tyneq / QueryPlanOptimizer

Class: QueryPlanOptimizer

Defined in: src/queryplan/QueryPlanOptimizer.ts:59

A built-in QueryPlanTransformer that fuses redundant consecutive operators.

Remarks

This optimizer rewrites the query plan tree only - it does not affect execution of the original sequence. The returned QueryPlanNode reflects what an optimized pipeline would look like; the live sequence that produced the original plan is unchanged.

Fusion is only semantics-preserving for pure, side-effect-free functions. If a predicate or projection has side effects (e.g. logging, mutation), fusing two nodes into one changes when and how many times those effects fire. For example, in a fused where, the second predicate is never called for items that fail the first - any mutation inside the second predicate is skipped for those items. Do not use this optimizer on pipelines with impure predicates or projections.

Fusions applied

PatternResult
where(a) -> where(b)where(x => a(x) && b(x))
select(a) -> select(b)select(x => b(a(x)))

Additional fusions can be added by subclassing and overriding QueryPlanTransformer.transformNode.

Example

ts
import { Tyneq, tyneqQueryNode, QueryPlanOptimizer, QueryPlanPrinter } from "tyneq";

const seq = Tyneq.from([1, 2, 3])
    .where(x => x > 1)
    .where(x => x < 3)
    .select(x => x * 2)
    .select(x => x + 1);

const original = seq[tyneqQueryNode]!;
const optimized = new QueryPlanOptimizer().visit(original);

console.log(QueryPlanPrinter.print(original));
// from([...])
//   -> where(<fn>)
//   -> where(<fn>)
//   -> select(<fn>)
//   -> select(<fn>)

console.log(QueryPlanPrinter.print(optimized));
// from([...])
//   -> where(<fn>)
//   -> select(<fn>)

Extends

Constructors

Constructor

new QueryPlanOptimizer(): QueryPlanOptimizer

Returns

QueryPlanOptimizer

Inherited from

QueryPlanTransformer.constructor

Methods

transformNode()

protected transformNode(node, source): IQueryNode

Defined in: src/queryplan/QueryPlanOptimizer.ts:61

Transforms a single node. Override to intercept specific operators.

Parameters

ParameterTypeDescription
nodeIQueryNodeThe original node (unmodified).
sourceNullable<IQueryNode>The transformed predecessor, or null for source nodes.

Returns

IQueryNode

Remarks

The default implementation reconstructs the node with identical data (identity transform). source is the already-transformed predecessor - use it as the source of any returned node to preserve chain continuity.

Overrides

QueryPlanTransformer.transformNode


visit()

visit(node): IQueryNode

Defined in: src/queryplan/QueryPlanTransformer.ts:46

Transforms the full chain rooted at node and returns the new root node.

Parameters

ParameterType
nodeIQueryNode

Returns

IQueryNode

Remarks

Processes source-first (bottom-up): the source chain is fully transformed before transformNode is called for the current node. This means source passed to transformNode is always already the transformed predecessor.

Inherited from

QueryPlanTransformer.visit