@Test public void testConstructor() { for (int i = -2; i <= -1; i++) { try { //noinspection ResultOfObjectAllocationIgnored new TypedSet(BIGINT, i, FUNCTION_NAME); fail("Should throw exception if expectedSize < 0"); } catch (IllegalArgumentException e) { // ignored } } try { //noinspection ResultOfObjectAllocationIgnored new TypedSet(null, 1, FUNCTION_NAME); fail("Should throw exception if type is null"); } catch (NullPointerException | IllegalArgumentException e) { // ignored } }
private static void testBigint(Block longBlock, int expectedSetSize) { TypedSet typedSet = new TypedSet(BIGINT, expectedSetSize, FUNCTION_NAME); testBigintFor(typedSet, longBlock); BlockBuilder emptyBlockBuilder = BIGINT.createBlockBuilder(null, expectedSetSize); TypedSet typedSetWithPassedInBuilder = new TypedSet(BIGINT, emptyBlockBuilder, expectedSetSize, FUNCTION_NAME); testBigintFor(typedSetWithPassedInBuilder, longBlock); }
@Test public void testGetElementPositionRandom() { TypedSet set = new TypedSet(VARCHAR, 1, FUNCTION_NAME); testGetElementPositionRandomFor(set); BlockBuilder emptyBlockBuilder = VARCHAR.createBlockBuilder(null, 3); TypedSet setWithPassedInBuilder = new TypedSet(VARCHAR, emptyBlockBuilder, 1, FUNCTION_NAME); testGetElementPositionRandomFor(setWithPassedInBuilder); }
@Test public void testMemoryExceeded() { try { TypedSet typedSet = new TypedSet(BIGINT, 10, FUNCTION_NAME); for (int i = 0; i <= TypedSet.FOUR_MEGABYTES + 1; i++) { Block block = createLongsBlock(nCopies(1, (long) i)); typedSet.add(block, 0); } fail("expected exception"); } catch (PrestoException e) { assertEquals(e.getErrorCode(), EXCEEDED_FUNCTION_MEMORY_LIMIT.toErrorCode()); } }
private static double mapDotProduct(Block leftMap, Block rightMap) { TypedSet rightMapKeys = new TypedSet(VARCHAR, rightMap.getPositionCount(), "cosine_similarity"); for (int i = 0; i < rightMap.getPositionCount(); i += 2) { rightMapKeys.add(rightMap, i); } double result = 0.0; for (int i = 0; i < leftMap.getPositionCount(); i += 2) { int position = rightMapKeys.positionOf(leftMap, i); if (position != -1) { result += DOUBLE.getDouble(leftMap, i + 1) * DOUBLE.getDouble(rightMap, 2 * position + 1); } } return result; }
@Test public void testGetElementPosition() { int elementCount = 100; // Set initialTypedSetEntryCount to a small number to trigger rehash() int initialTypedSetEntryCount = 10; TypedSet typedSet = new TypedSet(BIGINT, initialTypedSetEntryCount, FUNCTION_NAME); BlockBuilder blockBuilder = BIGINT.createFixedSizeBlockBuilder(elementCount); for (int i = 0; i < elementCount; i++) { BIGINT.writeLong(blockBuilder, i); typedSet.add(blockBuilder, i); } assertEquals(typedSet.size(), elementCount); for (int j = 0; j < blockBuilder.getPositionCount(); j++) { assertEquals(typedSet.positionOf(blockBuilder, j), j); } }
@Test public void testGetElementPositionWithProvidedEmptyBlockBuilder() { int elementCount = 100; // Set initialTypedSetEntryCount to a small number to trigger rehash() int initialTypedSetEntryCount = 10; BlockBuilder emptyBlockBuilder = BIGINT.createFixedSizeBlockBuilder(elementCount); TypedSet typedSet = new TypedSet(BIGINT, emptyBlockBuilder, initialTypedSetEntryCount, FUNCTION_NAME); BlockBuilder externalBlockBuilder = BIGINT.createFixedSizeBlockBuilder(elementCount); for (int i = 0; i < elementCount; i++) { if (i % 10 == 0) { externalBlockBuilder.appendNull(); } else { BIGINT.writeLong(externalBlockBuilder, i); } typedSet.add(externalBlockBuilder, i); } assertEquals(typedSet.size(), emptyBlockBuilder.getPositionCount()); assertEquals(typedSet.size(), elementCount - elementCount / 10 + 1); for (int j = 0; j < typedSet.size(); j++) { assertEquals(typedSet.positionOf(emptyBlockBuilder, j), j); } }
@TypeParameter("E") @SqlType("array(E)") public static Block union( @TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) { int leftArrayCount = leftArray.getPositionCount(); int rightArrayCount = rightArray.getPositionCount(); TypedSet typedSet = new TypedSet(type, leftArrayCount + rightArrayCount, "array_union"); BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(null, leftArrayCount + rightArrayCount); appendTypedArray(leftArray, type, typedSet, distinctElementBlockBuilder); appendTypedArray(rightArray, type, typedSet, distinctElementBlockBuilder); return distinctElementBlockBuilder.build(); }
TypedSet typedSet = new TypedSet(BIGINT, initialTypedSetEntryCount, FUNCTION_NAME); BlockBuilder blockBuilder = BIGINT.createFixedSizeBlockBuilder(elementCount); for (int i = 0; i < elementCount; i++) {
TypedSet typedSet = new TypedSet(BIGINT, secondBlockBuilder, initialTypedSetEntryCount, FUNCTION_NAME); BlockBuilder externalBlockBuilder = BIGINT.createFixedSizeBlockBuilder(elementCount); for (int i = 0; i < elementCount; i++) {
TypedSet rightTypedSet = new TypedSet(type, rightPositionCount, "array_intersect"); for (int i = 0; i < rightPositionCount; i++) { rightTypedSet.add(rightArray, i); TypedSet intersectTypedSet = new TypedSet(type, blockBuilder, rightPositionCount, "array_intersect"); for (int i = 0; i < leftPositionCount; i++) { if (rightTypedSet.contains(leftArray, i)) {
TypedSet typedSet = new TypedSet(toKeyType, fromMap.getPositionCount() / 2, "map-to-map cast"); BlockBuilder keyBlockBuilder = toKeyType.createBlockBuilder(null, fromMap.getPositionCount() / 2); for (int i = 0; i < fromMap.getPositionCount(); i += 2) {
@TypeParameter("E") @SqlType("array(E)") public static Block except( @TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) { int leftPositionCount = leftArray.getPositionCount(); int rightPositionCount = rightArray.getPositionCount(); if (leftPositionCount == 0) { return leftArray; } TypedSet typedSet = new TypedSet(type, leftPositionCount + rightPositionCount, "array_except"); BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(null, leftPositionCount); for (int i = 0; i < rightPositionCount; i++) { typedSet.add(rightArray, i); } for (int i = 0; i < leftPositionCount; i++) { if (!typedSet.contains(leftArray, i)) { typedSet.add(leftArray, i); type.appendTo(leftArray, i, distinctElementBlockBuilder); } } return distinctElementBlockBuilder.build(); } }
TypedSet typedSet = new TypedSet(type, array.getPositionCount(), "array_distinct"); int distinctCount = 0;
TypedSet typedSet = new TypedSet(keyType, entries / 2, FUNCTION_NAME); BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0); BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
TypedSet uniqueKeys = new TypedSet(keyType, entryCount, "map_from_entries");
@ScalarFunction @SqlType("array(varchar)") public static Block oldArrayDistinct(@SqlType("array(varchar)") Block array) { if (array.getPositionCount() == 0) { return array; } TypedSet typedSet = new TypedSet(VARCHAR, array.getPositionCount(), "old_array_distinct"); BlockBuilder distinctElementBlockBuilder = VARCHAR.createBlockBuilder(null, array.getPositionCount()); for (int i = 0; i < array.getPositionCount(); i++) { if (!typedSet.contains(array, i)) { typedSet.add(array, i); VARCHAR.appendTo(array, i, distinctElementBlockBuilder); } } return distinctElementBlockBuilder.build(); } }
valueArrayBlockBuilders.ensureCapacity(state.getEntryCount()); BlockBuilder distinctKeyBlockBuilder = keyType.createBlockBuilder(null, state.getEntryCount(), expectedValueSize(keyType, 100)); TypedSet keySet = new TypedSet(keyType, state.getEntryCount(), MultimapAggregationFunction.NAME);
initializeEntryIndicesList(entryCount); TypedSet keySet = new TypedSet(keyType, entryCount, NAME);
public KeyValuePairs(Type keyType, Type valueType, boolean isMultiValue) { this.keyType = requireNonNull(keyType, "keyType is null"); this.valueType = requireNonNull(valueType, "valueType is null"); this.keySet = new TypedSet(keyType, EXPECTED_ENTRIES); keyBlockBuilder = this.keyType.createBlockBuilder(new BlockBuilderStatus(), EXPECTED_ENTRIES, expectedValueSize(keyType, EXPECTED_ENTRY_SIZE)); valueBlockBuilder = this.valueType.createBlockBuilder(new BlockBuilderStatus(), EXPECTED_ENTRIES, expectedValueSize(valueType, EXPECTED_ENTRY_SIZE)); this.isMultiValue = isMultiValue; }