Re: Semantics of SUM

On 12 Nov 2009, at 20:10, Andy Seaborne wrote:
>>> However, it does not quite follow the semantics of "+" because it
>>> changes the datatype:
>>>
>>> SUM(?x | ?x{1,2,3} ) ==> "6"^^xsd:integer
>
> Notation was meant to be SUM(?x) over a group with ?x=1 ,?x=2, ?x=3
> It was a bit terse - had been writing comprehensions just before  
> hand so it was obvious to me, at the time.

Hm, yeah, I guessed that, but the rest I didn't follow. Makes sense  
now though.

>>> but
>>>
>>> SUM(COALESCE(xsd:double(?x), 0) | ?x{1,2,3} ) => "6"^^xsd:double
>
> Using
>   SUM(COALESCE(xsd:double(?x), 0))
> for group members ?x=1,2,3 (xsd:integers) results in xsd:double, not  
> xsd:integer as it would for 1+2+3

No, sure, but COALESCE(?x, 0) would in this case.

>>> Using xsd:integer does not work e.g. 1, 1.5, 3 !=>  
>>> "5.5"^^xsd:decimal
>
> Encoding the full XSD hierarchy to minimise the promotion as + used  
> natively does, and cope with errors/non-numbers might be:
>
> COALESCE(xsd:integer(?x), xsd:decimal(?x), xsd:double(?x), 0)
>
> which I don't think is a serious contender on practical grounds.

No, but I don't imagine there are too many situations where users  
would want the exact semantics, do you have good examples?

> I think ending up with xsd:double, not xsd:integer, in the potential  
> present of errors, is significant so maybe a way to indicate that  
> errors be excluded might be a better.  However, a plethora of  
> options is also bad design.  Hmm.

I think you could do that with COALESCE(?x), if none of the values  
passed to COALESCE are not type errors or bound, you get an unbound  
value returned, so it will be dropped. I think.

- Steve

-- 
Steve Harris, CTO, Garlik Limited
2 Sheen Road, Richmond, TW9 1AE, UK
+44(0)20 8973 2465  http://www.garlik.com/
Registered in England and Wales 535 7233 VAT # 849 0517 11
Registered office: Thames House, Portsmouth Road, Esher, Surrey, KT10  
9AD

Received on Thursday, 12 November 2009 22:55:26 UTC