generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 181
Return comparable LinkedHashMap in valueForCalcite() of ExprTupleValue
#4629
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
Merged
qianheng-aws
merged 2 commits into
opensearch-project:main
from
LantaoJin:pr/issues/4339
Oct 23, 2025
Merged
Changes from all commits
Commits
Show all changes
2 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
62 changes: 62 additions & 0 deletions
62
core/src/main/java/org/opensearch/sql/data/utils/ComparableLinkedHashMap.java
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 |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.data.utils; | ||
|
|
||
| import java.util.Iterator; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class ComparableLinkedHashMap<K, V> extends LinkedHashMap<K, V> | ||
| implements Comparable<ComparableLinkedHashMap<K, V>> { | ||
|
|
||
| public ComparableLinkedHashMap() { | ||
| super(); | ||
| } | ||
|
|
||
| public ComparableLinkedHashMap(int initialCapacity) { | ||
| super(initialCapacity); | ||
| } | ||
|
|
||
| public ComparableLinkedHashMap(int initialCapacity, float loadFactor) { | ||
| super(initialCapacity, loadFactor); | ||
| } | ||
|
|
||
| @Override | ||
| public int compareTo(ComparableLinkedHashMap<K, V> other) { | ||
| if (this.isEmpty() && other.isEmpty()) return 0; | ||
| if (this.isEmpty()) return -1; | ||
| if (other.isEmpty()) return 1; | ||
| Iterator<Map.Entry<K, V>> thisIterator = this.entrySet().iterator(); | ||
| Iterator<Map.Entry<K, V>> otherIterator = other.entrySet().iterator(); | ||
| return compareRecursive(thisIterator, otherIterator); | ||
| } | ||
|
|
||
| private int compareRecursive( | ||
| Iterator<Map.Entry<K, V>> thisIterator, Iterator<Map.Entry<K, V>> otherIterator) { | ||
| boolean thisHasNext = thisIterator.hasNext(); | ||
| boolean otherHasNext = otherIterator.hasNext(); | ||
| if (!thisHasNext && !otherHasNext) return 0; | ||
| if (!thisHasNext) return -1; | ||
| if (!otherHasNext) return 1; | ||
|
|
||
| V thisValue = thisIterator.next().getValue(); | ||
| V otherValue = otherIterator.next().getValue(); | ||
| int comparison = compareValues(thisValue, otherValue); | ||
| if (comparison != 0) return comparison; | ||
| return compareRecursive(thisIterator, otherIterator); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private int compareValues(V value1, V value2) { | ||
| if (value1 == null && value2 == null) return 0; | ||
| if (value1 == null) return -1; | ||
| if (value2 == null) return 1; | ||
| if (value1 instanceof Comparable) { | ||
| return ((Comparable<V>) value1).compareTo(value2); | ||
| } | ||
| return value1.toString().compareTo(value2.toString()); | ||
| } | ||
| } |
258 changes: 258 additions & 0 deletions
258
core/src/test/java/org/opensearch/sql/utils/ComparableLinkedHashMapTest.java
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 |
|---|---|---|
| @@ -0,0 +1,258 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.utils; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.Comparator; | ||
| import java.util.Iterator; | ||
| import java.util.TreeSet; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.opensearch.sql.data.utils.ComparableLinkedHashMap; | ||
|
|
||
| public class ComparableLinkedHashMapTest { | ||
|
|
||
| @Test | ||
| public void testEmptyMaps() { | ||
| ComparableLinkedHashMap<String, Integer> map1 = new ComparableLinkedHashMap<>(); | ||
| ComparableLinkedHashMap<String, Integer> map2 = new ComparableLinkedHashMap<>(); | ||
|
|
||
| assertEquals(0, map1.compareTo(map2)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOneEmptyMap() { | ||
| ComparableLinkedHashMap<String, Integer> map1 = new ComparableLinkedHashMap<>(); | ||
| ComparableLinkedHashMap<String, Integer> map2 = new ComparableLinkedHashMap<>(); | ||
| map2.put("a", 1); | ||
|
|
||
| assertTrue(map1.compareTo(map2) < 0); | ||
| assertTrue(map2.compareTo(map1) > 0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEqualMaps() { | ||
| ComparableLinkedHashMap<String, Integer> map1 = new ComparableLinkedHashMap<>(); | ||
| map1.put("a", 1); | ||
| map1.put("b", 2); | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map2 = new ComparableLinkedHashMap<>(); | ||
| map2.put("c", 1); | ||
| map2.put("d", 2); | ||
|
|
||
| assertEquals(0, map1.compareTo(map2)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDifferentFirstValue() { | ||
| ComparableLinkedHashMap<String, Integer> map1 = new ComparableLinkedHashMap<>(); | ||
| map1.put("a", 1); | ||
| map1.put("b", 2); | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map2 = new ComparableLinkedHashMap<>(); | ||
| map2.put("c", 3); | ||
| map2.put("d", 2); | ||
|
|
||
| assertTrue(map1.compareTo(map2) < 0); | ||
| assertTrue(map2.compareTo(map1) > 0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDifferentLaterValue() { | ||
| ComparableLinkedHashMap<String, Integer> map1 = new ComparableLinkedHashMap<>(); | ||
| map1.put("a", 1); | ||
| map1.put("b", 2); | ||
| map1.put("c", 3); | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map2 = new ComparableLinkedHashMap<>(); | ||
| map2.put("d", 1); | ||
| map2.put("e", 3); | ||
| map2.put("f", 3); | ||
|
|
||
| assertTrue(map1.compareTo(map2) < 0); | ||
| assertTrue(map2.compareTo(map1) > 0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDifferentSizes() { | ||
| ComparableLinkedHashMap<String, Integer> map1 = new ComparableLinkedHashMap<>(); | ||
| map1.put("a", 1); | ||
| map1.put("b", 2); | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map2 = new ComparableLinkedHashMap<>(); | ||
| map2.put("c", 1); | ||
|
|
||
| assertTrue(map1.compareTo(map2) > 0); | ||
| assertTrue(map2.compareTo(map1) < 0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNullValues() { | ||
| ComparableLinkedHashMap<String, Integer> map1 = new ComparableLinkedHashMap<>(); | ||
| map1.put("a", null); | ||
| map1.put("b", 2); | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map2 = new ComparableLinkedHashMap<>(); | ||
| map2.put("c", 1); | ||
| map2.put("d", 2); | ||
|
|
||
| assertTrue(map1.compareTo(map2) < 0); | ||
| assertTrue(map2.compareTo(map1) > 0); | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map3 = new ComparableLinkedHashMap<>(); | ||
| map3.put("e", null); | ||
| map3.put("f", 2); | ||
|
|
||
| assertEquals(0, map1.compareTo(map3)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCustomObjects() { | ||
| class Person { | ||
| String name; | ||
|
|
||
| Person(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return name; | ||
| } | ||
| } | ||
|
|
||
| ComparableLinkedHashMap<String, Person> map1 = new ComparableLinkedHashMap<>(); | ||
| map1.put("a", new Person("Alice")); | ||
| map1.put("b", new Person("Bob")); | ||
|
|
||
| ComparableLinkedHashMap<String, Person> map2 = new ComparableLinkedHashMap<>(); | ||
| map2.put("c", new Person("Alice")); | ||
| map2.put("d", new Person("Charlie")); | ||
|
|
||
| assertTrue(map1.compareTo(map2) < 0); | ||
| assertTrue(map2.compareTo(map1) > 0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testMixedTypes() { | ||
| ComparableLinkedHashMap<String, Object> map1 = new ComparableLinkedHashMap<>(); | ||
| map1.put("a", 1); | ||
| map1.put("b", "test"); | ||
|
|
||
| ComparableLinkedHashMap<String, Object> map2 = new ComparableLinkedHashMap<>(); | ||
| map2.put("c", 1); | ||
| map2.put("d", "test"); | ||
|
|
||
| assertEquals(0, map1.compareTo(map2)); | ||
|
|
||
| ComparableLinkedHashMap<String, Object> map3 = new ComparableLinkedHashMap<>(); | ||
| map3.put("e", 1); | ||
| map3.put("f", "test2"); | ||
|
|
||
| assertTrue(map1.compareTo(map3) < 0); | ||
| assertTrue(map3.compareTo(map1) > 0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWithTreeSet() { | ||
| TreeSet<ComparableLinkedHashMap<String, Integer>> set = new TreeSet<>(); | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map1 = new ComparableLinkedHashMap<>(); | ||
| map1.put("a", 1); | ||
| map1.put("b", 2); | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map2 = new ComparableLinkedHashMap<>(); | ||
| map2.put("c", 1); | ||
| map2.put("d", 3); | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map3 = new ComparableLinkedHashMap<>(); | ||
| map3.put("e", 0); | ||
| map3.put("f", 4); | ||
|
|
||
| set.add(map2); | ||
| set.add(map1); | ||
| set.add(map3); | ||
|
|
||
| Iterator<ComparableLinkedHashMap<String, Integer>> iterator = set.iterator(); | ||
| ComparableLinkedHashMap<String, Integer> first = iterator.next(); | ||
| ComparableLinkedHashMap<String, Integer> second = iterator.next(); | ||
| ComparableLinkedHashMap<String, Integer> third = iterator.next(); | ||
|
|
||
| assertEquals(Integer.valueOf(0), first.get("e")); | ||
| assertEquals(Integer.valueOf(4), first.get("f")); | ||
|
|
||
| assertEquals(Integer.valueOf(1), second.get("a")); | ||
| assertEquals(Integer.valueOf(2), second.get("b")); | ||
|
|
||
| assertEquals(Integer.valueOf(1), third.get("c")); | ||
| assertEquals(Integer.valueOf(3), third.get("d")); | ||
|
|
||
| assertEquals(3, set.size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWithComparator() { | ||
| Comparator<ComparableLinkedHashMap<String, Integer>> comparator = | ||
| ComparableLinkedHashMap::compareTo; | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map1 = new ComparableLinkedHashMap<>(); | ||
| map1.put("a", 5); | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map2 = new ComparableLinkedHashMap<>(); | ||
| map2.put("b", 3); | ||
|
|
||
| assertTrue(comparator.compare(map1, map2) > 0); | ||
| assertTrue(comparator.compare(map2, map1) < 0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEqualValuesDifferentKeys() { | ||
| ComparableLinkedHashMap<String, Integer> map1 = new ComparableLinkedHashMap<>(); | ||
| map1.put("a", 1); | ||
| map1.put("b", 2); | ||
| map1.put("c", 3); | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map2 = new ComparableLinkedHashMap<>(); | ||
| map2.put("d", 1); | ||
| map2.put("e", 2); | ||
| map2.put("f", 3); | ||
|
|
||
| assertEquals(0, map1.compareTo(map2)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDifferentOrder() { | ||
| ComparableLinkedHashMap<String, Integer> map1 = new ComparableLinkedHashMap<>(); | ||
| map1.put("a", 1); | ||
| map1.put("b", 2); | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map2 = new ComparableLinkedHashMap<>(); | ||
| map2.put("c", 2); | ||
| map2.put("d", 1); | ||
|
|
||
| assertTrue(map1.compareTo(map2) < 0); | ||
| assertTrue(map2.compareTo(map1) > 0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testLargeMaps() { | ||
| ComparableLinkedHashMap<String, Integer> map1 = new ComparableLinkedHashMap<>(); | ||
| for (int i = 0; i < 100; i++) { | ||
| map1.put("key" + i, i); | ||
| } | ||
|
|
||
| ComparableLinkedHashMap<String, Integer> map2 = new ComparableLinkedHashMap<>(); | ||
| for (int i = 0; i < 100; i++) { | ||
| map2.put("differentKey" + i, i); | ||
| } | ||
|
|
||
| assertEquals(0, map1.compareTo(map2)); | ||
|
|
||
| map2.put("differentKey99", 100); | ||
| assertTrue(map1.compareTo(map2) < 0); | ||
| } | ||
| } | ||
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.
Can the keys of ExprTupleValues be ignored? Or are they guaranteed to come with the same order?