Search in sources :

Example 6 with LazyBlock

use of com.facebook.presto.common.block.LazyBlock in project presto by prestodb.

the class TestPageProcessor method testProjectLazyLoad.

@Test
public void testProjectLazyLoad() {
    PageProcessor pageProcessor = new PageProcessor(Optional.of(new SelectAllFilter()), ImmutableList.of(new PageProjectionWithOutputs(new LazyPagePageProjection(), new int[] { 0 })), OptionalInt.of(MAX_BATCH_SIZE));
    // if channel 1 is loaded, test will fail
    Page inputPage = new Page(createLongSequenceBlock(0, 100), new LazyBlock(100, lazyBlock -> {
        throw new AssertionError("Lazy block should not be loaded");
    }));
    LocalMemoryContext memoryContext = newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName());
    Iterator<Optional<Page>> output = pageProcessor.process(SESSION.getSqlFunctionProperties(), new DriverYieldSignal(), memoryContext, inputPage);
    List<Optional<Page>> outputPages = ImmutableList.copyOf(output);
    assertEquals(outputPages.size(), 1);
    assertPageEquals(ImmutableList.of(BIGINT), outputPages.get(0).orElse(null), new Page(createLongSequenceBlock(0, 100)));
}
Also used : TestingTicker(com.facebook.airlift.testing.TestingTicker) Page(com.facebook.presto.common.Page) CompletedWork(com.facebook.presto.operator.CompletedWork) Arrays(java.util.Arrays) MetadataManager(com.facebook.presto.metadata.MetadataManager) Test(org.testng.annotations.Test) Expressions.constant(com.facebook.presto.sql.relational.Expressions.constant) BlockAssertions(com.facebook.presto.block.BlockAssertions) Duration(io.airlift.units.Duration) Expressions.field(com.facebook.presto.sql.relational.Expressions.field) ADD(com.facebook.presto.common.function.OperatorType.ADD) Executors.newSingleThreadScheduledExecutor(java.util.concurrent.Executors.newSingleThreadScheduledExecutor) SESSION(com.facebook.presto.testing.TestingConnectorSession.SESSION) Slices(io.airlift.slice.Slices) CallExpression(com.facebook.presto.spi.relation.CallExpression) PageFunctionCompiler(com.facebook.presto.sql.gen.PageFunctionCompiler) Assert.assertFalse(org.testng.Assert.assertFalse) SPLIT_RUN_QUANTA(com.facebook.presto.execution.executor.PrioritizedSplitRunner.SPLIT_RUN_QUANTA) Collections.nCopies(java.util.Collections.nCopies) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) LocalMemoryContext(com.facebook.presto.memory.context.LocalMemoryContext) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Assert.assertNotNull(org.testng.Assert.assertNotNull) Threads.daemonThreadsNamed(com.facebook.airlift.concurrent.Threads.daemonThreadsNamed) List(java.util.List) ClassLayout(org.openjdk.jol.info.ClassLayout) Optional(java.util.Optional) LazyBlock(com.facebook.presto.common.block.LazyBlock) IntStream(java.util.stream.IntStream) Slice(io.airlift.slice.Slice) Assert.assertNull(org.testng.Assert.assertNull) DriverYieldSignal(com.facebook.presto.operator.DriverYieldSignal) BlockAssertions.createSlicesBlock(com.facebook.presto.block.BlockAssertions.createSlicesBlock) RowType.withDefaultFieldNames(com.facebook.presto.common.type.RowType.withDefaultFieldNames) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) MAX_BATCH_SIZE(com.facebook.presto.operator.project.PageProcessor.MAX_BATCH_SIZE) PageAssertions.assertPageEquals(com.facebook.presto.operator.PageAssertions.assertPageEquals) Assert.assertEquals(org.testng.Assert.assertEquals) VariableWidthBlock(com.facebook.presto.common.block.VariableWidthBlock) OptionalInt(java.util.OptionalInt) Supplier(java.util.function.Supplier) TypeSignatureProvider.fromTypes(com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes) Expressions.call(com.facebook.presto.sql.relational.Expressions.call) REAL(com.facebook.presto.common.type.RealType.REAL) ImmutableList(com.google.common.collect.ImmutableList) String.join(java.lang.String.join) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MIN_PAGE_SIZE_IN_BYTES(com.facebook.presto.operator.project.PageProcessor.MIN_PAGE_SIZE_IN_BYTES) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) SelectedPositions.positionsRange(com.facebook.presto.operator.project.SelectedPositions.positionsRange) ArrayType(com.facebook.presto.common.type.ArrayType) AggregatedMemoryContext.newSimpleAggregatedMemoryContext(com.facebook.presto.memory.context.AggregatedMemoryContext.newSimpleAggregatedMemoryContext) MAX_PAGE_SIZE_IN_BYTES(com.facebook.presto.operator.project.PageProcessor.MAX_PAGE_SIZE_IN_BYTES) Type(com.facebook.presto.common.type.Type) BlockAssertions.createLongSequenceBlock(com.facebook.presto.block.BlockAssertions.createLongSequenceBlock) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) PageAssertions.createPageWithRandomData(com.facebook.presto.operator.PageAssertions.createPageWithRandomData) BlockAssertions.createMapType(com.facebook.presto.block.BlockAssertions.createMapType) Iterator(java.util.Iterator) BlockAssertions.createStringsBlock(com.facebook.presto.block.BlockAssertions.createStringsBlock) AggregatedMemoryContext(com.facebook.presto.memory.context.AggregatedMemoryContext) SelectedPositions.positionsList(com.facebook.presto.operator.project.SelectedPositions.positionsList) MetadataManager.createTestMetadataManager(com.facebook.presto.metadata.MetadataManager.createTestMetadataManager) Work(com.facebook.presto.operator.Work) Assert.assertTrue(org.testng.Assert.assertTrue) ExpressionProfiler(com.facebook.presto.sql.gen.ExpressionProfiler) Block(com.facebook.presto.common.block.Block) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) LocalMemoryContext(com.facebook.presto.memory.context.LocalMemoryContext) Optional(java.util.Optional) DriverYieldSignal(com.facebook.presto.operator.DriverYieldSignal) Page(com.facebook.presto.common.Page) LazyBlock(com.facebook.presto.common.block.LazyBlock) Test(org.testng.annotations.Test)

Example 7 with LazyBlock

use of com.facebook.presto.common.block.LazyBlock in project presto by prestodb.

the class TestPageProcessor method testSelectNoneFilterLazyLoad.

@Test
public void testSelectNoneFilterLazyLoad() {
    PageProcessor pageProcessor = new PageProcessor(Optional.of(new SelectNoneFilter()), ImmutableList.of(createInputPageProjectionWithOutputs(1, BIGINT, 0)));
    // if channel 1 is loaded, test will fail
    Page inputPage = new Page(createLongSequenceBlock(0, 100), new LazyBlock(100, lazyBlock -> {
        throw new AssertionError("Lazy block should not be loaded");
    }));
    LocalMemoryContext memoryContext = newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName());
    Iterator<Optional<Page>> output = pageProcessor.process(SESSION.getSqlFunctionProperties(), new DriverYieldSignal(), memoryContext, inputPage);
    assertEquals(memoryContext.getBytes(), 0);
    List<Optional<Page>> outputPages = ImmutableList.copyOf(output);
    assertEquals(outputPages.size(), 0);
}
Also used : TestingTicker(com.facebook.airlift.testing.TestingTicker) Page(com.facebook.presto.common.Page) CompletedWork(com.facebook.presto.operator.CompletedWork) Arrays(java.util.Arrays) MetadataManager(com.facebook.presto.metadata.MetadataManager) Test(org.testng.annotations.Test) Expressions.constant(com.facebook.presto.sql.relational.Expressions.constant) BlockAssertions(com.facebook.presto.block.BlockAssertions) Duration(io.airlift.units.Duration) Expressions.field(com.facebook.presto.sql.relational.Expressions.field) ADD(com.facebook.presto.common.function.OperatorType.ADD) Executors.newSingleThreadScheduledExecutor(java.util.concurrent.Executors.newSingleThreadScheduledExecutor) SESSION(com.facebook.presto.testing.TestingConnectorSession.SESSION) Slices(io.airlift.slice.Slices) CallExpression(com.facebook.presto.spi.relation.CallExpression) PageFunctionCompiler(com.facebook.presto.sql.gen.PageFunctionCompiler) Assert.assertFalse(org.testng.Assert.assertFalse) SPLIT_RUN_QUANTA(com.facebook.presto.execution.executor.PrioritizedSplitRunner.SPLIT_RUN_QUANTA) Collections.nCopies(java.util.Collections.nCopies) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) LocalMemoryContext(com.facebook.presto.memory.context.LocalMemoryContext) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Assert.assertNotNull(org.testng.Assert.assertNotNull) Threads.daemonThreadsNamed(com.facebook.airlift.concurrent.Threads.daemonThreadsNamed) List(java.util.List) ClassLayout(org.openjdk.jol.info.ClassLayout) Optional(java.util.Optional) LazyBlock(com.facebook.presto.common.block.LazyBlock) IntStream(java.util.stream.IntStream) Slice(io.airlift.slice.Slice) Assert.assertNull(org.testng.Assert.assertNull) DriverYieldSignal(com.facebook.presto.operator.DriverYieldSignal) BlockAssertions.createSlicesBlock(com.facebook.presto.block.BlockAssertions.createSlicesBlock) RowType.withDefaultFieldNames(com.facebook.presto.common.type.RowType.withDefaultFieldNames) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) MAX_BATCH_SIZE(com.facebook.presto.operator.project.PageProcessor.MAX_BATCH_SIZE) PageAssertions.assertPageEquals(com.facebook.presto.operator.PageAssertions.assertPageEquals) Assert.assertEquals(org.testng.Assert.assertEquals) VariableWidthBlock(com.facebook.presto.common.block.VariableWidthBlock) OptionalInt(java.util.OptionalInt) Supplier(java.util.function.Supplier) TypeSignatureProvider.fromTypes(com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes) Expressions.call(com.facebook.presto.sql.relational.Expressions.call) REAL(com.facebook.presto.common.type.RealType.REAL) ImmutableList(com.google.common.collect.ImmutableList) String.join(java.lang.String.join) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MIN_PAGE_SIZE_IN_BYTES(com.facebook.presto.operator.project.PageProcessor.MIN_PAGE_SIZE_IN_BYTES) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) SelectedPositions.positionsRange(com.facebook.presto.operator.project.SelectedPositions.positionsRange) ArrayType(com.facebook.presto.common.type.ArrayType) AggregatedMemoryContext.newSimpleAggregatedMemoryContext(com.facebook.presto.memory.context.AggregatedMemoryContext.newSimpleAggregatedMemoryContext) MAX_PAGE_SIZE_IN_BYTES(com.facebook.presto.operator.project.PageProcessor.MAX_PAGE_SIZE_IN_BYTES) Type(com.facebook.presto.common.type.Type) BlockAssertions.createLongSequenceBlock(com.facebook.presto.block.BlockAssertions.createLongSequenceBlock) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) PageAssertions.createPageWithRandomData(com.facebook.presto.operator.PageAssertions.createPageWithRandomData) BlockAssertions.createMapType(com.facebook.presto.block.BlockAssertions.createMapType) Iterator(java.util.Iterator) BlockAssertions.createStringsBlock(com.facebook.presto.block.BlockAssertions.createStringsBlock) AggregatedMemoryContext(com.facebook.presto.memory.context.AggregatedMemoryContext) SelectedPositions.positionsList(com.facebook.presto.operator.project.SelectedPositions.positionsList) MetadataManager.createTestMetadataManager(com.facebook.presto.metadata.MetadataManager.createTestMetadataManager) Work(com.facebook.presto.operator.Work) Assert.assertTrue(org.testng.Assert.assertTrue) ExpressionProfiler(com.facebook.presto.sql.gen.ExpressionProfiler) Block(com.facebook.presto.common.block.Block) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) LocalMemoryContext(com.facebook.presto.memory.context.LocalMemoryContext) LazyBlock(com.facebook.presto.common.block.LazyBlock) Optional(java.util.Optional) DriverYieldSignal(com.facebook.presto.operator.DriverYieldSignal) Page(com.facebook.presto.common.Page) Test(org.testng.annotations.Test)

Example 8 with LazyBlock

use of com.facebook.presto.common.block.LazyBlock in project presto by prestodb.

the class ParquetPageSource method getNextPage.

@Override
public Page getNextPage() {
    try {
        batchId++;
        int batchSize = parquetReader.nextBatch();
        if (closed || batchSize <= 0) {
            close();
            return null;
        }
        completedPositions += batchSize;
        Block[] blocks = new Block[fields.size()];
        for (int fieldId = 0; fieldId < blocks.length; fieldId++) {
            Optional<Field> field = fields.get(fieldId);
            if (field.isPresent()) {
                blocks[fieldId] = new LazyBlock(batchSize, new ParquetBlockLoader(field.get()));
            } else {
                blocks[fieldId] = RunLengthEncodedBlock.create(types.get(fieldId), null, batchSize);
            }
        }
        return new Page(batchSize, blocks);
    } catch (PrestoException e) {
        closeWithSuppression(e);
        throw e;
    } catch (RuntimeException e) {
        closeWithSuppression(e);
        throw new PrestoException(HIVE_CURSOR_ERROR, e);
    }
}
Also used : Field(com.facebook.presto.parquet.Field) LazyBlock(com.facebook.presto.common.block.LazyBlock) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) Block(com.facebook.presto.common.block.Block) LazyBlock(com.facebook.presto.common.block.LazyBlock) Page(com.facebook.presto.common.Page) PrestoException(com.facebook.presto.spi.PrestoException)

Example 9 with LazyBlock

use of com.facebook.presto.common.block.LazyBlock in project presto by prestodb.

the class OrcSelectiveRecordReader method getNextPage.

public Page getNextPage() throws IOException {
    if (constantFilterIsFalse) {
        return null;
    }
    int batchSize = prepareNextBatch();
    if (batchSize < 0) {
        return null;
    }
    readPositions += batchSize;
    initializePositions(batchSize);
    int[] positionsToRead = this.positions;
    int positionCount = batchSize;
    if (filterFunctionWithoutInput.isPresent()) {
        positionCount = applyFilterFunctionWithNoInputs(positionCount);
        if (positionCount == 0) {
            batchRead(batchSize);
            return EMPTY_PAGE;
        }
        positionsToRead = outputPositions;
    }
    if (!filterFunctionsWithConstantInputs.isEmpty()) {
        positionCount = applyFilterFunctions(filterFunctionsWithConstantInputs, filterFunctionConstantInputs, positionsToRead, positionCount);
        if (positionCount == 0) {
            batchRead(batchSize);
            return EMPTY_PAGE;
        }
        positionsToRead = outputPositions;
    }
    int offset = getNextRowInGroup();
    if (reorderFilters && offset >= MAX_BATCH_SIZE) {
        reorderFiltersIfNeeded();
    }
    for (int i = 0; i < streamReaderOrder.length; i++) {
        int columnIndex = streamReaderOrder[i];
        if (!hasAnyFilter(columnIndex)) {
            break;
        }
        SelectiveStreamReader streamReader = getStreamReader(columnIndex);
        positionCount = streamReader.read(offset, positionsToRead, positionCount);
        if (positionCount == 0) {
            break;
        }
        positionsToRead = streamReader.getReadPositions();
        verify(positionCount == 1 || positionsToRead[positionCount - 1] - positionsToRead[0] >= positionCount - 1, "positions must monotonically increase");
        if (filterFunctionsOrder[i] != null) {
            positionCount = applyFilterFunctions(filterFunctionsOrder[i], filterFunctionInputs[i], positionsToRead, positionCount);
            if (positionCount == 0) {
                break;
            }
            positionsToRead = outputPositions;
        }
    }
    localMemoryContext.setBytes(getSelfRetainedSizeInBytes());
    batchRead(batchSize);
    if (positionCount == 0) {
        return EMPTY_PAGE;
    }
    if (constantFilterError != null) {
        throw constantFilterError;
    }
    for (int i = 0; i < positionCount; i++) {
        if (errors[positionsToRead[i]] != null) {
            throw errors[positionsToRead[i]];
        }
    }
    for (SelectiveStreamReader reader : getStreamReaders()) {
        if (reader != null) {
            reader.throwAnyError(positionsToRead, positionCount);
        }
    }
    Block[] blocks = new Block[appendRowNumber ? outputColumns.size() + 1 : outputColumns.size()];
    for (int i = 0; i < outputColumns.size(); i++) {
        int columnIndex = outputColumns.get(i);
        if (constantValues[columnIndex] != null) {
            blocks[i] = RunLengthEncodedBlock.create(columnTypes.get(columnIndex), constantValues[columnIndex] == NULL_MARKER ? null : constantValues[columnIndex], positionCount);
        } else if (!hasAnyFilter(columnIndex)) {
            blocks[i] = new LazyBlock(positionCount, new OrcBlockLoader(columnIndex, offset, positionsToRead, positionCount));
        } else {
            Block block = getStreamReader(columnIndex).getBlock(positionsToRead, positionCount);
            updateMaxCombinedBytesPerRow(hiveColumnIndices[columnIndex], block);
            if (coercers[columnIndex] != null) {
                block = coercers[columnIndex].apply(block);
            }
            blocks[i] = block;
        }
    }
    if (appendRowNumber) {
        blocks[outputColumns.size()] = createRowNumbersBlock(positionsToRead, positionCount, this.getFilePosition());
    }
    Page page = new Page(positionCount, blocks);
    validateWritePageChecksum(page);
    return page;
}
Also used : LazyBlock(com.facebook.presto.common.block.LazyBlock) SelectiveStreamReader(com.facebook.presto.orc.reader.SelectiveStreamReader) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) LazyBlock(com.facebook.presto.common.block.LazyBlock) LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) Block(com.facebook.presto.common.block.Block) Page(com.facebook.presto.common.Page)

Example 10 with LazyBlock

use of com.facebook.presto.common.block.LazyBlock in project presto by prestodb.

the class OrcBatchPageSource method getNextPage.

@Override
public Page getNextPage() {
    try {
        batchId++;
        int batchSize = recordReader.nextBatch();
        if (batchSize <= 0) {
            close();
            return null;
        }
        completedPositions += batchSize;
        Block[] blocks = new Block[hiveColumnIndexes.length];
        for (int fieldId = 0; fieldId < blocks.length; fieldId++) {
            if (constantBlocks[fieldId] != null) {
                blocks[fieldId] = constantBlocks[fieldId].getRegion(0, batchSize);
            } else {
                blocks[fieldId] = new LazyBlock(batchSize, new OrcBlockLoader(hiveColumnIndexes[fieldId]));
            }
        }
        return new Page(batchSize, blocks);
    } catch (PrestoException e) {
        closeWithSuppression(e);
        throw e;
    } catch (OrcCorruptionException e) {
        closeWithSuppression(e);
        throw new PrestoException(HIVE_BAD_DATA, e);
    } catch (IOException | RuntimeException e) {
        closeWithSuppression(e);
        throw new PrestoException(HIVE_CURSOR_ERROR, format("Failed to read ORC file: %s", orcDataSource.getId()), e);
    }
}
Also used : LazyBlock(com.facebook.presto.common.block.LazyBlock) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) Block(com.facebook.presto.common.block.Block) LazyBlock(com.facebook.presto.common.block.LazyBlock) Page(com.facebook.presto.common.Page) PrestoException(com.facebook.presto.spi.PrestoException) OrcCorruptionException(com.facebook.presto.orc.OrcCorruptionException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Aggregations

Page (com.facebook.presto.common.Page)11 Block (com.facebook.presto.common.block.Block)11 LazyBlock (com.facebook.presto.common.block.LazyBlock)11 RunLengthEncodedBlock (com.facebook.presto.common.block.RunLengthEncodedBlock)4 Test (org.testng.annotations.Test)4 Threads.daemonThreadsNamed (com.facebook.airlift.concurrent.Threads.daemonThreadsNamed)3 BlockAssertions (com.facebook.presto.block.BlockAssertions)3 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)3 BOOLEAN (com.facebook.presto.common.type.BooleanType.BOOLEAN)3 VARCHAR (com.facebook.presto.common.type.VarcharType.VARCHAR)3 MetadataManager (com.facebook.presto.metadata.MetadataManager)3 MetadataManager.createTestMetadataManager (com.facebook.presto.metadata.MetadataManager.createTestMetadataManager)3 PageAssertions.assertPageEquals (com.facebook.presto.operator.PageAssertions.assertPageEquals)3 MAX_BATCH_SIZE (com.facebook.presto.operator.project.PageProcessor.MAX_BATCH_SIZE)3 PrestoException (com.facebook.presto.spi.PrestoException)3 TypeSignatureProvider.fromTypes (com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes)3 PageFunctionCompiler (com.facebook.presto.sql.gen.PageFunctionCompiler)3 Expressions.call (com.facebook.presto.sql.relational.Expressions.call)3 Expressions.constant (com.facebook.presto.sql.relational.Expressions.constant)3 Expressions.field (com.facebook.presto.sql.relational.Expressions.field)3