You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by GitBox <gi...@apache.org> on 2021/08/22 16:14:20 UTC
[GitHub] [drill] luocooong opened a new pull request #2299: DRILL-7989: Use the UTC formatter in the JSON reader
luocooong opened a new pull request #2299:
URL: https://github.com/apache/drill/pull/2299
# [DRILL-7989](https://issues.apache.org/jira/browse/DRILL-7989): Use the UTC formatter in the JSON reader
## Description
MongoDB use the UTC format to specify the date value by default. But the JSON reader (old version) use the fixed date formatter : `yyyy-MM-dd'T'HH:mm:ss.SSSXX`. Need to change to the `yyyy-MM-dd'T'HH:mm:ss.SSS'Z'` format.
For even more information see the tech talk #2298
## Documentation
N/A
## Testing
Added the Test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] paul-rogers commented on pull request #2299: DRILL-7989: Use the UTC formatter in the JSON reader
Posted by GitBox <gi...@apache.org>.
paul-rogers commented on pull request #2299:
URL: https://github.com/apache/drill/pull/2299#issuecomment-905020455
Hi @luocooong, thanks for the explanation. You probably know more about Mongo than I do: my knowledge comes from the Mongo specs. The code you highlighted suggests that Mongo is sending BSON (?), which the Mongo client converts to JSON (?).
So, we have a number of unknowns:
* What is the format of data sent from Mongo to Drill? JSON? BSON? Something else?
* Which code creates the JSON that we want tDrill to parse? What is the format of that JSON?
* Which of the Mongo-specified date formats does that JSON include?
* How do we ensure the "old" reader correctly reads the JSON which the Mongo plugin gives it?
I don't really understand the first three items: my knowledge is limited only to the Mongo specs, the "new" JSON reader and, to some degree, to the "old" JSON reader. Please feel free to explain how you actually see Mongo working to fill in the unknowns above.
The key point in my previous comments on this PR is that Mongo appears to provide dates in UTC: at least, that is what the specs say. Since Drill stores dates in local time, conversion from UTC-to-local is necessary when converting Mongo JSON to Drill value vectors. I've assumed that the JSON we want to parse follows the Mongo specs (and thus is in UTC.) If the JSON is something else (dates are already in local time?), then we have to rethink the approach.
BTW: that Drill uses local time for timestamps is something that has long been debated. It is far too late to change the existing types. Folks have proposed adding a `UTC_TIMESTAMP` type, though the work has never been completed.
I think you'll need to poke around a bit and explain what you see happening to fill in the unknowns listed above.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] paul-rogers commented on pull request #2299: DRILL-7989: Use the UTC formatter in the JSON reader
Posted by GitBox <gi...@apache.org>.
paul-rogers commented on pull request #2299:
URL: https://github.com/apache/drill/pull/2299#issuecomment-905830673
@luocooong, thanks for the explanation. The "new" JSON reader supports both date formats. Our question is about the "old" one.
As noted in a previous comment: the code change looks fine except that it seems that we are storing a UTC time value in a Drill TIMESTAMP vector. The tests seem to verify this. (But, I could be wrong.)
Using UTC in Drill will seem to work if we only consider Mongo, and only consider a scan query. Things won't work if we use those UTC dates with parts of Drill that expect local times. Here is a quick check: get the current time in UTC. Pass that to the old JSON reader in Mongo's relaxed format. Then, use Drill's newly revised `AGE()` function to compute the difference between the column value and the current time. The output should be just a few seconds or less. Of course, this will always work if your machine's time zone is UTC. But, if it is anything else, and we don't do the conversion correctly, then the `AGE()` function will return the difference between UTC and local time, which is wrong in this test case.
Time conversions are confusing. I hope the above makes sense.
If you can fix that issue (or explain why the code does, in fact, work correctly), we'll be good to go.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] luocooong commented on pull request #2299: DRILL-7989: Use the UTC formatter in the JSON reader
Posted by GitBox <gi...@apache.org>.
luocooong commented on pull request #2299:
URL: https://github.com/apache/drill/pull/2299#issuecomment-913158816
@paul-rogers Thanks for the information. YES, the time-zone is always GMT+8. The good thing is, I have pushed a new revision patch. The issue is that there was an error in the time conversion. [UtcTimestampValueListener](https://github.com/apache/drill/blob/master/exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/json/values/UtcTimestampValueListener.java) is a good solution.
```
// query text
select `date`, AGE(`date`, CAST('2019-09-30 20:47:43' as timestamp)) from ...
// output
date<TIMESTAMP(OPTIONAL)> EXPR$1<INTERVAL(OPTIONAL)>
2019-10-01T04:47:43.123 PT28800.123S
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] luocooong commented on pull request #2299: DRILL-7989: Use the UTC formatter in the JSON reader
Posted by GitBox <gi...@apache.org>.
luocooong commented on pull request #2299:
URL: https://github.com/apache/drill/pull/2299#issuecomment-904520267
@paul-rogers Thanks for the review. I think you said number is the `Canonical Format`? such as
```json
{"$date":{"$numberLong":"1565546054692"}}
```
I have tracked the mongo reader code, then found the mongo driver only uses the `Relaxed Format`.
```java
// org.apache.drill.exec.store.mongo.MongoRecordReader
String doc = cursor.next().toJson();
jsonReader.setSource(doc.getBytes(Charsets.UTF_8));
// com.mongodb.internal.connection.ByteBufBsonDocument
public String toJson() {
return toJson(JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build());
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] luocooong commented on pull request #2299: DRILL-7989: Use the UTC formatter in the JSON reader
Posted by GitBox <gi...@apache.org>.
luocooong commented on pull request #2299:
URL: https://github.com/apache/drill/pull/2299#issuecomment-912984109
@paul-rogers Thanks for the support, there is a new check result.
Use the AGE() function in the web console :
1. Setting the current date time to `2021-09-04 10:10:00`, the timezone is GMT+8.
2. Run and check the result :
```sql
SELECT AGE(CURRENT_TIMESTAMP, CAST('2021-09-04 10:10:00' AS TIMESTAMP)) FROM (VALUES(1))
```
```sql
EXPR$0
PT31.859S
```
3. As the above, I think the CAST function read the value as UTC directly.
Add the AGE() in the `testDateJsonInput()` :
1. Update the code (the json value is '2019-09-30T20:47:43.123Z') :
```java
@Test
public void testDateJsonInput() throws Exception {
test("select `date`, AGE(`date`, CAST('2019-09-30 20:47:43' as timestamp)) from cp.`jsoninput/input2.json` limit 1 ");
}
```
2. Run the test and check the result :
```
date<TIMESTAMP(OPTIONAL)> EXPR$1<INTERVAL(OPTIONAL)>
2019-09-30T20:47:43.123 PT0.123S
```
Actually, In the `writeTimestamp(boolean isNull)` function, the revision code will get the date time value with the UTC formatter, at the final, will convert to local time (not the UTC) and store the epoch value to vector.
Did I miss anything?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] luocooong commented on pull request #2299: DRILL-7989: Use the UTC formatter in the JSON reader
Posted by GitBox <gi...@apache.org>.
luocooong commented on pull request #2299:
URL: https://github.com/apache/drill/pull/2299#issuecomment-905316748
@paul-rogers Thank you for the question. In short, the mongo storage plugin of Drill supported both the BSON loader and JSON loader. The BSON loader is the default handler, unless user set the `store.mongo.bson.record.reader` to `false` (In this case, Drill will use the JSON loader to parse the JSON string).
https://github.com/apache/drill/blob/7d4d75a9bb9b4b697ef5bab74c2156810e31dc9b/contrib/storage-mongo/src/main/java/org/apache/drill/exec/store/mongo/MongoRecordReader.java#L197-L204
We see that mongo driver will convert the data structure to JSON string (enabled the JSON loader).
In the mongo-spec, the driver supported two JSON text output format : `Canonical Format` and `Relaxed Format`. Notably, the Java driver use the `Relaxed` format as output when call the `cursor.next().toJson()`. So we don't see both of the following date format at the same time.
```java
// Canonical
{"$date":{"$numberLong":"1565546054692"}}
// Relaxed
{"$date":"2019-08-11T17:54:14.692Z"}
```
If application need the `Canonical` format, the mongo reader code is that :
```java
JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();
String doc = cursor.next().toJson(settings);
```
It is showed that the JSON output format is depending on the application.
```java
// Canonical output
{"_id": {"$oid": "61226ac33b29ff6306b36aa9"}, "date": {"$date": {"$numberLong": "1629645507258"}}}
// Relaxed output
{"_id": {"$oid": "61226ac33b29ff6306b36aa9"}, "date": {"$date": "2021-08-22T15:18:27.258Z"}}
```
I think I've replied the first three items. Once the toJson() is done, the next operation (parse & save to vector value) is no difference than query the JSON files. The only issue is that we are now using an old version of JSON loader, the new JSON loader contains more complete unit tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] paul-rogers commented on pull request #2299: DRILL-7989: Use the UTC formatter in the JSON reader
Posted by GitBox <gi...@apache.org>.
paul-rogers commented on pull request #2299:
URL: https://github.com/apache/drill/pull/2299#issuecomment-913059912
Hi @luocooong, thanks for your work on this one.
You've uncovered another "tender point" in Drill. A quick check of the [documentation](http://drill.apache.org/docs/data-type-conversion/#cast) suggests that Drill does not specify how a `CAST` from a string to `TIMESTAMP` should be treated. If I read your first example correctly, then it appears that we assume that the string is in local time. You said you set the current time to `2021-09-04 10:10:00`. Presumably this is the local time? You then ran the query and got an age of ~30 seconds, which suggests that the `CAST` string was treated as local time.
Then you say "I think the CAST function read the value as UTC directly." In fact, I think `CAST` takes the string as local time, converts it as such, and stores that. Because both the string and value vector are in local time, no time zone conversion should be needed.
An interesting (if confusing, and unrelated) question occurs if you use a string such as `2021-09-04 10:10:00Z`. Such a string is in UTC and should be converted to local time. But, since the docs don't say what we do, it is hard to know...
In the second case, the JSON string contains `'2019-09-30T20:47:43.123Z'`. This *is* a UTC timestamp. You didn't mention the local time for the second case. Let's assume it is the same as the first, "`2021-09-04 10:10:00`, the timezone is GMT+8".
Now comes the tricky part. If local time is GMT+8, then when we convert the JSON date to UTC, we have to add 8 hours, so we get `2019-09-31T04:47:43.123` local time. If we use `AGE()`, we should get a value of around 18 hours. But, you only got a few seconds.
Do we still have some time math issues? Was the current time for your second test different? (Or, did I just get confused in doing the time conversion?)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] paul-rogers commented on a change in pull request #2299: DRILL-7989: Use the UTC formatter in the JSON reader
Posted by GitBox <gi...@apache.org>.
paul-rogers commented on a change in pull request #2299:
URL: https://github.com/apache/drill/pull/2299#discussion_r693601366
##########
File path: exec/java-exec/src/main/java/org/apache/drill/exec/vector/complex/fn/VectorOutput.java
##########
@@ -248,8 +249,8 @@ public void writeTimestamp(boolean isNull) throws IOException {
ts.writeTimeStamp(dt.getMillis());
break;
case VALUE_STRING:
- OffsetDateTime originalDateTime = OffsetDateTime.parse(parser.getValueAsString(), DateUtility.isoFormatTimeStamp);
- OffsetDateTime utcDateTime = OffsetDateTime.of(originalDateTime.toLocalDateTime(), ZoneOffset.UTC); // strips the time zone from the original
+ LocalDateTime localDateTime = LocalDateTime.parse(parser.getValueAsString(), DateUtility.UTC_FORMATTER);
+ OffsetDateTime utcDateTime = OffsetDateTime.of(localDateTime, ZoneOffset.UTC); // strips the time zone from the original
Review comment:
This one is going to be tricky! The incoming date is in UTC. So, we have to use the offset for the local time zone to convert the UTC date/time to local time. We then write local time into the value vector. See [this file](https://github.com/apache/drill/blob/master/exec/java-exec/src/main/java/org/apache/drill/exec/store/easy/json/values/TimestampValueListener.java) for how the new JSON parser does this task.
##########
File path: exec/java-exec/src/test/java/org/apache/drill/TestFrameworkTest.java
##########
@@ -219,7 +221,8 @@ public void testBaselineValsVerificationWithComplexAndNulls() throws Exception {
mapOf("pink", "purple")),
listOf(4l, 2l),
listOf(listOf(2l, 1l),
- listOf(4l, 6l)))
+ listOf(4l, 6l)),
+ LocalDateTime.parse("2019-09-30T20:47:43.123Z", DateUtility.UTC_FORMATTER))
Review comment:
This isn't right. The data file has this same timestamp in UTC. The date that should appear in Drill is that same time converted to the local time zone. See [this test](https://github.com/apache/drill/blob/master/exec/java-exec/src/test/java/org/apache/drill/exec/store/easy/json/loader/TestExtendedTypes.java#L180) for how the work is done for the new JSON parser.
##########
File path: exec/java-exec/src/main/java/org/apache/drill/exec/vector/complex/fn/VectorOutput.java
##########
@@ -248,8 +249,8 @@ public void writeTimestamp(boolean isNull) throws IOException {
ts.writeTimeStamp(dt.getMillis());
break;
case VALUE_STRING:
- OffsetDateTime originalDateTime = OffsetDateTime.parse(parser.getValueAsString(), DateUtility.isoFormatTimeStamp);
- OffsetDateTime utcDateTime = OffsetDateTime.of(originalDateTime.toLocalDateTime(), ZoneOffset.UTC); // strips the time zone from the original
+ LocalDateTime localDateTime = LocalDateTime.parse(parser.getValueAsString(), DateUtility.UTC_FORMATTER);
+ OffsetDateTime utcDateTime = OffsetDateTime.of(localDateTime, ZoneOffset.UTC); // strips the time zone from the original
Review comment:
Also, please add a comment to say why we're doing things this way. Perhaps reference the Mongo specs. Links are in the file mentioned in the previous comment.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] cgivre merged pull request #2299: DRILL-7989: Use the UTC formatter in the JSON reader
Posted by GitBox <gi...@apache.org>.
cgivre merged pull request #2299:
URL: https://github.com/apache/drill/pull/2299
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] paul-rogers commented on pull request #2299: DRILL-7989: Use the UTC formatter in the JSON reader
Posted by GitBox <gi...@apache.org>.
paul-rogers commented on pull request #2299:
URL: https://github.com/apache/drill/pull/2299#issuecomment-915627200
Hi @luocooong, thanks for the update. The test case cited above did not say the value of the input UTC timestamp. The output would be correct only if the value were `2019-0930T20:47:43.123Z`. This is the first value in `input2.json`, so it seems we're good.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org