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 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 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)
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 # handling arithmetic(addition/subtraction) of dateTime, date, time # and duration datatypes (if any) if hasattr(expr, 'datatype') and (expr.datatype in XSD_DateTime_DTs or expr.datatype in XSD_Duration_DTs): res = dateTimeObjects(expr) dt = expr.datatype for op, term in zip(e.op, other): # check if operation is datetime,date,time operation over # another datetime,date,time datatype if dt in XSD_DateTime_DTs and dt == term.datatype and op == '-': # checking if there are more than one datetime operands - # in that case it doesn't make sense for example # ( dateTime1 - dateTime2 - dateTime3 ) is an invalid operation if len(other) > 1: error_message = "Can't evaluate multiple %r arguments" raise SPARQLError(error_message, dt.datatype) else: n = dateTimeObjects(term) res = calculateDuration(res, n) return res # datetime,date,time +/- duration,dayTimeDuration,yearMonthDuration elif (dt in XSD_DateTime_DTs and term.datatype in XSD_Duration_DTs): n = dateTimeObjects(term) res = calculateFinalDateTime(res, dt, n, term.datatype, op) return res # duration,dayTimeDuration,yearMonthDuration + datetime,date,time elif dt in XSD_Duration_DTs and term.datatype in XSD_DateTime_DTs: if op == "+": n = dateTimeObjects(term) res = calculateFinalDateTime(res, dt, n, term.datatype, op) return res # rest are invalid types else: raise SPARQLError('Invalid DateTime Operations') # handling arithmetic(addition/subtraction) of numeric datatypes (if any) else: 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)