@Override public byte[] toBytes(Object object) { return PDate.INSTANCE.toBytes(object); }
@Override public int toBytes(Object object, byte[] bytes, int offset) { return PDate.INSTANCE.toBytes(object, bytes, offset); }
@Override public byte[] toBytes(Object object) { byte[] bytes = new byte[getByteSize()]; toBytes(object, bytes, 0); return bytes; }
private Put getLastStatsUpdatedTimePut(long timeStamp) { long currentTime = EnvironmentEdgeManager.currentTimeMillis(); byte[] prefix = tableName; Put put = new Put(prefix); put.addColumn(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES, PhoenixDatabaseMetaData.LAST_STATS_UPDATE_TIME_BYTES, timeStamp, PDate.INSTANCE.toBytes(new Date(currentTime))); return put; }
@Test public void testNullInMiddleOfRVC() throws SQLException { String tenantId = "000000000000001"; String parentId = null; Date createdDate = new Date(System.currentTimeMillis()); String query = "select * from entity_history where (organization_id, parent_id, created_date) >= (?,?,?)"; List<Object> binds = Arrays.<Object>asList(tenantId, parentId, createdDate); StatementContext context = compileStatement(query, binds); Scan scan = context.getScan(); Filter filter = scan.getFilter(); assertNull(filter); byte[] expectedStartRow = ByteUtil.concat(PChar.INSTANCE.toBytes(tenantId), new byte[15], ByteUtil.previousKey( PDate.INSTANCE.toBytes(createdDate))); assertArrayEquals(expectedStartRow, scan.getStartRow()); assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow()); }
@Test public void testNullAtStartOfRVC() throws SQLException { String tenantId = null; String parentId = "000000000000002"; Date createdDate = new Date(System.currentTimeMillis()); String query = "select * from entity_history where (organization_id, parent_id, created_date) >= (?,?,?)"; List<Object> binds = Arrays.<Object>asList(tenantId, parentId, createdDate); StatementContext context = compileStatement(query, binds); Scan scan = context.getScan(); Filter filter = scan.getFilter(); assertNull(filter); byte[] expectedStartRow = ByteUtil.concat(new byte[15], ByteUtil.previousKey(PChar.INSTANCE.toBytes(parentId)), PDate.INSTANCE.toBytes(createdDate)); assertArrayEquals(expectedStartRow, scan.getStartRow()); assertArrayEquals(HConstants.EMPTY_END_ROW, scan.getStopRow()); }
@Test public void testRVCInCombinationWithOtherNonRVC() throws SQLException { String firstOrgId = "000000000000001"; String secondOrgId = "000000000000008"; String parentId = "000000000000002"; Date createdDate = new Date(System.currentTimeMillis()); String query = "select * from entity_history where (organization_id, parent_id, created_date) >= (?,?,?) AND organization_id <= ?"; List<Object> binds = Arrays.<Object>asList(firstOrgId, parentId, createdDate, secondOrgId); StatementContext context = compileStatement(query, binds); Scan scan = context.getScan(); Filter filter = scan.getFilter(); assertNull(filter); assertArrayEquals(ByteUtil.concat(PVarchar.INSTANCE.toBytes(firstOrgId), PVarchar.INSTANCE.toBytes(parentId), PDate.INSTANCE.toBytes(createdDate)), scan.getStartRow()); assertArrayEquals(ByteUtil.nextKey(PVarchar.INSTANCE.toBytes(secondOrgId)), scan.getStopRow()); }
@Test public void testEqualRound() throws Exception { String inst = "a"; String host = "b"; Date startDate = DateUtil.parseDate("2012-01-01 00:00:00"); Date endDate = DateUtil.parseDate("2012-01-02 00:00:00"); String query = "select * from ptsdb where inst=? and host=? and round(date,'DAY')=?"; List<Object> binds = Arrays.<Object>asList(inst,host,startDate); Scan scan = compileStatement(query, binds).getScan(); assertNull(scan.getFilter()); byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY, PDate.INSTANCE.toBytes(startDate)); assertArrayEquals(startRow, scan.getStartRow()); byte[] stopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY, PDate.INSTANCE.toBytes(endDate)); assertArrayEquals(stopRow, scan.getStopRow()); }
@Test public void testMultiRVCExpressionsCombinedUsingLiteralExpressions() throws SQLException { String lowerTenantId = "000000000000001"; String lowerParentId = "000000000000002"; Date lowerCreatedDate = new Date(System.currentTimeMillis()); String query = "select * from entity_history where (organization_id, parent_id, created_date) >= (?, ?, ?) AND (organization_id, parent_id) <= ('7', '7')"; List<Object> binds = Arrays.<Object>asList(lowerTenantId, lowerParentId, lowerCreatedDate); StatementContext context = compileStatement(query, binds); Scan scan = context.getScan(); Filter filter = scan.getFilter(); assertNull(filter); byte[] expectedStartRow = ByteUtil.concat( PVarchar.INSTANCE.toBytes(lowerTenantId), PVarchar.INSTANCE.toBytes(lowerParentId), PDate.INSTANCE.toBytes(lowerCreatedDate)); byte[] expectedStopRow = ByteUtil.nextKey(ByteUtil.concat(StringUtil.padChar(PVarchar.INSTANCE.toBytes("7"),15), StringUtil.padChar( PVarchar.INSTANCE.toBytes("7"), 15))); assertArrayEquals(expectedStartRow, scan.getStartRow()); assertArrayEquals(expectedStopRow, scan.getStopRow()); }
@Test public void testTimestampToDateComparison() { long now = System.currentTimeMillis(); Timestamp ts1 = DateUtil.getTimestamp(now, 1111); final byte[] bytes1 = PTimestamp.INSTANCE.toBytes(ts1); Date ts2 = new Date(now); final byte[] bytes2 = PDate.INSTANCE.toBytes(ts2); assertTrue(PTimestamp.INSTANCE.compareTo(bytes1, 0, bytes1.length, SortOrder.getDefault(), bytes2, 0, bytes2.length, SortOrder.getDefault(), PDate.INSTANCE) > 0); Timestamp ts3 = DateUtil.getTimestamp(now, 0); final byte[] bytes3 = PTimestamp.INSTANCE.toBytes(ts3); assertTrue(PTimestamp.INSTANCE.compareTo(bytes3, 0, bytes3.length, SortOrder.getDefault(), bytes2, 0, bytes2.length, SortOrder.getDefault(), PDate.INSTANCE) == 0); Timestamp ts4 = DateUtil.getTimestamp(now, 0); final byte[] bytes4 = PUnsignedTimestamp.INSTANCE.toBytes(ts4); assertTrue(PUnsignedTimestamp.INSTANCE.compareTo(bytes4, 0, bytes4.length, SortOrder.getDefault(), bytes2, 0, bytes2.length, SortOrder.getDefault(), PDate.INSTANCE) == 0); }
@Test public void testBoundaryLessThanOrEqualRound() throws Exception { String inst = "a"; String host = "b"; Date startDate = DateUtil.parseDate("2012-01-01 00:00:00"); Date endDate = DateUtil.parseDate("2012-01-02 00:00:00"); String query = "select * from ptsdb where inst=? and host=? and round(date,'DAY')<=?"; List<Object> binds = Arrays.<Object>asList(inst,host,startDate); Scan scan = compileStatement(query, binds).getScan(); assertNull(scan.getFilter()); byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host)/*,QueryConstants.SEPARATOR_BYTE_ARRAY*/); assertArrayEquals(startRow, scan.getStartRow()); byte[] stopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY, PDate.INSTANCE.toBytes(endDate)); assertArrayEquals(stopRow, scan.getStopRow()); }
@Test public void testBoundaryLessThanRound() throws Exception { String inst = "a"; String host = "b"; Date startDate = DateUtil.parseDate("2012-01-01 00:00:00"); Date endDate = DateUtil.parseDate("2012-01-01 00:00:00"); String query = "select * from ptsdb where inst=? and host=? and round(date,'DAY')<?"; List<Object> binds = Arrays.<Object>asList(inst,host,startDate); Scan scan = compileStatement(query, binds).getScan(); assertNull(scan.getFilter()); byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host)/*,QueryConstants.SEPARATOR_BYTE_ARRAY*/); assertArrayEquals(startRow, scan.getStartRow()); byte[] stopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY, PDate.INSTANCE.toBytes(endDate)); assertArrayEquals(stopRow, scan.getStopRow()); }
@Test public void testLessThanRound() throws Exception { String inst = "a"; String host = "b"; Date startDate = DateUtil.parseDate("2012-01-01 01:00:00"); Date endDate = DateUtil.parseDate("2012-01-02 00:00:00"); String query = "select * from ptsdb where inst=? and host=? and round(date,'DAY')<?"; List<Object> binds = Arrays.<Object>asList(inst,host,startDate); Scan scan = compileStatement(query, binds).getScan(); assertNull(scan.getFilter()); byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host)/*,QueryConstants.SEPARATOR_BYTE_ARRAY*/); assertArrayEquals(startRow, scan.getStartRow()); byte[] stopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY, PDate.INSTANCE.toBytes(endDate)); assertArrayEquals(stopRow, scan.getStopRow()); }
@Test public void testBoundaryGreaterThanRound() throws Exception { String inst = "a"; String host = "b"; Date startDate = DateUtil.parseDate("2012-01-01 00:00:00"); Date endDate = DateUtil.parseDate("2012-01-02 00:00:00"); String query = "select * from ptsdb where inst=? and host=? and round(date,'DAY')>?"; List<Object> binds = Arrays.<Object>asList(inst,host,startDate); Scan scan = compileStatement(query, binds).getScan(); assertNull(scan.getFilter()); byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY, PDate.INSTANCE.toBytes(endDate)); assertArrayEquals(startRow, scan.getStartRow()); byte[] stopRow = ByteUtil.nextKey(ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY)); assertArrayEquals(stopRow, scan.getStopRow()); }
@Test public void testLessThanOrEqualRound() throws Exception { String inst = "a"; String host = "b"; Date startDate = DateUtil.parseDate("2012-01-01 01:00:00"); Date endDate = DateUtil.parseDate("2012-01-02 00:00:00"); String query = "select * from ptsdb where inst=? and host=? and round(date,'DAY')<=?"; List<Object> binds = Arrays.<Object>asList(inst,host,startDate); Scan scan = compileStatement(query, binds).getScan(); assertNull(scan.getFilter()); byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host)/*,QueryConstants.SEPARATOR_BYTE_ARRAY*/); assertArrayEquals(startRow, scan.getStartRow()); byte[] stopRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY, PDate.INSTANCE.toBytes(endDate)); assertArrayEquals(stopRow, scan.getStopRow()); }
@Test public void testBoundaryGreaterThanOrEqualRound() throws Exception { String inst = "a"; String host = "b"; Date startDate = DateUtil.parseDate("2012-01-01 00:00:00"); Date endDate = DateUtil.parseDate("2012-01-01 00:00:00"); String query = "select * from ptsdb where inst=? and host=? and round(date,'DAY')>=?"; List<Object> binds = Arrays.<Object>asList(inst,host,startDate); Scan scan = compileStatement(query, binds).getScan(); assertNull(scan.getFilter()); byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY, PDate.INSTANCE.toBytes(endDate)); assertArrayEquals(startRow, scan.getStartRow()); byte[] stopRow = ByteUtil.nextKey(ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY)); assertArrayEquals(stopRow, scan.getStopRow()); }
@Test public void testGreaterThanRound() throws Exception { String inst = "a"; String host = "b"; Date startDate = DateUtil.parseDate("2012-01-01 01:00:00"); Date endDate = DateUtil.parseDate("2012-01-02 00:00:00"); String query = "select * from ptsdb where inst=? and host=? and round(date,'DAY')>?"; List<Object> binds = Arrays.<Object>asList(inst,host,startDate); Scan scan = compileStatement(query, binds).getScan(); assertNull(scan.getFilter()); byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY, PDate.INSTANCE.toBytes(endDate)); assertArrayEquals(startRow, scan.getStartRow()); byte[] stopRow = ByteUtil.nextKey(ByteUtil.concat(PVarchar.INSTANCE.toBytes(inst),QueryConstants.SEPARATOR_BYTE_ARRAY, PVarchar.INSTANCE.toBytes(host),QueryConstants.SEPARATOR_BYTE_ARRAY)); assertArrayEquals(stopRow, scan.getStopRow()); }