Skip to content

Commit

Permalink
Support setting column types in PostgreSQL connector
Browse files Browse the repository at this point in the history
  • Loading branch information
ebyhr committed Jan 10, 2023
1 parent 07152c0 commit bbfa269
Show file tree
Hide file tree
Showing 45 changed files with 335 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
return false;

case SUPPORTS_ADD_COLUMN:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_COMMENT_ON_TABLE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,24 @@ public void dropColumn(ConnectorSession session, JdbcTableHandle handle, JdbcCol
}
}

@Override
public void setColumnType(ConnectorSession session, JdbcTableHandle handle, JdbcColumnHandle column, Type type)
{
try (Connection connection = connectionFactory.openConnection(session)) {
verify(connection.getAutoCommit());
String remoteColumnName = identifierMapping.toRemoteColumnName(connection, column.getColumnName());
String sql = format(
"ALTER TABLE %s ALTER COLUMN %s SET DATA TYPE %s",
quoted(handle.asPlainTable().getRemoteTableName()),
quoted(remoteColumnName),
toWriteMapping(session, type).getDataType());
execute(session, connection, sql);
}
catch (SQLException e) {
throw new TrinoException(JDBC_ERROR, e);
}
}

@Override
public void dropTable(ConnectorSession session, JdbcTableHandle handle)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,13 @@ public void renameColumn(ConnectorSession session, JdbcTableHandle handle, JdbcC
invalidateTableCaches(handle.asPlainTable().getSchemaTableName());
}

@Override
public void setColumnType(ConnectorSession session, JdbcTableHandle handle, JdbcColumnHandle column, Type type)
{
delegate.setColumnType(session, handle, column, type);
invalidateTableCaches(handle.asPlainTable().getSchemaTableName());
}

@Override
public void renameTable(ConnectorSession session, JdbcTableHandle handle, SchemaTableName newTableName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import io.trino.spi.security.TrinoPrincipal;
import io.trino.spi.statistics.ComputedStatistics;
import io.trino.spi.statistics.TableStatistics;
import io.trino.spi.type.Type;

import java.sql.Types;
import java.util.ArrayList;
Expand Down Expand Up @@ -907,6 +908,15 @@ public void renameColumn(ConnectorSession session, ConnectorTableHandle table, C
jdbcClient.renameColumn(session, tableHandle, columnHandle, target);
}

@Override
public void setColumnType(ConnectorSession session, ConnectorTableHandle table, ColumnHandle column, Type type)
{
JdbcTableHandle tableHandle = (JdbcTableHandle) table;
JdbcColumnHandle columnHandle = (JdbcColumnHandle) column;
verify(!tableHandle.isSynthetic(), "Not a table reference: %s", tableHandle);
jdbcClient.setColumnType(session, tableHandle, columnHandle, type);
}

@Override
public void renameTable(ConnectorSession session, ConnectorTableHandle table, SchemaTableName newTableName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ public void renameColumn(ConnectorSession session, JdbcTableHandle handle, JdbcC
delegate().renameColumn(session, handle, jdbcColumn, newColumnName);
}

@Override
public void setColumnType(ConnectorSession session, JdbcTableHandle handle, JdbcColumnHandle column, Type type)
{
delegate().setColumnType(session, handle, column, type);
}

@Override
public void renameTable(ConnectorSession session, JdbcTableHandle handle, SchemaTableName newTableName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ default void setColumnComment(ConnectorSession session, JdbcTableHandle handle,

void renameColumn(ConnectorSession session, JdbcTableHandle handle, JdbcColumnHandle jdbcColumn, String newColumnName);

void setColumnType(ConnectorSession session, JdbcTableHandle handle, JdbcColumnHandle column, Type type);

void renameTable(ConnectorSession session, JdbcTableHandle handle, SchemaTableName newTableName);

default void setTableProperties(ConnectorSession session, JdbcTableHandle handle, Map<String, Optional<Object>> properties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public final class JdbcClientStats
private final JdbcApiStats getTableNames = new JdbcApiStats();
private final JdbcApiStats getTableStatistics = new JdbcApiStats();
private final JdbcApiStats renameColumn = new JdbcApiStats();
private final JdbcApiStats setColumnType = new JdbcApiStats();
private final JdbcApiStats renameTable = new JdbcApiStats();
private final JdbcApiStats setTableProperties = new JdbcApiStats();
private final JdbcApiStats rollbackCreateTable = new JdbcApiStats();
Expand Down Expand Up @@ -271,6 +272,13 @@ public JdbcApiStats getRenameColumn()
return renameColumn;
}

@Managed
@Nested
public JdbcApiStats getSetColumnType()
{
return setColumnType;
}

@Managed
@Nested
public JdbcApiStats getRenameTable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ public void renameColumn(ConnectorSession session, JdbcTableHandle handle, JdbcC
stats.getRenameColumn().wrap(() -> delegate().renameColumn(session, handle, jdbcColumn, newColumnName));
}

@Override
public void setColumnType(ConnectorSession session, JdbcTableHandle handle, JdbcColumnHandle column, Type type)
{
stats.getSetColumnType().wrap(() -> delegate().setColumnType(session, handle, column, type));
}

@Override
public void renameTable(ConnectorSession session, JdbcTableHandle handle, SchemaTableName newTableName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ public void testAddColumnConcurrently()
throw new SkipException("TODO: Enable this test after finding the failure cause");
}

@Override
protected void verifySetColumnTypeFailurePermissible(Throwable e)
{
assertThat(e).hasMessageMatching("(?s).*(Data conversion error converting|value out of range).*");
}

@Override
protected JdbcSqlExecutor onRemoteDatabase()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)

case SUPPORTS_ADD_COLUMN:
case SUPPORTS_RENAME_COLUMN:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_NOT_NULL_CONSTRAINT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)

case SUPPORTS_ADD_COLUMN:
case SUPPORTS_RENAME_COLUMN:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_COMMENT_ON_TABLE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,12 @@ public void setColumnComment(ConnectorSession session, JdbcTableHandle handle, J
execute(session, sql);
}

@Override
public void setColumnType(ConnectorSession session, JdbcTableHandle handle, JdbcColumnHandle column, Type type)
{
throw new TrinoException(NOT_SUPPORTED, "This connector does not support setting column types");
}

private static String clickhouseVarcharLiteral(String value)
{
requireNonNull(value, "value is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_TOPN_PUSHDOWN:
return false;

case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_DELETE:
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)

case SUPPORTS_DROP_COLUMN:
case SUPPORTS_RENAME_COLUMN:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_DELETE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,12 @@ public void renameColumn(ConnectorSession session, JdbcTableHandle handle, JdbcC
throw new TrinoException(NOT_SUPPORTED, "This connector does not support renaming columns");
}

@Override
public void setColumnType(ConnectorSession session, JdbcTableHandle handle, JdbcColumnHandle column, Type type)
{
throw new TrinoException(NOT_SUPPORTED, "This connector does not support setting column types");
}

@Override
public void dropColumn(ConnectorSession session, JdbcTableHandle handle, JdbcColumnHandle column)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)

case SUPPORTS_ADD_COLUMN:
case SUPPORTS_RENAME_COLUMN:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_COMMENT_ON_TABLE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)

case SUPPORTS_ADD_COLUMN:
case SUPPORTS_RENAME_COLUMN:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_COMMENT_ON_TABLE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_COMMENT_ON_VIEW_COLUMN:
return true;

case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_CREATE_VIEW:
return true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)

case SUPPORTS_ADD_COLUMN:
case SUPPORTS_RENAME_COLUMN:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_COMMENT_ON_TABLE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_COMMENT_ON_VIEW_COLUMN:
return true;

case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_CREATE_VIEW:
return true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)

case SUPPORTS_ADD_COLUMN:
case SUPPORTS_RENAME_COLUMN:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_COMMENT_ON_TABLE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_COMMENT_ON_COLUMN:
return false;

case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_NOT_NULL_CONSTRAINT:
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,12 @@ protected void renameColumn(ConnectorSession session, Connection connection, Rem
}
}

@Override
public void setColumnType(ConnectorSession session, JdbcTableHandle handle, JdbcColumnHandle column, Type type)
{
throw new TrinoException(NOT_SUPPORTED, "This connector does not support setting column types");
}

@Override
protected void copyTableSchema(ConnectorSession session, Connection connection, String catalogName, String schemaName, String tableName, String newTableName, List<String> columnNames)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
return false;

case SUPPORTS_ADD_COLUMN_WITH_COMMENT:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_COMMENT_ON_COLUMN:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)

case SUPPORTS_ADD_COLUMN:
case SUPPORTS_RENAME_COLUMN:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_COMMENT_ON_VIEW:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
return false;

case SUPPORTS_RENAME_COLUMN:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_NOT_NULL_CONSTRAINT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,12 @@ protected void renameColumn(ConnectorSession session, Connection connection, Rem
execute(session, connection, sql);
}

@Override
public void setColumnType(ConnectorSession session, JdbcTableHandle handle, JdbcColumnHandle column, Type type)
{
throw new TrinoException(NOT_SUPPORTED, "This connector does not support setting column types");
}

@Override
public void renameSchema(ConnectorSession session, String schemaName, String newSchemaName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
return false;

case SUPPORTS_ADD_COLUMN_WITH_COMMENT:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_COMMENT_ON_COLUMN:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,4 +678,10 @@ public void setColumnComment(ConnectorSession session, JdbcTableHandle handle, J
varcharLiteral(comment.orElse("")));
execute(session, sql);
}

@Override
public void setColumnType(ConnectorSession session, JdbcTableHandle handle, JdbcColumnHandle column, Type type)
{
throw new TrinoException(NOT_SUPPORTED, "This connector does not support setting column types");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
return false;

case SUPPORTS_ADD_COLUMN_WITH_COMMENT:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_COMMENT_ON_TABLE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,12 @@ public Map<String, Object> getTableProperties(ConnectorSession session, JdbcTabl
return properties.buildOrThrow();
}

@Override
public void setColumnType(ConnectorSession session, JdbcTableHandle handle, JdbcColumnHandle column, Type type)
{
throw new TrinoException(NOT_SUPPORTED, "This connector does not support setting column types");
}

private static LongReadFunction dateReadFunction()
{
return (resultSet, index) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
return false;

case SUPPORTS_ADD_COLUMN_WITH_COMMENT:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_COMMENT_ON_TABLE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.UUID;
import java.util.stream.Stream;
Expand Down Expand Up @@ -1025,4 +1026,20 @@ protected void verifyColumnNameLengthFailurePermissible(Throwable e)
{
assertThat(e).hasMessageMatching("Column name must be shorter than or equal to '63' characters but got '64': '.*'");
}

@Override
protected void verifySetColumnTypeFailurePermissible(Throwable e)
{
assertThat(e).hasMessageMatching("(?s)ERROR: .*(cannot be cast automatically to type|out of range).*");
}

@Override
protected Optional<SetColumnTypeSetup> filterSetColumnTypesDataProvider(SetColumnTypeSetup setup)
{
// The connector returns UTC instead of the given time zone
if (setup.sourceColumnType().equals("timestamp(3) with time zone")) {
return Optional.of(new SetColumnTypeSetup(setup.sourceColumnType(), setup.sourceValueLiteral(), setup.newColumnType(), "timestamp '2020-02-12 14:03:00.123000 +00:00'"));
}
return Optional.of(setup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
return false;

case SUPPORTS_ADD_COLUMN_WITH_COMMENT:
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_COMMENT_ON_TABLE:
Expand Down
Loading

0 comments on commit bbfa269

Please sign in to comment.