Search in sources :

Example 6 with PDataType

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

the class ArrayModifierFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression arrayExpr = null;
    PDataType baseDataType = null;
    Expression otherExpr = null;
    PDataType otherExpressionType = null;
    if (getLHSExpr().getDataType().isArrayType()) {
        arrayExpr = getLHSExpr();
        baseDataType = getLHSBaseType();
        otherExpr = getRHSExpr();
        otherExpressionType = getRHSBaseType();
    } else {
        arrayExpr = getRHSExpr();
        baseDataType = getRHSBaseType();
        otherExpr = getLHSExpr();
        otherExpressionType = getLHSBaseType();
    }
    if (!arrayExpr.evaluate(tuple, ptr)) {
        return false;
    } else if (ptr.getLength() == 0) {
        return true;
    }
    int arrayLength = PArrayDataType.getArrayLength(ptr, baseDataType, arrayExpr.getMaxLength());
    int length = ptr.getLength();
    int offset = ptr.getOffset();
    byte[] arrayBytes = ptr.get();
    otherExpr.evaluate(tuple, ptr);
    checkSizeCompatibility(ptr, otherExpr.getSortOrder(), arrayExpr, baseDataType, otherExpr, otherExpressionType);
    coerceBytes(ptr, arrayExpr, baseDataType, otherExpr, otherExpressionType);
    return modifierFunction(ptr, length, offset, arrayBytes, baseDataType, arrayLength, getMaxLength(), arrayExpr);
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression)

Example 7 with PDataType

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

the class LiteralExpression method newConstant.

// TODO: cache?
public static LiteralExpression newConstant(Object value, Determinism determinism) {
    if (value instanceof Boolean) {
        return getBooleanLiteralExpression((Boolean) value, determinism);
    } else if (value == null) {
        return getNullLiteralExpression(determinism);
    }
    PDataType type = PDataType.fromLiteral(value);
    byte[] b = type.toBytes(value);
    if (type.isNull(b)) {
        return getTypedNullLiteralExpression(type, determinism);
    }
    if (type == PVarchar.INSTANCE) {
        String s = (String) value;
        if (s.length() == b.length) {
            // single byte characters only
            type = PChar.INSTANCE;
        }
    }
    return new LiteralExpression(value, type, b, determinism);
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) PBoolean(org.apache.phoenix.schema.types.PBoolean)

Example 8 with PDataType

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

the class CeilTimestampExpression method create.

public static Expression create(List<Expression> children) throws SQLException {
    Expression firstChild = children.get(0);
    PDataType firstChildDataType = firstChild.getDataType();
    String timeUnit = (String) ((LiteralExpression) children.get(1)).getValue();
    if (TimeUnit.MILLISECOND.toString().equalsIgnoreCase(timeUnit)) {
        return new CeilTimestampExpression(children);
    }
    // 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()));
    return CeilDateExpression.create(newChildren);
}
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)

Example 9 with PDataType

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

the class RoundTimestampExpression method create.

public static Expression create(List<Expression> children) throws SQLException {
    Expression firstChild = children.get(0);
    PDataType firstChildDataType = firstChild.getDataType();
    String timeUnit = (String) ((LiteralExpression) children.get(1)).getValue();
    LiteralExpression multiplierExpr = (LiteralExpression) children.get(2);
    /*
         * When rounding off timestamp to milliseconds, nanos play a part only when the multiplier value
         * is equal to 1. This is because for cases when multiplier value is greater than 1, number of nanos/multiplier
         * will always be less than half the nanos in a millisecond. 
         */
    if ((timeUnit == null || TimeUnit.MILLISECOND.toString().equalsIgnoreCase(timeUnit)) && ((Number) multiplierExpr.getValue()).intValue() == 1) {
        return new RoundTimestampExpression(children);
    }
    // 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()));
    return RoundDateExpression.create(newChildren);
}
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 10 with PDataType

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

the class PrefixFunction method newKeyPart.

@Override
public KeyPart newKeyPart(final KeyPart childPart) {
    return new KeyPart() {

        private final List<Expression> extractNodes = extractNode() ? Collections.<Expression>singletonList(PrefixFunction.this) : Collections.<Expression>emptyList();

        @Override
        public PColumn getColumn() {
            return childPart.getColumn();
        }

        @Override
        public List<Expression> getExtractNodes() {
            return extractNodes;
        }

        @Override
        public KeyRange getKeyRange(CompareOp op, Expression rhs) {
            byte[] lowerRange = KeyRange.UNBOUND;
            byte[] upperRange = KeyRange.UNBOUND;
            boolean lowerInclusive = true;
            PDataType type = getColumn().getDataType();
            switch(op) {
                case EQUAL:
                    lowerRange = evaluateExpression(rhs);
                    upperRange = ByteUtil.nextKey(lowerRange);
                    break;
                case GREATER:
                    lowerRange = ByteUtil.nextKey(evaluateExpression(rhs));
                    break;
                case LESS_OR_EQUAL:
                    upperRange = ByteUtil.nextKey(evaluateExpression(rhs));
                    lowerInclusive = false;
                    break;
                default:
                    return childPart.getKeyRange(op, rhs);
            }
            PColumn column = getColumn();
            Integer length = column.getMaxLength();
            if (type.isFixedWidth()) {
                if (length != null) {
                    // *after* rows with no padding.
                    if (lowerRange != KeyRange.UNBOUND) {
                        lowerRange = type.pad(lowerRange, length, SortOrder.ASC);
                    }
                    if (upperRange != KeyRange.UNBOUND) {
                        upperRange = type.pad(upperRange, length, SortOrder.ASC);
                    }
                }
            } else if (column.getSortOrder() == SortOrder.DESC && getTable().rowKeyOrderOptimizable()) {
                // a lowerRange of 'a\xFF' would skip 'ab', while 'a\x00\xFF' would not.
                if (lowerRange != KeyRange.UNBOUND) {
                    lowerRange = Arrays.copyOf(lowerRange, lowerRange.length + 1);
                    lowerRange[lowerRange.length - 1] = QueryConstants.SEPARATOR_BYTE;
                }
            }
            return KeyRange.getKeyRange(lowerRange, lowerInclusive, upperRange, false);
        }

        @Override
        public PTable getTable() {
            return childPart.getTable();
        }
    };
}
Also used : PColumn(org.apache.phoenix.schema.PColumn) PDataType(org.apache.phoenix.schema.types.PDataType) Expression(org.apache.phoenix.expression.Expression) KeyPart(org.apache.phoenix.compile.KeyPart) List(java.util.List) CompareOp(org.apache.hadoop.hbase.filter.CompareFilter.CompareOp)

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