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

Ingest: Support integer and long hex values in convert #32213

Merged
merged 5 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ enum Type {
@Override
public Object convert(Object value) {
try {
return Integer.parseInt(value.toString());
return Integer.decode(value.toString());
Copy link
Member

@jasontedor jasontedor Jul 19, 2018

Choose a reason for hiding this comment

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

I worry about using Integer#decode, I think this is a silent dangerous breaking change. Today with Integer#parseInt 010 would be parsed as 10. With Integer#decode it would be parsed as an octal to 8!

Copy link
Member

@jasontedor jasontedor Jul 20, 2018

Choose a reason for hiding this comment

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

Here is a test case that passes today and fails after this change:

diff --git a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java
index 4a6ce21b2dc..2dece6d1b9a 100644
--- a/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java
+++ b/modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java
@@ -59,6 +59,14 @@ public class ConvertProcessorTests extends ESTestCase {
         assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(randomInt));
     }
 
+    public void testConvertIntWithLeadingZero() throws Exception {
+        IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
+        String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "010");
+        Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.INTEGER, false);
+        processor.execute(ingestDocument);
+        assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(10));
+    }
+
     public void testConvertIntList() throws Exception {
         IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
         int numItems = randomIntBetween(1, 10);

Now this gives:

FAILURE 0.14s | ConvertProcessorTests.testConvertIntWithLeadingZero <<< FAILURES!
   > Throwable #1: java.lang.AssertionError: 
   > Expected: <10>
   >      but: was <8>
   >    at __randomizedtesting.SeedInfo.seed([535A28EEEA332364:F868934FEE583676]:0)
   >    at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
   >    at org.elasticsearch.ingest.common.ConvertProcessorTests.testConvertIntWithLeadingZero(ConvertProcessorTests.java:67)
   >    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   >    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   >    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   >    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
   >    at java.base/java.lang.Thread.run(Thread.java:844)
  2> NOTE: leaving temporary files on disk at: /home/jason/src/elastic/elasticsearch/modules/ingest-common/build/testrun/test/J0/temp/org.elasticsearch.ingest.common.ConvertProcessorTests_535A28EEEA332364-001
  2> NOTE: test params are: codec=Asserting(Lucene70): {}, docValues:{}, maxPointsInLeafNode=616, maxMBSortInHeap=6.377649114725773, sim=RandomSimilarity(queryNorm=true): {}, locale=ckb-IQ, timezone=EST
  2> NOTE: Linux 4.17.5-200.fc28.x86_64 amd64/Oracle Corporation 10.0.1 (64-bit)/cpus=20,threads=1,free=492873192,total=536870912
  2> NOTE: All tests run in this JVM: [ConvertProcessorTests]
Completed [1/1] in 0.82s, 1 test, 1 failure <<< FAILURES!


> Task :modules:ingest-common:test FAILED
   [junit4] <JUnit4> says hello! Master seed: 535A28EEEA332364
==> Test Info: seed=535A28EEEA332364; jvm=1; suite=1
Tests with failures:
  - org.elasticsearch.ingest.common.ConvertProcessorTests.testConvertIntWithLeadingZero

   [junit4] JVM J0:     0.31 ..     1.71 =     1.39s
   [junit4] Execution time total: 1.72 sec.
   [junit4] Tests summary: 1 suite, 1 test, 1 failure

Copy link
Member Author

@rjernst rjernst Jul 20, 2018

Choose a reason for hiding this comment

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

I see the concern. Would you be ok with having a sysprop that controls this in 6.x (defaulting to the old behavior), and logging a deprecation warning in 6.x for the old behavior? While I understand the concern, having numbers zero padded is very rare IMO, so the likelihood this affects that many users seems low.

Alternatively, I can switch back to my first implementation and test for "0x", only passing to decode with that prefix.

Copy link
Member

Choose a reason for hiding this comment

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

A system property, or alternatively a converter setting on the processor, is okay with me! The nice thing with the latter is we can introduce this with no behavior change today. I’m open to either approach so curious to hear your preference.

} catch(NumberFormatException e) {
throw new IllegalArgumentException("unable to convert [" + value + "] to integer", e);
}
Expand All @@ -52,7 +52,7 @@ public Object convert(Object value) {
@Override
public Object convert(Object value) {
try {
return Long.parseLong(value.toString());
return Long.decode(value.toString());
} catch(NumberFormatException e) {
throw new IllegalArgumentException("unable to convert [" + value + "] to long", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public void testConvertInt() throws Exception {
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(randomInt));
}

public void testConvertIntHex() throws Exception {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
int randomInt = randomInt();
String intString = randomInt < 0 ? "-0x" + Integer.toHexString(-randomInt) : "0x" + Integer.toHexString(randomInt);
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, intString);
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.INTEGER, false);
processor.execute(ingestDocument);
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(randomInt));
}

public void testConvertIntList() throws Exception {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
int numItems = randomIntBetween(1, 10);
Expand Down Expand Up @@ -92,6 +102,16 @@ public void testConvertLong() throws Exception {
assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(randomLong));
}

public void testConvertLongHex() throws Exception {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
long randomLong = randomLong();
String longString = randomLong < 0 ? "-0x" + Long.toHexString(-randomLong) : "0x" + Long.toHexString(randomLong);
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, longString);
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false);
processor.execute(ingestDocument);
assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(randomLong));
}

public void testConvertLongList() throws Exception {
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
int numItems = randomIntBetween(1, 10);
Expand Down