Search in sources :

Example 71 with Expression

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;
                }
            }
        }
    }
}
Also used : PInteger(org.apache.phoenix.schema.types.PInteger) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression)

Example 72 with Expression

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;
}
Also used : LiteralExpression(org.apache.phoenix.expression.LiteralExpression) Expression(org.apache.phoenix.expression.Expression) SortOrder(org.apache.phoenix.schema.SortOrder)

Example 73 with Expression

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;
}
Also used : Expression(org.apache.phoenix.expression.Expression) IllegalDataException(org.apache.phoenix.schema.IllegalDataException)

Example 74 with Expression

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;
}
Also used : Expression(org.apache.phoenix.expression.Expression) IndexType(org.apache.phoenix.schema.PTable.IndexType)

Example 75 with Expression

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;
}
Also used : Expression(org.apache.phoenix.expression.Expression) ViewType(org.apache.phoenix.schema.PTable.ViewType)

Aggregations

Expression (org.apache.phoenix.expression.Expression)182 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)101 PDataType (org.apache.phoenix.schema.types.PDataType)54 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)39 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)37 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)33 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)30 PTable (org.apache.phoenix.schema.PTable)25 ParseNode (org.apache.phoenix.parse.ParseNode)23 RowValueConstructorExpression (org.apache.phoenix.expression.RowValueConstructorExpression)22 Test (org.junit.Test)22 ComparisonExpression (org.apache.phoenix.expression.ComparisonExpression)21 AndExpression (org.apache.phoenix.expression.AndExpression)20 SingleCellColumnExpression (org.apache.phoenix.expression.SingleCellColumnExpression)20 PColumn (org.apache.phoenix.schema.PColumn)20 ArrayList (java.util.ArrayList)19 InListExpression (org.apache.phoenix.expression.InListExpression)17 IsNullExpression (org.apache.phoenix.expression.IsNullExpression)16 OrExpression (org.apache.phoenix.expression.OrExpression)16 LikeExpression (org.apache.phoenix.expression.LikeExpression)15