Comparability of xsd:date, xsd:gYearMonth, and xsd:gYear in SPARQL Filter

Hi there

I have a question regarding xsd date formats (no worries, timezones are not part of question ... ;-)).

Given some data (see upload below) using dates with different precisions (day, month, year) , I'd like to do searches with arbitrary precisions:

PREFIX schema: <http://schema.org/>

SELECT distinct ?proj ?projName ?projStarted WHERE {
    ?proj a schema:ResearchProject .
    
    ?proj schema:startDate ?projStarted ;
          schema:name ?projName .
    
    
    FILTER(?projStarted > "2019-03"^^xsd:gYearMonth)
}

Given three projects with the dates 2019-03-01^^xsd:date, 2019^^xsd:gYear, and 2019-11^^xsd:gYearMonth I get the results:

http://www.example.com/project-1 test project 1 2019-03-01
http://www.example.com/project-3 test project 3 2019-11

What are the rules applied here to compare dates with different precisions? Is this specific to Stardog or are there defined relations in the specs between xsd:date, xsd:gYearMonth, and xsd:gYear?

I understand why 2019 does not show up, but why does 2019-03-01?

Thanks a lot for any hint. Dates seem a simple thing but in fact they are not (thinking of calendars) :slight_smile:

data.json (789 Bytes)

Just for a change the behavior is not directly related to date/times. The SPARQL spec does not allow comparisons between literals with different datatypes (except comparisons between numeric datatypes) but allows implementations to extend this behavior which is what Stardog does.

For literals with different datatypes, Stardog by default compares the lexical values of literals (which is why "2019-03-01"^^xsd:date > "2019-03"^^xsd:gYearMonth) and if they are equal compares the datatype IRIs (e.g. "2019-03-01"^^xsd:date < "2019-03-01"^^xsd:string).

There is a configuration option that you can flip to get back the strict spec behavior but this might be even more confusing because:

SELECT * WHERE {
    VALUES ?projStarted { "2019-03-01"^^xsd:date "2019"^^xsd:gYear "2019-11"^^xsd:gYearMonth }
    FILTER(?projStarted > "2019-03"^^xsd:gYearMonth)
}

would return only "2019-11"^^xsd:gYearMonth but changing the condition to < would make the query return 0 results.

Best,
Evren

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.