Search in sources :

Example 1 with IResultTypeComputer

use of org.apache.asterix.om.typecomputer.base.IResultTypeComputer in project asterixdb by apache.

the class ExpressionTypeComputer method getTypeForFunction.

private IAType getTypeForFunction(AbstractFunctionCallExpression expr, IVariableTypeEnvironment env, IMetadataProvider<?, ?> mp) throws AlgebricksException {
    FunctionIdentifier fi = expr.getFunctionIdentifier();
    // Note: built-in functions + udfs
    IResultTypeComputer rtc;
    FunctionSignature signature = new FunctionSignature(fi.getNamespace(), fi.getName(), fi.getArity());
    if (BuiltinFunctions.isBuiltinCompilerFunction(signature, true)) {
        rtc = BuiltinFunctions.getResultTypeComputer(fi);
    } else {
        rtc = ((ExternalFunctionInfo) expr.getFunctionInfo()).getResultTypeComputer();
    }
    if (rtc == null) {
        throw new AlgebricksException("Type computer missing for " + fi);
    }
    return rtc.computeType(expr, env, mp);
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Example 2 with IResultTypeComputer

use of org.apache.asterix.om.typecomputer.base.IResultTypeComputer in project asterixdb by apache.

the class ExceptionTest method testTypeComputer.

private void testTypeComputer(Class<? extends IResultTypeComputer> c) throws Exception {
    // Mocks the type environment.
    IVariableTypeEnvironment mockTypeEnv = mock(IVariableTypeEnvironment.class);
    // Mocks the metadata provider.
    IMetadataProvider<?, ?> mockMetadataProvider = mock(IMetadataProvider.class);
    // Mocks function expression.
    AbstractFunctionCallExpression mockExpr = mock(AbstractFunctionCallExpression.class);
    FunctionIdentifier fid = mock(FunctionIdentifier.class);
    when(mockExpr.getFunctionIdentifier()).thenReturn(fid);
    when(fid.getName()).thenReturn("testFunction");
    int numCombination = (int) Math.pow(ATypeTag.values().length, 2);
    // Sets two arguments for the mocked function expression.
    for (int index = 0; index < numCombination; ++index) {
        try {
            List<Mutable<ILogicalExpression>> argRefs = new ArrayList<>();
            for (int argIndex = 0; argIndex < 2; ++argIndex) {
                int base = (int) Math.pow(ATypeTag.values().length, argIndex);
                ILogicalExpression mockArg = mock(ILogicalExpression.class);
                argRefs.add(new MutableObject<>(mockArg));
                IAType mockType = mock(IAType.class);
                when(mockTypeEnv.getType(mockArg)).thenReturn(mockType);
                int serializedTypeTag = (index / base) % ATypeTag.values().length + 1;
                ATypeTag typeTag = ATypeTag.VALUE_TYPE_MAPPING[serializedTypeTag];
                if (typeTag == null) {
                    // For some reason, type tag 39 does not exist.
                    typeTag = ATypeTag.ANY;
                }
                when(mockType.getTypeTag()).thenReturn(typeTag);
            }
            // Sets up arguments for the mocked expression.
            when(mockExpr.getArguments()).thenReturn(argRefs);
            // Sets up required/actual types of the mocked expression.
            Object[] opaqueParameters = new Object[2];
            opaqueParameters[0] = BuiltinType.ANY;
            opaqueParameters[1] = BuiltinType.ANY;
            when(mockExpr.getOpaqueParameters()).thenReturn(opaqueParameters);
            // Invokes a type computer.
            IResultTypeComputer instance = (IResultTypeComputer) c.getField("INSTANCE").get(null);
            instance.computeType(mockExpr, mockTypeEnv, mockMetadataProvider);
        } catch (AlgebricksException ae) {
            String msg = ae.getMessage();
            if (msg.startsWith("ASX")) {
                // Verifies the error code.
                int errorCode = Integer.parseInt(msg.substring(3, 7));
                Assert.assertTrue(errorCode >= 1000 && errorCode < 2000);
                continue;
            } else {
                // Any root-level compilation exceptions thrown from type computers should have an error code.
                Assert.assertTrue(!(ae instanceof AlgebricksException) || (ae.getCause() != null));
            }
        } catch (ClassCastException e) {
            continue;
        }
    }
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ATypeTag(org.apache.asterix.om.types.ATypeTag) MutableObject(org.apache.commons.lang3.mutable.MutableObject) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) IAType(org.apache.asterix.om.types.IAType)

Example 3 with IResultTypeComputer

use of org.apache.asterix.om.typecomputer.base.IResultTypeComputer in project asterixdb by apache.

the class ExceptionTest method test.

@Test
public void test() throws Exception {
    // Tests all usual type computers.
    Reflections reflections = new Reflections("org.apache.asterix.om.typecomputer", new SubTypesScanner(false));
    Set<Class<? extends IResultTypeComputer>> classes = reflections.getSubTypesOf(IResultTypeComputer.class);
    int numTypeComputers = 0;
    for (Class<? extends IResultTypeComputer> c : classes) {
        if (Modifier.isAbstract(c.getModifiers())) {
            continue;
        }
        testTypeComputer(c);
        ++numTypeComputers;
    }
    // Currently, there are 83 type computers.
    Assert.assertTrue(numTypeComputers >= 83);
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) SubTypesScanner(org.reflections.scanners.SubTypesScanner) Reflections(org.reflections.Reflections) Test(org.junit.Test)

Example 4 with IResultTypeComputer

use of org.apache.asterix.om.typecomputer.base.IResultTypeComputer in project asterixdb by apache.

the class TypeComputerTest method testTypeComputer.

private boolean testTypeComputer(Class<? extends IResultTypeComputer> c) throws Exception {
    // Mocks the type environment.
    IVariableTypeEnvironment mockTypeEnv = mock(IVariableTypeEnvironment.class);
    // Mocks the metadata provider.
    IMetadataProvider<?, ?> mockMetadataProvider = mock(IMetadataProvider.class);
    // Mocks function expression.
    AbstractFunctionCallExpression mockExpr = mock(AbstractFunctionCallExpression.class);
    FunctionIdentifier fid = mock(FunctionIdentifier.class);
    when(mockExpr.getFunctionIdentifier()).thenReturn(fid);
    when(fid.getName()).thenReturn("testFunction");
    // A function at most has six argument.
    List<Mutable<ILogicalExpression>> argRefs = new ArrayList<>();
    for (int argIndex = 0; argIndex < 6; ++argIndex) {
        ILogicalExpression mockArg = mock(ILogicalExpression.class);
        argRefs.add(new MutableObject<>(mockArg));
        when(mockTypeEnv.getType(mockArg)).thenReturn(BuiltinType.ANY);
    }
    // Sets up arguments for the mocked expression.
    when(mockExpr.getArguments()).thenReturn(argRefs);
    // Sets up required/actual types of the mocked expression.
    Object[] opaqueParameters = new Object[2];
    opaqueParameters[0] = BuiltinType.ANY;
    opaqueParameters[1] = BuiltinType.ANY;
    when(mockExpr.getOpaqueParameters()).thenReturn(opaqueParameters);
    // Tests the return type. It should be either ANY or NULLABLE/MISSABLE.
    IResultTypeComputer instance = (IResultTypeComputer) c.getField("INSTANCE").get(null);
    IAType resultType = instance.computeType(mockExpr, mockTypeEnv, mockMetadataProvider);
    ATypeTag typeTag = resultType.getTypeTag();
    if (typeTag == ATypeTag.ANY) {
        return true;
    }
    if (typeTag == ATypeTag.UNION) {
        AUnionType unionType = (AUnionType) resultType;
        return unionType.isMissableType() && unionType.isNullableType();
    }
    return false;
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) AUnionType(org.apache.asterix.om.types.AUnionType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ATypeTag(org.apache.asterix.om.types.ATypeTag) MutableObject(org.apache.commons.lang3.mutable.MutableObject) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) IAType(org.apache.asterix.om.types.IAType)

Example 5 with IResultTypeComputer

use of org.apache.asterix.om.typecomputer.base.IResultTypeComputer in project asterixdb by apache.

the class TypeComputerTest method test.

@Test
public void test() throws Exception {
    // Several exceptional type computers.
    Set<String> exceptionalTypeComputers = new HashSet<>();
    exceptionalTypeComputers.add("InjectFailureTypeComputer");
    exceptionalTypeComputers.add("RecordAddFieldsTypeComputer");
    exceptionalTypeComputers.add("OpenRecordConstructorResultType");
    exceptionalTypeComputers.add("RecordRemoveFieldsTypeComputer");
    exceptionalTypeComputers.add("ClosedRecordConstructorResultType");
    exceptionalTypeComputers.add("LocalAvgTypeComputer");
    exceptionalTypeComputers.add("BooleanOnlyTypeComputer");
    exceptionalTypeComputers.add("AMissingTypeComputer");
    exceptionalTypeComputers.add("NullableDoubleTypeComputer");
    exceptionalTypeComputers.add("RecordMergeTypeComputer");
    exceptionalTypeComputers.add("BooleanOrMissingTypeComputer");
    // Tests all usual type computers.
    Reflections reflections = new Reflections("org.apache.asterix.om.typecomputer", new SubTypesScanner(false));
    Set<Class<? extends IResultTypeComputer>> classes = reflections.getSubTypesOf(IResultTypeComputer.class);
    for (Class<? extends IResultTypeComputer> c : classes) {
        if (exceptionalTypeComputers.contains(c.getSimpleName()) || Modifier.isAbstract(c.getModifiers())) {
            continue;
        }
        System.out.println("Test type computer: " + c.getName());
        Assert.assertTrue(testTypeComputer(c));
    }
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) SubTypesScanner(org.reflections.scanners.SubTypesScanner) HashSet(java.util.HashSet) Reflections(org.reflections.Reflections) Test(org.junit.Test)

Aggregations

IResultTypeComputer (org.apache.asterix.om.typecomputer.base.IResultTypeComputer)7 IAType (org.apache.asterix.om.types.IAType)4 FunctionIdentifier (org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier)4 ArrayList (java.util.ArrayList)3 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)3 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)3 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)3 IVariableTypeEnvironment (org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)3 ATypeTag (org.apache.asterix.om.types.ATypeTag)2 Mutable (org.apache.commons.lang3.mutable.Mutable)2 MutableObject (org.apache.commons.lang3.mutable.MutableObject)2 Test (org.junit.Test)2 Reflections (org.reflections.Reflections)2 SubTypesScanner (org.reflections.scanners.SubTypesScanner)2 HashSet (java.util.HashSet)1 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)1 MetadataProvider (org.apache.asterix.metadata.declared.MetadataProvider)1 Dataset (org.apache.asterix.metadata.entities.Dataset)1 AUnionType (org.apache.asterix.om.types.AUnionType)1 Pair (org.apache.hyracks.algebricks.common.utils.Pair)1