SQL: restore support for timezones in the DateTimeProcessor (elastic/x-pack-elasticsearch#2450)
The timezone handling seems to be lost in a merge conflict in elastic/x-pack-elasticsearch@20f41e2 Original commit: elastic/x-pack-elasticsearch@92c0ca3572
This commit is contained in:
parent
664c087c3f
commit
6ef174aa19
|
@ -18,18 +18,22 @@ public class DateTimeProcessor implements ColumnProcessor {
|
|||
public static final String NAME = "d";
|
||||
|
||||
private final DateTimeExtractor extractor;
|
||||
private final DateTimeZone timeZone;
|
||||
|
||||
public DateTimeProcessor(DateTimeExtractor extractor) {
|
||||
public DateTimeProcessor(DateTimeExtractor extractor, DateTimeZone timeZone) {
|
||||
this.extractor = extractor;
|
||||
this.timeZone = timeZone;
|
||||
}
|
||||
|
||||
DateTimeProcessor(StreamInput in) throws IOException {
|
||||
extractor = in.readEnum(DateTimeExtractor.class);
|
||||
timeZone = DateTimeZone.forID(in.readString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(StreamOutput out) throws IOException {
|
||||
out.writeEnum(extractor);
|
||||
out.writeString(timeZone.getID());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -43,14 +47,17 @@ public class DateTimeProcessor implements ColumnProcessor {
|
|||
|
||||
@Override
|
||||
public Object apply(Object l) {
|
||||
ReadableDateTime dt = null;
|
||||
ReadableDateTime dt;
|
||||
// most dates are returned as long
|
||||
if (l instanceof Long) {
|
||||
dt = new DateTime((Long) l, DateTimeZone.UTC);
|
||||
dt = new DateTime(((Long) l).longValue(), DateTimeZone.UTC);
|
||||
}
|
||||
else {
|
||||
dt = (ReadableDateTime) l;
|
||||
}
|
||||
if (!timeZone.getID().equals("UTC")) {
|
||||
dt = dt.toDateTime().withZone(timeZone);
|
||||
}
|
||||
return extractor.extract(dt);
|
||||
}
|
||||
|
||||
|
|
|
@ -86,7 +86,7 @@ public abstract class DateTimeFunction extends ScalarFunction implements TimeZon
|
|||
|
||||
@Override
|
||||
public final ColumnProcessor asProcessor() {
|
||||
return new DateTimeProcessor(extractor());
|
||||
return new DateTimeProcessor(extractor(), timeZone);
|
||||
}
|
||||
|
||||
protected abstract DateTimeExtractor extractor();
|
||||
|
|
|
@ -15,7 +15,7 @@ import java.io.IOException;
|
|||
|
||||
public class DateTimeProcessorTests extends AbstractWireSerializingTestCase<DateTimeProcessor> {
|
||||
public static DateTimeProcessor randomDateTimeProcessor() {
|
||||
return new DateTimeProcessor(randomFrom(DateTimeExtractor.values()));
|
||||
return new DateTimeProcessor(randomFrom(DateTimeExtractor.values()), DateTimeZone.UTC);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -30,16 +30,17 @@ public class DateTimeProcessorTests extends AbstractWireSerializingTestCase<Date
|
|||
|
||||
@Override
|
||||
protected DateTimeProcessor mutateInstance(DateTimeProcessor instance) throws IOException {
|
||||
return new DateTimeProcessor(randomValueOtherThan(instance.extractor(), () -> randomFrom(DateTimeExtractor.values())));
|
||||
return new DateTimeProcessor(randomValueOtherThan(instance.extractor(), () -> randomFrom(DateTimeExtractor.values())),
|
||||
DateTimeZone.UTC);
|
||||
}
|
||||
|
||||
public void testApply() {
|
||||
DateTimeProcessor proc = new DateTimeProcessor(DateTimeExtractor.YEAR);
|
||||
DateTimeProcessor proc = new DateTimeProcessor(DateTimeExtractor.YEAR, DateTimeZone.UTC);
|
||||
assertEquals(1970, proc.apply(0L));
|
||||
assertEquals(1970, proc.apply(new DateTime(0L, DateTimeZone.UTC)));
|
||||
assertEquals(2017, proc.apply(new DateTime(2017, 01, 02, 10, 10, DateTimeZone.UTC)));
|
||||
|
||||
proc = new DateTimeProcessor(DateTimeExtractor.DAY_OF_MONTH);
|
||||
proc = new DateTimeProcessor(DateTimeExtractor.DAY_OF_MONTH, DateTimeZone.UTC);
|
||||
assertEquals(1, proc.apply(0L));
|
||||
assertEquals(1, proc.apply(new DateTime(0L, DateTimeZone.UTC)));
|
||||
assertEquals(2, proc.apply(new DateTime(2017, 01, 02, 10, 10, DateTimeZone.UTC)));
|
||||
|
|
|
@ -16,9 +16,8 @@ public class DayOfYearTests extends ESTestCase {
|
|||
|
||||
public void testAsColumnProcessor() {
|
||||
assertEquals(1, extract(dateTime(0), UTC));
|
||||
// NOCOMMIT: these time zones are not getting resolved on mac and extract seems to ignore the timezone anyway
|
||||
// assertEquals(1, extract(dateTime(0), DateTimeZone.forID("GMT+1")));
|
||||
// assertEquals(365, extract(dateTime(0), DateTimeZone.forID("GMT-1")));
|
||||
assertEquals(1, extract(dateTime(0), DateTimeZone.forID("+01:00")));
|
||||
assertEquals(365, extract(dateTime(0), DateTimeZone.forID("-01:00")));
|
||||
}
|
||||
|
||||
private DateTime dateTime(long millisSinceEpoch) {
|
||||
|
|
Loading…
Reference in New Issue