Skip to content

Commit

Permalink
Fix false positive date detection with trailing dot (#116953)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixbarny authored Dec 4, 2024
1 parent 28eda97 commit 9f581d5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/changelog/116953.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 116953
summary: Fix false positive date detection with trailing dot
area: Mapping
type: bug
issues:
- 116946
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ public long getFrom(TemporalAccessor temporal) {
.toFormatter(Locale.ROOT);

// this supports milliseconds ending in dot
private static final DateTimeFormatter MILLISECONDS_FORMATTER2 = new DateTimeFormatterBuilder().append(MILLISECONDS_FORMATTER1)
private static final DateTimeFormatter MILLISECONDS_FORMATTER2 = new DateTimeFormatterBuilder().optionalStart()
.appendText(NEGATIVE_SIGN_FIELD, Map.of(-1L, "-")) // field is only created in the presence of a '-' char.
.optionalEnd()
.appendValue(UNSIGNED_MILLIS, 1, 19, SignStyle.NOT_NEGATIVE)
.appendLiteral('.')
.toFormatter(Locale.ROOT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,17 @@ public void testEpochMillisParser() {
assertThat(formatter.format(instant), is("-0.12345"));
assertThat(Instant.from(formatter.parse(formatter.format(instant))), is(instant));
}
{
Instant instant = Instant.from(formatter.parse("12345."));
assertThat(instant.getEpochSecond(), is(12L));
assertThat(instant.getNano(), is(345_000_000));
assertThat(formatter.format(instant), is("12345"));
assertThat(Instant.from(formatter.parse(formatter.format(instant))), is(instant));
}
{
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> formatter.parse("12345.0."));
assertThat(e.getMessage(), is("failed to parse date field [12345.0.] with format [epoch_millis]"));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,23 @@ private void doTestDefaultFloatingPointMappings(DocumentMapper mapper, XContentB
assertThat(((FieldMapper) update.getRoot().getMapper("quux")).fieldType().typeName(), equalTo("float"));
}

public void testDateDetectionEnabled() throws Exception {
MapperService mapperService = createMapperService(topMapping(b -> b.field("date_detection", true)));

ParsedDocument doc = mapperService.documentMapper().parse(source(b -> {
b.field("date", "2024-11-18");
b.field("no_date", "128.0.");
}));
assertNotNull(doc.dynamicMappingsUpdate());
merge(mapperService, dynamicMapping(doc.dynamicMappingsUpdate()));

Mapper mapper = mapperService.documentMapper().mappers().getMapper("date");
assertThat(mapper.typeName(), equalTo("date"));

mapper = mapperService.documentMapper().mappers().getMapper("no_date");
assertThat(mapper.typeName(), equalTo("text"));
}

public void testNumericDetectionEnabled() throws Exception {
MapperService mapperService = createMapperService(topMapping(b -> b.field("numeric_detection", true)));

Expand Down

0 comments on commit 9f581d5

Please sign in to comment.