from common.ast import ast_node, ColRef start_expr = 'f"' index_expr = '{\'\' if x is None and y is None else f\'[{x}]\'}' end_expr = '"' class expr(ast_node): name='expr' builtin_func_maps = { 'max': 'max', 'min': 'min', 'avg': 'avg', 'sum': 'sum', 'count' : 'count', 'mins': ['mins', 'minw'], 'maxs': ['maxs', 'maxw'], 'avgs': ['avgs', 'avgw'], 'sums': ['sums', 'sumw'], } binary_ops = { 'sub':'-', 'add':'+', 'mul':'*', 'div':'/', 'mod':'%', 'and':'&&', 'or':'||', 'xor' : '^', 'gt':'>', 'lt':'<', 'lte':'<=', 'gte':'>=', 'neq':'!=', 'eq':'==' } compound_ops = { 'missing' : ['missing', lambda x: f'{x[0]} == nullval>'], } unary_ops = { 'neg' : '-', 'not' : '!' } coumpound_generating_ops = ['avgs', 'mins', 'maxs', 'sums'] + \ list( binary_ops.keys()) + list(compound_ops.keys()) + list(unary_ops.keys() ) def __init__(self, parent, node, materialize_cols = True, abs_col = False): self.materialize_cols = materialize_cols self.raw_col = None self.__abs = abs_col self.inside_agg = False if(type(parent) is expr): self.inside_agg = parent.inside_agg self.__abs = parent.__abs ast_node.__init__(self, parent, node, None) def init(self, _): from common.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._expr = '' self.cexpr = None 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: # TODO: distinguish between UDF agg functions and other UDF functions. self.inside_agg = True self.context.headers.add('"./server/aggregations.h"') if type(val) is list and len(val) > 1: cfunc = self.func_maps[key] cfunc = cfunc[len(val) - 1] if type(cfunc) is list else cfunc self._expr += f"{cfunc}(" for i, p in enumerate(val): self._expr += expr(self, p)._expr + (','if i