Skip to content

Commit

Permalink
DRILL-8195: Add Timestamp Zone offset ISO-8601 format for JSON EVF
Browse files Browse the repository at this point in the history
  • Loading branch information
vdiravka committed Apr 26, 2022
1 parent 1092f24 commit 8fd294a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.drill.common.util;

import java.time.format.DateTimeFormatterBuilder;


/**
* Extends regular {@link java.time.Instant#parse} with more formats.
* By default, {@link java.time.format.DateTimeFormatter#ISO_INSTANT} used.
*/
public class DrillDateTimeFormatter {
public static java.time.format.DateTimeFormatter ISO_DATETIME_FORMATTER =
new DateTimeFormatterBuilder().append(java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart().appendOffset("+HH:MM", "+00:00").optionalEnd()
.optionalStart().appendOffset("+HHMM", "+0000").optionalEnd()
.optionalStart().appendOffset("+HH", "Z").optionalEnd()
.toFormatter();
}
Expand Up @@ -26,6 +26,8 @@

import com.fasterxml.jackson.core.JsonToken;

import static org.apache.drill.common.util.DrillDateTimeFormatter.ISO_DATETIME_FORMATTER;

/**
* Per the <a href="https://docs.mongodb.com/manual/reference/mongodb-extended-json-v1/#bson.data_date">
* V1 docs</a>:
Expand Down Expand Up @@ -59,14 +61,14 @@ public void onValue(JsonToken token, TokenIterator tokenizer) {
break;
case VALUE_STRING:
try {
instant = Instant.parse(tokenizer.stringValue());
instant = ISO_DATETIME_FORMATTER.parse(tokenizer.stringValue(), Instant::from);
} catch (Exception e) {
throw loader.dataConversionError(schema(), "date", tokenizer.stringValue());
}
break;
default:
throw tokenizer.invalidValue(token);
}
writer.setLong(instant.toEpochMilli() + LOCAL_ZONE_ID.getRules().getOffset(instant).getTotalSeconds() * 1000);
writer.setLong(instant.toEpochMilli() + LOCAL_ZONE_ID.getRules().getOffset(instant).getTotalSeconds() * 1000L);
}
}
Expand Up @@ -25,8 +25,6 @@
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;

import org.apache.drill.common.exceptions.UserException;
import org.apache.drill.exec.expr.fn.impl.DateUtility;
Expand Down Expand Up @@ -58,6 +56,8 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

import static org.apache.drill.common.util.DrillDateTimeFormatter.ISO_DATETIME_FORMATTER;

abstract class VectorOutput {

private static final Logger logger = LoggerFactory.getLogger(VectorOutput.class);
Expand All @@ -74,12 +74,6 @@ abstract class VectorOutput {
protected final WorkingBuffer work;
protected JsonParser parser;

protected DateTimeFormatter isoDateTimeFormatter = new DateTimeFormatterBuilder().append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.optionalStart().appendOffset("+HH:MM", "+00:00").optionalEnd()
.optionalStart().appendOffset("+HHMM", "+0000").optionalEnd()
.optionalStart().appendOffset("+HH", "Z").optionalEnd()
.toFormatter();

public VectorOutput(WorkingBuffer work) {
this.work = work;
}
Expand Down Expand Up @@ -260,7 +254,7 @@ public void writeTimestamp(boolean isNull) throws IOException {
// See the mongo specs and the Drill handler (in new JSON loader) :
// 1. https://docs.mongodb.com/manual/reference/mongodb-extended-json
// 2. org.apache.drill.exec.store.easy.json.values.UtcTimestampValueListener
Instant instant = isoDateTimeFormatter.parse(parser.getValueAsString(), Instant::from);
Instant instant = ISO_DATETIME_FORMATTER.parse(parser.getValueAsString(), Instant::from);
long offset = ZoneId.systemDefault().getRules().getOffset(instant).getTotalSeconds() * 1000L;
ts.writeTimeStamp(instant.toEpochMilli() + offset);
break;
Expand Down Expand Up @@ -366,7 +360,7 @@ public void writeTimestamp(boolean isNull) throws IOException {
// See the mongo specs and the Drill handler (in new JSON loader) :
// 1. https://docs.mongodb.com/manual/reference/mongodb-extended-json
// 2. org.apache.drill.exec.store.easy.json.values.UtcTimestampValueListener
Instant instant = isoDateTimeFormatter.parse(parser.getValueAsString(), Instant::from);
Instant instant = ISO_DATETIME_FORMATTER.parse(parser.getValueAsString(), Instant::from);
long offset = ZoneId.systemDefault().getRules().getOffset(instant).getTotalSeconds() * 1000L;
ts.writeTimeStamp(instant.toEpochMilli() + offset);
break;
Expand Down

0 comments on commit 8fd294a

Please sign in to comment.