-
Notifications
You must be signed in to change notification settings - Fork 11
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
Feature request: grammar / WHERE
namespace extensions
#58
Comments
eval
namespace extensions WHERE
namespace extensions
I was able to achieve something similar with this grammar modification: %import python.expr_stmt -> python_expr
// Replaces current `where_clause` definition
where_clause: "where"i python_expr And this implementation: from lark.reconstruct import Reconstructor
# Add global items here such as "to_datetime": pd.to_datetime
WHERE_EXPRESSION_GLOBALS = {}
...
class _CypherNamespace(dict):
"""
dot.notation access to dictionary attributes, useful for enabling cypher filtering
syntax like `m.born < to_datetime("1990")`
"""
def __getattr__(self, attr):
out = self.get(attr)
if isinstance(out, dict):
return type(self)(out)
return out
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
...
_CypherGrammar = Lark(..., maybe_placeholders=False)
reconstructor = Reconstructor(_CypherGrammar)
...
# Update these CypherTransformer methods
def where_clause(self, where_clause: list[Tree]):
self.where_string = reconstructor.reconstruct(where_clause[0])
def _new_where_condition(cname_value_map: dict, target_graph: nx.DiGraph, _):
if not self.where_string:
return True, []
eval_locals = {
cname: _CypherNamespace(target_graph.nodes[value])
for cname, value in cname_value_map.items()
}
result = eval(self.where_string, WHERE_EXPRESSION_GLOBALS, eval_locals)
return result, [result] Currently, it assumes a hard-coded list of globals that the user can update with their own values. It was a fun intro to lark 🙂 |
@ntjess just wanted to pop in and tell you this looks so awesome — I need to take a closer look here (and in #59) but I'm unfortunately in the midst of my phd dissertation prooposal process and it's using up all my cycles for the next wee or so. But I love what you did here! I'm brainstorming about how we can integrate into the official codebase while keeping back-compat and vuln surface-area low!! |
One option is to add
I've discovered |
I wonder if a pattern similar to what the python I'd be concerned from a security standpoint to introduce evals into the query syntax. Seems like a lot could go wrong there. |
I'll also just drop here that if we support
|
Use case: Cypher supports a robust library for datetime analysis, among other extensions. While it is unrealistic to expect their integration here, it would be nice to allow python equivalents during queries. I envision something like this:
Would love to hear your thoughts.
The text was updated successfully, but these errors were encountered: