PHOENIX-3498 Query with index failed when query back to data table with desc PK column
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/3b3d8ad3 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/3b3d8ad3 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/3b3d8ad3 Branch: refs/heads/calcite Commit: 3b3d8ad3ab3bb0fae013154878ba0467691c5b30 Parents: 8152131 Author: William Yang <yhxx...@163.com> Authored: Fri Nov 18 10:57:19 2016 -0800 Committer: maryannxue <maryann....@gmail.com> Committed: Fri Nov 18 10:57:19 2016 -0800 ---------------------------------------------------------------------- .../apache/phoenix/end2end/index/IndexIT.java | 45 ++++++++++++++++++++ .../apache/phoenix/join/HashCacheClient.java | 4 +- 2 files changed, 47 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/3b3d8ad3/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexIT.java index 8ab563c..48fe3e6 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/IndexIT.java @@ -1093,4 +1093,49 @@ public class IndexIT extends ParallelStatsDisabledIT { } } + @Test + public void testQueryBackToDataTableWithDescPKColumn() throws SQLException { + doTestQueryBackToDataTableWithDescPKColumn(true); + doTestQueryBackToDataTableWithDescPKColumn(false); + } + + private void doTestQueryBackToDataTableWithDescPKColumn(boolean isSecondPKDesc) throws SQLException { + Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES); + String tableName = "TBL_" + generateUniqueName(); + String indexName = "IND_" + generateUniqueName(); + String fullTableName = SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, tableName); + String fullIndexName = SchemaUtil.getTableName(TestUtil.DEFAULT_SCHEMA_NAME, indexName); + + try (Connection conn = DriverManager.getConnection(getUrl(), props)) { + // create data table and index table + conn.setAutoCommit(true); + Statement stmt = conn.createStatement(); + String ddl = "CREATE TABLE " + fullTableName + "(p1 integer not null, p2 integer not null, " + + " a integer, b integer CONSTRAINT PK PRIMARY KEY "; + if (isSecondPKDesc) { + ddl += "(p1, p2 desc))"; + } else { + ddl += "(p1 desc, p2))"; + } + stmt.executeUpdate(ddl); + ddl = "CREATE "+ (localIndex ? "LOCAL " : "") + " INDEX " + fullIndexName + " on " + fullTableName + "(a)"; + stmt.executeUpdate(ddl); + + // upsert a single row + String upsert = "UPSERT INTO " + fullTableName + " VALUES(1,2,3,4)"; + stmt.executeUpdate(upsert); + + // try select with index + // a = 3, should hit index table, but we select column B, so it will query back to data table + String query = "SELECT /*+index(" + fullTableName + " " + fullIndexName + "*/ b from " + fullTableName + + " WHERE a = 3"; + ResultSet rs = stmt.executeQuery(query); + assertTrue(rs.next()); + assertEquals(4, rs.getInt(1)); + assertFalse(rs.next()); + rs.close(); + stmt.close(); + } + } + } http://git-wip-us.apache.org/repos/asf/phoenix/blob/3b3d8ad3/phoenix-core/src/main/java/org/apache/phoenix/join/HashCacheClient.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/join/HashCacheClient.java b/phoenix-core/src/main/java/org/apache/phoenix/join/HashCacheClient.java index e8b3389..32d0469 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/join/HashCacheClient.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/join/HashCacheClient.java @@ -158,7 +158,7 @@ public class HashCacheClient { PDataType type = keyExpression.getDataType(); keyExpression.reset(); if (keyExpression.evaluate(tuple, ptr)) { - return LiteralExpression.newConstant(type.toObject(ptr), type); + return LiteralExpression.newConstant(type.toObject(ptr, keyExpression.getSortOrder()), type); } return LiteralExpression.newConstant(null, type); @@ -170,7 +170,7 @@ public class HashCacheClient { PDataType type = child.getDataType(); child.reset(); if (child.evaluate(tuple, ptr)) { - values.add(LiteralExpression.newConstant(type.toObject(ptr), type)); + values.add(LiteralExpression.newConstant(type.toObject(ptr, child.getSortOrder()), type)); } else { values.add(LiteralExpression.newConstant(null, type)); }