Skip to content

Commit

Permalink
Support FIRST and AFTER clause when adding a new column in MariaDb
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjian2664 committed Jan 22, 2025
1 parent 6a89864 commit dee97b0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import io.trino.spi.TrinoException;
import io.trino.spi.connector.AggregateFunction;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ColumnMetadata;
import io.trino.spi.connector.ColumnPosition;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.ConnectorTableMetadata;
import io.trino.spi.connector.JoinCondition;
Expand Down Expand Up @@ -518,6 +520,43 @@ else if (varcharType.getBoundedLength() <= 16777215) {
throw new TrinoException(NOT_SUPPORTED, "Unsupported column type: " + type.getDisplayName());
}

@Override
public void addColumn(ConnectorSession session, JdbcTableHandle handle, ColumnMetadata column, ColumnPosition position)
{
verify(handle.getAuthorization().isEmpty(), "Unexpected authorization is required for table: %s", handle);

RemoteTableName table = handle.asPlainTable().getRemoteTableName();

switch (position) {
case ColumnPosition.First _ -> addColumn(session, table, column, "FIRST");
case ColumnPosition.After after -> addColumn(session, table, column, "AFTER " + quoted(after.columnName()));
case ColumnPosition.Last _ -> addColumn(session, table, column, "");
}
}

private void addColumn(ConnectorSession session, RemoteTableName table, ColumnMetadata column, String position)
{
if (column.getComment() != null) {
throw new TrinoException(NOT_SUPPORTED, "This connector does not support adding columns with comments");
}

try (Connection connection = connectionFactory.openConnection(session)) {
verify(connection.getAutoCommit());
String columnName = column.getName();
verifyColumnName(connection.getMetaData(), columnName);
String remoteColumnName = getIdentifierMapping().toRemoteColumnName(getRemoteIdentifiers(connection), columnName);
String sql = format(
"ALTER TABLE %s ADD %s %s",
quoted(table),
getColumnDefinitionSql(session, column, remoteColumnName),
position);
execute(session, connection, sql);
}
catch (SQLException e) {
throw new TrinoException(JDBC_ERROR, e);
}
}

@Override
protected void renameColumn(ConnectorSession session, Connection connection, RemoteTableName remoteTableName, String remoteColumnName, String newRemoteColumnName)
throws SQLException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public abstract class BaseMariaDbConnectorTest
protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
{
return switch (connectorBehavior) {
case SUPPORTS_JOIN_PUSHDOWN -> true;
case SUPPORTS_ADD_COLUMN_WITH_POSITION,
SUPPORTS_JOIN_PUSHDOWN -> true;
case SUPPORTS_ADD_COLUMN_WITH_COMMENT,
SUPPORTS_AGGREGATION_PUSHDOWN_CORRELATION,
SUPPORTS_AGGREGATION_PUSHDOWN_COUNT_DISTINCT,
Expand Down

0 comments on commit dee97b0

Please sign in to comment.