Skip to content

Commit 79e600a

Browse files
[ES|QL] Date nanos implicit casting in union types option #2 (#127797)
* implicit casting for union typed fields mixed with datetime and date_nanos
1 parent 6f27ac8 commit 79e600a

File tree

15 files changed

+1419
-31
lines changed

15 files changed

+1419
-31
lines changed

docs/changelog/127797.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 127797
2+
summary: "Date nanos implicit casting in union types option #2"
3+
area: ES|QL
4+
type: enhancement
5+
issues:
6+
- 110009

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/util/DateUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class DateUtils {
6565
.appendValue(MINUTE_OF_HOUR, 2)
6666
.appendLiteral(':')
6767
.appendValue(SECOND_OF_MINUTE, 2)
68-
.appendFraction(NANO_OF_SECOND, 3, 9, true)
68+
.appendFraction(NANO_OF_SECOND, 0, 9, true)
6969
.appendOffsetId()
7070
.toFormatter(Locale.ROOT);
7171

x-pack/plugin/esql/qa/server/single-node/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dependencies {
1313
javaRestTestImplementation project(xpackModule('esql:qa:testFixtures'))
1414
javaRestTestImplementation project(xpackModule('esql:qa:server'))
1515
javaRestTestImplementation project(xpackModule('esql:tools'))
16+
javaRestTestImplementation project(xpackModule('esql'))
1617
yamlRestTestImplementation project(xpackModule('esql:qa:server'))
1718

1819
javaRestTestImplementation('org.apache.arrow:arrow-vector:16.1.0')

x-pack/plugin/esql/qa/server/single-node/src/javaRestTest/java/org/elasticsearch/xpack/esql/qa/single_node/RestEsqlIT.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
import static org.elasticsearch.test.ListMatcher.matchesList;
5656
import static org.elasticsearch.test.MapMatcher.assertMap;
5757
import static org.elasticsearch.test.MapMatcher.matchesMap;
58+
import static org.elasticsearch.xpack.esql.action.EsqlCapabilities.Cap.IMPLICIT_CASTING_DATE_AND_DATE_NANOS;
59+
import static org.elasticsearch.xpack.esql.core.type.DataType.isMillisOrNanos;
5860
import static org.elasticsearch.xpack.esql.qa.rest.RestEsqlTestCase.Mode.SYNC;
5961
import static org.elasticsearch.xpack.esql.tools.ProfileParser.parseProfile;
6062
import static org.elasticsearch.xpack.esql.tools.ProfileParser.readProfileFromResponse;
@@ -736,19 +738,26 @@ public void testSuggestedCast() throws IOException {
736738
Map<String, Object> results = entityAsMap(resp);
737739
List<?> columns = (List<?>) results.get("columns");
738740
DataType suggestedCast = DataType.suggestedCast(Set.of(listOfTypes.get(i), listOfTypes.get(j)));
739-
assertThat(
740-
columns,
741-
equalTo(
742-
List.of(
743-
Map.ofEntries(
744-
Map.entry("name", "my_field"),
745-
Map.entry("type", "unsupported"),
746-
Map.entry("original_types", List.of(listOfTypes.get(i).typeName(), listOfTypes.get(j).typeName())),
747-
Map.entry("suggested_cast", suggestedCast.typeName())
741+
if (IMPLICIT_CASTING_DATE_AND_DATE_NANOS.isEnabled()
742+
&& isMillisOrNanos(listOfTypes.get(i))
743+
&& isMillisOrNanos(listOfTypes.get(j))) {
744+
// datetime and date_nanos are casted to date_nanos implicitly
745+
assertThat(columns, equalTo(List.of(Map.ofEntries(Map.entry("name", "my_field"), Map.entry("type", "date_nanos")))));
746+
} else {
747+
assertThat(
748+
columns,
749+
equalTo(
750+
List.of(
751+
Map.ofEntries(
752+
Map.entry("name", "my_field"),
753+
Map.entry("type", "unsupported"),
754+
Map.entry("original_types", List.of(listOfTypes.get(i).typeName(), listOfTypes.get(j).typeName())),
755+
Map.entry("suggested_cast", suggestedCast.typeName())
756+
)
748757
)
749758
)
750-
)
751-
);
759+
);
760+
}
752761

753762
String castedQuery = String.format(
754763
Locale.ROOT,

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/CsvTestsDataLoader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public class CsvTestsDataLoader {
131131
);
132132
private static final TestDataset AIRPORTS_WEB = new TestDataset("airports_web");
133133
private static final TestDataset DATE_NANOS = new TestDataset("date_nanos");
134+
private static final TestDataset DATE_NANOS_UNION_TYPES = new TestDataset("date_nanos_union_types");
134135
private static final TestDataset COUNTRIES_BBOX = new TestDataset("countries_bbox");
135136
private static final TestDataset COUNTRIES_BBOX_WEB = new TestDataset("countries_bbox_web");
136137
private static final TestDataset AIRPORT_CITY_BOUNDARIES = new TestDataset("airport_city_boundaries");
@@ -201,6 +202,7 @@ public class CsvTestsDataLoader {
201202
Map.entry(MULTIVALUE_GEOMETRIES.indexName, MULTIVALUE_GEOMETRIES),
202203
Map.entry(MULTIVALUE_POINTS.indexName, MULTIVALUE_POINTS),
203204
Map.entry(DATE_NANOS.indexName, DATE_NANOS),
205+
Map.entry(DATE_NANOS_UNION_TYPES.indexName, DATE_NANOS_UNION_TYPES),
204206
Map.entry(K8S.indexName, K8S),
205207
Map.entry(DISTANCES.indexName, DISTANCES),
206208
Map.entry(ADDRESSES.indexName, ADDRESSES),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
millis:date_nanos,nanos:date,num:long
2+
2023-10-23T13:55:01.543123456Z,2023-10-23T13:55:01.543Z,1698069301543123456
3+
2023-10-23T13:55:01.543123456Z,2023-10-23T13:55:01.543Z,1698069301543123456
4+
2023-10-23T13:53:55.832987654Z,2023-10-23T13:53:55.832Z,1698069235832987654
5+
2023-10-23T13:52:55.015787878Z,2023-10-23T13:52:55.015Z,1698069175015787878
6+
2023-10-23T13:51:54.732102837Z,2023-10-23T13:51:54.732Z,1698069114732102837
7+
2023-10-23T13:33:34.937193000Z,2023-10-23T13:33:34.937Z,1698068014937193000
8+
2023-10-23T12:27:28.948000000Z,2023-10-23T12:27:28.948Z,1698064048948000000
9+
2023-10-23T12:15:03.360103847Z,2023-10-23T12:15:03.360Z,1698063303360103847
10+
2023-10-23T12:15:03.360103847Z,2023-10-23T12:15:03.360Z,1698063303360103847
11+
1999-10-23T12:15:03.360103847Z,[2023-03-23T12:15:03.360Z, 2023-02-23T13:33:34.937Z, 2023-01-23T13:55:01.543Z], 0
12+
1999-10-22T12:15:03.360103847Z,[2023-03-23T12:15:03.360Z, 2023-03-23T12:15:03.360Z, 2023-03-23T12:15:03.360Z], 0
13+
2023-10-23T12:15:03.360103847Z,1923-10-23T12:15:03.360Z,1698063303360103847

x-pack/plugin/esql/qa/testFixtures/src/main/resources/data/employees_incompatible.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
birth_date:date_nanos ,emp_no:long,first_name:text,gender:text,hire_date:date_nanos,languages:byte,languages.long:long,languages.short:short,languages.byte:byte,last_name:text,salary:long,height:float,height.double:double,height.scaled_float:scaled_float,height.half_float:half_float,still_hired:keyword,avg_worked_seconds:unsigned_long,job_positions:text,is_rehired:keyword,salary_change:float,salary_change.int:integer,salary_change.long:long,salary_change.keyword:keyword
1+
birth_date:date ,emp_no:long,first_name:text,gender:text,hire_date:date_nanos,languages:byte,languages.long:long,languages.short:short,languages.byte:byte,last_name:text,salary:long,height:float,height.double:double,height.scaled_float:scaled_float,height.half_float:half_float,still_hired:keyword,avg_worked_seconds:unsigned_long,job_positions:text,is_rehired:keyword,salary_change:float,salary_change.int:integer,salary_change.long:long,salary_change.keyword:keyword
22
1953-09-02T00:00:00Z,10001,Georgi ,M,1986-06-26T00:00:00Z,2,2,2,2,Facello ,57305,2.03,2.03,2.03,2.03,true ,268728049,[Senior Python Developer,Accountant],[false,true],[1.19],[1],[1],[1.19]
33
1964-06-02T00:00:00Z,10002,Bezalel ,F,1985-11-21T00:00:00Z,5,5,5,5,Simmel ,56371,2.08,2.08,2.08,2.08,true ,328922887,[Senior Team Lead],[false,false],[-7.23,11.17],[-7,11],[-7,11],[-7.23,11.17]
44
1959-12-03T00:00:00Z,10003,Parto ,M,1986-08-28T00:00:00Z,4,4,4,4,Bamford ,61805,1.83,1.83,1.83,1.83,false,200296405,[],[],[14.68,12.82],[14,12],[14,12],[14.68,12.82]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"properties": {
3+
"millis": {
4+
"type": "date_nanos"
5+
},
6+
"nanos": {
7+
"type": "date"
8+
},
9+
"num": {
10+
"type": "long"
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)