Search in sources :

Example 6 with TableMetadata

use of io.trino.metadata.TableMetadata in project trino by trinodb.

the class BaseHiveConnectorTest method testCreatePartitionedTableAs.

private void testCreatePartitionedTableAs(Session session, HiveStorageFormat storageFormat) {
    @Language("SQL") String createTable = "" + "CREATE TABLE test_create_partitioned_table_as " + "WITH (" + "format = '" + storageFormat + "', " + "partitioned_by = ARRAY[ 'SHIP_PRIORITY', 'ORDER_STATUS' ]" + ") " + "AS " + "SELECT orderkey AS order_key, shippriority AS ship_priority, orderstatus AS order_status " + "FROM tpch.tiny.orders";
    assertUpdate(session, createTable, "SELECT count(*) FROM orders");
    TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_create_partitioned_table_as");
    assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
    assertEquals(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY), ImmutableList.of("ship_priority", "order_status"));
    List<?> partitions = getPartitions("test_create_partitioned_table_as");
    assertEquals(partitions.size(), 3);
    assertQuery(session, "SELECT * FROM test_create_partitioned_table_as", "SELECT orderkey, shippriority, orderstatus FROM orders");
    assertUpdate(session, "DROP TABLE test_create_partitioned_table_as");
    assertFalse(getQueryRunner().tableExists(session, "test_create_partitioned_table_as"));
}
Also used : TableMetadata(io.trino.metadata.TableMetadata) Language(org.intellij.lang.annotations.Language)

Example 7 with TableMetadata

use of io.trino.metadata.TableMetadata in project trino by trinodb.

the class BaseHiveConnectorTest method testInsertUnpartitionedTable.

private void testInsertUnpartitionedTable(Session session, HiveStorageFormat storageFormat) {
    String tableName = "test_insert_unpartitioned_table";
    @Language("SQL") String createTable = "" + "CREATE TABLE " + tableName + " " + "(" + "  order_key BIGINT," + "  comment VARCHAR," + "  order_status VARCHAR" + ") " + "WITH (" + "format = '" + storageFormat + "'" + ") ";
    assertUpdate(session, createTable);
    TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, tableName);
    assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
    for (int i = 0; i < 3; i++) {
        assertUpdate(session, format("INSERT INTO " + tableName + " " + "SELECT orderkey, comment, orderstatus " + "FROM tpch.tiny.orders " + "WHERE orderkey %% 3 = %d", i), format("SELECT count(*) FROM orders WHERE orderkey %% 3 = %d", i));
    }
    assertQuery(session, "SELECT * FROM " + tableName, "SELECT orderkey, comment, orderstatus FROM orders");
    assertUpdate(session, "DROP TABLE " + tableName);
    assertFalse(getQueryRunner().tableExists(session, tableName));
}
Also used : TableMetadata(io.trino.metadata.TableMetadata) Language(org.intellij.lang.annotations.Language) ColumnConstraint(io.trino.sql.planner.planprinter.IoPlanPrinter.ColumnConstraint) Constraint(io.trino.spi.connector.Constraint)

Example 8 with TableMetadata

use of io.trino.metadata.TableMetadata in project trino by trinodb.

the class BaseHiveConnectorTest method getTableMetadata.

private TableMetadata getTableMetadata(String catalog, String schema, String tableName) {
    Session session = getSession();
    Metadata metadata = getDistributedQueryRunner().getCoordinator().getMetadata();
    return transaction(getQueryRunner().getTransactionManager(), getQueryRunner().getAccessControl()).readOnly().execute(session, transactionSession -> {
        Optional<TableHandle> tableHandle = metadata.getTableHandle(transactionSession, new QualifiedObjectName(catalog, schema, tableName));
        assertTrue(tableHandle.isPresent());
        return metadata.getTableMetadata(transactionSession, tableHandle.get());
    });
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) TableMetadata(io.trino.metadata.TableMetadata) Metadata(io.trino.metadata.Metadata) InsertTableHandle(io.trino.metadata.InsertTableHandle) TableHandle(io.trino.metadata.TableHandle) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) HiveQueryRunner.createBucketedSession(io.trino.plugin.hive.HiveQueryRunner.createBucketedSession) Session(io.trino.Session)

Example 9 with TableMetadata

use of io.trino.metadata.TableMetadata 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 TableMetadata

use of io.trino.metadata.TableMetadata in project trino by trinodb.

the class BaseHiveConnectorTest method verifyPartitionedBucketedTableAsFewRows.

private void verifyPartitionedBucketedTableAsFewRows(HiveStorageFormat storageFormat, String tableName) {
    TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, tableName);
    assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
    assertEquals(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY), ImmutableList.of("partition_key"));
    assertEquals(tableMetadata.getMetadata().getProperties().get(BUCKETED_BY_PROPERTY), ImmutableList.of("bucket_key"));
    assertEquals(tableMetadata.getMetadata().getProperties().get(BUCKET_COUNT_PROPERTY), 11);
    List<?> partitions = getPartitions(tableName);
    assertEquals(partitions.size(), 3);
    MaterializedResult actual = computeActual("SELECT * FROM " + tableName);
    MaterializedResult expected = resultBuilder(getSession(), canonicalizeType(createUnboundedVarcharType()), canonicalizeType(createUnboundedVarcharType()), canonicalizeType(createUnboundedVarcharType())).row("a", "b", "c").row("aa", "bb", "cc").row("aaa", "bbb", "ccc").build();
    assertEqualsIgnoreOrder(actual.getMaterializedRows(), expected.getMaterializedRows());
}
Also used : TableMetadata(io.trino.metadata.TableMetadata) MaterializedResult(io.trino.testing.MaterializedResult)

Aggregations

TableMetadata (io.trino.metadata.TableMetadata)30 Language (org.intellij.lang.annotations.Language)18 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)10 Constraint (io.trino.spi.connector.Constraint)10 ColumnConstraint (io.trino.sql.planner.planprinter.IoPlanPrinter.ColumnConstraint)10 TableHandle (io.trino.metadata.TableHandle)7 BaseConnectorTest (io.trino.testing.BaseConnectorTest)7 MaterializedResult (io.trino.testing.MaterializedResult)7 Test (org.testng.annotations.Test)7 Session (io.trino.Session)6 QualifiedObjectName (io.trino.metadata.QualifiedObjectName)5 MaterializedRow (io.trino.testing.MaterializedRow)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 HiveQueryRunner.createBucketedSession (io.trino.plugin.hive.HiveQueryRunner.createBucketedSession)4 ColumnHandle (io.trino.spi.connector.ColumnHandle)4 ConnectorTableMetadata (io.trino.spi.connector.ConnectorTableMetadata)4 TableScanNode (io.trino.sql.planner.plan.TableScanNode)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 ImmutableList (com.google.common.collect.ImmutableList)3