/
ResponseValueUtils.java
214 lines (200 loc) · 10.8 KB
/
ResponseValueUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.esql.action;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.Iterators;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.compute.data.Block;
import org.elasticsearch.compute.data.BlockFactory;
import org.elasticsearch.compute.data.BooleanBlock;
import org.elasticsearch.compute.data.BytesRefBlock;
import org.elasticsearch.compute.data.DoubleBlock;
import org.elasticsearch.compute.data.IntBlock;
import org.elasticsearch.compute.data.LongBlock;
import org.elasticsearch.compute.data.Page;
import org.elasticsearch.compute.lucene.UnsupportedValueSource;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.json.JsonXContent;
import org.elasticsearch.xpack.core.esql.action.ColumnInfo;
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
import org.elasticsearch.xpack.esql.planner.PlannerUtils;
import org.elasticsearch.xpack.esql.type.EsqlDataTypes;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.dateTimeToLong;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.dateTimeToString;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.ipToString;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.longToUnsignedLong;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.spatialToString;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.stringToIP;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.stringToSpatial;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.stringToVersion;
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.versionToString;
import static org.elasticsearch.xpack.ql.util.NumericUtils.unsignedLongAsNumber;
/**
* Collection of static utility methods for helping transform response data between pages and values.
*/
public final class ResponseValueUtils {
/**
* Returns an iterator of iterators over the values in the given pages. There is one iterator
* for each block.
*/
public static Iterator<Iterator<Object>> pagesToValues(List<String> dataTypes, List<Page> pages) {
BytesRef scratch = new BytesRef();
return Iterators.flatMap(
pages.iterator(),
page -> Iterators.forRange(
0,
page.getPositionCount(),
pos -> Iterators.forRange(0, page.getBlockCount(), b -> valueAtPosition(page.getBlock(b), pos, dataTypes.get(b), scratch))
)
);
}
/** Returns an iterable of iterables over the values in the given pages. There is one iterables for each row. */
static Iterable<Iterable<Object>> valuesForRowsInPages(List<String> dataTypes, List<Page> pages) {
BytesRef scratch = new BytesRef();
return () -> Iterators.flatMap(pages.iterator(), page -> valuesForRowsInPage(dataTypes, page, scratch));
}
/** Returns an iterable of iterables over the values in the given page. There is one iterables for each row. */
static Iterator<Iterable<Object>> valuesForRowsInPage(List<String> dataTypes, Page page, BytesRef scratch) {
return Iterators.forRange(0, page.getPositionCount(), position -> valuesForRow(dataTypes, page, position, scratch));
}
/** Returns an iterable over the values in the given row in a page. */
static Iterable<Object> valuesForRow(List<String> dataTypes, Page page, int position, BytesRef scratch) {
return () -> Iterators.forRange(
0,
page.getBlockCount(),
blockIdx -> valueAtPosition(page.getBlock(blockIdx), position, dataTypes.get(blockIdx), scratch)
);
}
/** Returns an iterator of values for the given column. */
static Iterator<Object> valuesForColumn(int columnIndex, String dataType, List<Page> pages) {
BytesRef scratch = new BytesRef();
return Iterators.flatMap(
pages.iterator(),
page -> Iterators.forRange(
0,
page.getPositionCount(),
pos -> valueAtPosition(page.getBlock(columnIndex), pos, dataType, scratch)
)
);
}
/** Returns the value that the position and with the given data type, in the block. */
static Object valueAtPosition(Block block, int position, String dataType, BytesRef scratch) {
if (block.isNull(position)) {
return null;
}
int count = block.getValueCount(position);
int start = block.getFirstValueIndex(position);
if (count == 1) {
return valueAt(dataType, block, start, scratch);
}
List<Object> values = new ArrayList<>(count);
int end = count + start;
for (int i = start; i < end; i++) {
values.add(valueAt(dataType, block, i, scratch));
}
return values;
}
private static Object valueAt(String dataType, Block block, int offset, BytesRef scratch) {
return switch (dataType) {
case "unsigned_long" -> unsignedLongAsNumber(((LongBlock) block).getLong(offset));
case "long", "counter_long" -> ((LongBlock) block).getLong(offset);
case "integer", "counter_integer" -> ((IntBlock) block).getInt(offset);
case "double", "counter_double" -> ((DoubleBlock) block).getDouble(offset);
case "keyword", "text" -> ((BytesRefBlock) block).getBytesRef(offset, scratch).utf8ToString();
case "ip" -> {
BytesRef val = ((BytesRefBlock) block).getBytesRef(offset, scratch);
yield ipToString(val);
}
case "date" -> {
long longVal = ((LongBlock) block).getLong(offset);
yield dateTimeToString(longVal);
}
case "boolean" -> ((BooleanBlock) block).getBoolean(offset);
case "version" -> versionToString(((BytesRefBlock) block).getBytesRef(offset, scratch));
case "geo_point", "geo_shape", "cartesian_point", "cartesian_shape" -> spatialToString(
((BytesRefBlock) block).getBytesRef(offset, scratch)
);
case "unsupported" -> UnsupportedValueSource.UNSUPPORTED_OUTPUT;
case "_source" -> {
BytesRef val = ((BytesRefBlock) block).getBytesRef(offset, scratch);
try {
try (XContentParser parser = XContentHelper.createParser(XContentParserConfiguration.EMPTY, new BytesArray(val))) {
parser.nextToken();
yield parser.mapOrdered();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
default -> throw EsqlIllegalArgumentException.illegalDataType(dataType);
};
}
/**
* Converts a list of values to Pages so that we can parse from xcontent. It's not
* super efficient, but it doesn't really have to be.
*/
static Page valuesToPage(BlockFactory blockFactory, List<ColumnInfo> columns, List<List<Object>> values) {
List<String> dataTypes = columns.stream().map(ColumnInfo::type).toList();
List<Block.Builder> results = dataTypes.stream()
.map(c -> PlannerUtils.toElementType(EsqlDataTypes.fromName(c)).newBlockBuilder(values.size(), blockFactory))
.toList();
for (List<Object> row : values) {
for (int c = 0; c < row.size(); c++) {
var builder = results.get(c);
var value = row.get(c);
switch (dataTypes.get(c)) {
case "unsigned_long" -> ((LongBlock.Builder) builder).appendLong(
longToUnsignedLong(((Number) value).longValue(), true)
);
case "long", "counter_long" -> ((LongBlock.Builder) builder).appendLong(((Number) value).longValue());
case "integer", "counter_integer" -> ((IntBlock.Builder) builder).appendInt(((Number) value).intValue());
case "double", "counter_double" -> ((DoubleBlock.Builder) builder).appendDouble(((Number) value).doubleValue());
case "keyword", "text", "unsupported" -> ((BytesRefBlock.Builder) builder).appendBytesRef(
new BytesRef(value.toString())
);
case "ip" -> ((BytesRefBlock.Builder) builder).appendBytesRef(stringToIP(value.toString()));
case "date" -> {
long longVal = dateTimeToLong(value.toString());
((LongBlock.Builder) builder).appendLong(longVal);
}
case "boolean" -> ((BooleanBlock.Builder) builder).appendBoolean(((Boolean) value));
case "null" -> builder.appendNull();
case "version" -> ((BytesRefBlock.Builder) builder).appendBytesRef(stringToVersion(new BytesRef(value.toString())));
case "_source" -> {
@SuppressWarnings("unchecked")
Map<String, ?> o = (Map<String, ?>) value;
try {
try (XContentBuilder sourceBuilder = JsonXContent.contentBuilder()) {
sourceBuilder.map(o);
((BytesRefBlock.Builder) builder).appendBytesRef(BytesReference.bytes(sourceBuilder).toBytesRef());
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
case "geo_point", "geo_shape", "cartesian_point", "cartesian_shape" -> {
// This just converts WKT to WKB, so does not need CRS knowledge, we could merge GEO and CARTESIAN here
BytesRef wkb = stringToSpatial(value.toString());
((BytesRefBlock.Builder) builder).appendBytesRef(wkb);
}
default -> throw EsqlIllegalArgumentException.illegalDataType(dataTypes.get(c));
}
}
}
return new Page(results.stream().map(Block.Builder::build).toArray(Block[]::new));
}
}