Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -302,29 +302,40 @@ protected void verifyColumnName(DatabaseMetaData databaseMetadata, String column
protected void renameTable(ConnectorSession session, Connection connection, String catalogName, String remoteSchemaName, String remoteTableName, String newRemoteSchemaName, String newRemoteTableName)
throws SQLException
{
// sp_rename treats first argument as SQL object name, so it needs to be properly quoted and escaped.
// The second argument is treated literally.
if (!remoteSchemaName.equals(newRemoteSchemaName)) {
throw new TrinoException(NOT_SUPPORTED, "This connector does not support renaming tables across schemas");
}

execute(connection, format(
"sp_rename %s, %s",
singleQuote(catalogName, remoteSchemaName, remoteTableName),
singleQuote(newRemoteTableName)));
String fullTableFromName = DOT_JOINER.join(
quoted(catalogName),
quoted(remoteSchemaName),
quoted(remoteTableName));

try (CallableStatement renameTable = connection.prepareCall("exec sp_rename ?, ?")) {
renameTable.setString(1, fullTableFromName);
renameTable.setString(2, newRemoteTableName);
Comment on lines 316 to 317
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fullTableFromName -> oldTableName

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oldTableFullName (it's not just the table name)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I didn't like full I've spent some time to understand that it's full name of the table which has to be renamed.
oldTableFullName - table is not old. Full name is old.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tableOldFullName?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest I don't think there are any good names here - I'll just go with what exists already. 😄 We can rename when we come up with better name.

renameTable.execute();
}
}
Comment thread
vlad-lyutenko marked this conversation as resolved.
Outdated

@Override
protected void renameColumn(ConnectorSession session, Connection connection, RemoteTableName remoteTableName, String remoteColumnName, String newRemoteColumnName)
throws SQLException
{
execute(connection, format(
"sp_rename %s, %s, 'COLUMN'",
singleQuote(remoteTableName.getCatalogName().orElse(null), remoteTableName.getSchemaName().orElse(null), remoteTableName.getTableName(), "[" + escape(remoteColumnName) + "]"),
"[" + newRemoteColumnName + "]"));
}

private static String escape(String name)
{
return name.replace("'", "''");
// sp_rename treats first argument as SQL object name, so it needs to be properly quoted and escaped.
// The second arqgument is treated literally.
String columnFrom = DOT_JOINER.join(
Comment thread
vlad-lyutenko marked this conversation as resolved.
Outdated
quoted(remoteTableName.getCatalogName().orElseThrow()),
quoted(remoteTableName.getSchemaName().orElseThrow()),
quoted(remoteTableName.getTableName()),
quoted(remoteColumnName));

try (CallableStatement renameColumn = connection.prepareCall("exec sp_rename ?, ?, 'COLUMN'")) {
renameColumn.setString(1, columnFrom);
renameColumn.setString(2, newRemoteColumnName);
renameColumn.execute();
}
}

@Override
Expand Down Expand Up @@ -974,16 +985,6 @@ public Connection getConnection(ConnectorSession session, JdbcOutputTableHandle
return connection;
}

private static String singleQuote(String... objects)
{
return singleQuote(DOT_JOINER.join(objects));
}

private static String singleQuote(String literal)
{
return "\'" + literal + "\'";
}

public static ColumnMapping varbinaryColumnMapping()
{
return ColumnMapping.sliceMapping(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void testInsertWriteBulkinessWithTimestamps(String timestampType)
}
}

// TODO move test to BaseConnectorTest
// TODO move test to BaseConnectorTest https://github.com/trinodb/trino/issues/14517
@Test(dataProvider = "testTableNameDataProvider")
public void testCreateAndDropTableWithSpecialCharacterName(String tableName)
{
Expand All @@ -170,6 +170,39 @@ public void testCreateAndDropTableWithSpecialCharacterName(String tableName)
assertFalse(getQueryRunner().tableExists(getSession(), tableName));
}

// TODO remove this test after https://github.com/trinodb/trino/issues/14517
@Test(dataProvider = "testTableNameDataProvider")
Comment thread
hashhar marked this conversation as resolved.
Outdated
public void testRenameColumnNameAdditionalTests(String columnName)
{
String nameInSql = "\"" + columnName.replace("\"", "\"\"") + "\"";
String tableName = "tcn_" + nameInSql.replaceAll("[^a-z0-9]", "") + randomTableSuffix();
// Use complex identifier to test a source column name when renaming columns
String sourceColumnName = "a;b$c";

assertUpdate("CREATE TABLE " + tableName + "(\"" + sourceColumnName + "\" varchar(50))");
assertTableColumnNames(tableName, sourceColumnName);

assertUpdate("ALTER TABLE " + tableName + " RENAME COLUMN \"" + sourceColumnName + "\" TO " + nameInSql);
assertTableColumnNames(tableName, columnName.toLowerCase(ENGLISH));

assertUpdate("DROP TABLE " + tableName);
Comment thread
vlad-lyutenko marked this conversation as resolved.
Outdated
}

// TODO move this test to BaseConnectorTest https://github.com/trinodb/trino/issues/14517
@Test(dataProvider = "testTableNameDataProvider")
public void testRenameFromToTableWithSpecialCharacterName(String tableName)
{
String tableNameInSql = "\"" + tableName.replace("\"", "\"\"") + "\"";
String sourceTableName = "test_rename_source_" + randomTableSuffix();
assertUpdate("CREATE TABLE " + sourceTableName + " AS SELECT 123 x", 1);

assertUpdate("ALTER TABLE " + sourceTableName + " RENAME TO " + tableNameInSql);
assertQuery("SELECT x FROM " + tableNameInSql, "VALUES 123");
// test rename back is working properly
assertUpdate("ALTER TABLE " + tableNameInSql + " RENAME TO " + sourceTableName);
Comment thread
hashhar marked this conversation as resolved.
Outdated
assertUpdate("DROP TABLE " + sourceTableName);
}

private int getTableOperationsCount(String operation, String table)
throws SQLException
{
Expand Down