Search in sources :

Example 21 with TableMetadata

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

the class BaseHiveConnectorTest method createTableLike.

private void createTableLike(String likeSuffix, boolean hasPartition) {
    // Create a non-partitioned table
    @Language("SQL") String createTable = "" + "CREATE TABLE test_table_original (" + "  tinyint_col tinyint " + ", smallint_col smallint" + ")";
    assertUpdate(createTable);
    // Verify the table is correctly created
    TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_table_original");
    assertColumnType(tableMetadata, "tinyint_col", TINYINT);
    assertColumnType(tableMetadata, "smallint_col", SMALLINT);
    // Create a partitioned table
    @Language("SQL") String createPartitionedTable = "" + "CREATE TABLE test_partitioned_table_original (" + "  string_col VARCHAR" + ", decimal_long_col DECIMAL(30,10)" + ", partition_bigint BIGINT" + ", partition_decimal_long DECIMAL(30,10)" + ") " + "WITH (" + "partitioned_by = ARRAY['partition_bigint', 'partition_decimal_long']" + ")";
    assertUpdate(createPartitionedTable);
    // Verify the table is correctly created
    tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_partitioned_table_original");
    // Verify the partition keys are correctly created
    List<String> partitionedBy = ImmutableList.of("partition_bigint", "partition_decimal_long");
    assertEquals(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY), partitionedBy);
    // Verify the column types
    assertColumnType(tableMetadata, "string_col", createUnboundedVarcharType());
    assertColumnType(tableMetadata, "partition_bigint", BIGINT);
    assertColumnType(tableMetadata, "partition_decimal_long", createDecimalType(30, 10));
    // Create a table using only one LIKE
    @Language("SQL") String createTableSingleLike = "" + "CREATE TABLE test_partitioned_table_single_like (" + "LIKE test_partitioned_table_original " + likeSuffix + ")";
    assertUpdate(createTableSingleLike);
    tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_partitioned_table_single_like");
    // Verify the partitioned keys are correctly created if copying partition columns
    verifyPartition(hasPartition, tableMetadata, partitionedBy);
    // Verify the column types
    assertColumnType(tableMetadata, "string_col", createUnboundedVarcharType());
    assertColumnType(tableMetadata, "partition_bigint", BIGINT);
    assertColumnType(tableMetadata, "partition_decimal_long", createDecimalType(30, 10));
    @Language("SQL") String createTableLikeExtra = "" + "CREATE TABLE test_partitioned_table_like_extra (" + "  bigint_col BIGINT" + ", double_col DOUBLE" + ", LIKE test_partitioned_table_single_like " + likeSuffix + ")";
    assertUpdate(createTableLikeExtra);
    tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_partitioned_table_like_extra");
    // Verify the partitioned keys are correctly created if copying partition columns
    verifyPartition(hasPartition, tableMetadata, partitionedBy);
    // Verify the column types
    assertColumnType(tableMetadata, "bigint_col", BIGINT);
    assertColumnType(tableMetadata, "double_col", DOUBLE);
    assertColumnType(tableMetadata, "string_col", createUnboundedVarcharType());
    assertColumnType(tableMetadata, "partition_bigint", BIGINT);
    assertColumnType(tableMetadata, "partition_decimal_long", createDecimalType(30, 10));
    @Language("SQL") String createTableDoubleLike = "" + "CREATE TABLE test_partitioned_table_double_like (" + "  LIKE test_table_original " + ", LIKE test_partitioned_table_like_extra " + likeSuffix + ")";
    assertUpdate(createTableDoubleLike);
    tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_partitioned_table_double_like");
    // Verify the partitioned keys are correctly created if copying partition columns
    verifyPartition(hasPartition, tableMetadata, partitionedBy);
    // Verify the column types
    assertColumnType(tableMetadata, "tinyint_col", TINYINT);
    assertColumnType(tableMetadata, "smallint_col", SMALLINT);
    assertColumnType(tableMetadata, "string_col", createUnboundedVarcharType());
    assertColumnType(tableMetadata, "partition_bigint", BIGINT);
    assertColumnType(tableMetadata, "partition_decimal_long", createDecimalType(30, 10));
    assertUpdate("DROP TABLE test_table_original");
    assertUpdate("DROP TABLE test_partitioned_table_original");
    assertUpdate("DROP TABLE test_partitioned_table_single_like");
    assertUpdate("DROP TABLE test_partitioned_table_like_extra");
    assertUpdate("DROP TABLE test_partitioned_table_double_like");
}
Also used : TableMetadata(io.trino.metadata.TableMetadata) Language(org.intellij.lang.annotations.Language)

Example 22 with TableMetadata

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

the class BaseHiveConnectorTest method testEmptyBucketedTable.

private void testEmptyBucketedTable(Session session, HiveStorageFormat storageFormat, boolean createEmpty) {
    String tableName = "test_empty_bucketed_table";
    @Language("SQL") String createTable = "" + "CREATE TABLE " + tableName + " " + "(bucket_key VARCHAR, col_1 VARCHAR, col2 VARCHAR) " + "WITH (" + "format = '" + storageFormat + "', " + "bucketed_by = ARRAY[ 'bucket_key' ], " + "bucket_count = 11 " + ") ";
    assertUpdate(createTable);
    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);
    assertEquals(computeActual("SELECT * from " + tableName).getRowCount(), 0);
    // 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, "INSERT INTO " + tableName + " VALUES ('a0', 'b0', 'c0')", 1);
    assertUpdate(parallelWriter, "INSERT INTO " + tableName + " VALUES ('a1', 'b1', 'c1')", 1);
    assertQuery("SELECT * from " + tableName, "VALUES ('a0', 'b0', 'c0'), ('a1', 'b1', 'c1')");
    assertUpdate(session, "DROP TABLE " + tableName);
    assertFalse(getQueryRunner().tableExists(session, tableName));
}
Also used : TableMetadata(io.trino.metadata.TableMetadata) Language(org.intellij.lang.annotations.Language) HiveQueryRunner.createBucketedSession(io.trino.plugin.hive.HiveQueryRunner.createBucketedSession) Session(io.trino.Session)

Example 23 with TableMetadata

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

the class BaseHiveConnectorTest method testBucketedCatalog.

@Test
public void testBucketedCatalog() {
    String bucketedCatalog = bucketedSession.getCatalog().get();
    String bucketedSchema = bucketedSession.getSchema().get();
    TableMetadata ordersTableMetadata = getTableMetadata(bucketedCatalog, bucketedSchema, "orders");
    assertEquals(ordersTableMetadata.getMetadata().getProperties().get(BUCKETED_BY_PROPERTY), ImmutableList.of("custkey"));
    assertEquals(ordersTableMetadata.getMetadata().getProperties().get(BUCKET_COUNT_PROPERTY), 11);
    TableMetadata customerTableMetadata = getTableMetadata(bucketedCatalog, bucketedSchema, "customer");
    assertEquals(customerTableMetadata.getMetadata().getProperties().get(BUCKETED_BY_PROPERTY), ImmutableList.of("custkey"));
    assertEquals(customerTableMetadata.getMetadata().getProperties().get(BUCKET_COUNT_PROPERTY), 11);
}
Also used : TableMetadata(io.trino.metadata.TableMetadata) Test(org.testng.annotations.Test) BaseConnectorTest(io.trino.testing.BaseConnectorTest)

Example 24 with TableMetadata

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

the class BaseHiveConnectorTest method testInsertPartitionedTableOverwriteExistingPartition.

private void testInsertPartitionedTableOverwriteExistingPartition(Session session, HiveStorageFormat storageFormat) {
    String tableName = "test_insert_partitioned_table_overwrite_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, format("SELECT orderkey, comment, orderstatus FROM orders WHERE orderkey %% 3 = %d", i));
    }
    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 25 with TableMetadata

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

the class BaseHiveConnectorTest method testCreateTableAs.

private void testCreateTableAs(Session session, HiveStorageFormat storageFormat) {
    @Language("SQL") String select = "SELECT" + " 'foo' _varchar" + ", CAST('bar' AS CHAR(10)) _char" + ", CAST (1 AS BIGINT) _bigint" + ", 2 _integer" + ", CAST (3 AS SMALLINT) _smallint" + ", CAST (4 AS TINYINT) _tinyint" + ", CAST ('123.45' as REAL) _real" + ", CAST('3.14' AS DOUBLE) _double" + ", true _boolean" + ", CAST('3.14' AS DECIMAL(3,2)) _decimal_short" + ", CAST('12345678901234567890.0123456789' AS DECIMAL(30,10)) _decimal_long";
    if (storageFormat == HiveStorageFormat.AVRO) {
        select = select.replace(" CAST (3 AS SMALLINT) _smallint,", " 3 _smallint,");
        select = select.replace(" CAST (4 AS TINYINT) _tinyint,", " 4 _tinyint,");
    }
    String createTableAs = format("CREATE TABLE test_format_table WITH (format = '%s') AS %s", storageFormat, select);
    assertUpdate(session, createTableAs, 1);
    TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_format_table");
    assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
    assertColumnType(tableMetadata, "_varchar", createVarcharType(3));
    assertColumnType(tableMetadata, "_char", createCharType(10));
    // assure reader supports basic column reordering and pruning
    assertQuery(session, "SELECT _integer, _varchar, _integer FROM test_format_table", "SELECT 2, 'foo', 2");
    assertQuery(session, "SELECT * FROM test_format_table", select);
    assertUpdate(session, "DROP TABLE test_format_table");
    assertFalse(getQueryRunner().tableExists(session, "test_format_table"));
}
Also used : TableMetadata(io.trino.metadata.TableMetadata) Language(org.intellij.lang.annotations.Language)

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