Java Code Examples for org.apache.hadoop.hbase.io.ImmutableBytesWritable#set()

The following examples show how to use org.apache.hadoop.hbase.io.ImmutableBytesWritable#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LTrimFunction.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    // Starting from the front of the byte, look for all single bytes at the end of the string
    // that is below SPACE_UTF8 (space and control characters) or 0x7f (control chars).
    if (!getStringExpression().evaluate(tuple, ptr)) {
        return false;
    }
    
    if (ptr.getLength() == 0) {
        ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
        return true;
    }
    byte[] string = ptr.get();
    int offset = ptr.getOffset();
    int length = ptr.getLength();
    
    SortOrder sortOrder = getStringExpression().getSortOrder();
    int i = StringUtil.getFirstNonBlankCharIdxFromStart(string, offset, length, sortOrder);
    if (i == offset + length) {
        ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
        return true;
    }
    
    ptr.set(string, i, offset + length - i);
    return true;
}
 
Example 2
Source File: ProjectionCompiler.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (!tuple.getValue(QueryConstants.ARRAY_VALUE_COLUMN_FAMILY, QueryConstants.ARRAY_VALUE_COLUMN_QUALIFIER,
            ptr)) { 
      return false;
    }
    int maxOffset = ptr.getOffset() + ptr.getLength();
    arrayIndexesBitSet.or(ptr);
    arrayIndexesSchema.iterator(ptr, position, arrayIndexesBitSet);
    Boolean hasValue = arrayIndexesSchema.next(ptr, position, maxOffset, arrayIndexesBitSet);
    arrayIndexesBitSet.clear();
    if (hasValue == null) {
        ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
    }
    return true;
}
 
Example 3
Source File: AvgAggregateFunction.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (!countFunc.evaluate(tuple, ptr)) {
        return false;
    }
    long count = countFunc.getDataType().getCodec().decodeLong(ptr, SortOrder.getDefault());
    if (count == 0) {
        return false;
    }
    
    // Normal case where a column reference was used as the argument to AVG
    if (!countFunc.isConstantExpression()) {
        sumFunc.evaluate(tuple, ptr);
        BigDecimal sum = (BigDecimal) PDecimal.INSTANCE.toObject(ptr, sumFunc.getDataType());
        // For the final column projection, we divide the sum by the count, both coerced to BigDecimal.
        // TODO: base the precision on column metadata instead of constant
        BigDecimal avg = sum.divide(BigDecimal.valueOf(count), PDataType.DEFAULT_MATH_CONTEXT);
        avg = avg.setScale(scale, BigDecimal.ROUND_DOWN);
        ptr.set(PDecimal.INSTANCE.toBytes(avg));
        return true;
    }
    BigDecimal value = (BigDecimal) ((LiteralExpression)countFunc.getChildren().get(0)).getValue();
    value = value.setScale(scale, BigDecimal.ROUND_DOWN);
    ptr.set(PDecimal.INSTANCE.toBytes(value));
    return true;
}
 
Example 4
Source File: LZFTest.java    From Kylin with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws IOException {

    byte[] raw = new byte[] { 1, 2, 3, 3, 2, 23 };
    byte[] data = LZFEncoder.encode(raw);

    byte[] data2 = new byte[data.length * 2];
    java.lang.System.arraycopy(data, 0, data2, 0, data.length);
    ImmutableBytesWritable bytes = new ImmutableBytesWritable();
    bytes.set(data2, 0, data.length);

    try {
        byte[] uncompressed = LZFDecoder.decode(bytes.get(), bytes.getOffset(), bytes.getLength());
    } catch (IOException e) {
        throw new RuntimeException("LZF decode failure", e);
    }
}
 
Example 5
Source File: LengthFunction.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression child = getStringExpression();
    if (!child.evaluate(tuple, ptr)) {
        return false;
    }
    if (ptr.getLength() == 0) {
        ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
        return true;
    }
    int len;
    if (child.getDataType() == PChar.INSTANCE) {
        // Only single-byte characters allowed in CHAR
        len = ptr.getLength();
    } else {
        try {
            len = StringUtil.calculateUTF8Length(ptr.get(), ptr.getOffset(), ptr.getLength(), child.getSortOrder());
        } catch (UndecodableByteException e) {
            return false;
        }
    }
    ptr.set(PInteger.INSTANCE.toBytes(len));
    return true;
}
 
Example 6
Source File: WeekFunction.java    From phoenix with Apache License 2.0 6 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression expression = getChildExpression();
    if (!expression.evaluate(tuple, ptr)) {
        return false;
    }
    if ( ptr.getLength() == 0) {
        return true; //means null
    }
    long dateTime = inputCodec.decodeLong(ptr, expression.getSortOrder());
    DateTime dt = new DateTime(dateTime);
    int week = dt.getWeekOfWeekyear();
    PDataType returnType = getDataType();
    byte[] byteValue = new byte[returnType.getByteSize()];
    returnType.getCodec().encodeInt(week, byteValue, 0);
    ptr.set(byteValue);
    return true;
}
 
Example 7
Source File: PercentRankClientAggregator.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (cachedResult == null) {
        ColumnExpression columnExp = (ColumnExpression)exps.get(0);
        // Second exp will be a LiteralExpression of Boolean type indicating whether the ordering to
        // be ASC/DESC
        LiteralExpression isAscendingExpression = (LiteralExpression)exps.get(1);
        boolean isAscending = (Boolean)isAscendingExpression.getValue();

        // Third expression will be LiteralExpression
        LiteralExpression valueExp = (LiteralExpression)exps.get(2);
        Map<Object, Integer> sorted = getSortedValueVsCount(isAscending, columnExp.getDataType());
        long distinctCountsSum = 0;
        Object value = valueExp.getValue();
        for (Entry<Object, Integer> entry : sorted.entrySet()) {
            Object colValue = entry.getKey();
            int compareResult = columnExp.getDataType().compareTo(colValue, value, valueExp.getDataType());
            boolean done = isAscending ? compareResult > 0 : compareResult <= 0;
            if (done) break;
            distinctCountsSum += entry.getValue();
        }

        float result = (float)distinctCountsSum / totalCount;
        this.cachedResult = new BigDecimal(result);
    }
    if (buffer == null) {
        initBuffer();
    }
    buffer = PDataType.DECIMAL.toBytes(this.cachedResult);
    ptr.set(buffer);
    return true;
}
 
Example 8
Source File: DistinctCountHyperLogLogAggregateFunction.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {	
	try {
		valueByteArray.set(hll.getBytes(), 0, hll.getBytes().length);
		ptr.set(ByteUtil.copyKeyBytesIfNecessary(valueByteArray));
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
	return true;
}
 
Example 9
Source File: NumberSumAggregator.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (buffer == null) {
        if (isNullable()) {
            return false;
        }
        initBuffer();
    }
    ptr.set(buffer);
    getDataType().getCodec().encodeLong(sum, ptr);
    return true;
}
 
Example 10
Source File: RowKeyColumnExpression.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    tuple.getKey(ptr);
    int offset = accessor.getOffset(ptr.get(), ptr.getOffset() + this.offset);
    // Null is represented in the last expression of a multi-part key 
    // by the bytes not being present.
    int maxOffset = ptr.getOffset() + ptr.getLength();
    if (offset < maxOffset) {
        byte[] buffer = ptr.get();
        int byteSize = -1;
        // FIXME: fixedByteSize <= maxByteSize ? fixedByteSize : 0 required because HBase passes bogus keys to filter to position scan (HBASE-6562)
        if (fromType.isFixedWidth()) {
            Integer maxLength = getMaxLength();
            byteSize = fromType.getByteSize() == null ? maxLength : fromType.getByteSize();
            byteSize = byteSize <= maxOffset ? byteSize : 0;
        }
        int length = byteSize >= 0 ? byteSize  : accessor.getLength(buffer, offset, maxOffset);
        // In the middle of the key, an empty variable length byte array represents null
        if (length > 0) {
            ptr.set(buffer,offset,length);
            type.coerceBytes(ptr, fromType, getSortOrder(), getSortOrder());
        } else {
            ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
        }
    } else {
        ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
    }
    // Always return true because we're always able to evaluate a row key column
    return true;
}
 
Example 11
Source File: InvertFunction.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (!getChildExpression().evaluate(tuple, ptr)) { return false; }
    if (ptr.getLength() == 0) { return true; }
    byte[] buf = new byte[ptr.getLength()];
    ColumnModifier.SORT_DESC.apply(ptr.get(), ptr.getOffset(), buf, 0, ptr.getLength());
    ptr.set(buf);
    return true;
}
 
Example 12
Source File: RandomFunction.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (current == null) {
        current = random.nextDouble();
    }
    ptr.set(PDouble.INSTANCE.toBytes(current));
    return true;
}
 
Example 13
Source File: SequenceValueExpression.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
		byte[] valueBuffer = new byte[PLong.INSTANCE.getByteSize()];
    PLong.INSTANCE.getCodec().encodeLong(tuple.getSequenceValue(index), valueBuffer, 0);
    ptr.set(valueBuffer);
    return true;
}
 
Example 14
Source File: LowerFunction.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (!getStrExpression().evaluate(tuple, ptr)) {
        return false;
    }

    String sourceStr = (String) PVarchar.INSTANCE.toObject(ptr, getStrExpression().getSortOrder());

    if (sourceStr == null) {
        return true;
    }

    ptr.set(PVarchar.INSTANCE.toBytes(sourceStr.toLowerCase()));
    return true;
}
 
Example 15
Source File: MinAggregator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (isNull()) {
        return false;
    }
    ptr.set(value.get(), value.getOffset(), value.getLength());
    return true;
}
 
Example 16
Source File: UnnestArrayPlan.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    ptr.set(arrayPtr.get(), arrayPtr.getOffset(), arrayPtr.getLength());
    PArrayDataTypeDecoder.positionAtArrayElement(ptr, index++, getDataType(), getMaxLength());
    return true;
}
 
Example 17
Source File: PositionBasedMultiKeyValueTuple.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public void getKey(ImmutableBytesWritable ptr) {
    Cell value = values.getFirstCell();
    ptr.set(value.getRowArray(), value.getRowOffset(), value.getRowLength());
}
 
Example 18
Source File: MultiKeyValueComparisonFilter.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public void getKey(ImmutableBytesWritable ptr) {
    ptr.set(keyPtr.get(),keyPtr.getOffset(),keyPtr.getLength());
}
 
Example 19
Source File: PercentileDiscClientAggregator.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
	// Reset buffer so that it gets initialized with the current datatype of the column
	buffer = null;
	if (cachedResult == null) {
		columnExp = (ColumnExpression)exps.get(0);
		// Second exp will be a LiteralExpression of Boolean type indicating
		// whether the ordering to be ASC/DESC
		LiteralExpression isAscendingExpression = (LiteralExpression) exps
				.get(1);
		boolean isAscending = (Boolean) isAscendingExpression.getValue();

		// Third expression will be LiteralExpression
		LiteralExpression percentileExp = (LiteralExpression) exps.get(2);
		float p = ((Number) percentileExp.getValue()).floatValue();
		Map<Object, Integer> sorted = getSortedValueVsCount(isAscending, columnExp.getDataType());
		int currValue = 0;
		Object result = null;
		// Here the Percentile_disc returns the cum_dist() that is greater or equal to the
		// Percentile (p) specified in the query.  So the result set will be of that of the
		// datatype of the column being selected
		for (Entry<Object, Integer> entry : sorted.entrySet()) {
			result = entry.getKey();
			Integer value = entry.getValue();
			currValue += value;
			float cum_dist = (float) currValue / (float) totalCount;
			if (cum_dist >= p) {
				break;
			}
		}
		this.cachedResult = result;
	}
	if (buffer == null) {
		// Initialize based on the datatype
		// columnExp cannot be null
		buffer = new byte[columnExp.getDataType().getByteSize()];
	}
	// Copy the result to the buffer.
	System.arraycopy(columnExp.getDataType().toBytes(this.cachedResult), 0, buffer, 0, buffer.length);
	ptr.set(buffer);
	return true;
}
 
Example 20
Source File: ResultUtil.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
public static void getKey(KeyValue value, ImmutableBytesWritable key) {
    key.set(value.getBuffer(), value.getRowOffset(), value.getRowLength());
}