This is an automated email from the ASF dual-hosted git repository.

mihaibudiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new 7a3b5e1a3f [CALCITE-7556] NameMultimap.remove(key, value) leaves an 
empty key bucket behind
7a3b5e1a3f is described below

commit 7a3b5e1a3f454aa6e4012e2262fc2f03af110490
Author: Mihai Budiu <[email protected]>
AuthorDate: Thu Jun 11 21:14:49 2026 -0700

    [CALCITE-7556] NameMultimap.remove(key, value) leaves an empty key bucket 
behind
    
    Signed-off-by: Mihai Budiu <[email protected]>
---
 .../src/main/java/org/apache/calcite/util/NameMultimap.java |  9 +++++----
 core/src/test/java/org/apache/calcite/util/UtilTest.java    | 13 +++++++++++++
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/util/NameMultimap.java 
b/core/src/main/java/org/apache/calcite/util/NameMultimap.java
index 8d1505746e..d37f5366c9 100644
--- a/core/src/main/java/org/apache/calcite/util/NameMultimap.java
+++ b/core/src/main/java/org/apache/calcite/util/NameMultimap.java
@@ -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;
@@ -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
diff --git a/core/src/test/java/org/apache/calcite/util/UtilTest.java 
b/core/src/test/java/org/apache/calcite/util/UtilTest.java
index 2d7ab0f606..7a6df9a5a0 100644
--- a/core/src/test/java/org/apache/calcite/util/UtilTest.java
+++ b/core/src/test/java/org/apache/calcite/util/UtilTest.java
@@ -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() {
+    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));
+  }
 }

Reply via email to