Skip to content

Commit

Permalink
Add Redshift schema, table, and column length checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dain committed Dec 12, 2022
1 parent 87b8e6f commit 0312463
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ColumnMapping> toColumnMapping(ConnectorSession session, Connection connection, JdbcTypeHandle typeHandle)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0312463

Please sign in to comment.