use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class RegexpSubstrFunction method init.
private void init() {
ImmutableBytesWritable ptr = new ImmutableBytesWritable();
Expression patternExpr = getPatternExpression();
if (patternExpr.isStateless() && patternExpr.getDeterminism() == Determinism.ALWAYS && patternExpr.evaluate(null, ptr)) {
String patternStr = (String) patternExpr.getDataType().toObject(ptr, patternExpr.getSortOrder());
if (patternStr != null) {
pattern = compilePatternSpec(patternStr);
}
}
// If the source string has a fixed width, then the max length would be the length
// of the source string minus the offset, or the absolute value of the offset if
// it's negative. Offset number is a required argument. However, if the source string
// is not fixed width, the maxLength would be null.
Expression offsetExpr = getOffsetExpression();
if (offsetExpr.isStateless() && offsetExpr.getDeterminism() == Determinism.ALWAYS && offsetExpr.evaluate(null, ptr)) {
offset = (Integer) PInteger.INSTANCE.toObject(ptr, offsetExpr.getDataType(), offsetExpr.getSortOrder());
if (offset != null) {
PDataType type = getSourceStrExpression().getDataType();
if (type.isFixedWidth()) {
if (offset >= 0) {
Integer maxLength = getSourceStrExpression().getMaxLength();
this.maxLength = maxLength - offset - (offset == 0 ? 0 : 1);
} else {
this.maxLength = -offset;
}
}
}
}
}
use of org.apache.phoenix.expression.Expression in project phoenix by apache.
the class SubstrFunction method evaluate.
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
Expression offsetExpression = getOffsetExpression();
if (!offsetExpression.evaluate(tuple, ptr)) {
return false;
}
int offset = offsetExpression.getDataType().getCodec().decodeInt(ptr, offsetExpression.getSortOrder());
int length = -1;
if (hasLengthExpression) {
Expression lengthExpression = getLengthExpression();
if (!lengthExpression.evaluate(tuple, ptr)) {
return false;
}
length = lengthExpression.getDataType().getCodec().decodeInt(ptr, lengthExpression.getSortOrder());
if (length <= 0) {
return false;
}
}
if (!getStrExpression().evaluate(tuple, ptr)) {
return false;
}
boolean isCharType = getStrExpression().getDataType() == PChar.INSTANCE;
SortOrder sortOrder = getStrExpression().getSortOrder();
int strlen = isCharType ? ptr.getLength() : StringUtil.calculateUTF8Length(ptr.get(), ptr.getOffset(), ptr.getLength(), sortOrder);
// Account for 1 versus 0-based offset
offset = offset - (offset <= 0 ? 0 : 1);
if (offset < 0) {
// Offset < 0 means get from end
offset = strlen + offset;
}
if (offset < 0 || offset >= strlen) {
return false;
}
int maxLength = strlen - offset;
length = length == -1 ? maxLength : Math.min(length, maxLength);
int byteOffset = isCharType ? offset : StringUtil.getByteLengthForUtf8SubStr(ptr.get(), ptr.getOffset(), offset, sortOrder);
int byteLength = isCharType ? length : StringUtil.getByteLengthForUtf8SubStr(ptr.get(), ptr.getOffset() + byteOffset, length, sortOrder);
ptr.set(ptr.get(), ptr.getOffset() + byteOffset, byteLength);
return true;
}
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;
}
Aggregations