|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | +from sqlalchemy import Column, Integer, MetaData, Table, Text, select, text |
| 5 | +from sqlalchemy.dialects.postgresql import INT4RANGE |
| 6 | +from sqlalchemy.orm import Session |
| 7 | + |
| 8 | +from conftest import assert_uses_paradedb_scan |
| 9 | +from paradedb.sqlalchemy import search |
| 10 | + |
| 11 | + |
| 12 | +pytestmark = pytest.mark.integration |
| 13 | + |
| 14 | + |
| 15 | +def test_range_query_with_op_and_all_predicate(engine): |
| 16 | + metadata = MetaData() |
| 17 | + items = Table( |
| 18 | + "range_items", |
| 19 | + metadata, |
| 20 | + Column("id", Integer, primary_key=True), |
| 21 | + Column("description", Text, nullable=False), |
| 22 | + Column("weight_range", INT4RANGE, nullable=False), |
| 23 | + ) |
| 24 | + |
| 25 | + with engine.begin() as conn: |
| 26 | + conn.execute(text("DROP INDEX IF EXISTS range_items_bm25_idx")) |
| 27 | + conn.execute(text("DROP TABLE IF EXISTS range_items")) |
| 28 | + metadata.create_all(engine) |
| 29 | + |
| 30 | + with engine.begin() as conn: |
| 31 | + conn.execute( |
| 32 | + text( |
| 33 | + "CREATE INDEX range_items_bm25_idx ON range_items USING bm25 (id, description, weight_range) WITH (key_field='id')" |
| 34 | + ) |
| 35 | + ) |
| 36 | + conn.execute( |
| 37 | + text( |
| 38 | + """ |
| 39 | + INSERT INTO range_items (id, description, weight_range) |
| 40 | + VALUES |
| 41 | + (1, 'Ergonomic camera strap', int4range(1, 8)), |
| 42 | + (2, 'Mechanical keyboard', int4range(8, 15)), |
| 43 | + (3, 'Running shoes', int4range(3, 10)) |
| 44 | + """ |
| 45 | + ) |
| 46 | + ) |
| 47 | + |
| 48 | + try: |
| 49 | + with Session(engine) as session: |
| 50 | + stmt = ( |
| 51 | + select(items.c.id) |
| 52 | + .where(items.c.weight_range.op("@>")(5)) |
| 53 | + .where(search.all(items.c.id)) |
| 54 | + .order_by(items.c.id) |
| 55 | + ) |
| 56 | + assert_uses_paradedb_scan(session, stmt, index_name="range_items_bm25_idx") |
| 57 | + ids = list(session.scalars(stmt)) |
| 58 | + assert ids == [1, 3] |
| 59 | + |
| 60 | + stmt_with_text = ( |
| 61 | + select(items.c.id) |
| 62 | + .where(items.c.weight_range.op("@>")(5)) |
| 63 | + .where(search.match_any(items.c.description, "running", "camera")) |
| 64 | + .order_by(items.c.id) |
| 65 | + ) |
| 66 | + assert_uses_paradedb_scan(session, stmt_with_text, index_name="range_items_bm25_idx") |
| 67 | + ids_with_text = list(session.scalars(stmt_with_text)) |
| 68 | + assert ids_with_text == [1, 3] |
| 69 | + finally: |
| 70 | + with engine.begin() as conn: |
| 71 | + conn.execute(text("DROP INDEX IF EXISTS range_items_bm25_idx")) |
| 72 | + conn.execute(text("DROP TABLE IF EXISTS range_items")) |
0 commit comments