-
Notifications
You must be signed in to change notification settings - Fork 26k
LogsDB qa tests - add specific matcher for source #111568
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.datastreams.logsdb.qa.matchers.source; | ||
|
|
||
| import org.apache.lucene.sandbox.document.HalfFloatPoint; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public interface FieldSpecificMatcher { | ||
| boolean match(List<Object> actual, List<Object> expected); | ||
|
|
||
| class HalfFloatMatcher implements FieldSpecificMatcher { | ||
| public boolean match(List<Object> actual, List<Object> expected) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Override? |
||
| var actualHalfFloatBytes = normalize(actual); | ||
| var expectedHalfFloatBytes = normalize(expected); | ||
|
|
||
| return actualHalfFloatBytes.equals(expectedHalfFloatBytes); | ||
| } | ||
|
|
||
| private static Set<Short> normalize(List<Object> values) { | ||
| if (values == null) { | ||
| return Set.of(); | ||
| } | ||
|
|
||
| Function<Object, Float> toFloat = (o) -> o instanceof Number n ? n.floatValue() : Float.parseFloat((String) o); | ||
| return values.stream() | ||
| .filter(Objects::nonNull) | ||
| .map(toFloat) | ||
| // Based on logic in NumberFieldMapper | ||
| .map(HalfFloatPoint::halfFloatToSortableShort) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| package org.elasticsearch.datastreams.logsdb.qa.matchers.source; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| class MappingTransforms { | ||
| /** | ||
| * Normalize mapping to have the same structure as normalized source and enable field mapping lookup. | ||
| * Similar to {@link SourceTransforms#normalize(Map)} but needs to get rid of intermediate nodes | ||
| * and collect results into a different data structure. | ||
| * | ||
| * @param map | ||
| * @return | ||
| */ | ||
| public static Map<String, Map<String, Object>> normalizeMapping(Map<String, Object> map) { | ||
| var flattened = new HashMap<String, Map<String, Object>>(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm somewhat puzzled here, why not just use HashMap<String, Object> to track just the leaf fields?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, you do that for source checking while you also want to compare the mappings. Need to think a bit about it..
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a map from normalized field name ( |
||
|
|
||
| descend(null, map, flattened); | ||
|
|
||
| return flattened; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static void descend(String pathFromRoot, Map<String, Object> currentLevel, Map<String, Map<String, Object>> flattened) { | ||
| for (var entry : currentLevel.entrySet()) { | ||
| if (entry.getKey().equals("_doc") || entry.getKey().equals("properties")) { | ||
| descend(pathFromRoot, (Map<String, Object>) entry.getValue(), flattened); | ||
| } else { | ||
| if (entry.getValue() instanceof Map<?, ?> map) { | ||
| var pathToField = pathFromRoot == null ? entry.getKey() : pathFromRoot + "." + entry.getKey(); | ||
| descend(pathToField, (Map<String, Object>) map, flattened); | ||
| } else { | ||
| if (pathFromRoot == null) { | ||
| // Ignore top level mapping parameters for now | ||
| continue; | ||
| } | ||
|
|
||
| flattened.putIfAbsent(pathFromRoot, new HashMap<>()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: combine the two lines:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neat, didn't expect this from java. |
||
| flattened.get(pathFromRoot).put(entry.getKey(), entry.getValue()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this method return a MatchResult instead of a boolean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to do that but to produce full message in case there is a mismatch you'll need to pass mapping and settings here which ends up being a mouthful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it's not a big deal.