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);
}
}
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);
}
}
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));
}
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));
}
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())));
}
Aggregations