Is the total number of triples in a graph dataset even?

Hi,

Can we formulate a SPARQL query to answer the above question?

Thanks,
Pouya

Your question Is the total number of triples in a graph dataset even , please provide more detail/context to the question …

Let’s assume that I have loaded an RDF dataset on Virtuoso. I am now interested to have a SPARQL query (and submit it through isql) to return “even” if the total number of triples in my loaded dataset is even.

I still have no idea what you mean by even. Please provide a verbatim example of what you mean with a sequence of queries run from isql to clearly demonstrate this …

@hwilliams - I’m betting that @Pouya wants to know whether the value returned by –

SELECT ( COUNT (*) AS ?NumberOfTriples )
WHERE { ?s ?p ?o }
LIMIT 1

– is ODD (1, 3, 5, …) or EVEN (2, 4, 6, …).

I can’t think of why this might be worth knowing, nor have I been able to quickly build an appropriate SPARQL query, but I imagine someone else reading this can.

Thank you @TallTed. Yes, this is exactly what I meant. I do not know why, but I cannot imagine someone can do it.

@Pouya —

You can run the query below to get the answer you’re looking for.

Click to see it live on DBpedia (query form, live results), and on DBpedia-Live (query form, live results).

As of this writing, DBpedia is ODD, and DBpedia-Live is EVEN.

SELECT ?TotalTriples
       ?modulo
       ?OddOrEven
  WHERE
    {
      BIND ( IF ( ?modulo = 0 , "EVEN" , "ODD" ) AS ?OddOrEven )
        { 
          SELECT ( ( COUNT ( * ) ) AS ?TotalTriples ) 
                 ( bif:MOD ( COUNT ( * ) , 2 ) AS ?modulo )
          WHERE
            {
              ?s ?p ?o .
            }
          LIMIT 1
        }
    }
1 Like

I guess that bif:MOD comes from Virtuoso’s SQL engine. I was more looking for a SPARQL-only query to do the job.

Since you raised the question here on a Virtuoso-focused forum, it seemed likely that you would be running the query on Virtuoso, so yes, I did provide a Virtuoso-specific solution.

It may surprise you that this was my best option, as, regrettably, the current SPARQL 1.1 specifications do not include a modulo or MOD function (as it is not part of XQuery 1.0 and XPath 2.0 Formal Semantics (Second Edition)) — so there’s no generic SPARQL 1.1 query to deliver the answer you sought! If you need to run a similar query on a different SPARQL engine, you’ll need to refer to that engine’s documentation or support team.

(You might add your voice to those contemplating enhancements for a future SPARQL 1.2, where adding support for XPath and XQuery Functions and Operators 3.1 would bring support for op:numeric-mod, with a generic SPARQL 1.2 syntax.)

It is actually possible in SPARQL 1.1 but not obvious to determine if a value is even or odd.

SELECT ?number (IF((STRENDS(STR(xsd:decimal(?number)/2),".5")), "odd", "even")  AS ?oddOrEven)
WHERE {
  VALUES ?number { 1 2 3 4 }
}
1 Like

You are correct, @jerven, though wow what a complex expression is needed to achieve the results of MOD (or bif:MOD)!

Using your expression on DBpedia (query form, live results), and on DBpedia-Live (query form, live results), we can see that as of this writing, DBpedia is ODD, and DBpedia-Live is EVEN.

Thanks for your useful addition!

1 Like

Thanks @jerven . That’s great.
Also, thanks @TallTed