lance icon indicating copy to clipboard operation
lance copied to clipboard

bug: scan with order_by and limit=0 panics with "assertion failed: k > 0"

Open aimanmalib opened this issue 3 weeks ago • 0 comments

Description

A scan with order_by and limit=0 panics in a background thread (assertion failed: k > 0 in DataFusion's topk/mod.rs) and the caller gets RuntimeError: Task was aborted. The same scan without order_by returns 0 rows, and limit=0, offset=2 with order_by also returns 0 rows. Adding a filter or a full-text query does not change it.

The scanner plans SortExec and then GlobalLimitExec(fetch=0) (rust/lance/src/dataset/scanner/plan.rs). The physical optimizer pushes the zero fetch into the sort, and DataFusion's TopK asserts k > 0. A zero limit became a real limit node in #9111, so order_by with limit=0 now reaches this path.

Steps to reproduce

import tempfile
import lance
import pyarrow as pa
from lance.dataset import ColumnOrdering

ds = lance.write_dataset(pa.table({"id": list(range(10))}), tempfile.mkdtemp())

print(ds.to_table(limit=0).num_rows)  # 0
print(ds.to_table(limit=0, offset=2, order_by=[ColumnOrdering("id")]).num_rows)  # 0
ds.to_table(limit=0, order_by=[ColumnOrdering("id")])
# thread 'lance_background_thread' panicked at .../datafusion-physical-plan-55.2.0/src/topk/mod.rs:881:9:
# assertion failed: k > 0
# RuntimeError: Task was aborted

to_batches, a filter, a projected columns list and full_text_query with the same order_by + limit=0 all fail the same way.

Expected behavior

An empty result, the same as limit=0 without order_by.

Lance version

pylance 15.0.0-beta.17 (471352665). The planner code is unchanged on main fbfea4f8c.

I'd like to fix this (skip the sort when the limit is zero) and will open a PR with a regression test.

aimanmalib avatar Nov 11 '26 17:11 aimanmalib