Search in sources :

Example 16 with MaterializedResult

use of io.trino.testing.MaterializedResult in project trino by trinodb.

the class AbstractTestHive method testGetRecords.

@Test
public void testGetRecords() throws Exception {
    try (Transaction transaction = newTransaction()) {
        ConnectorMetadata metadata = transaction.getMetadata();
        ConnectorSession session = newSession();
        metadata.beginQuery(session);
        ConnectorTableHandle tableHandle = getTableHandle(metadata, tablePartitionFormat);
        ConnectorTableMetadata tableMetadata = metadata.getTableMetadata(session, tableHandle);
        List<ColumnHandle> columnHandles = ImmutableList.copyOf(metadata.getColumnHandles(session, tableHandle).values());
        Map<String, Integer> columnIndex = indexColumns(columnHandles);
        List<ConnectorSplit> splits = getAllSplits(tableHandle, transaction, session);
        assertEquals(splits.size(), tablePartitionFormatPartitions.size());
        for (ConnectorSplit split : splits) {
            HiveSplit hiveSplit = (HiveSplit) split;
            List<HivePartitionKey> partitionKeys = hiveSplit.getPartitionKeys();
            String ds = partitionKeys.get(0).getValue();
            String fileFormat = partitionKeys.get(1).getValue();
            HiveStorageFormat fileType = HiveStorageFormat.valueOf(fileFormat.toUpperCase(ENGLISH));
            int dummyPartition = Integer.parseInt(partitionKeys.get(2).getValue());
            long rowNumber = 0;
            long completedBytes = 0;
            try (ConnectorPageSource pageSource = pageSourceProvider.createPageSource(transaction.getTransactionHandle(), session, hiveSplit, tableHandle, columnHandles, DynamicFilter.EMPTY)) {
                MaterializedResult result = materializeSourceDataStream(session, pageSource, getTypes(columnHandles));
                assertPageSourceType(pageSource, fileType);
                for (MaterializedRow row : result) {
                    try {
                        assertValueTypes(row, tableMetadata.getColumns());
                    } catch (RuntimeException e) {
                        throw new RuntimeException("row " + rowNumber, e);
                    }
                    rowNumber++;
                    Object value;
                    value = row.getField(columnIndex.get("t_string"));
                    if (rowNumber % 19 == 0) {
                        assertNull(value);
                    } else if (rowNumber % 19 == 1) {
                        assertEquals(value, "");
                    } else {
                        assertEquals(value, "test");
                    }
                    assertEquals(row.getField(columnIndex.get("t_tinyint")), (byte) (1 + rowNumber));
                    assertEquals(row.getField(columnIndex.get("t_smallint")), (short) (2 + rowNumber));
                    assertEquals(row.getField(columnIndex.get("t_int")), 3 + (int) rowNumber);
                    if (rowNumber % 13 == 0) {
                        assertNull(row.getField(columnIndex.get("t_bigint")));
                    } else {
                        assertEquals(row.getField(columnIndex.get("t_bigint")), 4 + rowNumber);
                    }
                    assertEquals((Float) row.getField(columnIndex.get("t_float")), 5.1f + rowNumber, 0.001);
                    assertEquals(row.getField(columnIndex.get("t_double")), 6.2 + rowNumber);
                    if (rowNumber % 3 == 2) {
                        assertNull(row.getField(columnIndex.get("t_boolean")));
                    } else {
                        assertEquals(row.getField(columnIndex.get("t_boolean")), rowNumber % 3 != 0);
                    }
                    assertEquals(row.getField(columnIndex.get("ds")), ds);
                    assertEquals(row.getField(columnIndex.get("file_format")), fileFormat);
                    assertEquals(row.getField(columnIndex.get("dummy")), dummyPartition);
                    long newCompletedBytes = pageSource.getCompletedBytes();
                    assertTrue(newCompletedBytes >= completedBytes);
                    assertTrue(newCompletedBytes <= hiveSplit.getLength());
                    completedBytes = newCompletedBytes;
                }
                assertTrue(completedBytes <= hiveSplit.getLength());
                assertEquals(rowNumber, 100);
            }
        }
    }
}
Also used : HiveColumnHandle.bucketColumnHandle(io.trino.plugin.hive.HiveColumnHandle.bucketColumnHandle) ColumnHandle(io.trino.spi.connector.ColumnHandle) ConnectorPageSource(io.trino.spi.connector.ConnectorPageSource) Constraint(io.trino.spi.connector.Constraint) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) StorageFormat.fromHiveStorageFormat(io.trino.plugin.hive.metastore.StorageFormat.fromHiveStorageFormat) ConnectorSession(io.trino.spi.connector.ConnectorSession) TestingConnectorSession(io.trino.testing.TestingConnectorSession) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata) MaterializedResult(io.trino.testing.MaterializedResult) ConnectorSplit(io.trino.spi.connector.ConnectorSplit) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) MaterializedRow(io.trino.testing.MaterializedRow) Test(org.testng.annotations.Test)

Example 17 with MaterializedResult

use of io.trino.testing.MaterializedResult in project trino by trinodb.

the class AbstractTestHive method doTestBucketedTableValidation.

private void doTestBucketedTableValidation(HiveStorageFormat storageFormat, SchemaTableName tableName) throws Exception {
    prepareInvalidBuckets(storageFormat, tableName);
    // read succeeds when validation is disabled
    try (Transaction transaction = newTransaction()) {
        ConnectorMetadata metadata = transaction.getMetadata();
        ConnectorSession session = newSession(ImmutableMap.of("validate_bucketing", false));
        metadata.beginQuery(session);
        ConnectorTableHandle tableHandle = getTableHandle(metadata, tableName);
        List<ColumnHandle> columnHandles = filterNonHiddenColumnHandles(metadata.getColumnHandles(session, tableHandle).values());
        MaterializedResult result = readTable(transaction, tableHandle, columnHandles, session, TupleDomain.all(), OptionalInt.empty(), Optional.of(storageFormat));
        // fewer rows due to deleted file
        assertEquals(result.getRowCount(), 87);
    }
    // read fails due to validation failure
    assertReadFailsWithMessageMatching(storageFormat, tableName, "Hive table is corrupt\\. File '.*/000002_0_.*' is for bucket 2, but contains a row for bucket 5.");
}
Also used : HiveColumnHandle.bucketColumnHandle(io.trino.plugin.hive.HiveColumnHandle.bucketColumnHandle) ColumnHandle(io.trino.spi.connector.ColumnHandle) ConnectorSession(io.trino.spi.connector.ConnectorSession) TestingConnectorSession(io.trino.testing.TestingConnectorSession) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata) MaterializedResult(io.trino.testing.MaterializedResult) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle)

Example 18 with MaterializedResult

use of io.trino.testing.MaterializedResult in project trino by trinodb.

the class BaseHiveConnectorTest method testShowCreateTable.

@Test
@Override
public void testShowCreateTable() {
    assertThat(computeActual("SHOW CREATE TABLE orders").getOnlyValue()).isEqualTo("CREATE TABLE hive.tpch.orders (\n" + "   orderkey bigint,\n" + "   custkey bigint,\n" + "   orderstatus varchar(1),\n" + "   totalprice double,\n" + "   orderdate date,\n" + "   orderpriority varchar(15),\n" + "   clerk varchar(15),\n" + "   shippriority integer,\n" + "   comment varchar(79)\n" + ")\n" + "WITH (\n" + "   format = 'ORC'\n" + ")");
    String createTableSql = format("" + "CREATE TABLE %s.%s.%s (\n" + "   c1 bigint,\n" + "   c2 double,\n" + "   \"c 3\" varchar,\n" + "   \"c'4\" array(bigint),\n" + "   c5 map(bigint, varchar)\n" + ")\n" + "WITH (\n" + "   format = 'RCBINARY'\n" + ")", getSession().getCatalog().get(), getSession().getSchema().get(), "test_show_create_table");
    assertUpdate(createTableSql);
    MaterializedResult actualResult = computeActual("SHOW CREATE TABLE test_show_create_table");
    assertEquals(getOnlyElement(actualResult.getOnlyColumnAsSet()), createTableSql);
    createTableSql = format("" + "CREATE TABLE %s.%s.%s (\n" + "   c1 bigint,\n" + "   \"c 2\" varchar,\n" + "   \"c'3\" array(bigint),\n" + "   c4 map(bigint, varchar) COMMENT 'comment test4',\n" + "   c5 double COMMENT ''\n)\n" + "COMMENT 'test'\n" + "WITH (\n" + "   bucket_count = 5,\n" + "   bucketed_by = ARRAY['c1','c 2'],\n" + "   bucketing_version = 1,\n" + "   format = 'ORC',\n" + "   orc_bloom_filter_columns = ARRAY['c1','c2'],\n" + "   orc_bloom_filter_fpp = 7E-1,\n" + "   partitioned_by = ARRAY['c5'],\n" + "   sorted_by = ARRAY['c1','c 2 DESC'],\n" + "   transactional = true\n" + ")", getSession().getCatalog().get(), getSession().getSchema().get(), "\"test_show_create_table'2\"");
    assertUpdate(createTableSql);
    actualResult = computeActual("SHOW CREATE TABLE \"test_show_create_table'2\"");
    assertEquals(getOnlyElement(actualResult.getOnlyColumnAsSet()), createTableSql);
    createTableSql = format("" + "CREATE TABLE %s.%s.%s (\n" + "   c1 ROW(\"$a\" bigint, \"$b\" varchar)\n)\n" + "WITH (\n" + "   format = 'ORC'\n" + ")", getSession().getCatalog().get(), getSession().getSchema().get(), "test_show_create_table_with_special_characters");
    assertUpdate(createTableSql);
    actualResult = computeActual("SHOW CREATE TABLE test_show_create_table_with_special_characters");
    assertEquals(getOnlyElement(actualResult.getOnlyColumnAsSet()), createTableSql);
}
Also used : MaterializedResult(io.trino.testing.MaterializedResult) Test(org.testng.annotations.Test) BaseConnectorTest(io.trino.testing.BaseConnectorTest)

Example 19 with MaterializedResult

use of io.trino.testing.MaterializedResult in project trino by trinodb.

the class BaseHiveConnectorTest method testPartitionHiddenColumn.

@Test
public void testPartitionHiddenColumn() {
    @Language("SQL") String createTable = "CREATE TABLE test_partition_hidden_column " + "WITH (" + "partitioned_by = ARRAY['col1', 'col2']" + ") AS " + "SELECT * FROM (VALUES " + "(0, 11, 21), (1, 12, 22), (2, 13, 23), " + "(3, 14, 24), (4, 15, 25), (5, 16, 26), " + "(6, 17, 27), (7, 18, 28), (8, 19, 29)" + " ) t (col0, col1, col2) ";
    assertUpdate(createTable, 9);
    assertTrue(getQueryRunner().tableExists(getSession(), "test_partition_hidden_column"));
    TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_partition_hidden_column");
    assertEquals(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY), ImmutableList.of("col1", "col2"));
    List<String> columnNames = ImmutableList.of("col0", "col1", "col2", PATH_COLUMN_NAME, FILE_SIZE_COLUMN_NAME, FILE_MODIFIED_TIME_COLUMN_NAME, PARTITION_COLUMN_NAME);
    List<ColumnMetadata> columnMetadatas = tableMetadata.getColumns();
    assertEquals(columnMetadatas.size(), columnNames.size());
    for (int i = 0; i < columnMetadatas.size(); i++) {
        ColumnMetadata columnMetadata = columnMetadatas.get(i);
        assertEquals(columnMetadata.getName(), columnNames.get(i));
        if (columnMetadata.getName().equals(PARTITION_COLUMN_NAME)) {
            assertTrue(columnMetadata.isHidden());
        }
    }
    assertEquals(getPartitions("test_partition_hidden_column").size(), 9);
    MaterializedResult results = computeActual(format("SELECT *, \"%s\" FROM test_partition_hidden_column", PARTITION_COLUMN_NAME));
    for (MaterializedRow row : results.getMaterializedRows()) {
        String actualPartition = (String) row.getField(3);
        String expectedPartition = format("col1=%s/col2=%s", row.getField(1), row.getField(2));
        assertEquals(actualPartition, expectedPartition);
    }
    assertEquals(results.getRowCount(), 9);
    assertUpdate("DROP TABLE test_partition_hidden_column");
}
Also used : TableMetadata(io.trino.metadata.TableMetadata) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) Language(org.intellij.lang.annotations.Language) MaterializedResult(io.trino.testing.MaterializedResult) ColumnConstraint(io.trino.sql.planner.planprinter.IoPlanPrinter.ColumnConstraint) Constraint(io.trino.spi.connector.Constraint) MaterializedRow(io.trino.testing.MaterializedRow) Test(org.testng.annotations.Test) BaseConnectorTest(io.trino.testing.BaseConnectorTest)

Example 20 with MaterializedResult

use of io.trino.testing.MaterializedResult in project trino by trinodb.

the class BaseHiveConnectorTest 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 = 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 = 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(io.trino.testing.MaterializedResult) MaterializedRow(io.trino.testing.MaterializedRow) HiveQueryRunner.createBucketedSession(io.trino.plugin.hive.HiveQueryRunner.createBucketedSession) Session(io.trino.Session) Test(org.testng.annotations.Test) BaseConnectorTest(io.trino.testing.BaseConnectorTest)

Aggregations

MaterializedResult (io.trino.testing.MaterializedResult)328 Test (org.testng.annotations.Test)248 Page (io.trino.spi.Page)103 BaseConnectorTest (io.trino.testing.BaseConnectorTest)64 MaterializedRow (io.trino.testing.MaterializedRow)63 Type (io.trino.spi.type.Type)61 RowPagesBuilder (io.trino.RowPagesBuilder)57 PlanNodeId (io.trino.sql.planner.plan.PlanNodeId)51 OperatorAssertion.toMaterializedResult (io.trino.operator.OperatorAssertion.toMaterializedResult)41 TaskContext (io.trino.operator.TaskContext)37 TestingTaskContext (io.trino.testing.TestingTaskContext)37 OperatorFactory (io.trino.operator.OperatorFactory)31 Language (org.intellij.lang.annotations.Language)31 ConnectorSession (io.trino.spi.connector.ConnectorSession)27 Session (io.trino.Session)24 ColumnHandle (io.trino.spi.connector.ColumnHandle)24 ConnectorMetadata (io.trino.spi.connector.ConnectorMetadata)24 ConnectorTableHandle (io.trino.spi.connector.ConnectorTableHandle)23 ImmutableList (com.google.common.collect.ImmutableList)22 Constraint (io.trino.spi.connector.Constraint)20