Skip to content

Commit

Permalink
Fixed nullPointerException in InputFileSegmentNameGenerator when patt…
Browse files Browse the repository at this point in the history
…ern contains optional part (apache#13532)
  • Loading branch information
jhyao authored Jul 3, 2024
1 parent fa5726d commit 529fffb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ private String makeSegmentName() {
} else {
segmentName = _segmentNameTemplate;
for (int i = 1; i <= m.groupCount(); i++) {
segmentName = segmentName.replace(String.format(PARAMETER_TEMPLATE, i), m.group(i));
String value = m.group(i);
if (value == null) {
value = "";
}
segmentName = segmentName.replace(String.format(PARAMETER_TEMPLATE, i), value);
}
}
return _appendUUIDToSegmentName ? JOINER.join(segmentName, UUID.randomUUID()) : segmentName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,37 @@ public class InputFileSegmentNameGeneratorTest {

@Test
public void testWithInvalidPath() {
validateName("/my/path/to/segmentname.tsv", "my_path_to_segmentname_tsv");
validateName("hdfs:///my/path/to/segmentname.tsv", "my_path_to_segmentname_tsv");
validateCSVFileName("/my/path/to/segmentname.tsv", "my_path_to_segmentname_tsv");
validateCSVFileName("hdfs:///my/path/to/segmentname.tsv", "my_path_to_segmentname_tsv");
}

@Test
public void testWithHDFSPath() {
validateName("hdfs:///my/path/to/segmentname.csv", "segmentname");
validateName("hdfs:/server:9000//my/path/to/segmentname.csv", "segmentname");
validateCSVFileName("hdfs:///my/path/to/segmentname.csv", "segmentname");
validateCSVFileName("hdfs:/server:9000//my/path/to/segmentname.csv", "segmentname");
}

@Test
public void testWithFilePath() {
validateName("file:///my/path/to/segmentname.csv", "segmentname");
validateCSVFileName("file:///my/path/to/segmentname.csv", "segmentname");
}

private void validateName(String inputFileUriAsStr, String segmentName) {
@Test
public void testWithOptionalPattern() {
String pattern = ".+/(\\w+)(\\.csv)?";
String template = "${filePathPattern:\\1}";
validateName("file:///my/path/to/segmentname.csv", "segmentname", pattern, template);
validateName("file:///my/path/to/segmentname", "segmentname", pattern, template);
}

private void validateCSVFileName(String inputFileUriAsStr, String segmentName) {
String pattern = ".+/(.+)\\.csv";
String template = "${filePathPattern:\\1}";
validateName(inputFileUriAsStr, segmentName, pattern, template);
}

private void validateName(String inputFileUriAsStr, String segmentName, String pattern, String template) {
try {
String pattern = ".+/(.+)\\.csv";
String template = "${filePathPattern:\\1}";
SegmentNameGenerator segmentNameGenerator =
new InputFileSegmentNameGenerator(pattern, template, inputFileUriAsStr);
assertEquals(segmentNameGenerator.generateSegmentName(INVALID_SEQUENCE_ID, null, null), segmentName);
Expand Down

0 comments on commit 529fffb

Please sign in to comment.