Skip to content

Commit

Permalink
feat: short circuit TRUE and FALSE conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
barakalon committed May 15, 2024
1 parent b84e320 commit 6278e6d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
13 changes: 7 additions & 6 deletions odex/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,8 @@ def __init__(
self.update(self.objs)

self.executors: Dict[Type[Plan], Callable[[Plan], Set[T]]] = {
ScanFilter: lambda plan: {o for o in self.objs if self.match(plan.condition, o)}, # type: ignore
Filter: lambda plan: {
o
for o in self.execute(plan.input) # type: ignore
if self.match(plan.condition, o) # type: ignore
},
ScanFilter: lambda plan: self._execute_filter(self.objs, plan.condition), # type: ignore
Filter: lambda plan: self._execute_filter(self.execute(plan.input), plan.condition), # type: ignore
Union: lambda plan: set.union(*(self.execute(i) for i in plan.inputs)), # type: ignore
Intersect: lambda plan: intersect(*(self.execute(i) for i in plan.inputs)), # type: ignore
IndexLookup: lambda plan: plan.index.lookup(plan.value), # type: ignore
Expand Down Expand Up @@ -254,3 +250,8 @@ def _iter_indexes(self) -> Iterator[Index]:
for indexes in self.indexes.values():
for index in indexes:
yield index

def _execute_filter(self, objs: Set[T], condition: Condition) -> Set[T]:
if isinstance(condition, Literal):
return objs if condition.value else set()
return {o for o in objs if self.match(condition, o)}
17 changes: 17 additions & 0 deletions tests/fixtures/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ setups:
result:
- 0
- 1
- title: TRUE filter
condition: 'TRUE'
plan: |-
ScanFilter: True
optimized_plan: |-
ScanFilter: True
result:
- 0
- 1
- 2
- title: FALSE filter
condition: 'FALSE'
plan: |-
ScanFilter: False
optimized_plan: |-
ScanFilter: False
result: []
- objects:
- a: 1
b: 2
Expand Down

0 comments on commit 6278e6d

Please sign in to comment.