def AdditiveExpression(e, ctx): expr = e.expr other = e.other # because of the way the add-expr production handled operator precedence # we sometimes have nothing to do if other is None: return expr res = numeric(expr) dt = expr.datatype for op, term in zip(e.op, other): n = numeric(term) if isinstance(n, Decimal) and isinstance(res, float): n = float(n) if isinstance(n, float) and isinstance(res, Decimal): res = float(res) dt = type_promotion(dt, term.datatype) if op == '+': res += n else: res -= n return Literal(res, datatype=dt)
def agg_Avg(a, group, bindings): c = 0 s = 0 dt = None for x in group: try: e = _eval(a.vars, x) n = numeric(e) if dt == None: dt = e.datatype else: dt = type_promotion(dt, e.datatype) if type(s) == float and type(n) == Decimal: s += float(n) elif type(n) == float and type(s) == Decimal: s = float(s) + n else: s += n c += 1 except: return # error in aggregate => no binding if c == 0: bindings[a.res] = Literal(0) if dt == XSD.float or dt == XSD.double: bindings[a.res] = Literal(s / c) else: bindings[a.res] = Literal(Decimal(s) / Decimal(c))
def agg_Sum(a, group, bindings): c = 0 dt = None for x in group: try: e = _eval(a.vars, x) n = numeric(e) if dt == None: dt = e.datatype else: dt = type_promotion(dt, e.datatype) if type(c) == float and type(n) == Decimal: c += float(n) elif type(n) == float and type(c) == Decimal: c = float(c) + n else: c += n except: pass # simply dont count bindings[a.res] = Literal(c, datatype=dt)