mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
`PyarrowDatasetAdapter` lets DuckDB query a LanceDB table, but DuckDB pushes `WHERE` clauses into `Dataset.scanner(filter=...)` as `pyarrow.compute.Expression` objects. The adapter forwarded them to `Table.count_rows` and `LanceQueryBuilder.where`, which accept only SQL strings, so every filtered DuckDB query failed with `TypeError: argument 'filter': 'Expression' object is not an instance of 'str'`. This change: - Applies a `pyarrow.compute.Expression` filter locally with `pyarrow.dataset.Scanner.from_batches`. The scan reads all columns and pyarrow applies the filter and projection, because DuckDB can filter on a column it does not project. - Applies `head` limits after the filter, so the limit counts matching rows. - Routes `PyarrowDatasetAdapter.count_rows(filter)` through the scanner, so it accepts both filter types. SQL string filters keep the existing path and still run inside LanceDB. Fixes #4591 ### Testing - New `test_dataset_adapter_expression_filter` in `test_pyarrow.py` covers `count_rows`, `to_table`, `head` and a projection that excludes the filtered column. - New `test_query_with_filter` in `test_duckdb.py` runs DuckDB queries with a `WHERE` clause. - Both tests fail without the fix and pass with it. `ruff check` and `ruff format --check` pass.
68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
|
|
|
import pyarrow as pa
|
|
import pyarrow.compute as pc
|
|
|
|
import lancedb
|
|
from lancedb.integrations.pyarrow import PyarrowDatasetAdapter
|
|
|
|
|
|
def test_dataset_adapter(tmp_path):
|
|
data = pa.table({"x": [1, 2, 3, 4], "y": [5, 6, 7, 8]})
|
|
conn = lancedb.connect(tmp_path)
|
|
tbl = conn.create_table("test", data)
|
|
|
|
adapter = PyarrowDatasetAdapter(tbl)
|
|
|
|
assert adapter.count_rows() == 4
|
|
assert adapter.count_rows("x > 2") == 2
|
|
assert adapter.schema == data.schema
|
|
assert adapter.head(2) == data.slice(0, 2)
|
|
assert adapter.to_table() == data
|
|
assert adapter.to_batches().read_all() == data
|
|
assert adapter.scanner().to_table() == data
|
|
assert adapter.scanner().to_batches().read_all() == data
|
|
|
|
assert adapter.scanner().projected_schema == data.schema
|
|
assert adapter.scanner(columns=["x"]).projected_schema == pa.schema(
|
|
[data.schema.field("x")]
|
|
)
|
|
assert adapter.scanner(columns=["x"]).to_table() == pa.table({"x": [1, 2, 3, 4]})
|
|
|
|
# Make sure we bypass the limit
|
|
data = pa.table({"x": range(100)})
|
|
tbl = conn.create_table("test2", data)
|
|
|
|
adapter = PyarrowDatasetAdapter(tbl)
|
|
|
|
assert adapter.count_rows() == 100
|
|
assert adapter.to_table().num_rows == 100
|
|
assert adapter.head(10).num_rows == 10
|
|
|
|
# Empty table
|
|
tbl = conn.create_table("test3", None, schema=pa.schema({"x": pa.int64()}))
|
|
adapter = PyarrowDatasetAdapter(tbl)
|
|
|
|
assert adapter.count_rows() == 0
|
|
assert adapter.to_table().num_rows == 0
|
|
assert adapter.head(10).num_rows == 0
|
|
|
|
assert adapter.scanner().projected_schema == pa.schema({"x": pa.int64()})
|
|
|
|
|
|
def test_dataset_adapter_expression_filter(tmp_path):
|
|
data = pa.table({"x": [1, 2, 3, 4], "y": [5, 6, 7, 8]})
|
|
conn = lancedb.connect(tmp_path)
|
|
tbl = conn.create_table("test", data)
|
|
|
|
adapter = PyarrowDatasetAdapter(tbl)
|
|
filt = pc.field("x") > 2
|
|
|
|
assert adapter.count_rows(filt) == 2
|
|
assert adapter.to_table(filter=filt) == data.slice(2, 2)
|
|
assert adapter.head(1, filter=filt) == data.slice(2, 1)
|
|
# The filter can reference a column that is not projected
|
|
assert adapter.to_table(columns=["y"], filter=filt) == pa.table({"y": [7, 8]})
|
|
assert adapter.scanner(filter=filt).count_rows() == 2
|