|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.arrow.algorithm.deduplicate; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 22 | + |
| 23 | +import org.apache.arrow.memory.BufferAllocator; |
| 24 | +import org.apache.arrow.memory.RootAllocator; |
| 25 | +import org.apache.arrow.util.DataSizeRoundingUtil; |
| 26 | +import org.apache.arrow.vector.BitVectorHelper; |
| 27 | +import org.apache.arrow.vector.IntVector; |
| 28 | +import org.apache.arrow.vector.VarCharVector; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +import io.netty.buffer.ArrowBuf; |
| 34 | + |
| 35 | +/** |
| 36 | + * Test cases for {@link DeduplicationUtils}. |
| 37 | + */ |
| 38 | +public class TestDeduplicationUtils { |
| 39 | + |
| 40 | + private static final int VECTOR_LENGTH = 100; |
| 41 | + |
| 42 | + private static final int REPETITION_COUNT = 3; |
| 43 | + |
| 44 | + private BufferAllocator allocator; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void prepare() { |
| 48 | + allocator = new RootAllocator(1024 * 1024); |
| 49 | + } |
| 50 | + |
| 51 | + @After |
| 52 | + public void shutdown() { |
| 53 | + allocator.close(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testDeduplicateFixedWidth() { |
| 58 | + try (IntVector origVec = new IntVector("original vec", allocator); |
| 59 | + IntVector dedupVec = new IntVector("deduplicated vec", allocator); |
| 60 | + IntVector lengthVec = new IntVector("length vec", allocator); |
| 61 | + ArrowBuf distinctBuf = allocator.buffer( |
| 62 | + DataSizeRoundingUtil.divideBy8Ceil(VECTOR_LENGTH * REPETITION_COUNT))) { |
| 63 | + origVec.allocateNew(VECTOR_LENGTH * REPETITION_COUNT); |
| 64 | + origVec.setValueCount(VECTOR_LENGTH * REPETITION_COUNT); |
| 65 | + lengthVec.allocateNew(); |
| 66 | + |
| 67 | + // prepare data |
| 68 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 69 | + for (int j = 0; j < REPETITION_COUNT; j++) { |
| 70 | + origVec.set(i * REPETITION_COUNT + j, i); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + DeduplicationUtils.populateRunStartIndicators(origVec, distinctBuf); |
| 75 | + assertEquals( VECTOR_LENGTH, |
| 76 | + VECTOR_LENGTH * REPETITION_COUNT - |
| 77 | + BitVectorHelper.getNullCount(distinctBuf, VECTOR_LENGTH * REPETITION_COUNT)); |
| 78 | + |
| 79 | + DeduplicationUtils.populateDeduplicatedValues(distinctBuf, origVec, dedupVec); |
| 80 | + assertEquals(VECTOR_LENGTH, dedupVec.getValueCount()); |
| 81 | + |
| 82 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 83 | + assertEquals(i, dedupVec.get(i)); |
| 84 | + } |
| 85 | + |
| 86 | + DeduplicationUtils.populateRunLengths(distinctBuf, lengthVec, VECTOR_LENGTH * REPETITION_COUNT); |
| 87 | + assertEquals(VECTOR_LENGTH, lengthVec.getValueCount()); |
| 88 | + |
| 89 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 90 | + assertEquals(REPETITION_COUNT, lengthVec.get(i)); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testDeduplicateVariableWidth() { |
| 97 | + try (VarCharVector origVec = new VarCharVector("original vec", allocator); |
| 98 | + VarCharVector dedupVec = new VarCharVector("deduplicated vec", allocator); |
| 99 | + IntVector lengthVec = new IntVector("length vec", allocator); |
| 100 | + ArrowBuf distinctBuf = allocator.buffer( |
| 101 | + DataSizeRoundingUtil.divideBy8Ceil(VECTOR_LENGTH * REPETITION_COUNT))) { |
| 102 | + origVec.allocateNew( |
| 103 | + VECTOR_LENGTH * REPETITION_COUNT * 10, VECTOR_LENGTH * REPETITION_COUNT); |
| 104 | + origVec.setValueCount(VECTOR_LENGTH * REPETITION_COUNT); |
| 105 | + lengthVec.allocateNew(); |
| 106 | + |
| 107 | + // prepare data |
| 108 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 109 | + String str = String.valueOf(i * i); |
| 110 | + for (int j = 0; j < REPETITION_COUNT; j++) { |
| 111 | + origVec.set(i * REPETITION_COUNT + j, str.getBytes()); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + DeduplicationUtils.populateRunStartIndicators(origVec, distinctBuf); |
| 116 | + assertEquals(VECTOR_LENGTH, |
| 117 | + VECTOR_LENGTH * REPETITION_COUNT - |
| 118 | + BitVectorHelper.getNullCount(distinctBuf, VECTOR_LENGTH * REPETITION_COUNT)); |
| 119 | + |
| 120 | + DeduplicationUtils.populateDeduplicatedValues(distinctBuf, origVec, dedupVec); |
| 121 | + assertEquals(VECTOR_LENGTH, dedupVec.getValueCount()); |
| 122 | + |
| 123 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 124 | + assertArrayEquals(String.valueOf(i * i).getBytes(), dedupVec.get(i)); |
| 125 | + } |
| 126 | + |
| 127 | + DeduplicationUtils.populateRunLengths( |
| 128 | + distinctBuf, lengthVec, VECTOR_LENGTH * REPETITION_COUNT); |
| 129 | + assertEquals(VECTOR_LENGTH, lengthVec.getValueCount()); |
| 130 | + |
| 131 | + for (int i = 0; i < VECTOR_LENGTH; i++) { |
| 132 | + assertEquals(REPETITION_COUNT, lengthVec.get(i)); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments