from engine.ast import ast_node class expr(ast_node): name='expr' builtin_func_maps = { 'max': 'max', 'min': 'min', 'avg': 'avg', 'sum': 'sum', 'mod':'mod', 'mins': ['mins', 'minsw'], 'maxs': ['maxs', 'maxsw'], 'avgs': ['avgs', 'avgsw'], 'sums': ['sums', 'sumsw'], } binary_ops = { 'sub':'-', 'add':'+', 'mul':'*', 'div':'%', 'and':'&', 'or':'|', 'gt':'>', 'lt':'<', } compound_ops = { 'ge' : [2, lambda x: f'~({x[0]}<{x[1]})'], 'le' : [2, lambda x: f'~({x[0]}>{x[1]})'], } unary_ops = { 'neg' : '-', 'not' : '~' } coumpound_generating_ops = ['mod', 'mins', 'maxs', 'sums'] + \ list( binary_ops.keys()) + list(compound_ops.keys()) + list(unary_ops.keys() ) def __init__(self, parent, node): ast_node.__init__(self, parent, node, None) def init(self, _): from engine.projection import projection parent = self.parent self.isvector = parent.isvector if type(parent) is expr else False self.is_compound = parent.is_compound if type(parent) is expr else False if type(parent) in [projection, expr]: self.datasource = parent.datasource else: self.datasource = self.context.datasource self.udf_map = parent.context.udf_map self.k9expr = '' self.func_maps = {**self.udf_map, **self.builtin_func_maps} def produce(self, node): if type(node) is dict: for key, val in node.items(): if key in self.func_maps: # if type(val) in [dict, str]: if type(val) is list and len(val) > 1: k9func = self.func_maps[key] k9func = k9func[len(val) - 1] if type(k9func) is list else k9func self.k9expr += f"{k9func}[" for i, p in enumerate(val): self.k9expr += expr(self, p).k9expr + (';'if i