Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] verify that there are no duplicate leaf fields in aggs #41895

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.io.IOException;
import java.util.Objects;

import static org.elasticsearch.action.ValidateActions.addValidationError;

public class PutDataFrameTransformAction extends Action<AcknowledgedResponse> {

public static final PutDataFrameTransformAction INSTANCE = new PutDataFrameTransformAction();
Expand Down Expand Up @@ -53,7 +55,11 @@ public static Request fromXContent(final XContentParser parser, final String id)

@Override
public ActionRequestValidationException validate() {
return null;
ActionRequestValidationException validationException = null;
przemekwitek marked this conversation as resolved.
Show resolved Hide resolved
for(String failure : config.getPivotConfig().aggFieldValidation()) {
validationException = addValidationError(failure, validationException);
}
return validationException;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.PipelineAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder;
import org.elasticsearch.xpack.core.dataframe.DataFrameField;
import org.elasticsearch.xpack.core.dataframe.utils.ExceptionsHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
Expand Down Expand Up @@ -144,4 +151,66 @@ public boolean isValid() {
public static PivotConfig fromXContent(final XContentParser parser, boolean lenient) throws IOException {
return lenient ? LENIENT_PARSER.apply(parser, null) : STRICT_PARSER.apply(parser, null);
}

/**
* Does the following checks:
*
* - determines if there are any full duplicate names between the aggregation names and the group by names.
* - finds if there are conflicting name paths that could cause a failure later when the config is started.
*
* Examples showing conflicting field name paths:
*
* aggName1: foo.bar.baz
* aggName2: foo.bar
*
* This should fail as aggName1 will cause foo.bar to be an object, causing a conflict with the use of foo.bar in aggName2.
*
* @return List of validation failure messages
*/
public List<String> aggFieldValidation() {
if ((aggregationConfig.isValid() && groups.isValid()) == false) {
return Collections.emptyList();
}

List<String> validationFailures = new ArrayList<>();
List<String> usedNames = new ArrayList<>();
// TODO this will need changed once we allow multi-bucket aggs + field merging
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"need changed" -> "need to be changed"?

aggregationConfig.getAggregatorFactories().forEach(agg -> addAggNames(agg, usedNames));
aggregationConfig.getPipelineAggregatorFactories().forEach(agg -> addAggNames(agg, usedNames));
usedNames.addAll(groups.getGroups().keySet());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might miss something, but wouldn't it be simpler to sort and then compare adjacent name pairs?
(of course you need logic to handle the dots)


Set<String> leafNames = new HashSet<>(usedNames.size());
for(String name : usedNames) {
if (leafNames.contains(name)) {
validationFailures.add("duplicate field [" + name + "] detected");
}
benwtrent marked this conversation as resolved.
Show resolved Hide resolved
leafNames.add(name);
}

for (String fullName : usedNames) {
String[] tokens = fullName.split("\\.");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you omit the dots, so what if I have foo.bar.baz and foobar? If I get it right, this would fail validation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. I need to fix that

for (int i = tokens.length - 1; i > 0; i--) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why do you iterate backwards and create a separate StringBuilder in each iteration?
I would go fo something like:
for (String fullName : usedNames) {
String[] tokens = fullName.split("\.");
StringBuilder prefix = new StringBuilder();
for each token:
prefix.append(token)
check in "leafNames"

I believe this code will be both more performant and easier to read. Please LMK if I'm missing something here.

StringBuilder prefix = new StringBuilder();
for (int j = 0; j < i; j++) {
prefix.append(tokens[j]);
}
String prefixString = prefix.toString();
if (leafNames.contains(prefixString)) {
validationFailures.add("field [" + prefixString + "] cannot be both an object and a field");
}
}
}
return validationFailures;
}


private static void addAggNames(AggregationBuilder aggregationBuilder, List<String> names) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could be just Collection<String> ?

names.add(aggregationBuilder.getName());
aggregationBuilder.getSubAggregations().forEach(agg -> addAggNames(agg, names));
przemekwitek marked this conversation as resolved.
Show resolved Hide resolved
aggregationBuilder.getPipelineAggregations().forEach(agg -> addAggNames(agg, names));
}

private static void addAggNames(PipelineAggregationBuilder pipelineAggregationBuilder, List<String> names) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: as above, could be just Collection ?

names.add(pipelineAggregationBuilder.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import org.elasticsearch.xpack.core.dataframe.transforms.AbstractSerializingDataFrameTestCase;

import java.io.IOException;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;

public class PivotConfigTests extends AbstractSerializingDataFrameTestCase<PivotConfig> {

Expand Down Expand Up @@ -103,7 +106,7 @@ public void testEmptyGroupBy() throws IOException {
assertFalse(pivotConfig.isValid());
}

public void testMissingGroupBy() throws IOException {
public void testMissingGroupBy() {
String pivot = "{"
+ " \"aggs\": {"
+ " \"avg\": {"
Expand All @@ -114,7 +117,7 @@ public void testMissingGroupBy() throws IOException {
expectThrows(IllegalArgumentException.class, () -> createPivotConfigFromString(pivot, false));
}

public void testDoubleAggs() throws IOException {
public void testDoubleAggs() {
String pivot = "{"
+ " \"group_by\": {"
+ " \"id\": {"
Expand All @@ -136,6 +139,74 @@ public void testDoubleAggs() throws IOException {
expectThrows(IllegalArgumentException.class, () -> createPivotConfigFromString(pivot, false));
}

public void testAggNameValidations() throws IOException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests are good, but I wonder if aggFieldValidation(...) could be re-factored in a way to test it at the unit test level with less boiler plate and more coverage?

String pivotAggs = "{"
+ " \"group_by\": {"
+ " \"user\": {"
+ " \"terms\": {"
+ " \"field\": \"id\""
+ "} } },"
+ " \"aggs\": {"
+ " \"user.avg\": {"
+ " \"avg\": {"
+ " \"field\": \"points\""
+ "} } } }";
PivotConfig pivotConfig = createPivotConfigFromString(pivotAggs, true);
assertTrue(pivotConfig.isValid());
List<String> fieldValidation = pivotConfig.aggFieldValidation();
assertFalse(fieldValidation.isEmpty());
assertThat(fieldValidation.get(0), equalTo("field [user] cannot be both an object and a field"));

pivotAggs = "{"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually it is a good idea to split such a long test method with independent tests into a few (here, 4) shorter methods. This makes the tests more "unit", thus increasing readability.

+ " \"group_by\": {"
+ " \"user\": {"
+ " \"terms\": {"
+ " \"field\": \"id\""
+ "} } },"
+ " \"aggs\": {"
+ " \"user\": {"
+ " \"avg\": {"
+ " \"field\": \"points\""
+ "} } } }";
pivotConfig = createPivotConfigFromString(pivotAggs, true);
assertTrue(pivotConfig.isValid());
fieldValidation = pivotConfig.aggFieldValidation();
assertFalse(fieldValidation.isEmpty());
assertThat(fieldValidation.get(0), equalTo("duplicate field [user] detected"));

pivotAggs = "{"
+ " \"group_by\": {"
+ " \"user\": {"
+ " \"terms\": {"
+ " \"field\": \"id\""
+ "} } },"
+ " \"aggs\": {"
+ " \"avg\": {"
+ " \"avg\": {"
+ " \"field\": \"points\""
+ "} } } }";
pivotConfig = createPivotConfigFromString(pivotAggs, true);
assertTrue(pivotConfig.isValid());
fieldValidation = pivotConfig.aggFieldValidation();
assertTrue(fieldValidation.isEmpty());

pivotAggs = "{"
+ " \"group_by\": {"
+ " \"user.id.field\": {"
+ " \"terms\": {"
+ " \"field\": \"id\""
+ "} } },"
+ " \"aggs\": {"
+ " \"avg.field.value\": {"
+ " \"avg\": {"
+ " \"field\": \"points\""
+ "} } } }";
pivotConfig = createPivotConfigFromString(pivotAggs, true);
assertTrue(pivotConfig.isValid());
fieldValidation = pivotConfig.aggFieldValidation();
assertTrue(fieldValidation.isEmpty());
}

private PivotConfig createPivotConfigFromString(String json, boolean lenient) throws IOException {
final XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry(),
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,35 @@ setup:
"aggs": {"avg_response": {"avg": {"field": "responsetime"}}}
}
}
---
"Test creation failures due to duplicate and conflicting field names":
- do:
catch: /duplicate field \[airline\] detected/
data_frame.put_data_frame_transform:
transform_id: "duplicate-field-transform"
body: >
{
"source": {
"index": "source-index"
},
"dest": { "index": "dest-index" },
"pivot": {
"group_by": { "airline": {"terms": {"field": "airline"}}},
"aggs": {"airline": {"avg": {"field": "responsetime"}}}
}
}
- do:
catch: /field \[airline\] cannot be both an object and a field/
data_frame.put_data_frame_transform:
transform_id: "duplicate-field-transform"
body: >
{
"source": {
"index": "source-index"
},
"dest": { "index": "dest-index" },
"pivot": {
"group_by": { "airline": {"terms": {"field": "airline"}}},
"aggs": {"airline.responsetime": {"avg": {"field": "responsetime"}}}
}
}