use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class SqlTypeNameFunction method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
Expression child = children.get(0);
if (!child.evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
return true;
}
int sqlType = child.getDataType().getCodec().decodeInt(ptr, child.getSortOrder());
try {
byte[] sqlTypeNameBytes = PDataType.fromTypeId(sqlType).getSqlTypeNameBytes();
ptr.set(sqlTypeNameBytes);
} catch (IllegalDataException e) {
ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
}
return true;
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class SQLIndexTypeFunction method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
Expression child = children.get(0);
if (!child.evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
return true;
}
IndexType viewType = IndexType.fromSerializedValue(ptr.get()[ptr.getOffset()]);
ptr.set(viewType.getBytes());
return true;
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class SQLViewTypeFunction method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
Expression child = children.get(0);
if (!child.evaluate(tuple, ptr)) {
return false;
}
if (ptr.getLength() == 0) {
return true;
}
ViewType viewType = ViewType.fromSerializedValue(ptr.get()[ptr.getOffset()]);
ptr.set(viewType.getBytes());
return true;
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class RTrimFunction method newKeyPart.
@Override
public KeyPart newKeyPart(final KeyPart childPart) {
return new KeyPart() {
@Override
public KeyRange getKeyRange(CompareOp op, Expression rhs) {
byte[] lowerRange = KeyRange.UNBOUND;
byte[] upperRange = KeyRange.UNBOUND;
boolean lowerInclusive = true;
boolean upperInclusive = false;
PDataType type = getColumn().getDataType();
SortOrder sortOrder = getColumn().getSortOrder();
switch(op) {
case LESS_OR_EQUAL:
lowerInclusive = false;
case EQUAL:
upperRange = evaluateExpression(rhs);
if (op == CompareOp.EQUAL) {
lowerRange = upperRange;
}
if (sortOrder == SortOrder.ASC || !getTable().rowKeyOrderOptimizable()) {
upperRange = Arrays.copyOf(upperRange, upperRange.length + 1);
upperRange[upperRange.length - 1] = StringUtil.SPACE_UTF8;
ByteUtil.nextKey(upperRange, upperRange.length);
} else {
upperInclusive = true;
if (op == CompareOp.LESS_OR_EQUAL) {
// will be the RHS value.
break;
}
/*
* Somewhat tricky to get the range correct for the DESC equality case.
* The lower range is the RHS value followed by any number of inverted spaces.
* We need to add a zero byte as the lower range will have an \xFF byte
* appended to it and otherwise we'd skip past any rows where there is more
* than one space following the RHS.
* The upper range should span up to and including the RHS value. We need
* to add our own \xFF as otherwise this will look like a degenerate query
* since the lower would be bigger than the upper range.
*/
lowerRange = Arrays.copyOf(lowerRange, lowerRange.length + 2);
lowerRange[lowerRange.length - 2] = StringUtil.INVERTED_SPACE_UTF8;
lowerRange[lowerRange.length - 1] = QueryConstants.SEPARATOR_BYTE;
upperRange = Arrays.copyOf(upperRange, upperRange.length + 1);
upperRange[upperRange.length - 1] = QueryConstants.DESC_SEPARATOR_BYTE;
}
break;
default:
// TOOD: Is this ok for DESC?
return childPart.getKeyRange(op, rhs);
}
Integer length = getColumn().getMaxLength();
if (type.isFixedWidth() && length != null) {
// *after* rows with no padding.
if (lowerRange != KeyRange.UNBOUND) {
lowerRange = type.pad(lowerRange, length, SortOrder.ASC);
}
if (upperRange != KeyRange.UNBOUND) {
upperRange = type.pad(upperRange, length, SortOrder.ASC);
}
}
return KeyRange.getKeyRange(lowerRange, lowerInclusive, upperRange, upperInclusive);
}
@Override
public List<Expression> getExtractNodes() {
// non blank characters such as 'foo bar' where the RHS constant is 'foo'.
return Collections.<Expression>emptyList();
}
@Override
public PColumn getColumn() {
return childPart.getColumn();
}
@Override
public PTable getTable() {
return childPart.getTable();
}
};
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class SetBitFunction method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
// get offset parameter
Expression offsetExpr = children.get(1);
if (!offsetExpr.evaluate(tuple, ptr))
return false;
int offset = (Integer) PInteger.INSTANCE.toObject(ptr, offsetExpr.getSortOrder());
// get newValue parameter
Expression newValueExpr = children.get(2);
if (!newValueExpr.evaluate(tuple, ptr))
return false;
int newValue = (Integer) PInteger.INSTANCE.toObject(ptr, newValueExpr.getSortOrder());
byte newByteValue = (byte) (newValue & 0x1);
// get binary data parameter
Expression dataExpr = children.get(0);
if (!dataExpr.evaluate(tuple, ptr))
return false;
if (ptr.getLength() == 0)
return true;
int len = ptr.getLength() * Byte.SIZE;
offset = (offset % len + len) % len;
// set result
((PBinaryBase) dataExpr.getDataType()).setBit(ptr, dataExpr.getSortOrder(), offset, newByteValue, ptr);
return true;
}
Aggregations