# \`IllegalArgumentException\` when trying to order by \`xsd:dateTimeStamp\`

**URL:** https://community.stardog.com/t/illegalargumentexception-when-trying-to-order-by-xsd-datetimestamp/4484
**Category:** Bug
**Created:** [July 14, 2023, 7:04pm UTC](https://community.stardog.com/t/illegalargumentexception-when-trying-to-order-by-xsd-datetimestamp/4484 "2023-07-14T19:04:15Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ProvocaTeach](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/provocateach/32/1869_2.png) [@ProvocaTeach](https://community.stardog.com/u/ProvocaTeach)
#### Post date: [July 14, 2023, 7:04pm UTC](https://community.stardog.com/t/illegalargumentexception-when-trying-to-order-by-xsd-datetimestamp/4484/1 "2023-07-14T19:04:15Z")

</div>

Hello,

I have been trying to use SPARQL’s `ORDER BY` to order some blog posts by the date they were made public. Each blog post has a property called `theo:date_made_public` which is an `xsd:dateTimeStamp` (note: **not** an `xsd:dateTime`).

When I try to `ORDER BY DESC(?date_made_public)` I get an error (see Query A). But when I comment out that line, or change `?date_made_public` to a string variable like `?piece_title`, I get results (see Query B).

At first, I thought the issue might be the `<` operator, since the [SPARQL spec](https://www.w3.org/TR/sparql11-query/) only defines it for `xsd:dateTime` and does not mention `xsd:dateTimeStamp`. But then I realized the `FILTER` line works, so it can’t be that.

Am I doing something wrong, or is this a bug? (I am using Stardog Cloud.)

## Query A

```auto
PREFIX theo: <https://rdf.provocateach.art/theory-ontology#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?piece_title ?piece_slug ?date_made_public
WHERE {
	GRAPH ?g1 {
		?blog_piece
				a theo:Content_Piece ;
				theo:piece_slug ?piece_slug ;
				theo:piece_title ?piece_title ;
				theo:date_made_public ?date_made_public .
	}
	FILTER (?date_made_public < NOW())
}
ORDER BY DESC( ?date_made_public ) # this is the offending line
OFFSET 0
LIMIT 10

```

## Result A

```plaintext
Failed to run query: com.complexible.stardog.plan.eval.operator.OperatorException: Uncaught error during query evaluation: IllegalArgumentException: 2020-05-02T10:00-05:00

```

## Query B

```auto
PREFIX theo: <https://rdf.provocateach.art/theory-ontology#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?piece_title ?piece_slug ?date_made_public
WHERE {
	GRAPH ?g1 {
		?blog_piece
				a theo:Content_Piece ;
				theo:piece_slug ?piece_slug ;
				theo:piece_title ?piece_title ;
				theo:date_made_public ?date_made_public .
	}
	FILTER (?date_made_public < NOW())
}
# ORDER BY DESC( ?date_made_public )
OFFSET 0
LIMIT 10

```

## Result B

```json
{
  "head": {
    "vars": [
      "piece_title",
      "piece_slug",
      "date_made_public"
    ]
  },
  "results": {
    "bindings": [
      {
        "piece_slug": {
          "datatype": "http://www.w3.org/2001/XMLSchema#token",
          "type": "literal",
          "value": "the-playful-magic-of-humor-in-education"
        },
        "piece_title": {
          "xml:lang": "en",
          "type": "literal",
          "value": "The Playful Magic of Humor in Education"
        },
        "date_made_public": {
          "datatype": "http://www.w3.org/2001/XMLSchema#dateTimeStamp",
          "type": "literal",
          "value": "2020-05-02T10:00-05:00"
        }
      },
      {
        "piece_slug": {
          "datatype": "http://www.w3.org/2001/XMLSchema#token",
          "type": "literal",
          "value": "media-literacy-is-not-a-checklist"
        },
        "piece_title": {
          "xml:lang": "en",
          "type": "literal",
          "value": "Media Literacy is Not a Checklist"
        },
        "date_made_public": {
          "datatype": "http://www.w3.org/2001/XMLSchema#dateTimeStamp",
          "type": "literal",
          "value": "2020-06-03T08:00-07:00"
        }
      },
      {
        "piece_slug": {
          "datatype": "http://www.w3.org/2001/XMLSchema#token",
          "type": "literal",
          "value": "how-to-drill-and-kill-responsibly"
        },
        "piece_title": {
          "xml:lang": "en",
          "type": "literal",
          "value": "How to Drill and Kill Responsibly"
        },
        "date_made_public": {
          "datatype": "http://www.w3.org/2001/XMLSchema#dateTimeStamp",
          "type": "literal",
          "value": "2023-07-07T08:00-07:00"
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

### Author: ![PaulJackson](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pauljackson/32/243_2.png) [@PaulJackson](https://community.stardog.com/u/PaulJackson)
#### Post date: [July 14, 2023, 7:31pm UTC](https://community.stardog.com/t/illegalargumentexception-when-trying-to-order-by-xsd-datetimestamp/4484/2 "2023-07-14T19:31:40Z")

</div>

Do you get the same error when creating the query plan? If not, could you share the plan?

Thanks,  
-Paul

---

<div class="post-metadata">

### Author: ![pavel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pavel/32/18_2.png) [@pavel](https://community.stardog.com/u/pavel)
#### Post date: [July 14, 2023, 8:22pm UTC](https://community.stardog.com/t/illegalargumentexception-when-trying-to-order-by-xsd-datetimestamp/4484/3 "2023-07-14T20:22:17Z")

</div>

No, you're not doing anything wrong. What's your Cloud endpoint or username (so we can check the logs).

Best,  
Pavel

---

<div class="post-metadata">

### Author: ![ProvocaTeach](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/provocateach/32/1869_2.png) [@ProvocaTeach](https://community.stardog.com/u/ProvocaTeach)
#### Post date: [July 14, 2023, 8:30pm UTC](https://community.stardog.com/t/illegalargumentexception-when-trying-to-order-by-xsd-datetimestamp/4484/4 "2023-07-14T20:30:06Z")

</div>

Paul, no errors when I run the query plan. Here’s the output:

```plaintext
prefix theo: <https://rdf.provocateach.art/theory-ontology#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>

Slice(offset=0, limit=10) [#1]
`─ OrderBy(DESC(?date_made_public), offset=0, limit=10) [#1]
   `─ Projection(?piece_title, ?piece_slug, ?date_made_public) [#1]
      `─ NaryJoin(?blog_piece) [#1]
         +─ Scan[PSOC](?blog_piece, theo:piece_title, ?piece_title){?g1} [#1]
         +─ Scan[PSOC](?blog_piece, theo:piece_slug, ?piece_slug){?g1} [#1]
         +─ Scan[POSC](?blog_piece, <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>, theo:Content_Piece){?g1} [#1]
         `─ Filter(?date_made_public < "2023-07-14T20:26:29.625Z"^^xsd:dateTime) [#1]
            `─ Scan[PSOC](?blog_piece, theo:date_made_public, ?date_made_public){?g1} [#1]

```

Pavel, my endpoint is `https://sd-0d8a4a54.stardog.cloud:5820/prte-development`

---

<div class="post-metadata">

### Author: ![pavel](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/pavel/32/18_2.png) [@pavel](https://community.stardog.com/u/pavel)
#### Post date: [July 14, 2023, 8:59pm UTC](https://community.stardog.com/t/illegalargumentexception-when-trying-to-order-by-xsd-datetimestamp/4484/5 "2023-07-14T20:59:11Z")

</div>

Thanks, we got the logs and we will look into it. Regardless of the data, any data mistyping errors should not terminate query execution, can only make values incomparable. Filters handle these cases correctly but there might be an issue with `ORDER BY`.

I probably need to double check in the XMLSchema spec but it could be an invalid `xsd:dateTimeStamp` literal (missing seconds).

Thanks,  
Pavel

---

<div class="post-metadata">

### Author: ![ProvocaTeach](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/provocateach/32/1869_2.png) [@ProvocaTeach](https://community.stardog.com/u/ProvocaTeach)
#### Post date: [July 15, 2023, 8:38pm UTC](https://community.stardog.com/t/illegalargumentexception-when-trying-to-order-by-xsd-datetimestamp/4484/6 "2023-07-15T20:38:02Z")

</div>

You were right, it was that I forgot to specify the seconds on the `xsd:dateTimeStamp` values. Thank you!
