Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten AND and OR conditions #173

Merged
merged 3 commits into from
Jan 19, 2024
Merged
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
57 changes: 49 additions & 8 deletions src/aiodynamo/expressions.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@
Any,
Callable,
Dict,
Generator,
Iterable,
Iterator,
List,
Optional,
@@ -377,10 +379,24 @@ def encode(self, params: Parameters) -> str:

class Condition(metaclass=abc.ABCMeta):
def __and__(self, other: Condition) -> Condition:
return AndCondition(self, other)
if isinstance(self, AndCondition):
if isinstance(other, AndCondition):
return AndCondition(self.children.extending(other.children))
else:
return AndCondition(self.children.appending(other))
elif isinstance(other, AndCondition):
return AndCondition(other.children.prepending(self))
return AndCondition(SubConditions.create(self, other))

def __or__(self, other: Condition) -> Condition:
return OrCondition(self, other)
if isinstance(self, OrCondition):
if isinstance(other, OrCondition):
return OrCondition(self.children.extending(other.children))
else:
return OrCondition(self.children.appending(other))
elif isinstance(other, OrCondition):
return OrCondition(other.children.prepending(self))
return OrCondition(SubConditions.create(self, other))

def __invert__(self) -> Condition:
return NotCondition(self)
@@ -408,20 +424,18 @@ def encode(self, params: Parameters) -> str:

@dataclass(frozen=True)
class AndCondition(Condition):
lhs: Condition
rhs: Condition
children: SubConditions

def encode(self, params: Parameters) -> str:
return f"({self.lhs.encode(params)} AND {self.rhs.encode(params)})"
return "(" + " AND ".join(child.encode(params) for child in self.children) + ")"


@dataclass(frozen=True)
class OrCondition(Condition):
lhs: Condition
rhs: Condition
children: SubConditions

def encode(self, params: Parameters) -> str:
return f"({self.lhs.encode(params)} OR {self.rhs.encode(params)})"
return "(" + " OR ".join(child.encode(params) for child in self.children) + ")"


@dataclass(frozen=True)
@@ -659,3 +673,30 @@ def __and__(self, field: F) -> ProjectionExpression:

def encode(self, params: Parameters) -> str:
return ",".join(params.encode_path(field.path) for field in self.fields)


@dataclass(frozen=True)
class SubConditions:
first: Condition
second: Condition
rest: tuple[Condition, ...]

@classmethod
def create(
cls, first: Condition, second: Condition, *rest: Condition
) -> SubConditions:
return cls(first, second, rest)

def prepending(self, value: Condition) -> SubConditions:
return SubConditions(value, self.first, (self.second, *self.rest))

def appending(self, value: Condition) -> SubConditions:
return SubConditions(self.first, self.second, (*self.rest, value))

def extending(self, values: Iterable[Condition]) -> SubConditions:
return SubConditions(self.first, self.second, (*self.rest, *values))

def __iter__(self) -> Generator[Condition, None, None]:
yield self.first
yield self.second
yield from self.rest
115 changes: 115 additions & 0 deletions tests/unit/test_expressions.py
Original file line number Diff line number Diff line change
@@ -3,11 +3,15 @@
import pytest

from aiodynamo.expressions import (
AndCondition,
Comparison,
Condition,
F,
HashKey,
OrCondition,
Parameters,
ProjectionExpression,
SubConditions,
UpdateExpression,
)

@@ -115,6 +119,10 @@ def test_f_repr(f: F, r: str) -> None:
[
(F("a").equals(True) & F("b").gt(1), "(a = True AND b > 1)"),
(F("a", 1).begins_with("foo"), "begins_with(a[1], 'foo')"),
(
F("a").equals("a") & F("b").equals("b") & F("c").equals("c"),
"(a = 'a' AND b = 'b' AND c = 'c')",
),
],
)
def test_condition_debug(expr: Condition, expected: str) -> None:
@@ -133,3 +141,110 @@ def test_condition_debug(expr: Condition, expected: str) -> None:
)
def test_update_expression_debug(expr: UpdateExpression, expected: str) -> None:
assert expr.debug(int) == expected


@pytest.mark.parametrize(
"expr,expected",
[
(
F("a").equals("a") & F("b").equals("b"),
AndCondition(
SubConditions.create(
Comparison(F("a"), "=", "a"), Comparison(F("b"), "=", "b")
)
),
),
(
(F("a").equals("a") & F("b").equals("b")) & F("c").equals("c"),
AndCondition(
SubConditions.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
)
),
),
(
F("a").equals("a") & (F("b").equals("b") & F("c").equals("c")),
AndCondition(
SubConditions.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
)
),
),
(
(F("a").equals("a") & F("b").equals("b"))
& (F("c").equals("c") & F("d").equals("d")),
AndCondition(
SubConditions.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
Comparison(F("d"), "=", "d"),
)
),
),
(
F("a").equals("a") | F("b").equals("b"),
OrCondition(
SubConditions.create(
Comparison(F("a"), "=", "a"), Comparison(F("b"), "=", "b")
)
),
),
(
(F("a").equals("a") | F("b").equals("b")) | F("c").equals("c"),
OrCondition(
SubConditions.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
)
),
),
(
F("a").equals("a") | (F("b").equals("b") | F("c").equals("c")),
OrCondition(
SubConditions.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
)
),
),
(
(F("a").equals("a") | F("b").equals("b"))
| (F("c").equals("c") | F("d").equals("d")),
OrCondition(
SubConditions.create(
Comparison(F("a"), "=", "a"),
Comparison(F("b"), "=", "b"),
Comparison(F("c"), "=", "c"),
Comparison(F("d"), "=", "d"),
)
),
),
(
(F("a").equals("a") | F("b").equals("b"))
& (F("c").equals("c") | F("d").equals("d")),
AndCondition(
SubConditions.create(
OrCondition(
SubConditions.create(
Comparison(F("a"), "=", "a"), Comparison(F("b"), "=", "b")
)
),
OrCondition(
SubConditions.create(
Comparison(F("c"), "=", "c"), Comparison(F("d"), "=", "d")
)
),
)
),
),
],
)
def test_condition_flattening(expr: Condition, expected: Condition) -> None:
assert expr == expected