Search in sources :

Example 26 with AUnionType

use of org.apache.asterix.om.types.AUnionType 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 27 with AUnionType

use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.

the class SubsetCollectionTypeComputer method computeType.

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> mp) throws AlgebricksException {
    AbstractFunctionCallExpression fun = (AbstractFunctionCallExpression) expression;
    String funcName = fun.getFunctionIdentifier().getName();
    IAType t = (IAType) env.getType(fun.getArguments().get(0).getValue());
    ATypeTag actualTypeTag = t.getTypeTag();
    switch(actualTypeTag) {
        case MULTISET:
        case ARRAY:
            {
                AbstractCollectionType act = (AbstractCollectionType) t;
                return act.getItemType();
            }
        case UNION:
            {
                AUnionType ut = (AUnionType) t;
                if (!ut.isUnknownableType()) {
                    throw new TypeMismatchException(funcName, 0, actualTypeTag, ATypeTag.MULTISET, ATypeTag.ARRAY);
                }
                IAType t2 = ut.getActualType();
                ATypeTag tag2 = t2.getTypeTag();
                if (tag2 == ATypeTag.MULTISET || tag2 == ATypeTag.ARRAY) {
                    AbstractCollectionType act = (AbstractCollectionType) t2;
                    return act.getItemType();
                }
                throw new TypeMismatchException(funcName, 0, actualTypeTag, ATypeTag.MULTISET, ATypeTag.ARRAY);
            }
        case ANY:
            return BuiltinType.ANY;
        default:
            throw new TypeMismatchException(funcName, 0, actualTypeTag, ATypeTag.MULTISET, ATypeTag.ARRAY);
    }
}
Also used : ATypeTag(org.apache.asterix.om.types.ATypeTag) AUnionType(org.apache.asterix.om.types.AUnionType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) TypeMismatchException(org.apache.asterix.om.exceptions.TypeMismatchException) AbstractCollectionType(org.apache.asterix.om.types.AbstractCollectionType) IAType(org.apache.asterix.om.types.IAType)

Example 28 with AUnionType

use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.

the class TypeComputeUtils method isUnknownableType.

private static boolean[] isUnknownableType(IAType... inputTypes) {
    boolean[] unknownable = new boolean[inputTypes.length];
    for (int index = 0; index < unknownable.length; ++index) {
        IAType type = inputTypes[index];
        unknownable[index] = false;
        if (type.getTypeTag() != ATypeTag.UNION) {
            continue;
        } else {
            AUnionType unionType = (AUnionType) type;
            unknownable[index] = unionType.isUnknownableType();
        }
    }
    return unknownable;
}
Also used : AUnionType(org.apache.asterix.om.types.AUnionType) IAType(org.apache.asterix.om.types.IAType)

Example 29 with AUnionType

use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.

the class TypeComputeUtils method extractUnorderedListType.

public static AUnorderedListType extractUnorderedListType(IAType t) {
    if (t.getTypeTag() == ATypeTag.MULTISET) {
        return (AUnorderedListType) t;
    }
    if (t.getTypeTag() == ATypeTag.UNION) {
        AUnionType unionType = (AUnionType) t;
        IAType innerType = unionType.getActualType();
        if (innerType.getTypeTag() == ATypeTag.MULTISET) {
            return (AUnorderedListType) innerType;
        }
    }
    return null;
}
Also used : AUnionType(org.apache.asterix.om.types.AUnionType) AUnorderedListType(org.apache.asterix.om.types.AUnorderedListType) IAType(org.apache.asterix.om.types.IAType)

Example 30 with AUnionType

use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.

the class NonTaggedFormatUtil method hasOptionalField.

public static final boolean hasOptionalField(ARecordType recType) {
    for (int i = 0; i < recType.getFieldTypes().length; i++) {
        IAType type = recType.getFieldTypes()[i];
        if (type != null) {
            ATypeTag tag = type.getTypeTag();
            if (tag == ATypeTag.NULL || tag == ATypeTag.MISSING) {
                return true;
            }
            if (tag != ATypeTag.UNION) {
                continue;
            }
            // union
            AUnionType unionType = (AUnionType) type;
            if (unionType.isUnknownableType()) {
                return true;
            }
        }
    }
    return false;
}
Also used : ATypeTag(org.apache.asterix.om.types.ATypeTag) AUnionType(org.apache.asterix.om.types.AUnionType) IAType(org.apache.asterix.om.types.IAType)

Aggregations

AUnionType (org.apache.asterix.om.types.AUnionType)32 IAType (org.apache.asterix.om.types.IAType)31 ATypeTag (org.apache.asterix.om.types.ATypeTag)13 ARecordType (org.apache.asterix.om.types.ARecordType)11 ArrayList (java.util.ArrayList)10 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)7 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)6 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)5 AUnorderedListType (org.apache.asterix.om.types.AUnorderedListType)5 Mutable (org.apache.commons.lang3.mutable.Mutable)5 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)5 DataOutput (java.io.DataOutput)4 AString (org.apache.asterix.om.base.AString)4 IOException (java.io.IOException)3 List (java.util.List)3 RuntimeDataException (org.apache.asterix.common.exceptions.RuntimeDataException)3 IVisitablePointable (org.apache.asterix.om.pointables.base.IVisitablePointable)3 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)3 HashSet (java.util.HashSet)2 AsterixException (org.apache.asterix.common.exceptions.AsterixException)2