-
Notifications
You must be signed in to change notification settings - Fork 224
test(integ-test): stabilize stream-order commands across shards #5729
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
Open
mengweieric
wants to merge
3
commits into
opensearch-project:main
Choose a base branch
from
mengweieric:menwe/multi-shard-stream-commands
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,15 @@ | |
| import static org.opensearch.sql.util.MatcherUtils.*; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import org.json.JSONArray; | ||
| import org.json.JSONObject; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.opensearch.sql.ppl.PPLIntegTestCase; | ||
|
|
@@ -60,17 +69,25 @@ public void testDedupKeepEmpty() throws IOException { | |
| String.format( | ||
| "source=%s | dedup 1 name KEEPEMPTY=true | fields name, category", | ||
| TEST_INDEX_DUPLICATION_NULLABLE)); | ||
| verifyDataRows( | ||
| actual, | ||
| rows("A", "X"), | ||
| rows("B", "Z"), | ||
| rows("C", "X"), | ||
| rows("D", "Z"), | ||
| rows("E", null), | ||
| rows(null, "Y"), | ||
| rows(null, "X"), | ||
| rows(null, "Z"), | ||
| rows(null, null)); | ||
| // KEEPEMPTY keeps one row per distinct non-null name plus every null-name row. The null-name | ||
| // rows are fully determined (dedup keys on name only); the non-null representatives' category | ||
| // has no stable cross-shard tiebreaker, so assert one row per name with a valid pair. | ||
| List<List<Object>> rows = dataRows(actual); | ||
| assertEquals(9, rows.size()); | ||
| Set<Object> nonNullNames = new HashSet<>(); | ||
| Set<List<Object>> nullNameRows = new HashSet<>(); | ||
| for (List<Object> row : rows) { | ||
| Object name = row.get(0); | ||
| Object category = row.get(1); | ||
| if (name == null) { | ||
| nullNameRows.add(Arrays.asList(name, category)); | ||
| } else { | ||
| nonNullNames.add(name); | ||
| assertValidPair(name, category); | ||
| } | ||
| } | ||
|
Collaborator
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. does sort help?
Collaborator
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. Yes. I added sort name, category before dedup and replaced the permissive valid pair check with exact expected rows. |
||
| assertEquals(Set.of("A", "B", "C", "D", "E"), nonNullNames); | ||
| assertEquals(NULL_NAME_ROWS, nullNameRows); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -96,6 +113,15 @@ public void testDedupMultipleFieldsKeepEmpty() throws IOException { | |
| rows(null, null)); | ||
| } | ||
|
|
||
| /** | ||
| * {@code CONSECUTIVE=true} collapses only <em>adjacent</em> duplicates, so its result depends | ||
| * entirely on the row-encounter order. A multi-shard index has no stable merge order (the counts | ||
| * observed on a single shard, 8/12/12/16, become 12/... on five shards), so this test cannot run | ||
| * against a sharded index. Drive it from a coordinator-only {@code makeresults} literal instead: | ||
| * the literal fixes the encounter order to the historical {@code duplication_nullable} insertion | ||
| * sequence (name column, with nulls preserved), making the consecutive-dedup counts deterministic | ||
| * on every route while still exercising real CONSECUTIVE semantics. | ||
| */ | ||
| @Test | ||
| @RequiresCapability( | ||
| value = DEDUP_NONDETERMINISTIC, | ||
|
|
@@ -169,20 +195,25 @@ public void testDedupKeepEmpty2() throws IOException { | |
| String.format( | ||
| "source=%s | dedup 2 name KEEPEMPTY=true | fields name, category", | ||
| TEST_INDEX_DUPLICATION_NULLABLE)); | ||
| verifyDataRows( | ||
| actual, | ||
| rows("A", "X"), | ||
| rows("A", "Y"), | ||
| rows("B", "Z"), | ||
| rows("B", "Z"), | ||
| rows("C", "X"), | ||
| rows("C", "X"), | ||
| rows("D", "Z"), | ||
| rows("E", null), | ||
| rows(null, "Y"), | ||
| rows(null, "X"), | ||
| rows(null, "Z"), | ||
| rows(null, null)); | ||
| // dedup 2 keeps up to two rows per distinct non-null name (A/B/C have >=2, D/E have 1) plus | ||
| // every null-name row. Which two categories survive per name has no stable cross-shard | ||
| // tiebreaker, so assert the per-name kept-count, valid pairs, and the fixed null-name rows. | ||
| List<List<Object>> rows = dataRows(actual); | ||
| assertEquals(12, rows.size()); | ||
| Map<Object, Integer> nameCounts = new HashMap<>(); | ||
| Set<List<Object>> nullNameRows = new HashSet<>(); | ||
| for (List<Object> row : rows) { | ||
| Object name = row.get(0); | ||
| Object category = row.get(1); | ||
| if (name == null) { | ||
| nullNameRows.add(Arrays.asList(name, category)); | ||
| } else { | ||
| nameCounts.merge(name, 1, Integer::sum); | ||
| assertValidPair(name, category); | ||
| } | ||
| } | ||
| assertEquals(Map.of("A", 2, "B", 2, "C", 2, "D", 1, "E", 1), nameCounts); | ||
| assertEquals(NULL_NAME_ROWS, nullNameRows); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -264,20 +295,35 @@ public void testReorderDedupFieldsShouldNotAffectResult() throws IOException { | |
| public void testDedupComplex() throws IOException { | ||
| JSONObject actual = | ||
| executeQuery(String.format("source=%s | dedup 1 name", TEST_INDEX_DUPLICATION_NULLABLE)); | ||
| verifyDataRows( | ||
| actual, | ||
| rows("X", "A", 1), | ||
| rows("Z", "B", 1), | ||
| rows("X", "C", 1), | ||
| rows("Z", "D", 1), | ||
| rows(null, "E", 1)); | ||
| // dedup 1 name keeps one row per distinct non-null name. The surviving row's category (and any | ||
| // other non-key column) has no stable cross-shard tiebreaker, so assert the dedup invariant: | ||
| // exactly the five names, each once, and each surviving (name, category) is a real data pair. | ||
| List<List<Object>> byName = dataRows(actual); | ||
| assertEquals(5, byName.size()); | ||
| Set<Object> names = new HashSet<>(); | ||
| for (List<Object> row : byName) { | ||
| Object category = row.get(0); | ||
| Object name = row.get(1); | ||
| names.add(name); | ||
| assertEquals(1, ((Number) row.get(2)).intValue()); | ||
| assertValidPair(name, category); | ||
| } | ||
| assertEquals(Set.of("A", "B", "C", "D", "E"), names); | ||
| actual = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | fields category, name | dedup 1 name", | ||
| TEST_INDEX_DUPLICATION_NULLABLE)); | ||
| verifyDataRows( | ||
| actual, rows("X", "A"), rows("Z", "B"), rows("X", "C"), rows("Z", "D"), rows(null, "E")); | ||
| List<List<Object>> byNameProjected = dataRows(actual); | ||
| assertEquals(5, byNameProjected.size()); | ||
| Set<Object> projectedNames = new HashSet<>(); | ||
| for (List<Object> row : byNameProjected) { | ||
| Object category = row.get(0); | ||
| Object name = row.get(1); | ||
| projectedNames.add(name); | ||
| assertValidPair(name, category); | ||
| } | ||
| assertEquals(Set.of("A", "B", "C", "D", "E"), projectedNames); | ||
| actual = | ||
| executeQuery( | ||
| String.format("source=%s | dedup 1 name, category", TEST_INDEX_DUPLICATION_NULLABLE)); | ||
|
|
@@ -361,9 +407,13 @@ public void testDedupWithRenamedField() throws IOException { | |
| JSONObject actual = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | rename name as nm | dedup 1 category | fields category, nm", | ||
| "source=%s | where isnotnull(name) | rename name as nm | sort nm | dedup 1 category" | ||
| + " | fields category, nm", | ||
| TEST_INDEX_DUPLICATION_NULLABLE)); | ||
| // One representative row per category; nm must not be null | ||
| // Pin the surviving representative deterministically across shards: excluding null names and | ||
| // sorting by nm makes dedup keep the lexicographically-first name per category (X->A, Y->A, | ||
| // Z->B). This still exercises the #5150 fix (the renamed non-key field must resolve to a | ||
| // non-null value in the dedup top_hits response). | ||
| verifyDataRows(actual, rows("X", "A"), rows("Z", "B"), rows("Y", "A")); | ||
| } | ||
|
|
||
|
|
@@ -380,10 +430,12 @@ public void testDedupWithRenamedFieldMappingCollision() throws IOException { | |
| JSONObject actual = | ||
| executeQuery( | ||
| String.format( | ||
| "source=%s | eval nm2 = name | rename name as nm | dedup 1 category" | ||
| + " | fields category, nm, nm2", | ||
| "source=%s | where isnotnull(name) | eval nm2 = name | rename name as nm | sort nm" | ||
| + " | dedup 1 category | fields category, nm, nm2", | ||
| TEST_INDEX_DUPLICATION_NULLABLE)); | ||
| // Both nm (from rename) and nm2 (from eval col-ref) must carry the same non-null name value | ||
| // Pin the surviving representative deterministically across shards (see testDedupWithRenamed | ||
| // Field). Both nm (from rename) and nm2 (from the eval col-ref) must carry the same non-null | ||
| // name value, exercising the #5197 alias-collision fix. | ||
| verifyDataRows(actual, rows("X", "A", "A"), rows("Z", "B", "B"), rows("Y", "A", "A")); | ||
| } | ||
|
|
||
|
|
@@ -422,13 +474,22 @@ public void testDedupExpr() throws IOException { | |
| String.format( | ||
| "source=%s | eval new_name = lower(name) | dedup 1 new_name", | ||
| TEST_INDEX_DUPLICATION_NULLABLE)); | ||
| verifyDataRows( | ||
| actual, | ||
| rows("X", "A", 1, "a"), | ||
| rows("Z", "B", 1, "b"), | ||
| rows("X", "C", 1, "c"), | ||
| rows("Z", "D", 1, "d"), | ||
| rows(null, "E", 1, "e")); | ||
| // dedup 1 new_name keeps one row per distinct lower(name). name and new_name are fully | ||
| // determined (a<->A ...), but the surviving category has no stable cross-shard tiebreaker; | ||
| // assert the five keys, the derived-column relation, and a valid (name, category) pair. | ||
| List<List<Object>> byNewName = dataRows(actual); | ||
| assertEquals(5, byNewName.size()); | ||
| Set<Object> newNames = new HashSet<>(); | ||
| for (List<Object> row : byNewName) { | ||
| Object category = row.get(0); | ||
| Object name = row.get(1); | ||
| Object newName = row.get(3); | ||
| newNames.add(newName); | ||
| assertEquals(1, ((Number) row.get(2)).intValue()); | ||
| assertEquals(((String) name).toLowerCase(Locale.ROOT), newName); | ||
| assertValidPair(name, category); | ||
| } | ||
| assertEquals(Set.of("a", "b", "c", "d", "e"), newNames); | ||
| actual = | ||
| executeQuery( | ||
| String.format( | ||
|
|
@@ -481,4 +542,54 @@ public void testDedupExpr() throws IOException { | |
| rows("Z", 1, "B", "b", "z"), | ||
| rows("Z", 1, "B", "b", "z")); | ||
| } | ||
|
|
||
| // ---- Multi-shard dedup helpers ------------------------------------------------------------ | ||
|
|
||
| /** | ||
| * Every (name, category) pair with a non-null name present in the {@code duplication_nullable} | ||
| * dataset. dedup keeps one (or N) representative row(s) per key; because the merge has no stable | ||
| * cross-shard tiebreaker, tests assert the surviving row is a real pair rather than a fixed one. | ||
| */ | ||
| private static final Set<List<Object>> VALID_NAME_CATEGORY = | ||
| Set.of( | ||
| Arrays.asList("A", "X"), | ||
| Arrays.asList("A", "Y"), | ||
| Arrays.asList("B", "Z"), | ||
| Arrays.asList("B", "Y"), | ||
| Arrays.asList("B", null), | ||
| Arrays.asList("C", "X"), | ||
| Arrays.asList("D", "Z"), | ||
| Arrays.asList("E", null)); | ||
|
|
||
| /** | ||
| * The null-name rows kept by {@code KEEPEMPTY=true} are fully determined: dedup keys on name | ||
| * only, so every null-name document survives, one per distinct category those rows carry. | ||
| */ | ||
| private static final Set<List<Object>> NULL_NAME_ROWS = | ||
| Set.of( | ||
| Arrays.asList(null, "Y"), | ||
| Arrays.asList(null, "X"), | ||
| Arrays.asList(null, "Z"), | ||
| Arrays.asList(null, null)); | ||
|
|
||
| private static void assertValidPair(Object name, Object category) { | ||
| assertTrue( | ||
| "unexpected surviving (name, category) = (" + name + ", " + category + ")", | ||
| VALID_NAME_CATEGORY.contains(Arrays.asList(name, category))); | ||
| } | ||
|
|
||
| /** Materialize {@code datarows} into a list of rows, mapping JSON null to Java {@code null}. */ | ||
| private static List<List<Object>> dataRows(JSONObject response) { | ||
| List<List<Object>> rows = new ArrayList<>(); | ||
| JSONArray arr = response.getJSONArray("datarows"); | ||
| for (int i = 0; i < arr.length(); i++) { | ||
| JSONArray r = arr.getJSONArray(i); | ||
| List<Object> row = new ArrayList<>(); | ||
| for (int j = 0; j < r.length(); j++) { | ||
| row.add(r.isNull(j) ? null : r.get(j)); | ||
| } | ||
| rows.add(row); | ||
| } | ||
| return rows; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The test set not human readable. 2 ideas
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.
Updated this to use max(content), which makes the sample deterministic across shards. verifyDataRows already performs an order insensitive comparison.