diff --git a/docs/reference/sql/endpoints/translate.asciidoc b/docs/reference/sql/endpoints/translate.asciidoc index 086efe1e1de9c..41f0d98805380 100644 --- a/docs/reference/sql/endpoints/translate.asciidoc +++ b/docs/reference/sql/endpoints/translate.asciidoc @@ -35,7 +35,7 @@ Which returns: }, { "field": "release_date", - "format": "epoch_millis" + "format": "strict_date_optional_time_nanos" } ], "sort": [ diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/FieldHitExtractor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/FieldHitExtractor.java index 5ed427b383b33..69b32768bdf9a 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/FieldHitExtractor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/FieldHitExtractor.java @@ -11,7 +11,6 @@ import org.elasticsearch.xpack.ql.execution.search.extractor.AbstractFieldHitExtractor; import org.elasticsearch.xpack.ql.type.DataType; import org.elasticsearch.xpack.ql.util.DateUtils; - import java.io.IOException; import java.time.Instant; import java.time.ZoneId; @@ -19,7 +18,6 @@ import java.util.List; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; public class FieldHitExtractor extends AbstractFieldHitExtractor { @@ -50,20 +48,17 @@ protected Object unwrapCustomValue(Object values) { if (dataType == DATETIME) { if (values instanceof String) { - return parseDateString(values); - } - } - if (dataType == DATETIME_NANOS) { - if (values instanceof String) { - return DateUtils.asDateTimeWithNanos(values.toString(), zoneId()); + // We ask @timestamp (or the defined alternative field) to be returned as `epoch_millis` + // when matching sequence to avoid parsing into ZonedDateTime objects for performance reasons. + return parseEpochMillisAsString(values.toString()); } } return null; } - protected Object parseDateString(Object values) { - return ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(values.toString())), zoneId()); + protected Object parseEpochMillisAsString(String str) { + return ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(str)), zoneId()); } @Override diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/TimestampFieldHitExtractor.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/TimestampFieldHitExtractor.java index 42530c202800c..41a5961f13e90 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/TimestampFieldHitExtractor.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/execution/search/extractor/TimestampFieldHitExtractor.java @@ -15,7 +15,7 @@ public TimestampFieldHitExtractor(FieldHitExtractor target) { } @Override - protected Object parseDateString(Object values) { - return Long.parseLong(values.toString()); + protected Object parseEpochMillisAsString(String str) { + return Long.parseLong(str); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/SearchHitFieldRef.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/SearchHitFieldRef.java index 9778eecd6a38b..8056e365df312 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/SearchHitFieldRef.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/querydsl/container/SearchHitFieldRef.java @@ -12,7 +12,6 @@ import org.elasticsearch.xpack.ql.type.DataType; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; // NB: this class is taken from SQL - it hasn't been ported over to QL // since at this stage is unclear whether the whole FieldExtraction infrastructure @@ -65,9 +64,8 @@ public String toString() { } private static String format(DataType dataType) { - if (dataType == DATETIME_NANOS) { - return "strict_date_optional_time_nanos"; - } + // We need epoch_millis for the tiebreaker timestamp field, because parsing timestamp strings + // can have a negative performance impact return dataType == DATETIME ? "epoch_millis" : null; } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/ScalarFunction.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/ScalarFunction.java index 448daa2a9178c..9c7e6f343c82e 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/ScalarFunction.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/expression/function/scalar/ScalarFunction.java @@ -19,12 +19,11 @@ import org.elasticsearch.xpack.ql.expression.gen.script.ScriptTemplate; import org.elasticsearch.xpack.ql.expression.gen.script.Scripts; import org.elasticsearch.xpack.ql.tree.Source; -import org.elasticsearch.xpack.ql.type.DataTypes; import org.elasticsearch.xpack.ql.util.DateUtils; import static java.util.Collections.emptyList; import static org.elasticsearch.xpack.ql.expression.gen.script.ParamsBuilder.paramsBuilder; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; +import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; /** * A {@code ScalarFunction} is a {@code Function} that takes values from some @@ -128,9 +127,9 @@ protected ScriptTemplate scriptWithGrouping(GroupingFunction grouping) { // FIXME: this needs to be refactored to account for different datatypes in different projects (ie DATE from SQL) private String basicTemplate(Function function) { - if (function.dataType().name().equals("DATE") || function.dataType() == DataTypes.DATETIME || - // Aggregations on date_nanos are returned as double - (function instanceof AggregateFunction && ((AggregateFunction) function).field().dataType() == DATETIME_NANOS)) { + if (function.dataType().name().equals("DATE") || function.dataType() == DATETIME || + // Aggregations on date_nanos are returned as string + (function instanceof AggregateFunction && ((AggregateFunction) function).field().dataType() == DATETIME)) { return "{sql}.asDateTime({})"; } else { diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java index ac91942e56d97..98ae62e660145 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/index/IndexResolver.java @@ -66,7 +66,6 @@ import static java.util.Collections.emptySet; import static org.elasticsearch.action.ActionListener.wrap; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD; import static org.elasticsearch.xpack.ql.type.DataTypes.OBJECT; import static org.elasticsearch.xpack.ql.type.DataTypes.TEXT; @@ -450,9 +449,6 @@ private static EsField createField(DataTypeRegistry typeRegistry, String fieldNa if (esType == DATETIME) { return DateEsField.dateEsField(fieldName, props, isAggregateable); } - if (esType == DATETIME_NANOS) { - return DateEsField.dateNanosEsField(fieldName, props, isAggregateable); - } if (esType == UNSUPPORTED) { return new UnsupportedEsField(fieldName, typeName, null, props); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataTypes.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataTypes.java index 836025ec2a798..3eb5e7f7456c4 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataTypes.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DataTypes.java @@ -41,7 +41,6 @@ public final class DataTypes { public static final DataType TEXT = new DataType("text", Integer.MAX_VALUE, false, false, false); // date public static final DataType DATETIME = new DataType("DATETIME", "date", Long.BYTES, false, false, true); - public static final DataType DATETIME_NANOS = new DataType("DATETIME", "date_nanos", Long.BYTES, false, false, true); // ip public static final DataType IP = new DataType("ip", 45, false, false, true); // binary @@ -77,11 +76,11 @@ public final class DataTypes { private static final Map NAME_TO_TYPE = unmodifiableMap(TYPES.stream() .collect(toMap(DataType::typeName, t -> t))); - private static final Map ES_TO_TYPE; + private static Map ES_TO_TYPE; static { Map map = TYPES.stream().filter(e -> e.esType() != null).collect(toMap(DataType::esType, t -> t)); - map.put(DATETIME_NANOS.esType(), DATETIME_NANOS); + map.put("date_nanos", DATETIME); ES_TO_TYPE = Collections.unmodifiableMap(map); } @@ -160,7 +159,7 @@ public static boolean isSigned(DataType t) { } public static boolean isDateTime(DataType type) { - return type == DATETIME || type == DATETIME_NANOS; + return type == DATETIME; } public static boolean areCompatible(DataType left, DataType right) { diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DateEsField.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DateEsField.java index bd0403e963bed..cb488adab5e31 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DateEsField.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/DateEsField.java @@ -17,10 +17,6 @@ public static DateEsField dateEsField(String name, Map properti return new DateEsField(name, DataTypes.DATETIME, properties, hasDocValues); } - public static DateEsField dateNanosEsField(String name, Map properties, boolean hasDocValues) { - return new DateEsField(name, DataTypes.DATETIME_NANOS, properties, hasDocValues); - } - private DateEsField(String name, DataType dataType, Map properties, boolean hasDocValues) { super(name, dataType, properties, hasDocValues); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/Types.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/Types.java index f59b362cc4cb6..11e5c1844791e 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/Types.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/type/Types.java @@ -16,7 +16,6 @@ import static java.util.Collections.emptyMap; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD; import static org.elasticsearch.xpack.ql.type.DataTypes.NESTED; import static org.elasticsearch.xpack.ql.type.DataTypes.OBJECT; @@ -97,8 +96,6 @@ private static void walkMapping(DataTypeRegistry typeRegistry, String name, Obje field = new KeywordEsField(name, properties, docValues, length, normalized); } else if (esDataType == DATETIME) { field = DateEsField.dateEsField(name, properties, docValues); - } else if (esDataType == DATETIME_NANOS) { - field = DateEsField.dateNanosEsField(name, properties, docValues); } else if (esDataType == UNSUPPORTED) { String type = content.get("type").toString(); field = new UnsupportedEsField(name, type, null, properties); diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/scalar/FunctionTestUtils.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/scalar/FunctionTestUtils.java index 692bc78c5b518..2b96f835b4214 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/scalar/FunctionTestUtils.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/expression/function/scalar/FunctionTestUtils.java @@ -17,11 +17,9 @@ import java.util.BitSet; import java.util.Iterator; -import static org.elasticsearch.test.ESTestCase.randomFrom; import static org.elasticsearch.xpack.ql.tree.Source.EMPTY; import static org.elasticsearch.xpack.ql.type.DataTypes.BOOLEAN; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; import static org.elasticsearch.xpack.ql.type.DataTypes.INTEGER; import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD; @@ -48,8 +46,7 @@ public static Literal randomBooleanLiteral() { } public static Literal randomDatetimeLiteral() { - return l(ZonedDateTime.ofInstant(Instant.ofEpochMilli(ESTestCase.randomLong()), ESTestCase.randomZone()), - randomFrom(DATETIME, DATETIME_NANOS)); + return l(ZonedDateTime.ofInstant(Instant.ofEpochMilli(ESTestCase.randomLong()), ESTestCase.randomZone()), DATETIME); } public static class Combinations implements Iterable { diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/DataTypeConversionTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/DataTypeConversionTests.java index 7b4d78d544eda..cd56a59e3757a 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/DataTypeConversionTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/DataTypeConversionTests.java @@ -19,7 +19,6 @@ import static org.elasticsearch.xpack.ql.type.DataTypes.BOOLEAN; import static org.elasticsearch.xpack.ql.type.DataTypes.BYTE; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; import static org.elasticsearch.xpack.ql.type.DataTypes.DOUBLE; import static org.elasticsearch.xpack.ql.type.DataTypes.FLOAT; import static org.elasticsearch.xpack.ql.type.DataTypes.INTEGER; @@ -42,7 +41,7 @@ public void testConversionToString() { assertEquals("10.0", conversion.convert(10.0)); } { - Converter conversion = converterFor(randomFrom(DATETIME, DATETIME_NANOS), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals("1973-11-29T21:33:09.101Z", conversion.convert(asDateTime(123456789101L))); assertEquals("1966-02-02T02:26:50.899Z", conversion.convert(asDateTime(-123456789101L))); @@ -77,7 +76,7 @@ public void testConversionToLong() { assertEquals(0L, conversion.convert(false)); } { - Converter conversion = converterFor(randomFrom(DATETIME, DATETIME_NANOS), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals(123456789101L, conversion.convert(asDateTime(123456789101L))); assertEquals(-123456789101L, conversion.convert(asDateTime(-123456789101L))); @@ -138,7 +137,7 @@ public void testConversionToDateTime() { // double check back and forth conversion ZonedDateTime dt = org.elasticsearch.common.time.DateUtils.nowWithMillisResolution(); - Converter forward = converterFor(randomFrom(DATETIME, DATETIME_NANOS), KEYWORD); + Converter forward = converterFor(DATETIME, KEYWORD); Converter back = converterFor(KEYWORD, DATETIME); assertEquals(dt, back.convert(forward.convert(dt))); Exception e = expectThrows(QlIllegalArgumentException.class, () -> conversion.convert("0xff")); @@ -169,7 +168,7 @@ public void testConversionToFloat() { assertEquals(0.0f, (float) conversion.convert(false), 0); } { - Converter conversion = converterFor(randomFrom(DATETIME, DATETIME_NANOS), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals(1.23456789101E11f, (float) conversion.convert(asDateTime(123456789101L)), 0); assertEquals(-1.23456789101E11f, (float) conversion.convert(asDateTime(-123456789101L)), 0); @@ -209,7 +208,7 @@ public void testConversionToDouble() { assertEquals(0.0, (double) conversion.convert(false), 0); } { - Converter conversion = converterFor(randomFrom(DATETIME, DATETIME_NANOS), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals(1.23456789101E11, (double) conversion.convert(asDateTime(123456789101L)), 0); assertEquals(-1.23456789101E11, (double) conversion.convert(asDateTime(-123456789101L)), 0); @@ -258,7 +257,7 @@ public void testConversionToBoolean() { assertEquals(false, conversion.convert(0.0d)); } { - Converter conversion = converterFor(randomFrom(DATETIME, DATETIME_NANOS), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals(true, conversion.convert(asDateTime(123456789101L))); assertEquals(true, conversion.convert(asDateTime(-123456789101L))); @@ -300,7 +299,7 @@ public void testConversionToInt() { assertEquals("[" + Long.MAX_VALUE + "] out of [integer] range", e.getMessage()); } { - Converter conversion = converterFor(randomFrom(DATETIME, DATETIME_NANOS), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals(12345678, conversion.convert(asDateTime(12345678L))); assertEquals(223456789, conversion.convert(asDateTime(223456789L))); @@ -324,7 +323,7 @@ public void testConversionToShort() { assertEquals("[" + Integer.MAX_VALUE + "] out of [short] range", e.getMessage()); } { - Converter conversion = converterFor(randomFrom(DATETIME, DATETIME_NANOS), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals((short) 12345, conversion.convert(asDateTime(12345L))); assertEquals((short) -12345, conversion.convert(asDateTime(-12345L))); @@ -348,7 +347,7 @@ public void testConversionToByte() { assertEquals("[" + Short.MAX_VALUE + "] out of [byte] range", e.getMessage()); } { - Converter conversion = converterFor(randomFrom(DATETIME, DATETIME_NANOS), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals((byte) 123, conversion.convert(asDateTime(123L))); assertEquals((byte) -123, conversion.convert(asDateTime(-123L))); @@ -390,9 +389,6 @@ public void testCommonType() { assertEquals(FLOAT, commonType(FLOAT, INTEGER)); assertEquals(DOUBLE, commonType(DOUBLE, FLOAT)); - assertEquals(DATETIME, commonType(DATETIME, DATETIME_NANOS)); - assertEquals(DATETIME, commonType(DATETIME_NANOS, DATETIME)); - // strings assertEquals(TEXT, commonType(TEXT, KEYWORD)); assertEquals(TEXT, commonType(KEYWORD, TEXT)); diff --git a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/TypesTests.java b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/TypesTests.java index c9ec66c8c2082..b1647b43bb5f4 100644 --- a/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/TypesTests.java +++ b/x-pack/plugin/ql/src/test/java/org/elasticsearch/xpack/ql/type/TypesTests.java @@ -16,7 +16,6 @@ import static java.util.Collections.emptyMap; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; import static org.elasticsearch.xpack.ql.type.DataTypes.INTEGER; import static org.elasticsearch.xpack.ql.type.DataTypes.KEYWORD; import static org.elasticsearch.xpack.ql.type.DataTypes.NESTED; @@ -109,7 +108,7 @@ public void testDateNanosField() { assertThat(mapping.size(), is(1)); EsField field = mapping.get("date_nanos"); - assertThat(field.getDataType(), is(DATETIME_NANOS)); + assertThat(field.getDataType(), is(DATETIME)); assertThat(field.isAggregatable(), is(true)); assertThat(field, is(instanceOf(DateEsField.class))); } diff --git a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlTranslateActionIT.java b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlTranslateActionIT.java index a4c30bb35aebe..05f42cebc82e4 100644 --- a/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlTranslateActionIT.java +++ b/x-pack/plugin/sql/src/internalClusterTest/java/org/elasticsearch/xpack/sql/action/SqlTranslateActionIT.java @@ -6,15 +6,15 @@ */ package org.elasticsearch.xpack.sql.action; +import java.util.ArrayList; +import java.util.List; + import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.support.WriteRequest; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.FieldAndFormat; import org.elasticsearch.search.sort.SortBuilders; -import java.util.ArrayList; -import java.util.List; - import static java.util.Collections.singletonList; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; @@ -39,13 +39,12 @@ public void testSqlTranslateAction() { if (columnOrder) { expectedFields.add(new FieldAndFormat("data", null)); expectedFields.add(new FieldAndFormat("count", null)); - expectedFields.add(new FieldAndFormat("date", "epoch_millis")); + expectedFields.add(new FieldAndFormat("date", "strict_date_optional_time_nanos")); } else { - expectedFields.add(new FieldAndFormat("date", "epoch_millis")); + expectedFields.add(new FieldAndFormat("date", "strict_date_optional_time_nanos")); expectedFields.add(new FieldAndFormat("data", null)); expectedFields.add(new FieldAndFormat("count", null)); } - assertEquals(expectedFields, actualFields); assertEquals(singletonList(SortBuilders.fieldSort("count").missing("_last").unmappedType("long")), source.sorts()); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractor.java index 8f2e089c01c65..a0d74a84aab15 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractor.java @@ -18,14 +18,12 @@ import org.elasticsearch.xpack.sql.expression.literal.geo.GeoShape; import org.elasticsearch.xpack.sql.type.SqlDataTypes; import org.elasticsearch.xpack.sql.util.DateUtils; - import java.io.IOException; import java.time.ZoneId; import java.util.List; import java.util.Map; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; import static org.elasticsearch.xpack.sql.type.SqlDataTypes.GEO_POINT; import static org.elasticsearch.xpack.sql.type.SqlDataTypes.GEO_SHAPE; import static org.elasticsearch.xpack.sql.type.SqlDataTypes.SHAPE; @@ -123,15 +121,6 @@ protected Object unwrapCustomValue(Object values) { throw new SqlIllegalArgumentException("Objects (returned by [{}]) are not supported", fieldName()); } if (dataType == DATETIME) { - if (values instanceof String) { - try { - return DateUtils.asDateTimeWithMillis(Long.parseLong(values.toString()), zoneId()); - } catch (NumberFormatException e) { - return DateUtils.asDateTimeWithNanos(values.toString()).withZoneSameInstant(zoneId()); - } - } - } - if (dataType == DATETIME_NANOS) { if (values instanceof String) { return DateUtils.asDateTimeWithNanos(values.toString()).withZoneSameInstant(zoneId()); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractor.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractor.java index 347fdb17f2be0..323a20177c626 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractor.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractor.java @@ -16,13 +16,11 @@ import org.elasticsearch.xpack.sql.common.io.SqlStreamInput; import org.elasticsearch.xpack.sql.type.SqlDataTypes; import org.elasticsearch.xpack.sql.util.DateUtils; - import java.io.IOException; import java.time.ZoneId; import java.util.Objects; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; import static org.elasticsearch.xpack.sql.type.SqlDataTypes.DATE; public class TopHitsAggExtractor implements BucketExtractor { @@ -81,13 +79,7 @@ public Object extract(Bucket bucket) { Object value = agg.getHits().getAt(0).getFields().values().iterator().next().getValue(); if (fieldDataType == DATETIME || fieldDataType == DATE) { - try { - return DateUtils.asDateTimeWithMillis(Long.parseLong(value.toString()), zoneId); - } catch (NumberFormatException e) { - return DateUtils.asDateTimeWithNanos(value.toString()).withZoneSameInstant(zoneId); - } - } else if (fieldDataType == DATETIME_NANOS) { - return DateUtils.asDateTimeWithNanos(value.toString()); + return DateUtils.asDateTimeWithNanos(value.toString()).withZoneSameInstant(zoneId()); } else if (SqlDataTypes.isTimeBased(fieldDataType)) { return DateUtils.asTimeOnly(Long.parseLong(value.toString()), zoneId); } else { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainer.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainer.java index 13b8a59882f77..e0b1d081ec322 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainer.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/QueryContainer.java @@ -328,16 +328,7 @@ private String aliasName(Attribute attr) { // reference methods // private FieldExtraction topHitFieldRef(FieldAttribute fieldAttr) { - FieldAttribute actualField = fieldAttr; - FieldAttribute rootField = fieldAttr; - StringBuilder fullFieldName = new StringBuilder(fieldAttr.field().getName()); - - while (rootField.parent() != null) { - fullFieldName.insert(0, ".").insert(0, rootField.parent().field().getName()); - rootField = rootField.parent(); - } - return new SearchHitFieldRef(aliasName(actualField), fullFieldName.toString(), fieldAttr.field().getDataType(), - fieldAttr.field().isAggregatable(), fieldAttr.field().isAlias()); + return new SearchHitFieldRef(aliasName(fieldAttr), fieldAttr.field().getDataType()); } private Tuple nestedHitFieldRef(FieldAttribute attr) { @@ -347,8 +338,7 @@ private Tuple nestedHitFieldRef(FieldAttribute SqlDataTypes.format(attr.field().getDataType()), SqlDataTypes.isFromDocValuesOnly(attr.field().getDataType())); - SearchHitFieldRef nestedFieldRef = new SearchHitFieldRef(name, null, attr.field().getDataType(), attr.field().isAggregatable(), - false, attr.parent().name()); + SearchHitFieldRef nestedFieldRef = new SearchHitFieldRef(name, attr.field().getDataType(), attr.parent().name()); return new Tuple<>( new QueryContainer(q, aggs, fields, aliases, pseudoFunctions, scalarFunctions, sort, limit, trackHits, includeFrozen, diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/SearchHitFieldRef.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/SearchHitFieldRef.java index 675c6ed0207f3..1f28f06c3a926 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/SearchHitFieldRef.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/container/SearchHitFieldRef.java @@ -12,24 +12,16 @@ public class SearchHitFieldRef extends FieldReference { private final String name; - private final String fullFieldName; // path included. If field full path is a.b.c, full field name is "a.b.c" and name is "c" private final DataType dataType; - private final boolean docValue; private final String hitName; - public SearchHitFieldRef(String name, String fullFieldName, DataType dataType, boolean useDocValueInsteadOfSource, boolean isAlias) { - this(name, fullFieldName, dataType, useDocValueInsteadOfSource, isAlias, null); + public SearchHitFieldRef(String name, DataType dataType) { + this(name, dataType, null); } - public SearchHitFieldRef(String name, String fullFieldName, DataType dataType, boolean useDocValueInsteadOfSource, boolean isAlias, - String hitName) { + public SearchHitFieldRef(String name, DataType dataType, String hitName) { this.name = name; - this.fullFieldName = fullFieldName; this.dataType = dataType; - // these field types can only be extracted from docvalue_fields (ie, values already computed by Elasticsearch) - // because, for us to be able to extract them from _source, we would need the mapping of those fields (which we don't have) - this.docValue = isAlias ? useDocValueInsteadOfSource : - (SqlDataTypes.isFromDocValuesOnly(dataType) ? useDocValueInsteadOfSource : false); this.hitName = hitName; } @@ -42,18 +34,10 @@ public String name() { return name; } - public String fullFieldName() { - return fullFieldName; - } - public DataType getDataType() { return dataType; } - public boolean useDocValue() { - return docValue; - } - @Override public void collectFields(QlSourceBuilder sourceBuilder) { // nested fields are handled by inner hits diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverter.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverter.java index 23c74fb684c09..57f454fa9f20a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverter.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverter.java @@ -39,7 +39,6 @@ import static org.elasticsearch.xpack.ql.type.DataTypes.BOOLEAN; import static org.elasticsearch.xpack.ql.type.DataTypes.BYTE; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; import static org.elasticsearch.xpack.ql.type.DataTypes.DOUBLE; import static org.elasticsearch.xpack.ql.type.DataTypes.FLOAT; import static org.elasticsearch.xpack.ql.type.DataTypes.INTEGER; @@ -115,9 +114,6 @@ public static DataType commonType(DataType left, DataType right) { return DATETIME; } } - if (isDateTime(left) && isDateTime(right)) { - return DATETIME_NANOS; - } // Interval * integer is a valid operation if (isInterval(left)) { diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypes.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypes.java index 9d27e1df0e6af..e116b61eb29d1 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypes.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/SqlDataTypes.java @@ -34,7 +34,6 @@ import static org.elasticsearch.xpack.ql.type.DataTypes.BOOLEAN; import static org.elasticsearch.xpack.ql.type.DataTypes.BYTE; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; import static org.elasticsearch.xpack.ql.type.DataTypes.DOUBLE; import static org.elasticsearch.xpack.ql.type.DataTypes.FLOAT; import static org.elasticsearch.xpack.ql.type.DataTypes.HALF_FLOAT; @@ -164,14 +163,13 @@ public class SqlDataTypes { .collect(toList())); private static final Map NAME_TO_TYPE = unmodifiableMap(TYPES.stream() - .filter(t -> t != DATETIME_NANOS) .collect(toMap(DataType::typeName, t -> t))); private static final Map ES_TO_TYPE; static { Map map = TYPES.stream().filter(e -> e.esType() != null).collect(Collectors.toMap(DataType::esType, t -> t)); - map.put(DATETIME_NANOS.esType(), DATETIME_NANOS); + map.put("date_nanos", DATETIME); ES_TO_TYPE = Collections.unmodifiableMap(map); } @@ -274,17 +272,13 @@ public static boolean isGeo(DataType type) { } public static String format(DataType type) { - if (type == DATETIME_NANOS) { - return "strict_date_optional_time_nanos"; - } - return isDateOrTimeBased(type) ? "epoch_millis" : null; + return isDateOrTimeBased(type) ? "strict_date_optional_time_nanos" : null; } public static boolean isFromDocValuesOnly(DataType dataType) { return dataType == KEYWORD // because of ignore_above. Extracting this from _source wouldn't make sense || dataType == DATE // because of date formats || dataType == DATETIME - || dataType == DATETIME_NANOS || dataType == SCALED_FLOAT // because of scaling_factor || dataType == GEO_POINT || dataType == SHAPE; diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractorTests.java index 3c5a527d5ad34..52d5612e2c2ab 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractorTests.java @@ -16,8 +16,6 @@ import org.elasticsearch.xpack.sql.proto.StringUtils; import org.elasticsearch.xpack.sql.type.SqlDataTypes; import org.elasticsearch.xpack.sql.util.DateUtils; - -import java.io.IOException; import java.math.BigDecimal; import java.math.BigInteger; import java.time.ZoneId; @@ -35,7 +33,6 @@ import static java.util.Collections.singletonMap; import static org.elasticsearch.common.time.DateUtils.toMilliSeconds; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; import static org.elasticsearch.xpack.sql.type.SqlDataTypes.GEO_SHAPE; import static org.elasticsearch.xpack.sql.type.SqlDataTypes.SHAPE; import static org.elasticsearch.xpack.sql.util.DateUtils.UTC; @@ -115,16 +112,6 @@ public void testGetDocValue() { } } - public void testGetDate() { - ZoneId zoneId = randomZone(); - long millis = randomNonNegativeLong(); - List documentFieldValues = Collections.singletonList(Long.toString(millis)); - DocumentField field = new DocumentField("my_date_field", documentFieldValues); - SearchHit hit = new SearchHit(1, null, null, singletonMap("my_date_field", field), null); - FieldHitExtractor extractor = new FieldHitExtractor("my_date_field", DATETIME, zoneId, true); - assertEquals(DateUtils.asDateTimeWithMillis(millis, zoneId), extractor.extract(hit)); - } - public void testGetDateNanos() { ZoneId zoneId = randomZone(); long totalNanos = randomLongBetween(72000000000000L, Long.MAX_VALUE); @@ -134,7 +121,7 @@ public void testGetDateNanos() { List documentFieldValues = Collections.singletonList(StringUtils.toString(zdt)); DocumentField field = new DocumentField("my_date_nanos_field", documentFieldValues); SearchHit hit = new SearchHit(1, null, null, singletonMap("my_date_nanos_field", field), null); - FieldHitExtractor extractor = new FieldHitExtractor("my_date_nanos_field", DATETIME_NANOS, zoneId, true); + FieldHitExtractor extractor = new FieldHitExtractor("my_date_nanos_field", DATETIME, zoneId, true); assertEquals(zdt, extractor.extract(hit)); } @@ -154,14 +141,14 @@ public void testMultiValuedDocValue() { assertThat(ex.getMessage(), is("Arrays (returned by [" + fieldName + "]) are not supported")); } - public void testExtractSourcePath() throws IOException { + public void testExtractSourcePath() { FieldHitExtractor fe = getFieldHitExtractor("a.b.c"); Object value = randomValue(); DocumentField field = new DocumentField("a.b.c", singletonList(value)); SearchHit hit = new SearchHit(1, null, null, singletonMap("a.b.c", field), null); assertThat(fe.extract(hit), is(value)); } - + public void testMultiValuedSource() { FieldHitExtractor fe = getFieldHitExtractor("a"); Object value = randomValue(); @@ -170,7 +157,7 @@ public void testMultiValuedSource() { QlIllegalArgumentException ex = expectThrows(QlIllegalArgumentException.class, () -> fe.extract(hit)); assertThat(ex.getMessage(), is("Arrays (returned by [a]) are not supported")); } - + public void testMultiValuedSourceAllowed() { FieldHitExtractor fe = new FieldHitExtractor("a", null, UTC, true); Object valueA = randomValue(); @@ -192,11 +179,11 @@ public void testGeoShapeExtraction() { assertEquals(new GeoShape(1, 2), fe.extract(hit)); } - + public void testMultipleGeoShapeExtraction() { String fieldName = randomAlphaOfLength(5); FieldHitExtractor fe = new FieldHitExtractor(fieldName, randomBoolean() ? GEO_SHAPE : SHAPE, UTC, false); - + Map map1 = new HashMap<>(2); map1.put("coordinates", asList(1d, 2d)); map1.put("type", "Point"); @@ -208,7 +195,7 @@ public void testMultipleGeoShapeExtraction() { QlIllegalArgumentException ex = expectThrows(QlIllegalArgumentException.class, () -> fe.extract(hit)); assertThat(ex.getMessage(), is("Arrays (returned by [" + fieldName + "]) are not supported")); - + FieldHitExtractor lenientFe = new FieldHitExtractor(fieldName, randomBoolean() ? GEO_SHAPE : SHAPE, UTC, true); assertEquals(new GeoShape(3, 4), lenientFe.extract(new SearchHit(1, null, null, singletonMap(fieldName, new DocumentField(fieldName, singletonList(map2))), null))); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractorTests.java index 702f5a60aad49..655b32820981c 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/TopHitsAggExtractorTests.java @@ -19,9 +19,9 @@ import org.elasticsearch.xpack.ql.type.DataTypes; import org.elasticsearch.xpack.sql.AbstractSqlWireSerializingTestCase; import org.elasticsearch.xpack.sql.SqlIllegalArgumentException; +import org.elasticsearch.xpack.sql.proto.StringUtils; import org.elasticsearch.xpack.sql.type.SqlDataTypes; import org.elasticsearch.xpack.sql.util.DateUtils; - import java.time.ZoneId; import java.util.Collections; @@ -89,7 +89,8 @@ public void testExtractDateValue() { TopHitsAggExtractor extractor = new TopHitsAggExtractor("topHitsAgg", DataTypes.DATETIME, zoneId); long value = 123456789L; - Aggregation agg = new InternalTopHits(extractor.name(), 0, 1, null, searchHitsOf(value), null); + Aggregation agg = new InternalTopHits(extractor.name(), 0, 1, null, + searchHitsOf(StringUtils.toString(DateUtils.asDateTimeWithMillis(value, zoneId))), null); Bucket bucket = new TestBucket(emptyMap(), 0, new Aggregations(singletonList(agg))); assertEquals(DateUtils.asDateTimeWithMillis(value, zoneId), extractor.extract(bucket)); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ExpressionTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ExpressionTests.java index 1ca1da57e65a5..d0a8f7c5b964c 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ExpressionTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/parser/ExpressionTests.java @@ -304,11 +304,6 @@ public void testCastWithUnquotedDataType() { assertEquals(INTEGER, mul.dataType()); } - public void testCastToDatetimeNanosNotSupported() { - ParsingException e = expectThrows(ParsingException.class, () -> parser.createExpression("CAST('foo' AS DATETIME_NANOS)")); - assertEquals("line 1:16: Does not recognize type [DATETIME_NANOS]", e.getMessage()); - } - public void testCastWithQuotedDataType() { Expression expr = parser.createExpression("CAST(10*2 AS \"LonG\")"); assertEquals(Cast.class, expr.getClass()); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java index 4e532b3adde68..95eef86e14781 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/planner/QueryTranslatorTests.java @@ -1979,7 +1979,7 @@ public void testTopHitsAggregationWithOneArg() { assertEquals(DATETIME, eqe.output().get(0).dataType()); assertThat(eqe.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), endsWith("\"top_hits\":{\"from\":0,\"size\":1,\"version\":false,\"seq_no_primary_term\":false," + - "\"explain\":false,\"docvalue_fields\":[{\"field\":\"date\",\"format\":\"epoch_millis\"}]," + + "\"explain\":false,\"docvalue_fields\":[{\"field\":\"date\",\"format\":\"strict_date_optional_time_nanos\"}]," + "\"sort\":[{\"date\":{\"order\":\"desc\",\"missing\":\"_last\",\"unmapped_type\":\"date\"}}]}}}}}")); } { @@ -2020,7 +2020,7 @@ public void testTopHitsAggregationWithTwoArgs() { assertEquals(DATETIME, eqe.output().get(0).dataType()); assertThat(eqe.queryContainer().aggs().asAggBuilder().toString().replaceAll("\\s+", ""), endsWith("\"top_hits\":{\"from\":0,\"size\":1,\"version\":false,\"seq_no_primary_term\":false," + - "\"explain\":false,\"docvalue_fields\":[{\"field\":\"date\",\"format\":\"epoch_millis\"}]," + + "\"explain\":false,\"docvalue_fields\":[{\"field\":\"date\",\"format\":\"strict_date_optional_time_nanos\"}]," + "\"sort\":[{\"int\":{\"order\":\"desc\",\"missing\":\"_last\",\"unmapped_type\":\"integer\"}}," + "{\"date\":{\"order\":\"desc\",\"missing\":\"_last\",\"unmapped_type\":\"date\"}}]}}}}}")); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverterTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverterTests.java index 8b4117d6d8875..f0ebaa8967f79 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverterTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/SqlDataTypeConverterTests.java @@ -24,7 +24,6 @@ import static org.elasticsearch.xpack.ql.type.DataTypes.BOOLEAN; import static org.elasticsearch.xpack.ql.type.DataTypes.BYTE; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; import static org.elasticsearch.xpack.ql.type.DataTypes.DOUBLE; import static org.elasticsearch.xpack.ql.type.DataTypes.FLOAT; import static org.elasticsearch.xpack.ql.type.DataTypes.INTEGER; @@ -75,7 +74,7 @@ public void testConversionToString() { assertEquals("02:26:50.899Z", conversion.convert(asTimeOnly(-123456789101L))); } { - Converter conversion = converterFor(getDateTimeRandomNanos(), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals("1973-11-29T21:33:09.101Z", conversion.convert(DateUtils.asDateTimeWithMillis(123456789101L))); assertEquals("1966-02-02T02:26:50.899Z", conversion.convert(DateUtils.asDateTimeWithMillis(-123456789101L))); @@ -124,7 +123,7 @@ public void testConversionToLong() { assertEquals(8810899L, conversion.convert(asTimeOnly(-123456789101L))); } { - Converter conversion = converterFor(getDateTimeRandomNanos(), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals(123456789101L, conversion.convert(DateUtils.asDateTimeWithMillis(123456789101L))); assertEquals(-123456789101L, conversion.convert(DateUtils.asDateTimeWithMillis(-123456789101L))); @@ -168,7 +167,7 @@ public void testConversionToDate() { assertNull(converterFor(TIME, to)); } { - Converter conversion = converterFor(getDateTimeRandomNanos(), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals(date(123456780000L), conversion.convert(DateUtils.asDateTimeWithMillis(123456789101L))); assertEquals(date(-123456789101L), conversion.convert(DateUtils.asDateTimeWithMillis(-123456789101L))); @@ -243,7 +242,7 @@ public void testConversionToTime() { assertEquals(time(-123465600000L), conversion.convert(asDateOnly(-123456789101L))); } { - Converter conversion = converterFor(getDateTimeRandomNanos(), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals(time(77589101L), conversion.convert(DateUtils.asDateTimeWithMillis(123456789101L))); assertEquals(time(8810899L), conversion.convert(DateUtils.asDateTimeWithMillis(-123456789101L))); @@ -368,7 +367,7 @@ public void testConversionToFloat() { assertEquals(8810899.0f, (float) conversion.convert(asTimeOnly(-123456789101L)), 0); } { - Converter conversion = converterFor(getDateTimeRandomNanos(), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals(1.23456789101E11f, (float) conversion.convert(DateUtils.asDateTimeWithMillis(123456789101L)), 0); assertEquals(-1.23456789101E11f, (float) conversion.convert(DateUtils.asDateTimeWithMillis(-123456789101L)), 0); @@ -422,7 +421,7 @@ public void testConversionToDouble() { assertEquals(8810899.0, (double) conversion.convert(asTimeOnly(-123456789101L)), 0); } { - Converter conversion = converterFor(getDateTimeRandomNanos(), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals(1.23456789101E11, (double) conversion.convert(DateUtils.asDateTimeWithMillis(123456789101L)), 0); assertEquals(-1.23456789101E11, (double) conversion.convert(DateUtils.asDateTimeWithMillis(-123456789101L)), 0); @@ -485,7 +484,7 @@ public void testConversionToBoolean() { assertEquals(false, conversion.convert(asTimeOnly(0L))); } { - Converter conversion = converterFor(getDateTimeRandomNanos(), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals(true, conversion.convert(DateUtils.asDateTimeWithMillis(123456789101L))); assertEquals(true, conversion.convert(DateUtils.asDateTimeWithMillis(-123456789101L))); @@ -546,7 +545,7 @@ public void testConversionToInt() { assertEquals(25975807, conversion.convert(asTimeOnly(Long.MAX_VALUE))); } { - Converter conversion = converterFor(getDateTimeRandomNanos(), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals(12345678, conversion.convert(DateUtils.asDateTimeWithMillis(12345678L))); assertEquals(223456789, conversion.convert(DateUtils.asDateTimeWithMillis(223456789L))); @@ -589,7 +588,7 @@ public void testConversionToShort() { assertEquals("[37056789] out of [short] range", e2.getMessage()); } { - Converter conversion = converterFor(getDateTimeRandomNanos(), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals((short) 12345, conversion.convert(DateUtils.asDateTimeWithMillis(12345L))); assertEquals((short) -12345, conversion.convert(DateUtils.asDateTimeWithMillis(-12345L))); @@ -631,7 +630,7 @@ public void testConversionToByte() { assertEquals("[37056789] out of [byte] range", e2.getMessage()); } { - Converter conversion = converterFor(getDateTimeRandomNanos(), to); + Converter conversion = converterFor(DATETIME, to); assertNull(conversion.convert(null)); assertEquals((byte) 123, conversion.convert(DateUtils.asDateTimeWithMillis(123L))); assertEquals((byte) -123, conversion.convert(DateUtils.asDateTimeWithMillis(-123L))); @@ -684,15 +683,12 @@ public void testCommonType() { assertEquals(INTERVAL_HOUR_TO_MINUTE, commonType(INTEGER, INTERVAL_HOUR_TO_MINUTE)); // dates/datetimes and intervals - assertEquals(DATETIME, commonType(DATETIME, DATETIME_NANOS)); - assertEquals(DATETIME, commonType(DATETIME_NANOS, DATETIME)); - - assertEquals(DATETIME, commonType(DATE, getDateTimeRandomNanos())); - assertEquals(DATETIME, commonType(getDateTimeRandomNanos(), DATE)); - assertEquals(DATETIME, commonType(TIME, getDateTimeRandomNanos())); - assertEquals(DATETIME, commonType(getDateTimeRandomNanos(), TIME)); - assertEquals(DATETIME, commonType(getDateTimeRandomNanos(), randomInterval())); - assertEquals(DATETIME, commonType(randomInterval(), getDateTimeRandomNanos())); + assertEquals(DATETIME, commonType(DATE, DATETIME)); + assertEquals(DATETIME, commonType(DATETIME, DATE)); + assertEquals(DATETIME, commonType(TIME, DATETIME)); + assertEquals(DATETIME, commonType(DATETIME, TIME)); + assertEquals(DATETIME, commonType(DATETIME, randomInterval())); + assertEquals(DATETIME, commonType(randomInterval(), DATETIME)); assertEquals(DATETIME, commonType(DATE, TIME)); assertEquals(DATETIME, commonType(TIME, DATE)); assertEquals(DATE, commonType(DATE, INTERVAL_YEAR)); @@ -739,10 +735,6 @@ private DataType randomInterval() { .collect(toList())); } - private DataType getDateTimeRandomNanos() { - return randomFrom(DATETIME, DATETIME_NANOS); - } - static ZonedDateTime dateTime(long millisSinceEpoch) { return DateUtils.asDateTimeWithMillis(millisSinceEpoch); } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/types/SqlTypesTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/types/SqlTypesTests.java index 52b79cfd839ba..acb21ccdab9f1 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/types/SqlTypesTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/types/SqlTypesTests.java @@ -18,7 +18,6 @@ import java.util.Map; import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME; -import static org.elasticsearch.xpack.ql.type.DataTypes.DATETIME_NANOS; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; @@ -71,7 +70,7 @@ public void testDateNanosField() { assertThat(mapping.size(), is(1)); EsField field = mapping.get("date_nanos"); - assertThat(field.getDataType(), is(DATETIME_NANOS)); + assertThat(field.getDataType(), is(DATETIME)); assertThat(field.isAggregatable(), is(true)); assertThat(SqlDataTypes.defaultPrecision(field.getDataType()), is(9)); }