Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Add REGEXP_LIKE buit-in function #733

Open
RamyaStec opened this issue Jul 9, 2014 · 2 comments
Open

Add REGEXP_LIKE buit-in function #733

RamyaStec opened this issue Jul 9, 2014 · 2 comments

Comments

@RamyaStec
Copy link

No description provided.

@RamyaStec
Copy link
Author

package org.apache.phoenix.expression.function;

import java.io.DataInput;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.hadoop.hbase.io.ImmutableBytesWritable;

import org.apache.phoenix.expression.Expression;
import org.apache.phoenix.expression.LiteralExpression;
import org.apache.phoenix.parse.FunctionParseNode.Argument;
import org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction;
import org.apache.phoenix.schema.PDataType;
import org.apache.phoenix.schema.tuple.Tuple;

@BuiltInFunction(name=RegexpLikeFunction.NAME, args= {
@argument(allowedTypes={PDataType.VARCHAR}),
@argument(allowedTypes={PDataType.VARCHAR})} )
public class RegexpLikeFunction extends ScalarFunction {
public static final String NAME = "REGEXP_REPLACE";

private boolean hasReplaceStr;
private Pattern pattern;

public RegexpLikeFunction() { }

public RegexpLikeFunction(List<Expression> children) {
    super(children);
    init();
}

private void init() {
    Object patternString = ((LiteralExpression)children.get(1)).getValue();
    if (patternString != null) {
        pattern = Pattern.compile((String)patternString);
    }
}

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (pattern == null) {
        return false;
    }
    Expression sourceStrExpression = getSourceStrExpression();
    if (!sourceStrExpression.evaluate(tuple, ptr)) {
        return false;
    }
    String sourceStr = (String)PDataType.VARCHAR.toObject(ptr, sourceStrExpression.getColumnModifier());
    if (sourceStr == null) {
        return false;
    }


    boolean matched = pattern.matcher(sourceStr).matches();
    ptr.set(matched ? PDataType.TRUE_BYTES : PDataType.FALSE_BYTES);
    return true;
}

private Expression getSourceStrExpression() {
    return children.get(0);
}



@Override
public PDataType getDataType() {
    return PDataType.BOOLEAN;
}

@Override
public void readFields(DataInput input) throws IOException {
    super.readFields(input);
    init();
}

@Override
public String getName() {
    return NAME;
}

}

@jtaylor-sfdc
Copy link
Contributor

@RamyaStec - thanks for the patch, however this github repo is no longer active as Phoenix is an Apache project since last year. Please see here on how to contribute. Essentially you'll send a pull request to our Apache github repo and generate a patch file for each branch that'll contain your fix: 3.0, 4.0, and master.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants