On Tue, Jul 30, 2013 at 12:58 PM, Joshua TAYLOR <[email protected]> wrote:
> This query
>
> prefix : <http://example.org/>
> select ?x ?y where {
>   values (?x ?y) {
>     (1 2)
>     (1 UNDEF )
>   }
> }
>
> produces
>
> ---------
> | x | y |
> =========
> | 1 |   |
> | 1 | 2 |
> ---------
>
> because in the ordering, the UNDEF ?y comes before the 2 ?y.  As such,
> I'd expect that if I were to group by ?x and have the aggregate set
> for ?y = { UNDEF, 2 } and select max(?y) that I should get 2.
> However, this query
>
> order by ?x ?y
> prefix : <http://example.org/>
> select ?x (max(?y) as ?maxY) where {
>   values (?x ?y) {
>     (1 2)
>     (1 UNDEF)
>   }
> }
> group by ?x
> order by ?x
>
> which does just that, produces:
>
> ------------
> | x | maxY |
> ============
> | 1 |      |
> ------------
>
> Am I misunderstanding how max [1] works?  The spec says that max makes
> use of the ORDER BY ordering, so I'm surprised by these results.  Have
> I missed something?
>
> Thanks,
> //JT
>
> [1] http://www.w3.org/TR/sparql11-query/#defn_aggMax

And here's minimal working code, in case you want to reproduce this.
Sorry, I meant to include this in the first email:

import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class GroupAndIf {
        final static String orderQuery = ""+
                        "prefix : <http://example.org/> \n" +
                        "select ?x ?y where {\n" +
                        "  values (?x ?y) {\n" +
                        "    (1 2)\n" +
                        "    (1 UNDEF )\n" +
                        "  }\n" +
                        "}\n" +
                        "order by ?x ?y" +
                        "";
        
        final static String groupQuery = "" +
                        "prefix : <http://example.org/> \n" +
                        "select ?x (max(?y) as ?maxY) where {\n" +
                        "  values (?x ?y) {\n" +
                        "    (1 2)\n" +
                        "    (1 UNDEF)\n" +
                        "  }\n" +
                        "}\n" +
                        "group by ?x\n" +
                        "order by ?x\n" +
                        "";

        public static void main(String[] args) {
                final Model model = ModelFactory.createDefaultModel();
                System.out.println( orderQuery );
                System.out.println( groupQuery );

                // Note that in the output, ordered by ?x ?y, the UNDEF < 2
                ResultSetFormatter.out( QueryExecutionFactory.create( 
orderQuery,
model ).execSelect() );
                
                // Yet when we group by ?x and ask for (max(?y) as ?maxY), we 
get an
undefined ?maxY.
                ResultSetFormatter.out( QueryExecutionFactory.create( 
groupQuery,
model ).execSelect() );
        }
}

-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Reply via email to