Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -629,6 +629,39 @@ public Builder removeColumn(int index) {
return this;
}

/**
* Method for renaming an existing column, located by its current name
* instead of by index. If no column with given {@code oldName} exists,
* an {@link IllegalArgumentException} is thrown.

@OrangeDog OrangeDog Jul 21, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Shouldn't it be

@throws IllegalArgumentException if no column with given {@code oldName} exists

?

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.

Good catch — you're right. Converted the prose note into a proper @throws IllegalArgumentException tag (and added @param tags) on renameColumn, replaceColumn and removeColumn in 4037df1.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

(and _columnIndex)

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.

Good point — done in 0440170. Converted _columnIndex's prose exception note into a proper @throws IllegalArgumentException tag and added a @param name tag, matching the public name-based methods.

*
* @since 2.23
*/
public Builder renameColumn(String oldName, String newName) {
return renameColumn(_columnIndex(oldName), newName);
}

/**
* Method for replacing an existing column, located by its current name
* instead of by index. If no column with given {@code name} exists,
* an {@link IllegalArgumentException} is thrown.
*
* @since 2.23
*/
public Builder replaceColumn(String name, Column c) {
return replaceColumn(_columnIndex(name), c);
}

/**
* Method for removing an existing column, located by its name instead
* of by index. If no column with given {@code name} exists, an
* {@link IllegalArgumentException} is thrown.
*
* @since 2.23
*/
public Builder removeColumn(String name) {
return removeColumn(_columnIndex(name));
}

/**
* Helper method called to drop the last collected column name if
* it is empty: called if {link CsvParser.Feature#ALLOW_TRAILING_COMMA}
Expand Down Expand Up @@ -869,6 +902,21 @@ protected void _checkIndex(int index) {
throw new IllegalArgumentException("Illegal index "+index+"; only got "+_columns.size()+" columns");
}
}

/**
* Helper method for finding index of the column with given name; throws
* {@link IllegalArgumentException} if no column with the name exists.
*
* @since 2.23
*/
protected int _columnIndex(String name) {
for (int i = 0, end = _columns.size(); i < end; ++i) {
if (_columns.get(i).getName().equals(name)) {
return i;
}
}
throw new IllegalArgumentException("No column with name '"+name+"' (have "+_columns.size()+" columns)");
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,46 @@ public void testSchemaWithExplicitIndices()

_verifyLinks(pointSchema);
}

// For [dataformats-text#699]: modify columns in Builder by name
@Test
public void testModifyColumnsByName()
{
CsvSchema.Builder b = CsvSchema.builder()
.addColumn("a", CsvSchema.ColumnType.STRING)
.addColumn("b", CsvSchema.ColumnType.NUMBER)
.addColumn("c", CsvSchema.ColumnType.BOOLEAN);

// rename by name maps to same slot as rename by index
b.renameColumn("b", "b2");
// replace by name preserves position, changes type
b.replaceColumn("c", new Column(2, "c2", CsvSchema.ColumnType.STRING));

CsvSchema schema = b.build();
assertEquals(3, schema.size());
assertEquals("a", schema.column(0).getName());
assertEquals("b2", schema.column(1).getName());
assertEquals(CsvSchema.ColumnType.NUMBER, schema.column(1).getType());
assertEquals("c2", schema.column(2).getName());
assertEquals(CsvSchema.ColumnType.STRING, schema.column(2).getType());

// remove by name drops just that column
CsvSchema shrunk = schema.rebuild().removeColumn("a").build();
assertEquals(2, shrunk.size());
assertEquals("b2", shrunk.column(0).getName());
assertEquals("c2", shrunk.column(1).getName());
}

// For [dataformats-text#699]: unknown name should fail fast
@Test
public void testModifyUnknownColumnByName()
{
CsvSchema.Builder b = CsvSchema.builder().addColumn("a");
try {
b.removeColumn("missing");
fail("Should not pass");
} catch (IllegalArgumentException e) {
verifyException(e, "No column with name 'missing'");
}
}
}