Search in sources :

Example 16 with Expression

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

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

the class RoundDecimalExpression method newKeyPart.

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

        private final List<Expression> extractNodes = Collections.<Expression>singletonList(RoundDecimalExpression.this);

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

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

        @Override
        public KeyRange getKeyRange(CompareFilter.CompareOp op, Expression rhs) {
            final BigDecimal rhsDecimal = (BigDecimal) PDecimal.INSTANCE.toObject(evaluateExpression(rhs));
            // than needed for a match, it's impossible for there to be any matches
            if (op == CompareFilter.CompareOp.EQUAL && !hasEnoughPrecisionToProduce(rhsDecimal)) {
                return KeyRange.EMPTY_RANGE;
            }
            // if the decimal needs to be rounded, round it such that the given 
            // operator will still be valid
            BigDecimal roundedDecimal = roundAndPreserveOperator(rhsDecimal, op);
            // the range of big decimals that could be rounded to produce the rounded result
            // alternatively, the "rounding bucket" that this decimal falls into
            final KeyRange equalityRange = getInputRangeProducing(roundedDecimal);
            boolean lowerInclusive = equalityRange.isLowerInclusive();
            boolean upperInclusive = equalityRange.isUpperInclusive();
            byte[] lowerRange = KeyRange.UNBOUND;
            byte[] upperRange = KeyRange.UNBOUND;
            switch(op) {
                case EQUAL:
                    return equalityRange;
                case GREATER:
                    // from the equality range and up, NOT including the equality range
                    lowerRange = equalityRange.getUpperRange();
                    lowerInclusive = !equalityRange.isUpperInclusive();
                    break;
                case GREATER_OR_EQUAL:
                    // from the equality range and up, including the equality range
                    lowerRange = equalityRange.getLowerRange();
                    break;
                case LESS:
                    // from the equality range and down, NOT including the equality range
                    upperRange = equalityRange.getLowerRange();
                    upperInclusive = !equalityRange.isLowerInclusive();
                    break;
                case LESS_OR_EQUAL:
                    // from the equality range and down, including the equality range
                    upperRange = equalityRange.getUpperRange();
                    break;
                default:
                    throw new AssertionError("Invalid CompareOp: " + op);
            }
            return KeyRange.getKeyRange(lowerRange, lowerInclusive, upperRange, upperInclusive);
        }

        /**
             * Produces a the given decimal rounded to this rounding expression's scale. If the 
             * decimal requires more scale precision to produce than this expression has, as in
             * ROUND(?, 2) &gt; 2.0098974, it ensures that the decimal is rounded such that the
             * given operator will still produce correct results.
             * @param decimal  the decimal to round with this expression's scale
             * @param op  the operator to preserve comparison with in the event of lost precision
             * @return  the rounded decimal
             */
        private BigDecimal roundAndPreserveOperator(BigDecimal decimal, CompareFilter.CompareOp op) {
            final BigDecimal rounded = roundToScale(decimal);
            // if we lost information, make sure that the rounding didn't break the operator
            if (!hasEnoughPrecisionToProduce(decimal)) {
                switch(op) {
                    case GREATER_OR_EQUAL:
                        // 'ROUND(dec, 2) >= 2.01' but should be 'ROUND(dec, 2) >= 2.02'
                        if (decimal.compareTo(rounded) > 0) {
                            return stepNextInScale(rounded);
                        }
                        break;
                    case GREATER:
                        // 'ROUND(dec, 2) > 2.02' but should be 'ROUND(dec, 2) > 2.01'
                        if (decimal.compareTo(rounded) < 0) {
                            return stepPrevInScale(rounded);
                        }
                        break;
                    case LESS_OR_EQUAL:
                        // 'ROUND(dec, 2) < 2.02' but should be 'ROUND(dec, 2) < 2.01'
                        if (decimal.compareTo(rounded) < 0) {
                            return stepPrevInScale(rounded);
                        }
                        break;
                    case LESS:
                        // 'ROUND(dec, 2) <= 2.01' but should be 'ROUND(dec, 2) <= 2.02'
                        if (decimal.compareTo(rounded) > 0) {
                            return stepNextInScale(rounded);
                        }
                        break;
                }
            }
            // otherwise, rounding has not affected the operator, so return normally
            return rounded;
        }

        @Override
        public PTable getTable() {
            return childPart.getTable();
        }
    };
}
Also used : Expression(org.apache.phoenix.expression.Expression) LiteralExpression(org.apache.phoenix.expression.LiteralExpression) KeyRange(org.apache.phoenix.query.KeyRange) KeyPart(org.apache.phoenix.compile.KeyPart) List(java.util.List) BigDecimal(java.math.BigDecimal)

Example 18 with Expression

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

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

Example 20 with Expression

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

the class ReverseFunction method evaluate.

@Override
public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
    Expression arg = getChildren().get(0);
    if (!arg.evaluate(tuple, ptr)) {
        return false;
    }
    int targetOffset = ptr.getLength();
    if (targetOffset == 0) {
        return true;
    }
    byte[] source = ptr.get();
    byte[] target = new byte[targetOffset];
    int sourceOffset = ptr.getOffset();
    int endOffset = sourceOffset + ptr.getLength();
    SortOrder sortOrder = arg.getSortOrder();
    while (sourceOffset < endOffset) {
        int nBytes = StringUtil.getBytesInChar(source[sourceOffset], sortOrder);
        targetOffset -= nBytes;
        System.arraycopy(source, sourceOffset, target, targetOffset, nBytes);
        sourceOffset += nBytes;
    }
    ptr.set(target);
    return true;
}
Also used : 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