Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 5 additions & 4 deletions core/src/main/java/org/apache/calcite/util/NameMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package org.apache.calcite.util;

import org.apache.calcite.linq4j.function.Experimental;

import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.ArrayList;
Expand Down Expand Up @@ -71,13 +69,16 @@ public void put(String name, V v) {
/** Removes all entries that have the given case-sensitive key.
*
* @return Whether a value was removed */
@Experimental
public boolean remove(String key, V value) {
final List<V> list = map().get(key);
if (list == null) {
return false;
}
return list.remove(value);
boolean result = list.remove(value);
if (list.isEmpty()) {
map().remove(key);
}
return result;
}

/** Returns a map containing all the entries in this multimap that match the
Expand Down
13 changes: 13 additions & 0 deletions core/src/test/java/org/apache/calcite/util/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3292,4 +3292,17 @@ static String describe(Matcher m) {
m.describeTo(d);
return d.toString();
}

/** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-7556">[CALCITE-7556]
* NameMultimap.remove(key, value) leaves an empty key bucket behind</a>. */
@Test void testNameMultimapRemoveLastValueRemovesKey() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here can add a jira link.

final NameMultimap<Integer> map = new NameMultimap<>();
map.put("baz", 1);

assertTrue(map.remove("baz", 1));
assertThat(map.range("baz", true), hasSize(0));
assertThat(map.containsKey("baz", true), is(false));
assertThat(map.containsKey("BAZ", false), is(false));
assertThat(map.map(), aMapWithSize(0));
}
}
Loading