Move sql-proto and jdbc to java.util.TimeZone (elastic/x-pack-elasticsearch#4350)

* Move sql-proto and jdbc to java.util.TimeZone

This moves sql-proto and jdbc from Joda's `DateTimeZone` to
`java.util.TimeZone`, this will allow us in the future to be able to decouple
JDBC from Joda.

This does not decouple all of SQL from joda, it focuses on as small a piece as I
could for sql-proto and jdbc.

Requires https://github.com/elastic/elasticsearch/pull/29487 to be merged first.

Original commit: elastic/x-pack-elasticsearch@7c9d52e675
This commit is contained in:
Lee Hinman 2018-04-19 11:27:33 -06:00 committed by GitHub
parent 2574d4e704
commit 0bd9163c28
44 changed files with 216 additions and 172 deletions

View File

@ -12,14 +12,15 @@ import org.elasticsearch.xpack.sql.client.HttpClient;
import org.elasticsearch.xpack.sql.jdbc.jdbc.JdbcConfiguration;
import org.elasticsearch.xpack.sql.jdbc.net.protocol.ColumnInfo;
import org.elasticsearch.xpack.sql.jdbc.net.protocol.InfoResponse;
import org.elasticsearch.xpack.sql.plugin.AbstractSqlQueryRequest;
import org.elasticsearch.xpack.sql.plugin.AbstractSqlRequest;
import org.elasticsearch.xpack.sql.plugin.SqlQueryRequest;
import org.elasticsearch.xpack.sql.plugin.SqlQueryResponse;
import org.elasticsearch.xpack.sql.plugin.SqlTypedParamValue;
import org.joda.time.DateTimeZone;
import java.sql.SQLException;
import java.util.List;
import java.util.TimeZone;
import java.util.stream.Collectors;
import static org.elasticsearch.xpack.sql.client.shared.StringUtils.EMPTY;
@ -44,8 +45,9 @@ public class JdbcHttpClient {
public Cursor query(String sql, List<SqlTypedParamValue> params, RequestMeta meta) throws SQLException {
int fetch = meta.fetchSize() > 0 ? meta.fetchSize() : conCfg.pageSize();
SqlQueryRequest sqlRequest = new SqlQueryRequest(AbstractSqlRequest.Mode.JDBC, sql, params, null, DateTimeZone.UTC, fetch,
TimeValue.timeValueMillis(meta.timeoutInMs()), TimeValue.timeValueMillis(meta.queryTimeoutInMs()), "");
SqlQueryRequest sqlRequest = new SqlQueryRequest(AbstractSqlRequest.Mode.JDBC, sql, params, null,
AbstractSqlQueryRequest.DEFAULT_TIME_ZONE,
fetch, TimeValue.timeValueMillis(meta.timeoutInMs()), TimeValue.timeValueMillis(meta.queryTimeoutInMs()), "");
SqlQueryResponse response = httpClient.query(sqlRequest);
return new DefaultCursor(this, response.cursor(), toJdbcColumnInfo(response.columns()), response.rows(), meta);
}
@ -87,4 +89,4 @@ public class JdbcHttpClient {
new ColumnInfo(columnInfo.name(), columnInfo.jdbcType(), EMPTY, EMPTY, EMPTY, EMPTY, columnInfo.displaySize())
).collect(Collectors.toList());
}
}
}

View File

@ -11,28 +11,24 @@ import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.AbstractObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParserUtils;
import org.elasticsearch.index.query.AbstractQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.joda.time.DateTimeZone;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.TimeZone;
import java.util.function.Supplier;
/**
* Base class for requests that contain sql queries (Query and Translate)
*/
public abstract class AbstractSqlQueryRequest extends AbstractSqlRequest implements CompositeIndicesRequest, ToXContentFragment {
public static final DateTimeZone DEFAULT_TIME_ZONE = DateTimeZone.UTC;
public static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone("UTC");
/**
* Global choice for the default fetch size.
@ -42,7 +38,7 @@ public abstract class AbstractSqlQueryRequest extends AbstractSqlRequest impleme
public static final TimeValue DEFAULT_PAGE_TIMEOUT = TimeValue.timeValueSeconds(45);
private String query = "";
private DateTimeZone timeZone = DEFAULT_TIME_ZONE;
private TimeZone timeZone = DEFAULT_TIME_ZONE;
private int fetchSize = DEFAULT_FETCH_SIZE;
private TimeValue requestTimeout = DEFAULT_REQUEST_TIMEOUT;
private TimeValue pageTimeout = DEFAULT_PAGE_TIMEOUT;
@ -54,7 +50,7 @@ public abstract class AbstractSqlQueryRequest extends AbstractSqlRequest impleme
super();
}
public AbstractSqlQueryRequest(Mode mode, String query, List<SqlTypedParamValue> params, QueryBuilder filter, DateTimeZone timeZone,
public AbstractSqlQueryRequest(Mode mode, String query, List<SqlTypedParamValue> params, QueryBuilder filter, TimeZone timeZone,
int fetchSize, TimeValue requestTimeout, TimeValue pageTimeout) {
super(mode);
this.query = query;
@ -71,7 +67,7 @@ public abstract class AbstractSqlQueryRequest extends AbstractSqlRequest impleme
ObjectParser<R, Void> parser = new ObjectParser<>("sql/query", true, supplier);
parser.declareString(AbstractSqlQueryRequest::query, new ParseField("query"));
parser.declareObjectArray(AbstractSqlQueryRequest::params, (p, c) -> SqlTypedParamValue.fromXContent(p), new ParseField("params"));
parser.declareString((request, zoneId) -> request.timeZone(DateTimeZone.forID(zoneId)), new ParseField("time_zone"));
parser.declareString((request, zoneId) -> request.timeZone(TimeZone.getTimeZone(zoneId)), new ParseField("time_zone"));
parser.declareInt(AbstractSqlQueryRequest::fetchSize, new ParseField("fetch_size"));
parser.declareString(
(request, timeout) -> request.requestTimeout(TimeValue.parseTimeValue(timeout, DEFAULT_REQUEST_TIMEOUT, "request_timeout")),
@ -117,11 +113,11 @@ public abstract class AbstractSqlQueryRequest extends AbstractSqlRequest impleme
/**
* The client's time zone
*/
public DateTimeZone timeZone() {
public TimeZone timeZone() {
return timeZone;
}
public AbstractSqlQueryRequest timeZone(DateTimeZone timeZone) {
public AbstractSqlQueryRequest timeZone(TimeZone timeZone) {
if (query == null) {
throw new IllegalArgumentException("time zone may not be null.");
}
@ -190,7 +186,7 @@ public abstract class AbstractSqlQueryRequest extends AbstractSqlRequest impleme
super(in);
query = in.readString();
params = in.readList(SqlTypedParamValue::new);
timeZone = DateTimeZone.forID(in.readString());
timeZone = TimeZone.getTimeZone(in.readString());
fetchSize = in.readVInt();
requestTimeout = in.readTimeValue();
pageTimeout = in.readTimeValue();

View File

@ -17,11 +17,11 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.AbstractQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.joda.time.DateTimeZone;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.TimeZone;
import static org.elasticsearch.action.ValidateActions.addValidationError;
@ -45,7 +45,7 @@ public class SqlQueryRequest extends AbstractSqlQueryRequest implements ToXConte
public SqlQueryRequest() {
}
public SqlQueryRequest(Mode mode, String query, List<SqlTypedParamValue> params, QueryBuilder filter, DateTimeZone timeZone,
public SqlQueryRequest(Mode mode, String query, List<SqlTypedParamValue> params, QueryBuilder filter, TimeZone timeZone,
int fetchSize, TimeValue requestTimeout, TimeValue pageTimeout, String cursor) {
super(mode, query, params, filter, timeZone, fetchSize, requestTimeout, pageTimeout);
this.cursor = cursor;

View File

@ -10,10 +10,10 @@ import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.xpack.sql.plugin.AbstractSqlRequest.Mode;
import org.joda.time.DateTimeZone;
import java.util.Collections;
import java.util.List;
import java.util.TimeZone;
import static org.elasticsearch.xpack.sql.plugin.AbstractSqlQueryRequest.DEFAULT_FETCH_SIZE;
import static org.elasticsearch.xpack.sql.plugin.AbstractSqlQueryRequest.DEFAULT_PAGE_TIMEOUT;
@ -31,7 +31,7 @@ public class SqlQueryRequestBuilder extends ActionRequestBuilder<SqlQueryRequest
}
public SqlQueryRequestBuilder(ElasticsearchClient client, SqlQueryAction action, String query, List<SqlTypedParamValue> params,
QueryBuilder filter, DateTimeZone timeZone, int fetchSize, TimeValue requestTimeout,
QueryBuilder filter, TimeZone timeZone, int fetchSize, TimeValue requestTimeout,
TimeValue pageTimeout, String nextPageInfo, Mode mode) {
super(client, action, new SqlQueryRequest(mode, query, params, filter, timeZone, fetchSize, requestTimeout, pageTimeout,
nextPageInfo));
@ -62,7 +62,7 @@ public class SqlQueryRequestBuilder extends ActionRequestBuilder<SqlQueryRequest
return this;
}
public SqlQueryRequestBuilder timeZone(DateTimeZone timeZone) {
public SqlQueryRequestBuilder timeZone(TimeZone timeZone) {
request.timeZone(timeZone);
return this;
}

View File

@ -12,10 +12,10 @@ import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryBuilder;
import org.joda.time.DateTimeZone;
import java.io.IOException;
import java.util.List;
import java.util.TimeZone;
import static org.elasticsearch.action.ValidateActions.addValidationError;
@ -28,7 +28,7 @@ public class SqlTranslateRequest extends AbstractSqlQueryRequest {
public SqlTranslateRequest() {
}
public SqlTranslateRequest(Mode mode, String query, List<SqlTypedParamValue> params, QueryBuilder filter, DateTimeZone timeZone,
public SqlTranslateRequest(Mode mode, String query, List<SqlTypedParamValue> params, QueryBuilder filter, TimeZone timeZone,
int fetchSize, TimeValue requestTimeout, TimeValue pageTimeout) {
super(mode, query, params, filter, timeZone, fetchSize, requestTimeout, pageTimeout);
}

View File

@ -9,10 +9,10 @@ import org.elasticsearch.action.ActionRequestBuilder;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.joda.time.DateTimeZone;
import java.util.Collections;
import java.util.List;
import java.util.TimeZone;
import static org.elasticsearch.xpack.sql.plugin.AbstractSqlQueryRequest.DEFAULT_FETCH_SIZE;
import static org.elasticsearch.xpack.sql.plugin.AbstractSqlQueryRequest.DEFAULT_PAGE_TIMEOUT;
@ -30,7 +30,7 @@ public class SqlTranslateRequestBuilder extends ActionRequestBuilder<SqlTranslat
}
public SqlTranslateRequestBuilder(ElasticsearchClient client, SqlTranslateAction action, AbstractSqlRequest.Mode mode, String query,
QueryBuilder filter, List<SqlTypedParamValue> params, DateTimeZone timeZone, int fetchSize,
QueryBuilder filter, List<SqlTypedParamValue> params, TimeZone timeZone, int fetchSize,
TimeValue requestTimeout, TimeValue pageTimeout) {
super(client, action, new SqlTranslateRequest(mode, query, params, filter, timeZone, fetchSize, requestTimeout, pageTimeout));
}
@ -40,7 +40,7 @@ public class SqlTranslateRequestBuilder extends ActionRequestBuilder<SqlTranslat
return this;
}
public SqlTranslateRequestBuilder timeZone(DateTimeZone timeZone) {
public SqlTranslateRequestBuilder timeZone(TimeZone timeZone) {
request.timeZone(timeZone);
return this;
}

View File

@ -50,7 +50,7 @@ public class SqlQueryRequestTests extends AbstractSerializingTestCase<SqlQueryRe
@Override
protected SqlQueryRequest createTestInstance() {
return new SqlQueryRequest(testMode, randomAlphaOfLength(10), randomParameters(),
SqlTestUtils.randomFilterOrNull(random()), randomDateTimeZone(),
SqlTestUtils.randomFilterOrNull(random()), randomTimeZone(),
between(1, Integer.MAX_VALUE), randomTV(), randomTV(), randomAlphaOfLength(10)
);
}
@ -96,7 +96,7 @@ public class SqlQueryRequestTests extends AbstractSerializingTestCase<SqlQueryRe
request -> request.mode(randomValueOtherThan(request.mode(), () -> randomFrom(AbstractSqlRequest.Mode.values()))),
request -> request.query(randomValueOtherThan(request.query(), () -> randomAlphaOfLength(5))),
request -> request.params(randomValueOtherThan(request.params(), this::randomParameters)),
request -> request.timeZone(randomValueOtherThan(request.timeZone(), ESTestCase::randomDateTimeZone)),
request -> request.timeZone(randomValueOtherThan(request.timeZone(), ESTestCase::randomTimeZone)),
request -> request.fetchSize(randomValueOtherThan(request.fetchSize(), () -> between(1, Integer.MAX_VALUE))),
request -> request.requestTimeout(randomValueOtherThan(request.requestTimeout(), this::randomTV)),
request -> request.filter(randomValueOtherThan(request.filter(),

View File

@ -35,7 +35,7 @@ public class SqlTranslateRequestTests extends AbstractSerializingTestCase<SqlTra
@Override
protected SqlTranslateRequest createTestInstance() {
return new SqlTranslateRequest(testMode, randomAlphaOfLength(10), Collections.emptyList(), randomFilterOrNull(random()),
randomDateTimeZone(), between(1, Integer.MAX_VALUE), randomTV(), randomTV());
randomTimeZone(), between(1, Integer.MAX_VALUE), randomTV(), randomTV());
}
@Override
@ -69,7 +69,7 @@ public class SqlTranslateRequestTests extends AbstractSerializingTestCase<SqlTra
@SuppressWarnings("unchecked")
Consumer<SqlTranslateRequest> mutator = randomFrom(
request -> request.query(randomValueOtherThan(request.query(), () -> randomAlphaOfLength(5))),
request -> request.timeZone(randomValueOtherThan(request.timeZone(), ESTestCase::randomDateTimeZone)),
request -> request.timeZone(randomValueOtherThan(request.timeZone(), ESTestCase::randomTimeZone)),
request -> request.fetchSize(randomValueOtherThan(request.fetchSize(), () -> between(1, Integer.MAX_VALUE))),
request -> request.requestTimeout(randomValueOtherThan(request.requestTimeout(), () -> randomTV())),
request -> request.filter(randomValueOtherThan(request.filter(),

View File

@ -29,7 +29,6 @@ import org.elasticsearch.xpack.sql.plugin.SqlClearCursorResponse;
import org.elasticsearch.xpack.sql.plugin.SqlQueryAction;
import org.elasticsearch.xpack.sql.plugin.SqlQueryRequest;
import org.elasticsearch.xpack.sql.plugin.SqlQueryResponse;
import org.joda.time.DateTimeZone;
import java.io.IOException;
import java.io.InputStream;
@ -37,6 +36,7 @@ import java.security.AccessController;
import java.security.PrivilegedAction;
import java.sql.SQLException;
import java.util.Collections;
import java.util.TimeZone;
import java.util.function.Function;
/**
@ -67,7 +67,8 @@ public class HttpClient {
public SqlQueryResponse queryInit(String query, int fetchSize) throws SQLException {
// TODO allow customizing the time zone - this is what session set/reset/get should be about
SqlQueryRequest sqlRequest = new SqlQueryRequest(AbstractSqlRequest.Mode.PLAIN, query, Collections.emptyList(), null,
DateTimeZone.UTC, fetchSize, TimeValue.timeValueMillis(cfg.queryTimeout()), TimeValue.timeValueMillis(cfg.pageTimeout()), ""
TimeZone.getTimeZone("UTC"), fetchSize, TimeValue.timeValueMillis(cfg.queryTimeout()),
TimeValue.timeValueMillis(cfg.pageTimeout()), ""
);
return query(sqlRequest);
}

View File

@ -61,6 +61,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TimeZone;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
@ -89,9 +90,9 @@ public class Analyzer extends RuleExecutor<LogicalPlan> {
* Time zone in which we're executing this SQL. It is attached to functions
* that deal with date and time.
*/
private final DateTimeZone timeZone;
private final TimeZone timeZone;
public Analyzer(FunctionRegistry functionRegistry, IndexResolution results, DateTimeZone timeZone) {
public Analyzer(FunctionRegistry functionRegistry, IndexResolution results, TimeZone timeZone) {
this.functionRegistry = functionRegistry;
this.indexResolution = results;
this.timeZone = timeZone;

View File

@ -5,6 +5,7 @@
*/
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;
@ -16,6 +17,7 @@ import org.joda.time.DateTimeZone;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.TimeZone;
public class CompositeKeyExtractor implements BucketExtractor {
@ -26,13 +28,13 @@ public class CompositeKeyExtractor implements BucketExtractor {
private final String key;
private final Property property;
private final DateTimeZone timeZone;
private final TimeZone timeZone;
/**
* Constructs a new <code>CompositeKeyExtractor</code> instance.
* The time-zone parameter is used to indicate a date key.
*/
public CompositeKeyExtractor(String key, Property property, DateTimeZone timeZone) {
public CompositeKeyExtractor(String key, Property property, TimeZone timeZone) {
this.key = key;
this.property = property;
this.timeZone = timeZone;
@ -41,14 +43,36 @@ public class CompositeKeyExtractor implements BucketExtractor {
CompositeKeyExtractor(StreamInput in) throws IOException {
key = in.readString();
property = in.readEnum(Property.class);
timeZone = in.readOptionalTimeZone();
if (in.getVersion().onOrAfter(Version.V_6_3_0)) {
if (in.readBoolean()) {
timeZone = TimeZone.getTimeZone(in.readString());
} else {
timeZone = null;
}
} else {
DateTimeZone dtz = in.readOptionalTimeZone();
if (dtz == null) {
timeZone = null;
} else {
timeZone = dtz.toTimeZone();
}
}
}
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(key);
out.writeEnum(property);
out.writeOptionalTimeZone(timeZone);
if (out.getVersion().onOrAfter(Version.V_6_3_0)) {
if (timeZone == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeString(timeZone.getID());
}
} else {
out.writeOptionalTimeZone(timeZone == null ? null : DateTimeZone.forTimeZone(timeZone));
}
}
String key() {
@ -59,7 +83,7 @@ public class CompositeKeyExtractor implements BucketExtractor {
return property;
}
DateTimeZone timeZone() {
TimeZone timeZone() {
return timeZone;
}
@ -84,7 +108,7 @@ public class CompositeKeyExtractor implements BucketExtractor {
if (timeZone != null) {
if (object instanceof Long) {
object = new DateTime(((Long) object).longValue(), timeZone);
object = new DateTime(((Long) object).longValue(), DateTimeZone.forTimeZone(timeZone));
} else {
throw new SqlIllegalArgumentException("Invalid date key returned: {}", object);
}

View File

@ -7,7 +7,7 @@ package org.elasticsearch.xpack.sql.expression.function;
import java.util.List;
import java.util.Locale;
import org.joda.time.DateTimeZone;
import java.util.TimeZone;
import static java.lang.String.format;
@ -17,7 +17,7 @@ public class FunctionDefinition {
*/
@FunctionalInterface
public interface Builder {
Function build(UnresolvedFunction uf, boolean distinct, DateTimeZone tz);
Function build(UnresolvedFunction uf, boolean distinct, TimeZone tz);
}
private final String name;
private final List<String> aliases;

View File

@ -63,6 +63,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.function.BiFunction;
import java.util.regex.Pattern;
@ -261,7 +262,7 @@ public class FunctionRegistry {
return def(function, builder, true, aliases);
}
interface DatetimeUnaryFunctionBuilder<T> {
T build(Location location, Expression target, DateTimeZone tz);
T build(Location location, Expression target, TimeZone tz);
}
/**
@ -300,7 +301,7 @@ public class FunctionRegistry {
return new FunctionDefinition(primaryName, unmodifiableList(Arrays.asList(aliases)), function, datetime, realBuilder);
}
private interface FunctionBuilder {
Function build(Location location, List<Expression> children, boolean distinct, DateTimeZone tz);
Function build(Location location, List<Expression> children, boolean distinct, TimeZone tz);
}
private static String normalize(String name) {

View File

@ -13,18 +13,17 @@ import org.elasticsearch.xpack.sql.expression.Literal;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.util.CollectionUtils;
import org.elasticsearch.xpack.sql.util.StringUtils;
import org.joda.time.DateTimeZone;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.util.TimeZone;
import static java.util.Collections.singletonList;
import java.util.LinkedHashSet;
public class UnresolvedFunction extends Function implements Unresolvable {
private final String name;
private final String unresolvedMsg;
@ -80,7 +79,7 @@ public class UnresolvedFunction extends Function implements Unresolvable {
/**
* Build a function to replace this one after resolving the function.
*/
public Function buildResolved(DateTimeZone timeZone, FunctionDefinition def) {
public Function buildResolved(TimeZone timeZone, FunctionDefinition def) {
return resolutionType.buildResolved(this, timeZone, def);
}
@ -192,7 +191,7 @@ public class UnresolvedFunction extends Function implements Unresolvable {
return uf;
}
@Override
public Function buildResolved(UnresolvedFunction uf, DateTimeZone tz, FunctionDefinition def) {
public Function buildResolved(UnresolvedFunction uf, TimeZone tz, FunctionDefinition def) {
return def.builder().build(uf, false, tz);
}
@Override
@ -213,7 +212,7 @@ public class UnresolvedFunction extends Function implements Unresolvable {
return uf.withMessage("* is not valid with DISTINCT");
}
@Override
public Function buildResolved(UnresolvedFunction uf, DateTimeZone tz, FunctionDefinition def) {
public Function buildResolved(UnresolvedFunction uf, TimeZone tz, FunctionDefinition def) {
return def.builder().build(uf, true, tz);
}
@Override
@ -234,7 +233,7 @@ public class UnresolvedFunction extends Function implements Unresolvable {
return uf.withMessage("Can't extract from *");
}
@Override
public Function buildResolved(UnresolvedFunction uf, DateTimeZone tz, FunctionDefinition def) {
public Function buildResolved(UnresolvedFunction uf, TimeZone tz, FunctionDefinition def) {
if (def.datetime()) {
return def.builder().build(uf, false, tz);
}
@ -261,7 +260,7 @@ public class UnresolvedFunction extends Function implements Unresolvable {
/**
* Build the real function from this one and resolution metadata.
*/
protected abstract Function buildResolved(UnresolvedFunction uf, DateTimeZone tz, FunctionDefinition def);
protected abstract Function buildResolved(UnresolvedFunction uf, TimeZone tz, FunctionDefinition def);
/**
* Is {@code def} a valid alternative for function invocations
* of this kind. Used to filter the list of "did you mean"

View File

@ -27,16 +27,17 @@ import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.util.Objects;
import java.util.TimeZone;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ParamsBuilder.paramsBuilder;
import static org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTemplate.formatTemplate;
public abstract class DateTimeFunction extends UnaryScalarFunction {
private final DateTimeZone timeZone;
private final TimeZone timeZone;
private final String name;
DateTimeFunction(Location location, Expression field, DateTimeZone timeZone) {
DateTimeFunction(Location location, Expression field, TimeZone timeZone) {
super(location, field);
this.timeZone = timeZone;
@ -51,9 +52,9 @@ public abstract class DateTimeFunction extends UnaryScalarFunction {
protected final NodeInfo<DateTimeFunction> info() {
return NodeInfo.create(this, ctorForInfo(), field(), timeZone());
}
protected abstract NodeInfo.NodeCtor2<Expression, DateTimeZone, DateTimeFunction> ctorForInfo();
protected abstract NodeInfo.NodeCtor2<Expression, TimeZone, DateTimeFunction> ctorForInfo();
public DateTimeZone timeZone() {
public TimeZone timeZone() {
return timeZone;
}
@ -88,7 +89,7 @@ public abstract class DateTimeFunction extends UnaryScalarFunction {
ParamsBuilder params = paramsBuilder();
String template = null;
if (DateTimeZone.UTC.equals(timeZone)) {
if (TimeZone.getTimeZone("UTC").equals(timeZone)) {
// TODO: it would be nice to be able to externalize the extract function and reuse the script across all extractors
template = formatTemplate("doc[{}].value.get" + extractFunction() + "()");
params.variable(field.name());

View File

@ -7,7 +7,8 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.tree.Location;
import org.joda.time.DateTimeZone;
import java.util.TimeZone;
/**
* DateTimeFunctions that can be mapped as histogram. This means the dates order is maintained
@ -15,7 +16,7 @@ import org.joda.time.DateTimeZone;
*/
public abstract class DateTimeHistogramFunction extends DateTimeFunction {
DateTimeHistogramFunction(Location location, Expression field, DateTimeZone timeZone) {
DateTimeHistogramFunction(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
}

View File

@ -15,6 +15,7 @@ import org.joda.time.ReadableDateTime;
import java.io.IOException;
import java.util.Objects;
import java.util.TimeZone;
public class DateTimeProcessor implements Processor {
@ -44,16 +45,16 @@ public class DateTimeProcessor implements Processor {
public static final String NAME = "dt";
private final DateTimeExtractor extractor;
private final DateTimeZone timeZone;
private final TimeZone timeZone;
public DateTimeProcessor(DateTimeExtractor extractor, DateTimeZone timeZone) {
public DateTimeProcessor(DateTimeExtractor extractor, TimeZone timeZone) {
this.extractor = extractor;
this.timeZone = timeZone;
}
public DateTimeProcessor(StreamInput in) throws IOException {
extractor = in.readEnum(DateTimeExtractor.class);
timeZone = DateTimeZone.forID(in.readString());
timeZone = TimeZone.getTimeZone(in.readString());
}
@Override
@ -83,8 +84,8 @@ public class DateTimeProcessor implements Processor {
ReadableDateTime dt = (ReadableDateTime) l;
if (!DateTimeZone.UTC.equals(timeZone)) {
dt = dt.toDateTime().withZone(timeZone);
if (!TimeZone.getTimeZone("UTC").equals(timeZone)) {
dt = dt.toDateTime().withZone(DateTimeZone.forTimeZone(timeZone));
}
return extractor.extract(dt);
}

View File

@ -9,20 +9,20 @@ 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 org.elasticsearch.xpack.sql.tree.NodeInfo.NodeCtor2;
import org.joda.time.DateTimeZone;
import java.time.temporal.ChronoField;
import java.util.TimeZone;
/**
* Extract the day of the month from a datetime.
*/
public class DayOfMonth extends DateTimeFunction {
public DayOfMonth(Location location, Expression field, DateTimeZone timeZone) {
public DayOfMonth(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
}
@Override
protected NodeCtor2<Expression, DateTimeZone, DateTimeFunction> ctorForInfo() {
protected NodeCtor2<Expression, TimeZone, DateTimeFunction> ctorForInfo() {
return DayOfMonth::new;
}

View File

@ -9,20 +9,20 @@ 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 org.elasticsearch.xpack.sql.tree.NodeInfo.NodeCtor2;
import org.joda.time.DateTimeZone;
import java.time.temporal.ChronoField;
import java.util.TimeZone;
/**
* Extract the day of the week from a datetime. 1 is Monday, 2 is Tuesday, etc.
*/
public class DayOfWeek extends DateTimeFunction {
public DayOfWeek(Location location, Expression field, DateTimeZone timeZone) {
public DayOfWeek(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
}
@Override
protected NodeCtor2<Expression, DateTimeZone, DateTimeFunction> ctorForInfo() {
protected NodeCtor2<Expression, TimeZone, DateTimeFunction> ctorForInfo() {
return DayOfWeek::new;
}

View File

@ -10,20 +10,20 @@ import org.elasticsearch.xpack.sql.expression.function.scalar.UnaryScalarFunctio
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeProcessor.DateTimeExtractor;
import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.NodeInfo.NodeCtor2;
import org.joda.time.DateTimeZone;
import java.time.temporal.ChronoField;
import java.util.TimeZone;
/**
* Extract the day of the year from a datetime.
*/
public class DayOfYear extends DateTimeFunction {
public DayOfYear(Location location, Expression field, DateTimeZone timeZone) {
public DayOfYear(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
}
@Override
protected NodeCtor2<Expression, DateTimeZone, DateTimeFunction> ctorForInfo() {
protected NodeCtor2<Expression, TimeZone, DateTimeFunction> ctorForInfo() {
return DayOfYear::new;
}

View File

@ -12,17 +12,18 @@ import org.elasticsearch.xpack.sql.tree.NodeInfo.NodeCtor2;
import org.joda.time.DateTimeZone;
import java.time.temporal.ChronoField;
import java.util.TimeZone;
/**
* Extract the hour of the day from a datetime.
*/
public class HourOfDay extends DateTimeFunction {
public HourOfDay(Location location, Expression field, DateTimeZone timeZone) {
public HourOfDay(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
}
@Override
protected NodeCtor2<Expression, DateTimeZone, DateTimeFunction> ctorForInfo() {
protected NodeCtor2<Expression, TimeZone, DateTimeFunction> ctorForInfo() {
return HourOfDay::new;
}

View File

@ -9,21 +9,21 @@ 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 org.elasticsearch.xpack.sql.tree.NodeInfo.NodeCtor2;
import org.joda.time.DateTimeZone;
import java.time.temporal.ChronoField;
import java.util.TimeZone;
/**
* Extract the minute of the day from a datetime.
*/
public class MinuteOfDay extends DateTimeFunction {
public MinuteOfDay(Location location, Expression field, DateTimeZone timeZone) {
public MinuteOfDay(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
}
@Override
protected NodeCtor2<Expression, DateTimeZone, DateTimeFunction> ctorForInfo() {
protected NodeCtor2<Expression, TimeZone, DateTimeFunction> ctorForInfo() {
return MinuteOfDay::new;
}

View File

@ -9,20 +9,20 @@ 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 org.elasticsearch.xpack.sql.tree.NodeInfo.NodeCtor2;
import org.joda.time.DateTimeZone;
import java.time.temporal.ChronoField;
import java.util.TimeZone;
/**
* Exract the minute of the hour from a datetime.
*/
public class MinuteOfHour extends DateTimeFunction {
public MinuteOfHour(Location location, Expression field, DateTimeZone timeZone) {
public MinuteOfHour(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
}
@Override
protected NodeCtor2<Expression, DateTimeZone, DateTimeFunction> ctorForInfo() {
protected NodeCtor2<Expression, TimeZone, DateTimeFunction> ctorForInfo() {
return MinuteOfHour::new;
}

View File

@ -9,20 +9,20 @@ 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 org.elasticsearch.xpack.sql.tree.NodeInfo.NodeCtor2;
import org.joda.time.DateTimeZone;
import java.time.temporal.ChronoField;
import java.util.TimeZone;
/**
* Extract the month of the year from a datetime.
*/
public class MonthOfYear extends DateTimeFunction {
public MonthOfYear(Location location, Expression field, DateTimeZone timeZone) {
public MonthOfYear(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
}
@Override
protected NodeCtor2<Expression, DateTimeZone, DateTimeFunction> ctorForInfo() {
protected NodeCtor2<Expression, TimeZone, DateTimeFunction> ctorForInfo() {
return MonthOfYear::new;
}

View File

@ -9,20 +9,20 @@ 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 org.elasticsearch.xpack.sql.tree.NodeInfo.NodeCtor2;
import org.joda.time.DateTimeZone;
import java.time.temporal.ChronoField;
import java.util.TimeZone;
/**
* Extract the second of the minute from a datetime.
*/
public class SecondOfMinute extends DateTimeFunction {
public SecondOfMinute(Location location, Expression field, DateTimeZone timeZone) {
public SecondOfMinute(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
}
@Override
protected NodeCtor2<Expression, DateTimeZone, DateTimeFunction> ctorForInfo() {
protected NodeCtor2<Expression, TimeZone, DateTimeFunction> ctorForInfo() {
return SecondOfMinute::new;
}

View File

@ -9,20 +9,20 @@ 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 org.elasticsearch.xpack.sql.tree.NodeInfo.NodeCtor2;
import org.joda.time.DateTimeZone;
import java.time.temporal.ChronoField;
import java.util.TimeZone;
/**
* Extract the week of the year from a datetime.
*/
public class WeekOfYear extends DateTimeFunction {
public WeekOfYear(Location location, Expression field, DateTimeZone timeZone) {
public WeekOfYear(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
}
@Override
protected NodeCtor2<Expression, DateTimeZone, DateTimeFunction> ctorForInfo() {
protected NodeCtor2<Expression, TimeZone, DateTimeFunction> ctorForInfo() {
return WeekOfYear::new;
}

View File

@ -9,20 +9,20 @@ 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 org.elasticsearch.xpack.sql.tree.NodeInfo.NodeCtor2;
import org.joda.time.DateTimeZone;
import java.time.temporal.ChronoField;
import java.util.TimeZone;
/**
* Extract the year from a datetime.
*/
public class Year extends DateTimeHistogramFunction {
public Year(Location location, Expression field, DateTimeZone timeZone) {
public Year(Location location, Expression field, TimeZone timeZone) {
super(location, field, timeZone);
}
@Override
protected NodeCtor2<Expression, DateTimeZone, DateTimeFunction> ctorForInfo() {
protected NodeCtor2<Expression, TimeZone, DateTimeFunction> ctorForInfo() {
return Year::new;
}

View File

@ -65,6 +65,7 @@ import org.joda.time.DateTimeZone;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicReference;
import static org.elasticsearch.xpack.sql.planner.QueryTranslator.and;
@ -75,6 +76,8 @@ import static org.elasticsearch.xpack.sql.planner.QueryTranslator.toQuery;
* Folds the PhysicalPlan into a {@link Query}.
*/
class QueryFolder extends RuleExecutor<PhysicalPlan> {
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
PhysicalPlan fold(PhysicalPlan plan) {
return execute(plan);
}
@ -279,7 +282,7 @@ class QueryFolder extends RuleExecutor<PhysicalPlan> {
if (matchingGroup != null) {
if (exp instanceof Attribute || exp instanceof ScalarFunction) {
Processor action = null;
DateTimeZone tz = null;
TimeZone tz = null;
/*
* special handling of dates since aggs return the typed Date object which needs
* extraction instead of handling this in the scroller, the folder handles this
@ -330,7 +333,7 @@ class QueryFolder extends RuleExecutor<PhysicalPlan> {
// check if the field is a date - if so mark it as such to interpret the long as a date
// UTC is used since that's what the server uses and there's no conversion applied
// (like for date histograms)
DateTimeZone dt = DataType.DATE == child.dataType() ? DateTimeZone.UTC : null;
TimeZone dt = DataType.DATE == child.dataType() ? UTC : null;
queryC = queryC.addColumn(new GroupByRef(matchingGroup.id(), null, dt));
}
else {
@ -349,7 +352,7 @@ class QueryFolder extends RuleExecutor<PhysicalPlan> {
matchingGroup = groupingContext.groupFor(ne);
Check.notNull(matchingGroup, "Cannot find group [{}]", Expressions.name(ne));
DateTimeZone dt = DataType.DATE == ne.dataType() ? DateTimeZone.UTC : null;
TimeZone dt = DataType.DATE == ne.dataType() ? UTC : null;
queryC = queryC.addColumn(new GroupByRef(matchingGroup.id(), null, dt));
}
}

View File

@ -11,6 +11,7 @@ import org.elasticsearch.xpack.sql.querydsl.container.Sort.Direction;
import org.joda.time.DateTimeZone;
import java.util.Objects;
import java.util.TimeZone;
/**
* GROUP BY key specific for date fields.
@ -18,13 +19,13 @@ import java.util.Objects;
public class GroupByDateKey extends GroupByKey {
private final String interval;
private final DateTimeZone timeZone;
private final TimeZone timeZone;
public GroupByDateKey(String id, String fieldName, String interval, DateTimeZone timeZone) {
public GroupByDateKey(String id, String fieldName, String interval, TimeZone timeZone) {
this(id, fieldName, null, interval, timeZone);
}
public GroupByDateKey(String id, String fieldName, Direction direction, String interval, DateTimeZone timeZone) {
public GroupByDateKey(String id, String fieldName, Direction direction, String interval, TimeZone timeZone) {
super(id, fieldName, direction);
this.interval = interval;
this.timeZone = timeZone;
@ -34,7 +35,7 @@ public class GroupByDateKey extends GroupByKey {
return interval;
}
public DateTimeZone timeZone() {
public TimeZone timeZone() {
return timeZone;
}
@ -43,7 +44,7 @@ public class GroupByDateKey extends GroupByKey {
return new DateHistogramValuesSourceBuilder(id())
.field(fieldName())
.dateHistogramInterval(new DateHistogramInterval(interval))
.timeZone(timeZone);
.timeZone(DateTimeZone.forTimeZone(timeZone));
}
@Override

View File

@ -6,7 +6,8 @@
package org.elasticsearch.xpack.sql.querydsl.container;
import org.elasticsearch.xpack.sql.execution.search.AggRef;
import org.joda.time.DateTimeZone;
import java.util.TimeZone;
/**
* Reference to a GROUP BY agg (typically this gets translated to a composite key).
@ -19,13 +20,13 @@ public class GroupByRef extends AggRef {
private final String key;
private final Property property;
private final DateTimeZone timeZone;
private final TimeZone timeZone;
public GroupByRef(String key) {
this(key, null, null);
}
public GroupByRef(String key, Property property, DateTimeZone timeZone) {
public GroupByRef(String key, Property property, TimeZone timeZone) {
this.key = key;
this.property = property == null ? Property.VALUE : property;
this.timeZone = timeZone;
@ -39,7 +40,7 @@ public class GroupByRef extends AggRef {
return property;
}
public DateTimeZone timeZone() {
public TimeZone timeZone() {
return timeZone;
}

View File

@ -9,17 +9,18 @@ import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.xpack.sql.plugin.AbstractSqlQueryRequest;
import org.joda.time.DateTimeZone;
import java.util.TimeZone;
// Typed object holding properties for a given action
public class Configuration {
public static final Configuration DEFAULT = new Configuration(DateTimeZone.UTC,
public static final Configuration DEFAULT = new Configuration(TimeZone.getTimeZone("UTC"),
AbstractSqlQueryRequest.DEFAULT_FETCH_SIZE,
AbstractSqlQueryRequest.DEFAULT_REQUEST_TIMEOUT,
AbstractSqlQueryRequest.DEFAULT_PAGE_TIMEOUT,
null);
private DateTimeZone timeZone;
private TimeZone timeZone;
private int pageSize;
private TimeValue requestTimeout;
private TimeValue pageTimeout;
@ -27,7 +28,7 @@ public class Configuration {
@Nullable
private QueryBuilder filter;
public Configuration(DateTimeZone tz, int pageSize, TimeValue requestTimeout, TimeValue pageTimeout, QueryBuilder filter) {
public Configuration(TimeZone tz, int pageSize, TimeValue requestTimeout, TimeValue pageTimeout, QueryBuilder filter) {
this.timeZone = tz;
this.pageSize = pageSize;
this.requestTimeout = requestTimeout;
@ -35,7 +36,7 @@ public class Configuration {
this.filter = filter;
}
public DateTimeZone timeZone() {
public TimeZone timeZone() {
return timeZone;
}

View File

@ -20,10 +20,10 @@ import org.elasticsearch.xpack.sql.plan.logical.Project;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.type.EsField;
import org.elasticsearch.xpack.sql.type.TypesTests;
import org.joda.time.DateTimeZone;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import static org.elasticsearch.xpack.sql.type.DataType.BOOLEAN;
import static org.elasticsearch.xpack.sql.type.DataType.KEYWORD;
@ -49,7 +49,7 @@ public class FieldAttributeTests extends ESTestCase {
EsIndex test = new EsIndex("test", mapping);
getIndexResult = IndexResolution.valid(test);
analyzer = new Analyzer(functionRegistry, getIndexResult, DateTimeZone.UTC);
analyzer = new Analyzer(functionRegistry, getIndexResult, TimeZone.getTimeZone("UTC"));
}
private LogicalPlan plan(String sql) {
@ -166,7 +166,7 @@ public class FieldAttributeTests extends ESTestCase {
EsIndex index = new EsIndex("test", mapping);
getIndexResult = IndexResolution.valid(index);
analyzer = new Analyzer(functionRegistry, getIndexResult, DateTimeZone.UTC);
analyzer = new Analyzer(functionRegistry, getIndexResult, TimeZone.getTimeZone("UTC"));
VerificationException ex = expectThrows(VerificationException.class, () -> plan("SELECT test.bar FROM test"));
assertEquals(

View File

@ -13,9 +13,9 @@ import org.elasticsearch.xpack.sql.expression.function.FunctionRegistry;
import org.elasticsearch.xpack.sql.parser.SqlParser;
import org.elasticsearch.xpack.sql.type.EsField;
import org.elasticsearch.xpack.sql.type.TypesTests;
import org.joda.time.DateTimeZone;
import java.util.Map;
import java.util.TimeZone;
public class VerifierErrorMessagesTests extends ESTestCase {
private SqlParser parser = new SqlParser();
@ -27,7 +27,7 @@ public class VerifierErrorMessagesTests extends ESTestCase {
}
private String verify(IndexResolution getIndexResult, String sql) {
Analyzer analyzer = new Analyzer(new FunctionRegistry(), getIndexResult, DateTimeZone.UTC);
Analyzer analyzer = new Analyzer(new FunctionRegistry(), getIndexResult, TimeZone.getTimeZone("UTC"));
AnalysisException e = expectThrows(AnalysisException.class, () -> analyzer.analyze(parser.createStatement(sql), true));
assertTrue(e.getMessage().startsWith("Found "));
String header = "Found 1 problem(s)\nline ";

View File

@ -12,6 +12,7 @@ import org.elasticsearch.test.AbstractWireSerializingTestCase;
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 java.io.IOException;
@ -23,7 +24,7 @@ import static java.util.Collections.singletonMap;
public class CompositeKeyExtractorTests extends AbstractWireSerializingTestCase<CompositeKeyExtractor> {
public static CompositeKeyExtractor randomCompositeKeyExtractor() {
return new CompositeKeyExtractor(randomAlphaOfLength(16), randomFrom(asList(Property.values())), randomDateTimeZone());
return new CompositeKeyExtractor(randomAlphaOfLength(16), randomFrom(asList(Property.values())), randomTimeZone());
}
@Override
@ -44,7 +45,7 @@ public class CompositeKeyExtractorTests extends AbstractWireSerializingTestCase<
public void testExtractBucketCount() {
Bucket bucket = new TestBucket(emptyMap(), randomLong(), new Aggregations(emptyList()));
CompositeKeyExtractor extractor = new CompositeKeyExtractor(randomAlphaOfLength(16), Property.COUNT,
randomDateTimeZone());
randomTimeZone());
assertEquals(bucket.getDocCount(), extractor.extract(bucket));
}
@ -57,15 +58,15 @@ public class CompositeKeyExtractorTests extends AbstractWireSerializingTestCase<
}
public void testExtractDate() {
CompositeKeyExtractor extractor = new CompositeKeyExtractor(randomAlphaOfLength(16), Property.VALUE, randomDateTimeZone());
CompositeKeyExtractor extractor = new CompositeKeyExtractor(randomAlphaOfLength(16), Property.VALUE, randomTimeZone());
long millis = System.currentTimeMillis();
Bucket bucket = new TestBucket(singletonMap(extractor.key(), millis), randomLong(), new Aggregations(emptyList()));
assertEquals(new DateTime(millis, extractor.timeZone()), extractor.extract(bucket));
assertEquals(new DateTime(millis, DateTimeZone.forTimeZone(extractor.timeZone())), extractor.extract(bucket));
}
public void testExtractIncorrectDateKey() {
CompositeKeyExtractor extractor = new CompositeKeyExtractor(randomAlphaOfLength(16), Property.VALUE, randomDateTimeZone());
CompositeKeyExtractor extractor = new CompositeKeyExtractor(randomAlphaOfLength(16), Property.VALUE, randomTimeZone());
Object value = new Object();
Bucket bucket = new TestBucket(singletonMap(extractor.key(), value), randomLong(), new Aggregations(emptyList()));

View File

@ -10,7 +10,6 @@ import org.elasticsearch.xpack.sql.tree.Location;
import org.elasticsearch.xpack.sql.tree.LocationTests;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.type.DataType;
import org.joda.time.DateTimeZone;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.processor.definition.ProcessorDefinition;
@ -18,6 +17,7 @@ import org.elasticsearch.xpack.sql.expression.function.scalar.script.ScriptTempl
import org.elasticsearch.xpack.sql.parser.ParsingException;
import java.util.Arrays;
import java.util.List;
import java.util.TimeZone;
import static org.elasticsearch.xpack.sql.expression.function.FunctionRegistry.def;
import static org.elasticsearch.xpack.sql.expression.function.UnresolvedFunction.ResolutionType.DISTINCT;
@ -32,16 +32,16 @@ public class FunctionRegistryTests extends ESTestCase {
UnresolvedFunction ur = uf(STANDARD);
FunctionRegistry r = new FunctionRegistry(Arrays.asList(def(Dummy.class, Dummy::new)));
FunctionDefinition def = r.resolveFunction(ur.name());
assertEquals(ur.location(), ur.buildResolved(randomDateTimeZone(), def).location());
assertEquals(ur.location(), ur.buildResolved(randomTimeZone(), def).location());
// Distinct isn't supported
ParsingException e = expectThrows(ParsingException.class, () ->
uf(DISTINCT).buildResolved(randomDateTimeZone(), def));
uf(DISTINCT).buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("does not support DISTINCT yet it was specified"));
// Any children aren't supported
e = expectThrows(ParsingException.class, () ->
uf(STANDARD, mock(Expression.class)).buildResolved(randomDateTimeZone(), def));
uf(STANDARD, mock(Expression.class)).buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("expects no arguments"));
}
@ -53,21 +53,21 @@ public class FunctionRegistryTests extends ESTestCase {
})));
FunctionDefinition def = r.resolveFunction(ur.name());
assertFalse(def.datetime());
assertEquals(ur.location(), ur.buildResolved(randomDateTimeZone(), def).location());
assertEquals(ur.location(), ur.buildResolved(randomTimeZone(), def).location());
// Distinct isn't supported
ParsingException e = expectThrows(ParsingException.class, () ->
uf(DISTINCT, mock(Expression.class)).buildResolved(randomDateTimeZone(), def));
uf(DISTINCT, mock(Expression.class)).buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("does not support DISTINCT yet it was specified"));
// No children aren't supported
e = expectThrows(ParsingException.class, () ->
uf(STANDARD).buildResolved(randomDateTimeZone(), def));
uf(STANDARD).buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("expects exactly one argument"));
// Multiple children aren't supported
e = expectThrows(ParsingException.class, () ->
uf(STANDARD, mock(Expression.class), mock(Expression.class)).buildResolved(randomDateTimeZone(), def));
uf(STANDARD, mock(Expression.class), mock(Expression.class)).buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("expects exactly one argument"));
}
@ -80,25 +80,25 @@ public class FunctionRegistryTests extends ESTestCase {
return new Dummy(l);
})));
FunctionDefinition def = r.resolveFunction(ur.name());
assertEquals(ur.location(), ur.buildResolved(randomDateTimeZone(), def).location());
assertEquals(ur.location(), ur.buildResolved(randomTimeZone(), def).location());
assertFalse(def.datetime());
// No children aren't supported
ParsingException e = expectThrows(ParsingException.class, () ->
uf(STANDARD).buildResolved(randomDateTimeZone(), def));
uf(STANDARD).buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("expects exactly one argument"));
// Multiple children aren't supported
e = expectThrows(ParsingException.class, () ->
uf(STANDARD, mock(Expression.class), mock(Expression.class)).buildResolved(randomDateTimeZone(), def));
uf(STANDARD, mock(Expression.class), mock(Expression.class)).buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("expects exactly one argument"));
}
public void testDateTimeFunction() {
boolean urIsExtract = randomBoolean();
UnresolvedFunction ur = uf(urIsExtract ? EXTRACT : STANDARD, mock(Expression.class));
DateTimeZone providedTimeZone = randomDateTimeZone();
FunctionRegistry r = new FunctionRegistry(Arrays.asList(def(Dummy.class, (Location l, Expression e, DateTimeZone tz) -> {
TimeZone providedTimeZone = randomTimeZone();
FunctionRegistry r = new FunctionRegistry(Arrays.asList(def(Dummy.class, (Location l, Expression e, TimeZone tz) -> {
assertEquals(providedTimeZone, tz);
assertSame(e, ur.children().get(0));
return new Dummy(l);
@ -109,17 +109,17 @@ public class FunctionRegistryTests extends ESTestCase {
// Distinct isn't supported
ParsingException e = expectThrows(ParsingException.class, () ->
uf(DISTINCT, mock(Expression.class)).buildResolved(randomDateTimeZone(), def));
uf(DISTINCT, mock(Expression.class)).buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("does not support DISTINCT yet it was specified"));
// No children aren't supported
e = expectThrows(ParsingException.class, () ->
uf(STANDARD).buildResolved(randomDateTimeZone(), def));
uf(STANDARD).buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("expects exactly one argument"));
// Multiple children aren't supported
e = expectThrows(ParsingException.class, () ->
uf(STANDARD, mock(Expression.class), mock(Expression.class)).buildResolved(randomDateTimeZone(), def));
uf(STANDARD, mock(Expression.class), mock(Expression.class)).buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("expects exactly one argument"));
}
@ -131,28 +131,28 @@ public class FunctionRegistryTests extends ESTestCase {
return new Dummy(l);
})));
FunctionDefinition def = r.resolveFunction(ur.name());
assertEquals(ur.location(), ur.buildResolved(randomDateTimeZone(), def).location());
assertEquals(ur.location(), ur.buildResolved(randomTimeZone(), def).location());
assertFalse(def.datetime());
// Distinct isn't supported
ParsingException e = expectThrows(ParsingException.class, () ->
uf(DISTINCT, mock(Expression.class), mock(Expression.class)).buildResolved(randomDateTimeZone(), def));
uf(DISTINCT, mock(Expression.class), mock(Expression.class)).buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("does not support DISTINCT yet it was specified"));
// No children aren't supported
e = expectThrows(ParsingException.class, () ->
uf(STANDARD).buildResolved(randomDateTimeZone(), def));
uf(STANDARD).buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("expects exactly two arguments"));
// One child isn't supported
e = expectThrows(ParsingException.class, () ->
uf(STANDARD, mock(Expression.class)).buildResolved(randomDateTimeZone(), def));
uf(STANDARD, mock(Expression.class)).buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("expects exactly two arguments"));
// Many children aren't supported
e = expectThrows(ParsingException.class, () ->
uf(STANDARD, mock(Expression.class), mock(Expression.class), mock(Expression.class))
.buildResolved(randomDateTimeZone(), def));
.buildResolved(randomTimeZone(), def));
assertThat(e.getMessage(), endsWith("expects exactly two arguments"));
}

View File

@ -12,10 +12,13 @@ import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.io.IOException;
import java.util.TimeZone;
public class DateTimeProcessorTests extends AbstractWireSerializingTestCase<DateTimeProcessor> {
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
public static DateTimeProcessor randomDateTimeProcessor() {
return new DateTimeProcessor(randomFrom(DateTimeExtractor.values()), DateTimeZone.UTC);
return new DateTimeProcessor(randomFrom(DateTimeExtractor.values()), UTC);
}
@Override
@ -31,15 +34,15 @@ public class DateTimeProcessorTests extends AbstractWireSerializingTestCase<Date
@Override
protected DateTimeProcessor mutateInstance(DateTimeProcessor instance) throws IOException {
DateTimeExtractor replaced = randomValueOtherThan(instance.extractor(), () -> randomFrom(DateTimeExtractor.values()));
return new DateTimeProcessor(replaced, DateTimeZone.UTC);
return new DateTimeProcessor(replaced, UTC);
}
public void testApply() {
DateTimeProcessor proc = new DateTimeProcessor(DateTimeExtractor.YEAR, DateTimeZone.UTC);
DateTimeProcessor proc = new DateTimeProcessor(DateTimeExtractor.YEAR, UTC);
assertEquals(1970, proc.process(new DateTime(0L, DateTimeZone.UTC)));
assertEquals(2017, proc.process(new DateTime(2017, 01, 02, 10, 10, DateTimeZone.UTC)));
proc = new DateTimeProcessor(DateTimeExtractor.DAY_OF_MONTH, DateTimeZone.UTC);
proc = new DateTimeProcessor(DateTimeExtractor.DAY_OF_MONTH, UTC);
assertEquals(1, proc.process(new DateTime(0L, DateTimeZone.UTC)));
assertEquals(2, proc.process(new DateTime(2017, 01, 02, 10, 10, DateTimeZone.UTC)));
assertEquals(31, proc.process(new DateTime(2017, 01, 31, 10, 10, DateTimeZone.UTC)));

View File

@ -11,24 +11,26 @@ import org.elasticsearch.xpack.sql.type.DataType;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.util.TimeZone;
public class DayOfYearTests extends ESTestCase {
private static final DateTimeZone UTC = DateTimeZone.UTC;
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
public void testAsColumnProcessor() {
assertEquals(1, extract(dateTime(0), UTC));
assertEquals(1, extract(dateTime(0), DateTimeZone.forID("+01:00")));
assertEquals(365, extract(dateTime(0), DateTimeZone.forID("-01:00")));
assertEquals(1, extract(dateTime(0), TimeZone.getTimeZone("GMT+01:00")));
assertEquals(365, extract(dateTime(0), TimeZone.getTimeZone("GMT-01:00")));
}
private DateTime dateTime(long millisSinceEpoch) {
return new DateTime(millisSinceEpoch, UTC);
return new DateTime(millisSinceEpoch, DateTimeZone.forTimeZone(UTC));
}
private Object extract(Object value, DateTimeZone timeZone) {
private Object extract(Object value, TimeZone timeZone) {
return build(value, timeZone).asProcessorDefinition().asProcessor().process(value);
}
private DayOfYear build(Object value, DateTimeZone timeZone) {
private DayOfYear build(Object value, TimeZone timeZone) {
return new DayOfYear(null, new Literal(null, value, DataType.DATE), timeZone);
}
}

View File

@ -70,6 +70,7 @@ import org.joda.time.DateTimeZone;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.TimeZone;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
@ -275,14 +276,15 @@ public class OptimizerTests extends ESTestCase {
}
public void testConstantFoldingDatetime() {
final TimeZone UTC = TimeZone.getTimeZone("UTC");
Expression cast = new Cast(EMPTY, Literal.of(EMPTY, "2018-01-19T10:23:27Z"), DataType.DATE);
assertEquals(2018, foldFunction(new Year(EMPTY, cast, DateTimeZone.UTC)));
assertEquals(1, foldFunction(new MonthOfYear(EMPTY, cast, DateTimeZone.UTC)));
assertEquals(19, foldFunction(new DayOfMonth(EMPTY, cast, DateTimeZone.UTC)));
assertEquals(19, foldFunction(new DayOfYear(EMPTY, cast, DateTimeZone.UTC)));
assertEquals(3, foldFunction(new WeekOfYear(EMPTY, cast, DateTimeZone.UTC)));
assertEquals(2018, foldFunction(new Year(EMPTY, cast, UTC)));
assertEquals(1, foldFunction(new MonthOfYear(EMPTY, cast, UTC)));
assertEquals(19, foldFunction(new DayOfMonth(EMPTY, cast, UTC)));
assertEquals(19, foldFunction(new DayOfYear(EMPTY, cast, UTC)));
assertEquals(3, foldFunction(new WeekOfYear(EMPTY, cast, UTC)));
assertNull(foldFunction(
new WeekOfYear(EMPTY, new Literal(EMPTY, null, DataType.NULL), DateTimeZone.UTC)));
new WeekOfYear(EMPTY, new Literal(EMPTY, null, DataType.NULL), UTC)));
}
public void testArithmeticFolding() {
@ -367,4 +369,4 @@ public class OptimizerTests extends ESTestCase {
assertEquals(expected, simplification.rule(actual));
}
}
}

View File

@ -17,7 +17,8 @@ import org.elasticsearch.xpack.sql.parser.SqlParser;
import org.elasticsearch.xpack.sql.plan.logical.command.Command;
import org.elasticsearch.xpack.sql.session.SqlSession;
import org.elasticsearch.xpack.sql.type.TypesTests;
import org.joda.time.DateTimeZone;
import java.util.TimeZone;
import static java.util.Collections.singletonList;
import static org.mockito.Matchers.any;
@ -32,7 +33,7 @@ public class SysCatalogsTests extends ESTestCase {
@SuppressWarnings({ "rawtypes", "unchecked" })
private Tuple<Command, SqlSession> sql(String sql) {
EsIndex test = new EsIndex("test", TypesTests.loadMapping("mapping-multi-field-with-nested.json", true));
Analyzer analyzer = new Analyzer(new FunctionRegistry(), IndexResolution.valid(test), DateTimeZone.UTC);
Analyzer analyzer = new Analyzer(new FunctionRegistry(), IndexResolution.valid(test), TimeZone.getTimeZone("UTC"));
Command cmd = (Command) analyzer.analyze(parser.createStatement(sql), true);
IndexResolver resolver = mock(IndexResolver.class);

View File

@ -19,10 +19,10 @@ import org.elasticsearch.xpack.sql.session.SqlSession;
import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.type.EsField;
import org.elasticsearch.xpack.sql.type.TypesTests;
import org.joda.time.DateTimeZone;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
@ -39,7 +39,7 @@ public class SysParserTests extends ESTestCase {
@SuppressWarnings({ "rawtypes", "unchecked" })
private Tuple<Command, SqlSession> sql(String sql) {
EsIndex test = new EsIndex("test", mapping);
Analyzer analyzer = new Analyzer(new FunctionRegistry(), IndexResolution.valid(test), DateTimeZone.UTC);
Analyzer analyzer = new Analyzer(new FunctionRegistry(), IndexResolution.valid(test), TimeZone.getTimeZone("UTC"));
Command cmd = (Command) analyzer.analyze(parser.createStatement(sql), true);
IndexResolver resolver = mock(IndexResolver.class);

View File

@ -17,7 +17,8 @@ import org.elasticsearch.xpack.sql.parser.SqlParser;
import org.elasticsearch.xpack.sql.plan.logical.command.Command;
import org.elasticsearch.xpack.sql.session.SqlSession;
import org.elasticsearch.xpack.sql.type.TypesTests;
import org.joda.time.DateTimeZone;
import java.util.TimeZone;
import static org.mockito.Mockito.mock;
@ -27,7 +28,7 @@ public class SysTableTypesTests extends ESTestCase {
private Tuple<Command, SqlSession> sql(String sql) {
EsIndex test = new EsIndex("test", TypesTests.loadMapping("mapping-multi-field-with-nested.json", true));
Analyzer analyzer = new Analyzer(new FunctionRegistry(), IndexResolution.valid(test), DateTimeZone.UTC);
Analyzer analyzer = new Analyzer(new FunctionRegistry(), IndexResolution.valid(test), TimeZone.getTimeZone("UTC"));
Command cmd = (Command) analyzer.analyze(parser.createStatement(sql), true);
IndexResolver resolver = mock(IndexResolver.class);

View File

@ -23,12 +23,12 @@ import org.elasticsearch.xpack.sql.session.SqlSession;
import org.elasticsearch.xpack.sql.type.DataTypes;
import org.elasticsearch.xpack.sql.type.EsField;
import org.elasticsearch.xpack.sql.type.TypesTests;
import org.joda.time.DateTimeZone;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.function.Consumer;
import static java.util.Arrays.asList;
@ -203,7 +203,7 @@ public class SysTablesTests extends ESTestCase {
private Tuple<Command, SqlSession> sql(String sql, List<SqlTypedParamValue> params) {
EsIndex test = new EsIndex("test", mapping);
Analyzer analyzer = new Analyzer(new FunctionRegistry(), IndexResolution.valid(test), DateTimeZone.UTC);
Analyzer analyzer = new Analyzer(new FunctionRegistry(), IndexResolution.valid(test), TimeZone.getTimeZone("UTC"));
Command cmd = (Command) analyzer.analyze(parser.createStatement(sql, params), true);
IndexResolver resolver = mock(IndexResolver.class);
@ -231,4 +231,4 @@ public class SysTablesTests extends ESTestCase {
tuple.v1().execute(tuple.v2(), wrap(consumer::accept, ex -> fail(ex.getMessage())));
}
}
}

View File

@ -21,9 +21,9 @@ import org.elasticsearch.xpack.sql.querydsl.query.Query;
import org.elasticsearch.xpack.sql.querydsl.query.TermQuery;
import org.elasticsearch.xpack.sql.type.EsField;
import org.elasticsearch.xpack.sql.type.TypesTests;
import org.joda.time.DateTimeZone;
import java.util.Map;
import java.util.TimeZone;
public class QueryTranslatorTests extends ESTestCase {
@ -40,7 +40,7 @@ public class QueryTranslatorTests extends ESTestCase {
EsIndex test = new EsIndex("test", mapping);
getIndexResult = IndexResolution.valid(test);
analyzer = new Analyzer(functionRegistry, getIndexResult, DateTimeZone.UTC);
analyzer = new Analyzer(functionRegistry, getIndexResult, TimeZone.getTimeZone("UTC"));
}
private LogicalPlan plan(String sql) {

View File

@ -17,11 +17,11 @@ import org.elasticsearch.xpack.sql.type.DataType;
import org.elasticsearch.xpack.sql.type.EsField;
import org.elasticsearch.xpack.sql.type.KeywordEsField;
import org.elasticsearch.xpack.sql.type.TextEsField;
import org.joda.time.DateTimeZone;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TimeZone;
public class VerifierErrorMessagesTests extends ESTestCase {
@ -37,7 +37,7 @@ public class VerifierErrorMessagesTests extends ESTestCase {
mapping.put("keyword", new KeywordEsField("keyword", Collections.emptyMap(), true, DataType.KEYWORD.defaultPrecision, true));
EsIndex test = new EsIndex("test", mapping);
IndexResolution getIndexResult = IndexResolution.valid(test);
Analyzer analyzer = new Analyzer(new FunctionRegistry(), getIndexResult, DateTimeZone.UTC);
Analyzer analyzer = new Analyzer(new FunctionRegistry(), getIndexResult, TimeZone.getTimeZone("UTC"));
LogicalPlan plan = optimizer.optimize(analyzer.analyze(parser.createStatement(sql), true));
PlanningException e = expectThrows(PlanningException.class, () -> planner.mapPlan(plan, true));
assertTrue(e.getMessage().startsWith("Found "));