diff --git a/pydruid/utils/filters.py b/pydruid/utils/filters.py index 9ee5677f..51c423e8 100644 --- a/pydruid/utils/filters.py +++ b/pydruid/utils/filters.py @@ -27,6 +27,11 @@ def __init__(self, **args): "dimension": args["dimension"], "value": args["value"]}} + elif args["type"] == "javascript": + self.filter = {"filter": {"type": "javascript", + "dimension": args["dimension"], + "function": args["function"]}} + elif args["type"] == "and": self.filter = {"filter": {"type": "and", "fields": args["fields"]}} @@ -67,3 +72,10 @@ def __init__(self, dim): def __eq__(self, other): return Filter(dimension=self.dimension, value=other) + +class JavaScript: + def __init__(self, dim): + self.dimension = dim + + def __eq__(self, func): + return Filter(type='javascript', dimension=self.dimension, function=func) diff --git a/tests/utils/test_filters.py b/tests/utils/test_filters.py index c1a851f1..644d60e4 100644 --- a/tests/utils/test_filters.py +++ b/tests/utils/test_filters.py @@ -22,6 +22,12 @@ def test_selector_filter(self): expected = {'type': 'selector', 'dimension': 'dim', 'value': 'val'} assert actual == expected + def test_javascript_filter(self): + actual = filters.Filter.build_filter( + filters.Filter(type='javascript', dimension='dim', function='function(x){return true}')) + expected = {'type': 'javascript', 'dimension': 'dim', 'function': 'function(x){return true}'} + assert actual == expected + def test_and_filter(self): f1 = filters.Filter(dimension='dim1', value='val1') f2 = filters.Filter(dimension='dim2', value='val2')