gianm closed pull request #6390: Fix cache keys of DefaultDimensionSpec and
ExtractionDimensionSpec
URL: https://github.com/apache/incubator-druid/pull/6390
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/processing/src/main/java/org/apache/druid/query/dimension/DefaultDimensionSpec.java
b/processing/src/main/java/org/apache/druid/query/dimension/DefaultDimensionSpec.java
index 9f8e27ae2df..b948f6e0066 100644
---
a/processing/src/main/java/org/apache/druid/query/dimension/DefaultDimensionSpec.java
+++
b/processing/src/main/java/org/apache/druid/query/dimension/DefaultDimensionSpec.java
@@ -24,12 +24,11 @@
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
-import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.query.cache.CacheKeyBuilder;
import org.apache.druid.query.extraction.ExtractionFn;
import org.apache.druid.segment.DimensionSelector;
import org.apache.druid.segment.column.ValueType;
-import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
@@ -132,12 +131,10 @@ public boolean mustDecorate()
@Override
public byte[] getCacheKey()
{
- byte[] dimensionBytes = StringUtils.toUtf8(dimension);
-
- return ByteBuffer.allocate(1 + dimensionBytes.length)
- .put(CACHE_TYPE_ID)
- .put(dimensionBytes)
- .array();
+ return new CacheKeyBuilder(CACHE_TYPE_ID)
+ .appendString(dimension)
+ .appendString(outputType.toString())
+ .build();
}
@Override
diff --git
a/processing/src/main/java/org/apache/druid/query/dimension/ExtractionDimensionSpec.java
b/processing/src/main/java/org/apache/druid/query/dimension/ExtractionDimensionSpec.java
index 6a14a9f36c5..71885064de3 100644
---
a/processing/src/main/java/org/apache/druid/query/dimension/ExtractionDimensionSpec.java
+++
b/processing/src/main/java/org/apache/druid/query/dimension/ExtractionDimensionSpec.java
@@ -22,13 +22,11 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
-import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.query.cache.CacheKeyBuilder;
import org.apache.druid.query.extraction.ExtractionFn;
import org.apache.druid.segment.DimensionSelector;
import org.apache.druid.segment.column.ValueType;
-import java.nio.ByteBuffer;
-
/**
*/
public class ExtractionDimensionSpec implements DimensionSpec
@@ -114,14 +112,11 @@ public boolean mustDecorate()
@Override
public byte[] getCacheKey()
{
- byte[] dimensionBytes = StringUtils.toUtf8(dimension);
- byte[] dimExtractionFnBytes = extractionFn.getCacheKey();
-
- return ByteBuffer.allocate(1 + dimensionBytes.length +
dimExtractionFnBytes.length)
- .put(CACHE_TYPE_ID)
- .put(dimensionBytes)
- .put(dimExtractionFnBytes)
- .array();
+ return new CacheKeyBuilder(CACHE_TYPE_ID)
+ .appendString(dimension)
+ .appendCacheable(extractionFn)
+ .appendString(outputType.toString())
+ .build();
}
@Override
diff --git
a/processing/src/test/java/org/apache/druid/query/dimension/DefaultDimensionSpecTest.java
b/processing/src/test/java/org/apache/druid/query/dimension/DefaultDimensionSpecTest.java
index ba7ae52ec92..c0a251c7c71 100644
---
a/processing/src/test/java/org/apache/druid/query/dimension/DefaultDimensionSpecTest.java
+++
b/processing/src/test/java/org/apache/druid/query/dimension/DefaultDimensionSpecTest.java
@@ -55,4 +55,12 @@ public void testEqualsSerdeWithType() throws IOException
Assert.assertEquals(spec, other);
Assert.assertEquals(spec.hashCode(), other.hashCode());
}
+
+ @Test
+ public void testCacheKey()
+ {
+ final DimensionSpec spec = new DefaultDimensionSpec("foo", "foo",
ValueType.FLOAT);
+ final byte[] expected = new byte[] {0, 7, 102, 111, 111, 7, 70, 76, 79,
65, 84};
+ Assert.assertArrayEquals(expected, spec.getCacheKey());
+ }
}
diff --git
a/processing/src/test/java/org/apache/druid/query/dimension/ExtractionDimensionSpecTest.java
b/processing/src/test/java/org/apache/druid/query/dimension/ExtractionDimensionSpecTest.java
index e428d41c4d6..453656268dc 100644
---
a/processing/src/test/java/org/apache/druid/query/dimension/ExtractionDimensionSpecTest.java
+++
b/processing/src/test/java/org/apache/druid/query/dimension/ExtractionDimensionSpecTest.java
@@ -23,6 +23,7 @@
import org.apache.druid.jackson.DefaultObjectMapper;
import org.apache.druid.query.extraction.MatchingDimExtractionFn;
import org.apache.druid.query.extraction.RegexDimExtractionFn;
+import org.apache.druid.query.extraction.StrlenExtractionFn;
import org.apache.druid.segment.column.ValueType;
import org.junit.Assert;
import org.junit.Test;
@@ -144,4 +145,17 @@ public void testSerdeBackwardsCompatibility() throws
Exception
.getExtractionFn() instanceof MatchingDimExtractionFn
);
}
+
+ @Test
+ public void testCacheKey()
+ {
+ final ExtractionDimensionSpec dimensionSpec = new ExtractionDimensionSpec(
+ "foo",
+ "len",
+ ValueType.LONG,
+ StrlenExtractionFn.instance()
+ );
+ final byte[] expected = new byte[]{1, 7, 102, 111, 111, 9, 14, 7, 76, 79,
78, 71};
+ Assert.assertArrayEquals(expected, dimensionSpec.getCacheKey());
+ }
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]