From c3887b351e77ab6d23ddd662564b3d0d977895e4 Mon Sep 17 00:00:00 2001
From: David Smiley
Date: Tue, 7 Aug 2018 11:51:16 -0400
Subject: [PATCH] SOLR-12586: Change ParseDateFieldUpdateProcessorFactory to
use java.time.DateTimeFormatter, not Joda Time. Note: slightly different
pattern language! Remove Joda Time. Closes #428
---
lucene/ivy-versions.properties | 1 -
solr/CHANGES.txt | 10 +
.../collection1/conf/solrconfig.xml | 20 +-
solr/core/ivy.xml | 1 -
.../apache/solr/core/ConfigSetService.java | 7 +-
.../ParseDateFieldUpdateProcessorFactory.java | 82 +++++--
...-schema-fields-update-processor-chains.xml | 20 +-
...config-parsing-update-processor-chains.xml | 49 ++---
.../conf/solrconfig-schemaless.xml | 20 +-
.../configsets/_default/conf/solrconfig.xml | 20 +-
...chemaFieldsUpdateProcessorFactoryTest.java | 23 +-
.../ParsingFieldUpdateProcessorsTest.java | 157 +++++++-------
solr/example/files/conf/solrconfig.xml | 20 +-
solr/licenses/joda-time-2.2.jar.sha1 | 1 -
solr/licenses/joda-time-LICENSE-ASL.txt | 202 ------------------
solr/licenses/joda-time-NOTICE.txt | 5 -
.../configsets/_default/conf/solrconfig.xml | 20 +-
solr/solr-ref-guide/src/schemaless-mode.adoc | 20 +-
18 files changed, 218 insertions(+), 460 deletions(-)
delete mode 100644 solr/licenses/joda-time-2.2.jar.sha1
delete mode 100644 solr/licenses/joda-time-LICENSE-ASL.txt
delete mode 100644 solr/licenses/joda-time-NOTICE.txt
diff --git a/lucene/ivy-versions.properties b/lucene/ivy-versions.properties
index 3a1ef2b58d3..d7141bd3899 100644
--- a/lucene/ivy-versions.properties
+++ b/lucene/ivy-versions.properties
@@ -77,7 +77,6 @@ io.prometheus.version = 0.2.0
/javax.activation/activation = 1.1.1
/javax.servlet/javax.servlet-api = 3.1.0
-/joda-time/joda-time = 2.2
/junit/junit = 4.10
/mecab/mecab-ipadic = 2.7.0-20070801
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index d5edef396f1..94960bb1d33 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -44,11 +44,21 @@ Upgrade Notes
MemoryDocValues). If you used postingsFormat="Memory" or docValuesFormat="Memory"
switch to "Direct" instead. (Dawid Weiss)
+* SOLR-12586: The date format patterns used by ParseDateFieldUpdateProcessorFactory (present in "schemaless mode")
+ are now interpreted by Java 8's java.time.DateTimeFormatter instead of Joda Time. The pattern language is very
+ similar but not the same. Typically, simply update the pattern by changing an uppercase 'Z' to lowercase 'z' and
+ that's it. For the current recommended set of patterns in schemaless mode, see "Schemaless Mode" in the ref guide.
+ (David Smiley, Bar Rotstein)
+
Other Changes
----------------------
* SOLR-12614: Make "Nodes" view the default in AdminUI "Cloud" tab (janhoy)
+* SOLR-12586: Remove Joda Time dependency. Upgrade ParseDateFieldUpdateProcessorFactory (present in "schemaless mode")
+ to use Java 8's java.time.DateTimeFormatter instead (see upgrade notes).
+ (David Smiley, Bar Rotstein)
+
================== 7.5.0 ==================
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.
diff --git a/solr/contrib/prometheus-exporter/src/test-files/configsets/collection1/conf/solrconfig.xml b/solr/contrib/prometheus-exporter/src/test-files/configsets/collection1/conf/solrconfig.xml
index 72c54304f24..37c41d8909c 100644
--- a/solr/contrib/prometheus-exporter/src/test-files/configsets/collection1/conf/solrconfig.xml
+++ b/solr/contrib/prometheus-exporter/src/test-files/configsets/collection1/conf/solrconfig.xml
@@ -164,22 +164,10 @@
- yyyy-MM-dd'T'HH:mm:ss.SSSZ
- yyyy-MM-dd'T'HH:mm:ss,SSSZ
- yyyy-MM-dd'T'HH:mm:ss.SSS
- yyyy-MM-dd'T'HH:mm:ss,SSS
- yyyy-MM-dd'T'HH:mm:ssZ
- yyyy-MM-dd'T'HH:mm:ss
- yyyy-MM-dd'T'HH:mmZ
- yyyy-MM-dd'T'HH:mm
- yyyy-MM-dd HH:mm:ss.SSSZ
- yyyy-MM-dd HH:mm:ss,SSSZ
- yyyy-MM-dd HH:mm:ss.SSS
- yyyy-MM-dd HH:mm:ss,SSS
- yyyy-MM-dd HH:mm:ssZ
- yyyy-MM-dd HH:mm:ss
- yyyy-MM-dd HH:mmZ
- yyyy-MM-dd HH:mm
+ yyyy-MM-dd'T'HH:mm[:ss[.SSS]][z
+ yyyy-MM-dd'T'HH:mm[:ss[,SSS]][z
+ yyyy-MM-dd HH:mm[:ss[.SSS]][z
+ yyyy-MM-dd HH:mm[:ss[,SSS]][z
yyyy-MM-dd
diff --git a/solr/core/ivy.xml b/solr/core/ivy.xml
index ee6fe80db73..5a0fd091301 100644
--- a/solr/core/ivy.xml
+++ b/solr/core/ivy.xml
@@ -44,7 +44,6 @@
-
diff --git a/solr/core/src/java/org/apache/solr/core/ConfigSetService.java b/solr/core/src/java/org/apache/solr/core/ConfigSetService.java
index 69e160b5da1..7ce1a52dc6b 100644
--- a/solr/core/src/java/org/apache/solr/core/ConfigSetService.java
+++ b/solr/core/src/java/org/apache/solr/core/ConfigSetService.java
@@ -21,6 +21,7 @@ import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.time.Instant;
import java.util.Locale;
import java.util.concurrent.ExecutionException;
@@ -33,8 +34,6 @@ import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.IndexSchemaFactory;
-import org.joda.time.format.DateTimeFormat;
-import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -212,12 +211,10 @@ public abstract class ConfigSetService {
super(loader, configSetBase);
}
- public static final DateTimeFormatter cacheKeyFormatter = DateTimeFormat.forPattern("yyyyMMddHHmmss");
-
public static String cacheName(Path schemaFile) throws IOException {
long lastModified = Files.getLastModifiedTime(schemaFile).toMillis();
return String.format(Locale.ROOT, "%s:%s",
- schemaFile.toString(), cacheKeyFormatter.print(lastModified));
+ schemaFile.toString(), Instant.ofEpochMilli(lastModified).toString());
}
@Override
diff --git a/solr/core/src/java/org/apache/solr/update/processor/ParseDateFieldUpdateProcessorFactory.java b/solr/core/src/java/org/apache/solr/update/processor/ParseDateFieldUpdateProcessorFactory.java
index 234b48bc15e..f0ea5d25720 100644
--- a/solr/core/src/java/org/apache/solr/update/processor/ParseDateFieldUpdateProcessorFactory.java
+++ b/solr/core/src/java/org/apache/solr/update/processor/ParseDateFieldUpdateProcessorFactory.java
@@ -17,6 +17,17 @@
package org.apache.solr.update.processor;
import java.lang.invoke.MethodHandles;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeFormatterBuilder;
+import java.time.format.DateTimeParseException;
+import java.time.temporal.TemporalAccessor;
+import java.time.temporal.TemporalQueries;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
@@ -24,6 +35,7 @@ import java.util.Locale;
import java.util.Map;
import org.apache.commons.lang.LocaleUtils;
+import org.apache.solr.common.SolrException;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.SolrCore;
import org.apache.solr.request.SolrQueryRequest;
@@ -31,10 +43,6 @@ import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.schema.DateValueFieldType;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.IndexSchema;
-import org.joda.time.DateTime;
-import org.joda.time.DateTimeZone;
-import org.joda.time.format.DateTimeFormat;
-import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -57,8 +65,8 @@ import org.slf4j.LoggerFactory;
*
*
* One or more date "format" specifiers must be specified. See
- * Joda-time's DateTimeFormat javadocs for a description of format strings.
+ * Java 8's DateTimeFormatter javadocs for a description of format strings.
*
*
* A default time zone name or offset may optionally be specified for those dates
@@ -115,9 +123,8 @@ public class ParseDateFieldUpdateProcessorFactory extends FieldMutatingUpdatePro
for (Map.Entry format : formats.entrySet()) {
DateTimeFormatter parser = format.getValue();
try {
- DateTime dateTime = parser.parseDateTime(srcStringVal);
- return dateTime.withZone(DateTimeZone.UTC).toDate();
- } catch (IllegalArgumentException e) {
+ return Date.from(parseInstant(parser, srcStringVal));
+ } catch (DateTimeParseException e) {
log.debug("value '{}' is not parseable with format '{}'",
new Object[] { srcStringVal, format.getKey() });
}
@@ -144,15 +151,18 @@ public class ParseDateFieldUpdateProcessorFactory extends FieldMutatingUpdatePro
}
Object defaultTimeZoneParam = args.remove(DEFAULT_TIME_ZONE_PARAM);
- DateTimeZone defaultTimeZone = DateTimeZone.UTC;
+ ZoneId defaultTimeZone = ZoneOffset.UTC;
if (null != defaultTimeZoneParam) {
- defaultTimeZone = DateTimeZone.forID(defaultTimeZoneParam.toString());
+ defaultTimeZone = ZoneId.of(defaultTimeZoneParam.toString());
}
Collection formatsParam = args.removeConfigArgs(FORMATS_PARAM);
if (null != formatsParam) {
for (String value : formatsParam) {
- formats.put(value, DateTimeFormat.forPattern(value).withZone(defaultTimeZone).withLocale(locale));
+ DateTimeFormatter formatter = new DateTimeFormatterBuilder().parseCaseInsensitive()
+ .appendPattern(value).toFormatter(locale).withZone(defaultTimeZone);
+ validateFormatter(formatter);
+ formats.put(value, formatter);
}
}
super.init(args);
@@ -172,4 +182,52 @@ public class ParseDateFieldUpdateProcessorFactory extends FieldMutatingUpdatePro
return (null == type) || type instanceof DateValueFieldType;
};
}
+
+ public static void validateFormatter(DateTimeFormatter formatter) {
+ // check it's valid via round-trip
+ try {
+ parseInstant(formatter, formatter.format(Instant.now()));
+ } catch (Exception e) {
+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
+ "Bad or unsupported pattern: " + formatter.toFormat().toString(), e);
+ }
+ }
+
+ // see https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8177021 which is fixed in Java 9.
+ // The upshot is that trying to use parse(Instant::from) is unreliable in the event that
+ // the input string contains a timezone/offset that differs from the "override zone"
+ // (which we configure in DEFAULT_TIME_ZONE). Besides, we need the code below which handles
+ // the optionality of time. Were it not for that, we truly could do formatter.parse(Instant::from).
+ private static Instant parseInstant(DateTimeFormatter formatter, String dateStr) {
+ final TemporalAccessor temporal = formatter.parse(dateStr);
+ // Get Date; mandatory
+ LocalDate date = temporal.query(TemporalQueries.localDate());//mandatory
+ if (date == null) {
+ throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
+ "Date (year, month, day) is mandatory: " + formatter.toFormat().toString());
+ }
+ // Get Time; optional
+ LocalTime time = temporal.query(TemporalQueries.localTime());
+ if (time == null) {
+ time = LocalTime.MIN;
+ }
+
+ final LocalDateTime localDateTime = LocalDateTime.of(date, time);
+
+ // Get Zone Offset; optional
+ ZoneOffset offset = temporal.query(TemporalQueries.offset());
+ if (offset == null) {
+ // no Zone offset; get Zone ID
+ ZoneId zoneId = temporal.query(TemporalQueries.zone());
+ if (zoneId == null) {
+ zoneId = formatter.getZone();
+ if (zoneId == null) {
+ zoneId = ZoneOffset.UTC;
+ }
+ }
+ return localDateTime.atZone(zoneId).toInstant();
+ } else {
+ return localDateTime.toInstant(offset);
+ }
+ }
}
diff --git a/solr/core/src/test-files/solr/collection1/conf/solrconfig-add-schema-fields-update-processor-chains.xml b/solr/core/src/test-files/solr/collection1/conf/solrconfig-add-schema-fields-update-processor-chains.xml
index b0b63dfbb6e..4394d5c7a6e 100644
--- a/solr/core/src/test-files/solr/collection1/conf/solrconfig-add-schema-fields-update-processor-chains.xml
+++ b/solr/core/src/test-files/solr/collection1/conf/solrconfig-add-schema-fields-update-processor-chains.xml
@@ -179,22 +179,10 @@
- yyyy-MM-dd'T'HH:mm:ss.SSSZ
- yyyy-MM-dd'T'HH:mm:ss,SSSZ
- yyyy-MM-dd'T'HH:mm:ss.SSS
- yyyy-MM-dd'T'HH:mm:ss,SSS
- yyyy-MM-dd'T'HH:mm:ssZ
- yyyy-MM-dd'T'HH:mm:ss
- yyyy-MM-dd'T'HH:mmZ
- yyyy-MM-dd'T'HH:mm
- yyyy-MM-dd HH:mm:ss.SSSZ
- yyyy-MM-dd HH:mm:ss,SSSZ
- yyyy-MM-dd HH:mm:ss.SSS
- yyyy-MM-dd HH:mm:ss,SSS
- yyyy-MM-dd HH:mm:ssZ
- yyyy-MM-dd HH:mm:ss
- yyyy-MM-dd HH:mmZ
- yyyy-MM-dd HH:mm
+ yyyy-MM-dd'T'HH:mm[:ss[.SSS]][z
+ yyyy-MM-dd'T'HH:mm[:ss[,SSS]][z
+ yyyy-MM-dd HH:mm[:ss[.SSS]][z
+ yyyy-MM-dd HH:mm[:ss[,SSS]][z
yyyy-MM-dd
diff --git a/solr/core/src/test-files/solr/collection1/conf/solrconfig-parsing-update-processor-chains.xml b/solr/core/src/test-files/solr/collection1/conf/solrconfig-parsing-update-processor-chains.xml
index 5930d37d775..83be4eef789 100644
--- a/solr/core/src/test-files/solr/collection1/conf/solrconfig-parsing-update-processor-chains.xml
+++ b/solr/core/src/test-files/solr/collection1/conf/solrconfig-parsing-update-processor-chains.xml
@@ -29,35 +29,35 @@
- yyyy-MM-dd'T'HH:mm:ss.SSSZ
+ yyyy-MM-dd'T'HH:mm:ss.SSSz
- yyyy-MM-dd'T'HH:mm:ss.SSSZ
+ yyyy-MM-dd'T'HH:mm:ss.SSSz
false
- yyyy-MM-dd'T'HH:mm:ss.SSSZ
+ yyyy-MM-dd'T'HH:mm:ss.SSSz
solr.DatePointField
- yyyy-MM-dd'T'HH:mm:ss.SSSZ
+ yyyy-MM-dd'T'HH:mm:ss.SSSz
solr.TrieDateField
- yyyy-MM-dd'T'HH:mm:ss.SSSZ
+ yyyy-MM-dd'T'HH:mm:ss.SSSz
@@ -65,8 +65,7 @@
America/New_York
en_US
- yyyy-MM-dd'T'HH:mm:ss.SSSZ
- yyyy-MM-dd'T'HH:mm:ss.SSS
+ yyyy-MM-dd'T'HH:mm:ss.SSS[z]
@@ -75,7 +74,7 @@
America/Los_Angeles
- MM/dd/yyyy
+ M/d/yyyy
@@ -85,30 +84,16 @@
UTC
en_US
- yyyy-MM-dd'T'HH:mm:ss.SSSZ
- yyyy-MM-dd'T'HH:mm:ss,SSSZ
- yyyy-MM-dd'T'HH:mm:ss.SSS
- yyyy-MM-dd'T'HH:mm:ss,SSS
- yyyy-MM-dd'T'HH:mm:ssZ
- yyyy-MM-dd'T'HH:mm:ss
- yyyy-MM-dd'T'HH:mmZ
- yyyy-MM-dd'T'HH:mm
- yyyy-MM-dd HH:mm:ss.SSSZ
- yyyy-MM-dd HH:mm:ss,SSSZ
- yyyy-MM-dd HH:mm:ss.SSS
- yyyy-MM-dd HH:mm:ss,SSS
- yyyy-MM-dd HH:mm:ssZ
- yyyy-MM-dd HH:mm:ss
- yyyy-MM-dd HH:mmZ
- yyyy-MM-dd HH:mm
- yyyy-MM-dd hh:mm a
- yyyy-MM-dd hh:mma
+ yyyy-MM-dd'T'HH:mm[:ss[.SSS]][z
+ yyyy-MM-dd'T'HH:mm[:ss[,SSS]][z
+ yyyy-MM-dd HH:mm[:ss[.SSS]][z
+ yyyy-MM-dd HH:mm[:ss[,SSS]][z
yyyy-MM-dd
- EEE MMM dd HH:mm:ss Z yyyy
- EEE MMM dd HH:mm:ss yyyy Z
- EEE MMM dd HH:mm:ss yyyy
- EEE, dd MMM yyyy HH:mm:ss Z
- EEEE, dd-MMM-yy HH:mm:ss Z
+ yyyy-MM-dd hh:mm[ ]a
+ EEE MMM dd HH:mm:ss ZZZ yyyy
+ EEE MMM dd HH:mm:ss yyyy[ z]
+ EEE, dd MMM yyyy HH:mm:ss z
+ EEEE, dd-MMM-yy HH:mm:ss z
EEEE, MMMM dd, yyyy
MMMM dd, yyyy
MMM. dd, yyyy
@@ -229,7 +214,7 @@
yyyy-MM-dd
- yyyy-MM-dd'T'HH:mm:ss.SSSZ
+ yyyy-MM-dd'T'HH:mm:ss.SSSz
yyyy-MM-dd'T'HH:mm
diff --git a/solr/core/src/test-files/solr/collection1/conf/solrconfig-schemaless.xml b/solr/core/src/test-files/solr/collection1/conf/solrconfig-schemaless.xml
index 4f10a8e0e62..446f3f0feb2 100644
--- a/solr/core/src/test-files/solr/collection1/conf/solrconfig-schemaless.xml
+++ b/solr/core/src/test-files/solr/collection1/conf/solrconfig-schemaless.xml
@@ -61,22 +61,10 @@
- yyyy-MM-dd'T'HH:mm:ss.SSSZ
- yyyy-MM-dd'T'HH:mm:ss,SSSZ
- yyyy-MM-dd'T'HH:mm:ss.SSS
- yyyy-MM-dd'T'HH:mm:ss,SSS
- yyyy-MM-dd'T'HH:mm:ssZ
- yyyy-MM-dd'T'HH:mm:ss
- yyyy-MM-dd'T'HH:mmZ
- yyyy-MM-dd'T'HH:mm
- yyyy-MM-dd HH:mm:ss.SSSZ
- yyyy-MM-dd HH:mm:ss,SSSZ
- yyyy-MM-dd HH:mm:ss.SSS
- yyyy-MM-dd HH:mm:ss,SSS
- yyyy-MM-dd HH:mm:ssZ
- yyyy-MM-dd HH:mm:ss
- yyyy-MM-dd HH:mmZ
- yyyy-MM-dd HH:mm
+ yyyy-MM-dd'T'HH:mm[:ss[.SSS]][z
+ yyyy-MM-dd'T'HH:mm[:ss[,SSS]][z
+ yyyy-MM-dd HH:mm[:ss[.SSS]][z
+ yyyy-MM-dd HH:mm[:ss[,SSS]][z
yyyy-MM-dd
diff --git a/solr/core/src/test-files/solr/configsets/_default/conf/solrconfig.xml b/solr/core/src/test-files/solr/configsets/_default/conf/solrconfig.xml
index 51f2c639c64..d393316f207 100644
--- a/solr/core/src/test-files/solr/configsets/_default/conf/solrconfig.xml
+++ b/solr/core/src/test-files/solr/configsets/_default/conf/solrconfig.xml
@@ -1141,22 +1141,10 @@
- yyyy-MM-dd'T'HH:mm:ss.SSSZ
- yyyy-MM-dd'T'HH:mm:ss,SSSZ
- yyyy-MM-dd'T'HH:mm:ss.SSS
- yyyy-MM-dd'T'HH:mm:ss,SSS
- yyyy-MM-dd'T'HH:mm:ssZ
- yyyy-MM-dd'T'HH:mm:ss
- yyyy-MM-dd'T'HH:mmZ
- yyyy-MM-dd'T'HH:mm
- yyyy-MM-dd HH:mm:ss.SSSZ
- yyyy-MM-dd HH:mm:ss,SSSZ
- yyyy-MM-dd HH:mm:ss.SSS
- yyyy-MM-dd HH:mm:ss,SSS
- yyyy-MM-dd HH:mm:ssZ
- yyyy-MM-dd HH:mm:ss
- yyyy-MM-dd HH:mmZ
- yyyy-MM-dd HH:mm
+ yyyy-MM-dd'T'HH:mm[:ss[.SSS]][z
+ yyyy-MM-dd'T'HH:mm[:ss[,SSS]][z
+ yyyy-MM-dd HH:mm[:ss[.SSS]][z
+ yyyy-MM-dd HH:mm[:ss[,SSS]][z
yyyy-MM-dd
diff --git a/solr/core/src/test/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactoryTest.java b/solr/core/src/test/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactoryTest.java
index d1a3a535a48..96cc6766e93 100644
--- a/solr/core/src/test/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactoryTest.java
+++ b/solr/core/src/test/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactoryTest.java
@@ -17,17 +17,18 @@
package org.apache.solr.update.processor;
import java.io.File;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Date;
+import java.util.Locale;
import org.apache.commons.io.FileUtils;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.schema.IndexSchema;
-import org.joda.time.DateTime;
-import org.joda.time.format.DateTimeFormat;
-import org.joda.time.format.DateTimeFormatter;
-import org.joda.time.format.ISODateTimeFormat;
import org.junit.After;
import org.junit.Before;
@@ -62,9 +63,7 @@ public class AddSchemaFieldsUpdateProcessorFactoryTest extends UpdateProcessorTe
IndexSchema schema = h.getCore().getLatestSchema();
final String fieldName = "newfield1";
assertNull(schema.getFieldOrNull(fieldName));
- String dateString = "2010-11-12T13:14:15.168Z";
- DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
- Date date = dateTimeFormatter.parseDateTime(dateString).toDate();
+ Date date = Date.from(Instant.now());
SolrInputDocument d = processAdd("add-fields-no-run-processor", doc(f("id", "1"), f(fieldName, date)));
assertNotNull(d);
schema = h.getCore().getLatestSchema();
@@ -202,11 +201,11 @@ public class AddSchemaFieldsUpdateProcessorFactoryTest extends UpdateProcessorTe
String field3String2 = "-5.28E-3";
Double field3Value2 = -5.28E-3;
String field4String1 = "1999-04-17 17:42";
- DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm").withZoneUTC();
- DateTime dateTime = dateTimeFormatter.parseDateTime(field4String1);
- Date field4Value1 = dateTime.toDate();
- DateTimeFormatter dateTimeFormatter2 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").withZoneUTC();
- String field4Value1String = dateTimeFormatter2.print(dateTime) + "Z";
+ DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm", Locale.ROOT).withZone(ZoneOffset.UTC);
+ LocalDateTime dateTime = LocalDateTime.parse(field4String1, dateTimeFormatter);
+ Date field4Value1 = Date.from(dateTime.atZone(ZoneOffset.UTC).toInstant());
+ DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT).withZone(ZoneOffset.UTC);
+ String field4Value1String = dateTime.format(dateTimeFormatter2) + "Z";
SolrInputDocument d = processAdd
("parse-and-add-fields", doc(f("id", "6"), f(fieldName1, field1String1, field1String2, field1String3),
diff --git a/solr/core/src/test/org/apache/solr/update/processor/ParsingFieldUpdateProcessorsTest.java b/solr/core/src/test/org/apache/solr/update/processor/ParsingFieldUpdateProcessorsTest.java
index 3aeb1fbfccf..e26ca4122bf 100644
--- a/solr/core/src/test/org/apache/solr/update/processor/ParsingFieldUpdateProcessorsTest.java
+++ b/solr/core/src/test/org/apache/solr/update/processor/ParsingFieldUpdateProcessorsTest.java
@@ -16,28 +16,35 @@
*/
package org.apache.solr.update.processor;
-import org.apache.solr.common.SolrInputDocument;
-import org.apache.solr.schema.IndexSchema;
-import org.joda.time.DateTime;
-import org.joda.time.DateTimeZone;
-import org.joda.time.format.DateTimeFormat;
-import org.joda.time.format.DateTimeFormatter;
-import org.joda.time.format.ISODateTimeFormat;
-import org.junit.BeforeClass;
-
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
+import java.util.Locale;
import java.util.Map;
import java.util.Set;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.schema.IndexSchema;
+import org.junit.BeforeClass;
+
/**
* Tests for the field mutating update processors
* that parse Dates, Longs, Doubles, and Booleans.
*/
public class ParsingFieldUpdateProcessorsTest extends UpdateProcessorTestBase {
private static final double EPSILON = 1E-15;
+ private static final DateTimeFormatter isoDateOptionalTimeFormatter =
+ DateTimeFormatter.ofPattern("yyyy-MM-dd['T'HH:mm[:ss[.SSS]]][z", Locale.ROOT).withZone(ZoneOffset.UTC);
@BeforeClass
public static void beforeClass() throws Exception {
@@ -50,10 +57,8 @@ public class ParsingFieldUpdateProcessorsTest extends UpdateProcessorTestBase {
String dateString = "2010-11-12T13:14:15.168Z";
SolrInputDocument d = processAdd("parse-date", doc(f("id", "9"), f("date_dt", dateString)));
assertNotNull(d);
- DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
- DateTime dateTime = dateTimeFormatter.parseDateTime(dateString);
assertTrue(d.getFieldValue("date_dt") instanceof Date);
- assertEquals(dateTime.getMillis(), ((Date) d.getFieldValue("date_dt")).getTime());
+ assertEquals(Instant.parse(dateString), ((Date) d.getFieldValue("date_dt")).toInstant());
assertU(commit());
assertQ(req("id:9"), "//date[@name='date_dt'][.='" + dateString + "']");
}
@@ -64,10 +69,8 @@ public class ParsingFieldUpdateProcessorsTest extends UpdateProcessorTestBase {
String dateString = "2010-11-12T13:14:15.168Z";
SolrInputDocument d = processAdd("parse-date", doc(f("id", "39"), f("date_tdt", dateString)));
assertNotNull(d);
- DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
- DateTime dateTime = dateTimeFormatter.parseDateTime(dateString);
assertTrue(d.getFieldValue("date_tdt") instanceof Date);
- assertEquals(dateTime.getMillis(), ((Date) d.getFieldValue("date_tdt")).getTime());
+ assertEquals(Instant.parse(dateString), ((Date) d.getFieldValue("date_tdt")).toInstant());
assertU(commit());
assertQ(req("id:39"), "//date[@name='date_tdt'][.='" + dateString + "']");
}
@@ -77,14 +80,12 @@ public class ParsingFieldUpdateProcessorsTest extends UpdateProcessorTestBase {
IndexSchema schema = h.getCore().getLatestSchema();
assertNull(schema.getFieldOrNull("not_in_schema"));
String dateString = "2010-11-12T13:14:15.168Z";
- DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
- DateTime dateTime = dateTimeFormatter.parseDateTime(dateString);
SolrInputDocument d = processAdd("parse-date-no-run-processor",
doc(f("id", "18"), f("not_in_schema", dateString)));
assertNotNull(d);
assertTrue(d.getFieldValue("not_in_schema") instanceof Date);
- assertEquals(dateTime.getMillis(), ((Date)d.getFieldValue("not_in_schema")).getTime());
+ assertEquals(Instant.parse(dateString), ((Date)d.getFieldValue("not_in_schema")).toInstant());
d = processAdd("parse-date-no-run-processor",
doc(f("id", "36"), f("not_in_schema", "not a date", dateString)));
@@ -116,12 +117,8 @@ public class ParsingFieldUpdateProcessorsTest extends UpdateProcessorTestBase {
("parse-date-non-UTC-defaultTimeZone", doc(f("id", "99"), f("dateUTC_dt", dateStringUTC),
f("dateNoTimeZone_dt", dateStringNoTimeZone)));
assertNotNull(d);
- String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
- DateTimeFormatter dateTimeFormatterUTC = DateTimeFormat.forPattern(pattern);
- DateTime dateTimeUTC = dateTimeFormatterUTC.parseDateTime(dateStringUTC);
assertTrue(d.getFieldValue("dateUTC_dt") instanceof Date);
assertTrue(d.getFieldValue("dateNoTimeZone_dt") instanceof Date);
- assertEquals(dateTimeUTC.getMillis(), ((Date) d.getFieldValue("dateUTC_dt")).getTime());
assertU(commit());
assertQ(req("id:99")
,"//date[@name='dateUTC_dt'][.='" + dateStringUTC + "']"
@@ -132,22 +129,18 @@ public class ParsingFieldUpdateProcessorsTest extends UpdateProcessorTestBase {
IndexSchema schema = h.getCore().getLatestSchema();
assertNull(schema.getFieldOrNull("not_in_schema"));
String dateString = "2010-11-12T13:14:15.168Z";
- DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
- DateTime dateTime = dateTimeFormatter.parseDateTime(dateString);
SolrInputDocument d = processAdd("parse-date-explicit-not-in-schema-selector-no-run-processor",
doc(f("id", "88"), f("not_in_schema", dateString)));
assertNotNull(d);
assertTrue(d.getFieldValue("not_in_schema") instanceof Date);
- assertEquals(dateTime.getMillis(), ((Date)d.getFieldValue("not_in_schema")).getTime());
+ assertEquals(Instant.parse(dateString), ((Date)d.getFieldValue("not_in_schema")).toInstant());
}
public void testParseDateExplicitTypeClassSelector() throws Exception {
IndexSchema schema = h.getCore().getLatestSchema();
assertNotNull(schema.getFieldOrNull("date_dt"));
String dateString = "2010-11-12T13:14:15.168Z";
- DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
- DateTime dateTime = dateTimeFormatter.parseDateTime(dateString);
SolrInputDocument d;
if (schema.getField("date_dt").getType().isPointField()) {
d = processAdd("parse-date-explicit-typeclass-point-selector-no-run-processor",
@@ -159,71 +152,66 @@ public class ParsingFieldUpdateProcessorsTest extends UpdateProcessorTestBase {
assertNotNull(d);
assertTrue(d.getFieldValue("date_dt") instanceof Date);
- assertEquals(dateTime.getMillis(), ((Date)d.getFieldValue("date_dt")).getTime());
+ assertEquals(Instant.parse(dateString), ((Date)d.getFieldValue("date_dt")).toInstant());
}
public void testParseUSPacificDate() throws Exception {
IndexSchema schema = h.getCore().getLatestSchema();
assertNull(schema.getFieldOrNull("not_in_schema"));
String dateString = "8/9/2010"; // Interpreted as 00:00 US Pacific Daylight Time = UTC+07:00
- String dateStringUTC = "2010-08-09T07:00:00.000Z";
SolrInputDocument d = processAdd("US-Pacific-parse-date-no-run-processor",
doc(f("id", "288"), f("not_in_schema", dateString)));
assertNotNull(d);
assertTrue(d.getFieldValue("not_in_schema") instanceof Date);
- assertEquals(dateStringUTC,
- (new DateTime(((Date)d.getFieldValue("not_in_schema")).getTime(),DateTimeZone.UTC)).toString());
+ assertEquals(Instant.parse("2010-08-09T07:00:00.000Z"), ((Date)d.getFieldValue("not_in_schema")).toInstant());
}
public void testParseDateFormats() throws Exception {
String[] formatExamples = {
- "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "2010-01-15T00:00:00.000Z",
- "yyyy-MM-dd'T'HH:mm:ss,SSSZ", "2010-01-15T00:00:00,000Z",
- "yyyy-MM-dd'T'HH:mm:ss.SSS", "2010-01-15T00:00:00.000",
- "yyyy-MM-dd'T'HH:mm:ss,SSS", "2010-01-15T00:00:00,000",
- "yyyy-MM-dd'T'HH:mm:ssZ", "2010-01-15T00:00:00Z",
- "yyyy-MM-dd'T'HH:mm:ss", "2010-01-15T00:00:00",
- "yyyy-MM-dd'T'HH:mmZ", "2010-01-15T00:00Z",
- "yyyy-MM-dd'T'HH:mm", "2010-01-15T00:00",
- "yyyy-MM-dd HH:mm:ss.SSSZ", "2010-01-15 00:00:00.000Z",
- "yyyy-MM-dd HH:mm:ss,SSSZ", "2010-01-15 00:00:00,000Z",
- "yyyy-MM-dd HH:mm:ss.SSS", "2010-01-15 00:00:00.000",
- "yyyy-MM-dd HH:mm:ss,SSS", "2010-01-15 00:00:00,000",
- "yyyy-MM-dd HH:mm:ssZ", "2010-01-15 00:00:00Z",
- "yyyy-MM-dd HH:mm:ss", "2010-01-15 00:00:00",
- "yyyy-MM-dd HH:mmZ", "2010-01-15 00:00Z",
- "yyyy-MM-dd HH:mm", "2010-01-15 00:00",
- "yyyy-MM-dd hh:mm a", "2010-01-15 12:00 AM",
- "yyyy-MM-dd hh:mma", "2010-01-15 12:00AM",
- "yyyy-MM-dd", "2010-01-15",
- "EEE MMM dd HH:mm:ss Z yyyy", "Fri Jan 15 00:00:00 +0000 2010",
- "EEE MMM dd HH:mm:ss yyyy Z", "Fri Jan 15 00:00:00 2010 +00:00",
- "EEE MMM dd HH:mm:ss yyyy", "Fri Jan 15 00:00:00 2010",
- "EEE, dd MMM yyyy HH:mm:ss Z", "Fri, 15 Jan 2010 00:00:00 +00:00",
- "EEEE, dd-MMM-yy HH:mm:ss Z", "Friday, 15-Jan-10 00:00:00 +00:00",
- "EEEE, MMMM dd, yyyy", "Friday, January 15, 2010",
- "MMMM dd, yyyy", "January 15, 2010",
- "MMM. dd, yyyy", "Jan. 15, 2010"
+ "2010-01-15T00:00:00.000Z",
+ "2010-01-15T00:00:00,000Z",
+ "2010-01-15T00:00:00.000",
+ "2010-01-15T00:00:00,000",
+ "2010-01-15T00:00:00Z",
+ "2010-01-15T00:00:00",
+ "2010-01-15T00:00Z",
+ "2010-01-15T00:00",
+ "2010-01-15 00:00:00.000Z",
+ "2010-01-15 00:00:00,000Z",
+ "2010-01-15 00:00:00.000",
+ "2010-01-15 00:00:00,000",
+ "2010-01-15 00:00:00Z",
+ "2010-01-15 00:00:00",
+ "2010-01-15 00:00Z",
+ "2010-01-15 00:00",
+ "2010-01-15 12:00 AM",
+ "2010-01-15 12:00AM",
+ "2010-01-15",
+ "Fri Jan 15 00:00:00 +0000 2010",
+ "Fri Jan 15 00:00:00 2010 +00:00",
+ "Fri Jan 15 00:00:00 2010",
+ "Fri, 15 Jan 2010 00:00:00 +00:00",
+ "Friday, 15-Jan-10 00:00:00 +00:00",
+ "Friday, January 15, 2010",
+ "January 15, 2010",
+ "Jan. 15, 2010"
};
IndexSchema schema = h.getCore().getLatestSchema();
assertNotNull(schema.getFieldOrNull("dateUTC_dt")); // should match "*_dt" dynamic field
- String dateTimePattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
- DateTimeFormatter dateTimeFormatterUTC = DateTimeFormat.forPattern(dateTimePattern);
- DateTime dateTimeUTC = dateTimeFormatterUTC.parseDateTime(formatExamples[1]);
+ Instant expectedInstant = Instant.parse(formatExamples[0]);
- for (int i = 0 ; i < formatExamples.length ; i += 2) {
- String format = formatExamples[i];
- String dateString = formatExamples[i + 1];
+ for (int i = 0 ; i < formatExamples.length ; ++i) {
+ String dateString = formatExamples[i];
String id = "95" + i;
SolrInputDocument d = processAdd("parse-date-UTC-defaultTimeZone-no-run-processor",
doc(f("id", id), f("dateUTC_dt", dateString)));
assertNotNull(d);
- assertTrue("date '" + dateString + "' with format '" + format + "' is not mutated to a Date",
+ assertTrue("index: " + i + " date '" + dateString + "' is not mutated to a Date",
d.getFieldValue("dateUTC_dt") instanceof Date);
- assertEquals("date '" + dateString + "' with format '" + format + "' mismatched milliseconds",
- dateTimeUTC.getMillis(), ((Date)d.getFieldValue("dateUTC_dt")).getTime());
+ assertEquals("date '" + dateString + "' mismatched milliseconds",
+ expectedInstant, ((Date)d.getFieldValue("dateUTC_dt")).toInstant());
}
}
@@ -232,23 +220,20 @@ public class ParsingFieldUpdateProcessorsTest extends UpdateProcessorTestBase {
assertNull(schema.getFieldOrNull("not_in_schema"));
String frenchDateString = "le vendredi 15 janvier 2010";
String dateString = "2010-01-15T00:00:00.000Z";
- DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
- DateTime dateTime = dateTimeFormatter.parseDateTime(dateString);
SolrInputDocument d = processAdd("parse-french-date-UTC-defaultTimeZone-no-run-processor",
doc(f("id", "88"), f("not_in_schema", frenchDateString)));
assertNotNull(d);
assertTrue(d.getFieldValue("not_in_schema") instanceof Date);
- assertEquals(dateTime.getMillis(), ((Date)d.getFieldValue("not_in_schema")).getTime());
+ assertEquals(Instant.parse(dateString), ((Date)d.getFieldValue("not_in_schema")).toInstant());
}
public void testFailedParseMixedDate() throws Exception {
IndexSchema schema = h.getCore().getLatestSchema();
assertNull(schema.getFieldOrNull("not_in_schema"));
- DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateOptionalTimeParser().withZoneUTC();
Map