Re: Semantics of SUM

On 12/11/2009 15:47, Steve Harris wrote:
> On 11 Nov 2009, at 10:46, Andy Seaborne wrote:
>> On 10/11/2009 18:47, Steve Harris wrote:
>>>
>>>> I don't see that COALESCE helps because there isn't an intermediate
>>>> variable representing the sum so far.
>>>
>>> I think the idea was something like:
>>>
>>> SELECT SUM(COALESCE(xsd:double(?x), 0))
>>> WHERE {
>>> <x> <value> ?x .
>>> }
>>
>> Thanks - I'd missed that.
>
> To be clear, it's just my recollection of what we discussed, I don't
> think anything specific was in the minutes.
>
>> 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.

>> 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

>>
>> 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.

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 do wonder if SUM should filter out errors/unbound before delegating to 
"+".  I would be interested in more information about the F2F2 
discussions - I can only see a short section on this and mostly it's 
about COALESCE.

((
The full version is possibly
COALESCE(xsd:integer(?x), xsd:decimal(?x) ,
          xsd:float(?x),  xsd:double(?x), 0)
if the system has floats and the app cares.

Second table in
http://www.w3.org/TR/xpath-functions/#op.numeric
))

Sorry for the confusing notation earlier,

	Andy

Received on Thursday, 12 November 2009 20:10:29 UTC