use of io.trino.metadata.TableMetadata 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");
}
use of io.trino.metadata.TableMetadata in project trino by trinodb.
the class BaseHiveConnectorTest method testInsertPartitionedTable.
private void testInsertPartitionedTable(Session session, HiveStorageFormat storageFormat) {
@Language("SQL") String createTable = "" + "CREATE TABLE test_insert_partitioned_table " + "(" + " ORDER_KEY BIGINT," + " SHIP_PRIORITY INTEGER," + " ORDER_STATUS VARCHAR" + ") " + "WITH (" + "format = '" + storageFormat + "', " + "partitioned_by = ARRAY[ 'SHIP_PRIORITY', 'ORDER_STATUS' ]" + ") ";
assertUpdate(session, createTable);
TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_insert_partitioned_table");
assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
assertEquals(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY), ImmutableList.of("ship_priority", "order_status"));
String partitionsTable = "\"test_insert_partitioned_table$partitions\"";
assertQuery(session, "SELECT * FROM " + partitionsTable, "SELECT shippriority, orderstatus FROM orders LIMIT 0");
// Hive will reorder the partition keys, so we must insert into the table assuming the partition keys have been moved to the end
assertUpdate(session, "" + "INSERT INTO test_insert_partitioned_table " + "SELECT orderkey, shippriority, orderstatus " + "FROM tpch.tiny.orders", "SELECT count(*) FROM orders");
// verify the partitions
List<?> partitions = getPartitions("test_insert_partitioned_table");
assertEquals(partitions.size(), 3);
assertQuery(session, "SELECT * FROM test_insert_partitioned_table", "SELECT orderkey, shippriority, orderstatus FROM orders");
assertQuery(session, "SELECT * FROM " + partitionsTable, "SELECT DISTINCT shippriority, orderstatus FROM orders");
assertQuery(session, "SELECT * FROM " + partitionsTable + " ORDER BY order_status LIMIT 2", "SELECT DISTINCT shippriority, orderstatus FROM orders ORDER BY orderstatus LIMIT 2");
assertQuery(session, "SELECT * FROM " + partitionsTable + " WHERE order_status = 'O'", "SELECT DISTINCT shippriority, orderstatus FROM orders WHERE orderstatus = 'O'");
assertQueryFails(session, "SELECT * FROM " + partitionsTable + " WHERE no_such_column = 1", "line \\S*: Column 'no_such_column' cannot be resolved");
assertQueryFails(session, "SELECT * FROM " + partitionsTable + " WHERE orderkey = 1", "line \\S*: Column 'orderkey' cannot be resolved");
assertUpdate(session, "DROP TABLE test_insert_partitioned_table");
assertFalse(getQueryRunner().tableExists(session, "test_insert_partitioned_table"));
}
use of io.trino.metadata.TableMetadata in project trino by trinodb.
the class BaseHiveConnectorTest method testAutoPurgeProperty.
@Test
public void testAutoPurgeProperty() {
String tableName = "test_auto_purge_property";
@Language("SQL") String createTableSql = format("" + "CREATE TABLE %s " + "AS " + "SELECT * FROM tpch.tiny.customer", tableName);
assertUpdate(createTableSql, 1500L);
TableMetadata tableMetadataDefaults = getTableMetadata(catalog, TPCH_SCHEMA, tableName);
assertEquals(tableMetadataDefaults.getMetadata().getProperties().get(AUTO_PURGE), null);
assertUpdate("DROP TABLE " + tableName);
@Language("SQL") String createTableSqlWithAutoPurge = format("" + "CREATE TABLE %s " + "WITH (" + " auto_purge = true" + ") AS " + "SELECT * FROM tpch.tiny.customer", tableName);
assertUpdate(createTableSqlWithAutoPurge, 1500L);
TableMetadata tableMetadataWithPurge = getTableMetadata(catalog, TPCH_SCHEMA, tableName);
assertEquals(tableMetadataWithPurge.getMetadata().getProperties().get(AUTO_PURGE), true);
assertUpdate("DROP TABLE " + tableName);
}
use of io.trino.metadata.TableMetadata in project trino by trinodb.
the class BaseHiveConnectorTest method testInsertPartitionedTableExistingPartition.
private void testInsertPartitionedTableExistingPartition(Session session, HiveStorageFormat storageFormat) {
String tableName = "test_insert_partitioned_table_existing_partition";
@Language("SQL") String createTable = "" + "CREATE TABLE " + tableName + " " + "(" + " order_key BIGINT," + " comment VARCHAR," + " order_status VARCHAR" + ") " + "WITH (" + "format = '" + storageFormat + "', " + "partitioned_by = ARRAY[ 'order_status' ]" + ") ";
assertUpdate(session, createTable);
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("order_status"));
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));
}
// verify the partitions
List<?> partitions = getPartitions(tableName);
assertEquals(partitions.size(), 3);
assertQuery(session, "SELECT * FROM " + tableName, "SELECT orderkey, comment, orderstatus FROM orders");
assertUpdate(session, "DROP TABLE " + tableName);
assertFalse(getQueryRunner().tableExists(session, tableName));
}
use of io.trino.metadata.TableMetadata in project trino by trinodb.
the class BaseHiveConnectorTest method testBucketedTable.
private void testBucketedTable(Session session, HiveStorageFormat storageFormat, boolean createEmpty) {
String tableName = "test_bucketed_table";
@Language("SQL") String createTable = "" + "CREATE TABLE " + tableName + " " + "WITH (" + "format = '" + storageFormat + "', " + "bucketed_by = ARRAY[ 'bucket_key' ], " + "bucket_count = 11 " + ") " + "AS " + "SELECT * " + "FROM (" + "VALUES " + " (VARCHAR 'a', VARCHAR 'b', VARCHAR 'c'), " + " ('aa', 'bb', 'cc'), " + " ('aaa', 'bbb', 'ccc')" + ") t (bucket_key, col_1, col_2)";
// make sure that we will get one file per bucket regardless of writer count configured
Session parallelWriter = Session.builder(getParallelWriteSession()).setCatalogSessionProperty(catalog, "create_empty_bucket_files", String.valueOf(createEmpty)).build();
assertUpdate(parallelWriter, createTable, 3);
TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, tableName);
assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
assertNull(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY));
assertEquals(tableMetadata.getMetadata().getProperties().get(BUCKETED_BY_PROPERTY), ImmutableList.of("bucket_key"));
assertEquals(tableMetadata.getMetadata().getProperties().get(BUCKET_COUNT_PROPERTY), 11);
assertQuery("SELECT * from " + tableName, "VALUES ('a', 'b', 'c'), ('aa', 'bb', 'cc'), ('aaa', 'bbb', 'ccc')");
assertUpdate(parallelWriter, "INSERT INTO " + tableName + " VALUES ('a0', 'b0', 'c0')", 1, // buckets should be repartitioned locally hence local repartitioned exchange should exist in plan
assertLocalRepartitionedExchangesCount(1));
assertUpdate(parallelWriter, "INSERT INTO " + tableName + " VALUES ('a1', 'b1', 'c1')", 1);
assertQuery("SELECT * from " + tableName, "VALUES ('a', 'b', 'c'), ('aa', 'bb', 'cc'), ('aaa', 'bbb', 'ccc'), ('a0', 'b0', 'c0'), ('a1', 'b1', 'c1')");
assertUpdate(session, "DROP TABLE " + tableName);
assertFalse(getQueryRunner().tableExists(session, tableName));
}
Aggregations