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 update(self, row, aggregator): try: value = _eval(self.expr, row) dt = self.datatype if dt is None: dt = value.datatype else: dt = type_promotion(dt, value.datatype) self.datatype = dt self.value = sum(type_safe_numbers(self.value, numeric(value))) if self.distinct: self.seen.add(value) except NotBoundError: # skip UNDEF pass
def agg_Max(a, group, bindings): m = None for x in group: try: v = numeric(_eval(a.vars, x)) if m is None: m = v else: m = num_max(v, m) except: return # error in aggregate => no binding if m is not None: bindings[a.res] = Literal(m)
def agg_Max(a, group, bindings): m = None for v in _eval_rows(a.vars, group, None): # DISTINCT makes no difference for MAX try: v = numeric(v) if m is None: m = v else: m = num_max(v, m) except: return # error in aggregate => no binding if m is not None: bindings[a.res] = Literal(m)
def agg_Min(a, group, bindings): m = None for v in _eval_rows(a.vars, group, None): # DISTINCT makes no difference for MIN try: v = numeric(v) if m is None: m = v else: m = num_min(v, m) except: continue # try other values if m is not None: bindings[a.res] = Literal(m)
def update(self, row, aggregator): try: value = _eval(self.expr, row) dt = self.datatype self.sum = sum(type_safe_numbers(self.sum, numeric(value))) if dt is None: dt = value.datatype else: dt = type_promotion(dt, value.datatype) self.datatype = dt if self.distinct: self.seen.add(value) self.counter += 1 # skip UNDEF or BNode => SPARQLTypeError except NotBoundError: pass except SPARQLTypeError: pass
def agg_Sum(a, group, bindings): c = 0 dt = None for e in _eval_rows(a.vars, group, a.distinct): try: 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)