semsol / arc2

ARC RDF Classes for PHP
Other
332 stars 89 forks source link

count(*) seems to break the SPARQLPLUSPARSER #63

Closed coreation closed 10 years ago

coreation commented 10 years ago

Hi all,

Upon performing the following query:

select (count(*) AS ?count) WHERE { ?s ?p ?o }

The SPARQLPLUSPARSER break, and throws an error that it cannot find an column value with key 'value'. After further investigation, I've come to the line where this is thrown which is line 51 (ish) in the corresponding php file of SPARQLPLUSPARSER.

After some debugging, I couldn't find what went wrong, but the value key wasnt present. This made me try the following work around, inspired by the comment that stands above the if which was "* or var" :

/* * or var */
    if ((list($sub_r, $sub_v) = $this->x('\*', $v)) && $sub_r) {

//[START WORKAROUND]
      // The regex fails to parse * from the count(*) as a selector
      $val = '*';

      if (!empty($sub_r['value'])) {
        $val = $sub_r['value'];
      }
// [END WORKAROUND]
      return array(array('var' => $val, 'aggregate' => $aggregate, 'alias' => $aggregate ? $result_var : ''), $sub_v);
    }

Does anyone have an idea if this might break other things, I'm going to try some queries with this workaround, also non * selectors. In any case, I'll do a fork and do my queries through the fork. If this is a valid bug, and this (sorta) solves it. I'd be happy to provide a pull request.

bnowack commented 10 years ago

Wow, how could that go unnoticed for so long... Could you try wrapping the *-regex in parentheses? That should be the fix I think:

if ((list($sub_r, $sub_v) = $this->x('(*)', $v)) && $sub_r) {

coreation commented 10 years ago

Hi @bnowack , no idea how that went on for so long ;) The fix sorta works in that it returns an array with "*" as a value in it, however it's not mapped to the key "value", which is called in the next line. It currently sits in the first value, making me think that it just gets there because of the preg_match function (index 1 = first match?)

I'm not sure what I should change now, because the function x() is heavily used, so tinkering with it seems a big "nono". So probably something needs to be changed in the line where that "value" index is used?

bnowack commented 10 years ago

The $sub_r doesn't really make any sense there, to be honest. I've pushed a tweak, want to give it a try? Thanks!

coreation commented 10 years ago

That seems to do the trick yes ;).

coreation commented 10 years ago

Unrelated to this issue, but not wanting to create another issue When retrieving triples from the arc2 store, it's basically an assoc. array. How does one serialize this into a turtle or other structures, or even better, put this back into a arc2 graph? I don't seem to find this in the documentation :).

bnowack commented 10 years ago

When you use a query that uses ?s ?p ?o (i.e. SELECT ?s ?p ?o WHERE ...) then you can use the result array ($queryResult['result']['rows']) as a typical ARC structure that can be serialized.

coreation commented 10 years ago

Ah, that's what I initially thought but hadn't found the time yet to test it out. Thanks a bunch!