GEODE-91: Zero results when querying against a HashIndex indexed on PdxStrings Pdx string fields indexed with a hash index were not being resolved properly when doing comparisons.
Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/27b37efc Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/27b37efc Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/27b37efc Branch: refs/heads/feature/GEODE-77 Commit: 27b37efcc34f1d338bd0f6fdfb75e6265a4fefbe Parents: 6ff83bc Author: Jason Huynh <jhu...@pivotal.io> Authored: Fri Jul 24 13:39:37 2015 -0700 Committer: Jason Huynh <jhu...@pivotal.io> Committed: Fri Jul 24 13:41:46 2015 -0700 ---------------------------------------------------------------------- .../cache/query/internal/index/HashIndex.java | 11 +++++++++++ .../internal/index/HashIndexJUnitTest.java | 20 +++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/27b37efc/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndex.java ---------------------------------------------------------------------- diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndex.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndex.java index ed45742..64b8a61 100755 --- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndex.java +++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndex.java @@ -73,6 +73,7 @@ import com.gemstone.gemfire.internal.logging.LogService; import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl.Chunk; import com.gemstone.gemfire.internal.offheap.annotations.Released; import com.gemstone.gemfire.internal.offheap.annotations.Retained; +import com.gemstone.gemfire.pdx.internal.PdxString; /** * A HashIndex is an index that can be used for equal and not equals queries It @@ -1554,6 +1555,16 @@ public class HashIndex extends AbstractIndex { return true; } else { try { + if (fieldValue instanceof PdxString) { + if (indexKey instanceof String) { + fieldValue = ((PdxString) fieldValue).toString(); + } + } + else if (indexKey instanceof PdxString) { + if (fieldValue instanceof String) { + fieldValue = new PdxString((String)fieldValue); + } + } return TypeUtils.compare(fieldValue, indexKey, OQLLexerTokenTypes.TOK_EQ).equals(Boolean.TRUE); } catch (TypeMismatchException e) { http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/27b37efc/gemfire-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/HashIndexJUnitTest.java ---------------------------------------------------------------------- diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/HashIndexJUnitTest.java b/gemfire-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/HashIndexJUnitTest.java index c1d6e31..29a485b 100755 --- a/gemfire-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/HashIndexJUnitTest.java +++ b/gemfire-core/src/test/java/com/gemstone/gemfire/cache/query/internal/index/HashIndexJUnitTest.java @@ -40,6 +40,7 @@ import com.gemstone.gemfire.cache.query.data.Portfolio; import com.gemstone.gemfire.cache.query.internal.QueryObserverAdapter; import com.gemstone.gemfire.cache.query.internal.QueryObserverHolder; import com.gemstone.gemfire.test.junit.categories.IntegrationTest; +import com.gemstone.gemfire.pdx.PdxInstance; @Category(IntegrationTest.class) public class HashIndexJUnitTest { @@ -1354,6 +1355,23 @@ public class HashIndexJUnitTest { HashIndexSet.TEST_ALWAYS_REHASH = false; } } + + @Test + public void testPdxWithStringIndexKeyValues() throws Exception { + createPartitionedRegion("test_region"); + int numEntries = 10; + Index index = qs.createHashIndex("idHash", "p.id", "/test_region p"); + for (int i = 0; i < numEntries; i++) { + PdxInstance record = CacheUtils.getCache().createPdxInstanceFactory("test_region").writeString("id", "" + i).writeString("domain", "A").create(); + region.put("" + i, record); + } + + SelectResults results = (SelectResults) qs.newQuery( + "SELECT DISTINCT tr.domain FROM /test_region tr WHERE tr.id='1'").execute(); + assertEquals(1, results.size()); + assertTrue(observer.indexUsed); + } + private void printIndex(Index index) { if (index instanceof PartitionedIndex) { @@ -1561,4 +1579,4 @@ public class HashIndexJUnitTest { }; -} \ No newline at end of file +}