@rocicorp/zero 1.10.0-canary.22 → 1.10.0-canary.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/out/analyze-query/src/analyze-cli.js +2 -2
- package/out/analyze-query/src/analyze-cli.js.map +1 -1
- package/out/analyze-query/src/bin-analyze.js +9 -8
- package/out/analyze-query/src/bin-analyze.js.map +1 -1
- package/out/replicache/src/btree/node.d.ts.map +1 -1
- package/out/replicache/src/btree/node.js +3 -6
- package/out/replicache/src/btree/node.js.map +1 -1
- package/out/replicache/src/dag/gc.d.ts.map +1 -1
- package/out/replicache/src/dag/gc.js +6 -10
- package/out/replicache/src/dag/gc.js.map +1 -1
- package/out/replicache/src/kv/sqlite-store.d.ts.map +1 -1
- package/out/replicache/src/kv/sqlite-store.js +24 -2
- package/out/replicache/src/kv/sqlite-store.js.map +1 -1
- package/out/replicache/src/persist/persist.js +1 -1
- package/out/shared/src/arrays.d.ts +5 -0
- package/out/shared/src/arrays.d.ts.map +1 -1
- package/out/shared/src/arrays.js +8 -10
- package/out/shared/src/arrays.js.map +1 -1
- package/out/shared/src/map.d.ts +19 -2
- package/out/shared/src/map.d.ts.map +1 -1
- package/out/shared/src/map.js +18 -4
- package/out/shared/src/map.js.map +1 -1
- package/out/z2s/src/compiler.js +1 -1
- package/out/z2s/src/sql.d.ts.map +1 -1
- package/out/z2s/src/sql.js +2 -2
- package/out/z2s/src/sql.js.map +1 -1
- package/out/zero/package.js +1 -1
- package/out/zero/package.js.map +1 -1
- package/out/zero-cache/src/auth/write-authorizer.d.ts.map +1 -1
- package/out/zero-cache/src/auth/write-authorizer.js +1 -0
- package/out/zero-cache/src/auth/write-authorizer.js.map +1 -1
- package/out/zero-cache/src/config/zero-config.d.ts +23 -0
- package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
- package/out/zero-cache/src/config/zero-config.js +63 -1
- package/out/zero-cache/src/config/zero-config.js.map +1 -1
- package/out/zero-cache/src/db/lite-tables.d.ts.map +1 -1
- package/out/zero-cache/src/db/lite-tables.js +3 -4
- package/out/zero-cache/src/db/lite-tables.js.map +1 -1
- package/out/zero-cache/src/observability/metrics.d.ts.map +1 -1
- package/out/zero-cache/src/observability/metrics.js +2 -7
- package/out/zero-cache/src/observability/metrics.js.map +1 -1
- package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
- package/out/zero-cache/src/server/change-streamer.js +11 -3
- package/out/zero-cache/src/server/change-streamer.js.map +1 -1
- package/out/zero-cache/src/server/inspector-delegate.d.ts +18 -2
- package/out/zero-cache/src/server/inspector-delegate.d.ts.map +1 -1
- package/out/zero-cache/src/server/inspector-delegate.js +22 -15
- package/out/zero-cache/src/server/inspector-delegate.js.map +1 -1
- package/out/zero-cache/src/server/main.d.ts.map +1 -1
- package/out/zero-cache/src/server/main.js +8 -1
- package/out/zero-cache/src/server/main.js.map +1 -1
- package/out/zero-cache/src/server/mutator.js +2 -0
- package/out/zero-cache/src/server/mutator.js.map +1 -1
- package/out/zero-cache/src/server/reaper.d.ts.map +1 -1
- package/out/zero-cache/src/server/reaper.js +2 -0
- package/out/zero-cache/src/server/reaper.js.map +1 -1
- package/out/zero-cache/src/server/replicator.d.ts.map +1 -1
- package/out/zero-cache/src/server/replicator.js +2 -0
- package/out/zero-cache/src/server/replicator.js.map +1 -1
- package/out/zero-cache/src/server/runner/zero-dispatcher.d.ts.map +1 -1
- package/out/zero-cache/src/server/runner/zero-dispatcher.js +3 -0
- package/out/zero-cache/src/server/runner/zero-dispatcher.js.map +1 -1
- package/out/zero-cache/src/server/syncer.d.ts.map +1 -1
- package/out/zero-cache/src/server/syncer.js +5 -1
- package/out/zero-cache/src/server/syncer.js.map +1 -1
- package/out/zero-cache/src/services/analyze.d.ts.map +1 -1
- package/out/zero-cache/src/services/analyze.js +11 -9
- package/out/zero-cache/src/services/analyze.js.map +1 -1
- package/out/zero-cache/src/services/change-source/common/backfill-manager.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/common/backfill-manager.js +14 -1
- package/out/zero-cache/src/services/change-source/common/backfill-manager.js.map +1 -1
- package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js +15 -3
- package/out/zero-cache/src/services/change-source/common/change-stream-multiplexer.js.map +1 -1
- package/out/zero-cache/src/services/change-source/custom/change-source.js +2 -2
- package/out/zero-cache/src/services/change-source/custom/change-source.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/initial-sync.js +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts +3 -0
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +6 -4
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
- package/out/zero-cache/src/services/mutagen/mutagen.d.ts +5 -0
- package/out/zero-cache/src/services/mutagen/mutagen.d.ts.map +1 -1
- package/out/zero-cache/src/services/mutagen/mutagen.js +7 -1
- package/out/zero-cache/src/services/mutagen/mutagen.js.map +1 -1
- package/out/zero-cache/src/services/mutagen/pusher.d.ts +32 -32
- package/out/zero-cache/src/services/mutagen/pusher.d.ts.map +1 -1
- package/out/zero-cache/src/services/mutagen/pusher.js +5 -6
- package/out/zero-cache/src/services/mutagen/pusher.js.map +1 -1
- package/out/zero-cache/src/services/profz.d.ts +7 -0
- package/out/zero-cache/src/services/profz.d.ts.map +1 -0
- package/out/zero-cache/src/services/profz.js +103 -0
- package/out/zero-cache/src/services/profz.js.map +1 -0
- package/out/zero-cache/src/services/run-ast.d.ts +12 -0
- package/out/zero-cache/src/services/run-ast.d.ts.map +1 -1
- package/out/zero-cache/src/services/run-ast.js +26 -14
- package/out/zero-cache/src/services/run-ast.js.map +1 -1
- package/out/zero-cache/src/services/runner.d.ts.map +1 -1
- package/out/zero-cache/src/services/runner.js +1 -1
- package/out/zero-cache/src/services/runner.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/cvr.d.ts +45 -0
- package/out/zero-cache/src/services/view-syncer/cvr.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/cvr.js +133 -1
- package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/drain-coordinator.d.ts +14 -1
- package/out/zero-cache/src/services/view-syncer/drain-coordinator.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/drain-coordinator.js +30 -4
- package/out/zero-cache/src/services/view-syncer/drain-coordinator.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/hydration-budget.d.ts +33 -0
- package/out/zero-cache/src/services/view-syncer/hydration-budget.d.ts.map +1 -0
- package/out/zero-cache/src/services/view-syncer/hydration-budget.js +62 -0
- package/out/zero-cache/src/services/view-syncer/hydration-budget.js.map +1 -0
- package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.d.ts +51 -0
- package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.d.ts.map +1 -0
- package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.js +78 -0
- package/out/zero-cache/src/services/view-syncer/hydration-circuit-breaker.js.map +1 -0
- package/out/zero-cache/src/services/view-syncer/inspect-handler.d.ts +1 -1
- package/out/zero-cache/src/services/view-syncer/inspect-handler.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/inspect-handler.js +2 -2
- package/out/zero-cache/src/services/view-syncer/inspect-handler.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts +2 -1
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +42 -17
- package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/query-covering.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/query-covering.js +2 -6
- package/out/zero-cache/src/services/view-syncer/query-covering.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/snapshot-row-cache.d.ts +95 -0
- package/out/zero-cache/src/services/view-syncer/snapshot-row-cache.d.ts.map +1 -0
- package/out/zero-cache/src/services/view-syncer/snapshot-row-cache.js +155 -0
- package/out/zero-cache/src/services/view-syncer/snapshot-row-cache.js.map +1 -0
- package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts +42 -4
- package/out/zero-cache/src/services/view-syncer/snapshotter.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/snapshotter.js +117 -28
- package/out/zero-cache/src/services/view-syncer/snapshotter.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts +11 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.js +466 -95
- package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
- package/out/zero-cache/src/types/processes.d.ts +32 -0
- package/out/zero-cache/src/types/processes.d.ts.map +1 -1
- package/out/zero-cache/src/types/processes.js +43 -4
- package/out/zero-cache/src/types/processes.js.map +1 -1
- package/out/zero-cache/src/types/profiler.d.ts +12 -0
- package/out/zero-cache/src/types/profiler.d.ts.map +1 -1
- package/out/zero-cache/src/types/profiler.js +46 -1
- package/out/zero-cache/src/types/profiler.js.map +1 -1
- package/out/zero-cache/src/types/row-key.d.ts.map +1 -1
- package/out/zero-cache/src/types/row-key.js +5 -5
- package/out/zero-cache/src/types/row-key.js.map +1 -1
- package/out/zero-cache/src/workers/connection.js +3 -3
- package/out/zero-cache/src/workers/connection.js.map +1 -1
- package/out/zero-cache/src/workers/syncer-ws-message-handler.d.ts.map +1 -1
- package/out/zero-cache/src/workers/syncer-ws-message-handler.js +21 -16
- package/out/zero-cache/src/workers/syncer-ws-message-handler.js.map +1 -1
- package/out/zero-cache/src/workers/syncer.d.ts.map +1 -1
- package/out/zero-cache/src/workers/syncer.js +10 -1
- package/out/zero-cache/src/workers/syncer.js.map +1 -1
- package/out/zero-client/src/client/context.d.ts +16 -3
- package/out/zero-client/src/client/context.d.ts.map +1 -1
- package/out/zero-client/src/client/context.js +85 -14
- package/out/zero-client/src/client/context.js.map +1 -1
- package/out/zero-client/src/client/ivm-branch.d.ts.map +1 -1
- package/out/zero-client/src/client/ivm-branch.js +20 -3
- package/out/zero-client/src/client/ivm-branch.js.map +1 -1
- package/out/zero-client/src/client/query-manager.d.ts.map +1 -1
- package/out/zero-client/src/client/query-manager.js +2 -5
- package/out/zero-client/src/client/query-manager.js.map +1 -1
- package/out/zero-client/src/client/version.js +1 -1
- package/out/zero-client/src/client/zero-rep.d.ts.map +1 -1
- package/out/zero-client/src/client/zero-rep.js +1 -1
- package/out/zero-client/src/client/zero-rep.js.map +1 -1
- package/out/zero-client/src/client/zero.d.ts.map +1 -1
- package/out/zero-client/src/client/zero.js +2 -2
- package/out/zero-client/src/client/zero.js.map +1 -1
- package/out/zero-protocol/src/analyze-query-result.d.ts +5 -5
- package/out/zero-protocol/src/analyze-query-result.d.ts.map +1 -1
- package/out/zero-protocol/src/analyze-query-result.js +1 -1
- package/out/zero-protocol/src/analyze-query-result.js.map +1 -1
- package/out/zero-protocol/src/ast.d.ts.map +1 -1
- package/out/zero-protocol/src/ast.js +13 -14
- package/out/zero-protocol/src/ast.js.map +1 -1
- package/out/zero-protocol/src/down.d.ts +2 -2
- package/out/zero-protocol/src/error.d.ts +1 -1
- package/out/zero-protocol/src/inspect-down.d.ts +6 -6
- package/out/zero-protocol/src/protocol-version.d.ts.map +1 -1
- package/out/zero-protocol/src/protocol-version.js.map +1 -1
- package/out/zero-protocol/src/query-hash.d.ts.map +1 -1
- package/out/zero-protocol/src/query-hash.js +2 -5
- package/out/zero-protocol/src/query-hash.js.map +1 -1
- package/out/zero-solid/src/solid-view.d.ts +7 -0
- package/out/zero-solid/src/solid-view.d.ts.map +1 -1
- package/out/zero-solid/src/solid-view.js +13 -2
- package/out/zero-solid/src/solid-view.js.map +1 -1
- package/out/zql/src/builder/builder.d.ts +19 -0
- package/out/zql/src/builder/builder.d.ts.map +1 -1
- package/out/zql/src/builder/builder.js +28 -11
- package/out/zql/src/builder/builder.js.map +1 -1
- package/out/zql/src/builder/correlated-predicate-pushdown.d.ts +43 -0
- package/out/zql/src/builder/correlated-predicate-pushdown.d.ts.map +1 -0
- package/out/zql/src/builder/correlated-predicate-pushdown.js +161 -0
- package/out/zql/src/builder/correlated-predicate-pushdown.js.map +1 -0
- package/out/zql/src/builder/debug-delegate.d.ts +6 -1
- package/out/zql/src/builder/debug-delegate.d.ts.map +1 -1
- package/out/zql/src/builder/debug-delegate.js +14 -2
- package/out/zql/src/builder/debug-delegate.js.map +1 -1
- package/out/zql/src/ivm/array-view.d.ts +19 -0
- package/out/zql/src/ivm/array-view.d.ts.map +1 -1
- package/out/zql/src/ivm/array-view.js +39 -2
- package/out/zql/src/ivm/array-view.js.map +1 -1
- package/out/zql/src/ivm/connection-index.d.ts +68 -0
- package/out/zql/src/ivm/connection-index.d.ts.map +1 -0
- package/out/zql/src/ivm/connection-index.js +149 -0
- package/out/zql/src/ivm/connection-index.js.map +1 -0
- package/out/zql/src/ivm/exists.js +1 -1
- package/out/zql/src/ivm/filter-operators.d.ts +3 -2
- package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
- package/out/zql/src/ivm/filter-operators.js +19 -4
- package/out/zql/src/ivm/filter-operators.js.map +1 -1
- package/out/zql/src/ivm/memory-source.d.ts +13 -1
- package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
- package/out/zql/src/ivm/memory-source.js +43 -4
- package/out/zql/src/ivm/memory-source.js.map +1 -1
- package/out/zql/src/ivm/operator.d.ts +40 -0
- package/out/zql/src/ivm/operator.d.ts.map +1 -1
- package/out/zql/src/ivm/operator.js.map +1 -1
- package/out/zql/src/ivm/view.d.ts +15 -0
- package/out/zql/src/ivm/view.d.ts.map +1 -1
- package/out/zql/src/planner/planner-builder.d.ts +12 -3
- package/out/zql/src/planner/planner-builder.d.ts.map +1 -1
- package/out/zql/src/planner/planner-builder.js +40 -20
- package/out/zql/src/planner/planner-builder.js.map +1 -1
- package/out/zql/src/planner/planner-connection.d.ts +28 -2
- package/out/zql/src/planner/planner-connection.d.ts.map +1 -1
- package/out/zql/src/planner/planner-connection.js +108 -8
- package/out/zql/src/planner/planner-connection.js.map +1 -1
- package/out/zql/src/planner/planner-debug.d.ts +1 -1
- package/out/zql/src/planner/planner-debug.d.ts.map +1 -1
- package/out/zql/src/planner/planner-debug.js +3 -8
- package/out/zql/src/planner/planner-debug.js.map +1 -1
- package/out/zql/src/planner/planner-fan-out.d.ts +1 -0
- package/out/zql/src/planner/planner-fan-out.d.ts.map +1 -1
- package/out/zql/src/planner/planner-fan-out.js +3 -0
- package/out/zql/src/planner/planner-fan-out.js.map +1 -1
- package/out/zql/src/planner/planner-filter.d.ts +59 -0
- package/out/zql/src/planner/planner-filter.d.ts.map +1 -0
- package/out/zql/src/planner/planner-filter.js +116 -0
- package/out/zql/src/planner/planner-filter.js.map +1 -0
- package/out/zql/src/planner/planner-graph.d.ts +2 -0
- package/out/zql/src/planner/planner-graph.d.ts.map +1 -1
- package/out/zql/src/planner/planner-graph.js +2 -0
- package/out/zql/src/planner/planner-graph.js.map +1 -1
- package/out/zql/src/planner/planner-join.d.ts +2 -0
- package/out/zql/src/planner/planner-join.d.ts.map +1 -1
- package/out/zql/src/planner/planner-join.js +7 -0
- package/out/zql/src/planner/planner-join.js.map +1 -1
- package/out/zql/src/planner/planner-node.d.ts +2 -1
- package/out/zql/src/planner/planner-node.d.ts.map +1 -1
- package/out/zql/src/planner/planner-node.js.map +1 -1
- package/out/zql/src/planner/planner-source.d.ts +2 -2
- package/out/zql/src/planner/planner-source.d.ts.map +1 -1
- package/out/zql/src/planner/planner-source.js +2 -2
- package/out/zql/src/planner/planner-source.js.map +1 -1
- package/out/zql/src/query/query-delegate-base.d.ts +2 -2
- package/out/zql/src/query/query-delegate-base.d.ts.map +1 -1
- package/out/zql/src/query/query-delegate-base.js +16 -10
- package/out/zql/src/query/query-delegate-base.js.map +1 -1
- package/out/zql/src/query/query-delegate.d.ts +22 -5
- package/out/zql/src/query/query-delegate.d.ts.map +1 -1
- package/out/zql/src/query/query-impl.d.ts.map +1 -1
- package/out/zql/src/query/query-impl.js +6 -23
- package/out/zql/src/query/query-impl.js.map +1 -1
- package/out/zql/src/query/query-transitions.d.ts.map +1 -1
- package/out/zql/src/query/query-transitions.js +2 -5
- package/out/zql/src/query/query-transitions.js.map +1 -1
- package/out/zql/src/query/query.d.ts +4 -1
- package/out/zql/src/query/query.d.ts.map +1 -1
- package/out/zql/src/query/runnable-query-impl.d.ts.map +1 -1
- package/out/zql/src/query/runnable-query-impl.js +2 -10
- package/out/zql/src/query/runnable-query-impl.js.map +1 -1
- package/out/zqlite/src/db.d.ts +9 -1
- package/out/zqlite/src/db.d.ts.map +1 -1
- package/out/zqlite/src/db.js +8 -2
- package/out/zqlite/src/db.js.map +1 -1
- package/out/zqlite/src/query-builder.d.ts +1 -1
- package/out/zqlite/src/query-builder.d.ts.map +1 -1
- package/out/zqlite/src/query-builder.js +2 -1
- package/out/zqlite/src/query-builder.js.map +1 -1
- package/out/zqlite/src/query-delegate.d.ts.map +1 -1
- package/out/zqlite/src/query-delegate.js +5 -6
- package/out/zqlite/src/query-delegate.js.map +1 -1
- package/out/zqlite/src/sqlite-cost-model.d.ts.map +1 -1
- package/out/zqlite/src/sqlite-cost-model.js +2 -30
- package/out/zqlite/src/sqlite-cost-model.js.map +1 -1
- package/out/zqlite/src/sqlite-stat-fanout.d.ts.map +1 -1
- package/out/zqlite/src/sqlite-stat-fanout.js +3 -5
- package/out/zqlite/src/sqlite-stat-fanout.js.map +1 -1
- package/out/zqlite/src/table-source.d.ts +15 -2
- package/out/zqlite/src/table-source.d.ts.map +1 -1
- package/out/zqlite/src/table-source.js +28 -12
- package/out/zqlite/src/table-source.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { assert } from "../../../shared/src/asserts.js";
|
|
2
2
|
import { must } from "../../../shared/src/must.js";
|
|
3
3
|
import { planIdSymbol } from "../../../zero-protocol/src/ast.js";
|
|
4
|
+
import { transformFilters } from "../builder/filter.js";
|
|
4
5
|
import { PlannerFanIn } from "./planner-fan-in.js";
|
|
5
6
|
import { PlannerFanOut } from "./planner-fan-out.js";
|
|
7
|
+
import { PlannerFilter } from "./planner-filter.js";
|
|
6
8
|
import { PlannerGraph } from "./planner-graph.js";
|
|
7
9
|
import { PlannerJoin } from "./planner-join.js";
|
|
8
10
|
import { PlannerTerminus } from "./planner-terminus.js";
|
|
@@ -12,6 +14,7 @@ function wireOutput(from, to) {
|
|
|
12
14
|
case "connection":
|
|
13
15
|
case "join":
|
|
14
16
|
case "fan-in":
|
|
17
|
+
case "filter":
|
|
15
18
|
from.setOutput(to);
|
|
16
19
|
break;
|
|
17
20
|
case "fan-out":
|
|
@@ -20,13 +23,17 @@ function wireOutput(from, to) {
|
|
|
20
23
|
case "terminus": assert(false, "Terminus nodes cannot have outputs");
|
|
21
24
|
}
|
|
22
25
|
}
|
|
23
|
-
|
|
26
|
+
/**
|
|
27
|
+
* @param pushed The conditions that correlated predicate pushdown copied into
|
|
28
|
+
* a child (see `pushDownCorrelatedPredicates`).
|
|
29
|
+
*/
|
|
30
|
+
function buildPlanGraph(ast, model, isRoot, baseConstraints, pushed) {
|
|
24
31
|
const graph = new PlannerGraph();
|
|
25
32
|
let nextPlanId = 0;
|
|
26
|
-
const connection = graph.addSource(ast.table, model).connect(ast.orderBy ?? [], ast.where, isRoot, baseConstraints, ast.limit);
|
|
33
|
+
const connection = graph.addSource(ast.table, model).connect(ast.orderBy ?? [], ast.where, isRoot, baseConstraints, ast.limit, pushed);
|
|
27
34
|
graph.connections.push(connection);
|
|
28
35
|
let end = connection;
|
|
29
|
-
if (ast.where) end = processCondition(ast.where, end, graph, model, ast.table, () => nextPlanId
|
|
36
|
+
if (ast.where) end = processCondition(ast.where, end, graph, model, ast.table, () => nextPlanId++, pushed);
|
|
30
37
|
const terminus = new PlannerTerminus(end);
|
|
31
38
|
wireOutput(end, terminus);
|
|
32
39
|
graph.setTerminus(terminus);
|
|
@@ -34,35 +41,42 @@ function buildPlanGraph(ast, model, isRoot, baseConstraints) {
|
|
|
34
41
|
if (ast.related) for (const csq of ast.related) {
|
|
35
42
|
const alias = must(csq.subquery.alias, "Related subquery must have alias");
|
|
36
43
|
const childConstraints = extractConstraint(csq.correlation.childField, csq.subquery.table);
|
|
37
|
-
subPlans[alias] = buildPlanGraph(csq.subquery, model, true, childConstraints);
|
|
44
|
+
subPlans[alias] = buildPlanGraph(csq.subquery, model, true, childConstraints, pushed);
|
|
38
45
|
}
|
|
39
46
|
return {
|
|
40
47
|
plan: graph,
|
|
41
48
|
subPlans
|
|
42
49
|
};
|
|
43
50
|
}
|
|
44
|
-
function processCondition(condition, input, graph, model, parentTable, getPlanId) {
|
|
51
|
+
function processCondition(condition, input, graph, model, parentTable, getPlanId, pushed) {
|
|
45
52
|
switch (condition.type) {
|
|
46
53
|
case "simple": return input;
|
|
47
|
-
case "and": return processAnd(condition, input, graph, model, parentTable, getPlanId);
|
|
48
|
-
case "or": return processOr(condition, input, graph, model, parentTable, getPlanId);
|
|
49
|
-
case "correlatedSubquery": return processCorrelatedSubquery(condition, input, graph, model, parentTable, getPlanId);
|
|
54
|
+
case "and": return processAnd(condition, input, graph, model, parentTable, getPlanId, pushed);
|
|
55
|
+
case "or": return processOr(condition, input, graph, model, parentTable, getPlanId, pushed);
|
|
56
|
+
case "correlatedSubquery": return processCorrelatedSubquery(condition, input, graph, model, parentTable, getPlanId, pushed);
|
|
50
57
|
}
|
|
51
58
|
}
|
|
52
|
-
function processAnd(condition, input, graph, model, parentTable, getPlanId) {
|
|
59
|
+
function processAnd(condition, input, graph, model, parentTable, getPlanId, pushed) {
|
|
53
60
|
let end = input;
|
|
54
|
-
for (const subCondition of condition.conditions) end = processCondition(subCondition, end, graph, model, parentTable, getPlanId);
|
|
61
|
+
for (const subCondition of condition.conditions) end = processCondition(subCondition, end, graph, model, parentTable, getPlanId, pushed);
|
|
55
62
|
return end;
|
|
56
63
|
}
|
|
57
|
-
function processOr(condition, input, graph, model, parentTable, getPlanId) {
|
|
58
|
-
|
|
59
|
-
if (subqueryConditions.length === 0) return input;
|
|
64
|
+
function processOr(condition, input, graph, model, parentTable, getPlanId, pushed) {
|
|
65
|
+
if (!condition.conditions.some((c) => c.type === "correlatedSubquery" || hasCorrelatedSubquery(c))) return input;
|
|
60
66
|
const fanOut = new PlannerFanOut(input);
|
|
61
67
|
graph.fanOuts.push(fanOut);
|
|
62
68
|
wireOutput(input, fanOut);
|
|
63
69
|
const branches = [];
|
|
64
|
-
for (const subCondition of
|
|
65
|
-
|
|
70
|
+
for (const subCondition of condition.conditions) {
|
|
71
|
+
let branch;
|
|
72
|
+
if (subCondition.type === "correlatedSubquery" || hasCorrelatedSubquery(subCondition)) branch = processCondition(subCondition, fanOut, graph, model, parentTable, getPlanId, pushed);
|
|
73
|
+
else {
|
|
74
|
+
const transformed = transformFilters(subCondition).filters;
|
|
75
|
+
const filter = new PlannerFilter(fanOut, transformed);
|
|
76
|
+
graph.filters.push(filter);
|
|
77
|
+
wireOutput(fanOut, filter);
|
|
78
|
+
branch = filter;
|
|
79
|
+
}
|
|
66
80
|
branches.push(branch);
|
|
67
81
|
fanOut.addOutput(branch);
|
|
68
82
|
}
|
|
@@ -71,13 +85,13 @@ function processOr(condition, input, graph, model, parentTable, getPlanId) {
|
|
|
71
85
|
for (const branch of branches) wireOutput(branch, fanIn);
|
|
72
86
|
return fanIn;
|
|
73
87
|
}
|
|
74
|
-
function processCorrelatedSubquery(condition, input, graph, model, parentTable, getPlanId) {
|
|
88
|
+
function processCorrelatedSubquery(condition, input, graph, model, parentTable, getPlanId, pushed) {
|
|
75
89
|
const { related } = condition;
|
|
76
90
|
const childTable = related.subquery.table;
|
|
77
|
-
const childConnection = (graph.hasSource(childTable) ? graph.getSource(childTable) : graph.addSource(childTable, model)).connect(related.subquery.orderBy ?? [], related.subquery.where, false, void 0, condition.op === "EXISTS" ? 1 : void 0);
|
|
91
|
+
const childConnection = (graph.hasSource(childTable) ? graph.getSource(childTable) : graph.addSource(childTable, model)).connect(related.subquery.orderBy ?? [], related.subquery.where, false, void 0, condition.op === "EXISTS" ? 1 : void 0, pushed);
|
|
78
92
|
graph.connections.push(childConnection);
|
|
79
93
|
let childEnd = childConnection;
|
|
80
|
-
if (related.subquery.where) childEnd = processCondition(related.subquery.where, childEnd, graph, model, childTable, getPlanId);
|
|
94
|
+
if (related.subquery.where) childEnd = processCondition(related.subquery.where, childEnd, graph, model, childTable, getPlanId, pushed);
|
|
81
95
|
const parentConstraint = extractConstraint(related.correlation.parentField, parentTable);
|
|
82
96
|
const childConstraint = extractConstraint(related.correlation.childField, childTable);
|
|
83
97
|
const planId = getPlanId();
|
|
@@ -101,6 +115,7 @@ function processCorrelatedSubquery(condition, input, graph, model, parentTable,
|
|
|
101
115
|
}
|
|
102
116
|
const join = new PlannerJoin(input, childEnd, parentConstraint, childConstraint, flippable, planId, initialType);
|
|
103
117
|
graph.joins.push(join);
|
|
118
|
+
childConnection.setParentJoin(join);
|
|
104
119
|
wireOutput(input, join);
|
|
105
120
|
wireOutput(childEnd, join);
|
|
106
121
|
return join;
|
|
@@ -117,8 +132,13 @@ function planRecursively(plans, planDebugger, lc) {
|
|
|
117
132
|
for (const subPlan of Object.values(plans.subPlans)) planRecursively(subPlan, planDebugger, lc);
|
|
118
133
|
plans.plan.plan(planDebugger, lc);
|
|
119
134
|
}
|
|
120
|
-
|
|
121
|
-
|
|
135
|
+
/**
|
|
136
|
+
* @param pushed The conditions that correlated predicate pushdown copied into
|
|
137
|
+
* a child (see `pushDownCorrelatedPredicates`). The planner counts each one
|
|
138
|
+
* only where it removes rows.
|
|
139
|
+
*/
|
|
140
|
+
function planQuery(ast, model, planDebugger, lc, pushed) {
|
|
141
|
+
const plans = buildPlanGraph(ast, model, true, void 0, pushed);
|
|
122
142
|
planRecursively(plans, planDebugger, lc);
|
|
123
143
|
return applyPlansToAST(ast, plans);
|
|
124
144
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"planner-builder.js","names":[],"sources":["../../../../../zql/src/planner/planner-builder.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport type {\n AST,\n Condition,\n Conjunction,\n CorrelatedSubqueryCondition,\n Disjunction,\n} from '../../../zero-protocol/src/ast.ts';\nimport {planIdSymbol} from '../../../zero-protocol/src/ast.ts';\nimport type {ConnectionCostModel} from './planner-connection.ts';\nimport type {PlannerConstraint} from './planner-constraint.ts';\nimport type {PlanDebugger} from './planner-debug.ts';\nimport {PlannerFanIn} from './planner-fan-in.ts';\nimport {PlannerFanOut} from './planner-fan-out.ts';\nimport {PlannerGraph} from './planner-graph.ts';\nimport {PlannerJoin} from './planner-join.ts';\nimport type {PlannerNode} from './planner-node.ts';\nimport {PlannerTerminus} from './planner-terminus.ts';\n\nfunction wireOutput(from: PlannerNode, to: PlannerNode): void {\n switch (from.kind) {\n case 'connection':\n case 'join':\n case 'fan-in':\n from.setOutput(to);\n break;\n case 'fan-out':\n from.addOutput(to);\n break;\n case 'terminus':\n assert(false, 'Terminus nodes cannot have outputs');\n }\n}\n\nexport type Plans = {\n plan: PlannerGraph;\n subPlans: {[key: string]: Plans};\n};\n\nexport function buildPlanGraph(\n ast: AST,\n model: ConnectionCostModel,\n isRoot: boolean,\n baseConstraints?: PlannerConstraint,\n): Plans {\n const graph = new PlannerGraph();\n let nextPlanId = 0;\n\n const source = graph.addSource(ast.table, model);\n const connection = source.connect(\n ast.orderBy ?? [],\n ast.where,\n isRoot,\n baseConstraints,\n ast.limit,\n );\n graph.connections.push(connection);\n\n let end: PlannerNode = connection;\n if (ast.where) {\n end = processCondition(\n ast.where,\n end,\n graph,\n model,\n ast.table,\n () => nextPlanId++,\n );\n }\n\n const terminus = new PlannerTerminus(end);\n wireOutput(end, terminus);\n graph.setTerminus(terminus);\n\n const subPlans: {[key: string]: Plans} = {};\n if (ast.related) {\n for (const csq of ast.related) {\n const alias = must(\n csq.subquery.alias,\n 'Related subquery must have alias',\n );\n const childConstraints = extractConstraint(\n csq.correlation.childField,\n csq.subquery.table,\n );\n subPlans[alias] = buildPlanGraph(\n csq.subquery,\n model,\n true,\n childConstraints,\n );\n }\n }\n\n return {plan: graph, subPlans};\n}\n\nfunction processCondition(\n condition: Condition,\n input: Exclude<PlannerNode, PlannerTerminus>,\n graph: PlannerGraph,\n model: ConnectionCostModel,\n parentTable: string,\n getPlanId: () => number,\n): Exclude<PlannerNode, PlannerTerminus> {\n switch (condition.type) {\n case 'simple':\n return input;\n case 'and':\n return processAnd(condition, input, graph, model, parentTable, getPlanId);\n case 'or':\n return processOr(condition, input, graph, model, parentTable, getPlanId);\n case 'correlatedSubquery':\n return processCorrelatedSubquery(\n condition,\n input,\n graph,\n model,\n parentTable,\n getPlanId,\n );\n }\n}\n\nfunction processAnd(\n condition: Conjunction,\n input: Exclude<PlannerNode, PlannerTerminus>,\n graph: PlannerGraph,\n model: ConnectionCostModel,\n parentTable: string,\n getPlanId: () => number,\n): Exclude<PlannerNode, PlannerTerminus> {\n let end = input;\n for (const subCondition of condition.conditions) {\n end = processCondition(\n subCondition,\n end,\n graph,\n model,\n parentTable,\n getPlanId,\n );\n }\n return end;\n}\n\nfunction processOr(\n condition: Disjunction,\n input: Exclude<PlannerNode, PlannerTerminus>,\n graph: PlannerGraph,\n model: ConnectionCostModel,\n parentTable: string,\n getPlanId: () => number,\n): Exclude<PlannerNode, PlannerTerminus> {\n const subqueryConditions = condition.conditions.filter(\n c => c.type === 'correlatedSubquery' || hasCorrelatedSubquery(c),\n );\n\n if (subqueryConditions.length === 0) {\n return input;\n }\n\n const fanOut = new PlannerFanOut(input);\n graph.fanOuts.push(fanOut);\n wireOutput(input, fanOut);\n\n const branches: Exclude<PlannerNode, PlannerTerminus>[] = [];\n for (const subCondition of subqueryConditions) {\n const branch = processCondition(\n subCondition,\n fanOut,\n graph,\n model,\n parentTable,\n getPlanId,\n );\n branches.push(branch);\n fanOut.addOutput(branch);\n }\n\n const fanIn = new PlannerFanIn(branches);\n graph.fanIns.push(fanIn);\n for (const branch of branches) {\n wireOutput(branch, fanIn);\n }\n\n return fanIn;\n}\n\nfunction processCorrelatedSubquery(\n condition: CorrelatedSubqueryCondition,\n input: Exclude<PlannerNode, PlannerTerminus>,\n graph: PlannerGraph,\n model: ConnectionCostModel,\n parentTable: string,\n getPlanId: () => number,\n): Exclude<PlannerNode, PlannerTerminus> {\n const {related} = condition;\n const childTable = related.subquery.table;\n\n const childSource = graph.hasSource(childTable)\n ? graph.getSource(childTable)\n : graph.addSource(childTable, model);\n\n const childConnection = childSource.connect(\n related.subquery.orderBy ?? [],\n related.subquery.where,\n false,\n undefined, // no base constraints for EXISTS/NOT EXISTS\n condition.op === 'EXISTS' ? 1 : undefined,\n );\n graph.connections.push(childConnection);\n\n let childEnd: PlannerNode = childConnection;\n if (related.subquery.where) {\n childEnd = processCondition(\n related.subquery.where,\n childEnd,\n graph,\n model,\n childTable,\n getPlanId,\n );\n }\n\n const parentConstraint = extractConstraint(\n related.correlation.parentField,\n parentTable,\n );\n const childConstraint = extractConstraint(\n related.correlation.childField,\n childTable,\n );\n\n const planId = getPlanId();\n condition[planIdSymbol] = planId;\n\n // Determine flippability and initial type based on flip flag and operator\n const isNotExists = condition.op === 'NOT EXISTS';\n const manualFlip = condition.flip;\n\n let flippable: boolean;\n let initialType: 'semi' | 'flipped';\n\n if (isNotExists) {\n // NOT EXISTS joins can never be flipped\n flippable = false;\n initialType = 'semi';\n } else if (manualFlip === true) {\n // User explicitly requested flip=true: start flipped, don't allow planner to change\n flippable = false;\n initialType = 'flipped';\n } else if (manualFlip === false) {\n // User explicitly requested flip=false: start semi, don't allow planner to change\n flippable = false;\n initialType = 'semi';\n } else {\n // flip is undefined: planner can decide\n flippable = true;\n initialType = 'semi';\n }\n\n const join = new PlannerJoin(\n input,\n childEnd,\n parentConstraint,\n childConstraint,\n flippable,\n planId,\n initialType,\n );\n graph.joins.push(join);\n\n wireOutput(input, join);\n wireOutput(childEnd, join);\n\n return join;\n}\n\nfunction hasCorrelatedSubquery(condition: Condition): boolean {\n if (condition.type === 'correlatedSubquery') {\n return true;\n }\n if (condition.type === 'and' || condition.type === 'or') {\n return condition.conditions.some(hasCorrelatedSubquery);\n }\n // simple conditions don't contain correlated subqueries\n return false;\n}\n\nfunction extractConstraint(\n fields: readonly string[],\n _tableName: string,\n): PlannerConstraint {\n return Object.fromEntries(fields.map(field => [field, undefined]));\n}\n\nfunction planRecursively(\n plans: Plans,\n planDebugger?: PlanDebugger,\n lc?: LogContext,\n): void {\n for (const subPlan of Object.values(plans.subPlans)) {\n planRecursively(subPlan, planDebugger, lc);\n }\n plans.plan.plan(planDebugger, lc);\n}\n\nexport function planQuery(\n ast: AST,\n model: ConnectionCostModel,\n planDebugger?: PlanDebugger,\n lc?: LogContext,\n): AST {\n const plans = buildPlanGraph(ast, model, true);\n planRecursively(plans, planDebugger, lc);\n return applyPlansToAST(ast, plans);\n}\n\nfunction applyToCondition(\n condition: Condition,\n flippedIds: Set<number>,\n): Condition {\n if (condition.type === 'simple') {\n return condition;\n }\n\n if (condition.type === 'correlatedSubquery') {\n const planId = (condition as unknown as Record<symbol, number>)[\n planIdSymbol\n ];\n const shouldFlip = planId !== undefined && flippedIds.has(planId);\n\n return {\n ...condition,\n flip: shouldFlip,\n related: {\n ...condition.related,\n subquery: {\n ...condition.related.subquery,\n where: condition.related.subquery.where\n ? applyToCondition(condition.related.subquery.where, flippedIds)\n : undefined,\n },\n },\n };\n }\n\n return {\n ...condition,\n conditions: condition.conditions.map(c => applyToCondition(c, flippedIds)),\n };\n}\n\nexport function applyPlansToAST(ast: AST, plans: Plans): AST {\n const flippedIds = new Set<number>();\n for (const join of plans.plan.joins) {\n if (join.type === 'flipped' && join.planId !== undefined) {\n flippedIds.add(join.planId);\n }\n }\n\n return {\n ...ast,\n where: ast.where ? applyToCondition(ast.where, flippedIds) : undefined,\n related: ast.related?.map(csq => {\n const alias = must(\n csq.subquery.alias,\n 'Related subquery must have alias',\n );\n const subPlan = plans.subPlans[alias];\n return {\n ...csq,\n subquery: subPlan\n ? applyPlansToAST(csq.subquery, subPlan)\n : csq.subquery,\n };\n }),\n };\n}\n"],"mappings":";;;;;;;;;AAqBA,SAAS,WAAW,MAAmB,IAAuB;CAC5D,QAAQ,KAAK,MAAb;EACE,KAAK;EACL,KAAK;EACL,KAAK;GACH,KAAK,UAAU,EAAE;GACjB;EACF,KAAK;GACH,KAAK,UAAU,EAAE;GACjB;EACF,KAAK,YACH,OAAO,OAAO,oCAAoC;CACtD;AACF;AAOA,SAAgB,eACd,KACA,OACA,QACA,iBACO;CACP,MAAM,QAAQ,IAAI,aAAa;CAC/B,IAAI,aAAa;CAGjB,MAAM,aADS,MAAM,UAAU,IAAI,OAAO,KACvB,EAAO,QACxB,IAAI,WAAW,CAAC,GAChB,IAAI,OACJ,QACA,iBACA,IAAI,KACN;CACA,MAAM,YAAY,KAAK,UAAU;CAEjC,IAAI,MAAmB;CACvB,IAAI,IAAI,OACN,MAAM,iBACJ,IAAI,OACJ,KACA,OACA,OACA,IAAI,aACE,YACR;CAGF,MAAM,WAAW,IAAI,gBAAgB,GAAG;CACxC,WAAW,KAAK,QAAQ;CACxB,MAAM,YAAY,QAAQ;CAE1B,MAAM,WAAmC,CAAC;CAC1C,IAAI,IAAI,SACN,KAAK,MAAM,OAAO,IAAI,SAAS;EAC7B,MAAM,QAAQ,KACZ,IAAI,SAAS,OACb,kCACF;EACA,MAAM,mBAAmB,kBACvB,IAAI,YAAY,YAChB,IAAI,SAAS,KACf;EACA,SAAS,SAAS,eAChB,IAAI,UACJ,OACA,MACA,gBACF;CACF;CAGF,OAAO;EAAC,MAAM;EAAO;CAAQ;AAC/B;AAEA,SAAS,iBACP,WACA,OACA,OACA,OACA,aACA,WACuC;CACvC,Q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cACA,IACK;CACL,MAAM,QAAQ,eAAe,KAAK,OAAO,IAAI;CAC7C,gBAAgB,OAAO,cAAc,EAAE;CACvC,OAAO,gBAAgB,KAAK,KAAK;AACnC;AAEA,SAAS,iBACP,WACA,YACW;CACX,IAAI,UAAU,SAAS,UACrB,OAAO;CAGT,IAAI,UAAU,SAAS,sBAAsB;EAC3C,MAAM,SAAU,UACd;EAEF,MAAM,aAAa,WAAW,KAAA,KAAa,WAAW,IAAI,MAAM;EAEhE,OAAO;GACL,GAAG;GACH,MAAM;GACN,SAAS;IACP,GAAG,UAAU;IACb,UAAU;KACR,GAAG,UAAU,QAAQ;KACrB,OAAO,UAAU,QAAQ,SAAS,QAC9B,iBAAiB,UAAU,QAAQ,SAAS,OAAO,UAAU,IAC7D,KAAA;IACN;GACF;EACF;CACF;CAEA,OAAO;EACL,GAAG;EACH,YAAY,UAAU,WAAW,KAAI,MAAK,iBAAiB,GAAG,UAAU,CAAC;CAC3E;AACF;AAEA,SAAgB,gBAAgB,KAAU,OAAmB;CAC3D,MAAM,6BAAa,IAAI,IAAY;CACnC,KAAK,MAAM,QAAQ,MAAM,KAAK,OAC5B,IAAI,KAAK,SAAS,aAAa,KAAK,WAAW,KAAA,GAC7C,WAAW,IAAI,KAAK,MAAM;CAI9B,OAAO;EACL,GAAG;EACH,OAAO,IAAI,QAAQ,iBAAiB,IAAI,OAAO,UAAU,IAAI,KAAA;EAC7D,SAAS,IAAI,SAAS,KAAI,QAAO;GAC/B,MAAM,QAAQ,KACZ,IAAI,SAAS,OACb,kCACF;GACA,MAAM,UAAU,MAAM,SAAS;GAC/B,OAAO;IACL,GAAG;IACH,UAAU,UACN,gBAAgB,IAAI,UAAU,OAAO,IACrC,IAAI;GACV;EACF,CAAC;CACH;AACF"}
|
|
1
|
+
{"version":3,"file":"planner-builder.js","names":[],"sources":["../../../../../zql/src/planner/planner-builder.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {assert} from '../../../shared/src/asserts.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport type {\n AST,\n Condition,\n Conjunction,\n CorrelatedSubqueryCondition,\n Disjunction,\n SimpleCondition,\n} from '../../../zero-protocol/src/ast.ts';\nimport {planIdSymbol} from '../../../zero-protocol/src/ast.ts';\nimport {transformFilters} from '../builder/filter.ts';\nimport type {ConnectionCostModel} from './planner-connection.ts';\nimport type {PlannerConstraint} from './planner-constraint.ts';\nimport type {PlanDebugger} from './planner-debug.ts';\nimport {PlannerFanIn} from './planner-fan-in.ts';\nimport {PlannerFanOut} from './planner-fan-out.ts';\nimport {PlannerFilter} from './planner-filter.ts';\nimport {PlannerGraph} from './planner-graph.ts';\nimport {PlannerJoin} from './planner-join.ts';\nimport type {PlannerNode} from './planner-node.ts';\nimport {PlannerTerminus} from './planner-terminus.ts';\n\nfunction wireOutput(from: PlannerNode, to: PlannerNode): void {\n switch (from.kind) {\n case 'connection':\n case 'join':\n case 'fan-in':\n case 'filter':\n from.setOutput(to);\n break;\n case 'fan-out':\n from.addOutput(to);\n break;\n case 'terminus':\n assert(false, 'Terminus nodes cannot have outputs');\n }\n}\n\nexport type Plans = {\n plan: PlannerGraph;\n subPlans: {[key: string]: Plans};\n};\n\n/**\n * @param pushed The conditions that correlated predicate pushdown copied into\n * a child (see `pushDownCorrelatedPredicates`).\n */\nexport function buildPlanGraph(\n ast: AST,\n model: ConnectionCostModel,\n isRoot: boolean,\n baseConstraints?: PlannerConstraint,\n pushed?: ReadonlySet<SimpleCondition>,\n): Plans {\n const graph = new PlannerGraph();\n let nextPlanId = 0;\n\n const source = graph.addSource(ast.table, model);\n const connection = source.connect(\n ast.orderBy ?? [],\n ast.where,\n isRoot,\n baseConstraints,\n ast.limit,\n pushed,\n );\n graph.connections.push(connection);\n\n let end: PlannerNode = connection;\n if (ast.where) {\n end = processCondition(\n ast.where,\n end,\n graph,\n model,\n ast.table,\n () => nextPlanId++,\n pushed,\n );\n }\n\n const terminus = new PlannerTerminus(end);\n wireOutput(end, terminus);\n graph.setTerminus(terminus);\n\n const subPlans: {[key: string]: Plans} = {};\n if (ast.related) {\n for (const csq of ast.related) {\n const alias = must(\n csq.subquery.alias,\n 'Related subquery must have alias',\n );\n const childConstraints = extractConstraint(\n csq.correlation.childField,\n csq.subquery.table,\n );\n subPlans[alias] = buildPlanGraph(\n csq.subquery,\n model,\n true,\n childConstraints,\n pushed,\n );\n }\n }\n\n return {plan: graph, subPlans};\n}\n\nfunction processCondition(\n condition: Condition,\n input: Exclude<PlannerNode, PlannerTerminus>,\n graph: PlannerGraph,\n model: ConnectionCostModel,\n parentTable: string,\n getPlanId: () => number,\n pushed: ReadonlySet<SimpleCondition> | undefined,\n): Exclude<PlannerNode, PlannerTerminus> {\n switch (condition.type) {\n case 'simple':\n return input;\n case 'and':\n return processAnd(\n condition,\n input,\n graph,\n model,\n parentTable,\n getPlanId,\n pushed,\n );\n case 'or':\n return processOr(\n condition,\n input,\n graph,\n model,\n parentTable,\n getPlanId,\n pushed,\n );\n case 'correlatedSubquery':\n return processCorrelatedSubquery(\n condition,\n input,\n graph,\n model,\n parentTable,\n getPlanId,\n pushed,\n );\n }\n}\n\nfunction processAnd(\n condition: Conjunction,\n input: Exclude<PlannerNode, PlannerTerminus>,\n graph: PlannerGraph,\n model: ConnectionCostModel,\n parentTable: string,\n getPlanId: () => number,\n pushed: ReadonlySet<SimpleCondition> | undefined,\n): Exclude<PlannerNode, PlannerTerminus> {\n let end = input;\n for (const subCondition of condition.conditions) {\n end = processCondition(\n subCondition,\n end,\n graph,\n model,\n parentTable,\n getPlanId,\n pushed,\n );\n }\n return end;\n}\n\nfunction processOr(\n condition: Disjunction,\n input: Exclude<PlannerNode, PlannerTerminus>,\n graph: PlannerGraph,\n model: ConnectionCostModel,\n parentTable: string,\n getPlanId: () => number,\n pushed: ReadonlySet<SimpleCondition> | undefined,\n): Exclude<PlannerNode, PlannerTerminus> {\n // Skip building fan structure when no branch contains a CSQ. The runtime\n // collapses such ORs to a single Filter node, so the planner has nothing\n // to choose between.\n const hasAnySubquery = condition.conditions.some(\n c => c.type === 'correlatedSubquery' || hasCorrelatedSubquery(c),\n );\n if (!hasAnySubquery) {\n return input;\n }\n\n const fanOut = new PlannerFanOut(input);\n graph.fanOuts.push(fanOut);\n wireOutput(input, fanOut);\n\n const branches: Exclude<PlannerNode, PlannerTerminus>[] = [];\n for (const subCondition of condition.conditions) {\n let branch: Exclude<PlannerNode, PlannerTerminus>;\n if (\n subCondition.type === 'correlatedSubquery' ||\n hasCorrelatedSubquery(subCondition)\n ) {\n branch = processCondition(\n subCondition,\n fanOut,\n graph,\n model,\n parentTable,\n getPlanId,\n pushed,\n );\n } else {\n // Simple OR branch: wrap in a PlannerFilter so its filter can be\n // registered at the receiving connection on a per-branch basis when\n // the FanIn is in UFI mode.\n const transformed = transformFilters(subCondition).filters;\n const filter = new PlannerFilter(fanOut, transformed);\n graph.filters.push(filter);\n wireOutput(fanOut, filter);\n branch = filter;\n }\n branches.push(branch);\n fanOut.addOutput(branch);\n }\n\n const fanIn = new PlannerFanIn(branches);\n graph.fanIns.push(fanIn);\n for (const branch of branches) {\n wireOutput(branch, fanIn);\n }\n\n return fanIn;\n}\n\nfunction processCorrelatedSubquery(\n condition: CorrelatedSubqueryCondition,\n input: Exclude<PlannerNode, PlannerTerminus>,\n graph: PlannerGraph,\n model: ConnectionCostModel,\n parentTable: string,\n getPlanId: () => number,\n pushed: ReadonlySet<SimpleCondition> | undefined,\n): Exclude<PlannerNode, PlannerTerminus> {\n const {related} = condition;\n const childTable = related.subquery.table;\n\n const childSource = graph.hasSource(childTable)\n ? graph.getSource(childTable)\n : graph.addSource(childTable, model);\n\n const childConnection = childSource.connect(\n related.subquery.orderBy ?? [],\n related.subquery.where,\n false,\n undefined, // no base constraints for EXISTS/NOT EXISTS\n condition.op === 'EXISTS' ? 1 : undefined,\n pushed,\n );\n graph.connections.push(childConnection);\n\n let childEnd: PlannerNode = childConnection;\n if (related.subquery.where) {\n childEnd = processCondition(\n related.subquery.where,\n childEnd,\n graph,\n model,\n childTable,\n getPlanId,\n pushed,\n );\n }\n\n const parentConstraint = extractConstraint(\n related.correlation.parentField,\n parentTable,\n );\n const childConstraint = extractConstraint(\n related.correlation.childField,\n childTable,\n );\n\n const planId = getPlanId();\n condition[planIdSymbol] = planId;\n\n // Determine flippability and initial type based on flip flag and operator\n const isNotExists = condition.op === 'NOT EXISTS';\n const manualFlip = condition.flip;\n\n let flippable: boolean;\n let initialType: 'semi' | 'flipped';\n\n if (isNotExists) {\n // NOT EXISTS joins can never be flipped\n flippable = false;\n initialType = 'semi';\n } else if (manualFlip === true) {\n // User explicitly requested flip=true: start flipped, don't allow planner to change\n flippable = false;\n initialType = 'flipped';\n } else if (manualFlip === false) {\n // User explicitly requested flip=false: start semi, don't allow planner to change\n flippable = false;\n initialType = 'semi';\n } else {\n // flip is undefined: planner can decide\n flippable = true;\n initialType = 'semi';\n }\n\n const join = new PlannerJoin(\n input,\n childEnd,\n parentConstraint,\n childConstraint,\n flippable,\n planId,\n initialType,\n );\n graph.joins.push(join);\n childConnection.setParentJoin(join);\n\n wireOutput(input, join);\n wireOutput(childEnd, join);\n\n return join;\n}\n\nfunction hasCorrelatedSubquery(condition: Condition): boolean {\n if (condition.type === 'correlatedSubquery') {\n return true;\n }\n if (condition.type === 'and' || condition.type === 'or') {\n return condition.conditions.some(hasCorrelatedSubquery);\n }\n // simple conditions don't contain correlated subqueries\n return false;\n}\n\nfunction extractConstraint(\n fields: readonly string[],\n _tableName: string,\n): PlannerConstraint {\n return Object.fromEntries(fields.map(field => [field, undefined]));\n}\n\nfunction planRecursively(\n plans: Plans,\n planDebugger?: PlanDebugger,\n lc?: LogContext,\n): void {\n for (const subPlan of Object.values(plans.subPlans)) {\n planRecursively(subPlan, planDebugger, lc);\n }\n plans.plan.plan(planDebugger, lc);\n}\n\n/**\n * @param pushed The conditions that correlated predicate pushdown copied into\n * a child (see `pushDownCorrelatedPredicates`). The planner counts each one\n * only where it removes rows.\n */\nexport function planQuery(\n ast: AST,\n model: ConnectionCostModel,\n planDebugger?: PlanDebugger,\n lc?: LogContext,\n pushed?: ReadonlySet<SimpleCondition>,\n): AST {\n const plans = buildPlanGraph(ast, model, true, undefined, pushed);\n planRecursively(plans, planDebugger, lc);\n return applyPlansToAST(ast, plans);\n}\n\nfunction applyToCondition(\n condition: Condition,\n flippedIds: Set<number>,\n): Condition {\n if (condition.type === 'simple') {\n return condition;\n }\n\n if (condition.type === 'correlatedSubquery') {\n const planId = (condition as unknown as Record<symbol, number>)[\n planIdSymbol\n ];\n const shouldFlip = planId !== undefined && flippedIds.has(planId);\n\n return {\n ...condition,\n flip: shouldFlip,\n related: {\n ...condition.related,\n subquery: {\n ...condition.related.subquery,\n where: condition.related.subquery.where\n ? applyToCondition(condition.related.subquery.where, flippedIds)\n : undefined,\n },\n },\n };\n }\n\n return {\n ...condition,\n conditions: condition.conditions.map(c => applyToCondition(c, flippedIds)),\n };\n}\n\nexport function applyPlansToAST(ast: AST, plans: Plans): AST {\n const flippedIds = new Set<number>();\n for (const join of plans.plan.joins) {\n if (join.type === 'flipped' && join.planId !== undefined) {\n flippedIds.add(join.planId);\n }\n }\n\n return {\n ...ast,\n where: ast.where ? applyToCondition(ast.where, flippedIds) : undefined,\n related: ast.related?.map(csq => {\n const alias = must(\n csq.subquery.alias,\n 'Related subquery must have alias',\n );\n const subPlan = plans.subPlans[alias];\n return {\n ...csq,\n subquery: subPlan\n ? applyPlansToAST(csq.subquery, subPlan)\n : csq.subquery,\n };\n }),\n };\n}\n"],"mappings":";;;;;;;;;;;AAwBA,SAAS,WAAW,MAAmB,IAAuB;CAC5D,QAAQ,KAAK,MAAb;EACE,KAAK;EACL,KAAK;EACL,KAAK;EACL,KAAK;GACH,KAAK,UAAU,EAAE;GACjB;EACF,KAAK;GACH,KAAK,UAAU,EAAE;GACjB;EACF,KAAK,YACH,OAAO,OAAO,oCAAoC;CACtD;AACF;;;;;AAWA,SAAgB,eACd,KACA,OACA,QACA,iBACA,QACO;CACP,MAA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AC3C,MAAM,SAAU,UACd;EAEF,MAAM,aAAa,WAAW,KAAA,KAAa,WAAW,IAAI,MAAM;EAEhE,OAAO;GACL,GAAG;GACH,MAAM;GACN,SAAS;IACP,GAAG,UAAU;IACb,UAAU;KACR,GAAG,UAAU,QAAQ;KACrB,OAAO,UAAU,QAAQ,SAAS,QAC9B,iBAAiB,UAAU,QAAQ,SAAS,OAAO,UAAU,IAC7D,KAAA;IACN;GACF;EACF;CACF;CAEA,OAAO;EACL,GAAG;EACH,YAAY,UAAU,WAAW,KAAI,MAAK,iBAAiB,GAAG,UAAU,CAAC;CAC3E;AACF;AAEA,SAAgB,gBAAgB,KAAU,OAAmB;CAC3D,MAAM,6BAAa,IAAI,IAAY;CACnC,KAAK,MAAM,QAAQ,MAAM,KAAK,OAC5B,IAAI,KAAK,SAAS,aAAa,KAAK,WAAW,KAAA,GAC7C,WAAW,IAAI,KAAK,MAAM;CAI9B,OAAO;EACL,GAAG;EACH,OAAO,IAAI,QAAQ,iBAAiB,IAAI,OAAO,UAAU,IAAI,KAAA;EAC7D,SAAS,IAAI,SAAS,KAAI,QAAO;GAC/B,MAAM,QAAQ,KACZ,IAAI,SAAS,OACb,kCACF;GACA,MAAM,UAAU,MAAM,SAAS;GAC/B,OAAO;IACL,GAAG;IACH,UAAU,UACN,gBAAgB,IAAI,UAAU,OAAO,IACrC,IAAI;GACV;EACF,CAAC;CACH;AACF"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type { Condition, Ordering } from '../../../zero-protocol/src/ast.ts';
|
|
1
|
+
import type { Condition, Ordering, SimpleCondition } from '../../../zero-protocol/src/ast.ts';
|
|
2
|
+
import type { NoSubqueryCondition } from '../builder/filter.ts';
|
|
2
3
|
import { type PlannerConstraint } from './planner-constraint.ts';
|
|
3
4
|
import type { PlanDebugger } from './planner-debug.ts';
|
|
5
|
+
import type { PlannerJoin } from './planner-join.ts';
|
|
4
6
|
import type { CostEstimate, JoinOrConnection, PlannerNode } from './planner-node.ts';
|
|
5
7
|
/**
|
|
6
8
|
* Represents a connection to a source (table scan).
|
|
@@ -40,6 +42,17 @@ import type { CostEstimate, JoinOrConnection, PlannerNode } from './planner-node
|
|
|
40
42
|
* - If `assignee` chosen first: Reveals constraint `assignee_id` for issue connection
|
|
41
43
|
* - Updated costs guide the next selection
|
|
42
44
|
*
|
|
45
|
+
* # Pushed conditions
|
|
46
|
+
* Correlated predicate pushdown copies a parent's condition on a correlation
|
|
47
|
+
* column into the child (`pushed`). When the parent drives the join into the
|
|
48
|
+
* child, the join binds that column to a parent row, so the parent's
|
|
49
|
+
* condition implies the copy and the copy removes no rows. The connection
|
|
50
|
+
* leaves such a copy out of the cost model call. Another join that binds the
|
|
51
|
+
* column does not imply the copy, because its rows need not hold the parent's
|
|
52
|
+
* condition. The connection also leaves every copy out of `selectivity`: a
|
|
53
|
+
* semi-join always binds the column, and a flipped join gets the copy's
|
|
54
|
+
* reduction through `returnedRows` instead.
|
|
55
|
+
*
|
|
43
56
|
* # Lifecycle
|
|
44
57
|
* 1. Construct with immutable structure (ordering, filters, cost model)
|
|
45
58
|
* 2. Wire to output node during graph construction
|
|
@@ -58,9 +71,10 @@ export declare class PlannerConnection {
|
|
|
58
71
|
* and should not be limited by EXISTS semantics.
|
|
59
72
|
*/
|
|
60
73
|
limit: number | undefined;
|
|
61
|
-
constructor(table: string, model: ConnectionCostModel, sort: Ordering, filters: Condition | undefined, isRoot: boolean, baseConstraints?: PlannerConstraint, limit?: number, name?: string);
|
|
74
|
+
constructor(table: string, model: ConnectionCostModel, sort: Ordering, filters: Condition | undefined, isRoot: boolean, baseConstraints?: PlannerConstraint, limit?: number, name?: string, pushed?: ReadonlySet<SimpleCondition>);
|
|
62
75
|
setOutput(node: PlannerNode): void;
|
|
63
76
|
get output(): PlannerNode;
|
|
77
|
+
setParentJoin(join: PlannerJoin): void;
|
|
64
78
|
closestJoinOrSource(): JoinOrConnection;
|
|
65
79
|
/**
|
|
66
80
|
* Constraints are uniquely identified by their path through the
|
|
@@ -77,6 +91,13 @@ export declare class PlannerConnection {
|
|
|
77
91
|
* root branches.
|
|
78
92
|
*/
|
|
79
93
|
propagateConstraints(path: number[], c: PlannerConstraint | undefined, from?: PlannerNode, planDebugger?: PlanDebugger): void;
|
|
94
|
+
/**
|
|
95
|
+
* Register a per-branch filter for the given branch pattern. Called by
|
|
96
|
+
* `PlannerFilter` (a simple OR branch) when the enclosing FanIn is in
|
|
97
|
+
* UFI mode. The filter is AND-merged with `this.#filters` when the cost
|
|
98
|
+
* model is invoked for this branch.
|
|
99
|
+
*/
|
|
100
|
+
setPerBranchFilter(path: number[], filter: NoSubqueryCondition): void;
|
|
80
101
|
estimateCost(downstreamChildSelectivity: number, branchPattern: number[], planDebugger?: PlanDebugger): CostEstimate;
|
|
81
102
|
/**
|
|
82
103
|
* Remove the limit from this connection.
|
|
@@ -98,6 +119,11 @@ export declare class PlannerConnection {
|
|
|
98
119
|
/**
|
|
99
120
|
* Restore constraint state from a snapshot.
|
|
100
121
|
* Used by PlannerGraph to restore planning state.
|
|
122
|
+
*
|
|
123
|
+
* Per-branch filters are not snapshotted — they are re-derived by the
|
|
124
|
+
* subsequent `propagateConstraints` pass. Clearing them here ensures
|
|
125
|
+
* that any entries left over from the previous iteration don't linger
|
|
126
|
+
* in case the restored plan registers a different set.
|
|
101
127
|
*/
|
|
102
128
|
restoreConstraints(constraints: Map<string, PlannerConstraint | undefined>): void;
|
|
103
129
|
/** Get current constraints for debugging. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"planner-connection.d.ts","sourceRoot":"","sources":["../../../../../zql/src/planner/planner-connection.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"planner-connection.d.ts","sourceRoot":"","sources":["../../../../../zql/src/planner/planner-connection.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,SAAS,EACT,QAAQ,EACR,eAAe,EAChB,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAA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S,EAC9B,UAAU,EAAE,iBAAiB,GAAG,SAAS,KACtC,aAAa,CAAC"}
|
|
@@ -40,6 +40,17 @@ import { mergeConstraints } from "./planner-constraint.js";
|
|
|
40
40
|
* - If `assignee` chosen first: Reveals constraint `assignee_id` for issue connection
|
|
41
41
|
* - Updated costs guide the next selection
|
|
42
42
|
*
|
|
43
|
+
* # Pushed conditions
|
|
44
|
+
* Correlated predicate pushdown copies a parent's condition on a correlation
|
|
45
|
+
* column into the child (`pushed`). When the parent drives the join into the
|
|
46
|
+
* child, the join binds that column to a parent row, so the parent's
|
|
47
|
+
* condition implies the copy and the copy removes no rows. The connection
|
|
48
|
+
* leaves such a copy out of the cost model call. Another join that binds the
|
|
49
|
+
* column does not imply the copy, because its rows need not hold the parent's
|
|
50
|
+
* condition. The connection also leaves every copy out of `selectivity`: a
|
|
51
|
+
* semi-join always binds the column, and a flipped join gets the copy's
|
|
52
|
+
* reduction through `returnedRows` instead.
|
|
53
|
+
*
|
|
43
54
|
* # Lifecycle
|
|
44
55
|
* 1. Construct with immutable structure (ordering, filters, cost model)
|
|
45
56
|
* 2. Wire to output node during graph construction
|
|
@@ -50,6 +61,9 @@ var PlannerConnection = class {
|
|
|
50
61
|
kind = "connection";
|
|
51
62
|
#sort;
|
|
52
63
|
#filters;
|
|
64
|
+
#pushed;
|
|
65
|
+
/** `#filters` without the pushed conditions. */
|
|
66
|
+
#unpushedFilters;
|
|
53
67
|
#model;
|
|
54
68
|
table;
|
|
55
69
|
name;
|
|
@@ -58,6 +72,13 @@ var PlannerConnection = class {
|
|
|
58
72
|
selectivity;
|
|
59
73
|
#output;
|
|
60
74
|
/**
|
|
75
|
+
* The join from the parent into this connection's EXISTS subquery. Set once
|
|
76
|
+
* during graph construction. The pushed conditions are copies of that
|
|
77
|
+
* parent's conditions. Undefined for a root connection, whose parent (if
|
|
78
|
+
* it is a `related` subquery) always drives it.
|
|
79
|
+
*/
|
|
80
|
+
#parentJoin;
|
|
81
|
+
/**
|
|
61
82
|
* Current limit during planning. Can be cleared (set to undefined) when a
|
|
62
83
|
* parent join is flipped, indicating this connection is now in an outer loop
|
|
63
84
|
* and should not be limited by EXISTS semantics.
|
|
@@ -71,6 +92,16 @@ var PlannerConnection = class {
|
|
|
71
92
|
* a single join in the UFO is flipped - other branches report undefined.
|
|
72
93
|
*/
|
|
73
94
|
#constraints;
|
|
95
|
+
/**
|
|
96
|
+
* Per-branch filters registered by `PlannerFilter` nodes (simple OR
|
|
97
|
+
* branches) when the enclosing FanIn is in UFI mode. Key is the same
|
|
98
|
+
* `branchPattern.join(',')` used by `#constraints`.
|
|
99
|
+
*
|
|
100
|
+
* These are AND-merged with `#filters` when calling the cost model so
|
|
101
|
+
* the model sees the true filter that the runtime would push to the
|
|
102
|
+
* source for this specific branch.
|
|
103
|
+
*/
|
|
104
|
+
#perBranchFilters = /* @__PURE__ */ new Map();
|
|
74
105
|
#isRoot;
|
|
75
106
|
/**
|
|
76
107
|
* Cached per-constraint costs to avoid redundant cost model calls.
|
|
@@ -78,22 +109,20 @@ var PlannerConnection = class {
|
|
|
78
109
|
* Invalidated when constraints change.
|
|
79
110
|
*/
|
|
80
111
|
#cachedConstraintCosts = /* @__PURE__ */ new Map();
|
|
81
|
-
constructor(table, model, sort, filters, isRoot, baseConstraints, limit, name) {
|
|
112
|
+
constructor(table, model, sort, filters, isRoot, baseConstraints, limit, name, pushed) {
|
|
82
113
|
this.table = table;
|
|
83
114
|
this.name = name ?? table;
|
|
84
115
|
this.#sort = sort;
|
|
85
116
|
this.#filters = filters;
|
|
117
|
+
this.#pushed = pushed;
|
|
118
|
+
this.#unpushedFilters = pushed ? withoutConjuncts(filters, (c) => pushed.has(c)) : filters;
|
|
86
119
|
this.#model = model;
|
|
87
120
|
this.#baseConstraints = baseConstraints;
|
|
88
121
|
this.#baseLimit = limit;
|
|
89
122
|
this.limit = limit;
|
|
90
123
|
this.#constraints = /* @__PURE__ */ new Map();
|
|
91
124
|
this.#isRoot = isRoot;
|
|
92
|
-
|
|
93
|
-
const costWithFilters = model(table, sort, filters, void 0);
|
|
94
|
-
const costWithoutFilters = model(table, sort, void 0, void 0);
|
|
95
|
-
this.selectivity = costWithoutFilters.rows > 0 ? costWithFilters.rows / costWithoutFilters.rows : 1;
|
|
96
|
-
} else this.selectivity = 1;
|
|
125
|
+
this.selectivity = this.#computeSelectivity(this.#unpushedFilters);
|
|
97
126
|
}
|
|
98
127
|
setOutput(node) {
|
|
99
128
|
this.#output = node;
|
|
@@ -102,6 +131,9 @@ var PlannerConnection = class {
|
|
|
102
131
|
assert(this.#output !== void 0, "Output not set");
|
|
103
132
|
return this.#output;
|
|
104
133
|
}
|
|
134
|
+
setParentJoin(join) {
|
|
135
|
+
this.#parentJoin = join;
|
|
136
|
+
}
|
|
105
137
|
closestJoinOrSource() {
|
|
106
138
|
return "connection";
|
|
107
139
|
}
|
|
@@ -132,19 +164,31 @@ var PlannerConnection = class {
|
|
|
132
164
|
from: from?.kind ?? "unknown"
|
|
133
165
|
});
|
|
134
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* Register a per-branch filter for the given branch pattern. Called by
|
|
169
|
+
* `PlannerFilter` (a simple OR branch) when the enclosing FanIn is in
|
|
170
|
+
* UFI mode. The filter is AND-merged with `this.#filters` when the cost
|
|
171
|
+
* model is invoked for this branch.
|
|
172
|
+
*/
|
|
173
|
+
setPerBranchFilter(path, filter) {
|
|
174
|
+
this.#perBranchFilters.set(path.join(","), filter);
|
|
175
|
+
this.#cachedConstraintCosts.clear();
|
|
176
|
+
}
|
|
135
177
|
estimateCost(downstreamChildSelectivity, branchPattern, planDebugger) {
|
|
136
178
|
const key = branchPattern.join(",");
|
|
137
179
|
let cost = this.#cachedConstraintCosts.get(key);
|
|
138
180
|
if (cost !== void 0) return cost;
|
|
139
181
|
const constraint = this.#constraints.get(key);
|
|
140
182
|
const mergedConstraint = mergeConstraints(this.#baseConstraints, constraint);
|
|
141
|
-
const
|
|
183
|
+
const perBranchFilter = this.#perBranchFilters.get(key);
|
|
184
|
+
const { startupCost, fanout, rows } = this.#model(this.table, this.#sort, andFilters(this.#filtersFor(mergedConstraint), perBranchFilter), mergedConstraint);
|
|
185
|
+
const selectivity = perBranchFilter ? this.#computeSelectivity(andFilters(this.#unpushedFilters, perBranchFilter)) : this.selectivity;
|
|
142
186
|
cost = {
|
|
143
187
|
startupCost,
|
|
144
188
|
scanEst: this.limit === void 0 ? rows : Math.min(rows, this.limit / downstreamChildSelectivity),
|
|
145
189
|
cost: 0,
|
|
146
190
|
returnedRows: rows,
|
|
147
|
-
selectivity
|
|
191
|
+
selectivity,
|
|
148
192
|
limit: this.limit,
|
|
149
193
|
fanout
|
|
150
194
|
};
|
|
@@ -162,6 +206,26 @@ var PlannerConnection = class {
|
|
|
162
206
|
return cost;
|
|
163
207
|
}
|
|
164
208
|
/**
|
|
209
|
+
* `#filters` without the pushed conditions that `constraint` binds the
|
|
210
|
+
* column of, when the parent drives the join into this connection. The
|
|
211
|
+
* runtime still applies them, but they remove no rows.
|
|
212
|
+
*
|
|
213
|
+
* When that join is flipped, this connection is the outer loop, so every
|
|
214
|
+
* pushed condition counts, even where another flipped join binds its
|
|
215
|
+
* column.
|
|
216
|
+
*/
|
|
217
|
+
#filtersFor(constraint) {
|
|
218
|
+
const pushed = this.#pushed;
|
|
219
|
+
if (!pushed || !constraint || this.#unpushedFilters === this.#filters || this.#parentJoin?.type === "flipped") return this.#filters;
|
|
220
|
+
return withoutConjuncts(this.#filters, (c) => pushed.has(c) && c.left.type === "column" && Object.hasOwn(constraint, c.left.name));
|
|
221
|
+
}
|
|
222
|
+
#computeSelectivity(filters) {
|
|
223
|
+
if (this.#baseLimit === void 0 || filters === void 0) return 1;
|
|
224
|
+
const costWithFilters = this.#model(this.table, this.#sort, filters, void 0);
|
|
225
|
+
const costWithoutFilters = this.#model(this.table, this.#sort, void 0, void 0);
|
|
226
|
+
return costWithoutFilters.rows > 0 ? costWithFilters.rows / costWithoutFilters.rows : 1;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
165
229
|
* Remove the limit from this connection.
|
|
166
230
|
* Called when a parent join is flipped, making this connection part of an
|
|
167
231
|
* outer loop that should produce all rows rather than stopping at the limit.
|
|
@@ -179,6 +243,7 @@ var PlannerConnection = class {
|
|
|
179
243
|
}
|
|
180
244
|
reset() {
|
|
181
245
|
this.#constraints.clear();
|
|
246
|
+
this.#perBranchFilters.clear();
|
|
182
247
|
this.limit = this.#baseLimit;
|
|
183
248
|
this.#cachedConstraintCosts.clear();
|
|
184
249
|
}
|
|
@@ -192,9 +257,15 @@ var PlannerConnection = class {
|
|
|
192
257
|
/**
|
|
193
258
|
* Restore constraint state from a snapshot.
|
|
194
259
|
* Used by PlannerGraph to restore planning state.
|
|
260
|
+
*
|
|
261
|
+
* Per-branch filters are not snapshotted — they are re-derived by the
|
|
262
|
+
* subsequent `propagateConstraints` pass. Clearing them here ensures
|
|
263
|
+
* that any entries left over from the previous iteration don't linger
|
|
264
|
+
* in case the restored plan registers a different set.
|
|
195
265
|
*/
|
|
196
266
|
restoreConstraints(constraints) {
|
|
197
267
|
this.#constraints.clear();
|
|
268
|
+
this.#perBranchFilters.clear();
|
|
198
269
|
for (const [key, value] of constraints) this.#constraints.set(key, value);
|
|
199
270
|
this.#cachedConstraintCosts.clear();
|
|
200
271
|
}
|
|
@@ -219,6 +290,35 @@ var PlannerConnection = class {
|
|
|
219
290
|
return record;
|
|
220
291
|
}
|
|
221
292
|
};
|
|
293
|
+
function andFilters(a, b) {
|
|
294
|
+
return a && b ? {
|
|
295
|
+
type: "and",
|
|
296
|
+
conditions: [a, b]
|
|
297
|
+
} : a ?? b;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* `c` without the simple conjuncts, including those of nested ANDs, for which
|
|
301
|
+
* `remove` returns true. Returns `c` itself when it removes nothing.
|
|
302
|
+
*/
|
|
303
|
+
function withoutConjuncts(c, remove) {
|
|
304
|
+
switch (c?.type) {
|
|
305
|
+
case "simple": return remove(c) ? void 0 : c;
|
|
306
|
+
case "and": {
|
|
307
|
+
const conditions = [];
|
|
308
|
+
for (const x of c.conditions) {
|
|
309
|
+
const y = withoutConjuncts(x, remove);
|
|
310
|
+
if (y !== void 0) conditions.push(y);
|
|
311
|
+
}
|
|
312
|
+
if (conditions.length === c.conditions.length && conditions.every((x, i) => x === c.conditions[i])) return c;
|
|
313
|
+
if (conditions.length <= 1) return conditions[0];
|
|
314
|
+
return {
|
|
315
|
+
type: "and",
|
|
316
|
+
conditions
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
default: return c;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
222
322
|
//#endregion
|
|
223
323
|
export { PlannerConnection };
|
|
224
324
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"planner-connection.js","names":["#sort","#filters","#model","#baseConstraints","#baseLimit","#constraints","#isRoot","#output","#cachedConstraintCosts"],"sources":["../../../../../zql/src/planner/planner-connection.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport type {Condition, Ordering} from '../../../zero-protocol/src/ast.ts';\nimport {\n mergeConstraints,\n type PlannerConstraint,\n} from './planner-constraint.ts';\nimport type {PlanDebugger} from './planner-debug.ts';\nimport {omitFanout} from './planner-node.ts';\nimport type {\n CostEstimate,\n JoinOrConnection,\n PlannerNode,\n} from './planner-node.ts';\n\n/**\n * Represents a connection to a source (table scan).\n *\n * # Dual State Pattern\n * Like all planner nodes, PlannerConnection separates:\n * 1. immutable structure: Ordering, filters, cost model (set at construction)\n * 2. mutable state: Pinned status, constraints (mutated during planning)\n *\n * # Cost Estimation\n * The ordering and filters determine the initial cost. As planning progresses,\n * constraints from parent joins refine the cost estimate.\n *\n * # Constraint Flow\n * When a connection is pinned as the outer loop, it reveals constraints for\n * connected joins. These constraints propagate through the graph, allowing\n * other connections to update their cost estimates.\n *\n * Example:\n *\n * ```ts\n * builder.issue.whereExists('assignee', a => a.where('name', 'Alice'))\n * ```\n *\n * ```\n * [issue] [assignee]\n * | |\n * | +-- where name = 'Alice'\n * \\ /\n * \\ /\n * [join]\n * |\n * ```\n *\n * - Initial state: Both connections have no constraints, costs are unconstrained\n * - If `issue` chosen first: Reveals constraint `assignee_id` for assignee connection\n * - If `assignee` chosen first: Reveals constraint `assignee_id` for issue connection\n * - Updated costs guide the next selection\n *\n * # Lifecycle\n * 1. Construct with immutable structure (ordering, filters, cost model)\n * 2. Wire to output node during graph construction\n * 3. Planning mutates pinned status and accumulates constraints\n * 4. reset() clears mutable state for replanning\n */\nexport class PlannerConnection {\n readonly kind = 'connection' as const;\n\n // ========================================================================\n // IMMUTABLE STRUCTURE (set during construction, never changes)\n // ========================================================================\n readonly #sort: Ordering;\n readonly #filters: Condition | undefined;\n readonly #model: ConnectionCostModel;\n readonly table: string;\n readonly name: string; // Human-readable name for debugging (defaults to table name)\n readonly #baseConstraints: PlannerConstraint | undefined; // Constraints from parent correlation\n readonly #baseLimit: number | undefined; // Original limit from query structure (never modified)\n readonly selectivity: number; // Fraction of rows passing filters (1.0 = no filtering)\n #output?: PlannerNode | undefined; // Set once during graph construction\n\n // ========================================================================\n // MUTABLE PLANNING STATE (changes during plan search)\n // ========================================================================\n /**\n * Current limit during planning. Can be cleared (set to undefined) when a\n * parent join is flipped, indicating this connection is now in an outer loop\n * and should not be limited by EXISTS semantics.\n */\n limit: number | undefined;\n\n /**\n * Constraints accumulated from parent joins during planning.\n * Key is a path through the graph (e.g., \"0,1\" for branch pattern [0,1]).\n *\n * Undefined constraints are possible when a FO converts to UFO and only\n * a single join in the UFO is flipped - other branches report undefined.\n */\n readonly #constraints: Map<string, PlannerConstraint | undefined>;\n\n readonly #isRoot: boolean;\n\n /**\n * Cached per-constraint costs to avoid redundant cost model calls.\n * Maps constraint key (branch pattern string) to computed cost.\n * Invalidated when constraints change.\n */\n #cachedConstraintCosts: Map<string, CostEstimate> = new Map();\n\n constructor(\n table: string,\n model: ConnectionCostModel,\n sort: Ordering,\n filters: Condition | undefined,\n isRoot: boolean,\n baseConstraints?: PlannerConstraint,\n limit?: number,\n name?: string,\n ) {\n this.table = table;\n this.name = name ?? table;\n this.#sort = sort;\n this.#filters = filters;\n this.#model = model;\n this.#baseConstraints = baseConstraints;\n this.#baseLimit = limit;\n this.limit = limit;\n this.#constraints = new Map();\n this.#isRoot = isRoot;\n\n // Compute selectivity for EXISTS child connections (baseLimit === 1)\n // Selectivity = fraction of rows that pass filters\n if (limit !== undefined && filters) {\n const costWithFilters = model(table, sort, filters, undefined);\n const costWithoutFilters = model(table, sort, undefined, undefined);\n this.selectivity =\n costWithoutFilters.rows > 0\n ? costWithFilters.rows / costWithoutFilters.rows\n : 1.0;\n } else {\n // Root connections or connections without filters\n this.selectivity = 1.0;\n }\n }\n\n setOutput(node: PlannerNode): void {\n this.#output = node;\n }\n\n get output(): PlannerNode {\n assert(this.#output !== undefined, 'Output not set');\n return this.#output;\n }\n\n closestJoinOrSource(): JoinOrConnection {\n return 'connection';\n }\n\n /**\n * Constraints are uniquely identified by their path through the\n * graph.\n *\n * FO represents all sub-joins as a single path.\n * UFO represents each sub-join as a separate path.\n * The first branch in a UFO will match the path of FO so no re-set needs to happen\n * when swapping from FO to UFO.\n *\n * FO swaps to UFO when a join inside FO-FI gets flipped.\n *\n * The max of the last element of the paths is the number of\n * root branches.\n */\n propagateConstraints(\n path: number[],\n c: PlannerConstraint | undefined,\n from?: PlannerNode,\n planDebugger?: PlanDebugger,\n ): void {\n const key = path.join(',');\n this.#constraints.set(key, c);\n // Constraints changed, invalidate cost caches\n this.#cachedConstraintCosts.clear();\n\n planDebugger?.log({\n type: 'node-constraint',\n nodeType: 'connection',\n node: this.name,\n branchPattern: path,\n constraint: c,\n from: from?.kind ?? 'unknown',\n });\n }\n\n estimateCost(\n downstreamChildSelectivity: number,\n branchPattern: number[],\n planDebugger?: PlanDebugger,\n ): CostEstimate {\n // Branch pattern specified - return cost for this specific branch\n const key = branchPattern.join(',');\n\n // Check per-constraint cache first\n let cost = this.#cachedConstraintCosts.get(key);\n if (cost !== undefined) {\n return cost;\n }\n\n // Cache miss - compute and cache\n const constraint = this.#constraints.get(key);\n // Merge base constraints with propagated constraints\n const mergedConstraint = mergeConstraints(\n this.#baseConstraints,\n constraint,\n );\n const {startupCost, fanout, rows} = this.#model(\n this.table,\n this.#sort,\n this.#filters,\n mergedConstraint,\n );\n cost = {\n startupCost,\n scanEst:\n this.limit === undefined\n ? rows\n : Math.min(rows, this.limit / downstreamChildSelectivity),\n cost: 0,\n returnedRows: rows,\n selectivity: this.selectivity,\n limit: this.limit,\n fanout,\n };\n this.#cachedConstraintCosts.set(key, cost);\n\n if (planDebugger) {\n planDebugger.log({\n type: 'node-cost',\n nodeType: 'connection',\n node: this.name,\n branchPattern,\n downstreamChildSelectivity,\n costEstimate: omitFanout(cost),\n filters: this.#filters,\n ordering: this.#sort,\n });\n }\n\n return cost;\n }\n\n /**\n * Remove the limit from this connection.\n * Called when a parent join is flipped, making this connection part of an\n * outer loop that should produce all rows rather than stopping at the limit.\n */\n unlimit(): void {\n if (this.#isRoot) {\n // We cannot unlimit root connections\n return;\n }\n if (this.limit !== undefined) {\n this.limit = undefined;\n // Limit changes do not impact connection costs.\n // Limit is taken into account at the join level.\n // Given that, we do not need to invalidate cost caches here.\n }\n }\n\n /**\n * Propagate unlimiting when a parent join is flipped.\n * For connections, we simply remove the limit.\n */\n propagateUnlimitFromFlippedJoin(): void {\n this.unlimit();\n }\n\n reset() {\n this.#constraints.clear();\n this.limit = this.#baseLimit;\n // Clear all cost caches\n this.#cachedConstraintCosts.clear();\n }\n\n /**\n * Capture constraint state for snapshotting.\n * Used by PlannerGraph to save/restore planning state.\n */\n captureConstraints(): Map<string, PlannerConstraint | undefined> {\n return new Map(this.#constraints);\n }\n\n /**\n * Restore constraint state from a snapshot.\n * Used by PlannerGraph to restore planning state.\n */\n restoreConstraints(\n constraints: Map<string, PlannerConstraint | undefined>,\n ): void {\n this.#constraints.clear();\n for (const [key, value] of constraints) {\n this.#constraints.set(key, value);\n }\n // Constraints changed, invalidate cost caches\n this.#cachedConstraintCosts.clear();\n }\n\n /** Get current constraints for debugging. */\n getConstraintsForDebug(): Record<string, PlannerConstraint | undefined> {\n const record: Record<string, PlannerConstraint | undefined> = {};\n for (const [key, value] of this.#constraints) {\n record[key] = value;\n }\n return record;\n }\n\n /** Get filters for debugging. */\n getFiltersForDebug(): Condition | undefined {\n return this.#filters;\n }\n\n /** Get sort/ordering for debugging. */\n getSortForDebug(): Ordering {\n return this.#sort;\n }\n\n /** Get estimated cost for each constraint branch. */\n getConstraintCostsForDebug(): Record<string, CostEstimate> {\n const record: Record<string, CostEstimate> = {};\n for (const [key, value] of this.#cachedConstraintCosts) {\n record[key] = value;\n }\n return record;\n }\n}\n\ntype FanoutEst = {\n fanout: number;\n confidence: 'high' | 'med' | 'none';\n};\nexport type FanoutCostModel = (columns: string[]) => FanoutEst;\n\nexport type CostModelCost = {\n startupCost: number;\n rows: number;\n fanout: FanoutCostModel;\n};\nexport type ConnectionCostModel = (\n table: string,\n sort: Ordering,\n filters: Condition | undefined,\n constraint: PlannerConstraint | undefined,\n) => CostModelCost;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0DA,IAAa,oBAAb,MAA+B;CAC7B,OAAgB;CAKhB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;;;;CAUA;;;;;;;;CASA;CAEA;;;;;;CAOA,yCAAoD,IAAI,IAAI;CAE5D,YACE,OACA,OACA,MACA,SACA,QACA,iBACA,OACA,MACA;EACA,KAAK,QAAQ;EACb,KAAK,OAAO,QAAQ;EACpB,KAAKA,QAAQ;EACb,KAAKC,WAAW;EAChB,KAAKC,SAAS;EACd,KAAKC,mBAAmB;EACxB,KAAKC,aAAa;EAClB,KAAK,QAAQ;EACb,KAAKC,+BAAe,IAAI,IAAI;EAC5B,KAAKC,UAAU;EAIf,IAAI,UAAU,KAAA,KAAa,SAAS;GAClC,MAAM,kBAAkB,MAAM,OAA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ACA,cAAc,WAAW,IAAI;GAC7B,SAAS,KAAKP;GACd,UAAU,KAAKD;EACjB,CAAC;EAGH,OAAO;CACT;;;;;;CAOA,UAAgB;EACd,IAAI,KAAKM,SAEP;EAEF,IAAI,KAAK,UAAU,KAAA,GACjB,KAAK,QAAQ,KAAA;CAKjB;;;;;CAMA,kCAAwC;EACtC,KAAK,QAAQ;CACf;CAEA,QAAQ;EACN,KAAKD,aAAa,MAAM;EACxB,KAAK,QAAQ,KAAKD;EAElB,KAAKI,uBAAuB,MAAM;CACpC;;;;;CAMA,qBAAiE;EAC/D,OAAO,IAAI,IAAI,KAAKH,YAAY;CAClC;;;;;CAMA,mBACE,aACM;EACN,KAAKA,aAAa,MAAM;EACxB,KAAK,MAAM,CAAC,KAAK,UAAU,aACzB,KAAKA,aAAa,IAAI,KAAK,KAAK;EAGlC,KAAKG,uBAAuB,MAAM;CACpC;;CAGA,yBAAwE;EACtE,MAAM,SAAwD,CAAC;EAC/D,KAAK,MAAM,CAAC,KAAK,UAAU,KAAKH,cAC9B,OAAO,OAAO;EAEhB,OAAO;CACT;;CAGA,qBAA4C;EAC1C,OAAO,KAAKJ;CACd;;CAGA,kBAA4B;EAC1B,OAAO,KAAKD;CACd;;CAGA,6BAA2D;EACzD,MAAM,SAAuC,CAAC;EAC9C,KAAK,MAAM,CAAC,KAAK,UAAU,KAAKQ,wBAC9B,OAAO,OAAO;EAEhB,OAAO;CACT;AACF"}
|
|
1
|
+
{"version":3,"file":"planner-connection.js","names":["#sort","#filters","#pushed","#unpushedFilters","#model","#baseConstraints","#baseLimit","#constraints","#perBranchFilters","#isRoot","#computeSelectivity","#output","#parentJoin","#cachedConstraintCosts","#filtersFor"],"sources":["../../../../../zql/src/planner/planner-connection.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport type {\n Condition,\n Ordering,\n SimpleCondition,\n} from '../../../zero-protocol/src/ast.ts';\nimport type {NoSubqueryCondition} from '../builder/filter.ts';\nimport {\n mergeConstraints,\n type PlannerConstraint,\n} from './planner-constraint.ts';\nimport type {PlanDebugger} from './planner-debug.ts';\nimport type {PlannerJoin} from './planner-join.ts';\nimport {omitFanout} from './planner-node.ts';\nimport type {\n CostEstimate,\n JoinOrConnection,\n PlannerNode,\n} from './planner-node.ts';\n\n/**\n * Represents a connection to a source (table scan).\n *\n * # Dual State Pattern\n * Like all planner nodes, PlannerConnection separates:\n * 1. immutable structure: Ordering, filters, cost model (set at construction)\n * 2. mutable state: Pinned status, constraints (mutated during planning)\n *\n * # Cost Estimation\n * The ordering and filters determine the initial cost. As planning progresses,\n * constraints from parent joins refine the cost estimate.\n *\n * # Constraint Flow\n * When a connection is pinned as the outer loop, it reveals constraints for\n * connected joins. These constraints propagate through the graph, allowing\n * other connections to update their cost estimates.\n *\n * Example:\n *\n * ```ts\n * builder.issue.whereExists('assignee', a => a.where('name', 'Alice'))\n * ```\n *\n * ```\n * [issue] [assignee]\n * | |\n * | +-- where name = 'Alice'\n * \\ /\n * \\ /\n * [join]\n * |\n * ```\n *\n * - Initial state: Both connections have no constraints, costs are unconstrained\n * - If `issue` chosen first: Reveals constraint `assignee_id` for assignee connection\n * - If `assignee` chosen first: Reveals constraint `assignee_id` for issue connection\n * - Updated costs guide the next selection\n *\n * # Pushed conditions\n * Correlated predicate pushdown copies a parent's condition on a correlation\n * column into the child (`pushed`). When the parent drives the join into the\n * child, the join binds that column to a parent row, so the parent's\n * condition implies the copy and the copy removes no rows. The connection\n * leaves such a copy out of the cost model call. Another join that binds the\n * column does not imply the copy, because its rows need not hold the parent's\n * condition. The connection also leaves every copy out of `selectivity`: a\n * semi-join always binds the column, and a flipped join gets the copy's\n * reduction through `returnedRows` instead.\n *\n * # Lifecycle\n * 1. Construct with immutable structure (ordering, filters, cost model)\n * 2. Wire to output node during graph construction\n * 3. Planning mutates pinned status and accumulates constraints\n * 4. reset() clears mutable state for replanning\n */\nexport class PlannerConnection {\n readonly kind = 'connection' as const;\n\n // ========================================================================\n // IMMUTABLE STRUCTURE (set during construction, never changes)\n // ========================================================================\n readonly #sort: Ordering;\n readonly #filters: Condition | undefined;\n readonly #pushed: ReadonlySet<SimpleCondition> | undefined;\n /** `#filters` without the pushed conditions. */\n readonly #unpushedFilters: Condition | undefined;\n readonly #model: ConnectionCostModel;\n readonly table: string;\n readonly name: string; // Human-readable name for debugging (defaults to table name)\n readonly #baseConstraints: PlannerConstraint | undefined; // Constraints from parent correlation\n readonly #baseLimit: number | undefined; // Original limit from query structure (never modified)\n readonly selectivity: number; // Fraction of rows passing filters (1.0 = no filtering)\n #output?: PlannerNode | undefined; // Set once during graph construction\n /**\n * The join from the parent into this connection's EXISTS subquery. Set once\n * during graph construction. The pushed conditions are copies of that\n * parent's conditions. Undefined for a root connection, whose parent (if\n * it is a `related` subquery) always drives it.\n */\n #parentJoin: PlannerJoin | undefined;\n\n // ========================================================================\n // MUTABLE PLANNING STATE (changes during plan search)\n // ========================================================================\n /**\n * Current limit during planning. Can be cleared (set to undefined) when a\n * parent join is flipped, indicating this connection is now in an outer loop\n * and should not be limited by EXISTS semantics.\n */\n limit: number | undefined;\n\n /**\n * Constraints accumulated from parent joins during planning.\n * Key is a path through the graph (e.g., \"0,1\" for branch pattern [0,1]).\n *\n * Undefined constraints are possible when a FO converts to UFO and only\n * a single join in the UFO is flipped - other branches report undefined.\n */\n readonly #constraints: Map<string, PlannerConstraint | undefined>;\n\n /**\n * Per-branch filters registered by `PlannerFilter` nodes (simple OR\n * branches) when the enclosing FanIn is in UFI mode. Key is the same\n * `branchPattern.join(',')` used by `#constraints`.\n *\n * These are AND-merged with `#filters` when calling the cost model so\n * the model sees the true filter that the runtime would push to the\n * source for this specific branch.\n */\n readonly #perBranchFilters: Map<string, NoSubqueryCondition> = new Map();\n\n readonly #isRoot: boolean;\n\n /**\n * Cached per-constraint costs to avoid redundant cost model calls.\n * Maps constraint key (branch pattern string) to computed cost.\n * Invalidated when constraints change.\n */\n #cachedConstraintCosts: Map<string, CostEstimate> = new Map();\n\n constructor(\n table: string,\n model: ConnectionCostModel,\n sort: Ordering,\n filters: Condition | undefined,\n isRoot: boolean,\n baseConstraints?: PlannerConstraint,\n limit?: number,\n name?: string,\n pushed?: ReadonlySet<SimpleCondition>,\n ) {\n this.table = table;\n this.name = name ?? table;\n this.#sort = sort;\n this.#filters = filters;\n this.#pushed = pushed;\n this.#unpushedFilters = pushed\n ? withoutConjuncts(filters, c => pushed.has(c))\n : filters;\n this.#model = model;\n this.#baseConstraints = baseConstraints;\n this.#baseLimit = limit;\n this.limit = limit;\n this.#constraints = new Map();\n this.#isRoot = isRoot;\n\n this.selectivity = this.#computeSelectivity(this.#unpushedFilters);\n }\n\n setOutput(node: PlannerNode): void {\n this.#output = node;\n }\n\n get output(): PlannerNode {\n assert(this.#output !== undefined, 'Output not set');\n return this.#output;\n }\n\n setParentJoin(join: PlannerJoin): void {\n this.#parentJoin = join;\n }\n\n closestJoinOrSource(): JoinOrConnection {\n return 'connection';\n }\n\n /**\n * Constraints are uniquely identified by their path through the\n * graph.\n *\n * FO represents all sub-joins as a single path.\n * UFO represents each sub-join as a separate path.\n * The first branch in a UFO will match the path of FO so no re-set needs to happen\n * when swapping from FO to UFO.\n *\n * FO swaps to UFO when a join inside FO-FI gets flipped.\n *\n * The max of the last element of the paths is the number of\n * root branches.\n */\n propagateConstraints(\n path: number[],\n c: PlannerConstraint | undefined,\n from?: PlannerNode,\n planDebugger?: PlanDebugger,\n ): void {\n const key = path.join(',');\n this.#constraints.set(key, c);\n // Constraints changed, invalidate cost caches\n this.#cachedConstraintCosts.clear();\n\n planDebugger?.log({\n type: 'node-constraint',\n nodeType: 'connection',\n node: this.name,\n branchPattern: path,\n constraint: c,\n from: from?.kind ?? 'unknown',\n });\n }\n\n /**\n * Register a per-branch filter for the given branch pattern. Called by\n * `PlannerFilter` (a simple OR branch) when the enclosing FanIn is in\n * UFI mode. The filter is AND-merged with `this.#filters` when the cost\n * model is invoked for this branch.\n */\n setPerBranchFilter(path: number[], filter: NoSubqueryCondition): void {\n this.#perBranchFilters.set(path.join(','), filter);\n // The cost depends on the filter, so invalidate caches.\n this.#cachedConstraintCosts.clear();\n }\n\n estimateCost(\n downstreamChildSelectivity: number,\n branchPattern: number[],\n planDebugger?: PlanDebugger,\n ): CostEstimate {\n // Branch pattern specified - return cost for this specific branch\n const key = branchPattern.join(',');\n\n // Check per-constraint cache first\n let cost = this.#cachedConstraintCosts.get(key);\n if (cost !== undefined) {\n return cost;\n }\n\n // Cache miss - compute and cache\n const constraint = this.#constraints.get(key);\n // Merge base constraints with propagated constraints\n const mergedConstraint = mergeConstraints(\n this.#baseConstraints,\n constraint,\n );\n // AND the per-branch filter (registered by a PlannerFilter for a\n // simple OR branch in UFI mode) with the connection-time filter so\n // the cost model sees the same effective filter the runtime applies.\n const perBranchFilter = this.#perBranchFilters.get(key);\n const {startupCost, fanout, rows} = this.#model(\n this.table,\n this.#sort,\n andFilters(this.#filtersFor(mergedConstraint), perBranchFilter),\n mergedConstraint,\n );\n const selectivity = perBranchFilter\n ? this.#computeSelectivity(\n andFilters(this.#unpushedFilters, perBranchFilter),\n )\n : this.selectivity;\n cost = {\n startupCost,\n scanEst:\n this.limit === undefined\n ? rows\n : Math.min(rows, this.limit / downstreamChildSelectivity),\n cost: 0,\n returnedRows: rows,\n selectivity,\n limit: this.limit,\n fanout,\n };\n this.#cachedConstraintCosts.set(key, cost);\n\n if (planDebugger) {\n planDebugger.log({\n type: 'node-cost',\n nodeType: 'connection',\n node: this.name,\n branchPattern,\n downstreamChildSelectivity,\n costEstimate: omitFanout(cost),\n filters: this.#filters,\n ordering: this.#sort,\n });\n }\n\n return cost;\n }\n\n /**\n * `#filters` without the pushed conditions that `constraint` binds the\n * column of, when the parent drives the join into this connection. The\n * runtime still applies them, but they remove no rows.\n *\n * When that join is flipped, this connection is the outer loop, so every\n * pushed condition counts, even where another flipped join binds its\n * column.\n */\n #filtersFor(\n constraint: PlannerConstraint | undefined,\n ): Condition | undefined {\n const pushed = this.#pushed;\n if (\n !pushed ||\n !constraint ||\n this.#unpushedFilters === this.#filters ||\n this.#parentJoin?.type === 'flipped'\n ) {\n return this.#filters;\n }\n return withoutConjuncts(\n this.#filters,\n c =>\n pushed.has(c) &&\n c.left.type === 'column' &&\n Object.hasOwn(constraint, c.left.name),\n );\n }\n\n #computeSelectivity(filters: Condition | undefined): number {\n if (this.#baseLimit === undefined || filters === undefined) {\n return 1.0;\n }\n\n const costWithFilters = this.#model(\n this.table,\n this.#sort,\n filters,\n undefined,\n );\n const costWithoutFilters = this.#model(\n this.table,\n this.#sort,\n undefined,\n undefined,\n );\n return costWithoutFilters.rows > 0\n ? costWithFilters.rows / costWithoutFilters.rows\n : 1.0;\n }\n\n /**\n * Remove the limit from this connection.\n * Called when a parent join is flipped, making this connection part of an\n * outer loop that should produce all rows rather than stopping at the limit.\n */\n unlimit(): void {\n if (this.#isRoot) {\n // We cannot unlimit root connections\n return;\n }\n if (this.limit !== undefined) {\n this.limit = undefined;\n // Limit changes do not impact connection costs.\n // Limit is taken into account at the join level.\n // Given that, we do not need to invalidate cost caches here.\n }\n }\n\n /**\n * Propagate unlimiting when a parent join is flipped.\n * For connections, we simply remove the limit.\n */\n propagateUnlimitFromFlippedJoin(): void {\n this.unlimit();\n }\n\n reset() {\n this.#constraints.clear();\n this.#perBranchFilters.clear();\n this.limit = this.#baseLimit;\n // Clear all cost caches\n this.#cachedConstraintCosts.clear();\n }\n\n /**\n * Capture constraint state for snapshotting.\n * Used by PlannerGraph to save/restore planning state.\n */\n captureConstraints(): Map<string, PlannerConstraint | undefined> {\n return new Map(this.#constraints);\n }\n\n /**\n * Restore constraint state from a snapshot.\n * Used by PlannerGraph to restore planning state.\n *\n * Per-branch filters are not snapshotted — they are re-derived by the\n * subsequent `propagateConstraints` pass. Clearing them here ensures\n * that any entries left over from the previous iteration don't linger\n * in case the restored plan registers a different set.\n */\n restoreConstraints(\n constraints: Map<string, PlannerConstraint | undefined>,\n ): void {\n this.#constraints.clear();\n this.#perBranchFilters.clear();\n for (const [key, value] of constraints) {\n this.#constraints.set(key, value);\n }\n // Constraints changed, invalidate cost caches\n this.#cachedConstraintCosts.clear();\n }\n\n /** Get current constraints for debugging. */\n getConstraintsForDebug(): Record<string, PlannerConstraint | undefined> {\n const record: Record<string, PlannerConstraint | undefined> = {};\n for (const [key, value] of this.#constraints) {\n record[key] = value;\n }\n return record;\n }\n\n /** Get filters for debugging. */\n getFiltersForDebug(): Condition | undefined {\n return this.#filters;\n }\n\n /** Get sort/ordering for debugging. */\n getSortForDebug(): Ordering {\n return this.#sort;\n }\n\n /** Get estimated cost for each constraint branch. */\n getConstraintCostsForDebug(): Record<string, CostEstimate> {\n const record: Record<string, CostEstimate> = {};\n for (const [key, value] of this.#cachedConstraintCosts) {\n record[key] = value;\n }\n return record;\n }\n}\n\nfunction andFilters(\n a: Condition | undefined,\n b: Condition | undefined,\n): Condition | undefined {\n return a && b ? {type: 'and', conditions: [a, b]} : (a ?? b);\n}\n\n/**\n * `c` without the simple conjuncts, including those of nested ANDs, for which\n * `remove` returns true. Returns `c` itself when it removes nothing.\n */\nfunction withoutConjuncts(\n c: Condition | undefined,\n remove: (c: SimpleCondition) => boolean,\n): Condition | undefined {\n switch (c?.type) {\n case 'simple':\n return remove(c) ? undefined : c;\n case 'and': {\n const conditions: Condition[] = [];\n for (const x of c.conditions) {\n const y = withoutConjuncts(x, remove);\n if (y !== undefined) {\n conditions.push(y);\n }\n }\n if (\n conditions.length === c.conditions.length &&\n conditions.every((x, i) => x === c.conditions[i])\n ) {\n return c;\n }\n if (conditions.length <= 1) {\n return conditions[0];\n }\n return {type: 'and', conditions};\n }\n default:\n return c;\n }\n}\n\ntype FanoutEst = {\n fanout: number;\n confidence: 'high' | 'med' | 'none';\n};\nexport type FanoutCostModel = (columns: string[]) => FanoutEst;\n\nexport type CostModelCost = {\n startupCost: number;\n rows: number;\n fanout: FanoutCostModel;\n};\nexport type ConnectionCostModel = (\n table: string,\n sort: Ordering,\n filters: Condition | undefined,\n constraint: Plann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AAO,KAAK,IAAI;EAAC,MAAM;EAAO,YAAY,CAAC,GAAG,CAAC;CAAC,IAAK,KAAK;AAC5D;;;;;AAMA,SAAS,iBACP,GACA,QACuB;CACvB,QAAQ,GAAG,MAAX;EACE,KAAK,UACH,OAAO,OAAO,CAAC,IAAI,KAAA,IAAY;EACjC,KAAK,OAAO;GACV,MAAM,aAA0B,CAAC;GACjC,KAAK,MAAM,KAAK,EAAE,YAAY;IAC5B,MAAM,IAAI,iBAAiB,GAAG,MAAM;IACpC,IAAI,MAAM,KAAA,GACR,WAAW,KAAK,CAAC;GAErB;GACA,IACE,WAAW,WAAW,EAAE,WAAW,UACnC,WAAW,OAAO,GAAG,MAAM,MAAM,EAAE,WAAW,EAAE,GAEhD,OAAO;GAET,IAAI,WAAW,UAAU,GACvB,OAAO,WAAW;GAEpB,OAAO;IAAC,MAAM;IAAO;GAAU;EACjC;EACA,SACE,OAAO;CACX;AACF"}
|
|
@@ -74,7 +74,7 @@ export type BestPlanSelectedEvent = v.Infer<typeof bestPlanSelectedEventJSONSche
|
|
|
74
74
|
export type NodeCostEvent = {
|
|
75
75
|
type: 'node-cost';
|
|
76
76
|
attemptNumber?: number;
|
|
77
|
-
nodeType: 'connection' | 'join' | 'fan-out' | 'fan-in' | 'terminus';
|
|
77
|
+
nodeType: 'connection' | 'join' | 'fan-out' | 'fan-in' | 'filter' | 'terminus';
|
|
78
78
|
node: string;
|
|
79
79
|
branchPattern: number[];
|
|
80
80
|
downstreamChildSelectivity: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"planner-debug.d.ts","sourceRoot":"","sources":["../../../../../zql/src/planner/planner-debug.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"planner-debug.d.ts","sourceRoot":"","sources":["../../../../../zql/src/planner/planner-debug.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,CAAC,MAAM,+BAA+B,CAAC;AACxD,OAAO,KAAK,EACV,2BAA2B,EAC3B,+BA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,MAAM,mBAAmB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,6BAA6B,CAAC,CAAC;AAEhF;;GAEG;AACH,MAAM,MAAM,cAAc,GACtB,iBAAiB,GACjB,oBAAoB,GACpB,uBAAuB,GACvB,0BAA0B,GAC1B,iBAAiB,GACjB,eAAe,GACf,qBAAqB,GACrB,aAAa,GACb,mBAAmB,CAAC;AAExB;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,GAAG,CAAC,KAAK,EAAE,cAAc,GAAG,IAAI,CAAC;CAClC;AAED;;;GAGG;AACH,qBAAa,mBAAoB,YAAW,YAAY;IACtD,QAAQ,CAAC,MAAM,EAAE,cAAc,EAAE,CAAM;IACvC,OAAO,CAAC,cAAc,CAAK;IAE3B,GAAG,CAAC,KAAK,EAAE,cAAc,GAAG,IAAI;IAehC;;OAEG;IACH,SAAS,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,CAAC,EACxC,IAAI,EAAE,CAAC,GACN,OAAO,CAAC,cAAc,EAAE;QAAC,IAAI,EAAE,CAAC,CAAA;KAAC,CAAC,EAAE;IAOvC;;OAEG;IACH,MAAM,IAAI,MAAM;CAGjB;AA2QD;;;;GAIG;AACH,wBAAgB,wBAAwB,CACtC,MAAM,EAAE,cAAc,EAAE,GACvB,kBAAkB,EAAE,CAEtB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,kBAAkB,EAAE,GAAG,cAAc,EAAE,GAC9C,MAAM,CAoDR"}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { getOrInsertComputed } from "../../../shared/src/map.js";
|
|
2
|
+
import { newArray } from "../../../shared/src/arrays.js";
|
|
1
3
|
//#region ../zql/src/planner/planner-debug.ts
|
|
2
4
|
/**
|
|
3
5
|
* Simple accumulator debugger that stores all events.
|
|
@@ -187,14 +189,7 @@ function formatPlannerEvents(events) {
|
|
|
187
189
|
let bestPlanEvent;
|
|
188
190
|
for (const event of events) if ("attemptNumber" in event) {
|
|
189
191
|
const attempt = event.attemptNumber;
|
|
190
|
-
if (attempt !== void 0)
|
|
191
|
-
let attemptEvents = eventsByAttempt.get(attempt);
|
|
192
|
-
if (!attemptEvents) {
|
|
193
|
-
attemptEvents = [];
|
|
194
|
-
eventsByAttempt.set(attempt, attemptEvents);
|
|
195
|
-
}
|
|
196
|
-
attemptEvents.push(event);
|
|
197
|
-
}
|
|
192
|
+
if (attempt !== void 0) getOrInsertComputed(eventsByAttempt, attempt, newArray).push(event);
|
|
198
193
|
} else if (event.type === "best-plan-selected") bestPlanEvent = event;
|
|
199
194
|
for (const [attemptNum, events] of eventsByAttempt.entries()) {
|
|
200
195
|
lines.push(...formatAttemptSummary(attemptNum, events));
|