Skip to content

Commit

Permalink
fix: convert range back to lookup (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
barakalon authored Jul 20, 2024
1 parent afdad41 commit 043b4d6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
9 changes: 6 additions & 3 deletions odex/optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,15 @@ def transform(self, plan: Plan, ctx: Context) -> Plan:
else:
new_range = combined

inputs.append(
IndexRange(
if new_range.is_equality():
assert isinstance(new_range.left, Bound)
new_plan: Plan = IndexLookup(index=index, value=new_range.left.value)
else:
new_plan = IndexRange(
index=index,
range=new_range,
)
)
inputs.append(new_plan)

inputs.extend(others)

Expand Down
10 changes: 10 additions & 0 deletions odex/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ def combine(self, other: "Range[C]") -> "Optional[Range[C]]":
right=right,
)

def is_equality(self) -> bool:
"""Determine if a range can be treated like an equality"""
return (
isinstance(self.left, Bound)
and isinstance(self.right, Bound)
and self.left.inclusive
and self.right.inclusive
and self.left == self.right
)

def _combine_bounds(
self,
a: OptionalBound,
Expand Down
12 changes: 11 additions & 1 deletion tests/fixtures/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ setups:
- ScanFilter: a >= 3
- ScanFilter: a <= 3
optimized_plan: |-
IndexRange: 3 <= SortedDictIndex(a) <= 3
IndexLookup: SortedDictIndex(a) = 3
result:
- 2
- title: Conflicting equalities
Expand Down Expand Up @@ -260,6 +260,16 @@ setups:
IndexLookup: HashIndex(a) = 'foo'
result:
- 0
- title: Duplicate String equality
condition: a = 'foo' AND a = 'foo'
plan: |-
Intersect
- ScanFilter: a = 'foo'
- ScanFilter: a = 'foo'
optimized_plan: |-
IndexLookup: HashIndex(a) = 'foo'
result:
- 0
- title: Conflicting equalities, string
condition: a = 'foo' AND a = 'bar'
plan: |-
Expand Down

0 comments on commit 043b4d6

Please sign in to comment.