11 Replies Latest reply on Apr 15, 2008 1:28 PM by alwu-Oracle

    Optional parts of queries

    user625573
      Is there a way to mark parts of a triple pattern in a SEM_MATCH query as optional? C.f. the OPTIONAL clause in SPARQL. I suppose a query with an optional clause can be rewritten as SQL query consisting of the union of two inner triple queries, but if there is significant overlap between these two queries, this is less efficient. It would also makes queries complex when it has multiple OPTIONAL parts.

      Regards,
      Jan
        • 1. Re: Optional parts of queries
          Mannamal-Oracle
          Currently there is no way of doing so in a SEM_MATCH query. A workaround would be to use the Jena interface and use the Oracle-Jena integration to execute SPARQL queries (which supports OPTIONAL) against triples stored in Oracle.

          As you say, using union or an outer join will work with SEM_MATCH, but can be less efficient. The Jena interface is the current alternative.

          Adding OPTIONAL support to SEM_MATCH is planned in the future.

          Melli
          • 2. Re: Optional parts of queries
            24743
            It's very much needed (and I hope the "future" is not too far away).

            My work-around is to insert "rdf:NULL"s where necessary.

            Regards - Christian
            • 3. Re: Optional parts of queries
              user625573
              Thanks for the reply. I too hope this becomes available soon, as it's quite a limit on the types of queries that are possible. I will have a look at the Jena interface in the meantime.

              Regards,
              Jan
              • 4. Re: Optional parts of queries
                user625573
                Hi Christian,

                could you explain a bit further how you use "rdf:NULL" to work around this limitation?

                Thanks,
                Jan
                • 5. Re: Optional parts of queries
                  24743
                  Hi Jan

                  I put in "rdf:null" as objects when there are no data. F.ex: if a person has no middle name (s)he still should have the property "middelName" with value "rdf:null".

                  This is not a good solution and I am looking forward to "OPTIONAL".

                  I think "OPTIONAL" could be treated something like "NULL"s in the relational tables.

                  Regards - Christian
                  • 6. Re: Optional parts of queries
                    alwu-Oracle
                    For simple cases like the following,

                    PREFIX foaf: <http://xmlns.com/foaf/0.1/>
                    SELECT ?name ?mbox
                    WHERE { ?x foaf:name  ?name .
                    OPTIONAL { ?x  foaf:mbox  ?mbox }
                    }

                    one simple solution is to use two SEM_MATCH queries to "simulate" optional.

                    select name, null mbox from table(sem_match('(?x foaf:name ?name)',
                    sem_models('test'), null,
                    sem_aliases( sem_alias('foaf', 'http://xmlns.com/foaf/0.1/')),
                    null))
                    union all
                    select name, mbox from table(sem_match(
                    '(?x foaf:name ?name)(?name foaf:mbox ?mbox)',
                    sem_models('test'), null,
                    sem_aliases( sem_alias('foaf', 'http://xmlns.com/foaf/0.1/')),
                    null));

                    Your application needs to go over the result set and filter out
                    {name, null} pairs if there is corresponding {name, non_null_value} in the result set.
                    • 7. Re: Optional parts of queries
                      user625573
                      Oh I see, so you put the rdf:nulls in the model you are querying? Thanks for the tip, I won't be able to do that in my case though.

                      Regards,
                      Jan
                      • 8. Re: Optional parts of queries
                        user625573
                        alwu: as I mentioned in my original post, I guess the "union" approach is a workaround that may work for very simple queries, but very quickly becomes unmanagable for real queries (as well as being less efficient).

                        Regards,
                        Jan
                        • 9. Re: Optional parts of queries
                          alwu-Oracle
                          Union all (or union) approach is indeed for simple cases. Could you share with us some typical real OPTIONAL related queries?
                          • 10. Re: Optional parts of queries
                            user625573
                            alwu, I'm afraid I can't really share any real queries from my company on a public forum. :-)

                            But in general, OPTIONAL clauses are useful every time we query a model of our business domain where a property of an individual is not required. This is of course very common, just as it is common to have columns that are allowed to be NULL in relational database schemas.

                            Regards,
                            Jan
                            • 11. Re: Optional parts of queries
                              alwu-Oracle
                              Sorry I did not make myself clear. You don't have to provide the query as is. Just the structure will be enough. Or, you can email some typical queries to alan dot wu at oracle dot com.