Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/SDK_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ query = (
)
```

### Multiple Filters on the Same Field

You can apply more than one condition to the same field—for example, to constrain
a date dimension to a range:

```python
query = model.query().filter(
(model.dimensions.order_date >= "2026-01-01") &
(model.dimensions.order_date <= "2026-01-31")
)
```

The same works across separate `.filter()` calls, since they are AND-ed together:

```python
query = (
model.query()
.filter(model.dimensions.order_date >= "2026-01-01")
.filter(model.dimensions.order_date <= "2026-01-31")
)
```

---

## Dimensions and Metrics
Expand Down
9 changes: 2 additions & 7 deletions lightdash/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,12 @@ def __post_init__(self):

def to_dict(self):
out = []
processed_field_ids = set()
for f in self.filters:
# Check that the filter is not a composite filter
if not hasattr(f, "field"):
raise TypeError("Multi-level filter composites not supported yet")
# Check that we have at most one filter per field
if f.field.field_id in processed_field_ids:
raise NotImplementedError(
f"Multiple filters for field {f.field.field_id} not implemented yet"
)
processed_field_ids.add(f.field.field_id)
# Multiple filters may target the same field, e.g. a date range
# expressed as (dim >= start) & (dim <= end).
out.append(f.to_dict())
return {"dimensions": {self.aggregation: out}}

Expand Down
50 changes: 50 additions & 0 deletions tests/test_filter_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,56 @@ def test_chain_or_filters(self, dimension):
assert len(result.filters) == 3


class TestSameFieldFilters:
"""Test multiple filters on the same field (issue #20)."""

def test_range_filter_on_same_field(self, dimension):
"""Test (dim >= x) & (dim <= y) serializes both rules."""
composite = (dimension >= "2026-01-01") & (dimension <= "2026-01-31")
result = composite.to_dict()
rules = result["dimensions"]["and"]
assert len(rules) == 2
assert rules[0]["target"]["fieldId"] == "test_model_country"
assert rules[0]["operator"] == "greaterThanOrEqual"
assert rules[0]["values"] == ["2026-01-01"]
assert rules[1]["target"]["fieldId"] == "test_model_country"
assert rules[1]["operator"] == "lessThanOrEqual"
assert rules[1]["values"] == ["2026-01-31"]

def test_or_filters_on_same_field(self, dimension):
"""Test (dim == a) | (dim == b) serializes both rules."""
composite = (dimension == "USA") | (dimension == "UK")
rules = composite.to_dict()["dimensions"]["or"]
assert len(rules) == 2
assert all(r["target"]["fieldId"] == "test_model_country" for r in rules)

def test_three_filters_on_same_field(self, dimension2):
"""Test chaining more than two filters on the same field."""
composite = (dimension2 > 0) & (dimension2 < 100) & (dimension2 != 50)
rules = composite.to_dict()["dimensions"]["and"]
assert len(rules) == 3

def test_same_field_via_query_filter_chain(self):
"""Two .filter() calls on the same field are both serialized."""
from lightdash.models import Model

model = Model(
name="test_model", type="default",
database_name="db", schema_name="s",
)
order_date = Dimension(name="order_date", model_name="test_model")
query = (
model.query()
.filter(order_date >= "2026-01-01")
.filter(order_date <= "2026-01-31")
)
rules = query._build_payload()["filters"]["dimensions"]["and"]
assert len(rules) == 2
assert {r["operator"] for r in rules} == {
"greaterThanOrEqual", "lessThanOrEqual"
}


class TestBackwardsCompatibility:
"""Test that original filter constructors still work."""

Expand Down