Search in sources :

Example 1 with AbstractBasePattern

use of org.apache.phoenix.expression.util.regex.AbstractBasePattern in project phoenix by apache.

the class LikeExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    AbstractBasePattern pattern = this.pattern;
    if (pattern == null) {
        // TODO: don't allow? this is going to be slooowwww
        if (!getPatternExpression().evaluate(tuple, ptr)) {
            if (logger.isTraceEnabled()) {
                logger.trace("LIKE is FALSE: pattern is null");
            }
            return false;
        }
        String value = (String) PVarchar.INSTANCE.toObject(ptr, getPatternExpression().getSortOrder());
        pattern = compilePattern(value);
        if (logger.isTraceEnabled()) {
            logger.trace("LIKE pattern is expression: " + pattern.pattern());
        }
    }
    Expression strExpression = getStrExpression();
    SortOrder strSortOrder = strExpression.getSortOrder();
    PVarchar strDataType = PVarchar.INSTANCE;
    if (!strExpression.evaluate(tuple, ptr)) {
        if (logger.isTraceEnabled()) {
            logger.trace("LIKE is FALSE: child expression is null");
        }
        return false;
    }
    String value = null;
    if (logger.isTraceEnabled()) {
        value = (String) strDataType.toObject(ptr, strSortOrder);
    }
    strDataType.coerceBytes(ptr, strDataType, strSortOrder, SortOrder.ASC);
    pattern.matches(ptr);
    if (logger.isTraceEnabled()) {
        boolean matched = ((Boolean) PBoolean.INSTANCE.toObject(ptr)).booleanValue();
        logger.trace("LIKE(value='" + value + "'pattern='" + pattern.pattern() + "' is " + matched);
    }
    return true;
}
Also used : AbstractBasePattern(org.apache.phoenix.expression.util.regex.AbstractBasePattern) SortOrder(org.apache.phoenix.schema.SortOrder) PVarchar(org.apache.phoenix.schema.types.PVarchar) PBoolean(org.apache.phoenix.schema.types.PBoolean)

Example 2 with AbstractBasePattern

use of org.apache.phoenix.expression.util.regex.AbstractBasePattern in project phoenix by apache.

the class RegexpSubstrFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    AbstractBasePattern pattern = this.pattern;
    if (pattern == null) {
        Expression patternExpr = getPatternExpression();
        if (!patternExpr.evaluate(tuple, ptr)) {
            return false;
        }
        if (ptr.getLength() == 0) {
            return true;
        }
        pattern = compilePatternSpec((String) patternExpr.getDataType().toObject(ptr, patternExpr.getSortOrder()));
    }
    int offset;
    if (this.offset == null) {
        Expression offsetExpression = getOffsetExpression();
        if (!offsetExpression.evaluate(tuple, ptr)) {
            return false;
        }
        if (ptr.getLength() == 0) {
            return true;
        }
        offset = offsetExpression.getDataType().getCodec().decodeInt(ptr, offsetExpression.getSortOrder());
    } else {
        offset = this.offset;
    }
    Expression strExpression = getSourceStrExpression();
    if (!strExpression.evaluate(tuple, ptr)) {
        return false;
    }
    if (ptr.get().length == 0) {
        return true;
    }
    TYPE.coerceBytes(ptr, strExpression.getDataType(), strExpression.getSortOrder(), SortOrder.ASC);
    // Account for 1 versus 0-based offset
    offset = offset - (offset <= 0 ? 0 : 1);
    pattern.substr(ptr, offset);
    return true;
}
Also used : Expression(org.apache.phoenix.expression.Expression) AbstractBasePattern(org.apache.phoenix.expression.util.regex.AbstractBasePattern)

Example 3 with AbstractBasePattern

use of org.apache.phoenix.expression.util.regex.AbstractBasePattern in project phoenix by apache.

the class RegexpReplaceFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    AbstractBasePattern pattern = this.pattern;
    if (pattern == null) {
        Expression e = getPatternStrExpression();
        if (!e.evaluate(tuple, ptr)) {
            return false;
        }
        String patternStr = (String) TYPE.toObject(ptr, e.getDataType(), e.getSortOrder());
        if (patternStr == null) {
            return false;
        } else {
            pattern = compilePatternSpec(patternStr);
        }
    }
    byte[] rStrBytes = this.rStrBytes;
    int rStrOffset = this.rStrOffset, rStrLen = this.rStrLen;
    if (rStrBytes == null) {
        Expression replaceStrExpression = getReplaceStrExpression();
        if (!replaceStrExpression.evaluate(tuple, ptr)) {
            return false;
        }
        TYPE.coerceBytes(ptr, TYPE, replaceStrExpression.getSortOrder(), SortOrder.ASC);
        rStrBytes = ptr.get();
        rStrOffset = ptr.getOffset();
        rStrLen = ptr.getLength();
    }
    Expression sourceStrExpression = getSourceStrExpression();
    if (!sourceStrExpression.evaluate(tuple, ptr)) {
        return false;
    }
    TYPE.coerceBytes(ptr, TYPE, sourceStrExpression.getSortOrder(), SortOrder.ASC);
    pattern.replaceAll(ptr, rStrBytes, rStrOffset, rStrLen);
    return true;
}
Also used : Expression(org.apache.phoenix.expression.Expression) AbstractBasePattern(org.apache.phoenix.expression.util.regex.AbstractBasePattern)

Aggregations

AbstractBasePattern (org.apache.phoenix.expression.util.regex.AbstractBasePattern)3 Expression (org.apache.phoenix.expression.Expression)2 SortOrder (org.apache.phoenix.schema.SortOrder)1 PBoolean (org.apache.phoenix.schema.types.PBoolean)1 PVarchar (org.apache.phoenix.schema.types.PVarchar)1