|
| 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.dictionary; |
| 19 | + |
| 20 | +import static junit.framework.TestCase.assertTrue; |
| 21 | +import static org.junit.Assert.assertNull; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | + |
| 24 | +import org.apache.arrow.memory.BufferAllocator; |
| 25 | +import org.apache.arrow.memory.RootAllocator; |
| 26 | +import org.apache.arrow.vector.IntVector; |
| 27 | +import org.apache.arrow.vector.VarCharVector; |
| 28 | + |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +/** |
| 34 | + * Test cases for {@link HashTableBasedDictionaryBuilder}. |
| 35 | + */ |
| 36 | +public class TestHashTableBasedDictionaryEncoder { |
| 37 | + |
| 38 | + private BufferAllocator allocator; |
| 39 | + |
| 40 | + @Before |
| 41 | + public void prepare() { |
| 42 | + allocator = new RootAllocator(1024 * 1024); |
| 43 | + } |
| 44 | + |
| 45 | + @After |
| 46 | + public void shutdown() { |
| 47 | + allocator.close(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testBuildVariableWidthDictionaryWithNull() { |
| 52 | + try (VarCharVector vec = new VarCharVector("", allocator); |
| 53 | + VarCharVector dictionary = new VarCharVector("", allocator)) { |
| 54 | + |
| 55 | + vec.allocateNew(100, 10); |
| 56 | + vec.setValueCount(10); |
| 57 | + |
| 58 | + dictionary.allocateNew(); |
| 59 | + |
| 60 | + // fill data |
| 61 | + vec.set(0, "hello".getBytes()); |
| 62 | + vec.set(1, "abc".getBytes()); |
| 63 | + vec.setNull(2); |
| 64 | + vec.set(3, "world".getBytes()); |
| 65 | + vec.set(4, "12".getBytes()); |
| 66 | + vec.set(5, "dictionary".getBytes()); |
| 67 | + vec.setNull(6); |
| 68 | + vec.set(7, "hello".getBytes()); |
| 69 | + vec.set(8, "good".getBytes()); |
| 70 | + vec.set(9, "abc".getBytes()); |
| 71 | + |
| 72 | + HashTableBasedDictionaryBuilder<VarCharVector> dictionaryBuilder = |
| 73 | + new HashTableBasedDictionaryBuilder<>(dictionary, true); |
| 74 | + |
| 75 | + int result = dictionaryBuilder.addValues(vec); |
| 76 | + |
| 77 | + assertEquals(7, result); |
| 78 | + assertEquals(7, dictionary.getValueCount()); |
| 79 | + |
| 80 | + assertEquals("hello", new String(dictionary.get(0))); |
| 81 | + assertEquals("abc", new String(dictionary.get(1))); |
| 82 | + assertNull(dictionary.get(2)); |
| 83 | + assertEquals("world", new String(dictionary.get(3))); |
| 84 | + assertEquals("12", new String(dictionary.get(4))); |
| 85 | + assertEquals("dictionary", new String(dictionary.get(5))); |
| 86 | + assertEquals("good", new String(dictionary.get(6))); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testBuildVariableWidthDictionaryWithoutNull() { |
| 92 | + try (VarCharVector vec = new VarCharVector("", allocator); |
| 93 | + VarCharVector dictionary = new VarCharVector("", allocator)) { |
| 94 | + |
| 95 | + vec.allocateNew(100, 10); |
| 96 | + vec.setValueCount(10); |
| 97 | + |
| 98 | + dictionary.allocateNew(); |
| 99 | + |
| 100 | + // fill data |
| 101 | + vec.set(0, "hello".getBytes()); |
| 102 | + vec.set(1, "abc".getBytes()); |
| 103 | + vec.setNull(2); |
| 104 | + vec.set(3, "world".getBytes()); |
| 105 | + vec.set(4, "12".getBytes()); |
| 106 | + vec.set(5, "dictionary".getBytes()); |
| 107 | + vec.setNull(6); |
| 108 | + vec.set(7, "hello".getBytes()); |
| 109 | + vec.set(8, "good".getBytes()); |
| 110 | + vec.set(9, "abc".getBytes()); |
| 111 | + |
| 112 | + HashTableBasedDictionaryBuilder<VarCharVector> dictionaryBuilder = |
| 113 | + new HashTableBasedDictionaryBuilder<>(dictionary, false); |
| 114 | + |
| 115 | + int result = dictionaryBuilder.addValues(vec); |
| 116 | + |
| 117 | + assertEquals(6, result); |
| 118 | + assertEquals(6, dictionary.getValueCount()); |
| 119 | + |
| 120 | + assertEquals("hello", new String(dictionary.get(0))); |
| 121 | + assertEquals("abc", new String(dictionary.get(1))); |
| 122 | + assertEquals("world", new String(dictionary.get(2))); |
| 123 | + assertEquals("12", new String(dictionary.get(3))); |
| 124 | + assertEquals("dictionary", new String(dictionary.get(4))); |
| 125 | + assertEquals("good", new String(dictionary.get(5))); |
| 126 | + |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testBuildFixedWidthDictionaryWithNull() { |
| 132 | + try (IntVector vec = new IntVector("", allocator); |
| 133 | + IntVector dictionary = new IntVector("", allocator)) { |
| 134 | + vec.allocateNew(10); |
| 135 | + vec.setValueCount(10); |
| 136 | + |
| 137 | + dictionary.allocateNew(); |
| 138 | + |
| 139 | + // fill data |
| 140 | + vec.set(0, 4); |
| 141 | + vec.set(1, 8); |
| 142 | + vec.set(2, 32); |
| 143 | + vec.set(3, 8); |
| 144 | + vec.set(4, 16); |
| 145 | + vec.set(5, 32); |
| 146 | + vec.setNull(6); |
| 147 | + vec.set(7, 4); |
| 148 | + vec.set(8, 4); |
| 149 | + vec.setNull(9); |
| 150 | + |
| 151 | + HashTableBasedDictionaryBuilder<IntVector> dictionaryBuilder = |
| 152 | + new HashTableBasedDictionaryBuilder<>(dictionary, true); |
| 153 | + |
| 154 | + int result = dictionaryBuilder.addValues(vec); |
| 155 | + |
| 156 | + assertEquals(5, result); |
| 157 | + assertEquals(5, dictionary.getValueCount()); |
| 158 | + |
| 159 | + assertEquals(4, dictionary.get(0)); |
| 160 | + assertEquals(8, dictionary.get(1)); |
| 161 | + assertEquals(32, dictionary.get(2)); |
| 162 | + assertEquals(16, dictionary.get(3)); |
| 163 | + assertTrue(dictionary.isNull(4)); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testBuildFixedWidthDictionaryWithoutNull() { |
| 169 | + try (IntVector vec = new IntVector("", allocator); |
| 170 | + IntVector dictionary = new IntVector("", allocator)) { |
| 171 | + vec.allocateNew(10); |
| 172 | + vec.setValueCount(10); |
| 173 | + |
| 174 | + dictionary.allocateNew(); |
| 175 | + |
| 176 | + // fill data |
| 177 | + vec.set(0, 4); |
| 178 | + vec.set(1, 8); |
| 179 | + vec.set(2, 32); |
| 180 | + vec.set(3, 8); |
| 181 | + vec.set(4, 16); |
| 182 | + vec.set(5, 32); |
| 183 | + vec.setNull(6); |
| 184 | + vec.set(7, 4); |
| 185 | + vec.set(8, 4); |
| 186 | + vec.setNull(9); |
| 187 | + |
| 188 | + HashTableBasedDictionaryBuilder<IntVector> dictionaryBuilder = |
| 189 | + new HashTableBasedDictionaryBuilder<>(dictionary, false); |
| 190 | + |
| 191 | + int result = dictionaryBuilder.addValues(vec); |
| 192 | + |
| 193 | + assertEquals(4, result); |
| 194 | + assertEquals(4, dictionary.getValueCount()); |
| 195 | + |
| 196 | + assertEquals(4, dictionary.get(0)); |
| 197 | + assertEquals(8, dictionary.get(1)); |
| 198 | + assertEquals(32, dictionary.get(2)); |
| 199 | + assertEquals(16, dictionary.get(3)); |
| 200 | + |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments