Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine date processor patterns into single parser #83942

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions docs/changelog/83942.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 83942
summary: Combine date processor patterns into single parser
area: Ingest
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,23 @@ public final class DateProcessor extends AbstractProcessor {
this.targetField = targetField;
this.formats = formats;
this.dateParsers = new ArrayList<>(this.formats.size());
final List<String> javaFormats = new ArrayList<>(this.formats.size());
for (String format : formats) {
DateFormat dateFormat = DateFormat.fromString(format);
dateParsers.add((params) -> dateFormat.getFunction(format, newDateTimeZone(params), newLocale(params)));
if (DateFormat.Java == dateFormat) {
// pull out the java formats separately so they can all be processed as a single combined date parser
javaFormats.add(format);
Copy link
Contributor

Choose a reason for hiding this comment

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

It's probably worth doing our future selves a solid and adding a comment that explains what this is doing and why. Maybe something like:

// pull out the java formats separately so they can all be processed as a single combined date parser (see below)

} else {
dateParsers.add((params) -> dateFormat.getFunction(format, newDateTimeZone(params), newLocale(params)));
}
}

if (javaFormats.isEmpty() == false) {
dateParsers.add(
(params) -> DateFormat.Java.getFunction(String.join("||", javaFormats), newDateTimeZone(params), newLocale(params))
);
}

this.outputFormat = outputFormat;
formatter = DateFormatter.forPattern(this.outputFormat);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ public void testJavaPatternMultipleFormats() {
}

public void testShortCircuitAdditionalPatternsAfterFirstMatchingPattern() {
final String inputDate = "2010-03-04";
List<String> matchFormats = new ArrayList<>();
matchFormats.add("invalid");
matchFormats.add("uuuu");
// the following two formats will match the input date but only the first should be applied
matchFormats.add("uuuu-dd-MM");
matchFormats.add("uuuu-MM-dd");
DateProcessor dateProcessor = new DateProcessor(
Expand All @@ -116,7 +118,7 @@ public void testShortCircuitAdditionalPatternsAfterFirstMatchingPattern() {
);

Map<String, Object> document = new HashMap<>();
document.put("date_as_string", "2010-03-04");
document.put("date_as_string", inputDate);
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
dateProcessor.execute(ingestDocument);
assertThat(ingestDocument.getFieldValue("date_as_date", String.class), equalTo("2010-04-03T00:00:00.000+02:00"));
Expand Down