Skip to content

Commit 4bd8e7b

Browse files
authored
[ML-DataFrame] Refactorings and tidying (#41248)
Remove unnecessary generic params from SingleGroupSource and unused code from the HLRC
1 parent d706b40 commit 4bd8e7b

File tree

14 files changed

+39
-72
lines changed

14 files changed

+39
-72
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/dataframe/transforms/pivot/SingleGroupSource.java

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,9 @@ public abstract class SingleGroupSource implements ToXContentObject {
3030
protected static final ParseField FIELD = new ParseField("field");
3131

3232
public enum Type {
33-
TERMS(0),
34-
HISTOGRAM(1),
35-
DATE_HISTOGRAM(2);
36-
37-
private final byte id;
38-
39-
Type(int id) {
40-
this.id = (byte) id;
41-
}
42-
43-
public byte getId() {
44-
return id;
45-
}
46-
47-
public static Type fromId(byte id) {
48-
switch (id) {
49-
case 0:
50-
return TERMS;
51-
case 1:
52-
return HISTOGRAM;
53-
case 2:
54-
return DATE_HISTOGRAM;
55-
default:
56-
throw new IllegalArgumentException("unknown type");
57-
}
58-
}
33+
TERMS,
34+
HISTOGRAM,
35+
DATE_HISTOGRAM;
5936

6037
public String value() {
6138
return name().toLowerCase(Locale.ROOT);

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/dataframe/transforms/DataFrameTransformConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public String toString() {
215215
}
216216

217217
public static DataFrameTransformConfig fromXContent(final XContentParser parser, @Nullable final String optionalTransformId,
218-
boolean lenient) throws IOException {
218+
boolean lenient) {
219219

220220
return lenient ? LENIENT_PARSER.apply(parser, optionalTransformId) : STRICT_PARSER.apply(parser, optionalTransformId);
221221
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/dataframe/transforms/pivot/DateHistogramGroupSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.util.Objects;
2121

2222

23-
public class DateHistogramGroupSource extends SingleGroupSource<DateHistogramGroupSource> {
23+
public class DateHistogramGroupSource extends SingleGroupSource {
2424

2525
private static final String NAME = "data_frame_date_histogram_group";
2626
private static final ParseField TIME_ZONE = new ParseField("time_zone");
@@ -51,7 +51,7 @@ private static ConstructingObjectParser<DateHistogramGroupSource, Void> createPa
5151
return new DateHistogramGroupSource(field);
5252
});
5353

54-
declareValuesSourceFields(parser, null);
54+
declareValuesSourceFields(parser);
5555

5656
parser.declareField((histogram, interval) -> {
5757
if (interval instanceof Long) {

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/dataframe/transforms/pivot/GroupConfig.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public class GroupConfig implements Writeable, ToXContentObject {
4040
private static final Logger logger = LogManager.getLogger(GroupConfig.class);
4141

4242
private final Map<String, Object> source;
43-
private final Map<String, SingleGroupSource<?>> groups;
43+
private final Map<String, SingleGroupSource> groups;
4444

45-
public GroupConfig(final Map<String, Object> source, final Map<String, SingleGroupSource<?>> groups) {
45+
public GroupConfig(final Map<String, Object> source, final Map<String, SingleGroupSource> groups) {
4646
this.source = ExceptionsHelper.requireNonNull(source, DataFrameField.GROUP_BY.getPreferredName());
4747
this.groups = groups;
4848
}
@@ -64,7 +64,7 @@ public GroupConfig(StreamInput in) throws IOException {
6464
});
6565
}
6666

67-
public Map <String, SingleGroupSource<?>> getGroups() {
67+
public Map <String, SingleGroupSource> getGroups() {
6868
return groups;
6969
}
7070

@@ -109,7 +109,7 @@ public int hashCode() {
109109
public static GroupConfig fromXContent(final XContentParser parser, boolean lenient) throws IOException {
110110
NamedXContentRegistry registry = parser.getXContentRegistry();
111111
Map<String, Object> source = parser.mapOrdered();
112-
Map<String, SingleGroupSource<?>> groups = null;
112+
Map<String, SingleGroupSource> groups = null;
113113

114114
if (source.isEmpty()) {
115115
if (lenient) {
@@ -133,9 +133,9 @@ public static GroupConfig fromXContent(final XContentParser parser, boolean leni
133133
return new GroupConfig(source, groups);
134134
}
135135

136-
private static Map<String, SingleGroupSource<?>> parseGroupConfig(final XContentParser parser,
136+
private static Map<String, SingleGroupSource> parseGroupConfig(final XContentParser parser,
137137
boolean lenient) throws IOException {
138-
LinkedHashMap<String, SingleGroupSource<?>> groups = new LinkedHashMap<>();
138+
LinkedHashMap<String, SingleGroupSource> groups = new LinkedHashMap<>();
139139

140140
// be parsing friendly, whether the token needs to be advanced or not (similar to what ObjectParser does)
141141
XContentParser.Token token;
@@ -158,7 +158,7 @@ private static Map<String, SingleGroupSource<?>> parseGroupConfig(final XContent
158158

159159
token = parser.nextToken();
160160
ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser::getTokenLocation);
161-
SingleGroupSource<?> groupSource;
161+
SingleGroupSource groupSource;
162162
switch (groupType) {
163163
case TERMS:
164164
groupSource = TermsGroupSource.fromXContent(parser, lenient);

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/dataframe/transforms/pivot/HistogramGroupSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
1919

20-
public class HistogramGroupSource extends SingleGroupSource<HistogramGroupSource> {
20+
public class HistogramGroupSource extends SingleGroupSource {
2121

2222
static final ParseField INTERVAL = new ParseField("interval");
2323
private static final String NAME = "data_frame_histogram_group";
@@ -44,7 +44,7 @@ private static ConstructingObjectParser<HistogramGroupSource, Void> createParser
4444
double interval = (double) args[1];
4545
return new HistogramGroupSource(field, interval);
4646
});
47-
declareValuesSourceFields(parser, null);
47+
declareValuesSourceFields(parser);
4848
parser.declareDouble(optionalConstructorArg(), INTERVAL);
4949
return parser;
5050
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/dataframe/transforms/pivot/PivotConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void toCompositeAggXContent(XContentBuilder builder, Params params) throw
9191
builder.field(CompositeAggregationBuilder.SOURCES_FIELD_NAME.getPreferredName());
9292
builder.startArray();
9393

94-
for (Entry<String, SingleGroupSource<?>> groupBy : groups.getGroups().entrySet()) {
94+
for (Entry<String, SingleGroupSource> groupBy : groups.getGroups().entrySet()) {
9595
builder.startObject();
9696
builder.startObject(groupBy.getKey());
9797
builder.field(groupBy.getValue().getType().value(), groupBy.getValue());

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/dataframe/transforms/pivot/SingleGroupSource.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.elasticsearch.common.xcontent.AbstractObjectParser;
1414
import org.elasticsearch.common.xcontent.ToXContentObject;
1515
import org.elasticsearch.common.xcontent.XContentBuilder;
16-
import org.elasticsearch.search.aggregations.support.ValueType;
1716

1817
import java.io.IOException;
1918
import java.util.Locale;
@@ -24,7 +23,7 @@
2423
/*
2524
* Base class for a single source for group_by
2625
*/
27-
public abstract class SingleGroupSource<AB extends SingleGroupSource<AB>> implements Writeable, ToXContentObject {
26+
public abstract class SingleGroupSource implements Writeable, ToXContentObject {
2827

2928
public enum Type {
3029
TERMS(0),
@@ -64,8 +63,7 @@ public String value() {
6463
// TODO: add script
6564
protected final String field;
6665

67-
static <VB extends SingleGroupSource<?>, T> void declareValuesSourceFields(AbstractObjectParser<VB, T> parser,
68-
ValueType targetValueType) {
66+
static <T> void declareValuesSourceFields(AbstractObjectParser<? extends SingleGroupSource, T> parser) {
6967
// either script or field
7068
parser.declareString(optionalConstructorArg(), FIELD);
7169
}
@@ -109,7 +107,7 @@ public boolean equals(Object other) {
109107
return false;
110108
}
111109

112-
final SingleGroupSource<?> that = (SingleGroupSource<?>) other;
110+
final SingleGroupSource that = (SingleGroupSource) other;
113111

114112
return Objects.equals(this.field, that.field);
115113
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/dataframe/transforms/pivot/TermsGroupSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/*
1616
* A terms aggregation source for group_by
1717
*/
18-
public class TermsGroupSource extends SingleGroupSource<TermsGroupSource> {
18+
public class TermsGroupSource extends SingleGroupSource {
1919
private static final String NAME = "data_frame_terms_group";
2020

2121
private static final ConstructingObjectParser<TermsGroupSource, Void> STRICT_PARSER = createParser(false);
@@ -27,7 +27,7 @@ private static ConstructingObjectParser<TermsGroupSource, Void> createParser(boo
2727
return new TermsGroupSource(field);
2828
});
2929

30-
SingleGroupSource.declareValuesSourceFields(parser, null);
30+
SingleGroupSource.declareValuesSourceFields(parser);
3131
return parser;
3232
}
3333

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/dataframe/transforms/pivot/GroupConfigTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public class GroupConfigTests extends AbstractSerializingTestCase<GroupConfig> {
2929

3030
public static GroupConfig randomGroupConfig() {
3131
Map<String, Object> source = new LinkedHashMap<>();
32-
Map<String, SingleGroupSource<?>> groups = new LinkedHashMap<>();
32+
Map<String, SingleGroupSource> groups = new LinkedHashMap<>();
3333

3434
// ensure that the unlikely does not happen: 2 group_by's share the same name
3535
Set<String> names = new HashSet<>();
3636
for (int i = 0; i < randomIntBetween(1, 20); ++i) {
3737
String targetFieldName = randomAlphaOfLengthBetween(1, 20);
3838
if (names.add(targetFieldName)) {
39-
SingleGroupSource<?> groupBy;
39+
SingleGroupSource groupBy;
4040
Type type = randomFrom(SingleGroupSource.Type.values());
4141
switch (type) {
4242
case TERMS:
@@ -88,7 +88,7 @@ public void testEmptyGroupBy() throws IOException {
8888
}
8989
}
9090

91-
private static Map<String, Object> getSource(SingleGroupSource<?> groupSource) {
91+
private static Map<String, Object> getSource(SingleGroupSource groupSource) {
9292
try (XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()) {
9393
XContentBuilder content = groupSource.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
9494
return XContentHelper.convertToMap(BytesReference.bytes(content), true, XContentType.JSON).v2();

x-pack/plugin/data-frame/src/main/java/org/elasticsearch/xpack/dataframe/action/TransportGetDataFrameTransformsAction.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
import org.elasticsearch.xpack.core.dataframe.transforms.DataFrameTransformConfig;
2929
import org.elasticsearch.xpack.dataframe.persistence.DataFrameInternalIndex;
3030

31-
import java.io.IOException;
32-
3331
import static org.elasticsearch.xpack.core.dataframe.DataFrameField.INDEX_DOC_TYPE;
3432

3533

@@ -62,7 +60,7 @@ protected String[] getIndices() {
6260
}
6361

6462
@Override
65-
protected DataFrameTransformConfig parse(XContentParser parser) throws IOException {
63+
protected DataFrameTransformConfig parse(XContentParser parser) {
6664
return DataFrameTransformConfig.fromXContent(parser, null, true);
6765
}
6866

0 commit comments

Comments
 (0)