Search in sources :

Example 1 with TypeCompilerFactory

use of org.apache.derby.iapi.sql.compile.TypeCompilerFactory in project derby by apache.

the class MethodCallNode method getObjectSignature.

/**
 *	Build an array of names of the argument types. These types are biased toward
 *	Java objects. That is, if an argument is of SQLType, then we map it to the
 *	corresponding Java synonym class (e.g., SQLINT is mapped to 'java.lang.Integer').
 *
 *	@return	array of type names
 *
 * @exception StandardException		Thrown on error
 */
protected String[] getObjectSignature() throws StandardException {
    int count = signature.length;
    String[] parmTypeNames = new String[count];
    TypeCompilerFactory tcf = (routineInfo == null) ? null : getCompilerContext().getTypeCompilerFactory();
    for (int i = 0; i < count; i++) {
        parmTypeNames[i] = getObjectTypeName(signature[i], tcf);
    }
    return parmTypeNames;
}
Also used : TypeCompilerFactory(org.apache.derby.iapi.sql.compile.TypeCompilerFactory)

Example 2 with TypeCompilerFactory

use of org.apache.derby.iapi.sql.compile.TypeCompilerFactory in project derby by apache.

the class SumAvgAggregateDefinition method getAggregator.

/**
 * Determines the result datatype.  Accept NumberDataValues
 * only.
 * <P>
 * <I>Note</I>: In the future you should be able to do
 * a sum user data types.  One option would be to run
 * sum on anything that implements plus().  In which
 * case avg() would need divide().
 *
 * @param inputType	the input type, either a user type or a java.lang object
 *
 * @return the output Class (null if cannot operate on
 *	value expression of this type.
 */
public final DataTypeDescriptor getAggregator(DataTypeDescriptor inputType, StringBuffer aggregatorClass) {
    try {
        TypeId compType = inputType.getTypeId();
        CompilerContext cc = (CompilerContext) QueryTreeNode.getContext(CompilerContext.CONTEXT_ID);
        TypeCompilerFactory tcf = cc.getTypeCompilerFactory();
        TypeCompiler tc = tcf.getTypeCompiler(compType);
        /*
			** If the class implements NumberDataValue, then we
			** are in business.  Return type is same as input
			** type.
			*/
        if (compType.isNumericTypeId()) {
            aggregatorClass.append(getAggregatorClassName());
            DataTypeDescriptor outDts = tc.resolveArithmeticOperation(inputType, inputType, getOperator());
            /*
				** SUM and AVG may return null
				*/
            return outDts.getNullabilityType(true);
        }
    } catch (StandardException e) {
        if (SanityManager.DEBUG) {
            SanityManager.THROWASSERT("Unexpected exception", e);
        }
    }
    return null;
}
Also used : TypeId(org.apache.derby.iapi.types.TypeId) StandardException(org.apache.derby.shared.common.error.StandardException) TypeCompilerFactory(org.apache.derby.iapi.sql.compile.TypeCompilerFactory) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) TypeCompiler(org.apache.derby.iapi.sql.compile.TypeCompiler)

Example 3 with TypeCompilerFactory

use of org.apache.derby.iapi.sql.compile.TypeCompilerFactory in project derby by apache.

the class UserAggregateDefinition method getAggregator.

/**
 * Determines the result datatype and verifies that the input datatype is correct.
 *
 * @param inputType	the input type
 * @param aggregatorClass (Output arg) the name of the Derby execution-time class which wraps the aggregate logic
 *
 * @return the result type of the user-defined aggregator
 */
public final DataTypeDescriptor getAggregator(DataTypeDescriptor inputType, StringBuffer aggregatorClass) throws StandardException {
    try {
        CompilerContext cc = (CompilerContext) QueryTreeNode.getContext(CompilerContext.CONTEXT_ID);
        ClassFactory classFactory = cc.getClassFactory();
        TypeCompilerFactory tcf = cc.getTypeCompilerFactory();
        Class<?> derbyAggregatorInterface = classFactory.loadApplicationClass("org.apache.derby.agg.Aggregator");
        Class<?> userAggregatorClass = classFactory.loadApplicationClass(_alias.getJavaClassName());
        Class[][] typeBounds = classFactory.getClassInspector().getTypeBounds(derbyAggregatorInterface, userAggregatorClass);
        if ((typeBounds == null) || (typeBounds.length != AGGREGATOR_PARAM_COUNT) || (typeBounds[INPUT_TYPE] == null) || (typeBounds[RETURN_TYPE] == null)) {
            throw StandardException.newException(SQLState.LANG_ILLEGAL_UDA_CLASS, _alias.getSchemaName(), _alias.getName(), userAggregatorClass.getName());
        }
        Class<?>[] genericParameterTypes = classFactory.getClassInspector().getGenericParameterTypes(derbyAggregatorInterface, userAggregatorClass);
        if (genericParameterTypes == null) {
            genericParameterTypes = new Class<?>[AGGREGATOR_PARAM_COUNT];
        }
        AggregateAliasInfo aai = (AggregateAliasInfo) _alias.getAliasInfo();
        DataTypeDescriptor expectedInputType = DataTypeDescriptor.getType(aai.getForType());
        DataTypeDescriptor expectedReturnType = DataTypeDescriptor.getType(aai.getReturnType());
        Class<?> expectedInputClass = getJavaClass(classFactory, expectedInputType);
        Class<?> expectedReturnClass = getJavaClass(classFactory, expectedReturnType);
        // the input operand must be coercible to the expected input type of the aggregate
        if (!tcf.getTypeCompiler(expectedInputType.getTypeId()).storable(inputType.getTypeId(), classFactory)) {
            return null;
        }
        // 
        // Make sure that the declared input type of the UDA actually falls within
        // the type bounds of the Aggregator implementation.
        // 
        Class[] inputBounds = typeBounds[INPUT_TYPE];
        for (int i = 0; i < inputBounds.length; i++) {
            vetCompatibility((Class<?>) inputBounds[i], expectedInputClass, SQLState.LANG_UDA_WRONG_INPUT_TYPE);
        }
        if (genericParameterTypes[INPUT_TYPE] != null) {
            vetCompatibility(genericParameterTypes[INPUT_TYPE], expectedInputClass, SQLState.LANG_UDA_WRONG_INPUT_TYPE);
        }
        // 
        // Make sure that the declared return type of the UDA actually falls within
        // the type bounds of the Aggregator implementation.
        // 
        Class[] returnBounds = typeBounds[RETURN_TYPE];
        for (int i = 0; i < returnBounds.length; i++) {
            vetCompatibility(returnBounds[i], expectedReturnClass, SQLState.LANG_UDA_WRONG_RETURN_TYPE);
        }
        if (genericParameterTypes[RETURN_TYPE] != null) {
            vetCompatibility(genericParameterTypes[RETURN_TYPE], expectedReturnClass, SQLState.LANG_UDA_WRONG_RETURN_TYPE);
        }
        aggregatorClass.append(ClassName.UserDefinedAggregator);
        return expectedReturnType;
    } catch (ClassNotFoundException cnfe) {
        throw aggregatorInstantiation(cnfe);
    }
}
Also used : ClassFactory(org.apache.derby.iapi.services.loader.ClassFactory) TypeCompilerFactory(org.apache.derby.iapi.sql.compile.TypeCompilerFactory) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) AggregateAliasInfo(org.apache.derby.catalog.types.AggregateAliasInfo)

Aggregations

TypeCompilerFactory (org.apache.derby.iapi.sql.compile.TypeCompilerFactory)3 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)2 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)2 AggregateAliasInfo (org.apache.derby.catalog.types.AggregateAliasInfo)1 ClassFactory (org.apache.derby.iapi.services.loader.ClassFactory)1 TypeCompiler (org.apache.derby.iapi.sql.compile.TypeCompiler)1 TypeId (org.apache.derby.iapi.types.TypeId)1 StandardException (org.apache.derby.shared.common.error.StandardException)1