如果没有匹配的三重模式,Jena ARQ returns AVG() 的非空结果

Jena ARQ returns non-empty result for AVG() if there is no matching triple pattern

我有一个使用 AVG() 运算符的查询:

SELECT (AVG(?z) AS ?avg) { ?x <http://ex.com/value> ?z }

假设三元组存储没有匹配给定三元组模式的三元组,那么我们期望 (至少我有) 查询应该 return 为空结果。而 Virtuoso 实际上 return 的结果是空的,你可以使用 DBpedia 的 SPARQL 端点来检查 (execute).

但是 Fuseki 和 Jena ARQ return 非空结果:0。您可以在 sparql.org (execute) 上查看。

是否可以将 Jena ARQ 配置为 return 给定查询的空结果?如果是,那又如何?

we expect (at least I do) that the query should return empty result. And Virtuoso actually returns empty result, you can use DBpedia's SPARQL endpoint to check.

But Fuseki and Jena ARQ return non-empty result: 0. You can check it on sparql.org.

我不知道你是否可以改变avg的行为,但你可能不应该,因为 Jena 在这里做对了,而 Virtuoso(DBpedia 的端点)做错了。 SPARQL 1.1标准专门定义了组为空时avg到return:

18.5.1.4 Avg

The Avg set function calculates the average value for an expression over a group. It is defined in terms of Sum and Count.

Definition: Avg numeric Avg(multiset M)

Avg(M) = "0"^^xsd:integer, where Count(M) = 0

Avg(M) = Sum(M) / Count(M), where Count(M) > 0

也就是说,您可以使用 if 检查计数是否为零,return 在这种情况下是未定义的值,否则是平均值。这也适用于任何端点,而不仅仅是 ARQ。我在这里使用 values 来引入一个具有未定义值的变量,但您也可以很容易地使用会产生错误的表达式,例如 1/0。 (当然,存在实现可以扩展运算符行为的危险,因此实际上很难保证任何特定表达式都会出错。)

select (if(count(?x) = 0,?undef,avg(?x)) as ?average) where {
      values ?x { 2 3 4 }
      values ?undef { undef }
}
group by ?undef

-----------
| average |
===========
| 3.0     |
-----------

并且在没有值的情况下 ?x:

select (if(count(?x) = 0,?undef,avg(?x)) as ?average) where {
  values ?x {}
  values ?undef { undef }
}
group by ?undef

-----------
| average |
===========
|         |
-----------