Search in sources :

Example 21 with ColumnMetadata

use of io.trino.spi.connector.ColumnMetadata 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 22 with ColumnMetadata

use of io.trino.spi.connector.ColumnMetadata in project trino by trinodb.

the class BaseHiveConnectorTest method verifyPartition.

private void verifyPartition(boolean hasPartition, TableMetadata tableMetadata, List<String> partitionKeys) {
    Object partitionByProperty = tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY);
    if (hasPartition) {
        assertEquals(partitionByProperty, partitionKeys);
        for (ColumnMetadata columnMetadata : tableMetadata.getColumns()) {
            boolean partitionKey = partitionKeys.contains(columnMetadata.getName());
            assertEquals(columnMetadata.getExtraInfo(), columnExtraInfo(partitionKey));
        }
    } else {
        assertNull(partitionByProperty);
    }
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata)

Example 23 with ColumnMetadata

use of io.trino.spi.connector.ColumnMetadata 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 24 with ColumnMetadata

use of io.trino.spi.connector.ColumnMetadata in project trino by trinodb.

the class BaseHiveConnectorTest method testFileModifiedTimeHiddenColumn.

@Test(dataProvider = "timestampPrecision")
public void testFileModifiedTimeHiddenColumn(HiveTimestampPrecision precision) {
    long testStartTime = Instant.now().toEpochMilli();
    @Language("SQL") String createTable = "CREATE TABLE test_file_modified_time " + "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_modified_time"));
    TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_file_modified_time");
    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_MODIFIED_TIME_COLUMN_NAME)) {
            assertTrue(columnMetadata.isHidden());
        }
    }
    assertEquals(getPartitions("test_file_modified_time").size(), 3);
    Session sessionWithTimestampPrecision = withTimestampPrecision(getSession(), precision);
    MaterializedResult results = computeActual(sessionWithTimestampPrecision, format("SELECT *, \"%s\" FROM test_file_modified_time", FILE_MODIFIED_TIME_COLUMN_NAME));
    Map<Integer, Instant> fileModifiedTimeMap = 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);
        Instant fileModifiedTime = ((ZonedDateTime) row.getField(2)).toInstant();
        assertThat(fileModifiedTime.toEpochMilli()).isCloseTo(testStartTime, offset(2000L));
        assertEquals(col0 % 3, col1);
        if (fileModifiedTimeMap.containsKey(col1)) {
            assertEquals(fileModifiedTimeMap.get(col1), fileModifiedTime);
        } else {
            fileModifiedTimeMap.put(col1, fileModifiedTime);
        }
    }
    assertEquals(fileModifiedTimeMap.size(), 3);
    assertUpdate("DROP TABLE test_file_modified_time");
}
Also used : TableMetadata(io.trino.metadata.TableMetadata) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Instant(java.time.Instant) ColumnConstraint(io.trino.sql.planner.planprinter.IoPlanPrinter.ColumnConstraint) Constraint(io.trino.spi.connector.Constraint) Language(org.intellij.lang.annotations.Language) ZonedDateTime(java.time.ZonedDateTime) 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 25 with ColumnMetadata

use of io.trino.spi.connector.ColumnMetadata in project trino by trinodb.

the class TestingMetadata method addColumn.

@Override
public void addColumn(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnMetadata column) {
    ConnectorTableMetadata tableMetadata = getTableMetadata(session, tableHandle);
    SchemaTableName tableName = getTableName(tableHandle);
    ImmutableList.Builder<ColumnMetadata> columns = ImmutableList.builder();
    columns.addAll(tableMetadata.getColumns());
    columns.add(column);
    tables.put(tableName, new ConnectorTableMetadata(tableName, columns.build(), tableMetadata.getProperties(), tableMetadata.getComment()));
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ImmutableList(com.google.common.collect.ImmutableList) SchemaTableName(io.trino.spi.connector.SchemaTableName) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata)

Aggregations

ColumnMetadata (io.trino.spi.connector.ColumnMetadata)154 SchemaTableName (io.trino.spi.connector.SchemaTableName)75 ConnectorTableMetadata (io.trino.spi.connector.ConnectorTableMetadata)73 Test (org.testng.annotations.Test)64 ImmutableList (com.google.common.collect.ImmutableList)63 ImmutableMap (com.google.common.collect.ImmutableMap)55 List (java.util.List)45 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)43 Optional (java.util.Optional)43 ConnectorSession (io.trino.spi.connector.ConnectorSession)41 TrinoException (io.trino.spi.TrinoException)38 ColumnHandle (io.trino.spi.connector.ColumnHandle)38 Map (java.util.Map)38 Type (io.trino.spi.type.Type)35 Constraint (io.trino.spi.connector.Constraint)32 ConnectorTableHandle (io.trino.spi.connector.ConnectorTableHandle)31 ConnectorMetadata (io.trino.spi.connector.ConnectorMetadata)30 BIGINT (io.trino.spi.type.BigintType.BIGINT)30 HashMap (java.util.HashMap)27 Set (java.util.Set)27