Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL: Move internals from Joda to java.time #35649

Merged
merged 5 commits into from
Nov 17, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.xpack.sql.proto.ColumnInfo;
import org.elasticsearch.xpack.sql.proto.Mode;
import org.joda.time.ReadableDateTime;

import java.io.IOException;
import java.sql.JDBCType;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -167,9 +167,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
* Serializes the provided value in SQL-compatible way based on the client mode
*/
public static XContentBuilder value(XContentBuilder builder, Mode mode, Object value) throws IOException {
if (Mode.isDriver(mode) && value instanceof ReadableDateTime) {
// JDBC cannot parse dates in string format
builder.value(((ReadableDateTime) value).getMillis());
if (value instanceof ZonedDateTime) {
ZonedDateTime zdt = (ZonedDateTime) value;
if (Mode.isDriver(mode)) {
// JDBC cannot parse dates in string format and ODBC can have issues with it
// so instead, use the millis since epoch (in UTC)
builder.value(zdt.toInstant().toEpochMilli());
}
// otherwise use the ISO format
else {
builder.value(zdt.toLocalDateTime().toString() + zdt.getOffset().toString());
}
} else {
builder.value(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
*/
package org.elasticsearch.xpack.sql.execution.search.extractor;

import org.elasticsearch.Version;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.querydsl.container.GroupByRef.Property;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.elasticsearch.xpack.sql.util.DateUtils;

import java.io.IOException;
import java.time.ZoneId;
import java.util.Map;
import java.util.Objects;
import java.util.TimeZone;
Expand All @@ -29,6 +28,7 @@ public class CompositeKeyExtractor implements BucketExtractor {
private final String key;
private final Property property;
private final TimeZone timeZone;
private final ZoneId zoneId;

/**
* Constructs a new <code>CompositeKeyExtractor</code> instance.
Expand All @@ -38,40 +38,29 @@ public CompositeKeyExtractor(String key, Property property, TimeZone timeZone) {
this.key = key;
this.property = property;
this.timeZone = timeZone;
this.zoneId = timeZone != null ? timeZone.toZoneId() : null;
}

CompositeKeyExtractor(StreamInput in) throws IOException {
key = in.readString();
property = in.readEnum(Property.class);
if (in.getVersion().onOrAfter(Version.V_6_3_0)) {
if (in.readBoolean()) {
timeZone = TimeZone.getTimeZone(in.readString());
} else {
timeZone = null;
}
if (in.readBoolean()) {
timeZone = TimeZone.getTimeZone(in.readString());
} else {
DateTimeZone dtz = in.readOptionalTimeZone();
if (dtz == null) {
timeZone = null;
} else {
timeZone = dtz.toTimeZone();
}
timeZone = null;
}
this.zoneId = timeZone != null ? timeZone.toZoneId() : null;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(key);
out.writeEnum(property);
if (out.getVersion().onOrAfter(Version.V_6_3_0)) {
if (timeZone == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeString(timeZone.getID());
}
if (timeZone == null) {
out.writeBoolean(false);
} else {
out.writeOptionalTimeZone(timeZone == null ? null : DateTimeZone.forTimeZone(timeZone));
out.writeBoolean(true);
out.writeString(timeZone.getID());
}
}

Expand Down Expand Up @@ -110,7 +99,7 @@ public Object extract(Bucket bucket) {
if (object == null) {
return object;
} else if (object instanceof Long) {
object = new DateTime(((Long) object).longValue(), DateTimeZone.forTimeZone(timeZone));
object = DateUtils.of(((Long) object).longValue(), zoneId);
} else {
throw new SqlIllegalArgumentException("Invalid date key returned: {}", object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.util.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.ReadableDateTime;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -136,11 +135,16 @@ private Object unwrapMultiValue(Object values) {
if (values instanceof Map) {
throw new SqlIllegalArgumentException("Objects (returned by [{}]) are not supported", fieldName);
}
if (values instanceof String && dataType == DataType.DATE) {
return new DateTime(Long.parseLong(values.toString()), DateTimeZone.UTC);
if (dataType == DataType.DATE) {
if (values instanceof String) {
return DateUtils.of(Long.parseLong(values.toString()));
}
// returned by nested types...
if (values instanceof DateTime) {
return DateUtils.of((DateTime) values);
}
}
if (values instanceof Long || values instanceof Double || values instanceof String || values instanceof Boolean
|| values instanceof ReadableDateTime) {
if (values instanceof Long || values instanceof Double || values instanceof String || values instanceof Boolean) {
return values;
}
throw new SqlIllegalArgumentException("Type {} (returned by [{}]) is not supported", values.getClass().getSimpleName(), fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@
import org.elasticsearch.xpack.sql.expression.function.scalar.UnaryScalarFunction;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.joda.time.DateTime;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Objects;
import java.util.TimeZone;

abstract class BaseDateTimeFunction extends UnaryScalarFunction {

private final TimeZone timeZone;
private final ZoneId zoneId;
private final String name;

BaseDateTimeFunction(Location location, Expression field, TimeZone timeZone) {
super(location, field);
this.timeZone = timeZone;
this.zoneId = timeZone != null ? timeZone.toZoneId() : null;

StringBuilder sb = new StringBuilder(super.name());
// add timezone as last argument
Expand Down Expand Up @@ -61,15 +64,15 @@ public boolean foldable() {

@Override
public Object fold() {
DateTime folded = (DateTime) field().fold();
ZonedDateTime folded = (ZonedDateTime) field().fold();
if (folded == null) {
return null;
}

return doFold(folded.getMillis(), timeZone().getID());
return doFold(folded.withZoneSameInstant(zoneId));
}

protected abstract Object doFold(long millis, String tzId);
protected abstract Object doFold(ZonedDateTime dateTime);


@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,25 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;
import org.joda.time.ReadableInstant;

import java.io.IOException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.TimeZone;

public abstract class BaseDateTimeProcessor implements Processor {

private final TimeZone timeZone;
private final ZoneId zoneId;

BaseDateTimeProcessor(TimeZone timeZone) {
this.timeZone = timeZone;
this.zoneId = timeZone.toZoneId();
}

BaseDateTimeProcessor(StreamInput in) throws IOException {
timeZone = TimeZone.getTimeZone(in.readString());
zoneId = timeZone.toZoneId();
}

@Override
Expand All @@ -37,23 +41,17 @@ TimeZone timeZone() {
}

@Override
public Object process(Object l) {
if (l == null) {
public Object process(Object input) {
if (input == null) {
return null;
}
long millis;
if (l instanceof String) {
// 6.4+
millis = Long.parseLong(l.toString());
} else if (l instanceof ReadableInstant) {
// 6.3-
millis = ((ReadableInstant) l).getMillis();
} else {
throw new SqlIllegalArgumentException("A string or a date is required; received {}", l);

if (!(input instanceof ZonedDateTime)) {
throw new SqlIllegalArgumentException("A date is required; received {}", input);
}
return doProcess(millis);

return doProcess(((ZonedDateTime) input).withZoneSameInstant(zoneId));
}

abstract Object doProcess(long millis);
}
abstract Object doProcess(ZonedDateTime dateTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.type.DataType;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
Expand All @@ -24,23 +23,25 @@

public abstract class DateTimeFunction extends BaseDateTimeFunction {

DateTimeFunction(Location location, Expression field, TimeZone timeZone) {
private final DateTimeExtractor extractor;

DateTimeFunction(Location location, Expression field, TimeZone timeZone, DateTimeExtractor extractor) {
super(location, field, timeZone);
this.extractor = extractor;
}

@Override
protected Object doFold(long millis, String tzId) {
return dateTimeChrono(millis, tzId, chronoField().name());
protected Object doFold(ZonedDateTime dateTime) {
return dateTimeChrono(dateTime, extractor.chronoField());
}

public static Integer dateTimeChrono(long millis, String tzId, String chronoName) {
ZonedDateTime time = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.of(tzId));
return Integer.valueOf(time.get(ChronoField.valueOf(chronoName)));
public static Integer dateTimeChrono(ZonedDateTime dateTime, String tzId, String chronoName) {
ZonedDateTime zdt = dateTime.withZoneSameInstant(ZoneId.of(tzId));
return dateTimeChrono(zdt, ChronoField.valueOf(chronoName));
}

public static Integer dateTimeChrono(ZonedDateTime millis, String tzId, String chronoName) {
ZonedDateTime time = millis.withZoneSameInstant(ZoneId.of(tzId));
return Integer.valueOf(time.get(ChronoField.valueOf(chronoName)));
private static Integer dateTimeChrono(ZonedDateTime dateTime, ChronoField field) {
return Integer.valueOf(dateTime.get(field));
}

@Override
Expand All @@ -51,21 +52,14 @@ public ScriptTemplate scriptWithField(FieldAttribute field) {
template = formatTemplate("{sql}.dateTimeChrono(doc[{}].value, {}, {})");
params.variable(field.name())
.variable(timeZone().getID())
.variable(chronoField().name());
.variable(extractor.chronoField().name());

return new ScriptTemplate(template, params.build(), dataType());
}

/**
* Used for generating the painless script version of this function when the time zone is not UTC
*/
protected abstract ChronoField chronoField();

protected abstract DateTimeExtractor extractor();

@Override
protected Processor makeProcessor() {
return new DateTimeProcessor(extractor(), timeZone());
return new DateTimeProcessor(extractor, timeZone());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;

import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeProcessor.DateTimeExtractor;
import org.elasticsearch.xpack.sql.tree.Location;

import java.util.TimeZone;
Expand All @@ -16,8 +17,8 @@
*/
public abstract class DateTimeHistogramFunction extends DateTimeFunction {

DateTimeHistogramFunction(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
DateTimeHistogramFunction(Location location, Expression field, TimeZone timeZone, DateTimeExtractor extractor) {
super(location, field, timeZone, extractor);
}

/**
Expand Down
Loading