Skip to content

Commit 3d3a6cd

Browse files
committed
refactor to be less dumb
1 parent fdb9628 commit 3d3a6cd

File tree

7 files changed

+131
-43
lines changed

7 files changed

+131
-43
lines changed

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/type/DateEsField.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,35 @@
2222
public class DateEsField extends EsField {
2323

2424
public static DateEsField dateEsField(String name, Map<String, EsField> properties, boolean hasDocValues) {
25-
return new DateEsField(name, DataType.DATETIME, properties, hasDocValues);
25+
return new DateEsField(name, DataType.DATETIME, properties, hasDocValues, TimeSeriesFieldType.UNKNOWN);
2626
}
2727

28-
private DateEsField(String name, DataType dataType, Map<String, EsField> properties, boolean hasDocValues) {
29-
super(name, dataType, properties, hasDocValues);
28+
private DateEsField(
29+
String name,
30+
DataType dataType,
31+
Map<String, EsField> properties,
32+
boolean hasDocValues,
33+
TimeSeriesFieldType timeSeriesFieldType
34+
) {
35+
super(name, dataType, properties, hasDocValues, timeSeriesFieldType);
3036
}
3137

3238
protected DateEsField(StreamInput in) throws IOException {
33-
this(readCachedStringWithVersionCheck(in), DataType.DATETIME, in.readImmutableMap(EsField::readFrom), in.readBoolean());
34-
if (in.getTransportVersion().onOrAfter(TransportVersions.ESQL_SERIALIZE_TIMESERIES_FIELD_TYPE)) {
35-
this.timeSeriesFieldType = TimeSeriesFieldType.readFromStream(in);
36-
} else {
37-
this.timeSeriesFieldType = TimeSeriesFieldType.UNKNOWN;
38-
}
39+
this(
40+
readCachedStringWithVersionCheck(in),
41+
DataType.DATETIME,
42+
in.readImmutableMap(EsField::readFrom),
43+
in.readBoolean(),
44+
readTimeSeriesFieldType(in)
45+
);
3946
}
4047

4148
@Override
4249
public void writeContent(StreamOutput out) throws IOException {
4350
writeCachedStringWithVersionCheck(out, getName());
4451
out.writeMap(getProperties(), (o, x) -> x.writeTo(out));
4552
out.writeBoolean(isAggregatable());
46-
if (out.getTransportVersion().onOrAfter(TransportVersions.ESQL_SERIALIZE_TIMESERIES_FIELD_TYPE)) {
47-
this.timeSeriesFieldType.writeTo(out);
48-
}
53+
writeTimeSeriesFieldType(out);
4954
}
5055

5156
public String getWriteableName() {

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/type/EsField.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,36 @@ public static Reader<? extends EsField> getReader(String name) {
8484
private final String name;
8585
private final boolean isAlias;
8686
// Because the subclasses all reimplement serialization, this needs to be writeable from subclass constructors
87-
protected TimeSeriesFieldType timeSeriesFieldType;
87+
private final TimeSeriesFieldType timeSeriesFieldType;
8888

8989
public EsField(String name, DataType esDataType, Map<String, EsField> properties, boolean aggregatable) {
90-
this(name, esDataType, properties, aggregatable, false);
90+
this(name, esDataType, properties, aggregatable, false, TimeSeriesFieldType.UNKNOWN);
9191
}
9292

93-
public EsField(String name, DataType esDataType, Map<String, EsField> properties, boolean aggregatable, boolean isAlias) {
93+
public EsField(
94+
String name,
95+
DataType esDataType,
96+
Map<String, EsField> properties,
97+
boolean aggregatable,
98+
TimeSeriesFieldType timeSeriesFieldType
99+
) {
100+
this(name, esDataType, properties, aggregatable, false, timeSeriesFieldType);
101+
}
102+
103+
public EsField(
104+
String name,
105+
DataType esDataType,
106+
Map<String, EsField> properties,
107+
boolean aggregatable,
108+
boolean isAlias,
109+
TimeSeriesFieldType timeSeriesFieldType
110+
) {
94111
this.name = name;
95112
this.esDataType = esDataType;
96113
this.aggregatable = aggregatable;
97114
this.properties = properties;
98115
this.isAlias = isAlias;
99-
this.timeSeriesFieldType = TimeSeriesFieldType.UNKNOWN;
116+
this.timeSeriesFieldType = timeSeriesFieldType;
100117
}
101118

102119
public EsField(StreamInput in) throws IOException {
@@ -105,11 +122,7 @@ public EsField(StreamInput in) throws IOException {
105122
this.properties = in.readImmutableMap(EsField::readFrom);
106123
this.aggregatable = in.readBoolean();
107124
this.isAlias = in.readBoolean();
108-
if (in.getTransportVersion().onOrAfter(TransportVersions.ESQL_SERIALIZE_TIMESERIES_FIELD_TYPE)) {
109-
this.timeSeriesFieldType = TimeSeriesFieldType.readFromStream(in);
110-
} else {
111-
this.timeSeriesFieldType = TimeSeriesFieldType.UNKNOWN;
112-
}
125+
this.timeSeriesFieldType = readTimeSeriesFieldType(in);
113126
}
114127

115128
private DataType readDataType(StreamInput in) throws IOException {
@@ -148,11 +161,23 @@ public void writeContent(StreamOutput out) throws IOException {
148161
out.writeMap(properties, (o, x) -> x.writeTo(out));
149162
out.writeBoolean(aggregatable);
150163
out.writeBoolean(isAlias);
164+
writeTimeSeriesFieldType(out);
165+
}
166+
167+
protected void writeTimeSeriesFieldType(StreamOutput out) throws IOException {
151168
if (out.getTransportVersion().onOrAfter(TransportVersions.ESQL_SERIALIZE_TIMESERIES_FIELD_TYPE)) {
152169
this.timeSeriesFieldType.writeTo(out);
153170
}
154171
}
155172

173+
protected static TimeSeriesFieldType readTimeSeriesFieldType(StreamInput in) throws IOException {
174+
if (in.getTransportVersion().onOrAfter(TransportVersions.ESQL_SERIALIZE_TIMESERIES_FIELD_TYPE)) {
175+
return TimeSeriesFieldType.readFromStream(in);
176+
} else {
177+
return TimeSeriesFieldType.UNKNOWN;
178+
}
179+
}
180+
156181
/**
157182
* This needs to be overridden by subclasses for specific serialization
158183
*/

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/type/InvalidMappedField.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package org.elasticsearch.xpack.esql.core.type;
99

10+
import org.elasticsearch.TransportVersions;
1011
import org.elasticsearch.common.io.stream.StreamInput;
1112
import org.elasticsearch.common.io.stream.StreamOutput;
1213
import org.elasticsearch.xpack.esql.core.QlIllegalArgumentException;
@@ -34,7 +35,7 @@ public class InvalidMappedField extends EsField {
3435
private final Map<String, Set<String>> typesToIndices;
3536

3637
public InvalidMappedField(String name, String errorMessage, Map<String, EsField> properties) {
37-
this(name, errorMessage, properties, Map.of());
38+
this(name, errorMessage, properties, Map.of(), TimeSeriesFieldType.UNKNOWN);
3839
}
3940

4041
public InvalidMappedField(String name, String errorMessage) {
@@ -45,17 +46,29 @@ public InvalidMappedField(String name, String errorMessage) {
4546
* Constructor supporting union types, used in ES|QL.
4647
*/
4748
public InvalidMappedField(String name, Map<String, Set<String>> typesToIndices) {
48-
this(name, makeErrorMessage(typesToIndices, false), new TreeMap<>(), typesToIndices);
49+
this(name, makeErrorMessage(typesToIndices, false), new TreeMap<>(), typesToIndices, TimeSeriesFieldType.UNKNOWN);
4950
}
5051

51-
private InvalidMappedField(String name, String errorMessage, Map<String, EsField> properties, Map<String, Set<String>> typesToIndices) {
52-
super(name, DataType.UNSUPPORTED, properties, false);
52+
private InvalidMappedField(
53+
String name,
54+
String errorMessage,
55+
Map<String, EsField> properties,
56+
Map<String, Set<String>> typesToIndices,
57+
TimeSeriesFieldType type
58+
) {
59+
super(name, DataType.UNSUPPORTED, properties, false, type);
5360
this.errorMessage = errorMessage;
5461
this.typesToIndices = typesToIndices;
5562
}
5663

5764
protected InvalidMappedField(StreamInput in) throws IOException {
58-
this(readCachedStringWithVersionCheck(in), in.readString(), in.readImmutableMap(StreamInput::readString, EsField::readFrom));
65+
this(
66+
readCachedStringWithVersionCheck(in),
67+
in.readString(),
68+
in.readImmutableMap(StreamInput::readString, EsField::readFrom),
69+
Map.of(),
70+
readTimeSeriesFieldType(in)
71+
);
5972
}
6073

6174
public Set<DataType> types() {
@@ -67,6 +80,7 @@ public void writeContent(StreamOutput out) throws IOException {
6780
writeCachedStringWithVersionCheck(out, getName());
6881
out.writeString(errorMessage);
6982
out.writeMap(getProperties(), (o, x) -> x.writeTo(out));
83+
writeTimeSeriesFieldType(out);
7084
}
7185

7286
public String getWriteableName() {

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/type/KeywordEsField.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
package org.elasticsearch.xpack.esql.core.type;
88

9+
import org.elasticsearch.TransportVersions;
910
import org.elasticsearch.common.io.stream.StreamInput;
1011
import org.elasticsearch.common.io.stream.StreamOutput;
1112

@@ -42,7 +43,7 @@ public KeywordEsField(
4243
boolean normalized,
4344
boolean isAlias
4445
) {
45-
this(name, KEYWORD, properties, hasDocValues, precision, normalized, isAlias);
46+
this(name, KEYWORD, properties, hasDocValues, precision, normalized, isAlias, TimeSeriesFieldType.UNKNOWN);
4647
}
4748

4849
protected KeywordEsField(
@@ -52,9 +53,10 @@ protected KeywordEsField(
5253
boolean hasDocValues,
5354
int precision,
5455
boolean normalized,
55-
boolean isAlias
56+
boolean isAlias,
57+
TimeSeriesFieldType timeSeriesFieldType
5658
) {
57-
super(name, esDataType, properties, hasDocValues, isAlias);
59+
super(name, esDataType, properties, hasDocValues, isAlias, timeSeriesFieldType);
5860
this.precision = precision;
5961
this.normalized = normalized;
6062
}
@@ -67,7 +69,8 @@ public KeywordEsField(StreamInput in) throws IOException {
6769
in.readBoolean(),
6870
in.readInt(),
6971
in.readBoolean(),
70-
in.readBoolean()
72+
in.readBoolean(),
73+
readTimeSeriesFieldType(in)
7174
);
7275
}
7376

@@ -79,6 +82,7 @@ public void writeContent(StreamOutput out) throws IOException {
7982
out.writeInt(precision);
8083
out.writeBoolean(normalized);
8184
out.writeBoolean(isAlias());
85+
writeTimeSeriesFieldType(out);
8286
}
8387

8488
public String getWriteableName() {

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/type/MultiTypeEsField.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
package org.elasticsearch.xpack.esql.core.type;
99

10+
import org.elasticsearch.TransportVersions;
1011
import org.elasticsearch.common.io.stream.StreamInput;
1112
import org.elasticsearch.common.io.stream.StreamOutput;
1213
import org.elasticsearch.xpack.esql.core.expression.Expression;
@@ -38,12 +39,24 @@ public MultiTypeEsField(String name, DataType dataType, boolean aggregatable, Ma
3839
this.indexToConversionExpressions = indexToConversionExpressions;
3940
}
4041

42+
public MultiTypeEsField(
43+
String name,
44+
DataType dataType,
45+
boolean aggregatable,
46+
Map<String, Expression> indexToConversionExpressions,
47+
TimeSeriesFieldType timeSeriesFieldType
48+
) {
49+
super(name, dataType, Map.of(), aggregatable, timeSeriesFieldType);
50+
this.indexToConversionExpressions = indexToConversionExpressions;
51+
}
52+
4153
protected MultiTypeEsField(StreamInput in) throws IOException {
4254
this(
4355
readCachedStringWithVersionCheck(in),
4456
DataType.readFrom(in),
4557
in.readBoolean(),
46-
in.readImmutableMap(i -> i.readNamedWriteable(Expression.class))
58+
in.readImmutableMap(i -> i.readNamedWriteable(Expression.class)),
59+
readTimeSeriesFieldType(in)
4760
);
4861
}
4962

@@ -53,6 +66,7 @@ public void writeContent(StreamOutput out) throws IOException {
5366
getDataType().writeTo(out);
5467
out.writeBoolean(isAggregatable());
5568
out.writeMap(getIndexToConversionExpressions(), (o, v) -> out.writeNamedWriteable(v));
69+
writeTimeSeriesFieldType(out);
5670
}
5771

5872
public String getWriteableName() {

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/type/TextEsField.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,27 @@ public TextEsField(String name, Map<String, EsField> properties, boolean hasDocV
3131
}
3232

3333
public TextEsField(String name, Map<String, EsField> properties, boolean hasDocValues, boolean isAlias) {
34-
super(name, TEXT, properties, hasDocValues, isAlias);
34+
super(name, TEXT, properties, hasDocValues, isAlias, TimeSeriesFieldType.UNKNOWN);
35+
}
36+
37+
public TextEsField(
38+
String name,
39+
Map<String, EsField> properties,
40+
boolean hasDocValues,
41+
boolean isAlias,
42+
TimeSeriesFieldType timeSeriesFieldType
43+
) {
44+
super(name, TEXT, properties, hasDocValues, isAlias, timeSeriesFieldType);
3545
}
3646

3747
protected TextEsField(StreamInput in) throws IOException {
38-
this(readCachedStringWithVersionCheck(in), in.readImmutableMap(EsField::readFrom), in.readBoolean(), in.readBoolean());
39-
if (in.getTransportVersion().onOrAfter(TransportVersions.ESQL_SERIALIZE_TIMESERIES_FIELD_TYPE)) {
40-
this.timeSeriesFieldType = TimeSeriesFieldType.readFromStream(in);
41-
} else {
42-
this.timeSeriesFieldType = TimeSeriesFieldType.UNKNOWN;
43-
}
48+
this(
49+
readCachedStringWithVersionCheck(in),
50+
in.readImmutableMap(EsField::readFrom),
51+
in.readBoolean(),
52+
in.readBoolean(),
53+
readTimeSeriesFieldType(in)
54+
);
4455
}
4556

4657
@Override
@@ -49,9 +60,7 @@ public void writeContent(StreamOutput out) throws IOException {
4960
out.writeMap(getProperties(), (o, x) -> x.writeTo(out));
5061
out.writeBoolean(isAggregatable());
5162
out.writeBoolean(isAlias());
52-
if (out.getTransportVersion().onOrAfter(TransportVersions.ESQL_SERIALIZE_TIMESERIES_FIELD_TYPE)) {
53-
this.timeSeriesFieldType.writeTo(out);
54-
}
63+
writeTimeSeriesFieldType(out);
5564
}
5665

5766
public String getWriteableName() {

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/type/UnsupportedEsField.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,29 @@ public UnsupportedEsField(String name, List<String> originalTypes) {
3535
}
3636

3737
public UnsupportedEsField(String name, List<String> originalTypes, String inherited, Map<String, EsField> properties) {
38-
super(name, DataType.UNSUPPORTED, properties, false);
38+
this(name, originalTypes, inherited, properties, TimeSeriesFieldType.UNKNOWN);
39+
}
40+
41+
public UnsupportedEsField(
42+
String name,
43+
List<String> originalTypes,
44+
String inherited,
45+
Map<String, EsField> properties,
46+
TimeSeriesFieldType timeSeriesFieldType
47+
) {
48+
super(name, DataType.UNSUPPORTED, properties, false, timeSeriesFieldType);
3949
this.originalTypes = originalTypes;
4050
this.inherited = inherited;
4151
}
4252

4353
public UnsupportedEsField(StreamInput in) throws IOException {
44-
this(readCachedStringWithVersionCheck(in), readOriginalTypes(in), in.readOptionalString(), in.readImmutableMap(EsField::readFrom));
54+
this(
55+
readCachedStringWithVersionCheck(in),
56+
readOriginalTypes(in),
57+
in.readOptionalString(),
58+
in.readImmutableMap(EsField::readFrom),
59+
readTimeSeriesFieldType(in)
60+
);
4561
}
4662

4763
private static List<String> readOriginalTypes(StreamInput in) throws IOException {
@@ -64,6 +80,7 @@ public void writeContent(StreamOutput out) throws IOException {
6480
}
6581
out.writeOptionalString(getInherited());
6682
out.writeMap(getProperties(), (o, x) -> x.writeTo(out));
83+
writeTimeSeriesFieldType(out);
6784
}
6885

6986
public String getWriteableName() {

0 commit comments

Comments
 (0)