use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class PrefixFunction method newKeyPart.
@Override
public KeyPart newKeyPart(final KeyPart childPart) {
return new KeyPart() {
private final List<Expression> extractNodes = extractNode() ? Collections.<Expression>singletonList(PrefixFunction.this) : Collections.<Expression>emptyList();
@Override
public PColumn getColumn() {
return childPart.getColumn();
}
@Override
public List<Expression> getExtractNodes() {
return extractNodes;
}
@Override
public KeyRange getKeyRange(CompareOp op, Expression rhs) {
byte[] lowerRange = KeyRange.UNBOUND;
byte[] upperRange = KeyRange.UNBOUND;
boolean lowerInclusive = true;
PDataType type = getColumn().getDataType();
switch(op) {
case EQUAL:
lowerRange = evaluateExpression(rhs);
upperRange = ByteUtil.nextKey(lowerRange);
break;
case GREATER:
lowerRange = ByteUtil.nextKey(evaluateExpression(rhs));
break;
case LESS_OR_EQUAL:
upperRange = ByteUtil.nextKey(evaluateExpression(rhs));
lowerInclusive = false;
break;
default:
return childPart.getKeyRange(op, rhs);
}
PColumn column = getColumn();
Integer length = column.getMaxLength();
if (type.isFixedWidth()) {
if (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);
}
}
} else if (column.getSortOrder() == SortOrder.DESC && getTable().rowKeyOrderOptimizable()) {
// a lowerRange of 'a\xFF' would skip 'ab', while 'a\x00\xFF' would not.
if (lowerRange != KeyRange.UNBOUND) {
lowerRange = Arrays.copyOf(lowerRange, lowerRange.length + 1);
lowerRange[lowerRange.length - 1] = QueryConstants.SEPARATOR_BYTE;
}
}
return KeyRange.getKeyRange(lowerRange, lowerInclusive, upperRange, false);
}
@Override
public PTable getTable() {
return childPart.getTable();
}
};
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class ReverseFunction method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
Expression arg = getChildren().get(0);
if (!arg.evaluate(tuple, ptr)) {
return false;
}
int targetOffset = ptr.getLength();
if (targetOffset == 0) {
return true;
}
byte[] source = ptr.get();
byte[] target = new byte[targetOffset];
int sourceOffset = ptr.getOffset();
int endOffset = sourceOffset + ptr.getLength();
SortOrder sortOrder = arg.getSortOrder();
while (sourceOffset < endOffset) {
int nBytes = StringUtil.getBytesInChar(source[sourceOffset], sortOrder);
targetOffset -= nBytes;
System.arraycopy(source, sourceOffset, target, targetOffset, nBytes);
sourceOffset += nBytes;
}
ptr.set(target);
return true;
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class RoundDateExpression method newKeyPart.
/**
* Form the key range from the key to the key right before or at the
* next rounded value.
*/
@Override
public KeyPart newKeyPart(final KeyPart childPart) {
return new KeyPart() {
private final List<Expression> extractNodes = Collections.<Expression>singletonList(RoundDateExpression.this);
@Override
public PColumn getColumn() {
return childPart.getColumn();
}
@Override
public List<Expression> getExtractNodes() {
return extractNodes;
}
@Override
public KeyRange getKeyRange(CompareOp op, Expression rhs) {
PDataType type = getColumn().getDataType();
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
rhs.evaluate(null, ptr);
byte[] key = ByteUtil.copyKeyBytesIfNecessary(ptr);
// No need to take into account SortOrder, because ROUND
// always forces the value to be in ascending order
PDataCodec codec = getKeyRangeCodec(type);
int offset = ByteUtil.isInclusive(op) ? 1 : 0;
long value = codec.decodeLong(key, 0, SortOrder.getDefault());
byte[] nextKey = new byte[type.getByteSize()];
switch(op) {
case EQUAL:
// boundary.
if (value % divBy != 0) {
return KeyRange.EMPTY_RANGE;
}
codec.encodeLong(value + divBy, nextKey, 0);
return type.getKeyRange(key, true, nextKey, false);
case GREATER:
case GREATER_OR_EQUAL:
codec.encodeLong((value + divBy - offset) / divBy * divBy, nextKey, 0);
return type.getKeyRange(nextKey, true, KeyRange.UNBOUND, false);
case LESS:
case LESS_OR_EQUAL:
codec.encodeLong((value + divBy - (1 - offset)) / divBy * divBy, nextKey, 0);
return type.getKeyRange(KeyRange.UNBOUND, false, nextKey, false);
default:
return childPart.getKeyRange(op, rhs);
}
}
@Override
public PTable getTable() {
return childPart.getTable();
}
};
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class RoundDecimalExpression method create.
/**
* Creates a {@link RoundDecimalExpression} with rounding scale given by @param scale.
*
*/
public static Expression create(Expression expr, int scale) throws SQLException {
if (expr.getDataType().isCoercibleTo(PLong.INSTANCE)) {
return expr;
}
Expression scaleExpr = LiteralExpression.newConstant(scale, PInteger.INSTANCE, Determinism.ALWAYS);
List<Expression> expressions = Lists.newArrayList(expr, scaleExpr);
return new RoundDecimalExpression(expressions);
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class RegexpSplitFunction method init.
private void init() {
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
Expression e = getPatternStrExpression();
if (e.isStateless() && e.getDeterminism() == Determinism.ALWAYS && e.evaluate(null, ptr)) {
String pattern = (String) TYPE.toObject(ptr, TYPE, e.getSortOrder());
if (pattern != null) {
initializedSplitter = compilePatternSpec(pattern);
}
}
}
Aggregations