Page MenuHomePhabricator

NullPointerException when subtracting dates
Closed, ResolvedPublic

Description

This query will lead to a NullPointerException most of the time:

SELECT ?person ?age
WHERE
{
  ?person wdt:P31 wd:Q5;
          wdt:P569 ?dateOfBirth;
          wdt:P570 ?dateOfDeath.
  BIND(?dateOfDeath - ?dateOfBirth AS ?age).
}

Link

The bottom of the stack trace is:

Caused by: java.lang.NullPointerException
	at org.wikidata.query.rdf.blazegraph.inline.literal.WikibaseDateExtension.canInvokeMathOp(WikibaseDateExtension.java:133)
	at com.bigdata.rdf.internal.constraints.MathBOp.get(MathBOp.java:167)
	at com.bigdata.rdf.internal.constraints.MathBOp.get(MathBOp.java:56)
	at com.bigdata.rdf.internal.constraints.ConditionalBind.get(ConditionalBind.java:133)
	at com.bigdata.rdf.internal.constraints.ProjectedConstraint.accept(ProjectedConstraint.java:77)
	at com.bigdata.rdf.internal.constraints.TryBeforeMaterializationConstraint.accept(TryBeforeMaterializationConstraint.java:103)
	... 12 more

I suspect this happens whenever one of the variables is *no value* or *unknown value*.

No known workaround.

Event Timeline

Restricted Application added subscribers: Zppix, Aklapper. · View Herald Transcript
Smalyshev triaged this task as High priority.
Smalyshev moved this task from Incoming to Current work on the Wikidata-Query-Service board.

"no value" would not match wdt:*, but "unknown value" could.

BTW, this query is huge, I'd seriously recommend running it with limit.

The trigger is wd:Q4165863:

SELECT ?person ?age
WHERE
{
  BIND(wd:Q4165863 as ?person)
  ?person wdt:P569 ?dateOfBirth;
          wdt:P570 ?dateOfDeath.
  BIND(?dateOfDeath - ?dateOfBirth AS ?age).
}

and it fails on wdq1 but not on wdq2.

Huh, so it’s the discrepancy in the precision?

I ran the query a few times with LIMIT 10 before reporting this, and when it did go through, the results were all old Pharaohs (probably because the index for those properties is ordered by date, I guess?), which don’t have sub-year precision. Perhaps that worked because both dates had the same precision.

The difference is that wdq1 somehow has old data where ?dateOfDeath for Q4165863 is an invalid date string. Still should not do NPE, but at least clear what the difference is. Once I am able to reproduce it consistently, I'll update Q4165863 and the issue should be gone.

Still advise using limits on Q5 queries. 1M+ replies slow down things for everybody.

Ah… I should have thought of that. Because I actually saw an odd date string for that item at one point, and fixed it. Sorry, I really should have mentioned that. (But it probably won’t be the only item with such a date string…)

Change 298676 had a related patch set uploaded (by Smalyshev):
Don't try to check type when value has no type

https://gerrit.wikimedia.org/r/298676

Change 298676 merged by jenkins-bot:
Don't try to check type when value has no type

https://gerrit.wikimedia.org/r/298676

Fixed, will be deployed next Monday.