Skip to content

Commit

Permalink
Change the parsing logic for constraint expressions
Browse files Browse the repository at this point in the history
The original way we used pyparsing didn't make proper use of its features. This rewrites all constraint parsing to make use of setParseAction and uses infixNotation to handle the nested expressions. It should be a transparent change that has no effect of functionality.
  • Loading branch information
stevearc committed Oct 19, 2020
1 parent 03cfea1 commit e245229
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 187 deletions.
14 changes: 6 additions & 8 deletions dql/engine.py
Expand Up @@ -444,8 +444,8 @@ def _build_query(self, table, tree, visitor):
if tree.using:
index_name = kwargs["index"] = tree.using[1]
index = table.get_index(index_name)
if tree.where:
constraints = ConstraintExpression.from_where(tree.where)
if isinstance(tree.where, ConstraintExpression):
constraints = tree.where
possible_hash = constraints.possible_hash_fields()
possible_range = constraints.possible_range_fields()
if index is None:
Expand Down Expand Up @@ -705,9 +705,8 @@ def _delete(self, tree):
table = self.describe(tablename, require=True)
kwargs = {}
visitor = Visitor(self.reserved_words)
if tree.where:
constraints = ConstraintExpression.from_where(tree.where)
kwargs["condition"] = constraints.build(visitor)
if isinstance(tree.where, ConstraintExpression):
kwargs["condition"] = tree.where.build(visitor)
kwargs["expr_values"] = visitor.expression_values
kwargs["alias"] = visitor.attribute_names
return self._query_and_op(tree, table, "delete_item", kwargs)
Expand All @@ -726,9 +725,8 @@ def _update(self, tree):
visitor = Visitor(self.reserved_words)
updates = UpdateExpression.from_update(tree.update)
kwargs["expression"] = updates.build(visitor)
if tree.where:
constraints = ConstraintExpression.from_where(tree.where)
kwargs["condition"] = constraints.build(visitor)
if isinstance(tree.where, ConstraintExpression):
kwargs["condition"] = tree.where.build(visitor)
kwargs["expr_values"] = visitor.expression_values
kwargs["alias"] = visitor.attribute_names
return self._query_and_op(tree, table, "update_item", kwargs)
Expand Down
12 changes: 12 additions & 0 deletions dql/expressions/base.py
Expand Up @@ -47,6 +47,12 @@ def evaluate(self, item):
return None
return item

def __hash__(self):
return hash(self.field)

def __eq__(self, other):
return isinstance(other, Field) and self.field == other.field


class Value(Expression):

Expand All @@ -61,3 +67,9 @@ def build(self, visitor):
def evaluate(self, item):
""" Values evaluate to themselves regardless of the item """
return self.value

def __hash__(self):
return hash(self.value)

def __eq__(self, other):
return isinstance(other, Value) and self.value == other.value

0 comments on commit e245229

Please sign in to comment.