From 449899abdb36b4af19a906d4509fe662e658ca8c Mon Sep 17 00:00:00 2001 From: SpookyJumpyBeans <116844292+SpookyJumpyBeans@users.noreply.github.com> Date: Sun, 4 Oct 2026 00:06:54 -0400 Subject: [PATCH] Correct two notes that predate the cost model and TopK The README still said there is no cost-based optimizer, though join order has come from a cost model since the join-reordering change. The Sort docstring said LIMIT pays for a full sort because there is no heap, but the planner uses TopK whenever ORDER BY comes with LIMIT. --- README.md | 2 +- tinyquery/operators.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7755eda..eff2aa4 100644 --- a/README.md +++ b/README.md @@ -256,7 +256,7 @@ SQL `ORDER BY` alone uses a blocking `Sort` (memory O(n)). `ORDER BY` + `LIMIT` becomes `TopK`: still one pass over the child, but the heap only keeps k rows. `LIMIT` alone just stops after n rows. -The planner splits `WHERE` on `AND` and attaches each piece to the lowest input whose columns can resolve it. That is the same idea as predicate pushdown in Spark; there is no cost-based optimizer. +The planner splits `WHERE` on `AND` and attaches each piece to the lowest input whose columns can resolve it. That is the same idea as predicate pushdown in Spark. Join order is the one cost-based decision: the filtered row counts from pushdown feed the Selinger search described above. ## Example diff --git a/tinyquery/operators.py b/tinyquery/operators.py index 3355c1b..8bc5a40 100644 --- a/tinyquery/operators.py +++ b/tinyquery/operators.py @@ -428,8 +428,8 @@ def __eq__(self, other: object) -> bool: class Sort(Operator): """Blocking sort: read the child fully, then emit in order. - LIMIT after this still pays for the full sort. A real engine would keep a heap - of size LIMIT; we do not. + Memory is O(n). The planner only uses this for ORDER BY without LIMIT; with a + LIMIT it picks TopK, which keeps a heap of k rows instead. """ def __init__(self, child: Operator, keys: list[Expr], descending: list[bool]) -> None: