rdblue commented on code in PR #16747: URL: https://github.com/apache/iceberg/pull/16747#discussion_r4010724310
########## core/src/test/java/org/apache/iceberg/mumbling/TestMumblingBitmap.java: ########## @@ -0,0 +1,352 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.iceberg.mumbling; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.nio.ByteBuffer; +import java.util.Arrays; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.junit.jupiter.api.Test; + +class TestMumblingBitmap { + + @Test + void testEmptyBitmap() { + MumblingBitmap bitmap = bitmap(); + assertThat(bitmap.cardinality()).isEqualTo(0); + + // all positions beyond the bitmap range are false + assertThat(bitmap.isSet(0)).isFalse(); + assertThat(bitmap.isSet(255)).isFalse(); + assertThat(bitmap.isSet(256)).isFalse(); + } + + @Test + void testInvalidPosition() { + MumblingBitmap bitmap = bitmap(); + assertThat(bitmap.cardinality()).isEqualTo(0); + assertThat(bitmap.isSet(0)).isFalse(); + assertThatThrownBy(() -> bitmap.isSet(-1)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Invalid bit position: -1 < 0"); + } + + @Test + void testEmptySparseContainer() { + MumblingBitmap bitmap = bitmap(sparse()); + assertThat(bitmap.cardinality()).isEqualTo(0); + assertThat(bitmap.isSet(0)).isFalse(); + assertThat(bitmap.isSet(100)).isFalse(); + assertThat(bitmap.isSet(255)).isFalse(); + } + + @Test + void testSparseContainerSetPositions() { + MumblingBitmap bitmap = bitmap(sparse(0, 5, 100, 255)); + assertThat(bitmap.cardinality()).isEqualTo(4); + + assertThat(bitmap.isSet(0)).isTrue(); + assertThat(bitmap.isSet(5)).isTrue(); + assertThat(bitmap.isSet(100)).isTrue(); + assertThat(bitmap.isSet(255)).isTrue(); + + assertThat(bitmap.isSet(1)).isFalse(); + assertThat(bitmap.isSet(4)).isFalse(); + assertThat(bitmap.isSet(6)).isFalse(); + assertThat(bitmap.isSet(99)).isFalse(); + assertThat(bitmap.isSet(101)).isFalse(); + assertThat(bitmap.isSet(254)).isFalse(); + assertThat(bitmap.isSet(256)).isFalse(); + } + + @Test + void testFullSparseContainer() { + int[] positions = new int[31]; + for (int i = 0; i < 31; i += 1) { + positions[i] = i * 8; // 0, 8, 16, ..., 240 + } + + MumblingBitmap bitmap = bitmap(sparse(positions)); + assertThat(bitmap.cardinality()).isEqualTo(31); + + for (int p : positions) { + assertThat(bitmap.isSet(p)).isTrue(); + } + + assertThat(bitmap.isSet(1)).isFalse(); + assertThat(bitmap.isSet(7)).isFalse(); + assertThat(bitmap.isSet(255)).isFalse(); + } + + @Test + void testFullDenseContainer() { + byte[] container = new byte[32]; + Arrays.fill(container, (byte) 0xFF); + + MumblingBitmap bitmap = bitmap(dense(container)); + assertThat(bitmap.cardinality()).isEqualTo(256); + + for (int i = 0; i < 256; i += 1) { + assertThat(bitmap.isSet(i)).isTrue(); + } + + assertThat(bitmap.isSet(256)).isFalse(); + } + + // Example 1: positions 0-31: `FF FF FF FF 00 ... 00` + @Test + void testDenseSpecExample1() { + byte[] container = new byte[32]; + container[0] = (byte) 0xFF; + container[1] = (byte) 0xFF; + container[2] = (byte) 0xFF; + container[3] = (byte) 0xFF; + MumblingBitmap bitmap = bitmap(dense(container)); + assertThat(bitmap.cardinality()).isEqualTo(32); + + for (int i = 0; i <= 31; i += 1) { + assertThat(bitmap.isSet(i)).isTrue(); + } + + assertThat(bitmap.isSet(32)).isFalse(); + assertThat(bitmap.isSet(255)).isFalse(); + } + + // Example 2: positions 0-32: `FF FF FF FF 80 00 ... 00` + @Test + void testDenseSpecExample2() { + byte[] container = new byte[32]; + container[0] = (byte) 0xFF; + container[1] = (byte) 0xFF; + container[2] = (byte) 0xFF; + container[3] = (byte) 0xFF; + container[4] = (byte) 0x80; + + MumblingBitmap bitmap = bitmap(dense(container)); + assertThat(bitmap.cardinality()).isEqualTo(33); + + for (int i = 0; i <= 32; i += 1) { + assertThat(bitmap.isSet(i)).isTrue(); + } + + assertThat(bitmap.isSet(33)).isFalse(); + assertThat(bitmap.isSet(255)).isFalse(); + } + + // Example 3: positions 0-15 and 240-255: `FF FF 00 ... 00 FF FF` + @Test + void testDenseSpecExample3() { + byte[] container = new byte[32]; + container[0] = (byte) 0xFF; + container[1] = (byte) 0xFF; + container[30] = (byte) 0xFF; + container[31] = (byte) 0xFF; + + MumblingBitmap bitmap = bitmap(dense(container)); + assertThat(bitmap.cardinality()).isEqualTo(32); + + for (int i = 0; i <= 15; i += 1) { + assertThat(bitmap.isSet(i)).isTrue(); + } + for (int i = 240; i <= 255; i += 1) { + assertThat(bitmap.isSet(i)).isTrue(); + } + assertThat(bitmap.isSet(16)).isFalse(); + assertThat(bitmap.isSet(239)).isFalse(); + assertThat(bitmap.isSet(256)).isFalse(); + } + + // Example 4: even positions 0, 2, 4, ...: `AA AA ... AA AA` + @Test + void testDenseSpecExample4() { + byte[] container = new byte[32]; + Arrays.fill(container, (byte) 0xAA); + + MumblingBitmap bitmap = bitmap(dense(container)); + assertThat(bitmap.cardinality()).isEqualTo(128); + + for (int i = 0; i < 256; i += 1) { + assertThat(bitmap.isSet(i)).isEqualTo(i % 2 == 0); + } + + assertThat(bitmap.isSet(256)).isFalse(); + } + + @Test + void testMultipleContainers() { + MumblingBitmap bitmap = bitmap(sparse(5), sparse(), sparse(10)); + assertThat(bitmap.cardinality()).isEqualTo(2); + + assertThat(bitmap.isSet(5)).isTrue(); // container 0, pos 5 + assertThat(bitmap.isSet(256)).isFalse(); // container 1 + assertThat(bitmap.isSet(522)).isTrue(); // container 2, pos 10 + + assertThat(bitmap.isSet(512)).isFalse(); + assertThat(bitmap.isSet(4)).isFalse(); + assertThat(bitmap.isSet(265)).isFalse(); + assertThat(bitmap.isSet(267)).isFalse(); + } + + @Test + void testMixedSparseAndDense() { + byte[] denseContainer = new byte[32]; + denseContainer[0] = (byte) 0xFF; + denseContainer[1] = (byte) 0xFF; + denseContainer[2] = (byte) 0xFF; + denseContainer[3] = (byte) 0xFF; + + MumblingBitmap bitmap = bitmap(dense(denseContainer), sparse(1)); + assertThat(bitmap.cardinality()).isEqualTo(33); + + for (int i = 0; i < 32; i += 1) { + assertThat(bitmap.isSet(i)).isTrue(); + } + assertThat(bitmap.isSet(32)).isFalse(); + + assertThat(bitmap.isSet(256)).isFalse(); + assertThat(bitmap.isSet(257)).isTrue(); // container 1, pos 1 + assertThat(bitmap.isSet(258)).isFalse(); + } + + @Test + void testBufferWithOffset() { + // Prepend 4 bytes of garbage before the actual bitmap data + ByteBuffer buffer = build(sparse(42)); + byte[] rawBytes = new byte[buffer.remaining()]; + buffer.get(rawBytes); + + ByteBuffer padded = ByteBuffer.allocate(4 + rawBytes.length); + padded.position(4); + padded.put(rawBytes); + padded.position(4); // position the buffer at the start of bitmap data + + MumblingBitmap bitmap = new MumblingBitmap(padded); + assertThat(bitmap.cardinality()).isEqualTo(1); + + assertThat(bitmap.isSet(41)).isFalse(); + assertThat(bitmap.isSet(42)).isTrue(); + assertThat(bitmap.isSet(43)).isFalse(); + } + + private static Container sparse(int... positions) { + byte[] bytes = new byte[positions.length]; + for (int i = 0; i < positions.length; i += 1) { + if (i > 0) { + Preconditions.checkArgument( + positions[i] < 256, "Invalid position in container: %s", positions[i]); + Preconditions.checkArgument( + positions[i] > positions[i - 1], + "Invalid sparse container: pos %s=%s >= pos %s=%s", + i - 1, + positions[i - 1], + i, + positions[i]); + } + + bytes[i] = (byte) positions[i]; + } + + return new Container(bytes); + } + + /** Descriptor + bytes for a dense container. */ + private static Container dense(byte[] container) { + Preconditions.checkArgument(container.length == 32, "Dense container must be 32 bytes"); + return new Container(container); + } + + private static class Container { + private final byte[] bytes; + private final int descriptor; + private final int cardinality; + + Container(byte[] bytes) { + this.bytes = bytes; + this.descriptor = bytes.length; Review Comment: I just updated the logic based on @nssalian's comment that the container is either sparse (< 32) or dense (= 0x20). I think this is a good idea so that we don't accept invalid descriptors. As for the spec wording here, I think maybe we should just remove it. We may want to use those bits for a dense container later, but that would require a version bump. It also says that those bits must be 0. Implementations should probably make the decision about whether to ignore them or not, but the reference implementation should check that they are 0s. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
