Search in sources :

Example 1 with TypeId

use of org.apache.derby.iapi.types.TypeId in project derby by apache.

the class GenericParameter method throwInvalidOutParamMap.

private StandardException throwInvalidOutParamMap(int sqlType) {
    // TypeId typeId = TypeId.getBuiltInTypeId(sqlType);
    // String sqlTypeName = typeId == null ? "OTHER" : typeId.getSQLTypeName();
    String jdbcTypesName = org.apache.derby.impl.jdbc.Util.typeName(sqlType);
    TypeId typeId = TypeId.getBuiltInTypeId(jdbcTypeId);
    String thisTypeName = typeId == null ? declaredClassName : typeId.getSQLTypeName();
    StandardException e = StandardException.newException(SQLState.LANG_INVALID_OUT_PARAM_MAP, getJDBCParameterNumberStr(), jdbcTypesName, thisTypeName);
    return e;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) StandardException(org.apache.derby.shared.common.error.StandardException)

Example 2 with TypeId

use of org.apache.derby.iapi.types.TypeId in project derby by apache.

the class InListOperatorNode method getDominantType.

/**
 * Get the dominant type of all the operands in this IN list.
 * @return the type descriptor for the dominant type
 * @see DataTypeDescriptor#getDominantType(DataTypeDescriptor, ClassFactory)
 */
private DataTypeDescriptor getDominantType() {
    DataTypeDescriptor targetType = leftOperand.getTypeServices();
    TypeId judgeTypeId = targetType.getTypeId();
    if (!rightOperandList.allSamePrecendence(judgeTypeId.typePrecedence())) {
        // Iterate through the entire list of values to find out
        // what the dominant type is.
        ClassFactory cf = getClassFactory();
        for (ValueNode vn : rightOperandList) {
            targetType = targetType.getDominantType(vn.getTypeServices(), cf);
        }
    }
    return targetType;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) ClassFactory(org.apache.derby.iapi.services.loader.ClassFactory) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Example 3 with TypeId

use of org.apache.derby.iapi.types.TypeId in project derby by apache.

the class NumericTypeCompiler method resolveArithmeticOperation.

/**
 * @see TypeCompiler#resolveArithmeticOperation
 *
 * @exception StandardException		Thrown on error
 */
@Override
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.
		*/
    if (SanityManager.DEBUG)
        SanityManager.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())) {
        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) {
        throw StandardException.newException(SQLState.LANG_BINARY_OPERATOR_NOT_SUPPORTED, operator, leftType.getTypeId().getSQLTypeName(), rightType.getTypeId().getSQLTypeName());
    }
    /*
		** 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);
    if (higherType.getTypeId().isDecimalTypeId()) {
        scale = higherTC.getScale(operator, leftType, rightType);
        maximumWidth = (scale > 0) ? precision + 3 : precision + 1;
        /*
			** Be careful not to overflow
			*/
        if (maximumWidth < precision) {
            maximumWidth = Integer.MAX_VALUE;
        }
    } else {
        scale = 0;
        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);
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Example 4 with TypeId

use of org.apache.derby.iapi.types.TypeId in project derby by apache.

the class ResultColumnList method unionCompatible.

/**
 * Return true if the types of two expressions are union compatible. The rules for union
 * compatibility are found in the SQL Standard, part 2, section 7.3 (<query expression>),
 * syntax rule 20.b.ii. That in turn, refers you to section 9.3 (Result of data type combinations).
 * See, for instance, <a href="https://issues.apache.org/jira/browse/DERBY-4692">DERBY-4692</a>.
 *
 * This logic may enforce only a weaker set of rules. Here is the original comment
 * on the original logic: "We want to make sure that the types are assignable in either direction
 * and they are comparable." We may need to revisit this code to make it conform to the
 * Standard.
 */
private boolean unionCompatible(ValueNode left, ValueNode right) throws StandardException {
    TypeId leftTypeId = left.getTypeId();
    TypeId rightTypeId = right.getTypeId();
    ClassFactory cf = getClassFactory();
    if (!left.getTypeCompiler().storable(rightTypeId, cf) && !right.getTypeCompiler().storable(leftTypeId, cf)) {
        return false;
    }
    if (leftTypeId.isBooleanTypeId() != rightTypeId.isBooleanTypeId()) {
        return false;
    }
    return true;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) ClassFactory(org.apache.derby.iapi.services.loader.ClassFactory)

Example 5 with TypeId

use of org.apache.derby.iapi.types.TypeId in project derby by apache.

the class RowResultSetNode method setTableConstructorTypes.

/**
 * Set the type of each parameter in the result column list for this table constructor.
 *
 * @param typeColumns	The ResultColumnList containing the desired result
 *						types.
 *
 * @exception StandardException		Thrown on error
 */
@Override
void setTableConstructorTypes(ResultColumnList typeColumns) throws StandardException {
    if (SanityManager.DEBUG)
        SanityManager.ASSERT(getResultColumns().visibleSize() <= typeColumns.size(), "More columns in ResultColumnList than in base table");
    /* Look for ? parameters in the result column list */
    int rclSize = getResultColumns().size();
    for (int index = 0; index < rclSize; index++) {
        ResultColumn rc = getResultColumns().elementAt(index);
        ValueNode re = rc.getExpression();
        if (re.requiresTypeFromContext()) {
            ResultColumn typeCol = typeColumns.elementAt(index);
            /*
				** We found a ? - set its type to the type of the
				** corresponding column of the target table.
				*/
            re.setType(typeCol.getTypeServices());
        } else if (re instanceof CharConstantNode) {
            // Character constants are of type CHAR (fixed length string).
            // This causes a problem (beetle 5160) when multiple row values are provided
            // as constants for insertion into a variable length string column.
            // 
            // This issue is the query expression
            // VALUES 'abc', 'defghi'
            // has type of CHAR(6), ie. the length of largest row value for that column.
            // This is from the UNION defined behaviour.
            // This causes strings with less than the maximum length to be blank padded
            // to that length (CHAR semantics). Thus if this VALUES clause is used to
            // insert into a variable length string column, then these blank padded values
            // are inserted, which is not what is required ...
            // 
            // BECAUSE, when the VALUES is used as a table constructor SQL standard says the
            // types of the table constructor's columns are set by the table's column types.
            // Thus, in this case, each of those string constants should be of type VARCHAR
            // (or the matching string type for the table).
            // 
            // 
            // This is only an issue for fixed length character (CHAR, BIT) string or
            // binary consraints being inserted into variable length types.
            // This is because any other type's fundemental literal value is not affected
            // by its data type. E.g. Numeric types such as INT, REAL, BIGINT, DECIMAL etc.
            // do not have their value modifed by the union since even if the type is promoted
            // to a higher type, its fundemental value remains unchanged.
            // values (1.2, 34.4567, 234.47) will be promoted to
            // values (1.2000, 34.4567, 234.4700)
            // but their numeric value remains the same.
            // 
            // 
            // 
            // The fix is to change the base type of the table constructor's value to
            // match the column type. Its length can be left as-is, because there is
            // still a normailzation step when the value is inserted into the table.
            // That will set the correct length and perform truncation checks etc.
            ResultColumn typeCol = typeColumns.elementAt(index);
            TypeId colTypeId = typeCol.getTypeId();
            if (colTypeId.isStringTypeId()) {
                if (colTypeId.getJDBCTypeId() != java.sql.Types.CHAR) {
                    int maxWidth = re.getTypeServices().getMaximumWidth();
                    re.setType(new DataTypeDescriptor(colTypeId, true, maxWidth));
                }
            } else if (colTypeId.isBitTypeId()) {
                if (colTypeId.getJDBCTypeId() == java.sql.Types.VARBINARY) {
                    // then we're trying to cast a char literal into a
                    // variable bit column.  We can't change the base
                    // type of the table constructor's value from char
                    // to bit, so instead, we just change the base type
                    // of that value from char to varchar--that way,
                    // no padding will be added when we convert to
                    // bits later on (Beetle 5306).
                    TypeId tId = TypeId.getBuiltInTypeId(java.sql.Types.VARCHAR);
                    re.setType(new DataTypeDescriptor(tId, true));
                    typeColumns.setElementAt(typeCol, index);
                } else if (colTypeId.getJDBCTypeId() == java.sql.Types.LONGVARBINARY) {
                    TypeId tId = TypeId.getBuiltInTypeId(java.sql.Types.LONGVARCHAR);
                    re.setType(new DataTypeDescriptor(tId, true));
                    typeColumns.setElementAt(typeCol, index);
                }
            }
        } else if (re instanceof BitConstantNode) {
            ResultColumn typeCol = typeColumns.elementAt(index);
            TypeId colTypeId = typeCol.getTypeId();
            if (colTypeId.isBitTypeId()) {
                // columns (they have to be explicitly casted first); beetle 5266.
                if ((colTypeId.getJDBCTypeId() != java.sql.Types.BINARY) && (colTypeId.getJDBCTypeId() != java.sql.Types.BLOB)) {
                    int maxWidth = re.getTypeServices().getMaximumWidth();
                    re.setType(new DataTypeDescriptor(colTypeId, true, maxWidth));
                }
            } else if (colTypeId.isStringTypeId()) {
                if (colTypeId.getJDBCTypeId() == java.sql.Types.VARCHAR) {
                    // then we're trying to cast a bit literal into a
                    // variable char column.  We can't change the base
                    // type of the table constructor's value from bit
                    // to char, so instead, we just change the base
                    // type of that value from bit to varbit--that way,
                    // no padding will be added when we convert to
                    // char later on.
                    TypeId tId = TypeId.getBuiltInTypeId(java.sql.Types.VARBINARY);
                    re.setType(new DataTypeDescriptor(tId, true));
                    typeColumns.setElementAt(typeCol, index);
                } else if (colTypeId.getJDBCTypeId() == java.sql.Types.LONGVARCHAR) {
                    TypeId tId = TypeId.getBuiltInTypeId(java.sql.Types.LONGVARBINARY);
                    re.setType(new DataTypeDescriptor(tId, true));
                    typeColumns.setElementAt(typeCol, index);
                }
            }
        }
    }
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor)

Aggregations

TypeId (org.apache.derby.iapi.types.TypeId)53 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)32 TypeCompiler (org.apache.derby.iapi.sql.compile.TypeCompiler)8 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)6 ClassFactory (org.apache.derby.iapi.services.loader.ClassFactory)4 UserDefinedTypeIdImpl (org.apache.derby.catalog.types.UserDefinedTypeIdImpl)3 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)3 StandardException (org.apache.derby.shared.common.error.StandardException)3 TypeDescriptor (org.apache.derby.catalog.TypeDescriptor)2 DefaultInfoImpl (org.apache.derby.catalog.types.DefaultInfoImpl)2 RoutineAliasInfo (org.apache.derby.catalog.types.RoutineAliasInfo)2 ClassInspector (org.apache.derby.iapi.services.loader.ClassInspector)2 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)2 ProviderList (org.apache.derby.iapi.sql.depend.ProviderList)2 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)2 JSQLType (org.apache.derby.iapi.types.JSQLType)2 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Properties (java.util.Properties)1