Datetime type storing empty string for "0001-01-01T00:00:00Z"

I’m just getting started with Dgraph, but I’m hitting some behavior I don’t understand.

The gist of it is that if you create a predicate of type datetime and try to store the date 0001-01-01T00:00:00Z, the value will store as an empty string "". That value doesn’t seem to be a validate datetime, so it seems like it should (1) error if that value for a date isn’t acceptable, (2) treat it as though the predicate were omitted entirely, or (3) store it as is. Is there a reason or documentation for the current behavior?

I ran into this when doing my first interaction with Dgraph using the Go client. I queried for a node, made an update to an unrelated predicate, and then mutated it. when running the program a second time, part the submits the query and then unmarshals it back into my struct resulted in a json error which wasn’t there the first time the node was fetched: parsing time """" as ""2006-01-02T15:04:05Z07:00"": cannot parse """ as "2006". The problem was that the result now contained an empty string for a date value instead of omitting the field completely, as was the case the first time though before my mutate saved an empty string.

For this specific use case I could work around this by using pointers to time.Time values, which may be the right approach anyway. But I’d still like to figure out exactly what’s going on here.

I recently ran through the same problem. Would be great if someone could explain how to handle it with the go client. :slightly_smiling_face:

The issues seem specific to the following value “0001-01-01T00:00:00Z”.
I added this data and queried it back.

{
  "set": [
    {
      "created_at": "0000-01-01T00:00:00Z"
    },
    {
      "created_at": "0001-01-01T00:00:00Z"
    },
    {
      "created_at": "0002-01-01T00:00:00Z"
    },
    {
      "created_at": "0003-01-01T00:00:00Z"
    },
    {
      "created_at": "0001-02-02T00:00:00Z"
    }
	]
}

Query:

{
  q(func: has(created_at)) {
    uid
    created_at
  }
}

Result:

{
    "data": {
		"q": [
		  {
			"uid": "0x18",
			"created_at": "0000-01-01T00:00:00Z"
		  },
		  {
			"uid": "0x19",
			"created_at": ""
		  },
		  {
			"uid": "0x1a",
			"created_at": "0002-01-01T00:00:00Z"
		  },
		  {
			"uid": "0x1b",
			"created_at": "0003-01-01T00:00:00Z"
		  },
		  {
			"uid": "0x1c",
			"created_at": "0001-02-02T00:00:00Z"
		  }
		]
    }
}

Created a github issue for this.
https://github.com/dgraph-io/dgraph/issues/5364

1 Like

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

@arijit
Digging into this issue, I found that it is working as intended. The Time package considers
“0001-01-01T00:00:00Z” as a zero time, from which seconds are counted. In this particular line in dgraph we are checking if the value is zero time, then we return an empty string.

Now after this PR, when we are not accepting empty datetime in mutation, I think it would be right to return “0001-01-01T00:00:00Z” (a zero time) instead of an empty string.

What are your thoughts about it?

@Anurag FYI

Yeah, we should be returning the zero time(“0001-01-01T00:00:00Z”) instead of an empty string if the user stored that in the DB. Also look into the linked issue while you fix this to avoid introducing a regression.

1 Like

Yes. “0001-01-01T00:00:00Z” should be returned instead of an empty string in that case.

1 Like