Skip to content

Commit 67e85ba

Browse files
ebyhrwendigo
authored andcommitted
Convert IndexMetadata to record in Elasticsearch
1 parent f0f1898 commit 67e85ba

File tree

3 files changed

+39
-107
lines changed

3 files changed

+39
-107
lines changed

plugin/trino-elasticsearch/src/main/java/io/trino/plugin/elasticsearch/ElasticsearchMetadata.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public ElasticsearchTableHandle getTableHandle(ConnectorSession session, SchemaT
163163
query = Optional.of(parts[1]);
164164
}
165165

166-
if (client.indexExists(table) && !client.getIndexMetadata(table).getSchema().getFields().isEmpty()) {
166+
if (client.indexExists(table) && !client.getIndexMetadata(table).schema().fields().isEmpty()) {
167167
return new ElasticsearchTableHandle(SCAN, schemaName, table, query);
168168
}
169169
}
@@ -205,12 +205,12 @@ private InternalTableMetadata makeInternalTableMetadata(String schema, String ta
205205

206206
private List<IndexMetadata.Field> getColumnFields(IndexMetadata metadata)
207207
{
208-
Map<String, Long> counts = metadata.getSchema()
209-
.getFields().stream()
210-
.collect(Collectors.groupingBy(f -> f.getName().toLowerCase(ENGLISH), Collectors.counting()));
208+
Map<String, Long> counts = metadata.schema()
209+
.fields().stream()
210+
.collect(Collectors.groupingBy(f -> f.name().toLowerCase(ENGLISH), Collectors.counting()));
211211

212-
return metadata.getSchema().getFields().stream()
213-
.filter(field -> toTrino(field) != null && counts.get(field.getName().toLowerCase(ENGLISH)) <= 1)
212+
return metadata.schema().fields().stream()
213+
.filter(field -> toTrino(field) != null && counts.get(field.name().toLowerCase(ENGLISH)) <= 1)
214214
.collect(toImmutableList());
215215
}
216216

@@ -224,7 +224,7 @@ private List<ColumnMetadata> makeColumnMetadata(List<IndexMetadata.Field> fields
224224

225225
for (IndexMetadata.Field field : fields) {
226226
result.add(ColumnMetadata.builder()
227-
.setName(field.getName())
227+
.setName(field.name())
228228
.setType(toTrino(field).type())
229229
.build());
230230
}
@@ -241,11 +241,11 @@ private Map<String, ColumnHandle> makeColumnHandles(List<IndexMetadata.Field> fi
241241

242242
for (IndexMetadata.Field field : fields) {
243243
TypeAndDecoder converted = toTrino(field);
244-
result.put(field.getName(), new ElasticsearchColumnHandle(
245-
field.getName(),
244+
result.put(field.name(), new ElasticsearchColumnHandle(
245+
field.name(),
246246
converted.type(),
247247
converted.decoderDescriptor(),
248-
supportsPredicates(field.getType())));
248+
supportsPredicates(field.type())));
249249
}
250250

251251
return result.buildOrThrow();
@@ -258,7 +258,7 @@ private static boolean supportsPredicates(IndexMetadata.Type type)
258258
}
259259

260260
if (type instanceof PrimitiveType) {
261-
switch (((PrimitiveType) type).getName().toLowerCase(ENGLISH)) {
261+
switch (((PrimitiveType) type).name().toLowerCase(ENGLISH)) {
262262
case "boolean":
263263
case "byte":
264264
case "short":
@@ -281,7 +281,7 @@ private TypeAndDecoder toTrino(IndexMetadata.Field field)
281281

282282
private TypeAndDecoder toTrino(String prefix, IndexMetadata.Field field)
283283
{
284-
String path = appendPath(prefix, field.getName());
284+
String path = appendPath(prefix, field.name());
285285

286286
checkArgument(!field.asRawJson() || !field.isArray(), format("A column, (%s) cannot be declared as a Trino array and also be rendered as json.", path));
287287

@@ -294,9 +294,9 @@ private TypeAndDecoder toTrino(String prefix, IndexMetadata.Field field)
294294
return new TypeAndDecoder(new ArrayType(element.type()), new ArrayDecoder.Descriptor(element.decoderDescriptor()));
295295
}
296296

297-
IndexMetadata.Type type = field.getType();
297+
IndexMetadata.Type type = field.type();
298298
if (type instanceof PrimitiveType primitiveType) {
299-
switch (primitiveType.getName()) {
299+
switch (primitiveType.name()) {
300300
case "float":
301301
return new TypeAndDecoder(REAL, new RealDecoder.Descriptor(path));
302302
case "double":
@@ -324,16 +324,16 @@ else if (type instanceof ScaledFloatType) {
324324
return new TypeAndDecoder(DOUBLE, new DoubleDecoder.Descriptor(path));
325325
}
326326
else if (type instanceof DateTimeType dateTimeType) {
327-
if (dateTimeType.getFormats().isEmpty()) {
327+
if (dateTimeType.formats().isEmpty()) {
328328
return new TypeAndDecoder(TIMESTAMP_MILLIS, new TimestampDecoder.Descriptor(path));
329329
}
330330
// otherwise, skip -- we don't support custom formats, yet
331331
}
332332
else if (type instanceof ObjectType objectType) {
333333
ImmutableList.Builder<RowType.Field> rowFieldsBuilder = ImmutableList.builder();
334334
ImmutableList.Builder<RowDecoder.NameAndDescriptor> decoderFields = ImmutableList.builder();
335-
for (IndexMetadata.Field rowField : objectType.getFields()) {
336-
String name = rowField.getName();
335+
for (IndexMetadata.Field rowField : objectType.fields()) {
336+
String name = rowField.name();
337337
TypeAndDecoder child = toTrino(path, rowField);
338338

339339
if (child != null) {
@@ -365,7 +365,7 @@ private static String appendPath(String base, String element)
365365
public static IndexMetadata.Field elementField(IndexMetadata.Field field)
366366
{
367367
checkArgument(field.isArray(), "Cannot get element field from a non-array field");
368-
return new IndexMetadata.Field(field.asRawJson(), false, field.getName(), field.getType());
368+
return new IndexMetadata.Field(field.asRawJson(), false, field.name(), field.type());
369369
}
370370

371371
@Override
@@ -547,9 +547,9 @@ public Optional<ConstraintApplicationResult<ConnectorTableHandle>> applyFilter(C
547547

548548
if (!newRegexes.containsKey(columnName) && pattern instanceof Slice) {
549549
IndexMetadata metadata = client.getIndexMetadata(handle.index());
550-
if (metadata.getSchema()
551-
.getFields().stream()
552-
.anyMatch(field -> columnName.equals(field.getName()) && field.getType() instanceof PrimitiveType && "keyword".equals(((PrimitiveType) field.getType()).getName()))) {
550+
if (metadata.schema()
551+
.fields().stream()
552+
.anyMatch(field -> columnName.equals(field.name()) && field.type() instanceof PrimitiveType && "keyword".equals(((PrimitiveType) field.type()).name()))) {
553553
newRegexes.put(columnName, likeToRegexp((Slice) pattern, escape));
554554
continue;
555555
}

plugin/trino-elasticsearch/src/main/java/io/trino/plugin/elasticsearch/client/ElasticsearchClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ public List<String> getIndexes()
391391
if (docsCount == 0 && deletedDocsCount == 0) {
392392
try {
393393
// without documents, the index won't have any dynamic mappings, but maybe there are some explicit ones
394-
if (getIndexMetadata(index).getSchema().getFields().isEmpty()) {
394+
if (getIndexMetadata(index).schema().fields().isEmpty()) {
395395
continue;
396396
}
397397
}

plugin/trino-elasticsearch/src/main/java/io/trino/plugin/elasticsearch/client/IndexMetadata.java

Lines changed: 17 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -21,125 +21,57 @@
2121
import static java.lang.String.format;
2222
import static java.util.Objects.requireNonNull;
2323

24-
public class IndexMetadata
24+
public record IndexMetadata(ObjectType schema)
2525
{
26-
private final ObjectType schema;
27-
28-
public IndexMetadata(ObjectType schema)
26+
public IndexMetadata
2927
{
30-
this.schema = requireNonNull(schema, "schema is null");
28+
requireNonNull(schema, "schema is null");
3129
}
3230

33-
public ObjectType getSchema()
31+
public record Field(boolean asRawJson, boolean isArray, String name, Type type)
3432
{
35-
return schema;
36-
}
37-
38-
public static class Field
39-
{
40-
private final boolean asRawJson;
41-
private final boolean isArray;
42-
private final String name;
43-
private final Type type;
44-
45-
public Field(boolean asRawJson, boolean isArray, String name, Type type)
33+
public Field
4634
{
4735
checkArgument(!asRawJson || !isArray,
4836
format("A column, (%s) cannot be declared as a Trino array and also be rendered as json.", name));
49-
this.asRawJson = asRawJson;
50-
this.isArray = isArray;
51-
this.name = requireNonNull(name, "name is null");
52-
this.type = requireNonNull(type, "type is null");
53-
}
54-
55-
public boolean asRawJson()
56-
{
57-
return asRawJson;
58-
}
59-
60-
public boolean isArray()
61-
{
62-
return isArray;
63-
}
64-
65-
public String getName()
66-
{
67-
return name;
68-
}
69-
70-
public Type getType()
71-
{
72-
return type;
37+
requireNonNull(name, "name is null");
38+
requireNonNull(type, "type is null");
7339
}
7440
}
7541

7642
public interface Type {}
7743

78-
public static class PrimitiveType
44+
public record PrimitiveType(String name)
7945
implements Type
8046
{
81-
private final String name;
82-
83-
public PrimitiveType(String name)
47+
public PrimitiveType
8448
{
85-
this.name = requireNonNull(name, "name is null");
86-
}
87-
88-
public String getName()
89-
{
90-
return name;
49+
requireNonNull(name, "name is null");
9150
}
9251
}
9352

94-
public static class DateTimeType
53+
public record DateTimeType(List<String> formats)
9554
implements Type
9655
{
97-
private final List<String> formats;
98-
99-
public DateTimeType(List<String> formats)
56+
public DateTimeType
10057
{
10158
requireNonNull(formats, "formats is null");
102-
103-
this.formats = ImmutableList.copyOf(formats);
104-
}
105-
106-
public List<String> getFormats()
107-
{
108-
return formats;
59+
formats = ImmutableList.copyOf(formats);
10960
}
11061
}
11162

112-
public static class ObjectType
63+
public record ObjectType(List<Field> fields)
11364
implements Type
11465
{
115-
private final List<Field> fields;
116-
117-
public ObjectType(List<Field> fields)
66+
public ObjectType
11867
{
11968
requireNonNull(fields, "fields is null");
120-
121-
this.fields = ImmutableList.copyOf(fields);
122-
}
123-
124-
public List<Field> getFields()
125-
{
126-
return fields;
69+
fields = ImmutableList.copyOf(fields);
12770
}
12871
}
12972

130-
public static class ScaledFloatType
73+
public record ScaledFloatType(double scale)
13174
implements Type
13275
{
133-
private final double scale;
134-
135-
public ScaledFloatType(double scale)
136-
{
137-
this.scale = scale;
138-
}
139-
140-
public double getScale()
141-
{
142-
return scale;
143-
}
14476
}
14577
}

0 commit comments

Comments
 (0)