Search in sources :

Example 91 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class DateScalarFunction method init.

private void init() {
    PDataType returnType = getChildren().get(0).getDataType();
    inputCodec = DateUtil.getCodecFor(returnType);
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType)

Example 92 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class DayOfMonthFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression expression = getChildExpression();
    if (!expression.evaluate(tuple, ptr)) {
        return false;
    }
    if (ptr.getLength() == 0) {
        //means null
        return true;
    }
    long dateTime = inputCodec.decodeLong(ptr, expression.getSortOrder());
    DateTime dt = new DateTime(dateTime);
    int day = dt.getDayOfMonth();
    PDataType returnType = getDataType();
    byte[] byteValue = new byte[returnType.getByteSize()];
    returnType.getCodec().encodeInt(day, byteValue, 0);
    ptr.set(byteValue);
    return true;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) DateTime(org.joda.time.DateTime)

Example 93 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class FloorDateExpression method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    if (children.get(0).evaluate(tuple, ptr)) {
        PDataType dataType = getDataType();
        long time = dataType.getCodec().decodeLong(ptr, children.get(0).getSortOrder());
        long value = roundTime(time);
        Date d = new Date(value);
        byte[] byteValue = dataType.toBytes(d);
        ptr.set(byteValue);
        return true;
    }
    return false;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) Date(java.sql.Date) PUnsignedDate(org.apache.phoenix.schema.types.PUnsignedDate) PDate(org.apache.phoenix.schema.types.PDate)

Example 94 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class FloorDateExpression method create.

public static Expression create(List<Expression> children) throws SQLException {
    Expression firstChild = children.get(0);
    PDataType firstChildDataType = firstChild.getDataType();
    if (firstChildDataType == PTimestamp.INSTANCE || firstChildDataType == PUnsignedTimestamp.INSTANCE) {
        // Coerce TIMESTAMP to DATE, as the nanos has no affect
        List<Expression> newChildren = Lists.newArrayListWithExpectedSize(children.size());
        newChildren.add(CoerceExpression.create(firstChild, firstChildDataType == PTimestamp.INSTANCE ? PDate.INSTANCE : PUnsignedDate.INSTANCE));
        newChildren.addAll(children.subList(1, children.size()));
        children = newChildren;
    }
    Object timeUnitValue = ((LiteralExpression) children.get(1)).getValue();
    TimeUnit timeUnit = TimeUnit.getTimeUnit(timeUnitValue != null ? timeUnitValue.toString() : null);
    switch(timeUnit) {
        case WEEK:
            return new FloorWeekExpression(children);
        case MONTH:
            return new FloorMonthExpression(children);
        case YEAR:
            return new FloorYearExpression(children);
        default:
            return new FloorDateExpression(children);
    }
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) Expression(org.apache.phoenix.expression.Expression) CoerceExpression(org.apache.phoenix.expression.CoerceExpression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression)

Example 95 with PDataType

use of org.apache.phoenix.schema.types.PDataType in project phoenix by apache.

the class DecodeFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression expression = getExpression();
    if (!expression.evaluate(tuple, ptr)) {
        return false;
    }
    if (ptr.getLength() == 0) {
        // expression was evaluated, but evaluated to null
        return true;
    }
    PDataType type = expression.getDataType();
    String stringToDecode = (String) type.toObject(ptr);
    Expression encodingExpression = getEncodingExpression();
    if (!encodingExpression.evaluate(tuple, ptr)) {
        return false;
    }
    if (ptr.getLength() == 0) {
        throw new IllegalDataException(new SQLExceptionInfo.Builder(SQLExceptionCode.ILLEGAL_DATA).setMessage("Missing bytes encoding").build().buildException());
    }
    type = encodingExpression.getDataType();
    String encoding = ((String) type.toObject(ptr)).toUpperCase();
    byte[] out;
    EncodeFormat format = EncodeFormat.valueOf(encoding);
    switch(format) {
        case HEX:
            out = decodeHex(stringToDecode);
            break;
        default:
            throw new IllegalDataException("Unsupported encoding \"" + encoding + "\"");
    }
    ptr.set(out);
    return true;
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) IllegalDataException(org.apache.phoenix.schema.IllegalDataException)

Aggregations

PDataType (org.apache.phoenix.schema.types.PDataType)152 Expression (org.apache.phoenix.expression.Expression)54 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)31 SortOrder (org.apache.phoenix.schema.SortOrder)29 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)21 CoerceExpression (org.apache.phoenix.expression.CoerceExpression)18 Test (org.junit.Test)15 PDatum (org.apache.phoenix.schema.PDatum)12 BigDecimal (java.math.BigDecimal)11 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)11 List (java.util.List)10 KeyValueColumnExpression (org.apache.phoenix.expression.KeyValueColumnExpression)10 RowKeyColumnExpression (org.apache.phoenix.expression.RowKeyColumnExpression)10 SingleCellConstructorExpression (org.apache.phoenix.expression.SingleCellConstructorExpression)10 IOException (java.io.IOException)9 PreparedStatement (java.sql.PreparedStatement)7 Pair (org.apache.hadoop.hbase.util.Pair)7 Date (java.sql.Date)6 AndExpression (org.apache.phoenix.expression.AndExpression)6