You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by "Dimov, Stefan" <st...@sap.com> on 2017/10/13 01:36:27 UTC

Nested select doesn't work as expected

Hello,

I have two graphs:

m_p

p1    pred1    mp1
p2    pred1    mp1
p3    pred1   mp2
p4    pred1   mp2
p5    pred1   mp3
p6    pred1   mp3

and m_p_s

mp1   pred2    w:frnd
mp1   pred2    w:fdlfkdl
mp2   pred2    w:kdsjflk
mp2   pred2   w:jflksdlkj
mp3   pred2   w:frnd
mp3   pred2   w:fjksldjfls

and I want to get all the triples in m_p which objects are predicates in m_p_s and the object of that predicates in m_p_s is w:frnd

In other words I want to make query that returns (results with) p1, p2, p5 and p6 from m_p and doesn’t return p3 and p4.

I’m trying to do this with nested queries, but it doesn’t work: E.g.

    SELECT $subj $pred $pr
                FROM NAMED named_graph:m_p
                WHERE
    {
        SELECT $pr
        WHERE
        {
           GRAPH named_graph:m_p_s { $pr $pred0 w:frnd }
        }
    }

returns empty result. I tried different things, but either I get an error or empty result or everything in m_p.

I don’t want to use UNION or FILTER for performance reasons.

Do you have an idea how I can do it?

Regards,
Stefan


Re: Nested select doesn't work as expected

Posted by "Dimov, Stefan" <st...@sap.com>.
Thanks, Andy!

S.

On 10/13/17, 3:40 AM, "Andy Seaborne" <an...@apache.org> wrote:

    Thanks.
    
    The inner SELECT isn't really necessary - it is just hiding ?p so rename 
    that and don't have it in the outer projection:
    
    SELECT ?s ?p ?o
    FROM named_graph:m_p
    FROM NAMED named_graph:m_p_s
    WHERE
    {
       ?s ?p ?o
       GRAPH named_graph:m_p_s { ?o ?px w:frnd }
    }
    
         Andy
    
    On 13/10/17 11:14, Lorenz Buehmann wrote:
    > I answered it on StackOverflow. And the formulation of the question was
    > confusing. He probably meant that the subjects of the second graph are
    > the object of the first graph. Anything else wouldn't make sense...
    > 
    > 
    > Here is the data that I used for testing, thus, Andy could also use it:
    > 
    > Graph named_graph:m_p
    > 
    > @prefix : <http://ex.org/> .
    > :p1    :pred1    :mp1 .
    > :p2    :pred1    :mp1 .
    > :p3    :pred1    :mp2 .
    > :p4    :pred1    :mp2 .
    > :p5    :pred1    :mp3 .
    > :p6    :pred1    :mp3 .
    > 
    > 
    > Graph named_graph:m_p_s
    > 
    > @prefix : <http://ex.org/> .
    > @prefix w: <http://ex.org/w/> .
    > 
    > :mp1   :pred2    w:frnd .
    > :mp1   :pred2    w:fdlfkdl .
    > :mp2   :pred2    w:kdsjflk .
    > :mp2   :pred2    w:jflksdlkj .
    > :mp3   :pred2    w:frnd .
    > :mp3   :pred2    w:fjksldjfls .
    > 
    > 
    > Working query:
    > 
    > PREFIX named_graph: <http://ex.org/graphs/>
    > PREFIX w: <http://ex.org/w/>
    > 
    > SELECT *
    > FROM named_graph:m_p
    > FROM NAMED named_graph:m_p_s
    > WHERE
    > {
    >    ?s ?p ?o
    >    {
    >       SELECT ?o WHERE {
    >         GRAPH named_graph:m_p_s { ?o ?p w:frnd }
    >        }
    >    }
    > }
    > 
    > 
    > Cheers,
    > 
    > Lorenz
    > 
    > 
    > 
    > On 13.10.2017 10:20, Andy Seaborne wrote:
    >>
    >>
    >> On 13/10/17 02:36, Dimov, Stefan wrote:
    >>> Hello,
    >>>
    >>> I have two graphs:
    >>
    >> Which storage system are they in?
    >> Which version of Jena?
    >>
    >>>
    >>> m_p
    >>>
    >>> p1    pred1    mp1
    >>> p2    pred1    mp1
    >>> p3    pred1   mp2
    >>> p4    pred1   mp2
    >>> p5    pred1   mp3
    >>> p6    pred1   mp3
    >>>
    >>> and m_p_s
    >>>
    >>> mp1   pred2    w:frnd
    >>> mp1   pred2    w:fdlfkdl
    >>> mp2   pred2    w:kdsjflk
    >>> mp2   pred2   w:jflksdlkj
    >>> mp3   pred2   w:frnd
    >>> mp3   pred2   w:fjksldjfls
    >>
    >> Please could you provide complete data such as TriG?
    >>
    >> It is a barrier to volunteers who answers questions if the first thing
    >> you have to do is mangle email, data preparation and disentangle
    >> partial queries.
    >>
    >>> and I want to get all the triples in m_p which objects are predicates
    >>> in m_p_s and the object of that predicates in m_p_s is w:frnd
    >>>
    >>> In other words I want to make query that returns (results with) p1,
    >>> p2, p5 and p6 from m_p and doesn’t return p3 and p4.
    >>>
    >>> I’m trying to do this with nested queries,
    >>
    >> You don't need a nested SELECT.
    >>
    >> SELECT * {
    >>     GRAPH m_p   { ?s ?p ?o }
    >>     GRAPH m_p_s { ?x ?o w:frnd }
    >> }
    >>
    >> (untested)
    >>
    >>> but it doesn’t work: E.g.
    >>>
    >>>       SELECT $subj $pred $pr
    >>
    >> $subj and $pred are not set in the query.
    >>
    >> This isn't SQL! In SPARQL, variables get bound in graph patterns.
    >>
    >>>                   FROM NAMED named_graph:m_p
    >>
    >> The RDF dataset for this query is a single named graph and empty
    >> default graph.
    >>
    >> Did you mean:
    >>
    >> FROM NAMED named_graph:m_p
    >> FROM NAMED named_graph:m_p_s
    >>
    >> ?
    >>
    >> or indeed no FROM NAMED and use a dataset directly.
    >>
    >>
    >>>                   WHERE
    >>>       {
    >>>           SELECT $pr
    >>>           WHERE
    >>>           {
    >>>              GRAPH named_graph:m_p_s { $pr $pred0 w:frnd }
    >>
    >> the m_p_s graph isn't in the dataset hence this pattern is empty.
    >>
    >> GRAPH is for access; FROM NAMED for setting up.
    >>
    >>>           }
    >>>       }
    >>>
    >>> returns empty result. I tried different things, but either I get an
    >>> error
    >>
    >> What is the error?
    >>
    >>> or empty result or everything in m_p.
    >>>
    >>> I don’t want to use UNION or FILTER for performance reasons.
    >>>
    >>> Do you have an idea how I can do it?
    >>>
    >>> Regards,
    >>> Stefan
    >>>
    > 
    


Re: Nested select doesn't work as expected

Posted by Andy Seaborne <an...@apache.org>.
Thanks.

The inner SELECT isn't really necessary - it is just hiding ?p so rename 
that and don't have it in the outer projection:

SELECT ?s ?p ?o
FROM named_graph:m_p
FROM NAMED named_graph:m_p_s
WHERE
{
   ?s ?p ?o
   GRAPH named_graph:m_p_s { ?o ?px w:frnd }
}

     Andy

On 13/10/17 11:14, Lorenz Buehmann wrote:
> I answered it on StackOverflow. And the formulation of the question was
> confusing. He probably meant that the subjects of the second graph are
> the object of the first graph. Anything else wouldn't make sense...
> 
> 
> Here is the data that I used for testing, thus, Andy could also use it:
> 
> Graph named_graph:m_p
> 
> @prefix : <http://ex.org/> .
> :p1    :pred1    :mp1 .
> :p2    :pred1    :mp1 .
> :p3    :pred1    :mp2 .
> :p4    :pred1    :mp2 .
> :p5    :pred1    :mp3 .
> :p6    :pred1    :mp3 .
> 
> 
> Graph named_graph:m_p_s
> 
> @prefix : <http://ex.org/> .
> @prefix w: <http://ex.org/w/> .
> 
> :mp1   :pred2    w:frnd .
> :mp1   :pred2    w:fdlfkdl .
> :mp2   :pred2    w:kdsjflk .
> :mp2   :pred2    w:jflksdlkj .
> :mp3   :pred2    w:frnd .
> :mp3   :pred2    w:fjksldjfls .
> 
> 
> Working query:
> 
> PREFIX named_graph: <http://ex.org/graphs/>
> PREFIX w: <http://ex.org/w/>
> 
> SELECT *
> FROM named_graph:m_p
> FROM NAMED named_graph:m_p_s
> WHERE
> {
>    ?s ?p ?o
>    {
>       SELECT ?o WHERE {
>         GRAPH named_graph:m_p_s { ?o ?p w:frnd }
>        }
>    }
> }
> 
> 
> Cheers,
> 
> Lorenz
> 
> 
> 
> On 13.10.2017 10:20, Andy Seaborne wrote:
>>
>>
>> On 13/10/17 02:36, Dimov, Stefan wrote:
>>> Hello,
>>>
>>> I have two graphs:
>>
>> Which storage system are they in?
>> Which version of Jena?
>>
>>>
>>> m_p
>>>
>>> p1    pred1    mp1
>>> p2    pred1    mp1
>>> p3    pred1   mp2
>>> p4    pred1   mp2
>>> p5    pred1   mp3
>>> p6    pred1   mp3
>>>
>>> and m_p_s
>>>
>>> mp1   pred2    w:frnd
>>> mp1   pred2    w:fdlfkdl
>>> mp2   pred2    w:kdsjflk
>>> mp2   pred2   w:jflksdlkj
>>> mp3   pred2   w:frnd
>>> mp3   pred2   w:fjksldjfls
>>
>> Please could you provide complete data such as TriG?
>>
>> It is a barrier to volunteers who answers questions if the first thing
>> you have to do is mangle email, data preparation and disentangle
>> partial queries.
>>
>>> and I want to get all the triples in m_p which objects are predicates
>>> in m_p_s and the object of that predicates in m_p_s is w:frnd
>>>
>>> In other words I want to make query that returns (results with) p1,
>>> p2, p5 and p6 from m_p and doesn’t return p3 and p4.
>>>
>>> I’m trying to do this with nested queries,
>>
>> You don't need a nested SELECT.
>>
>> SELECT * {
>>     GRAPH m_p   { ?s ?p ?o }
>>     GRAPH m_p_s { ?x ?o w:frnd }
>> }
>>
>> (untested)
>>
>>> but it doesn’t work: E.g.
>>>
>>>       SELECT $subj $pred $pr
>>
>> $subj and $pred are not set in the query.
>>
>> This isn't SQL! In SPARQL, variables get bound in graph patterns.
>>
>>>                   FROM NAMED named_graph:m_p
>>
>> The RDF dataset for this query is a single named graph and empty
>> default graph.
>>
>> Did you mean:
>>
>> FROM NAMED named_graph:m_p
>> FROM NAMED named_graph:m_p_s
>>
>> ?
>>
>> or indeed no FROM NAMED and use a dataset directly.
>>
>>
>>>                   WHERE
>>>       {
>>>           SELECT $pr
>>>           WHERE
>>>           {
>>>              GRAPH named_graph:m_p_s { $pr $pred0 w:frnd }
>>
>> the m_p_s graph isn't in the dataset hence this pattern is empty.
>>
>> GRAPH is for access; FROM NAMED for setting up.
>>
>>>           }
>>>       }
>>>
>>> returns empty result. I tried different things, but either I get an
>>> error
>>
>> What is the error?
>>
>>> or empty result or everything in m_p.
>>>
>>> I don’t want to use UNION or FILTER for performance reasons.
>>>
>>> Do you have an idea how I can do it?
>>>
>>> Regards,
>>> Stefan
>>>
> 

Re: Nested select doesn't work as expected

Posted by Lorenz Buehmann <bu...@informatik.uni-leipzig.de>.
I answered it on StackOverflow. And the formulation of the question was
confusing. He probably meant that the subjects of the second graph are
the object of the first graph. Anything else wouldn't make sense...


Here is the data that I used for testing, thus, Andy could also use it:

Graph named_graph:m_p

@prefix : <http://ex.org/> .
:p1    :pred1    :mp1 .
:p2    :pred1    :mp1 .
:p3    :pred1    :mp2 .
:p4    :pred1    :mp2 .
:p5    :pred1    :mp3 .
:p6    :pred1    :mp3 .


Graph named_graph:m_p_s

@prefix : <http://ex.org/> .
@prefix w: <http://ex.org/w/> .

:mp1   :pred2    w:frnd .
:mp1   :pred2    w:fdlfkdl .
:mp2   :pred2    w:kdsjflk .
:mp2   :pred2    w:jflksdlkj .
:mp3   :pred2    w:frnd .
:mp3   :pred2    w:fjksldjfls .


Working query:

PREFIX named_graph: <http://ex.org/graphs/>
PREFIX w: <http://ex.org/w/>

SELECT *
FROM named_graph:m_p
FROM NAMED named_graph:m_p_s
WHERE
{
  ?s ?p ?o
  {
     SELECT ?o WHERE {
       GRAPH named_graph:m_p_s { ?o ?p w:frnd }
      }
  }
}


Cheers,

Lorenz



On 13.10.2017 10:20, Andy Seaborne wrote:
>
>
> On 13/10/17 02:36, Dimov, Stefan wrote:
>> Hello,
>>
>> I have two graphs:
>
> Which storage system are they in?
> Which version of Jena?
>
>>
>> m_p
>>
>> p1    pred1    mp1
>> p2    pred1    mp1
>> p3    pred1   mp2
>> p4    pred1   mp2
>> p5    pred1   mp3
>> p6    pred1   mp3
>>
>> and m_p_s
>>
>> mp1   pred2    w:frnd
>> mp1   pred2    w:fdlfkdl
>> mp2   pred2    w:kdsjflk
>> mp2   pred2   w:jflksdlkj
>> mp3   pred2   w:frnd
>> mp3   pred2   w:fjksldjfls
>
> Please could you provide complete data such as TriG?
>
> It is a barrier to volunteers who answers questions if the first thing
> you have to do is mangle email, data preparation and disentangle
> partial queries.
>
>> and I want to get all the triples in m_p which objects are predicates
>> in m_p_s and the object of that predicates in m_p_s is w:frnd
>>
>> In other words I want to make query that returns (results with) p1,
>> p2, p5 and p6 from m_p and doesn’t return p3 and p4.
>>
>> I’m trying to do this with nested queries,
>
> You don't need a nested SELECT.
>
> SELECT * {
>    GRAPH m_p   { ?s ?p ?o }
>    GRAPH m_p_s { ?x ?o w:frnd }
> }
>
> (untested)
>
>> but it doesn’t work: E.g.
>>
>>      SELECT $subj $pred $pr
>
> $subj and $pred are not set in the query.
>
> This isn't SQL! In SPARQL, variables get bound in graph patterns.
>
>>                  FROM NAMED named_graph:m_p
>
> The RDF dataset for this query is a single named graph and empty
> default graph.
>
> Did you mean:
>
> FROM NAMED named_graph:m_p
> FROM NAMED named_graph:m_p_s
>
> ?
>
> or indeed no FROM NAMED and use a dataset directly.
>
>
>>                  WHERE
>>      {
>>          SELECT $pr
>>          WHERE
>>          {
>>             GRAPH named_graph:m_p_s { $pr $pred0 w:frnd }
>
> the m_p_s graph isn't in the dataset hence this pattern is empty.
>
> GRAPH is for access; FROM NAMED for setting up.
>
>>          }
>>      }
>>
>> returns empty result. I tried different things, but either I get an
>> error
>
> What is the error?
>
>> or empty result or everything in m_p.
>>
>> I don’t want to use UNION or FILTER for performance reasons.
>>
>> Do you have an idea how I can do it?
>>
>> Regards,
>> Stefan
>>


Re: Nested select doesn't work as expected

Posted by Andy Seaborne <an...@apache.org>.

On 13/10/17 02:36, Dimov, Stefan wrote:
> Hello,
> 
> I have two graphs:

Which storage system are they in?
Which version of Jena?

> 
> m_p
> 
> p1    pred1    mp1
> p2    pred1    mp1
> p3    pred1   mp2
> p4    pred1   mp2
> p5    pred1   mp3
> p6    pred1   mp3
> 
> and m_p_s
> 
> mp1   pred2    w:frnd
> mp1   pred2    w:fdlfkdl
> mp2   pred2    w:kdsjflk
> mp2   pred2   w:jflksdlkj
> mp3   pred2   w:frnd
> mp3   pred2   w:fjksldjfls

Please could you provide complete data such as TriG?

It is a barrier to volunteers who answers questions if the first thing 
you have to do is mangle email, data preparation and disentangle partial 
queries.

> and I want to get all the triples in m_p which objects are predicates in m_p_s and the object of that predicates in m_p_s is w:frnd
> 
> In other words I want to make query that returns (results with) p1, p2, p5 and p6 from m_p and doesn’t return p3 and p4.
> 
> I’m trying to do this with nested queries,

You don't need a nested SELECT.

SELECT * {
    GRAPH m_p   { ?s ?p ?o }
    GRAPH m_p_s { ?x ?o w:frnd }
}

(untested)

> but it doesn’t work: E.g.
> 
>      SELECT $subj $pred $pr

$subj and $pred are not set in the query.

This isn't SQL! In SPARQL, variables get bound in graph patterns.

>                  FROM NAMED named_graph:m_p

The RDF dataset for this query is a single named graph and empty default 
graph.

Did you mean:

FROM NAMED named_graph:m_p
FROM NAMED named_graph:m_p_s

?

or indeed no FROM NAMED and use a dataset directly.


>                  WHERE
>      {
>          SELECT $pr
>          WHERE
>          {
>             GRAPH named_graph:m_p_s { $pr $pred0 w:frnd }

the m_p_s graph isn't in the dataset hence this pattern is empty.

GRAPH is for access; FROM NAMED for setting up.

>          }
>      }
> 
> returns empty result. I tried different things, but either I get an error

What is the error?

> or empty result or everything in m_p.
> 
> I don’t want to use UNION or FILTER for performance reasons.
> 
> Do you have an idea how I can do it?
> 
> Regards,
> Stefan
>