Skip to content

Commit 84b4c59

Browse files
Backporting two union-types fixes to 8.15 (from 111932 and 112610) (elastic#112821)
* Fix union-types where one index is missing the field (elastic#111932) * Fix union-types where one index is missing the field When none of the indexes has the field, a validation error is correctly thrown, and when all indexes have the field, union-types works as normal. But when some indexes have the field and some do not, we were getting and internal error. We treat this case similarly to when some documents are missing the field, in which case `null` values are produced. So now a multi-index query where some indexes are missing the field will produce nulls for the documents coming from those indexes. * Update docs/changelog/111932.yaml * Added capability for this fix (missing-field) * Support widening of numeric types in union-types (elastic#112610) * Support widening of numeric types in union-types Only two lines of this PR are the actual fix. All the rest is updating the CSV-spec testing infrastructure to make it easier to test this, and adding the tests. The refactoring involve some cleanup and simplifications also. This update allows us to add alternative mappings of existing data files without copying the files and changing the header line. Some of the existing union-types test files were deleted as a result, which is a step in the right direction. * Update docs/changelog/112610.yaml * Link capability to PR
1 parent dd4f4d5 commit 84b4c59

File tree

15 files changed

+288
-166
lines changed

15 files changed

+288
-166
lines changed

docs/changelog/111932.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 111932
2+
summary: Fix union-types where one index is missing the field
3+
area: ES|QL
4+
type: bug
5+
issues:
6+
- 111912

docs/changelog/112610.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 112610
2+
summary: Support widening of numeric types in union-types
3+
area: ES|QL
4+
type: bug
5+
issues:
6+
- 111277

x-pack/plugin/esql/qa/testFixtures/build.gradle

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ apply plugin: 'elasticsearch.java'
22
apply plugin: org.elasticsearch.gradle.dependencies.CompileOnlyResolvePlugin
33

44
dependencies {
5-
implementation project(':x-pack:plugin:esql:compute')
6-
implementation project(':x-pack:plugin:esql')
7-
compileOnly project(path: xpackModule('core'))
8-
implementation project(":libs:elasticsearch-x-content")
9-
implementation project(':client:rest')
10-
implementation project(':libs:elasticsearch-logging')
11-
implementation project(':test:framework')
12-
api(testArtifact(project(xpackModule('esql-core'))))
13-
implementation project(':server')
14-
implementation "net.sf.supercsv:super-csv:${versions.supercsv}"
5+
implementation project(':x-pack:plugin:esql:compute')
6+
implementation project(':x-pack:plugin:esql')
7+
compileOnly project(path: xpackModule('core'))
8+
implementation project(":libs:elasticsearch-x-content")
9+
implementation project(':client:rest')
10+
implementation project(':libs:elasticsearch-logging')
11+
implementation project(':test:framework')
12+
api(testArtifact(project(xpackModule('esql-core'))))
13+
implementation project(':server')
14+
implementation "net.sf.supercsv:super-csv:${versions.supercsv}"
15+
implementation "com.fasterxml.jackson.core:jackson-core:${versions.jackson}"
16+
implementation "com.fasterxml.jackson.core:jackson-databind:${versions.jackson}"
1517
}
1618

1719
/**

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public static Tuple<Version, Version> skipVersionRange(String testName) {
116116
return null;
117117
}
118118

119-
public static Tuple<Page, List<String>> loadPageFromCsv(URL source) throws Exception {
119+
public static Tuple<Page, List<String>> loadPageFromCsv(URL source, Map<String, String> typeMapping) throws Exception {
120120

121121
record CsvColumn(String name, Type type, BuilderWrapper builderWrapper) implements Releasable {
122122
void append(String stringValue) {
@@ -162,21 +162,16 @@ public void close() {
162162
if (columns == null) {
163163
columns = new CsvColumn[entries.length];
164164
for (int i = 0; i < entries.length; i++) {
165-
int split = entries[i].indexOf(':');
166-
String name, typeName;
165+
String[] header = entries[i].split(":");
166+
String name = header[0].trim();
167+
String typeName = (typeMapping != null && typeMapping.containsKey(name)) ? typeMapping.get(name)
168+
: header.length > 1 ? header[1].trim()
169+
: null;
167170

168-
if (split < 0) {
171+
if (typeName == null || typeName.isEmpty()) {
169172
throw new IllegalArgumentException(
170173
"A type is always expected in the schema definition; found " + entries[i]
171174
);
172-
} else {
173-
name = entries[i].substring(0, split).trim();
174-
typeName = entries[i].substring(split + 1).trim();
175-
if (typeName.length() == 0) {
176-
throw new IllegalArgumentException(
177-
"A type is always expected in the schema definition; found " + entries[i]
178-
);
179-
}
180175
}
181176
Type type = Type.asType(typeName);
182177
if (type == null) {

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

Lines changed: 100 additions & 109 deletions
Large diffs are not rendered by default.

x-pack/plugin/esql/qa/testFixtures/src/main/resources/mapping-sample_data_str.json renamed to x-pack/plugin/esql/qa/testFixtures/src/main/resources/mapping-missing_ip_sample_data.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
"@timestamp": {
44
"type": "date"
55
},
6-
"client_ip": {
7-
"type": "keyword"
8-
},
96
"event_duration": {
107
"type": "long"
118
},

x-pack/plugin/esql/qa/testFixtures/src/main/resources/mapping-sample_data_ts_long.json

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@timestamp:date,event_duration:long,message:keyword
2+
2023-10-23T13:55:01.543Z,1756467,Connected to 10.1.0.1
3+
2023-10-23T13:53:55.832Z,5033755,Connection error
4+
2023-10-23T13:52:55.015Z,8268153,Connection error
5+
2023-10-23T13:51:54.732Z,725448,Connection error
6+
2023-10-23T13:33:34.937Z,1232382,Disconnected
7+
2023-10-23T12:27:28.948Z,2764889,Connected to 10.1.0.2
8+
2023-10-23T12:15:03.360Z,3450233,Connected to 10.1.0.3

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

Lines changed: 0 additions & 8 deletions
This file was deleted.

x-pack/plugin/esql/qa/testFixtures/src/main/resources/union_types.csv-spec

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,74 @@ count:long | message:keyword
384384
2 | Connected to 10.1.0.3
385385
;
386386

387+
multiIndexMissingIpToString
388+
required_capability: union_types
389+
required_capability: union_types_missing_field
390+
391+
FROM sample_data, sample_data_str, missing_ip_sample_data METADATA _index
392+
| EVAL client_ip = TO_STRING(client_ip)
393+
| KEEP _index, @timestamp, client_ip, event_duration, message
394+
| SORT _index ASC, @timestamp DESC
395+
;
396+
397+
_index:keyword | @timestamp:date | client_ip:keyword | event_duration:long | message:keyword
398+
missing_ip_sample_data | 2023-10-23T13:55:01.543Z | null | 1756467 | Connected to 10.1.0.1
399+
missing_ip_sample_data | 2023-10-23T13:53:55.832Z | null | 5033755 | Connection error
400+
missing_ip_sample_data | 2023-10-23T13:52:55.015Z | null | 8268153 | Connection error
401+
missing_ip_sample_data | 2023-10-23T13:51:54.732Z | null | 725448 | Connection error
402+
missing_ip_sample_data | 2023-10-23T13:33:34.937Z | null | 1232382 | Disconnected
403+
missing_ip_sample_data | 2023-10-23T12:27:28.948Z | null | 2764889 | Connected to 10.1.0.2
404+
missing_ip_sample_data | 2023-10-23T12:15:03.360Z | null | 3450233 | Connected to 10.1.0.3
405+
sample_data | 2023-10-23T13:55:01.543Z | 172.21.3.15 | 1756467 | Connected to 10.1.0.1
406+
sample_data | 2023-10-23T13:53:55.832Z | 172.21.3.15 | 5033755 | Connection error
407+
sample_data | 2023-10-23T13:52:55.015Z | 172.21.3.15 | 8268153 | Connection error
408+
sample_data | 2023-10-23T13:51:54.732Z | 172.21.3.15 | 725448 | Connection error
409+
sample_data | 2023-10-23T13:33:34.937Z | 172.21.0.5 | 1232382 | Disconnected
410+
sample_data | 2023-10-23T12:27:28.948Z | 172.21.2.113 | 2764889 | Connected to 10.1.0.2
411+
sample_data | 2023-10-23T12:15:03.360Z | 172.21.2.162 | 3450233 | Connected to 10.1.0.3
412+
sample_data_str | 2023-10-23T13:55:01.543Z | 172.21.3.15 | 1756467 | Connected to 10.1.0.1
413+
sample_data_str | 2023-10-23T13:53:55.832Z | 172.21.3.15 | 5033755 | Connection error
414+
sample_data_str | 2023-10-23T13:52:55.015Z | 172.21.3.15 | 8268153 | Connection error
415+
sample_data_str | 2023-10-23T13:51:54.732Z | 172.21.3.15 | 725448 | Connection error
416+
sample_data_str | 2023-10-23T13:33:34.937Z | 172.21.0.5 | 1232382 | Disconnected
417+
sample_data_str | 2023-10-23T12:27:28.948Z | 172.21.2.113 | 2764889 | Connected to 10.1.0.2
418+
sample_data_str | 2023-10-23T12:15:03.360Z | 172.21.2.162 | 3450233 | Connected to 10.1.0.3
419+
;
420+
421+
multiIndexMissingIpToIp
422+
required_capability: union_types
423+
required_capability: union_types_missing_field
424+
425+
FROM sample_data, sample_data_str, missing_ip_sample_data METADATA _index
426+
| EVAL client_ip = TO_IP(client_ip)
427+
| KEEP _index, @timestamp, client_ip, event_duration, message
428+
| SORT _index ASC, @timestamp DESC
429+
;
430+
431+
_index:keyword | @timestamp:date | client_ip:ip | event_duration:long | message:keyword
432+
missing_ip_sample_data | 2023-10-23T13:55:01.543Z | null | 1756467 | Connected to 10.1.0.1
433+
missing_ip_sample_data | 2023-10-23T13:53:55.832Z | null | 5033755 | Connection error
434+
missing_ip_sample_data | 2023-10-23T13:52:55.015Z | null | 8268153 | Connection error
435+
missing_ip_sample_data | 2023-10-23T13:51:54.732Z | null | 725448 | Connection error
436+
missing_ip_sample_data | 2023-10-23T13:33:34.937Z | null | 1232382 | Disconnected
437+
missing_ip_sample_data | 2023-10-23T12:27:28.948Z | null | 2764889 | Connected to 10.1.0.2
438+
missing_ip_sample_data | 2023-10-23T12:15:03.360Z | null | 3450233 | Connected to 10.1.0.3
439+
sample_data | 2023-10-23T13:55:01.543Z | 172.21.3.15 | 1756467 | Connected to 10.1.0.1
440+
sample_data | 2023-10-23T13:53:55.832Z | 172.21.3.15 | 5033755 | Connection error
441+
sample_data | 2023-10-23T13:52:55.015Z | 172.21.3.15 | 8268153 | Connection error
442+
sample_data | 2023-10-23T13:51:54.732Z | 172.21.3.15 | 725448 | Connection error
443+
sample_data | 2023-10-23T13:33:34.937Z | 172.21.0.5 | 1232382 | Disconnected
444+
sample_data | 2023-10-23T12:27:28.948Z | 172.21.2.113 | 2764889 | Connected to 10.1.0.2
445+
sample_data | 2023-10-23T12:15:03.360Z | 172.21.2.162 | 3450233 | Connected to 10.1.0.3
446+
sample_data_str | 2023-10-23T13:55:01.543Z | 172.21.3.15 | 1756467 | Connected to 10.1.0.1
447+
sample_data_str | 2023-10-23T13:53:55.832Z | 172.21.3.15 | 5033755 | Connection error
448+
sample_data_str | 2023-10-23T13:52:55.015Z | 172.21.3.15 | 8268153 | Connection error
449+
sample_data_str | 2023-10-23T13:51:54.732Z | 172.21.3.15 | 725448 | Connection error
450+
sample_data_str | 2023-10-23T13:33:34.937Z | 172.21.0.5 | 1232382 | Disconnected
451+
sample_data_str | 2023-10-23T12:27:28.948Z | 172.21.2.113 | 2764889 | Connected to 10.1.0.2
452+
sample_data_str | 2023-10-23T12:15:03.360Z | 172.21.2.162 | 3450233 | Connected to 10.1.0.3
453+
;
454+
387455
multiIndexTsLong
388456
required_capability: union_types
389457
required_capability: metadata_fields
@@ -1191,3 +1259,54 @@ FROM sample_data, sample_data_ts_long
11911259
null | 172.21.0.5 | 1232382 | Disconnected | Disconnected
11921260
null | 172.21.0.5 | 1232382 | Disconnected | Disconnected
11931261
;
1262+
1263+
shortIntegerWidening
1264+
required_capability: union_types
1265+
required_capability: metadata_fields
1266+
required_capability: casting_operator
1267+
required_capability: union_types_numeric_widening
1268+
1269+
FROM apps, apps_short METADATA _index
1270+
| EVAL id = id::integer
1271+
| KEEP _index, id, version, name
1272+
| WHERE name == "aaaaa" OR name == "hhhhh"
1273+
| SORT _index ASC, id ASC
1274+
;
1275+
1276+
_index:keyword | id:integer | version:version | name:keyword
1277+
apps | 1 | 1 | aaaaa
1278+
apps | 8 | 1.2.3.4 | hhhhh
1279+
apps | 12 | 1.2.3.4 | aaaaa
1280+
apps_short | 1 | 1 | aaaaa
1281+
apps_short | 8 | 1.2.3.4 | hhhhh
1282+
apps_short | 12 | 1.2.3.4 | aaaaa
1283+
;
1284+
1285+
shortIntegerWideningStats
1286+
required_capability: union_types
1287+
required_capability: casting_operator
1288+
required_capability: union_types_numeric_widening
1289+
1290+
FROM apps, apps_short
1291+
| EVAL id = id::integer
1292+
| STATS count=count() BY name, id
1293+
| KEEP id, name, count
1294+
| SORT id ASC, name ASC
1295+
;
1296+
1297+
id:integer | name:keyword | count:long
1298+
1 | aaaaa | 2
1299+
2 | bbbbb | 2
1300+
3 | ccccc | 2
1301+
4 | ddddd | 2
1302+
5 | eeeee | 2
1303+
6 | fffff | 2
1304+
7 | ggggg | 2
1305+
8 | hhhhh | 2
1306+
9 | iiiii | 2
1307+
10 | jjjjj | 2
1308+
11 | kkkkk | 2
1309+
12 | aaaaa | 2
1310+
13 | lllll | 2
1311+
14 | mmmmm | 2
1312+
;

0 commit comments

Comments
 (0)