Search in sources :

Example 21 with MaterializedResult

use of com.facebook.presto.testing.MaterializedResult in project presto by prestodb.

the class TestHiveIntegrationSmokeTest method testDeleteAndInsert.

@Test
public void testDeleteAndInsert() {
    Session session = getSession();
    // Partition 1 is untouched
    // Partition 2 is altered (dropped and then added back)
    // Partition 3 is added
    // Partition 4 is dropped
    assertUpdate(session, "CREATE TABLE tmp_delete_insert WITH (partitioned_by=array ['z']) AS " + "SELECT * from (VALUES (CAST (101 AS BIGINT), CAST (1 AS BIGINT)), (201, 2), (202, 2), (401, 4), (402, 4), (403, 4)) t(a, z)", 6);
    List<MaterializedRow> expectedBefore = MaterializedResult.resultBuilder(session, BIGINT, BIGINT).row(101L, 1L).row(201L, 2L).row(202L, 2L).row(401L, 4L).row(402L, 4L).row(403L, 4L).build().getMaterializedRows();
    List<MaterializedRow> expectedAfter = MaterializedResult.resultBuilder(session, BIGINT, BIGINT).row(101L, 1L).row(203L, 2L).row(204L, 2L).row(205L, 2L).row(301L, 2L).row(302L, 3L).build().getMaterializedRows();
    try {
        transaction(getQueryRunner().getTransactionManager(), getQueryRunner().getAccessControl()).execute(session, transactionSession -> {
            assertUpdate(transactionSession, "DELETE FROM tmp_delete_insert WHERE z >= 2");
            assertUpdate(transactionSession, "INSERT INTO tmp_delete_insert VALUES (203, 2), (204, 2), (205, 2), (301, 2), (302, 3)", 5);
            MaterializedResult actualFromAnotherTransaction = computeActual(session, "SELECT * FROM tmp_delete_insert");
            assertEqualsIgnoreOrder(actualFromAnotherTransaction, expectedBefore);
            MaterializedResult actualFromCurrentTransaction = computeActual(transactionSession, "SELECT * FROM tmp_delete_insert");
            assertEqualsIgnoreOrder(actualFromCurrentTransaction, expectedAfter);
            rollback();
        });
    } catch (RollbackException e) {
    // ignore
    }
    MaterializedResult actualAfterRollback = computeActual(session, "SELECT * FROM tmp_delete_insert");
    assertEqualsIgnoreOrder(actualAfterRollback, expectedBefore);
    transaction(getQueryRunner().getTransactionManager(), getQueryRunner().getAccessControl()).execute(session, transactionSession -> {
        assertUpdate(transactionSession, "DELETE FROM tmp_delete_insert WHERE z >= 2");
        assertUpdate(transactionSession, "INSERT INTO tmp_delete_insert VALUES (203, 2), (204, 2), (205, 2), (301, 2), (302, 3)", 5);
        MaterializedResult actualOutOfTransaction = computeActual(session, "SELECT * FROM tmp_delete_insert");
        assertEqualsIgnoreOrder(actualOutOfTransaction, expectedBefore);
        MaterializedResult actualInTransaction = computeActual(transactionSession, "SELECT * FROM tmp_delete_insert");
        assertEqualsIgnoreOrder(actualInTransaction, expectedAfter);
    });
    MaterializedResult actualAfterTransaction = computeActual(session, "SELECT * FROM tmp_delete_insert");
    assertEqualsIgnoreOrder(actualAfterTransaction, expectedAfter);
}
Also used : MaterializedResult(com.facebook.presto.testing.MaterializedResult) MaterializedRow(com.facebook.presto.testing.MaterializedRow) HiveQueryRunner.createBucketedSession(com.facebook.presto.hive.HiveQueryRunner.createBucketedSession) Session(com.facebook.presto.Session) Test(org.testng.annotations.Test) AbstractTestIntegrationSmokeTest(com.facebook.presto.tests.AbstractTestIntegrationSmokeTest)

Example 22 with MaterializedResult

use of com.facebook.presto.testing.MaterializedResult in project presto by prestodb.

the class TestHiveIntegrationSmokeTest method testDeleteFromUnpartitionedTable.

@Test
public void testDeleteFromUnpartitionedTable() throws Exception {
    assertUpdate("CREATE TABLE test_delete_unpartitioned AS SELECT orderstatus FROM tpch.tiny.orders", "SELECT count(*) from orders");
    assertUpdate("DELETE FROM test_delete_unpartitioned");
    MaterializedResult result = computeActual("SELECT * from test_delete_unpartitioned");
    assertEquals(result.getRowCount(), 0);
    assertUpdate("DROP TABLE test_delete_unpartitioned");
    assertFalse(getQueryRunner().tableExists(getSession(), "test_delete_unpartitioned"));
}
Also used : MaterializedResult(com.facebook.presto.testing.MaterializedResult) Test(org.testng.annotations.Test) AbstractTestIntegrationSmokeTest(com.facebook.presto.tests.AbstractTestIntegrationSmokeTest)

Example 23 with MaterializedResult

use of com.facebook.presto.testing.MaterializedResult in project presto by prestodb.

the class TestHiveIntegrationSmokeTest method testCreateExternalTable.

@Test
public void testCreateExternalTable() throws Exception {
    File tempDir = createTempDir();
    File dataFile = new File(tempDir, "test.txt");
    Files.write("hello\nworld\n", dataFile, UTF_8);
    @Language("SQL") String createTableSql = format("" + "CREATE TABLE %s.%s.test_create_external (\n" + "   name varchar\n" + ")\n" + "WITH (\n" + "   external_location = '%s',\n" + "   format = 'TEXTFILE'\n" + ")", getSession().getCatalog().get(), getSession().getSchema().get(), new Path(tempDir.toURI().toASCIIString()).toString());
    assertUpdate(createTableSql);
    MaterializedResult actual = computeActual("SHOW CREATE TABLE test_create_external");
    assertEquals(actual.getOnlyValue(), createTableSql);
    actual = computeActual("SELECT name FROM test_create_external");
    assertEquals(actual.getOnlyColumnAsSet(), ImmutableSet.of("hello", "world"));
    assertUpdate("DROP TABLE test_create_external");
    // file should still exist after drop
    assertFile(dataFile);
    deleteRecursively(tempDir);
}
Also used : Path(org.apache.hadoop.fs.Path) Language(org.intellij.lang.annotations.Language) MaterializedResult(com.facebook.presto.testing.MaterializedResult) FileAssert.assertFile(org.testng.FileAssert.assertFile) File(java.io.File) Test(org.testng.annotations.Test) AbstractTestIntegrationSmokeTest(com.facebook.presto.tests.AbstractTestIntegrationSmokeTest)

Example 24 with MaterializedResult

use of com.facebook.presto.testing.MaterializedResult in project presto by prestodb.

the class TestHivePageSink method writeTestFile.

private static long writeTestFile(HiveClientConfig config, ExtendedHiveMetastore metastore, String outputPath) {
    HiveTransactionHandle transaction = new HiveTransactionHandle();
    ConnectorPageSink pageSink = createPageSink(transaction, config, metastore, new Path("file:///" + outputPath));
    List<LineItemColumn> columns = getTestColumns();
    List<Type> columnTypes = columns.stream().map(LineItemColumn::getType).map(TestHivePageSink::getHiveType).map(hiveType -> hiveType.getType(TYPE_MANAGER)).collect(toList());
    PageBuilder pageBuilder = new PageBuilder(columnTypes);
    int rows = 0;
    for (LineItem lineItem : new LineItemGenerator(0.01, 1, 1)) {
        rows++;
        if (rows >= NUM_ROWS) {
            break;
        }
        pageBuilder.declarePosition();
        for (int i = 0; i < columns.size(); i++) {
            LineItemColumn column = columns.get(i);
            BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(i);
            switch(column.getType().getBase()) {
                case IDENTIFIER:
                    BIGINT.writeLong(blockBuilder, column.getIdentifier(lineItem));
                    break;
                case INTEGER:
                    INTEGER.writeLong(blockBuilder, column.getInteger(lineItem));
                    break;
                case DATE:
                    DATE.writeLong(blockBuilder, column.getDate(lineItem));
                    break;
                case DOUBLE:
                    DOUBLE.writeDouble(blockBuilder, column.getDouble(lineItem));
                    break;
                case VARCHAR:
                    createUnboundedVarcharType().writeSlice(blockBuilder, Slices.utf8Slice(column.getString(lineItem)));
                    break;
                default:
                    throw new IllegalArgumentException("Unsupported type " + column.getType());
            }
        }
    }
    Page page = pageBuilder.build();
    pageSink.appendPage(page);
    getFutureValue(pageSink.finish());
    File outputDir = new File(outputPath);
    List<File> files = ImmutableList.copyOf(outputDir.listFiles((dir, name) -> !name.endsWith(".crc")));
    File outputFile = getOnlyElement(files);
    long length = outputFile.length();
    ConnectorPageSource pageSource = createPageSource(transaction, config, outputFile);
    List<Page> pages = new ArrayList<>();
    while (!pageSource.isFinished()) {
        Page nextPage = pageSource.getNextPage();
        if (nextPage != null) {
            nextPage.assureLoaded();
            pages.add(nextPage);
        }
    }
    MaterializedResult expectedResults = toMaterializedResult(getSession(config), columnTypes, ImmutableList.of(page));
    MaterializedResult results = toMaterializedResult(getSession(config), columnTypes, pages);
    assertEquals(results, expectedResults);
    return length;
}
Also used : Path(org.apache.hadoop.fs.Path) Page(com.facebook.presto.spi.Page) HiveTestUtils.getDefaultHiveDataStreamFactories(com.facebook.presto.hive.HiveTestUtils.getDefaultHiveDataStreamFactories) HiveTestUtils.getDefaultHiveFileWriterFactories(com.facebook.presto.hive.HiveTestUtils.getDefaultHiveFileWriterFactories) Assertions.assertGreaterThan(io.airlift.testing.Assertions.assertGreaterThan) Test(org.testng.annotations.Test) TpchColumnTypes(io.airlift.tpch.TpchColumnTypes) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ExtendedHiveMetastore(com.facebook.presto.hive.metastore.ExtendedHiveMetastore) Slices(io.airlift.slice.Slices) ConnectorPageSink(com.facebook.presto.spi.ConnectorPageSink) Path(org.apache.hadoop.fs.Path) HIVE_LONG(com.facebook.presto.hive.HiveType.HIVE_LONG) SERIALIZATION_LIB(org.apache.hadoop.hive.serde.serdeConstants.SERIALIZATION_LIB) TpchColumnType(io.airlift.tpch.TpchColumnType) FileUtils(io.airlift.testing.FileUtils) ImmutableMap(com.google.common.collect.ImmutableMap) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) String.format(java.lang.String.format) ConnectorSession(com.facebook.presto.spi.ConnectorSession) LineItemGenerator(io.airlift.tpch.LineItemGenerator) TupleDomain(com.facebook.presto.spi.predicate.TupleDomain) HIVE_STRING(com.facebook.presto.hive.HiveType.HIVE_STRING) List(java.util.List) Stream(java.util.stream.Stream) TYPE_MANAGER(com.facebook.presto.hive.HiveTestUtils.TYPE_MANAGER) Optional(java.util.Optional) INTEGER(com.facebook.presto.spi.type.IntegerType.INTEGER) Joiner(com.google.common.base.Joiner) JsonCodec(io.airlift.json.JsonCodec) DOUBLE(com.facebook.presto.spi.type.DoubleType.DOUBLE) LineItem(io.airlift.tpch.LineItem) REGULAR(com.facebook.presto.hive.HiveColumnHandle.ColumnType.REGULAR) HivePageSinkMetadata(com.facebook.presto.hive.metastore.HivePageSinkMetadata) Assert.assertEquals(org.testng.Assert.assertEquals) OptionalInt(java.util.OptionalInt) ArrayList(java.util.ArrayList) HIVE_DATE(com.facebook.presto.hive.HiveType.HIVE_DATE) HIVE_INT(com.facebook.presto.hive.HiveType.HIVE_INT) HiveTestUtils.createTestHdfsEnvironment(com.facebook.presto.hive.HiveTestUtils.createTestHdfsEnvironment) TestingHiveMetastore(com.facebook.presto.hive.metastore.TestingHiveMetastore) ImmutableList(com.google.common.collect.ImmutableList) HIVE_DOUBLE(com.facebook.presto.hive.HiveType.HIVE_DOUBLE) Files(com.google.common.io.Files) Type(com.facebook.presto.spi.type.Type) LineItemColumn(io.airlift.tpch.LineItemColumn) Properties(java.util.Properties) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) MoreFutures.getFutureValue(io.airlift.concurrent.MoreFutures.getFutureValue) File(java.io.File) VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) MaterializedResult(com.facebook.presto.testing.MaterializedResult) NONE(com.facebook.presto.hive.HiveCompressionCodec.NONE) Collectors.toList(java.util.stream.Collectors.toList) ConnectorPageSource(com.facebook.presto.spi.ConnectorPageSource) PageBuilder(com.facebook.presto.spi.PageBuilder) DATE(com.facebook.presto.spi.type.DateType.DATE) FILE_INPUT_FORMAT(org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.FILE_INPUT_FORMAT) Assert.assertTrue(org.testng.Assert.assertTrue) GroupByHashPageIndexerFactory(com.facebook.presto.GroupByHashPageIndexerFactory) JoinCompiler(com.facebook.presto.sql.gen.JoinCompiler) HiveTestUtils.getDefaultHiveRecordCursorProvider(com.facebook.presto.hive.HiveTestUtils.getDefaultHiveRecordCursorProvider) LineItemColumn(io.airlift.tpch.LineItemColumn) ArrayList(java.util.ArrayList) LineItem(io.airlift.tpch.LineItem) Page(com.facebook.presto.spi.Page) PageBuilder(com.facebook.presto.spi.PageBuilder) ConnectorPageSource(com.facebook.presto.spi.ConnectorPageSource) TpchColumnType(io.airlift.tpch.TpchColumnType) Type(com.facebook.presto.spi.type.Type) VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) ConnectorPageSink(com.facebook.presto.spi.ConnectorPageSink) MaterializedResult(com.facebook.presto.testing.MaterializedResult) File(java.io.File) LineItemGenerator(io.airlift.tpch.LineItemGenerator) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder)

Example 25 with MaterializedResult

use of com.facebook.presto.testing.MaterializedResult in project presto by prestodb.

the class WindowAssertions method assertWindowQuery.

public static void assertWindowQuery(@Language("SQL") String sql, MaterializedResult expected, LocalQueryRunner localQueryRunner) {
    @Language("SQL") String query = format("" + "SELECT orderkey, orderstatus,\n%s\n" + "FROM (%s) x", sql, VALUES);
    MaterializedResult actual = localQueryRunner.execute(query);
    assertEqualsIgnoreOrder(actual.getMaterializedRows(), expected.getMaterializedRows());
}
Also used : Language(org.intellij.lang.annotations.Language) MaterializedResult(com.facebook.presto.testing.MaterializedResult)

Aggregations

MaterializedResult (com.facebook.presto.testing.MaterializedResult)298 Test (org.testng.annotations.Test)255 Page (com.facebook.presto.spi.Page)75 PlanNodeId (com.facebook.presto.sql.planner.plan.PlanNodeId)54 MaterializedRow (com.facebook.presto.testing.MaterializedRow)52 Type (com.facebook.presto.spi.type.Type)43 RowPagesBuilder (com.facebook.presto.RowPagesBuilder)35 Session (com.facebook.presto.Session)23 TestingTaskContext (com.facebook.presto.testing.TestingTaskContext)21 AbstractTestIntegrationSmokeTest (com.facebook.presto.tests.AbstractTestIntegrationSmokeTest)20 ImmutableList (com.google.common.collect.ImmutableList)20 ColumnHandle (com.facebook.presto.spi.ColumnHandle)19 ConnectorSession (com.facebook.presto.spi.ConnectorSession)18 TestingConnectorSession (com.facebook.presto.testing.TestingConnectorSession)18 ConnectorTableHandle (com.facebook.presto.spi.ConnectorTableHandle)17 ConnectorMetadata (com.facebook.presto.spi.connector.ConnectorMetadata)17 ImmutableMap (com.google.common.collect.ImmutableMap)17 List (java.util.List)17 BIGINT (com.facebook.presto.spi.type.BigintType.BIGINT)16 Path (org.apache.hadoop.fs.Path)14