Search in sources :

Example 6 with MaterializedRow

use of io.trino.testing.MaterializedRow 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 7 with MaterializedRow

use of io.trino.testing.MaterializedRow 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)

Example 8 with MaterializedRow

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

the class BaseHiveConnectorTest method testTargetMaxFileSize.

protected void testTargetMaxFileSize(int expectedTableWriters) {
    // We use TEXTFILE in this test because is has a very consistent and predictable size
    @Language("SQL") String createTableSql = "CREATE TABLE test_max_file_size WITH (format = 'TEXTFILE') AS SELECT * FROM tpch.sf1.lineitem LIMIT 1000000";
    @Language("SQL") String selectFileInfo = "SELECT distinct \"$path\", \"$file_size\" FROM test_max_file_size";
    // verify the default behavior is one file per node
    Session session = Session.builder(getSession()).setSystemProperty("task_writer_count", "1").build();
    assertUpdate(session, createTableSql, 1000000);
    assertThat(computeActual(selectFileInfo).getRowCount()).isEqualTo(expectedTableWriters);
    assertUpdate("DROP TABLE test_max_file_size");
    // Write table with small limit and verify we get multiple files per node near the expected size
    // Writer writes chunks of rows that are about 1MB
    DataSize maxSize = DataSize.of(1, Unit.MEGABYTE);
    session = Session.builder(getSession()).setSystemProperty("task_writer_count", "1").setCatalogSessionProperty("hive", "target_max_file_size", maxSize.toString()).build();
    assertUpdate(session, createTableSql, 1000000);
    MaterializedResult result = computeActual(selectFileInfo);
    assertThat(result.getRowCount()).isGreaterThan(expectedTableWriters);
    for (MaterializedRow row : result) {
        // allow up to a larger delta due to the very small max size and the relatively large writer chunk size
        assertThat((Long) row.getField(1)).isLessThan(maxSize.toBytes() * 3);
    }
    assertUpdate("DROP TABLE test_max_file_size");
}
Also used : Language(org.intellij.lang.annotations.Language) DataSize(io.airlift.units.DataSize) MaterializedResult(io.trino.testing.MaterializedResult) MaterializedRow(io.trino.testing.MaterializedRow) HiveQueryRunner.createBucketedSession(io.trino.plugin.hive.HiveQueryRunner.createBucketedSession) Session(io.trino.Session)

Example 9 with MaterializedRow

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

the class BaseHiveConnectorTest method testFileSizeHiddenColumn.

@Test
public void testFileSizeHiddenColumn() {
    @Language("SQL") String createTable = "CREATE TABLE test_file_size " + "WITH (" + "partitioned_by = ARRAY['col1']" + ") AS " + "SELECT * FROM (VALUES " + "(0, 0), (3, 0), (6, 0), " + "(1, 1), (4, 1), (7, 1), " + "(2, 2), (5, 2) " + " ) t(col0, col1) ";
    assertUpdate(createTable, 8);
    assertTrue(getQueryRunner().tableExists(getSession(), "test_file_size"));
    TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_file_size");
    List<String> columnNames = ImmutableList.of("col0", "col1", 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(FILE_SIZE_COLUMN_NAME)) {
            assertTrue(columnMetadata.isHidden());
        }
    }
    assertEquals(getPartitions("test_file_size").size(), 3);
    MaterializedResult results = computeActual(format("SELECT *, \"%s\" FROM test_file_size", FILE_SIZE_COLUMN_NAME));
    Map<Integer, Long> fileSizeMap = new HashMap<>();
    for (int i = 0; i < results.getRowCount(); i++) {
        MaterializedRow row = results.getMaterializedRows().get(i);
        int col0 = (int) row.getField(0);
        int col1 = (int) row.getField(1);
        long fileSize = (Long) row.getField(2);
        assertTrue(fileSize > 0);
        assertEquals(col0 % 3, col1);
        if (fileSizeMap.containsKey(col1)) {
            assertEquals(fileSizeMap.get(col1).longValue(), fileSize);
        } else {
            fileSizeMap.put(col1, fileSize);
        }
    }
    assertEquals(fileSizeMap.size(), 3);
    assertUpdate("DROP TABLE test_file_size");
}
Also used : TableMetadata(io.trino.metadata.TableMetadata) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ColumnConstraint(io.trino.sql.planner.planprinter.IoPlanPrinter.ColumnConstraint) Constraint(io.trino.spi.connector.Constraint) Language(org.intellij.lang.annotations.Language) MaterializedResult(io.trino.testing.MaterializedResult) MaterializedRow(io.trino.testing.MaterializedRow) Test(org.testng.annotations.Test) BaseConnectorTest(io.trino.testing.BaseConnectorTest)

Example 10 with MaterializedRow

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

the class BaseHiveConnectorTest method testTargetMaxFileSizePartitioned.

protected void testTargetMaxFileSizePartitioned(int expectedTableWriters) {
    // We use TEXTFILE in this test because is has a very consistent and predictable size
    @Language("SQL") String createTableSql = "" + "CREATE TABLE test_max_file_size_partitioned WITH (partitioned_by = ARRAY['returnflag'], format = 'TEXTFILE') AS " + "SELECT orderkey, partkey, suppkey, linenumber, quantity, extendedprice, discount, tax, linestatus, shipdate, commitdate, receiptdate, shipinstruct, shipmode, comment, returnflag " + "FROM tpch.sf1.lineitem LIMIT 1000000";
    @Language("SQL") String selectFileInfo = "SELECT distinct \"$path\", \"$file_size\" FROM test_max_file_size_partitioned";
    // verify the default behavior is one file per node per partition
    Session session = Session.builder(getSession()).setSystemProperty("task_writer_count", "1").build();
    assertUpdate(session, createTableSql, 1000000);
    assertThat(computeActual(selectFileInfo).getRowCount()).isEqualTo(expectedTableWriters * 3);
    assertUpdate("DROP TABLE test_max_file_size_partitioned");
    // Write table with small limit and verify we get multiple files per node near the expected size
    // Writer writes chunks of rows that are about 1MB
    DataSize maxSize = DataSize.of(1, Unit.MEGABYTE);
    session = Session.builder(getSession()).setSystemProperty("task_writer_count", "1").setCatalogSessionProperty("hive", "target_max_file_size", maxSize.toString()).build();
    assertUpdate(session, createTableSql, 1000000);
    MaterializedResult result = computeActual(selectFileInfo);
    assertThat(result.getRowCount()).isGreaterThan(expectedTableWriters * 3);
    for (MaterializedRow row : result) {
        // allow up to a larger delta due to the very small max size and the relatively large writer chunk size
        assertThat((Long) row.getField(1)).isLessThan(maxSize.toBytes() * 3);
    }
    assertUpdate("DROP TABLE test_max_file_size_partitioned");
}
Also used : Language(org.intellij.lang.annotations.Language) DataSize(io.airlift.units.DataSize) MaterializedResult(io.trino.testing.MaterializedResult) MaterializedRow(io.trino.testing.MaterializedRow) HiveQueryRunner.createBucketedSession(io.trino.plugin.hive.HiveQueryRunner.createBucketedSession) Session(io.trino.Session)

Aggregations

MaterializedRow (io.trino.testing.MaterializedRow)67 MaterializedResult (io.trino.testing.MaterializedResult)58 Test (org.testng.annotations.Test)44 BaseConnectorTest (io.trino.testing.BaseConnectorTest)21 Constraint (io.trino.spi.connector.Constraint)11 Language (org.intellij.lang.annotations.Language)11 Session (io.trino.Session)9 ConnectorPageSource (io.trino.spi.connector.ConnectorPageSource)9 ConnectorSession (io.trino.spi.connector.ConnectorSession)9 ImmutableList (com.google.common.collect.ImmutableList)8 ColumnHandle (io.trino.spi.connector.ColumnHandle)8 Type (io.trino.spi.type.Type)8 ConnectorMetadata (io.trino.spi.connector.ConnectorMetadata)7 ConnectorTableHandle (io.trino.spi.connector.ConnectorTableHandle)7 HiveColumnHandle.bucketColumnHandle (io.trino.plugin.hive.HiveColumnHandle.bucketColumnHandle)6 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)6 LocalDate (java.time.LocalDate)6 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)5 TableMetadata (io.trino.metadata.TableMetadata)5