diff --git a/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/creator/name/InputFileSegmentNameGenerator.java b/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/creator/name/InputFileSegmentNameGenerator.java index 1d703832373e..3f7e2c387d24 100644 --- a/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/creator/name/InputFileSegmentNameGenerator.java +++ b/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/creator/name/InputFileSegmentNameGenerator.java @@ -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; diff --git a/pinot-segment-spi/src/test/java/org/apache/pinot/segment/spi/creator/name/InputFileSegmentNameGeneratorTest.java b/pinot-segment-spi/src/test/java/org/apache/pinot/segment/spi/creator/name/InputFileSegmentNameGeneratorTest.java index 7ce23d83f0d0..11836de846dc 100644 --- a/pinot-segment-spi/src/test/java/org/apache/pinot/segment/spi/creator/name/InputFileSegmentNameGeneratorTest.java +++ b/pinot-segment-spi/src/test/java/org/apache/pinot/segment/spi/creator/name/InputFileSegmentNameGeneratorTest.java @@ -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);