[Java] DictionaryEncoder doesn't crash when decoding index outside of Dictionary

Open Beginner friendly
#1,158 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
74/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
java
Domain
data

Research direction

Start at DictionaryEncoder.decode and reproduce the behavior with the provided testReferencingIndexOutOfBounds test. Confirm that an encoded index equal to the dictionary value count raises IllegalArgumentException with the expected message, while valid indices still decode normally.

Written by the indexing model from the issue text.

Description

Type: bug
Describe the bug, including details regarding any error messages, version, and platform.
Background

When manually creating an encoded vector, with values referencing indices in a Dictionary, it is possible to have values equal to valueCount of the Dictionary vector i.e. one index out of bounds. This is interpreted as a null value in the Dictionary.


Example

Dictionary

Index Original Value
0 Foo

Encoded Vector - 1

Index Encoded Value Expected Decode Outcome Actual Outcome
0 0 Return the original value Returns the original value

Encoded Vector - 2

Index Encoded Value Expected Decode Outcome Actual Outcome
0 1 Raise IllegalArgumentException Returns null

Encoded Vector - 3

Index Encoded Value Expected Decode Outcome Actual Outcome
0 2 Raise IllegalArgumentException Raises IllegalArgumentException

Test to reproduce the error
    @Test
    public void testReferencingIndexOutOfBounds() {
        // Index at which the original value will be stored at in the dictionary
        var encodedIndex = 0;
        // The encoded value that references an index in the dictionary
        var indexReferenced = 1;

        try (final IntVector encodedVector = new IntVector("encodings", allocator);
             final VarCharVector dictionaryVector = newVarCharVector("dict", allocator); ) {
            var 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));

            try (ValueVector decoded = DictionaryEncoder.decode(encodedVector, dictionary)) {
                fail("There should be an exception when decoding index outside dictionary's range.");
            } catch (Exception e) {
                assertEquals("Provided dictionary does not contain value for index " + indexReferenced, e.getMessage());
            }
        }
    }

Dominant language
Java
Stars
95
Forks
154
Avg merge
2d 16h
Merged PRs (30d)
9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from apache/arrow-java

All issues in apache/arrow-java

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.