/** * Get the key used in the Phoenix metadata row for a table definition * @param schemaName * @param tableName */ public static byte[] getTableKey(byte[] tenantId, byte[] schemaName, byte[] tableName) { return ByteUtil.concat(tenantId, QueryConstants.SEPARATOR_BYTE_ARRAY, schemaName, QueryConstants.SEPARATOR_BYTE_ARRAY, tableName); }
public static byte[] getParentLinkKey(byte[] tenantId, byte[] schemaName, byte[] tableName, byte[] indexName) { return ByteUtil.concat(tenantId == null ? ByteUtil.EMPTY_BYTE_ARRAY : tenantId, QueryConstants.SEPARATOR_BYTE_ARRAY, schemaName == null ? ByteUtil.EMPTY_BYTE_ARRAY : schemaName, QueryConstants.SEPARATOR_BYTE_ARRAY, tableName, QueryConstants.SEPARATOR_BYTE_ARRAY, QueryConstants.SEPARATOR_BYTE_ARRAY, indexName); } }
private static byte[] getNameAsBytes(byte[] nameOne, byte[] nameTwo) { if (nameOne == null || nameOne.length == 0) { return nameTwo; } else if ((nameTwo == null || nameTwo.length == 0)) { return nameOne; } else { return ByteUtil.concat(nameOne, QueryConstants.NAME_SEPARATOR_BYTES, nameTwo); } }
public static byte[] getIndexColumnName(byte[] dataColumnFamilyName, byte[] dataColumnName) { return ByteUtil.concat(dataColumnFamilyName == null ? ByteUtil.EMPTY_BYTE_ARRAY : dataColumnFamilyName, INDEX_COLUMN_NAME_SEP_BYTES, dataColumnName); }
public static byte[] getTableKey(String tenantId, String schemaName, String tableName) { return ByteUtil.concat(tenantId == null ? ByteUtil.EMPTY_BYTE_ARRAY : Bytes.toBytes(tenantId), QueryConstants.SEPARATOR_BYTE_ARRAY, schemaName == null ? ByteUtil.EMPTY_BYTE_ARRAY : Bytes.toBytes(schemaName), QueryConstants.SEPARATOR_BYTE_ARRAY, Bytes.toBytes(tableName)); }
public static byte[] getParentLinkKey(String tenantId, String schemaName, String tableName, String indexName) { return ByteUtil.concat(tenantId == null ? ByteUtil.EMPTY_BYTE_ARRAY : Bytes.toBytes(tenantId), QueryConstants.SEPARATOR_BYTE_ARRAY, schemaName == null ? ByteUtil.EMPTY_BYTE_ARRAY : Bytes.toBytes(schemaName), QueryConstants.SEPARATOR_BYTE_ARRAY, Bytes.toBytes(tableName), QueryConstants.SEPARATOR_BYTE_ARRAY, QueryConstants.SEPARATOR_BYTE_ARRAY, Bytes.toBytes(indexName)); }
public KeyRange appendSeparator() { byte[] lowerBound = getLowerRange(); byte[] upperBound = getUpperRange(); if (lowerBound != UNBOUND) { lowerBound = ByteUtil.concat(lowerBound, SEPARATOR_BYTE_ARRAY); } if (upperBound != UNBOUND) { upperBound = ByteUtil.concat(upperBound, SEPARATOR_BYTE_ARRAY); } return getKeyRange(lowerBound, lowerInclusive, upperBound, upperInclusive); }
private static Scan newTableRowsScan(byte[] key, long startTimeStamp, long stopTimeStamp) throws IOException { Scan scan = new Scan(); scan.setTimeRange(startTimeStamp, stopTimeStamp); scan.setStartRow(key); byte[] stopKey = ByteUtil.concat(key, QueryConstants.SEPARATOR_BYTE_ARRAY); ByteUtil.nextKey(stopKey, stopKey.length); scan.setStopRow(stopKey); return scan; }
@Test public void testSubstrSetScanKey() throws Exception { String query = "SELECT inst FROM ptsdb WHERE substr(inst, 0, 3) = 'abc'"; List<Object> binds = Collections.emptyList(); Scan scan = compileQuery(query, binds); assertArrayEquals(ByteUtil.concat(Bytes.toBytes("abc")), scan.getStartRow()); assertArrayEquals(ByteUtil.concat(Bytes.toBytes("abd"),QueryConstants.SEPARATOR_BYTE_ARRAY), scan.getStopRow()); assertTrue(scan.getFilter() == null); // Extracted. }
@Test public void testRTrimSetScanKey() throws Exception { String query = "SELECT inst FROM ptsdb WHERE rtrim(inst) = 'abc'"; List<Object> binds = Collections.emptyList(); Scan scan = compileQuery(query, binds); assertArrayEquals(ByteUtil.concat(Bytes.toBytes("abc")), scan.getStartRow()); assertArrayEquals(ByteUtil.concat(ByteUtil.nextKey(Bytes.toBytes("abc ")),QueryConstants.SEPARATOR_BYTE_ARRAY), scan.getStopRow()); assertNotNull(scan.getFilter()); }
@Test public void testCombiningRVCWithNonRVCUsingOr() throws SQLException { String firstTenantId = "000000000000001"; String secondTenantId = "000000000000005"; String firstParentId = "000000000000011"; String query = "select * from entity_history where (organization_id, parent_id) >= (?,?) OR organization_id >= ?"; Scan scan = new Scan(); List<Object> binds = Arrays.<Object>asList(firstTenantId, firstParentId, secondTenantId); HashSet<Expression> extractedFilters = new HashSet<Expression>(); compileStatement(query, scan, binds, extractedFilters); assertTrue(extractedFilters.size() == 1); assertArrayEquals(ByteUtil.concat(PDataType.VARCHAR.toBytes(firstTenantId), PDataType.VARCHAR.toBytes(firstParentId)), scan.getStartRow()); assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow()); }
@Test public void testBasicRVCExpression() throws SQLException { String tenantId = "000000000000001"; String entityId = "002333333333331"; String query = "select * from atable where (organization_id,entity_id) >= (?,?)"; Scan scan = new Scan(); List<Object> binds = Arrays.<Object>asList(tenantId, entityId); HashSet<Expression> extractedFilters = new HashSet<Expression>(); compileStatement(query, scan, binds, extractedFilters); assertTrue(extractedFilters.size() == 1); byte[] expectedStartRow = ByteUtil.concat(PDataType.CHAR.toBytes(tenantId), PDataType.CHAR.toBytes(entityId)); assertArrayEquals(expectedStartRow, scan.getStartRow()); assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow()); }
@Test public void testMultiKeyBindExpression() throws SQLException { String tenantId = "000000000000001"; String keyPrefix = "002"; String query = "select * from atable where organization_id=? and substr(entity_id,1,3)=?"; Scan scan = new Scan(); List<Object> binds = Arrays.<Object>asList(tenantId,keyPrefix); compileStatement(query, scan, binds); byte[] startRow = ByteUtil.concat(PDataType.VARCHAR.toBytes(tenantId),ByteUtil.fillKey(PDataType.VARCHAR.toBytes(keyPrefix),15)); assertArrayEquals(startRow, scan.getStartRow()); byte[] stopRow = ByteUtil.concat(PDataType.VARCHAR.toBytes(tenantId),ByteUtil.fillKey(ByteUtil.nextKey(PDataType.VARCHAR.toBytes(keyPrefix)),15)); assertArrayEquals(stopRow, scan.getStopRow()); }
@Test public void testPaddedStartStopKey() throws SQLException { String tenantId = "000000000000001"; String keyPrefix = "fo"; String query = "select * from atable where organization_id=? AND entity_id=?"; List<Object> binds = Arrays.<Object>asList(tenantId,keyPrefix); PhoenixConnection pconn = DriverManager.getConnection(getUrl(), TEST_PROPERTIES).unwrap(PhoenixConnection.class); PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query); bindParams(pstmt, binds); QueryPlan plan = pstmt.optimizeQuery(); Scan scan = plan.getContext().getScan(); assertArrayEquals(ByteUtil.concat(Bytes.toBytes(tenantId), StringUtil.padChar(Bytes.toBytes(keyPrefix), 15)),scan.getStartRow()); assertArrayEquals(ByteUtil.nextKey(scan.getStartRow()),scan.getStopRow()); }
@Test public void testOverlappingKeyExpression() throws SQLException { String tenantId = "000000000000001"; String keyPrefix = "002"; String entityId = "002333333333333"; String query = "select * from atable where organization_id='" + tenantId + "' and substr(entity_id,1,3)='" + keyPrefix + "' and entity_id='" + entityId + "'"; Scan scan = new Scan(); List<Object> binds = Collections.emptyList(); compileStatement(query, scan, binds); byte[] startRow = ByteUtil.concat(PDataType.VARCHAR.toBytes(tenantId),PDataType.VARCHAR.toBytes(entityId)); assertArrayEquals(startRow, scan.getStartRow()); assertArrayEquals(ByteUtil.nextKey(startRow), scan.getStopRow()); }
@Test public void testKeyRangeExpression1() throws SQLException { String tenantId = "000000000000001"; String keyPrefix1 = "002"; String keyPrefix2= "004"; String query = "select * from atable where organization_id='" + tenantId + "' and substr(entity_id,1,3) >= '" + keyPrefix1 + "' and substr(entity_id,1,3) < '" + keyPrefix2 + "'"; Scan scan = new Scan(); List<Object> binds = Collections.emptyList(); compileStatement(query, scan, binds); assertNull(scan.getFilter()); byte[] startRow = ByteUtil.concat(PDataType.VARCHAR.toBytes(tenantId),ByteUtil.fillKey(PDataType.VARCHAR.toBytes(keyPrefix1),15)); assertArrayEquals(startRow, scan.getStartRow()); byte[] stopRow = ByteUtil.concat(PDataType.VARCHAR.toBytes(tenantId),ByteUtil.fillKey(PDataType.VARCHAR.toBytes(keyPrefix2),15)); assertArrayEquals(stopRow, scan.getStopRow()); }
@Test public void testBasicRangeExpression() throws SQLException { String tenantId = "000000000000001"; String query = "select * from atable where organization_id <= '" + tenantId + "'"; Scan scan = new Scan(); List<Object> binds = Collections.emptyList(); compileStatement(query, scan, binds); assertNull(scan.getFilter()); assertTrue(scan.getStartRow().length == 0); byte[] stopRow = ByteUtil.concat(ByteUtil.nextKey(PDataType.VARCHAR.toBytes(tenantId))); assertArrayEquals(stopRow, scan.getStopRow()); }
@Test public void testMultiKeyExpression() throws SQLException { String tenantId = "000000000000001"; String keyPrefix = "002"; String query = "select * from atable where organization_id='" + tenantId + "' and substr(entity_id,1,3)='" + keyPrefix + "'"; Scan scan = new Scan(); List<Object> binds = Collections.emptyList(); compileStatement(query, scan, binds); assertNull(scan.getFilter()); byte[] startRow = ByteUtil.concat(PDataType.VARCHAR.toBytes(tenantId),ByteUtil.fillKey(PDataType.VARCHAR.toBytes(keyPrefix),15)); assertArrayEquals(startRow, scan.getStartRow()); byte[] stopRow = ByteUtil.concat(PDataType.VARCHAR.toBytes(tenantId),ByteUtil.fillKey(ByteUtil.nextKey(PDataType.VARCHAR.toBytes(keyPrefix)), 15)); assertArrayEquals(stopRow, scan.getStopRow()); }
@Test public void testLikeExtractAllAsEqKeyExpression() throws SQLException { String tenantId = "000000000000001"; String keyPrefix = "002"; String query = "select * from atable where organization_id LIKE ? and entity_id LIKE '" + keyPrefix + "%'"; Scan scan = new Scan(); List<Object> binds = Arrays.<Object>asList(tenantId); compileStatement(query, scan, binds); assertNull(scan.getFilter()); byte[] startRow = ByteUtil.concat(PDataType.VARCHAR.toBytes(tenantId),ByteUtil.fillKey(PDataType.VARCHAR.toBytes(keyPrefix),15)); assertArrayEquals(startRow, scan.getStartRow()); byte[] stopRow = ByteUtil.concat(PDataType.VARCHAR.toBytes(tenantId),ByteUtil.fillKey(ByteUtil.nextKey(PDataType.VARCHAR.toBytes(keyPrefix)),15)); assertArrayEquals(stopRow, scan.getStopRow()); }