Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Merge pull request #10 from DerwenAI/update
Browse files Browse the repository at this point in the history
Update
  • Loading branch information
ceteri authored Oct 12, 2021
2 parents 548b609 + e9fe5b4 commit c740946
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 7 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# `goedwig` changelog

## 0.2.0

2021-10-11

* recursive descent traversal of AST items to develop a query plan
* can now handle arbitrary length path patterns
* toJSON() calls to pass into partitions at runtime


## 0.1.4

2021-10-10
Expand Down
88 changes: 81 additions & 7 deletions goedwig/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .cypher import CypherItem


class Variable: # pylint: disable=R0903
class Variable:
"""
Represent a node or edge variable within a query.
"""
Expand Down Expand Up @@ -45,18 +45,44 @@ def __repr__ (
return str(_repr)


def toJSON ( # pylint: disable=C0103
self,
) -> dict:
"""
JSON-serializable representation
"""
return {
"name": self.name,
"item": self.item,
"predicates": [ p.toJSON() for p in self.predicates ],
}


class Predicate: # pylint: disable=R0903
"""
Represent a simple predicate.
"""
def __init__ (
self,
var: Variable,
elem: str,
) -> None:
"""
Constructor.
"""
self.var: Variable = var
self.elem: str = elem


def toJSON ( # pylint: disable=C0103
self,
) -> dict:
"""
JSON-serializable representation
"""
return {
"elem": self.elem
}


class PredicateMap (Predicate): # pylint: disable=R0903
Expand All @@ -66,13 +92,14 @@ class PredicateMap (Predicate): # pylint: disable=R0903
def __init__ (
self,
var: Variable,
elem: str,
key: str,
val: str,
) -> None:
"""
Constructor.
"""
super().__init__(var)
super().__init__(var, elem)
self.key: str = key
self.val: str = val

Expand All @@ -92,19 +119,34 @@ def __repr__ (
return str(_repr)


def toJSON ( # pylint: disable=C0103
self,
) -> dict:
"""
JSON-serializable representation
"""
j = super().toJSON()
j["kind"] = "map"
j["key"] = self.key
j["val"] = self.val

return j


class PredicateLabel (Predicate): # pylint: disable=R0903
"""
Represent a predicate that specifies a label.
"""
def __init__ (
self,
var: Variable,
elem: str,
label: str,
) -> None:
"""
Constructor.
"""
super().__init__(var)
super().__init__(var, elem)
self.label: str = label


Expand All @@ -122,19 +164,33 @@ def __repr__ (
return str(_repr)


def toJSON ( # pylint: disable=C0103
self,
) -> dict:
"""
JSON-serializable representation
"""
j = super().toJSON()
j["kind"] = "label"
j["label"] = self.label

return j


class PredicateDirection (Predicate): # pylint: disable=R0903
"""
Represent a predicate that specifies a direction.
"""
def __init__ (
self,
var: Variable,
elem: str,
direction: str,
) -> None:
"""
Constructor.
"""
super().__init__(var)
super().__init__(var, elem)
self.direction: str = direction


Expand All @@ -152,6 +208,19 @@ def __repr__ (
return str(_repr)


def toJSON ( # pylint: disable=C0103
self,
) -> dict:
"""
JSON-serializable representation
"""
j = super().toJSON()
j["kind"] = "direction"
j["direction"] = self.direction

return j


class Path: # pylint: disable=R0903
"""
Represent a path pattern, with multiple node/edge/regex elements.
Expand All @@ -162,7 +231,7 @@ def __init__ (
"""
Constructor.
"""
self.elem: list = []
self.elem: typing.List[Variable] = []


def __repr__ (
Expand Down Expand Up @@ -320,10 +389,10 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915
child_type = self.items[j].ast_typestr

if child_type == "node pattern":
path.elem.append(self.parse_items(i = j, debug=debug))
path.elem.append(self.parse_items(i = j, debug=debug)) # type: ignore

elif child_type == "rel pattern":
path.elem.append(self.parse_items(i = j, debug=debug))
path.elem.append(self.parse_items(i = j, debug=debug)) # type: ignore

return path

Expand All @@ -343,6 +412,7 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915
if child_type == "label":
node_var.predicates.append(PredicateLabel(
node_var,
"node",
self.items[j].literal.strip(":"),
))

Expand All @@ -351,6 +421,7 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915

node_var.predicates.append(PredicateMap(
node_var,
"node",
self.items[key_i].literal, # pylint: disable=W0621
self.items[val_i].literal.strip("'"), # pylint: disable=W0621
))
Expand All @@ -372,6 +443,7 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915
if child_type == "label":
edge_var.predicates.append(PredicateLabel(
edge_var,
"edge",
self.items[j].literal.strip(":"),
))

Expand All @@ -380,13 +452,15 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915

edge_var.predicates.append(PredicateMap(
edge_var,
"edge",
self.items[key_i].literal, # pylint: disable=W0621
self.items[val_i].literal.strip("'"), # pylint: disable=W0621
))

elif child_type == "rel type":
edge_var.predicates.append(PredicateDirection(
edge_var,
"edge",
self.items[j].literal.strip(":"),
))

Expand Down
4 changes: 4 additions & 0 deletions sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
plan = eval(str(q)) # pylint: disable=W0123
print(json.dumps(plan, indent=1, sort_keys=False))

path = q.paths[0]
for elem in path.elem:
print(json.dumps(elem.toJSON()))

# testing assertions
if testing:
assert len(q.items) == 14
Expand Down

0 comments on commit c740946

Please sign in to comment.