Uploaded image for project: 'Blazegraph (by SYSTAP)'
  1. Blazegraph (by SYSTAP)
  2. BLZG-1119

problems with UNIONs + complex OPTIONAL groups

    XMLWordPrintable

    Details

      Description

      I am running a complex query. Greatly simplified the part of interest here would ideally be expressed as:

      select *
      
        WHERE
        { BIND(1 as ?A)
          { BIND(2 as ?B) }
           UNION
          { BIND(3 as ?C) }
           UNION
          { }
      OPTIONAL { BIND( 'unbound' as ?D ) }
      OPTIONAL { BIND( 'unbound' as ?B ) }
      OPTIONAL { BIND( 'unbound' as ?C ) }
      } 
      
      

      This does not work (because of issues in the complex optimizer?)
      I have been using the following workaround code that uses named subqueries to have only one optional block per query

      select DISTINCT *
      WITH {
        SELECT *
        WHERE
        { BIND(1 as ?A)
          { BIND(2 as ?B) }
           UNION
          { BIND(3 as ?C) }
           UNION
          { }
        } 
      } AS %q
        
      WITH {
      SELECT *
      WHERE {
          INCLUDE %q
          # OPTIONAL { BIND( 'unbound' as ?D ) }
          OPTIONAL { BIND( 'unbound' as ?B ) }
      }} AS %_union_sub_q_1
      WITH {
      SELECT *
      WHERE {
          INCLUDE %_union_sub_q_1
          OPTIONAL { BIND( 'unbound' as ?C ) }
      }} AS %_union_sub_q_2
      WITH {
      SELECT *
      WHERE {
          INCLUDE %_union_sub_q_2
          # OPTIONAL { BIND( 'unbound' as ?B ) }
          OPTIONAL { BIND( 'unbound' as ?D ) }
      }} AS %__MainQuery
                
      WHERE {
        INCLUDE %__MainQuery
      }
      

      This gives the correct results, but note that the DISTINCT is needed despite being unnecessary in theory.
      Also note that swapping the order of the OPTIONAL (BINDs ) then gives incorrect results, i.e.

      select DISTINCT *
      WITH {
        SELECT *
        WHERE
        { BIND(1 as ?A)
          { BIND(2 as ?B) }
           UNION
          { BIND(3 as ?C) }
           UNION
          { }
        } 
      } AS %q
        
      WITH {
      SELECT *
      WHERE {
          INCLUDE %q
          OPTIONAL { BIND( 'unbound' as ?D ) }
          # OPTIONAL { BIND( 'unbound' as ?B ) }
      }} AS %_union_sub_q_1
      WITH {
      SELECT *
      WHERE {
          INCLUDE %_union_sub_q_1
          OPTIONAL { BIND( 'unbound' as ?C ) }
      }} AS %_union_sub_q_2
      WITH {
      SELECT *
      WHERE {
          INCLUDE %_union_sub_q_2
          OPTIONAL { BIND( 'unbound' as ?B ) }
          # OPTIONAL { BIND( 'unbound' as ?D ) }
      }} AS %__MainQuery
                
      WHERE {
        INCLUDE %__MainQuery
      }
      

      Includes a result row with both ?B and ?C bound.
      Please suggest a workaround that allows me to bind the literal 'unbound' to a list of variables that may or may not be bound after the execution of the query (which is actually a named subquery ....) [Or better fix the issue in the complex optimizer ...)

        Attachments

          Activity

            People

            Assignee:
            michaelschmidt michaelschmidt
            Reporter:
            jeremycarroll jeremycarroll
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: