Package com.salesforce.phoenix.expression

Examples of com.salesforce.phoenix.expression.Expression


        return children.get(0);
    }

    @Override
    public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
        Expression child = getStringExpression();
        if (!child.evaluate(tuple, ptr)) {
            return false;
        }
        if (ptr.getLength() == 0) {
            ptr.set(ByteUtil.EMPTY_BYTE_ARRAY);
            return true;
        }
        int len;
        if (child.getDataType() == PDataType.CHAR) {
            // Only single-byte characters allowed in CHAR
            len = ptr.getLength();
        } else {
            try {
                len = StringUtil.calculateUTF8Length(ptr.get(), ptr.getOffset(), ptr.getLength(), child.getColumnModifier());
            } catch (UnsupportedEncodingException e) {
                return false;
            }
        }
        ptr.set(PDataType.INTEGER.toBytes(len));
View Full Code Here


        }
    }

    @Override
    public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
        Expression offsetExpression = getOffsetExpression();
        if (!offsetExpression.evaluate(tuple,  ptr)) {
            return false;
        }
        int offset = offsetExpression.getDataType().getCodec().decodeInt(ptr, offsetExpression.getColumnModifier());
       
        int length = -1;
        if (hasLengthExpression) {
            Expression lengthExpression = getLengthExpression();
            if (!lengthExpression.evaluate(tuple, ptr)) {
                return false;
            }
            length = lengthExpression.getDataType().getCodec().decodeInt(ptr, lengthExpression.getColumnModifier());
            if (length <= 0) {
                return false;
            }
        }
       
View Full Code Here

        super(children);
    }
   
    @Override
    public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
        Expression child = children.get(0);
        if (!child.evaluate(tuple, ptr)) {
            return false;
        }
        if (ptr.getLength() == 0) {
            return true;
        }
View Full Code Here

        List<Expression> childExprs = Lists.newArrayList(expr, getTimeUnitExpr(TimeUnit.MILLISECOND), getMultiplierExpr(multiplier));
        return new CeilTimestampExpression(childExprs);
    }
   
    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
View Full Code Here

            return super.visitLeave(node, children);
        }
       
        @Override
        public Expression visitLeave(ComparisonParseNode node, List<Expression> children) throws SQLException {
            Expression literal = children.get(1);
            ImmutableBytesWritable ptr = context.getTempPtr();
            literal.evaluate(null, ptr);
            PColumn column = columnRef.getColumn();
            column.getDataType().coerceBytes(ptr, literal.getDataType(), literal.getColumnModifier(), column.getColumnModifier());
            viewColumns.put(columnRef, ByteUtil.copyKeyBytesIfNecessary(ptr));
            return super.visitLeave(node, children);
        }
View Full Code Here

    @Override
    public Expression visitLeave(ComparisonParseNode node, List<Expression> children) throws SQLException {
        ParseNode lhsNode = node.getChildren().get(0);
        ParseNode rhsNode = node.getChildren().get(1);
        Expression lhsExpr = children.get(0);
        Expression rhsExpr = children.get(1);
        boolean isDeterministic = lhsExpr.isDeterministic() || rhsExpr.isDeterministic();

        PDataType lhsExprDataType = lhsExpr.getDataType();
        PDataType rhsExprDataType = rhsExpr.getDataType();
        checkComparability(node, lhsNode, rhsNode, lhsExpr, rhsExpr);
        // We don't yet support comparison between entire arrays
        if ( ( (lhsExprDataType != null && lhsExprDataType.isArrayType()) ||
               (rhsExprDataType != null && rhsExprDataType.isArrayType()) ) &&
             ( node.getFilterOp() != CompareOp.EQUAL && node.getFilterOp() != CompareOp.NOT_EQUAL ) ) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.NON_EQUALITY_ARRAY_COMPARISON)
            .setMessage(ComparisonExpression.toString(node.getFilterOp(), children)).build().buildException();
        }
       
        if (lhsExpr instanceof RowValueConstructorExpression || rhsExpr instanceof RowValueConstructorExpression) {
            rhsExpr = RowValueConstructorExpression.coerce(lhsExpr, rhsExpr, node.getFilterOp());
            // Always wrap both sides in row value constructor, so we don't have to consider comparing
            // a non rvc with a rvc.
            if ( ! ( lhsExpr instanceof RowValueConstructorExpression ) ) {
                lhsExpr = new RowValueConstructorExpression(Collections.singletonList(lhsExpr), lhsExpr.isStateless());
            }
            children = Arrays.asList(lhsExpr, rhsExpr);
        }
       
        Object lhsValue = null;
        // Can't use lhsNode.isConstant(), because we have cases in which we don't know
        // in advance if a function evaluates to null (namely when bind variables are used)
        // TODO: use lhsExpr.isStateless instead
        if (lhsExpr instanceof LiteralExpression) {
            lhsValue = ((LiteralExpression)lhsExpr).getValue();
            if (lhsValue == null) {
                return LiteralExpression.newConstant(false, PDataType.BOOLEAN, lhsExpr.isDeterministic());
            }
        }
        Object rhsValue = null;
        // TODO: use lhsExpr.isStateless instead
        if (rhsExpr instanceof LiteralExpression) {
            rhsValue = ((LiteralExpression)rhsExpr).getValue();
            if (rhsValue == null) {
                return LiteralExpression.newConstant(false, PDataType.BOOLEAN, rhsExpr.isDeterministic());
            }
        }
        if (lhsValue != null && rhsValue != null) {
            return LiteralExpression.newConstant(ByteUtil.compare(node.getFilterOp(),lhsExprDataType.compareTo(lhsValue, rhsValue, rhsExprDataType)), isDeterministic);
        }
        // Coerce constant to match type of lhs so that we don't need to
        // convert at filter time. Since we normalize the select statement
        // to put constants on the LHS, we don't need to check the RHS.
        if (rhsValue != null) {
            // Comparing an unsigned int/long against a negative int/long would be an example. We just need to take
            // into account the comparison operator.
            if (rhsExprDataType != lhsExprDataType
                    || rhsExpr.getColumnModifier() != lhsExpr.getColumnModifier()
                    || (rhsExpr.getMaxLength() != null && lhsExpr.getMaxLength() != null && rhsExpr.getMaxLength() < lhsExpr.getMaxLength())) {
                // TODO: if lengths are unequal and fixed width?
                if (rhsExprDataType.isCoercibleTo(lhsExprDataType, rhsValue)) { // will convert 2.0 -> 2
                    children = Arrays.asList(children.get(0), LiteralExpression.newConstant(rhsValue, lhsExprDataType,
                            lhsExpr.getMaxLength(), null, lhsExpr.getColumnModifier(), isDeterministic));
                } else if (node.getFilterOp() == CompareOp.EQUAL) {
                    return LiteralExpression.newConstant(false, PDataType.BOOLEAN, true);
                } else if (node.getFilterOp() == CompareOp.NOT_EQUAL) {
                    return LiteralExpression.newConstant(true, PDataType.BOOLEAN, true);
                } else { // TODO: generalize this with PDataType.getMinValue(), PDataTypeType.getMaxValue() methods
                    switch(rhsExprDataType) {
                    case DECIMAL:
                        /*
                         * We're comparing an int/long to a constant decimal with a fraction part.
                         * We need the types to match in case this is used to form a key. To form the start/stop key,
                         * we need to adjust the decimal by truncating it or taking its ceiling, depending on the comparison
                         * operator, to get a whole number.
                         */
                        int increment = 0;
                        switch (node.getFilterOp()) {
                        case GREATER_OR_EQUAL:
                        case LESS: // get next whole number
                            increment = 1;
                        default: // Else, we truncate the value
                            BigDecimal bd = (BigDecimal)rhsValue;
                            rhsValue = bd.longValue() + increment;
                            children = Arrays.asList(children.get(0), LiteralExpression.newConstant(rhsValue, lhsExprDataType, lhsExpr.getColumnModifier(), rhsExpr.isDeterministic()));
                            break;
                        }
                        break;
                    case LONG:
                        /*
 
View Full Code Here

    @Override
    public Expression visitLeave(AndParseNode node, List<Expression> children) throws SQLException {
        boolean isDeterministic = true;
        Iterator<Expression> iterator = children.iterator();
        while (iterator.hasNext()) {
            Expression child = iterator.next();
            if (child.getDataType() != PDataType.BOOLEAN) {
                throw TypeMismatchException.newException(PDataType.BOOLEAN, child.getDataType(), child.toString());
            }
            if (LiteralExpression.isFalse(child)) {
                return child;
            }
            if (LiteralExpression.isTrue(child)) {
                iterator.remove();
            }
            isDeterministic &= child.isDeterministic();
        }
        if (children.size() == 0) {
            return LiteralExpression.newConstant(true, isDeterministic);
        }
        if (children.size() == 1) {
View Full Code Here

    private Expression orExpression(List<Expression> children) throws SQLException {
        Iterator<Expression> iterator = children.iterator();
        boolean isDeterministic = true;
        while (iterator.hasNext()) {
            Expression child = iterator.next();
            if (child.getDataType() != PDataType.BOOLEAN) {
                throw TypeMismatchException.newException(PDataType.BOOLEAN, child.getDataType(), child.toString());
            }
            if (LiteralExpression.isFalse(child)) {
                iterator.remove();
            }
            if (LiteralExpression.isTrue(child)) {
                return child;
            }
            isDeterministic &= child.isDeterministic();
        }
        if (children.size() == 0) {
            return LiteralExpression.newConstant(true, isDeterministic);
        }
        if (children.size() == 1) {
View Full Code Here

    public Expression create(List<Expression> children, StatementContext context) throws SQLException {
        return getRoundExpression(children);
    }

    public static Expression getRoundExpression(List<Expression> children) throws SQLException {
        final Expression firstChild = children.get(0);
        final PDataType firstChildDataType = firstChild.getDataType();
       
        if(firstChildDataType.isCoercibleTo(PDataType.DATE)) {
            return RoundDateExpression.create(children); // FIXME: remove cast
        } else if (firstChildDataType.isCoercibleTo(PDataType.TIMESTAMP)) {
            return RoundTimestampExpression.create(children); // FIXME: remove cast
View Full Code Here

    public Expression create(List<Expression> children, StatementContext context) throws SQLException {
        return getFloorExpression(children);
    }

    public static Expression getFloorExpression(List<Expression> children) throws SQLException {
        final Expression firstChild = children.get(0);
        final PDataType firstChildDataType = firstChild.getDataType();
       
        //FLOOR on timestamp doesn't really care about the nanos part i.e. it just sets it to zero.
        //Which is exactly what FloorDateExpression does too.
        if(firstChildDataType.isCoercibleTo(PDataType.TIMESTAMP)) {
            return FloorDateExpression.create(children);
View Full Code Here

TOP

Related Classes of com.salesforce.phoenix.expression.Expression

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.