Repository: phoenix Updated Branches: refs/heads/4.0 5614dbd5e -> 77f24050c
PHOENIX-1387 Local index is not use when the query like "select * from dataTable where c1 like 'abc%'". Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/77f24050 Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/77f24050 Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/77f24050 Branch: refs/heads/4.0 Commit: 77f24050cc326fe98579d58fa52a2725f77244a7 Parents: 5614dbd Author: Rajeshbabu Chintaguntla <rajeshbabu.chintagun...@huawei.com> Authored: Wed Oct 29 21:19:09 2014 +0530 Committer: Rajeshbabu Chintaguntla <rajeshbabu.chintagun...@huawei.com> Committed: Fri Oct 31 07:58:26 2014 +0530 ---------------------------------------------------------------------- .../phoenix/end2end/index/LocalIndexIT.java | 24 +++++++++- .../phoenix/end2end/index/MutableIndexIT.java | 15 +++++- .../phoenix/compile/ProjectionCompiler.java | 50 +++++++++++++++++--- 3 files changed, 80 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/77f24050/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java index 7fa69d4..f03d053 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/LocalIndexIT.java @@ -266,7 +266,29 @@ public class LocalIndexIT extends BaseIndexIT { HBaseAdmin admin = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES).getAdmin(); int numRegions = admin.getTableRegions(TableName.valueOf(DATA_TABLE_NAME)).size(); - String query = "SELECT t_id, k1, k2,V1 FROM " + DATA_TABLE_NAME +" where v1='a'"; + String query = "SELECT * FROM " + DATA_TABLE_NAME +" where v1 like 'a%'"; + rs = conn1.createStatement().executeQuery("EXPLAIN "+ query); + + assertEquals( + "CLIENT PARALLEL " + numRegions + "-WAY RANGE SCAN OVER " + + MetaDataUtil.getLocalIndexTableName(DATA_TABLE_NAME) + " [-32768,'a'] - [-32768,'b']\nCLIENT MERGE SORT", + QueryUtil.getExplainPlan(rs)); + + rs = conn1.createStatement().executeQuery(query); + assertTrue(rs.next()); + assertEquals("f", rs.getString("t_id")); + assertEquals(1, rs.getInt("k1")); + assertEquals(2, rs.getInt("k2")); + assertEquals("a", rs.getString("v1")); + assertEquals(3, rs.getInt("k3")); + assertTrue(rs.next()); + assertEquals("j", rs.getString("t_id")); + assertEquals(2, rs.getInt("k1")); + assertEquals(4, rs.getInt("k2")); + assertEquals("a", rs.getString("v1")); + assertEquals(2, rs.getInt("k3")); + assertFalse(rs.next()); + query = "SELECT t_id, k1, k2,V1 FROM " + DATA_TABLE_NAME +" where v1='a'"; rs = conn1.createStatement().executeQuery("EXPLAIN "+ query); assertEquals( http://git-wip-us.apache.org/repos/asf/phoenix/blob/77f24050/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/MutableIndexIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/MutableIndexIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/MutableIndexIT.java index 429f96c..b4cbf9b 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/MutableIndexIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/index/MutableIndexIT.java @@ -333,7 +333,20 @@ public class MutableIndexIT extends BaseMutableIndexIT { assertEquals(4, rs.getInt(2)); assertEquals(5L, rs.getLong(3)); assertFalse(rs.next()); - + if(localIndex) { + query = "SELECT b.* from " + DATA_TABLE_FULL_NAME + " where int_col1 = 4"; + rs = conn.createStatement().executeQuery("EXPLAIN " + query); + assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER _LOCAL_IDX_" + DATA_TABLE_FULL_NAME +" [-32768]\n" + + " SERVER FILTER BY TO_INTEGER(INT_COL1) = 4\nCLIENT MERGE SORT", QueryUtil.getExplainPlan(rs)); + rs = conn.createStatement().executeQuery(query); + assertTrue(rs.next()); + assertEquals("varchar_b", rs.getString(1)); + assertEquals("charb", rs.getString(2)); + assertEquals(5, rs.getInt(3)); + assertEquals(5, rs.getLong(4)); + assertFalse(rs.next()); + + } } finally { conn.close(); } http://git-wip-us.apache.org/repos/asf/phoenix/blob/77f24050/phoenix-core/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java b/phoenix-core/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java index 0647e2e..3204e03 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/compile/ProjectionCompiler.java @@ -66,12 +66,15 @@ import org.apache.phoenix.schema.ColumnNotFoundException; import org.apache.phoenix.schema.ColumnRef; import org.apache.phoenix.schema.KeyValueSchema; import org.apache.phoenix.schema.KeyValueSchema.KeyValueSchemaBuilder; +import org.apache.phoenix.schema.ColumnFamilyNotFoundException; +import org.apache.phoenix.schema.LocalIndexDataColumnRef; import org.apache.phoenix.schema.PColumn; import org.apache.phoenix.schema.PColumnFamily; import org.apache.phoenix.schema.PDataType; import org.apache.phoenix.schema.PDatum; import org.apache.phoenix.schema.PName; import org.apache.phoenix.schema.PTable; +import org.apache.phoenix.schema.PTable.IndexType; import org.apache.phoenix.schema.PTable.ViewType; import org.apache.phoenix.schema.PTableKey; import org.apache.phoenix.schema.PTableType; @@ -172,9 +175,11 @@ public class ProjectionCompiler { int tableOffset = table.getBucketNum() == null ? 0 : 1; int minTablePKOffset = getMinPKOffset(table, tenantId); int minIndexPKOffset = getMinPKOffset(index, tenantId); - if (index.getColumns().size()-minIndexPKOffset != table.getColumns().size()-minTablePKOffset) { - // We'll end up not using this by the optimizer, so just throw - throw new ColumnNotFoundException(WildcardParseNode.INSTANCE.toString()); + if (index.getIndexType() != IndexType.LOCAL) { + if (index.getColumns().size()-minIndexPKOffset != table.getColumns().size()-minTablePKOffset) { + // We'll end up not using this by the optimizer, so just throw + throw new ColumnNotFoundException(WildcardParseNode.INSTANCE.toString()); + } } for (int i = tableOffset, j = tableOffset; i < table.getColumns().size(); i++) { PColumn column = table.getColumns().get(i); @@ -185,8 +190,23 @@ public class ProjectionCompiler { } PColumn tableColumn = table.getColumns().get(i); String indexColName = IndexUtil.getIndexColumnName(tableColumn); - PColumn indexColumn = index.getColumn(indexColName); - ColumnRef ref = new ColumnRef(tableRef,indexColumn.getPosition()); + PColumn indexColumn = null; + ColumnRef ref = null; + try { + indexColumn = index.getColumn(indexColName); + ref = new ColumnRef(tableRef, indexColumn.getPosition()); + } catch (ColumnNotFoundException e) { + if (index.getIndexType() == IndexType.LOCAL) { + try { + ref = new LocalIndexDataColumnRef(context, indexColName); + indexColumn = ref.getColumn(); + } catch (ColumnFamilyNotFoundException c) { + throw e; + } + } else { + throw e; + } + } String colName = tableColumn.getName().getString(); if (resolveColumn) { if (tableRef.getTableAlias() != null) { @@ -230,8 +250,24 @@ public class ProjectionCompiler { PTable table = conn.getMetaDataCache().getTable(new PTableKey(conn.getTenantId(), tableName)); PColumnFamily pfamily = table.getColumnFamily(cfName); for (PColumn column : pfamily.getColumns()) { - PColumn indexColumn = index.getColumn(IndexUtil.getIndexColumnName(column)); - ColumnRef ref = new ColumnRef(tableRef, indexColumn.getPosition()); + String indexColName = IndexUtil.getIndexColumnName(column); + PColumn indexColumn = null; + ColumnRef ref = null; + try { + indexColumn = index.getColumn(indexColName); + ref = new ColumnRef(tableRef, indexColumn.getPosition()); + } catch (ColumnNotFoundException e) { + if (index.getIndexType() == IndexType.LOCAL) { + try { + ref = new LocalIndexDataColumnRef(context, indexColName); + indexColumn = ref.getColumn(); + } catch (ColumnFamilyNotFoundException c) { + throw e; + } + } else { + throw e; + } + } Expression expression = ref.newColumnExpression(); projectedExpressions.add(expression); String colName = column.getName().toString();