diff --git a/vector/src/test/java/org/apache/arrow/vector/TestDictionaryVector.java b/vector/src/test/java/org/apache/arrow/vector/TestDictionaryVector.java index 65c04433e5..c42737a53d 100644 --- a/vector/src/test/java/org/apache/arrow/vector/TestDictionaryVector.java +++ b/vector/src/test/java/org/apache/arrow/vector/TestDictionaryVector.java @@ -404,7 +404,7 @@ public void testIntEquals() { } @Test - public void testVarcharEquals() { + public void testVarCharEquals() { try (final VarCharVector vector1 = new VarCharVector("varchar", allocator); final VarCharVector vector2 = new VarCharVector("varchar", allocator)) { @@ -1306,6 +1306,37 @@ public void testDictionaryUIntOverflow() { } } + /** + * Test related to Issue #1158 - it was possible to decode an encoded value 1 index outside the Dictionary's range. + */ + @Test + public void testReferencingIndexOutOfBounds() { + // Index at which the original value will be stored at in the dictionary + int encodedIndex = 0; + // The encoded value that references an index in the dictionary + int indexReferenced = 1; + + try (final IntVector encodedVector = new IntVector("encodings", allocator); + final VarCharVector dictionaryVector = newVarCharVector("dict", allocator); ) { + String originalValue = "Foo"; + dictionaryVector.allocateNew(1); + dictionaryVector.setValueCount(1); + dictionaryVector.set(encodedIndex, originalValue.getBytes(StandardCharsets.UTF_8)); + + encodedVector.allocateNew(1); + encodedVector.setValueCount(1); + encodedVector.set(0, indexReferenced); + + Dictionary dictionary = new Dictionary(dictionaryVector, new DictionaryEncoding(1L, false, null)); + + assertThrows( + IllegalArgumentException.class, + () -> DictionaryEncoder.decode(encodedVector, dictionary), + "Expected decode to throw when referencing index out of bounds." + ); + } + } + private int[] convertListToIntArray(List list) { int[] values = new int[list.size()]; for (int i = 0; i < list.size(); i++) {