Search in sources :

Example 66 with ConnectorPageSource

use of com.facebook.presto.spi.ConnectorPageSource in project presto by prestodb.

the class TestOrcFileRewriter method testRewriterDropThenAddSameColumns.

/**
 * The following test drop and add the same columns; the legacy ORC rewriter will fail due to unchanged schema.
 * However, if we enforce the newly added column to always have the largest ID, this won't happen.
 */
@Test
public void testRewriterDropThenAddSameColumns() throws Exception {
    FunctionAndTypeManager functionAndTypeManager = createTestFunctionAndTypeManager();
    DBI dbi = new DBI("jdbc:h2:mem:test" + System.nanoTime() + "_" + ThreadLocalRandom.current().nextInt());
    dbi.registerMapper(new TableColumn.Mapper(functionAndTypeManager));
    Handle dummyHandle = dbi.open();
    File dataDir = Files.createTempDir();
    StorageManager storageManager = createOrcStorageManager(dbi, dataDir);
    List<Long> columnIds = ImmutableList.of(3L, 7L);
    List<Type> columnTypes = ImmutableList.of(BIGINT, createVarcharType(20));
    File file = new File(temporary, randomUUID().toString());
    try (FileWriter writer = createFileWriter(columnIds, columnTypes, file, false)) {
        List<Page> pages = rowPagesBuilder(columnTypes).row(2L, "2").build();
        writer.appendPages(pages);
    }
    // Add a column
    File newFile1 = new File(temporary, randomUUID().toString());
    FileSystem fileSystem = new LocalOrcDataEnvironment().getFileSystem(DEFAULT_RAPTOR_CONTEXT);
    OrcFileInfo info = createFileRewriter().rewrite(fileSystem, getColumnTypes(ImmutableList.of(3L, 7L, 10L), ImmutableList.of(BIGINT, createVarcharType(20), DOUBLE)), path(file), path(newFile1), new BitSet(5));
    assertEquals(info.getRowCount(), 1);
    // Drop a column
    File newFile2 = new File(temporary, randomUUID().toString());
    info = createFileRewriter().rewrite(fileSystem, getColumnTypes(ImmutableList.of(7L, 10L), ImmutableList.of(createVarcharType(20), DOUBLE)), path(newFile1), path(newFile2), new BitSet(5));
    assertEquals(info.getRowCount(), 1);
    // Add a column with the same ID but different type
    File newFile3 = new File(temporary, randomUUID().toString());
    info = createFileRewriter().rewrite(fileSystem, getColumnTypes(ImmutableList.of(7L, 10L, 3L), ImmutableList.of(createVarcharType(20), DOUBLE, createVarcharType(5))), path(newFile2), path(newFile3), new BitSet(5));
    assertEquals(info.getRowCount(), 1);
    // Get prepared for the final file; make sure it is accessible from storage manager
    UUID uuid = randomUUID();
    File newFile4 = getFileSystemPath(new File(dataDir, "data/storage"), uuid);
    // Optimized ORC writer does not create the file itself
    newFile4.getParentFile().mkdirs();
    newFile4.createNewFile();
    // Drop a column and add a column
    info = createFileRewriter().rewrite(fileSystem, getColumnTypes(ImmutableList.of(7L, 3L, 8L), ImmutableList.of(createVarcharType(20), createVarcharType(5), INTEGER)), path(newFile3), path(newFile4), new BitSet(5));
    assertEquals(info.getRowCount(), 1);
    ConnectorPageSource source = storageManager.getPageSource(DEFAULT_RAPTOR_CONTEXT, DEFAULT_HIVE_FILE_CONTEXT, uuid, Optional.empty(), false, OptionalInt.empty(), ImmutableList.of(3L, 7L, 8L), ImmutableList.of(createVarcharType(5), createVarcharType(20), INTEGER), TupleDomain.all(), READER_ATTRIBUTES);
    Page page = null;
    while (page == null) {
        page = source.getNextPage();
    }
    assertEquals(page.getPositionCount(), 1);
    try {
        // Column 3L
        Block column0 = page.getBlock(0);
        assertTrue(column0.isNull(0));
        // Column 7L
        Block column1 = page.getBlock(1);
        assertEquals(createVarcharType(20).getSlice(column1, 0), utf8Slice("2"));
        // Column 8L
        Block column2 = page.getBlock(2);
        assertTrue(column2.isNull(0));
        dummyHandle.close();
        deleteRecursively(dataDir.toPath(), ALLOW_INSECURE);
    } catch (UnsupportedOperationException e) {
        // Optimized ORC rewriter will respect the schema
        fail();
    }
}
Also used : TestOrcStorageManager.createOrcStorageManager(com.facebook.presto.raptor.storage.TestOrcStorageManager.createOrcStorageManager) BitSet(java.util.BitSet) DBI(org.skife.jdbi.v2.DBI) Page(com.facebook.presto.common.Page) ConnectorPageSource(com.facebook.presto.spi.ConnectorPageSource) TableColumn(com.facebook.presto.raptor.metadata.TableColumn) Handle(org.skife.jdbi.v2.Handle) DecimalType(com.facebook.presto.common.type.DecimalType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) FunctionAndTypeManager.createTestFunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager) FileSystem(org.apache.hadoop.fs.FileSystem) LocalOrcDataEnvironment(com.facebook.presto.raptor.filesystem.LocalOrcDataEnvironment) Block(com.facebook.presto.common.block.Block) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) File(java.io.File) Test(org.testng.annotations.Test)

Aggregations

ConnectorPageSource (com.facebook.presto.spi.ConnectorPageSource)66 ConnectorSession (com.facebook.presto.spi.ConnectorSession)36 ColumnHandle (com.facebook.presto.spi.ColumnHandle)29 Test (org.testng.annotations.Test)28 ImmutableList (com.google.common.collect.ImmutableList)27 ConnectorSplit (com.facebook.presto.spi.ConnectorSplit)25 TestingConnectorSession (com.facebook.presto.testing.TestingConnectorSession)25 Optional (java.util.Optional)25 List (java.util.List)23 Path (org.apache.hadoop.fs.Path)23 SchemaTableName (com.facebook.presto.spi.SchemaTableName)22 ImmutableMap (com.google.common.collect.ImmutableMap)19 IOException (java.io.IOException)19 Objects.requireNonNull (java.util.Objects.requireNonNull)19 Type (com.facebook.presto.common.type.Type)18 Map (java.util.Map)18 Configuration (org.apache.hadoop.conf.Configuration)18 PrestoException (com.facebook.presto.spi.PrestoException)17 String.format (java.lang.String.format)16 UUID (java.util.UUID)16