Skip to content

Commit e737361

Browse files
authored
[ML][Data Frame] improve pivot nested field validations (#43548)
* [ML][Data Frame] improve pivot nested field validations * addressing pr comments
1 parent 3884cfb commit e737361

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/dataframe/action/PreviewDataFrameTransformAction.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.util.Map;
3434
import java.util.Objects;
3535

36+
import static org.elasticsearch.action.ValidateActions.addValidationError;
37+
3638
public class PreviewDataFrameTransformAction extends Action<PreviewDataFrameTransformAction.Response> {
3739

3840
public static final PreviewDataFrameTransformAction INSTANCE = new PreviewDataFrameTransformAction();
@@ -94,9 +96,16 @@ public static Request fromXContent(final XContentParser parser) throws IOExcepti
9496

9597
@Override
9698
public ActionRequestValidationException validate() {
97-
return null;
99+
ActionRequestValidationException validationException = null;
100+
if(config.getPivotConfig() != null) {
101+
for(String failure : config.getPivotConfig().aggFieldValidation()) {
102+
validationException = addValidationError(failure, validationException);
103+
}
104+
}
105+
return validationException;
98106
}
99107

108+
100109
@Override
101110
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
102111
return this.config.toXContent(builder, params);

x-pack/plugin/src/test/resources/rest-api-spec/test/data_frame/preview_transforms.yml

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ setup:
7575
"pivot": {
7676
"group_by": {
7777
"airline": {"terms": {"field": "airline"}},
78-
"by-hour": {"date_histogram": {"fixed_interval": "1h", "field": "time", "format": "yyyy-MM-dd HH"}}},
78+
"by-hour": {"date_histogram": {"fixed_interval": "1h", "field": "time"}}},
7979
"aggs": {
8080
"avg_response": {"avg": {"field": "responsetime"}},
8181
"time.max": {"max": {"field": "time"}},
@@ -84,17 +84,17 @@ setup:
8484
}
8585
}
8686
- match: { preview.0.airline: foo }
87-
- match: { preview.0.by-hour: "2017-02-18 00" }
87+
- match: { preview.0.by-hour: 1487376000000 }
8888
- match: { preview.0.avg_response: 1.0 }
8989
- match: { preview.0.time.max: "2017-02-18T00:30:00.000Z" }
9090
- match: { preview.0.time.min: "2017-02-18T00:00:00.000Z" }
9191
- match: { preview.1.airline: bar }
92-
- match: { preview.1.by-hour: "2017-02-18 01" }
92+
- match: { preview.1.by-hour: 1487379600000 }
9393
- match: { preview.1.avg_response: 42.0 }
9494
- match: { preview.1.time.max: "2017-02-18T01:00:00.000Z" }
9595
- match: { preview.1.time.min: "2017-02-18T01:00:00.000Z" }
9696
- match: { preview.2.airline: foo }
97-
- match: { preview.2.by-hour: "2017-02-18 01" }
97+
- match: { preview.2.by-hour: 1487379600000 }
9898
- match: { preview.2.avg_response: 42.0 }
9999
- match: { preview.2.time.max: "2017-02-18T01:01:00.000Z" }
100100
- match: { preview.2.time.min: "2017-02-18T01:01:00.000Z" }
@@ -123,22 +123,22 @@ setup:
123123
"pivot": {
124124
"group_by": {
125125
"airline": {"terms": {"field": "airline"}},
126-
"by-hour": {"date_histogram": {"fixed_interval": "1h", "field": "time", "format": "yyyy-MM-dd HH"}}},
126+
"by-hour": {"date_histogram": {"fixed_interval": "1h", "field": "time"}}},
127127
"aggs": {
128128
"avg_response": {"avg": {"field": "responsetime"}}
129129
}
130130
}
131131
}
132132
- match: { preview.0.airline: foo }
133-
- match: { preview.0.by-hour: "2017-02-18 00" }
133+
- match: { preview.0.by-hour: 1487376000000 }
134134
- match: { preview.0.avg_response: 1.0 }
135135
- match: { preview.0.my_field: 42 }
136136
- match: { preview.1.airline: bar }
137-
- match: { preview.1.by-hour: "2017-02-18 01" }
137+
- match: { preview.1.by-hour: 1487379600000 }
138138
- match: { preview.1.avg_response: 42.0 }
139139
- match: { preview.1.my_field: 42 }
140140
- match: { preview.2.airline: foo }
141-
- match: { preview.2.by-hour: "2017-02-18 01" }
141+
- match: { preview.2.by-hour: 1487379600000 }
142142
- match: { preview.2.avg_response: 42.0 }
143143
- match: { preview.2.my_field: 42 }
144144

@@ -166,7 +166,7 @@ setup:
166166
"pivot": {
167167
"group_by": {
168168
"airline": {"terms": {"field": "airline"}},
169-
"by-hour": {"date_histogram": {"fixed_interval": "1h", "field": "time", "format": "yyyy-MM-dd HH"}}},
169+
"by-hour": {"date_histogram": {"fixed_interval": "1h", "field": "time"}}},
170170
"aggs": {"avg_response": {"avg": {"field": "responsetime"}}}
171171
}
172172
}
@@ -180,7 +180,7 @@ setup:
180180
"source": { "index": "airline-data" },
181181
"pivot": {
182182
"group_by": {
183-
"time": {"date_histogram": {"fixed_interval": "1h", "field": "time", "format": "yyyy-MM-DD HH"}}},
183+
"time": {"date_histogram": {"fixed_interval": "1h", "field": "time"}}},
184184
"aggs": {
185185
"avg_response": {"avg": {"field": "responsetime"}},
186186
"time.min": {"min": {"field": "time"}}
@@ -189,20 +189,43 @@ setup:
189189
}
190190
191191
- do:
192-
catch: /mixed object types of nested and non-nested fields \[time.min\]/
192+
catch: /field \[time\] cannot be both an object and a field/
193193
data_frame.preview_data_frame_transform:
194194
body: >
195195
{
196196
"source": { "index": "airline-data" },
197197
"pivot": {
198198
"group_by": {
199-
"time": {"date_histogram": {"fixed_interval": "1h", "field": "time", "format": "yyyy-MM-DD HH"}}},
199+
"time": {"date_histogram": {"fixed_interval": "1h", "field": "time"}}},
200200
"aggs": {
201201
"avg_response": {"avg": {"field": "responsetime"}},
202202
"time.min": {"min": {"field": "time"}}
203203
}
204204
}
205205
}
206+
- do:
207+
catch: /field \[super_metric\] cannot be both an object and a field/
208+
data_frame.preview_data_frame_transform:
209+
body: >
210+
{
211+
"source": { "index": "airline-data" },
212+
"pivot": {
213+
"group_by": {
214+
"time": {"date_histogram": {"fixed_interval": "1h", "field": "time"}}},
215+
"aggs": {
216+
"avg_response": {"avg": {"field": "responsetime"}},
217+
"super_metric.time": {"min": {"field": "time"}},
218+
"super_metric": {
219+
"scripted_metric": {
220+
"init_script": "",
221+
"map_script": "",
222+
"combine_script": "",
223+
"reduce_script": "return ['value1': 1, 'value2':2]"
224+
}
225+
}
226+
}
227+
}
228+
}
206229
---
207230
"Test preview with missing pipeline":
208231
- do:
@@ -214,7 +237,7 @@ setup:
214237
"dest": { "pipeline": "missing-pipeline" },
215238
"pivot": {
216239
"group_by": {
217-
"time": {"date_histogram": {"fixed_interval": "1h", "field": "time", "format": "yyyy-MM-DD HH"}}},
240+
"time": {"date_histogram": {"fixed_interval": "1h", "field": "time"}}},
218241
"aggs": {
219242
"avg_response": {"avg": {"field": "responsetime"}},
220243
"time.min": {"min": {"field": "time"}}

0 commit comments

Comments
 (0)