Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,162 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
presto-main/src/test/java/com/facebook/presto/block/TestDictionaryBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.block; | ||
|
||
import com.facebook.presto.spi.block.DictionaryBlock; | ||
import com.facebook.presto.spi.block.SliceArrayBlock; | ||
import com.google.common.primitives.Ints; | ||
import io.airlift.slice.Slice; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static io.airlift.slice.Slices.wrappedIntArray; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
public class TestDictionaryBlock | ||
extends AbstractTestBlock | ||
{ | ||
@Test | ||
public void testCopyPositionsWithCompaction() | ||
throws Exception | ||
{ | ||
Slice[] expectedValues = createExpectedValues(10); | ||
DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 100); | ||
|
||
List<Integer> positionsToCopy = Ints.asList(0, 10, 20, 30, 40); | ||
DictionaryBlock copiedBlock = (DictionaryBlock) dictionaryBlock.copyPositions(positionsToCopy); | ||
|
||
assertEquals(copiedBlock.getDictionary().getPositionCount(), 1); | ||
assertEquals(copiedBlock.getPositionCount(), positionsToCopy.size()); | ||
assertBlock(copiedBlock.getDictionary(), Arrays.copyOfRange(expectedValues, 0, 1)); | ||
} | ||
|
||
@Test | ||
public void testCopyPositionsWithCompactionsAndReorder() | ||
throws Exception | ||
{ | ||
Slice[] expectedValues = createExpectedValues(10); | ||
DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 100); | ||
List<Integer> positionsToCopy = Ints.asList(50, 55, 40, 45, 60); | ||
|
||
DictionaryBlock copiedBlock = (DictionaryBlock) dictionaryBlock.copyPositions(positionsToCopy); | ||
|
||
assertEquals(copiedBlock.getDictionary().getPositionCount(), 2); | ||
assertEquals(copiedBlock.getPositionCount(), positionsToCopy.size()); | ||
|
||
assertBlock(copiedBlock.getDictionary(), new Slice[] { expectedValues[0], expectedValues[5] }); | ||
assertEquals(copiedBlock.getIds(), wrappedIntArray(0, 1, 0, 1, 0)); | ||
} | ||
|
||
@Test | ||
public void testCopyPositionsSamePosition() | ||
throws Exception | ||
{ | ||
Slice[] expectedValues = createExpectedValues(10); | ||
DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 100); | ||
List<Integer> positionsToCopy = Ints.asList(52, 52, 52); | ||
|
||
DictionaryBlock copiedBlock = (DictionaryBlock) dictionaryBlock.copyPositions(positionsToCopy); | ||
|
||
assertEquals(copiedBlock.getDictionary().getPositionCount(), 1); | ||
assertEquals(copiedBlock.getPositionCount(), positionsToCopy.size()); | ||
|
||
assertBlock(copiedBlock.getDictionary(), new Slice[] { expectedValues[2] }); | ||
assertEquals(copiedBlock.getIds(), wrappedIntArray(0, 0, 0)); | ||
} | ||
|
||
@Test | ||
public void testCopyPositionsNoCompaction() | ||
throws Exception | ||
{ | ||
Slice[] expectedValues = createExpectedValues(1); | ||
DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 100); | ||
|
||
List<Integer> positionsToCopy = Ints.asList(0, 2, 4, 5); | ||
DictionaryBlock copiedBlock = (DictionaryBlock) dictionaryBlock.copyPositions(positionsToCopy); | ||
|
||
assertEquals(copiedBlock.getPositionCount(), positionsToCopy.size()); | ||
assertBlock(copiedBlock.getDictionary(), expectedValues); | ||
} | ||
|
||
@Test | ||
public void testCompact() | ||
throws Exception | ||
{ | ||
Slice[] expectedValues = createExpectedValues(5); | ||
DictionaryBlock dictionaryBlock = createDictionaryBlockWithUnreferencedKeys(expectedValues, 10); | ||
DictionaryBlock compactBlock = dictionaryBlock.compact(); | ||
|
||
assertEquals(compactBlock.getDictionary().getPositionCount(), (expectedValues.length / 2) + 1); | ||
assertBlock(compactBlock.getDictionary(), new Slice[] { expectedValues[0], expectedValues[1], expectedValues[3] }); | ||
assertEquals(compactBlock.getIds(), wrappedIntArray(0, 1, 1, 2, 2, 0, 1, 1, 2, 2)); | ||
} | ||
|
||
@Test | ||
public void testCompactAllKeysReferenced() | ||
throws Exception | ||
{ | ||
Slice[] expectedValues = createExpectedValues(5); | ||
DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 10); | ||
DictionaryBlock compactBlock = dictionaryBlock.compact(); | ||
|
||
// When there is nothing to compact, we return the same block | ||
assertEquals(compactBlock, dictionaryBlock); | ||
} | ||
|
||
private static DictionaryBlock createDictionaryBlockWithUnreferencedKeys(Slice[] expectedValues, int positionCount) | ||
{ | ||
// adds references to 0 and all odd indexes | ||
int dictionarySize = expectedValues.length; | ||
int[] ids = new int[positionCount]; | ||
|
||
for (int i = 0; i < positionCount; i++) { | ||
int index = i % dictionarySize; | ||
if (index % 2 == 0 && index != 0) { | ||
index--; | ||
} | ||
ids[i] = index; | ||
} | ||
return new DictionaryBlock(positionCount, new SliceArrayBlock(dictionarySize, expectedValues), wrappedIntArray(ids)); | ||
} | ||
|
||
private static DictionaryBlock createDictionaryBlock(Slice[] expectedValues, int positionCount) | ||
{ | ||
int dictionarySize = expectedValues.length; | ||
int[] ids = new int[positionCount]; | ||
|
||
for (int i = 0; i < positionCount; i++) { | ||
ids[i] = i % dictionarySize; | ||
} | ||
return new DictionaryBlock(positionCount, new SliceArrayBlock(dictionarySize, expectedValues), wrappedIntArray(ids)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.