use of org.apache.phoenix.query.KeyRange in project phoenix by apache.
the class ViewIndexIT method testMultiTenantViewLocalIndex.
@Test
public void testMultiTenantViewLocalIndex() throws Exception {
String schemaName = generateUniqueName();
String tableName = generateUniqueName();
String indexName = "IND_" + generateUniqueName();
String viewName = "VIEW_" + generateUniqueName();
String fullTableName = SchemaUtil.getTableName(schemaName, tableName);
createBaseTable(schemaName, tableName, true, null, null);
Connection conn = DriverManager.getConnection(getUrl());
PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + fullTableName + " VALUES(?,?,?,?,?)");
stmt.setString(1, "10");
stmt.setString(2, "a");
stmt.setInt(3, 1);
stmt.setString(4, "x1");
stmt.setInt(5, 100);
stmt.execute();
stmt.setString(1, "10");
stmt.setString(2, "b");
stmt.setInt(3, 2);
stmt.setString(4, "x2");
stmt.setInt(5, 200);
stmt.execute();
stmt.setString(1, "10");
stmt.setString(2, "c");
stmt.setInt(3, 3);
stmt.setString(4, "x3");
stmt.setInt(5, 300);
stmt.execute();
stmt.setString(1, "20");
stmt.setString(2, "d");
stmt.setInt(3, 4);
stmt.setString(4, "x4");
stmt.setInt(5, 400);
stmt.execute();
conn.commit();
Properties props = PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES);
props.setProperty("TenantId", "10");
Connection conn1 = DriverManager.getConnection(getUrl(), props);
conn1.createStatement().execute("CREATE VIEW " + viewName + " AS select * from " + fullTableName);
conn1.createStatement().execute("CREATE LOCAL INDEX " + indexName + " ON " + viewName + "(v2)");
conn1.commit();
String sql = "SELECT * FROM " + viewName + " WHERE v2 = 100";
ResultSet rs = conn1.prepareStatement("EXPLAIN " + sql).executeQuery();
assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER " + SchemaUtil.getPhysicalHBaseTableName(fullTableName, isNamespaceMapped, PTableType.TABLE) + " [1,'10',100]\n" + " SERVER FILTER BY FIRST KEY ONLY\n" + "CLIENT MERGE SORT", QueryUtil.getExplainPlan(rs));
rs = conn1.prepareStatement(sql).executeQuery();
assertTrue(rs.next());
assertFalse(rs.next());
TestUtil.analyzeTable(conn, fullTableName);
List<KeyRange> guideposts = TestUtil.getAllSplits(conn, fullTableName);
assertEquals(1, guideposts.size());
assertEquals(KeyRange.EVERYTHING_RANGE, guideposts.get(0));
conn.createStatement().execute("ALTER TABLE " + fullTableName + " SET " + PhoenixDatabaseMetaData.GUIDE_POSTS_WIDTH + "=20");
TestUtil.analyzeTable(conn, fullTableName);
guideposts = TestUtil.getAllSplits(conn, fullTableName);
assertEquals(5, guideposts.size());
// Confirm that when view index used, the GUIDE_POSTS_WIDTH from the data physical table
// was used
sql = "SELECT * FROM " + viewName + " WHERE v2 >= 100";
stmt = conn1.prepareStatement(sql);
stmt.executeQuery();
QueryPlan plan = stmt.unwrap(PhoenixStatement.class).getQueryPlan();
assertEquals(4, plan.getSplits().size());
}
use of org.apache.phoenix.query.KeyRange in project phoenix by apache.
the class ViewIndexIT method testCreatingIndexOnGlobalView.
@Test
public void testCreatingIndexOnGlobalView() throws Exception {
String baseTable = generateUniqueName();
String globalView = generateUniqueName();
String globalViewIdx = generateUniqueName();
try (Connection conn = DriverManager.getConnection(getUrl())) {
conn.createStatement().execute("CREATE TABLE " + baseTable + " (TENANT_ID CHAR(15) NOT NULL, PK2 DATE NOT NULL, PK3 INTEGER NOT NULL, KV1 VARCHAR, KV2 VARCHAR, KV3 CHAR(15) CONSTRAINT PK PRIMARY KEY(TENANT_ID, PK2 ROW_TIMESTAMP, PK3)) MULTI_TENANT=true");
conn.createStatement().execute("CREATE VIEW " + globalView + " AS SELECT * FROM " + baseTable);
conn.createStatement().execute("CREATE INDEX " + globalViewIdx + " ON " + globalView + " (PK3 DESC, KV3) INCLUDE (KV1)");
PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + globalView + " (TENANT_ID, PK2, PK3, KV1, KV3) VALUES (?, ?, ?, ?, ?)");
stmt.setString(1, "tenantId");
stmt.setDate(2, new Date(100));
stmt.setInt(3, 1);
stmt.setString(4, "KV1");
stmt.setString(5, "KV3");
stmt.executeUpdate();
stmt.setString(1, "tenantId");
stmt.setDate(2, new Date(100));
stmt.setInt(3, 2);
stmt.setString(4, "KV4");
stmt.setString(5, "KV5");
stmt.executeUpdate();
stmt.setString(1, "tenantId");
stmt.setDate(2, new Date(100));
stmt.setInt(3, 3);
stmt.setString(4, "KV6");
stmt.setString(5, "KV7");
stmt.executeUpdate();
stmt.setString(1, "tenantId");
stmt.setDate(2, new Date(100));
stmt.setInt(3, 4);
stmt.setString(4, "KV8");
stmt.setString(5, "KV9");
stmt.executeUpdate();
stmt.setString(1, "tenantId");
stmt.setDate(2, new Date(100));
stmt.setInt(3, 5);
stmt.setString(4, "KV10");
stmt.setString(5, "KV11");
stmt.executeUpdate();
conn.commit();
// Verify that query against the global view index works
stmt = conn.prepareStatement("SELECT KV1 FROM " + globalView + " WHERE PK3 = ? AND KV3 = ?");
stmt.setInt(1, 1);
stmt.setString(2, "KV3");
ResultSet rs = stmt.executeQuery();
QueryPlan plan = stmt.unwrap(PhoenixStatement.class).getQueryPlan();
assertTrue(plan.getTableRef().getTable().getName().getString().equals(globalViewIdx));
assertTrue(rs.next());
assertEquals("KV1", rs.getString(1));
assertFalse(rs.next());
TestUtil.analyzeTable(conn, baseTable);
List<KeyRange> guideposts = TestUtil.getAllSplits(conn, baseTable);
assertEquals(1, guideposts.size());
assertEquals(KeyRange.EVERYTHING_RANGE, guideposts.get(0));
conn.createStatement().execute("ALTER TABLE " + baseTable + " SET " + PhoenixDatabaseMetaData.GUIDE_POSTS_WIDTH + "=20");
TestUtil.analyzeTable(conn, baseTable);
guideposts = TestUtil.getAllSplits(conn, baseTable);
assertEquals(6, guideposts.size());
// Confirm that when view index used, the GUIDE_POSTS_WIDTH from the data physical table
// was used
stmt = conn.prepareStatement("SELECT KV1 FROM " + globalView + " WHERE PK3 = ? AND KV3 >= ?");
stmt.setInt(1, 1);
stmt.setString(2, "KV3");
rs = stmt.executeQuery();
plan = stmt.unwrap(PhoenixStatement.class).getQueryPlan();
assertTrue(plan.getTableRef().getTable().getName().getString().equals(globalViewIdx));
assertEquals(6, plan.getSplits().size());
}
}
use of org.apache.phoenix.query.KeyRange in project phoenix by apache.
the class PDataTypeTest method testDescExclusiveTimestampRange.
@Test
public void testDescExclusiveTimestampRange() {
long now = System.currentTimeMillis();
Timestamp ts1 = new Timestamp(now + MILLIS_IN_DAY);
final byte[] lowerRange = PTimestamp.INSTANCE.toBytes(ts1, SortOrder.DESC);
Timestamp ts2 = new Timestamp(now);
final byte[] upperRange = PTimestamp.INSTANCE.toBytes(ts2, SortOrder.DESC);
KeyRange range = PTimestamp.INSTANCE.getKeyRange(lowerRange, false, upperRange, false);
Timestamp ts3 = DateUtil.getTimestamp(now + MILLIS_IN_DAY - 1, 999999);
// Rolled up to next millis
final byte[] expectedLowerRange = PTimestamp.INSTANCE.toBytes(ts3, SortOrder.DESC);
assertTrue(Bytes.compareTo(expectedLowerRange, range.getLowerRange()) == 0);
assertTrue(Bytes.compareTo(upperRange, range.getUpperRange()) == 0);
}
use of org.apache.phoenix.query.KeyRange in project phoenix by apache.
the class ScanUtil method getKey.
private static byte[] getKey(RowKeySchema schema, List<List<KeyRange>> slots, int[] slotSpan, Bound bound) {
if (slots.isEmpty()) {
return KeyRange.UNBOUND;
}
int[] position = new int[slots.size()];
int maxLength = 0;
for (int i = 0; i < position.length; i++) {
position[i] = bound == Bound.LOWER ? 0 : slots.get(i).size() - 1;
KeyRange range = slots.get(i).get(position[i]);
Field field = schema.getField(i + slotSpan[i]);
int keyLength = range.getRange(bound).length;
if (!field.getDataType().isFixedWidth()) {
keyLength++;
if (range.isUnbound(bound) && !range.isInclusive(bound) && field.getSortOrder() == SortOrder.DESC) {
keyLength++;
}
}
maxLength += keyLength;
}
byte[] key = new byte[maxLength];
int length = setKey(schema, slots, slotSpan, position, bound, key, 0, 0, position.length);
if (length == 0) {
return KeyRange.UNBOUND;
}
if (length == maxLength) {
return key;
}
byte[] keyCopy = new byte[length];
System.arraycopy(key, 0, keyCopy, 0, length);
return keyCopy;
}
use of org.apache.phoenix.query.KeyRange in project phoenix by apache.
the class SkipScanFilterIntersectTest method assertSameSlots.
private void assertSameSlots(List<List<KeyRange>> expectedSlots, List<List<KeyRange>> slots) {
assertEquals(expectedSlots.size(), slots.size());
for (int i = 0; i < expectedSlots.size(); i++) {
List<KeyRange> expectedSlot = expectedSlots.get(i);
List<KeyRange> slot = slots.get(i);
assertEquals("index: " + i, expectedSlot.size(), slot.size());
for (int j = 0; j < expectedSlot.size(); j++) {
KeyRange expectedRange = expectedSlot.get(j);
KeyRange range = slot.get(j);
assertArrayEquals(expectedRange.getLowerRange(), range.getLowerRange());
assertArrayEquals(expectedRange.getUpperRange(), range.getUpperRange());
assertEquals(expectedRange.isLowerInclusive(), range.isLowerInclusive());
assertEquals(expectedRange.isUpperInclusive(), range.isUpperInclusive());
}
}
}
Aggregations