Search in sources :

Example 51 with SchemaTableName

use of com.facebook.presto.spi.SchemaTableName in project presto by prestodb.

the class TestJdbcClient method testAlterColumns.

// disabled due to https://github.com/prestodb/presto/issues/16081
@Test(enabled = false)
public void testAlterColumns() {
    String tableName = randomUUID().toString().toUpperCase(ENGLISH);
    SchemaTableName schemaTableName = new SchemaTableName("schema_for_create_table_tests", tableName);
    List<ColumnMetadata> expectedColumns = ImmutableList.of(new ColumnMetadata("columnA", BigintType.BIGINT, null, null, false));
    jdbcClient.createTable(session, new ConnectorTableMetadata(schemaTableName, expectedColumns));
    JdbcTableHandle tableHandle = jdbcClient.getTableHandle(JdbcIdentity.from(session), schemaTableName);
    try {
        assertEquals(tableHandle.getTableName(), tableName);
        assertEquals(jdbcClient.getColumns(session, tableHandle), ImmutableList.of(new JdbcColumnHandle(CONNECTOR_ID, "COLUMNA", JDBC_BIGINT, BigintType.BIGINT, true, Optional.empty())));
        jdbcClient.addColumn(JdbcIdentity.from(session), tableHandle, new ColumnMetadata("columnB", DoubleType.DOUBLE, null, null, false));
        assertEquals(jdbcClient.getColumns(session, tableHandle), ImmutableList.of(new JdbcColumnHandle(CONNECTOR_ID, "COLUMNA", JDBC_BIGINT, BigintType.BIGINT, true, Optional.empty()), new JdbcColumnHandle(CONNECTOR_ID, "COLUMNB", JDBC_DOUBLE, DoubleType.DOUBLE, true, Optional.empty())));
        jdbcClient.dropColumn(JdbcIdentity.from(session), tableHandle, new JdbcColumnHandle(CONNECTOR_ID, "COLUMNB", JDBC_BIGINT, BigintType.BIGINT, true, Optional.empty()));
        assertEquals(jdbcClient.getColumns(session, tableHandle), ImmutableList.of(new JdbcColumnHandle(CONNECTOR_ID, "COLUMNA", JDBC_BIGINT, BigintType.BIGINT, true, Optional.empty())));
    } finally {
        jdbcClient.dropTable(JdbcIdentity.from(session), tableHandle);
    }
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) Test(org.testng.annotations.Test)

Example 52 with SchemaTableName

use of com.facebook.presto.spi.SchemaTableName in project presto by prestodb.

the class BaseJdbcClient method createTable.

protected JdbcOutputTableHandle createTable(ConnectorTableMetadata tableMetadata, ConnectorSession session, String tableName) throws SQLException {
    SchemaTableName schemaTableName = tableMetadata.getTable();
    JdbcIdentity identity = JdbcIdentity.from(session);
    if (!getSchemaNames(identity).contains(schemaTableName.getSchemaName())) {
        throw new PrestoException(NOT_FOUND, "Schema not found: " + schemaTableName.getSchemaName());
    }
    try (Connection connection = connectionFactory.openConnection(identity)) {
        boolean uppercase = connection.getMetaData().storesUpperCaseIdentifiers();
        String remoteSchema = toRemoteSchemaName(identity, connection, schemaTableName.getSchemaName());
        String remoteTable = toRemoteTableName(identity, connection, remoteSchema, schemaTableName.getTableName());
        if (uppercase) {
            tableName = tableName.toUpperCase(ENGLISH);
        }
        String catalog = connection.getCatalog();
        ImmutableList.Builder<String> columnNames = ImmutableList.builder();
        ImmutableList.Builder<Type> columnTypes = ImmutableList.builder();
        ImmutableList.Builder<String> columnList = ImmutableList.builder();
        for (ColumnMetadata column : tableMetadata.getColumns()) {
            String columnName = column.getName();
            if (uppercase) {
                columnName = columnName.toUpperCase(ENGLISH);
            }
            columnNames.add(columnName);
            columnTypes.add(column.getType());
            columnList.add(getColumnString(column, columnName));
        }
        String sql = format("CREATE TABLE %s (%s)", quoted(catalog, remoteSchema, tableName), join(", ", columnList.build()));
        execute(connection, sql);
        return new JdbcOutputTableHandle(connectorId, catalog, remoteSchema, remoteTable, columnNames.build(), columnTypes.build(), tableName);
    }
}
Also used : Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) StandardReadMappings.jdbcTypeToPrestoType(com.facebook.presto.plugin.jdbc.StandardReadMappings.jdbcTypeToPrestoType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ImmutableList(com.google.common.collect.ImmutableList) Connection(java.sql.Connection) PrestoException(com.facebook.presto.spi.PrestoException) SchemaTableName(com.facebook.presto.spi.SchemaTableName)

Example 53 with SchemaTableName

use of com.facebook.presto.spi.SchemaTableName in project presto by prestodb.

the class TestJdbcMetadata method testCreateAndAlterTable.

@Test
public void testCreateAndAlterTable() {
    SchemaTableName table = new SchemaTableName("example", "foo");
    metadata.createTable(SESSION, new ConnectorTableMetadata(table, ImmutableList.of(new ColumnMetadata("text", VARCHAR))), false);
    JdbcTableHandle handle = metadata.getTableHandle(SESSION, table);
    ConnectorTableMetadata layout = metadata.getTableMetadata(SESSION, handle);
    assertEquals(layout.getTable(), table);
    assertEquals(layout.getColumns().size(), 1);
    assertEquals(layout.getColumns().get(0), new ColumnMetadata("text", VARCHAR));
    metadata.addColumn(SESSION, handle, new ColumnMetadata("x", VARCHAR));
    layout = metadata.getTableMetadata(SESSION, handle);
    assertEquals(layout.getColumns().size(), 2);
    assertEquals(layout.getColumns().get(0), new ColumnMetadata("text", VARCHAR));
    assertEquals(layout.getColumns().get(1), new ColumnMetadata("x", VARCHAR));
    JdbcColumnHandle columnHandle = new JdbcColumnHandle(CONNECTOR_ID, "x", JDBC_VARCHAR, VARCHAR, true, Optional.empty());
    metadata.dropColumn(SESSION, handle, columnHandle);
    layout = metadata.getTableMetadata(SESSION, handle);
    assertEquals(layout.getColumns().size(), 1);
    assertEquals(layout.getColumns().get(0), new ColumnMetadata("text", VARCHAR));
    SchemaTableName newTableName = new SchemaTableName("example", "bar");
    metadata.renameTable(SESSION, handle, newTableName);
    handle = metadata.getTableHandle(SESSION, newTableName);
    layout = metadata.getTableMetadata(SESSION, handle);
    assertEquals(layout.getTable(), newTableName);
    assertEquals(layout.getColumns().size(), 1);
    assertEquals(layout.getColumns().get(0), new ColumnMetadata("text", VARCHAR));
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) SchemaTableName(com.facebook.presto.spi.SchemaTableName) ConnectorTableMetadata(com.facebook.presto.spi.ConnectorTableMetadata) Test(org.testng.annotations.Test)

Example 54 with SchemaTableName

use of com.facebook.presto.spi.SchemaTableName in project presto by prestodb.

the class TestJdbcMetadata method testListTableColumns.

@Test
public void testListTableColumns() {
    SchemaTableName tpchOrders = new SchemaTableName("tpch", "orders");
    ImmutableList<ColumnMetadata> tpchOrdersColumnMetadata = ImmutableList.of(ColumnMetadata.builder().setName("orderkey").setType(BIGINT).setNullable(false).build(), ColumnMetadata.builder().setName("custkey").setType(BIGINT).setNullable(true).build());
    SchemaTableName tpchLineItem = new SchemaTableName("tpch", "lineitem");
    ImmutableList<ColumnMetadata> tpchLineItemColumnMetadata = ImmutableList.of(ColumnMetadata.builder().setName("orderkey").setType(BIGINT).setNullable(false).build(), ColumnMetadata.builder().setName("partkey").setType(BIGINT).setNullable(true).build());
    // List columns for a given schema and table
    Map<SchemaTableName, List<ColumnMetadata>> tpchOrdersColumns = metadata.listTableColumns(SESSION, new SchemaTablePrefix("tpch", "orders"));
    assertThat(tpchOrdersColumns).containsOnly(entry(tpchOrders, tpchOrdersColumnMetadata));
    // List columns for a given schema
    Map<SchemaTableName, List<ColumnMetadata>> tpchColumns = metadata.listTableColumns(SESSION, new SchemaTablePrefix("tpch"));
    assertThat(tpchColumns).containsOnly(entry(tpchOrders, tpchOrdersColumnMetadata), entry(tpchLineItem, tpchLineItemColumnMetadata));
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) SchemaTablePrefix(com.facebook.presto.spi.SchemaTablePrefix) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) SchemaTableName(com.facebook.presto.spi.SchemaTableName) Test(org.testng.annotations.Test)

Example 55 with SchemaTableName

use of com.facebook.presto.spi.SchemaTableName in project presto by prestodb.

the class TestJdbcClient method testMetadataWithFloatAndDoubleCol.

@Test
public void testMetadataWithFloatAndDoubleCol() {
    SchemaTableName schemaTableName = new SchemaTableName("exa_ple", "table_with_float_col");
    JdbcTableHandle table = jdbcClient.getTableHandle(JdbcIdentity.from(session), schemaTableName);
    assertNotNull(table, "table is null");
    assertEquals(jdbcClient.getColumns(session, table), ImmutableList.of(new JdbcColumnHandle(CONNECTOR_ID, "COL1", JDBC_BIGINT, BIGINT, true, Optional.empty()), new JdbcColumnHandle(CONNECTOR_ID, "COL2", JDBC_DOUBLE, DOUBLE, true, Optional.empty()), new JdbcColumnHandle(CONNECTOR_ID, "COL3", JDBC_DOUBLE, DOUBLE, true, Optional.empty()), new JdbcColumnHandle(CONNECTOR_ID, "COL4", JDBC_REAL, REAL, true, Optional.empty())));
}
Also used : SchemaTableName(com.facebook.presto.spi.SchemaTableName) Test(org.testng.annotations.Test)

Aggregations

SchemaTableName (com.facebook.presto.spi.SchemaTableName)370 ImmutableList (com.google.common.collect.ImmutableList)122 Test (org.testng.annotations.Test)107 List (java.util.List)100 PrestoException (com.facebook.presto.spi.PrestoException)99 ImmutableMap (com.google.common.collect.ImmutableMap)95 TableNotFoundException (com.facebook.presto.spi.TableNotFoundException)91 ConnectorTableMetadata (com.facebook.presto.spi.ConnectorTableMetadata)79 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)73 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)68 ConnectorSession (com.facebook.presto.spi.ConnectorSession)62 ArrayList (java.util.ArrayList)60 ColumnHandle (com.facebook.presto.spi.ColumnHandle)58 Table (com.facebook.presto.hive.metastore.Table)57 Map (java.util.Map)57 Optional (java.util.Optional)55 Column (com.facebook.presto.hive.metastore.Column)52 Collectors.toList (java.util.stream.Collectors.toList)48 Path (org.apache.hadoop.fs.Path)47 Objects.requireNonNull (java.util.Objects.requireNonNull)46