Package com.foundationdb.sql.types

Examples of com.foundationdb.sql.types.DataTypeDescriptor


        if (rightTypeId.isDateTimeTimeStampTypeId()) {
            if (operator.equals(TypeCompiler.MINUS_OP)) {
                switch (rightTypeId.getTypeFormatId()) {
                case TypeId.FormatIds.TIME_TYPE_ID:
                    // TIME - TIME is INTERVAL HOUR TO SECOND
                    return new DataTypeDescriptor(TypeId.INTERVAL_HOUR_SECOND_ID, nullable);
                }
                // TIME - other datetime is INTERVAL DAY TO SECOND
                return new DataTypeDescriptor(TypeId.INTERVAL_DAY_SECOND_ID, nullable);
            }
        }
        else if (rightTypeId.isIntervalTypeId()) {
            if (operator.equals(TypeCompiler.PLUS_OP) ||
                operator.equals(TypeCompiler.MINUS_OP)) {
                switch (rightTypeId.getTypeFormatId()) {
                case TypeId.FormatIds.INTERVAL_DAY_SECOND_ID:
                    if ((rightTypeId == TypeId.INTERVAL_HOUR_ID) ||
                        (rightTypeId == TypeId.INTERVAL_MINUTE_ID) ||
                        (rightTypeId == TypeId.INTERVAL_SECOND_ID) ||
                        (rightTypeId == TypeId.INTERVAL_HOUR_MINUTE_ID) ||
                        (rightTypeId == TypeId.INTERVAL_HOUR_SECOND_ID) ||
                        (rightTypeId == TypeId.INTERVAL_MINUTE_SECOND_ID))
                        // TIME +/- sub day interval is TIME
                        return leftType.getNullabilityType(nullable);
                }
                // TIME +/- other interval is TIMESTAMP
                return new DataTypeDescriptor(TypeId.TIMESTAMP_ID, nullable);
            }
        }

        // Unsupported
        return super.resolveArithmeticOperation(leftType, rightType, operator);
View Full Code Here


    public DataTypeDescriptor resolveArithmeticOperation(DataTypeDescriptor leftType,
                                                         DataTypeDescriptor rightType,
                                                         String operator)
            throws StandardException {
        NumericTypeCompiler higherTC;
        DataTypeDescriptor higherType;
        boolean nullable;
        int precision, scale, maximumWidth;

        /*
        ** Check the right type to be sure it's a number.    By convention,
        ** we call this method off the TypeId of the left operand, so if
        ** we get here, we know the left operand is a number.
        */
        assert leftType.getTypeId().isNumericTypeId() : "The left type is supposed to be a number because we're resolving an arithmetic operator";

        TypeId leftTypeId = leftType.getTypeId();
        TypeId rightTypeId = rightType.getTypeId();

        boolean supported = true;

        if (!(rightTypeId.isNumericTypeId())) {
            if (rightTypeId.isIntervalTypeId() &&
                operator.equals(TypeCompiler.TIMES_OP)) {
                // Let interval type resolve it.
                return getTypeCompiler(rightTypeId).resolveArithmeticOperation(rightType, leftType, operator);
            }
            supported = false;
        }

        if (TypeCompiler.MOD_OP.equals(operator)) {
            switch (leftTypeId.getJDBCTypeId()) {
            case java.sql.Types.TINYINT:
            case java.sql.Types.SMALLINT:
            case java.sql.Types.INTEGER:
            case java.sql.Types.BIGINT:
                break;
            default:
                supported = false;
                break;
            }
            switch (rightTypeId.getJDBCTypeId()) {
            case java.sql.Types.TINYINT:
            case java.sql.Types.SMALLINT:
            case java.sql.Types.INTEGER:
            case java.sql.Types.BIGINT:
                break;
            default:
                supported = false;
                break;
            }

        }

        if (!supported) {
            return super.resolveArithmeticOperation(leftType, rightType, operator);
        }

        /*
        ** Take left as the higher precedence if equal
        */
        if (rightTypeId.typePrecedence() > leftTypeId.typePrecedence()) {
            higherType = rightType;
            higherTC = (NumericTypeCompiler)getTypeCompiler(rightTypeId);
        }
        else {
            higherType = leftType;
            higherTC = (NumericTypeCompiler)getTypeCompiler(leftTypeId);
        }

        /* The calculation of precision and scale should be based upon
         * the type with higher precedence, which is going to be the result
         * type, this is also to be consistent with maximumWidth.    Beetle 3906.
         */
        precision = higherTC.getPrecision(operator, leftType, rightType);
        scale = higherTC.getScale(operator, leftType, rightType);

        if (higherType.getTypeId().isDecimalTypeId()) {
            maximumWidth = (scale > 0) ? precision + 3 : precision + 1;

            /*
            ** Be careful not to overflow
            */
            if (maximumWidth < precision) {
                maximumWidth = Integer.MAX_VALUE;
            }
        }
        else {
            maximumWidth = higherType.getMaximumWidth();
        }
       
        /* The result is nullable if either side is nullable */
        nullable = leftType.isNullable() || rightType.isNullable();

        /*
        ** The higher type does not have the right nullability.  Create a
        ** new DataTypeDescriptor that has the correct type and nullability.
        **
        ** It's OK to call the implementation of the DataTypeDescriptorFactory
        ** here, because we're in the same package.
        */
        return new DataTypeDescriptor(higherType.getTypeId(),
                                      precision,
                                      scale,
                                      nullable,
                                      maximumWidth);
    }
View Full Code Here

            return aggregateNode((AggregateNode)node);
        case NodeTypes.CONCATENATION_OPERATOR_NODE:
            return concatenationOperatorNode((ConcatenationOperatorNode)node);
        case NodeTypes.IS_NULL_NODE:
        case NodeTypes.IS_NOT_NULL_NODE:
            return new DataTypeDescriptor(TypeId.BOOLEAN_ID, false);
        case NodeTypes.NEXT_SEQUENCE_NODE:
            return new DataTypeDescriptor(TypeId.BIGINT_ID, false);
        case NodeTypes.CURRENT_SEQUENCE_NODE:
            return new DataTypeDescriptor(TypeId.BIGINT_ID, false);
        default:
            // assert false;
            return null;
        }
    }
View Full Code Here

    }

    protected DataTypeDescriptor unaryLogicalOperatorNode(UnaryLogicalOperatorNode node)
            throws StandardException {
        ValueNode operand = node.getOperand();
        DataTypeDescriptor type = operand.getType();
        if ((type != null) &&
            !type.getTypeId().isBooleanTypeId()) {
            type = new DataTypeDescriptor(TypeId.BOOLEAN_ID, type.isNullable());
            operand = (ValueNode)node.getNodeFactory()
                .getNode(NodeTypes.CAST_NODE,
                         operand, type,
                         node.getParserContext());
            node.setOperand(operand);
        }
        if ((type == null) && isParameterOrUntypedNull(operand)) {
            type = new DataTypeDescriptor(TypeId.BOOLEAN_ID, true);
            operand.setType(type);
        }
        return type;
    }
View Full Code Here

    protected DataTypeDescriptor binaryLogicalOperatorNode(BinaryLogicalOperatorNode node)
            throws StandardException {
        ValueNode leftOperand = node.getLeftOperand();
        ValueNode rightOperand = node.getRightOperand();
        DataTypeDescriptor leftType = leftOperand.getType();
        DataTypeDescriptor rightType = rightOperand.getType();
        if ((leftType != null) &&
            !leftType.getTypeId().isBooleanTypeId()) {
            leftType = new DataTypeDescriptor(TypeId.BOOLEAN_ID, leftType.isNullable());
            leftOperand = (ValueNode)node.getNodeFactory()
                .getNode(NodeTypes.CAST_NODE,
                         leftOperand, leftType,
                         node.getParserContext());
            node.setLeftOperand(leftOperand);
        }
        if ((leftType == null) && isParameterOrUntypedNull(leftOperand)) {
            leftType = new DataTypeDescriptor(TypeId.BOOLEAN_ID, true);
            leftOperand.setType(leftType);
        }
        if ((rightType != null) &&
            !rightType.getTypeId().isBooleanTypeId()) {
            rightType = new DataTypeDescriptor(TypeId.BOOLEAN_ID, rightType.isNullable());
            rightOperand = (ValueNode)node.getNodeFactory()
                .getNode(NodeTypes.CAST_NODE,
                         rightOperand, rightType,
                         node.getParserContext());
            node.setRightOperand(rightOperand);
        }
        if ((rightType == null) && isParameterOrUntypedNull(rightOperand)) {
            rightType = new DataTypeDescriptor(TypeId.BOOLEAN_ID, true);
            rightOperand.setType(rightType);
        }
        if (node.getNodeType() == NodeTypes.IS_NODE)
            return new DataTypeDescriptor(TypeId.BOOLEAN_ID, false);
        if (leftType == null)
            return rightType;
        else if (rightType == null)
            return leftType;
        else
            return leftType.getNullabilityType(leftType.isNullable() ||
                                               rightType.isNullable());
    }
View Full Code Here

    protected DataTypeDescriptor binaryArithmeticOperatorNode(BinaryArithmeticOperatorNode node)
            throws StandardException {
        ValueNode leftOperand = node.getLeftOperand();
        ValueNode rightOperand = node.getRightOperand();
        DataTypeDescriptor leftType = leftOperand.getType();
        DataTypeDescriptor rightType = rightOperand.getType();
        if (isParameterOrUntypedNull(leftOperand) && (rightType != null)) {
            leftType = rightType.getNullabilityType(true);
            leftOperand.setType(leftType);
        }
        else if (isParameterOrUntypedNull(rightOperand) && (leftType != null)) {
            rightType = leftType.getNullabilityType(true);
            rightOperand.setType(rightType);
        }
        if ((leftType == null) || (rightType == null))
            return null;
        TypeId leftTypeId = leftType.getTypeId();
        TypeId rightTypeId = rightType.getTypeId();

        /* Do any implicit conversions from (long) (var)char. */
        if (leftTypeId.isStringTypeId() && rightTypeId.isNumericTypeId()) {
            boolean nullableResult;
            nullableResult = leftType.isNullable() || rightType.isNullable();

            /* If other side is decimal/numeric, then we need to diddle
             * with the precision, scale and max width in order to handle
             * computations like:    1.1 + '0.111'
             */
            int precision = rightType.getPrecision();
            int scale = rightType.getScale();
            int maxWidth = rightType.getMaximumWidth();

            if (rightTypeId.isDecimalTypeId()) {
                int charMaxWidth = leftType.getMaximumWidth();
                precision += (2 * charMaxWidth);
                scale += charMaxWidth;                             
                maxWidth = precision + 3;
            }

            leftOperand = (ValueNode)node.getNodeFactory()
                .getNode(NodeTypes.CAST_NODE,
                         leftOperand,
                         new DataTypeDescriptor(rightTypeId, precision,
                                                scale, nullableResult,
                                                maxWidth),
                         node.getParserContext());
            node.setLeftOperand(leftOperand);
        }
        else if (rightTypeId.isStringTypeId() && leftTypeId.isNumericTypeId()) {
            boolean nullableResult;
            nullableResult = leftType.isNullable() || rightType.isNullable();

            /* If other side is decimal/numeric, then we need to diddle
             * with the precision, scale and max width in order to handle
             * computations like:    1.1 + '0.111'
             */
            int precision = leftType.getPrecision();
            int scale = leftType.getScale();
            int maxWidth = leftType.getMaximumWidth();

            if (leftTypeId.isDecimalTypeId()) {
                int charMaxWidth = rightType.getMaximumWidth();
                precision += (2 * charMaxWidth);
                scale += charMaxWidth;                             
                maxWidth = precision + 3;
            }
           
            rightOperand = (ValueNode)node.getNodeFactory()
                .getNode(NodeTypes.CAST_NODE,
                         rightOperand,
                         new DataTypeDescriptor(leftTypeId, precision,
                                                scale, nullableResult,
                                                maxWidth),
                         node.getParserContext());
            node.setRightOperand(rightOperand);
        }
View Full Code Here

        ValueNode leftOperand = node.getLeftOperand();
        ValueNode rightOperand = node.getRightOperand();

        // Infer type for parameters from other comparand.
        if (isParameterOrUntypedNull(leftOperand)) {
            DataTypeDescriptor rightType = rightOperand.getType();
            if (rightType != null)
                leftOperand.setType(rightType.getNullabilityType(true));
        }
        else if (isParameterOrUntypedNull(rightOperand)) {
            DataTypeDescriptor leftType = leftOperand.getType();
            if (leftType != null)
                rightOperand.setType(leftType.getNullabilityType(true));
        }
           

        TypeId leftTypeId = leftOperand.getTypeId();
        TypeId rightTypeId = rightOperand.getTypeId();

        if ((leftTypeId == null) || (rightTypeId == null))
            return null;

        /*
         * If we are comparing a non-string with a string type, then we
         * must prevent the non-string value from being used to probe into
         * an index on a string column. This is because the string types
         * are all of low precedence, so the comparison rules of the non-string
         * value are used, so it may not find values in a string index because
         * it will be in the wrong order.
         */
        if (!leftTypeId.isStringTypeId() && rightTypeId.isStringTypeId()) {
            DataTypeDescriptor leftType = leftOperand.getType();
            DataTypeDescriptor rightType = rightOperand.getType();

            rightOperand = (ValueNode)node.getNodeFactory()
                .getNode(NodeTypes.CAST_NODE,
                         rightOperand,
                         leftType.getNullabilityType(rightType.isNullable()),
                         node.getParserContext());
            node.setRightOperand(rightOperand);
        }
        else if (!rightTypeId.isStringTypeId() && leftTypeId.isStringTypeId()) {
            DataTypeDescriptor leftType = leftOperand.getType();
            DataTypeDescriptor rightType = rightOperand.getType();

            leftOperand = (ValueNode)node.getNodeFactory()
                .getNode(NodeTypes.CAST_NODE,
                         leftOperand,
                         rightType.getNullabilityType(leftType.isNullable()),
                         node.getParserContext());
            node.setLeftOperand(leftOperand);
        }

        // Bypass the comparable check if this is a rewrite from the
        // optimizer.    We will assume Mr. Optimizer knows what he is doing.
        if (!node.isForQueryRewrite()) {
            String operator = node.getOperator();
            boolean forEquals = operator.equals("=") || operator.equals("<>");
            boolean cmp = leftOperand.getType().comparable(rightOperand.getType(),
                                                           forEquals);
            if (!cmp) {
                throw new IncomparableException("Types not comparable: " + leftOperand.getType().getTypeName() +
                                            " and " + rightOperand.getType().getTypeName());
            }
        }
       
        /*
        ** Set the result type of this comparison operator based on the
        ** operands.    The result type is always Boolean - the only question
        ** is whether it is nullable or not.    If either of the operands is
        ** nullable, the result of the comparison must be nullable, too, so
        ** we can represent the unknown truth value.
        */
        boolean nullableResult = leftOperand.getType().isNullable() ||
                                 rightOperand.getType().isNullable();
        return new DataTypeDescriptor(TypeId.BOOLEAN_ID, nullableResult);
    }
View Full Code Here

        return new DataTypeDescriptor(TypeId.BOOLEAN_ID, nullableResult);
    }

    protected DataTypeDescriptor betweenOperatorNode(BetweenOperatorNode node) throws StandardException {
        ValueNode leftOperand = node.getLeftOperand();
        DataTypeDescriptor leftType = leftOperand.getType();
        if (leftType == null)
            return null;

        ValueNodeList rightOperands = node.getRightOperandList();
        ValueNode lowOperand = rightOperands.get(0);
        ValueNode highOperand = rightOperands.get(1);
        if (isParameterOrUntypedNull(lowOperand)) {
            lowOperand.setType(leftType.getNullabilityType(true));
        }
        if (isParameterOrUntypedNull(highOperand)) {
            highOperand.setType(leftType.getNullabilityType(true));
        }

        TypeId leftTypeId = leftOperand.getTypeId();
        DataTypeDescriptor lowType = lowOperand.getType();
        DataTypeDescriptor highType = highOperand.getType();
        if (!leftTypeId.isStringTypeId()) {
            if ((lowType != null) && lowType.getTypeId().isStringTypeId()) {
                lowOperand = (ValueNode)node.getNodeFactory()
                    .getNode(NodeTypes.CAST_NODE,
                             lowOperand,
                             leftType.getNullabilityType(lowType.isNullable()),
                             node.getParserContext());
                rightOperands.set(0, lowOperand);
            }
            if ((highType != null) && highType.getTypeId().isStringTypeId()) {
                highOperand = (ValueNode)node.getNodeFactory()
                    .getNode(NodeTypes.CAST_NODE,
                             highOperand,
                             leftType.getNullabilityType(highType.isNullable()),
                             node.getParserContext());
                rightOperands.set(1, highOperand);
            }
        }

        if ((lowType == null) || (highType == null))
            return null;
        boolean nullableResult = leftType.isNullable() ||
                                 lowType.isNullable() ||
                                 highType.isNullable();
        return new DataTypeDescriptor(TypeId.BOOLEAN_ID, nullableResult);
    }
View Full Code Here

                row(ttID, 4, true));

        fromGroupRows = runPlanTxn(groupScanCreator(ftID));//runs given plan and returns output row
        toGroupRows = runPlanTxn(groupScanCreator(ttID));
        columnNames = Arrays.asList("id", "x");
        DataTypeDescriptor d = new DataTypeDescriptor(TypeId.INTEGER_ID, false);
        DataTypeDescriptor cd = new DataTypeDescriptor(TypeId.BOOLEAN_ID, false);
        toDescriptors = Arrays.asList(d, cd);
        server = new TestSession();
    }
View Full Code Here

                row(ttID, 4));

        fromGroupRows = runPlanTxn(groupScanCreator(ftID));//runs given plan and returns output row
        toGroupRows = runPlanTxn(groupScanCreator(ttID));
        columnNames = Arrays.asList("id");
        DataTypeDescriptor d = new DataTypeDescriptor(TypeId.INTEGER_ID, false);
        toDescriptors = Arrays.asList(d);
        server = new TestSession();
    }
View Full Code Here

TOP

Related Classes of com.foundationdb.sql.types.DataTypeDescriptor

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.