Merge pull request #595 from open-metadata/issue590

Fix #590 Same column name is allowed to be repeated in Table entity APIs
This commit is contained in:
Suresh Srinivas 2021-09-27 09:12:14 -07:00 committed by GitHub
commit 512dbd6201
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -79,6 +79,7 @@ public final class DatabaseUtil {
} }
public static void validateColumns(Table table) { public static void validateColumns(Table table) {
validateColumnNames(table.getColumns());
for (Column c : table.getColumns()) { for (Column c : table.getColumns()) {
validateColumnDataTypeDisplay(c); validateColumnDataTypeDisplay(c);
validateColumnDataLength(c); validateColumnDataLength(c);
@ -87,6 +88,16 @@ public final class DatabaseUtil {
} }
} }
public static void validateColumnNames(List<Column> columns) {
List<String> columnNames = new ArrayList<>();
for (Column c : columns) {
if (columnNames.contains(c.getName())) {
throw new IllegalArgumentException(String.format("Column name %s is repeated", c.getName()));
}
columnNames.add(c.getName());
}
}
public static void validateColumnDataTypeDisplay(Column column) { public static void validateColumnDataTypeDisplay(Column column) {
// If dataTypeDisplay is null then set it based on dataType // If dataTypeDisplay is null then set it based on dataType
if (column.getDataTypeDisplay() == null) { if (column.getDataTypeDisplay() == null) {
@ -143,6 +154,7 @@ public final class DatabaseUtil {
"must not be null"); "must not be null");
} }
validateColumnNames(column.getChildren());
if (!column.getDataTypeDisplay().startsWith("struct<")) { if (!column.getDataTypeDisplay().startsWith("struct<")) {
throw new IllegalArgumentException("For column data type struct, dataTypeDisplay must be of type " + throw new IllegalArgumentException("For column data type struct, dataTypeDisplay must be of type " +
"stuct<member fields>"); "stuct<member fields>");

View File

@ -200,6 +200,18 @@ public class TableResourceTest extends CatalogApplicationTest {
"For column data type array, dataTypeDisplay must be of type array<arrayDataType>"); "For column data type array, dataTypeDisplay must be of type array<arrayDataType>");
} }
@Test
public void post_duplicateColumnName_400(TestInfo test) {
// Duplicate column names c1
String repeatedColumnName = "c1";
List<Column> columns = Arrays.asList(getColumn(repeatedColumnName, ARRAY, "array<int>", null),
getColumn(repeatedColumnName, INT, null));
CreateTable create = create(test).withColumns(columns);
HttpResponseException exception = assertThrows(HttpResponseException.class, () ->
createTable(create, adminAuthHeaders()));
assertResponse(exception, BAD_REQUEST, String.format("Column name %s is repeated", repeatedColumnName));
}
@Test @Test
public void post_tableAlreadyExists_409_conflict(TestInfo test) throws HttpResponseException { public void post_tableAlreadyExists_409_conflict(TestInfo test) throws HttpResponseException {
CreateTable create = create(test); CreateTable create = create(test);
@ -227,9 +239,9 @@ public class TableResourceTest extends CatalogApplicationTest {
} }
private static Column getColumn(String name, ColumnDataType columnDataType, String dataTypeDisplay, TagLabel tag) { private static Column getColumn(String name, ColumnDataType columnDataType, String dataTypeDisplay, TagLabel tag) {
List<TagLabel> tags = tag == null ? new ArrayList<>() : singletonList(tag);
return new Column().withName(name).withDataType(columnDataType) return new Column().withName(name).withDataType(columnDataType)
.withDataTypeDisplay(dataTypeDisplay).withTags(singletonList(tag)); .withDataTypeDisplay(dataTypeDisplay).withTags(tags);
} }
@Test @Test