You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Benson Margulies <bi...@gmail.com> on 2011/01/06 19:47:32 UTC

count(*), construct, how many reifications

In my store I have (for purposes of discussion)

one tuple S P O

and then some number of quadlets on it.

I want to find out how many.

However, to make matters more complex, what I'd really like to do is
set up a construct query that finds


S (p1|p2|p3|p4...) O

and also then returns the count of quadlets for each of these it comes up with.

Is this plausible .... ? Obviously, I can do it by first finding the
statements and then going back and using the COUNT extension to do the
counting.

Is TDB particularly good or bad at this?

Re: count(*), construct, how many reifications

Posted by Andy Seaborne <an...@epimorphics.com>.
Subquery is the best option:

CONSTRUCT { ?s ?p ?o }
WHERE {
  SELECT ?s ?p ?o
  {
    GRAPH x-arq:UnionGraph {
     ?s rex:text "Taliban" .
     ?r rdf:subject ?s .
     ?r rdf:object ?o .
     ?r rdf:predicate ?p .
     FILTER ( ?p IN (
   <http://www.basistech.com/ontologies/2010/12/rules#acquired>,
   <http://www.basistech.com/ontologies/2010/12/rules#acquired>,
   <http://www.basistech.com/ontologies/2010/12/rules#costOfObject>,
   <http://www.basistech.com/ontologies/2010/12/rules#nameChangeTo>,
   <http://www.basistech.com/ontologies/2010/12/rules#locatedAt>,
   <http://www.basistech.com/ontologies/2010/12/rules#phoneNumber>,
   <http://www.basistech.com/ontologies/2010/12/rules#releasedProduct>,
   <http://www.basistech.com/ontologies/2010/12/rules#supplierOf>,
   <http://www.basistech.com/ontologies/2010/12/rules#subsidiaryOf>
    ) )
    }
   }
   GROUP BY ?s ?p ?o
}



On 06/01/11 23:07, Benson Margulies wrote:
> Here's a working query with SELECT
>
> SELECT  ?s ?p ?o (count(*) AS ?relCount)
> WHERE {
>   GRAPH x-arq:UnionGraph {
>    ?s rex:text "Taliban" .
>    ?r rdf:subject ?s .
>    ?r rdf:object ?o .
>    ?r rdf:predicate ?p .
>    FILTER ( ?p IN (
> <http://www.basistech.com/ontologies/2010/12/rules#acquired>,
> <http://www.basistech.com/ontologies/2010/12/rules#acquired>,
> <http://www.basistech.com/ontologies/2010/12/rules#costOfObject>,
> <http://www.basistech.com/ontologies/2010/12/rules#nameChangeTo>,
> <http://www.basistech.com/ontologies/2010/12/rules#locatedAt>,
> <http://www.basistech.com/ontologies/2010/12/rules#phoneNumber>,
> <http://www.basistech.com/ontologies/2010/12/rules#releasedProduct>,
> <http://www.basistech.com/ontologies/2010/12/rules#supplierOf>,
> <http://www.basistech.com/ontologies/2010/12/rules#subsidiaryOf>
> ) )
>   }
> }
> GROUP BY ?s ?p ?o
>
>
> How does all this work with CONSTRUCT? Can I do something like
>
> CONSTRUCT { ?s ?p ?o .
>                         ?s me:count (COUNT(*) as ?c) .
> }

Re: count(*), construct, how many reifications

Posted by Benson Margulies <bi...@gmail.com>.
Here's a working query with SELECT

SELECT  ?s ?p ?o (count(*) AS ?relCount)
WHERE {
 GRAPH x-arq:UnionGraph {
  ?s rex:text "Taliban" .
  ?r rdf:subject ?s .
  ?r rdf:object ?o .
  ?r rdf:predicate ?p .
  FILTER ( ?p IN (
<http://www.basistech.com/ontologies/2010/12/rules#acquired>,
<http://www.basistech.com/ontologies/2010/12/rules#acquired>,
<http://www.basistech.com/ontologies/2010/12/rules#costOfObject>,
<http://www.basistech.com/ontologies/2010/12/rules#nameChangeTo>,
<http://www.basistech.com/ontologies/2010/12/rules#locatedAt>,
<http://www.basistech.com/ontologies/2010/12/rules#phoneNumber>,
<http://www.basistech.com/ontologies/2010/12/rules#releasedProduct>,
<http://www.basistech.com/ontologies/2010/12/rules#supplierOf>,
<http://www.basistech.com/ontologies/2010/12/rules#subsidiaryOf>
) )
 }
}
GROUP BY ?s ?p ?o


How does all this work with CONSTRUCT? Can I do something like

CONSTRUCT { ?s ?p ?o .
                       ?s me:count (COUNT(*) as ?c) .
}

Re: count(*), construct, how many reifications

Posted by Andy Seaborne <an...@epimorphics.com>.

On 06/01/11 19:38, Benson Margulies wrote:
> Aha, a GROUP BY opportunity? (ignoring the filter on predicate for now)
>
>    ?r rdf:subject<urn:jug:00021a4501bfda090b3a95919c235744#e72>  .
>    ?r rdf:object ?o .
>    ?r rdf:predicate ?p .
>
> If I group by (O, P) and count, I can get results like  U(=that long
> uri) ?O ?P ?COUNT, if the relational analogy holds, no?

The analogue holds and SPARQL 1.1 has GROUP BY.

Note it is strict: if you use GROUP BY you can only use vars in the 
GROUP BY and aggregates in the SELECT line (unlike MySQL for example)

You can use SAMPLE to pick one value of a GROUP.

	Andy

>
>
>
> On Thu, Jan 6, 2011 at 2:32 PM, Benson Margulies<bi...@gmail.com>  wrote:
>> With current ARQ, the default syntax didn't accept count. I set syntax
>> to ARQ and it worked. I'll change it to use the sparql 1.1 syntax
>> instead.
>>
>> On Thu, Jan 6, 2011 at 2:06 PM, Andy Seaborne
>> <an...@epimorphics.com>  wrote:
>>>
>>>
>>> On 06/01/11 18:47, Benson Margulies wrote:
>>>>
>>>> In my store I have (for purposes of discussion)
>>>>
>>>> one tuple S P O
>>>>
>>>> and then some number of quadlets on it.
>>>>
>>>> I want to find out how many.
>>>
>>> SELECT (Count(*) AS ?C)
>>> {
>>>    ?x rdf:subject S .
>>>    ?x rdf:predicate P .
>>>    ?x rdf:object O .
>>> }
>>>
>>> which assumes fully reified.
>>>
>>>> However, to make matters more complex, what I'd really like to do is
>>>> set up a construct query that finds
>>>>
>>>>
>>>> S (p1|p2|p3|p4...) O
>>>
>>> SELECT (Count(*) AS ?C)
>>> {
>>>    ?x rdf:subject S .
>>>    ?x rdf:predicate ?P .
>>>    ?x rdf:object O .
>>>    FILTER ( ?P IN ( p1, p2, p3, p4) )
>>> }
>>>
>>>
>>>> and also then returns the count of quadlets for each of these it comes up
>>>> with.
>>>
>>> Finding then counting, and finding and returning needs duplication of the
>>> thing.  It's a quirk in the whole relation algebra thing  - you can't assign
>>> subexpressions and reuse them - this is not unique to SPARQL.  To look at it
>>> another way, counting is destructive on the things being counted.
>>>
>>>> Is this plausible .... ? Obviously, I can do it by first finding the
>>>> statements and then going back and using the COUNT extension to do the
>>>> counting.
>>>
>>> COUNT isn't an extension - it's part of SPARQL 1.1
>>>
>>>> Is TDB particularly good or bad at this?
>>>
>>> It does not anything special but caching is your friend (and mime).
>>>
>>>         Andy
>>>
>>

Re: count(*), construct, how many reifications

Posted by Benson Margulies <bi...@gmail.com>.
Aha, a GROUP BY opportunity? (ignoring the filter on predicate for now)

  ?r rdf:subject <urn:jug:00021a4501bfda090b3a95919c235744#e72> .
  ?r rdf:object ?o .
  ?r rdf:predicate ?p .

If I group by (O, P) and count, I can get results like  U(=that long
uri) ?O ?P ?COUNT, if the relational analogy holds, no?



On Thu, Jan 6, 2011 at 2:32 PM, Benson Margulies <bi...@gmail.com> wrote:
> With current ARQ, the default syntax didn't accept count. I set syntax
> to ARQ and it worked. I'll change it to use the sparql 1.1 syntax
> instead.
>
> On Thu, Jan 6, 2011 at 2:06 PM, Andy Seaborne
> <an...@epimorphics.com> wrote:
>>
>>
>> On 06/01/11 18:47, Benson Margulies wrote:
>>>
>>> In my store I have (for purposes of discussion)
>>>
>>> one tuple S P O
>>>
>>> and then some number of quadlets on it.
>>>
>>> I want to find out how many.
>>
>> SELECT (Count(*) AS ?C)
>> {
>>   ?x rdf:subject S .
>>   ?x rdf:predicate P .
>>   ?x rdf:object O .
>> }
>>
>> which assumes fully reified.
>>
>>> However, to make matters more complex, what I'd really like to do is
>>> set up a construct query that finds
>>>
>>>
>>> S (p1|p2|p3|p4...) O
>>
>> SELECT (Count(*) AS ?C)
>> {
>>   ?x rdf:subject S .
>>   ?x rdf:predicate ?P .
>>   ?x rdf:object O .
>>   FILTER ( ?P IN ( p1, p2, p3, p4) )
>> }
>>
>>
>>> and also then returns the count of quadlets for each of these it comes up
>>> with.
>>
>> Finding then counting, and finding and returning needs duplication of the
>> thing.  It's a quirk in the whole relation algebra thing  - you can't assign
>> subexpressions and reuse them - this is not unique to SPARQL.  To look at it
>> another way, counting is destructive on the things being counted.
>>
>>> Is this plausible .... ? Obviously, I can do it by first finding the
>>> statements and then going back and using the COUNT extension to do the
>>> counting.
>>
>> COUNT isn't an extension - it's part of SPARQL 1.1
>>
>>> Is TDB particularly good or bad at this?
>>
>> It does not anything special but caching is your friend (and mime).
>>
>>        Andy
>>
>

Re: count(*), construct, how many reifications

Posted by Andy Seaborne <an...@epimorphics.com>.
SPARQL 1.1 requires AS; ARQ will invent a variable if needbe.  Other 
than that they are the same (it's the same code in the parser as the spec).

e.g.
http://www.sparql.org/query-validator?query=SELECT+%28COUNT%28*%29+AS+%3Fc%29%0D%0A{+%3Fs+%3Fp+%3Fo+}&languageSyntax=SPARQL&outputFormat=sparql&outputFormat=algebra&linenumbers=true

	Andy


On 06/01/11 22:28, Benson Margulies wrote:
> hmm. 2.8.7. is the sparql 1.1 syntax different than that offered on
> the arq aggregate page?
>

Re: count(*), construct, how many reifications

Posted by Benson Margulies <bi...@gmail.com>.
hmm. 2.8.7. is the sparql 1.1 syntax different than that offered on
the arq aggregate page?

On Thu, Jan 6, 2011 at 3:55 PM, Andy Seaborne
<an...@epimorphics.com> wrote:
>
>
> On 06/01/11 19:32, Benson Margulies wrote:
>>
>> With current ARQ, the default syntax didn't accept count. I set syntax
>> to ARQ and it worked. I'll change it to use the sparql 1.1 syntax
>> instead.
>
> In current ARQ (2.8.7) the default is SPARQL 1.1.
>
> If it works in the ARQ extended language and not in the default language,
> you mostly have an older version of ARQ on the classpath.
>
>        Andy
>
>>
>> On Thu, Jan 6, 2011 at 2:06 PM, Andy Seaborne
>> <an...@epimorphics.com>  wrote:
>>>
>>>
>>> On 06/01/11 18:47, Benson Margulies wrote:
>>>>
>>>> In my store I have (for purposes of discussion)
>>>>
>>>> one tuple S P O
>>>>
>>>> and then some number of quadlets on it.
>>>>
>>>> I want to find out how many.
>>>
>>> SELECT (Count(*) AS ?C)
>>> {
>>>   ?x rdf:subject S .
>>>   ?x rdf:predicate P .
>>>   ?x rdf:object O .
>>> }
>>>
>>> which assumes fully reified.
>>>
>>>> However, to make matters more complex, what I'd really like to do is
>>>> set up a construct query that finds
>>>>
>>>>
>>>> S (p1|p2|p3|p4...) O
>>>
>>> SELECT (Count(*) AS ?C)
>>> {
>>>   ?x rdf:subject S .
>>>   ?x rdf:predicate ?P .
>>>   ?x rdf:object O .
>>>   FILTER ( ?P IN ( p1, p2, p3, p4) )
>>> }
>>>
>>>
>>>> and also then returns the count of quadlets for each of these it comes
>>>> up
>>>> with.
>>>
>>> Finding then counting, and finding and returning needs duplication of the
>>> thing.  It's a quirk in the whole relation algebra thing  - you can't
>>> assign
>>> subexpressions and reuse them - this is not unique to SPARQL.  To look at
>>> it
>>> another way, counting is destructive on the things being counted.
>>>
>>>> Is this plausible .... ? Obviously, I can do it by first finding the
>>>> statements and then going back and using the COUNT extension to do the
>>>> counting.
>>>
>>> COUNT isn't an extension - it's part of SPARQL 1.1
>>>
>>>> Is TDB particularly good or bad at this?
>>>
>>> It does not anything special but caching is your friend (and mime).
>>>
>>>        Andy
>>>
>

Re: count(*), construct, how many reifications

Posted by Andy Seaborne <an...@epimorphics.com>.

On 06/01/11 19:32, Benson Margulies wrote:
> With current ARQ, the default syntax didn't accept count. I set syntax
> to ARQ and it worked. I'll change it to use the sparql 1.1 syntax
> instead.

In current ARQ (2.8.7) the default is SPARQL 1.1.

If it works in the ARQ extended language and not in the default 
language, you mostly have an older version of ARQ on the classpath.

	Andy

>
> On Thu, Jan 6, 2011 at 2:06 PM, Andy Seaborne
> <an...@epimorphics.com>  wrote:
>>
>>
>> On 06/01/11 18:47, Benson Margulies wrote:
>>>
>>> In my store I have (for purposes of discussion)
>>>
>>> one tuple S P O
>>>
>>> and then some number of quadlets on it.
>>>
>>> I want to find out how many.
>>
>> SELECT (Count(*) AS ?C)
>> {
>>    ?x rdf:subject S .
>>    ?x rdf:predicate P .
>>    ?x rdf:object O .
>> }
>>
>> which assumes fully reified.
>>
>>> However, to make matters more complex, what I'd really like to do is
>>> set up a construct query that finds
>>>
>>>
>>> S (p1|p2|p3|p4...) O
>>
>> SELECT (Count(*) AS ?C)
>> {
>>    ?x rdf:subject S .
>>    ?x rdf:predicate ?P .
>>    ?x rdf:object O .
>>    FILTER ( ?P IN ( p1, p2, p3, p4) )
>> }
>>
>>
>>> and also then returns the count of quadlets for each of these it comes up
>>> with.
>>
>> Finding then counting, and finding and returning needs duplication of the
>> thing.  It's a quirk in the whole relation algebra thing  - you can't assign
>> subexpressions and reuse them - this is not unique to SPARQL.  To look at it
>> another way, counting is destructive on the things being counted.
>>
>>> Is this plausible .... ? Obviously, I can do it by first finding the
>>> statements and then going back and using the COUNT extension to do the
>>> counting.
>>
>> COUNT isn't an extension - it's part of SPARQL 1.1
>>
>>> Is TDB particularly good or bad at this?
>>
>> It does not anything special but caching is your friend (and mime).
>>
>>         Andy
>>

Re: count(*), construct, how many reifications

Posted by Benson Margulies <bi...@gmail.com>.
With current ARQ, the default syntax didn't accept count. I set syntax
to ARQ and it worked. I'll change it to use the sparql 1.1 syntax
instead.

On Thu, Jan 6, 2011 at 2:06 PM, Andy Seaborne
<an...@epimorphics.com> wrote:
>
>
> On 06/01/11 18:47, Benson Margulies wrote:
>>
>> In my store I have (for purposes of discussion)
>>
>> one tuple S P O
>>
>> and then some number of quadlets on it.
>>
>> I want to find out how many.
>
> SELECT (Count(*) AS ?C)
> {
>   ?x rdf:subject S .
>   ?x rdf:predicate P .
>   ?x rdf:object O .
> }
>
> which assumes fully reified.
>
>> However, to make matters more complex, what I'd really like to do is
>> set up a construct query that finds
>>
>>
>> S (p1|p2|p3|p4...) O
>
> SELECT (Count(*) AS ?C)
> {
>   ?x rdf:subject S .
>   ?x rdf:predicate ?P .
>   ?x rdf:object O .
>   FILTER ( ?P IN ( p1, p2, p3, p4) )
> }
>
>
>> and also then returns the count of quadlets for each of these it comes up
>> with.
>
> Finding then counting, and finding and returning needs duplication of the
> thing.  It's a quirk in the whole relation algebra thing  - you can't assign
> subexpressions and reuse them - this is not unique to SPARQL.  To look at it
> another way, counting is destructive on the things being counted.
>
>> Is this plausible .... ? Obviously, I can do it by first finding the
>> statements and then going back and using the COUNT extension to do the
>> counting.
>
> COUNT isn't an extension - it's part of SPARQL 1.1
>
>> Is TDB particularly good or bad at this?
>
> It does not anything special but caching is your friend (and mime).
>
>        Andy
>

Re: count(*), construct, how many reifications

Posted by Benson Margulies <bi...@gmail.com>.
It would be nice if the possible arguments to --syntax were listed on
the wiki page for tdbquery

On Thu, Jan 6, 2011 at 2:06 PM, Andy Seaborne
<an...@epimorphics.com> wrote:
>
>
> On 06/01/11 18:47, Benson Margulies wrote:
>>
>> In my store I have (for purposes of discussion)
>>
>> one tuple S P O
>>
>> and then some number of quadlets on it.
>>
>> I want to find out how many.
>
> SELECT (Count(*) AS ?C)
> {
>   ?x rdf:subject S .
>   ?x rdf:predicate P .
>   ?x rdf:object O .
> }
>
> which assumes fully reified.
>
>> However, to make matters more complex, what I'd really like to do is
>> set up a construct query that finds
>>
>>
>> S (p1|p2|p3|p4...) O
>
> SELECT (Count(*) AS ?C)
> {
>   ?x rdf:subject S .
>   ?x rdf:predicate ?P .
>   ?x rdf:object O .
>   FILTER ( ?P IN ( p1, p2, p3, p4) )
> }
>
>
>> and also then returns the count of quadlets for each of these it comes up
>> with.
>
> Finding then counting, and finding and returning needs duplication of the
> thing.  It's a quirk in the whole relation algebra thing  - you can't assign
> subexpressions and reuse them - this is not unique to SPARQL.  To look at it
> another way, counting is destructive on the things being counted.
>
>> Is this plausible .... ? Obviously, I can do it by first finding the
>> statements and then going back and using the COUNT extension to do the
>> counting.
>
> COUNT isn't an extension - it's part of SPARQL 1.1
>
>> Is TDB particularly good or bad at this?
>
> It does not anything special but caching is your friend (and mime).
>
>        Andy
>

Re: count(*), construct, how many reifications

Posted by Andy Seaborne <an...@epimorphics.com>.

On 06/01/11 18:47, Benson Margulies wrote:
> In my store I have (for purposes of discussion)
>
> one tuple S P O
>
> and then some number of quadlets on it.
>
> I want to find out how many.

SELECT (Count(*) AS ?C)
{
    ?x rdf:subject S .
    ?x rdf:predicate P .
    ?x rdf:object O .
}

which assumes fully reified.

> However, to make matters more complex, what I'd really like to do is
> set up a construct query that finds
>
>
> S (p1|p2|p3|p4...) O

SELECT (Count(*) AS ?C)
{
    ?x rdf:subject S .
    ?x rdf:predicate ?P .
    ?x rdf:object O .
    FILTER ( ?P IN ( p1, p2, p3, p4) )
}


> and also then returns the count of quadlets for each of these it comes up with.

Finding then counting, and finding and returning needs duplication of 
the thing.  It's a quirk in the whole relation algebra thing  - you 
can't assign subexpressions and reuse them - this is not unique to 
SPARQL.  To look at it another way, counting is destructive on the 
things being counted.

> Is this plausible .... ? Obviously, I can do it by first finding the
> statements and then going back and using the COUNT extension to do the
> counting.

COUNT isn't an extension - it's part of SPARQL 1.1

> Is TDB particularly good or bad at this?

It does not anything special but caching is your friend (and mime).

	Andy