Search in sources :

Example 96 with Expression

use of org.apache.phoenix.expression.Expression 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 97 with Expression

use of org.apache.phoenix.expression.Expression in project phoenix by apache.

the class PhoenixRuntimeTest method testGetTenantIdExpression.

@Test
public void testGetTenantIdExpression() throws Exception {
    Connection conn = DriverManager.getConnection(getUrl());
    Expression e1 = PhoenixRuntime.getTenantIdExpression(conn, PhoenixDatabaseMetaData.SYSTEM_STATS_NAME);
    assertNull(e1);
    Expression e2 = PhoenixRuntime.getTenantIdExpression(conn, PhoenixDatabaseMetaData.SYSTEM_CATALOG_NAME);
    assertNotNull(e2);
    Expression e3 = PhoenixRuntime.getTenantIdExpression(conn, PhoenixDatabaseMetaData.SYSTEM_SEQUENCE_NAME);
    assertNotNull(e3);
    conn.createStatement().execute("CREATE TABLE FOO (k VARCHAR PRIMARY KEY)");
    Expression e4 = PhoenixRuntime.getTenantIdExpression(conn, "FOO");
    assertNull(e4);
    conn.createStatement().execute("CREATE TABLE A.BAR (k1 VARCHAR NOT NULL, k2 VARCHAR, CONSTRAINT PK PRIMARY KEY(K1,K2)) MULTI_TENANT=true");
    Expression e5 = PhoenixRuntime.getTenantIdExpression(conn, "A.BAR");
    assertNotNull(e5);
    conn.createStatement().execute("CREATE INDEX I1 ON A.BAR (K2)");
    Expression e5A = PhoenixRuntime.getTenantIdExpression(conn, "A.I1");
    assertNotNull(e5A);
    conn.createStatement().execute("CREATE TABLE BAS (k1 VARCHAR NOT NULL, k2 VARCHAR, CONSTRAINT PK PRIMARY KEY(K1,K2)) MULTI_TENANT=true, SALT_BUCKETS=3");
    Expression e6 = PhoenixRuntime.getTenantIdExpression(conn, "BAS");
    assertNotNull(e6);
    conn.createStatement().execute("CREATE INDEX I2 ON BAS (K2)");
    Expression e6A = PhoenixRuntime.getTenantIdExpression(conn, "I2");
    assertNotNull(e6A);
    try {
        PhoenixRuntime.getTenantIdExpression(conn, "NOT.ATABLE");
        fail();
    } catch (TableNotFoundException e) {
    // Expected
    }
    Properties props = PropertiesUtil.deepCopy(TestUtil.TEST_PROPERTIES);
    props.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, "t1");
    Connection tsconn = DriverManager.getConnection(getUrl(), props);
    tsconn.createStatement().execute("CREATE VIEW V(V1 VARCHAR) AS SELECT * FROM BAS");
    Expression e7 = PhoenixRuntime.getTenantIdExpression(tsconn, "V");
    assertNotNull(e7);
    tsconn.createStatement().execute("CREATE LOCAL INDEX I3 ON V (V1)");
    try {
        PhoenixRuntime.getTenantIdExpression(tsconn, "I3");
        fail();
    } catch (SQLFeatureNotSupportedException e) {
    // Expected
    }
}
Also used : TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) Expression(org.apache.phoenix.expression.Expression) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) Connection(java.sql.Connection) Properties(java.util.Properties) Test(org.junit.Test) BaseConnectionlessQueryTest(org.apache.phoenix.query.BaseConnectionlessQueryTest)

Example 98 with Expression

use of org.apache.phoenix.expression.Expression in project phoenix by apache.

the class OctetLengthFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    // get binary data parameter
    Expression dataExpr = children.get(0);
    if (!dataExpr.evaluate(tuple, ptr))
        return false;
    // set result
    ((PBinaryBase) dataExpr.getDataType()).octetLength(ptr, dataExpr.getSortOrder(), ptr);
    return true;
}
Also used : Expression(org.apache.phoenix.expression.Expression) PBinaryBase(org.apache.phoenix.schema.types.PBinaryBase)

Example 99 with Expression

use of org.apache.phoenix.expression.Expression in project phoenix by apache.

the class RoundDateExpression method create.

/**
     * @param timeUnit - unit of time to round up to
     * @param multiplier - determines the roll up window size.
     * Create a {@link RoundDateExpression}. 
     */
public static Expression create(Expression expr, TimeUnit timeUnit, int multiplier) throws SQLException {
    Expression timeUnitExpr = getTimeUnitExpr(timeUnit);
    Expression defaultMultiplierExpr = getMultiplierExpr(multiplier);
    List<Expression> expressions = Lists.newArrayList(expr, timeUnitExpr, defaultMultiplierExpr);
    return create(expressions);
}
Also used : Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression)

Example 100 with Expression

use of org.apache.phoenix.expression.Expression in project phoenix by apache.

the class LpadFunction method evaluate.

/**
     * Left pads a string with with the given fill expression.
     */
@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression outputStrLenExpr = getOutputStrLenExpr();
    if (!outputStrLenExpr.evaluate(tuple, ptr)) {
        return false;
    }
    int outputStrLen = outputStrLenExpr.getDataType().getCodec().decodeInt(ptr, outputStrLenExpr.getSortOrder());
    if (outputStrLen < 0) {
        return false;
    }
    Expression strExp = getStrExpr();
    if (!strExp.evaluate(tuple, ptr)) {
        return false;
    }
    boolean isStrCharType = getStrExpr().getDataType() == PChar.INSTANCE;
    boolean isFillCharType = getFillExpr().getDataType() == PChar.INSTANCE;
    SortOrder strSortOrder = getStrExpr().getSortOrder();
    SortOrder fillSortOrder = getFillExpr().getSortOrder();
    int inputStrLen = getUTF8Length(ptr, strSortOrder, isStrCharType);
    if (outputStrLen == inputStrLen) {
        // nothing to do
        return true;
    }
    if (outputStrLen < inputStrLen) {
        // truncate the string from the right
        int subStrByteLength = getSubstringByteLength(ptr, outputStrLen, strSortOrder, isStrCharType);
        ptr.set(ptr.get(), ptr.getOffset(), subStrByteLength);
        return true;
    }
    // left pad the input string with the fill chars
    Expression fillExpr = getFillExpr();
    ImmutableBytesWritable fillPtr = new ImmutableBytesWritable();
    if (!fillExpr.evaluate(tuple, fillPtr)) {
        return false;
    }
    int fillExprLen = fillPtr.getLength();
    if (fillExprLen < 1) {
        // return if fill is empty
        return false;
    }
    // if the padding to be added is not a multiple of the length of the
    // fill string then we need to use part of the fill string to pad
    // LPAD(ab, 5, xy) = xyxab
    // padLen = 3
    // numFillsPrepended = 1
    // numFillCharsPrepended = 1
    // length of the fill string
    int fillLen = getUTF8Length(fillPtr, fillSortOrder, isFillCharType);
    // length of padding to be added
    int padLen = outputStrLen - inputStrLen;
    // number of fill strings to be prepended
    int numFillsPrepended = padLen / fillLen;
    // number of chars from fill string to be prepended
    int numFillCharsPrepended = padLen % fillLen;
    // byte length of the input string
    int strByteLength = ptr.getLength();
    // byte length of the fill string
    int fillByteLength = getSubstringByteLength(fillPtr, fillPtr.getLength(), fillSortOrder, isFillCharType);
    // byte length of the full fills to be prepended
    int fullFillsByteLength = numFillsPrepended * fillByteLength;
    // byte length of the chars of fill string to be prepended
    int fillCharsByteLength = getSubstringByteLength(fillPtr, numFillCharsPrepended, fillSortOrder, isFillCharType);
    // byte length of the padded string =
    int strWithPaddingByteLength = fullFillsByteLength + fillCharsByteLength + strByteLength;
    // need to invert the fill string if the sort order of fill and
    // input are different
    boolean invertFill = fillSortOrder != strSortOrder;
    byte[] paddedStr = StringUtil.lpad(ptr.get(), ptr.getOffset(), ptr.getLength(), fillPtr.get(), fillPtr.getOffset(), fillPtr.getLength(), invertFill, strWithPaddingByteLength);
    ptr.set(paddedStr);
    return true;
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) Expression(org.apache.phoenix.expression.Expression) SortOrder(org.apache.phoenix.schema.SortOrder)

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