diff --git a/plugin/trino-redshift/src/main/java/io/trino/plugin/redshift/RedshiftClient.java b/plugin/trino-redshift/src/main/java/io/trino/plugin/redshift/RedshiftClient.java index af0078309e9f..57f43820c501 100644 --- a/plugin/trino-redshift/src/main/java/io/trino/plugin/redshift/RedshiftClient.java +++ b/plugin/trino-redshift/src/main/java/io/trino/plugin/redshift/RedshiftClient.java @@ -35,6 +35,7 @@ import javax.inject.Inject; import java.sql.Connection; +import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -130,6 +131,36 @@ public PreparedStatement getPreparedStatement(Connection connection, String sql) return statement; } + @Override + protected void verifySchemaName(DatabaseMetaData databaseMetadata, String schemaName) + throws SQLException + { + // Redshift truncates schema name to 127 chars silently + if (schemaName.length() > databaseMetadata.getMaxSchemaNameLength()) { + throw new TrinoException(NOT_SUPPORTED, "Schema name must be shorter than or equal to '%d' characters but got '%d'".formatted(databaseMetadata.getMaxSchemaNameLength(), schemaName.length())); + } + } + + @Override + protected void verifyTableName(DatabaseMetaData databaseMetadata, String tableName) + throws SQLException + { + // Redshift truncates table name to 127 chars silently + if (tableName.length() > databaseMetadata.getMaxTableNameLength()) { + throw new TrinoException(NOT_SUPPORTED, "Table name must be shorter than or equal to '%d' characters but got '%d'".formatted(databaseMetadata.getMaxTableNameLength(), tableName.length())); + } + } + + @Override + protected void verifyColumnName(DatabaseMetaData databaseMetadata, String columnName) + throws SQLException + { + // Redshift truncates table name to 127 chars silently + if (columnName.length() > databaseMetadata.getMaxColumnNameLength()) { + throw new TrinoException(NOT_SUPPORTED, "Column name must be shorter than or equal to '%d' characters but got '%d'".formatted(databaseMetadata.getMaxColumnNameLength(), columnName.length())); + } + } + @Override public Optional toColumnMapping(ConnectorSession session, Connection connection, JdbcTypeHandle typeHandle) { diff --git a/plugin/trino-redshift/src/test/java/io/trino/plugin/redshift/TestRedshiftConnectorTest.java b/plugin/trino-redshift/src/test/java/io/trino/plugin/redshift/TestRedshiftConnectorTest.java index 14bf231c048c..0bd3f862e91e 100644 --- a/plugin/trino-redshift/src/test/java/io/trino/plugin/redshift/TestRedshiftConnectorTest.java +++ b/plugin/trino-redshift/src/test/java/io/trino/plugin/redshift/TestRedshiftConnectorTest.java @@ -24,10 +24,12 @@ import org.testng.annotations.Test; import java.util.Optional; +import java.util.OptionalInt; import static io.trino.plugin.redshift.RedshiftQueryRunner.TEST_SCHEMA; import static io.trino.plugin.redshift.RedshiftQueryRunner.createRedshiftQueryRunner; import static java.lang.String.format; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; public class TestRedshiftConnectorTest @@ -137,45 +139,39 @@ protected String errorMessageForInsertIntoNotNullColumn(String columnName) } @Override - public void testCreateSchemaWithLongName() + protected OptionalInt maxSchemaNameLength() { - throw new SkipException("Long name checks not implemented"); + return OptionalInt.of(127); } @Override - public void testRenameSchemaToLongName() + protected void verifySchemaNameLengthFailurePermissible(Throwable e) { - throw new SkipException("Long name checks not implemented"); + assertThat(e).hasMessage("Schema name must be shorter than or equal to '127' characters but got '128'"); } @Override - public void testCreateTableWithLongTableName() + protected OptionalInt maxTableNameLength() { - throw new SkipException("Long name checks not implemented"); + return OptionalInt.of(127); } @Override - public void testRenameTableToLongTableName() + protected void verifyTableNameLengthFailurePermissible(Throwable e) { - throw new SkipException("Long name checks not implemented"); + assertThat(e).hasMessage("Table name must be shorter than or equal to '127' characters but got '128'"); } @Override - public void testCreateTableWithLongColumnName() + protected OptionalInt maxColumnNameLength() { - throw new SkipException("Long name checks not implemented"); + return OptionalInt.of(127); } @Override - public void testAlterTableAddLongColumnName() + protected void verifyColumnNameLengthFailurePermissible(Throwable e) { - throw new SkipException("Long name checks not implemented"); - } - - @Override - public void testAlterTableRenameColumnToLongName() - { - throw new SkipException("Long name checks not implemented"); + assertThat(e).hasMessage("Column name must be shorter than or equal to '127' characters but got '128'"); } @Override