Skip to content

Commit cd21fda

Browse files
committed
Add initial helpers for creating filters in visual recipes programatically
1 parent 2bc5331 commit cd21fda

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

dataikuapi/dss/recipe.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ def add_virtual_input(self, input_dataset_index):
842842
"""
843843
self.raw_virtual_inputs.append({"index": input_dataset_index})
844844

845-
def add_computed_column(self, virtual_input_index, computed_column):
845+
def add_pre_join_computed_column(self, virtual_input_index, computed_column):
846846
"""
847847
Adds a computed column to a virtual input
848848
@@ -887,6 +887,17 @@ def add_condition_to_join(self, join, type="EQ", column1=None, column2=None):
887887
join["on"].append(cond)
888888
return cond
889889

890+
def add_post_join_computed_column(self, computed_column):
891+
"""
892+
Adds a post-join computed column
893+
894+
Use :class:`dataikuapi.dss.utils.DSSComputedColumn` to build the computed_column object
895+
"""
896+
self.get_json_payload()["computedColumns"].append(computed_column)
897+
898+
def set_post_filter(self, postfilter):
899+
self.get_json_payload()["postFilter"] = postfilter
900+
890901
class JoinRecipeCreator(VirtualInputsSingleOutputRecipeCreator):
891902
"""
892903
Create a Join recipe

dataikuapi/dss/utils.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,120 @@ class DSSComputedColumn(object):
4747
def formula(name, formula, type="double"):
4848
return {"expr": formula, "mode": "GREL", "name": name, "type": type}
4949

50+
from enum import Enum
51+
class DSSFilterOperator(Enum):
52+
EMPTY_ARRAY = "empty array"
53+
NOT_EMPTY_ARRAY = "not empty array"
54+
CONTAINS_ARRAY = "array contains"
55+
NOT_EMPTY = "not empty"
56+
EMPTY = "is empty"
57+
NOT_EMPTY_STRING = "not empty string"
58+
EMPTY_STRING = "empty string"
59+
IS_TRUE = "true"
60+
IS_FALSE = "false"
61+
EQUALS_STRING = "== [string]"
62+
EQUALS_CASE_INSENSITIVE_STRING = "== [string]i"
63+
NOT_EQUALS_STRING = "!= [string]"
64+
SAME = "== [NaNcolumn]"
65+
DIFFERENT = "!= [NaNcolumn]"
66+
EQUALS_NUMBER = "== [number]"
67+
NOT_EQUALS_NUMBER = "!= [number]"
68+
GREATER_NUMBER = "> [number]"
69+
LESS_NUMBER = "< [number]"
70+
GREATER_OR_EQUAL_NUMBER = ">= [number]"
71+
LESS_OR_EQUAL_NUMBER = "<= [number]"
72+
EQUALS_DATE = "== [date]"
73+
GREATER_DATE = "> [date]"
74+
GREATER_OR_EQUAL_DATE = ">= [date]"
75+
LESS_DATE = "< [date]"
76+
LESS_OR_EQUAL_DATE = "<= [date]"
77+
BETWEEN_DATE = ">< [date]"
78+
EQUALS_COL = "== [column]"
79+
NOT_EQUALS_COL = "!= [column]"
80+
GREATER_COL = "> [column]"
81+
LESS_COL = "< [column]"
82+
GREATER_OR_EQUAL_COL = ">= [column]"
83+
LESS_OR_EQUAL_COL = "<= [column]"
84+
CONTAINS_STRING = "contains"
85+
REGEX = "regex"
86+
87+
class DSSFilter(object):
88+
"""Helper class to build filter objects for use in visual recipes"""
89+
@staticmethod
90+
def of_single_condition(column, operator, string = None, num = None, date = None, time = None, date2 = None, time2 = None, unit = None):
91+
return {
92+
"enabled": True,
93+
"uiData": {
94+
'conditions': [DSSFilter.condition(column, operator, string, num, date, time, date2, time2, unit)],
95+
"mode": "&&"
96+
}
97+
}
98+
99+
@staticmethod
100+
def of_and_conditions(conditions):
101+
return {
102+
"enabled": True,
103+
"uiData": {
104+
'conditions': conditions,
105+
"mode": "&&"
106+
}
107+
}
108+
109+
@staticmethod
110+
def of_or_conditions(conditions):
111+
return {
112+
"enabled": True,
113+
"uiData": {
114+
'conditions': conditions,
115+
"mode": "||"
116+
}
117+
}
118+
119+
@staticmethod
120+
def of_formula(formula):
121+
return {
122+
"enabled": True,
123+
"uiData": {
124+
"mode": "CUSTOM"
125+
},
126+
"expression" : formula
127+
}
128+
129+
@staticmethod
130+
def of_sql_expression(sql_expression):
131+
return {
132+
"enabled": True,
133+
"uiData": {
134+
"mode": "SQL"
135+
},
136+
"expression" : sql_expression
137+
}
138+
139+
@staticmethod
140+
def condition(column, operator, string = None, num = None, date = None, time = None, date2 = None, time2 = None, unit = None):
141+
if isinstance(operator, DSSFilterOperator):
142+
operator = operator.value
143+
cond = {
144+
"input": column,
145+
"operator": operator
146+
}
147+
if string is not None:
148+
cond["string"] = string
149+
if num is not None:
150+
cond["num"] = num
151+
if date is not None:
152+
cond["date"] = date
153+
if time is not None:
154+
cond["time"] = time
155+
if date2 is not None:
156+
cond["date2"] = date2
157+
if time2 is not None:
158+
cond["time2"] = time2
159+
if unit is not None:
160+
cond["unit"] = unit
161+
162+
return cond
163+
50164
class DSSFilterBuilder(object):
51165
"""
52166
Builder for a "filter". In DSS, a filter is used to define a subset of rows for processing.

0 commit comments

Comments
 (0)