Search in sources :

Example 1 with AbstractCollectionType

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

the class StaticTypeCastUtil method rewriteFuncExpr.

/**
     * This method is to recursively enforce required types, for the list type and the record type.
     * The List constructor is very special because
     * 1. a nested list in a list is of type List<ANY>;
     * 2. a nested record in a list is of type Open_Record{}.
     * The open record constructor is very special because
     * 1. a nested list in the open part is of type List<ANY>;
     * 2. a nested record in the open part is of type Open_Record{}.
     * However, the bottom-up type inference (InferTypeRule in algebricks) did not infer that so we need this method to enforce the type.
     * We do not want to break the generality of algebricks so this method is called in an ASTERIX rule: @ IntroduceStaticTypeCastRule} .
     *
     * @param funcExpr
     *            the function expression whose type needs to be top-down enforced
     * @param reqType
     *            the required type inferred from parent operators/expressions
     * @param inputType
     *            the current inferred
     * @param env
     *            the type environment
     * @return true if the type is casted; otherwise, false.
     * @throws AlgebricksException
     */
public static boolean rewriteFuncExpr(AbstractFunctionCallExpression funcExpr, IAType reqType, IAType inputType, IVariableTypeEnvironment env) throws AlgebricksException {
    /**
         * sanity check: if there are list(ordered or unordered)/record variable expressions in the funcExpr, we will not do STATIC type casting
         * because they are not "statically cast-able".
         * instead, the record will be dynamically casted at the runtime
         */
    if (funcExpr.getFunctionIdentifier() == BuiltinFunctions.UNORDERED_LIST_CONSTRUCTOR) {
        if (reqType.equals(BuiltinType.ANY)) {
            reqType = DefaultOpenFieldType.NESTED_OPEN_AUNORDERED_LIST_TYPE;
        }
        return rewriteListFuncExpr(funcExpr, (AbstractCollectionType) reqType, (AbstractCollectionType) inputType, env);
    } else if (funcExpr.getFunctionIdentifier() == BuiltinFunctions.ORDERED_LIST_CONSTRUCTOR) {
        if (reqType.equals(BuiltinType.ANY)) {
            reqType = DefaultOpenFieldType.NESTED_OPEN_AORDERED_LIST_TYPE;
        }
        return rewriteListFuncExpr(funcExpr, (AbstractCollectionType) reqType, (AbstractCollectionType) inputType, env);
    } else if (inputType.getTypeTag().equals(ATypeTag.OBJECT)) {
        if (reqType.equals(BuiltinType.ANY)) {
            reqType = DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE;
        }
        return rewriteRecordFuncExpr(funcExpr, (ARecordType) reqType, (ARecordType) inputType, env);
    } else {
        List<Mutable<ILogicalExpression>> args = funcExpr.getArguments();
        boolean changed = false;
        for (Mutable<ILogicalExpression> arg : args) {
            ILogicalExpression argExpr = arg.getValue();
            if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                AbstractFunctionCallExpression argFuncExpr = (AbstractFunctionCallExpression) argExpr;
                IAType exprType = (IAType) env.getType(argFuncExpr);
                changed = changed || rewriteFuncExpr(argFuncExpr, exprType, exprType, env);
            }
        }
        if (!compatible(reqType, inputType)) {
            throw new AlgebricksException("type mismatch, required: " + reqType.toString() + " actual: " + inputType.toString());
        }
        return changed;
    }
}
Also used : Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AbstractCollectionType(org.apache.asterix.om.types.AbstractCollectionType) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) IAType(org.apache.asterix.om.types.IAType)

Example 2 with AbstractCollectionType

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

the class TypeTranslator method addIncompleteCollectionTypeReference.

private static void addIncompleteCollectionTypeReference(AbstractCollectionType collType, TypeReferenceExpression tre, Map<TypeSignature, List<AbstractCollectionType>> incompleteItemTypes, String defaultDataverse) {
    String typeName = tre.getIdent().second.getValue();
    TypeSignature typeSignature = new TypeSignature(tre.getIdent().first == null ? defaultDataverse : tre.getIdent().first.getValue(), typeName);
    List<AbstractCollectionType> typeList = incompleteItemTypes.get(typeSignature);
    if (typeList == null) {
        typeList = new LinkedList<>();
        incompleteItemTypes.put(typeSignature, typeList);
    }
    typeList.add(collType);
}
Also used : TypeSignature(org.apache.asterix.om.types.TypeSignature) AbstractCollectionType(org.apache.asterix.om.types.AbstractCollectionType)

Example 3 with AbstractCollectionType

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

the class NonTaggedFormatUtil method getTokenType.

public static IAType getTokenType(IAType keyType) throws AlgebricksException {
    IAType type = keyType;
    ATypeTag typeTag = keyType.getTypeTag();
    // Extract item type from list.
    if (typeTag == ATypeTag.MULTISET || typeTag == ATypeTag.ARRAY) {
        AbstractCollectionType listType = (AbstractCollectionType) keyType;
        if (!listType.isTyped()) {
            throw new AlgebricksException("Cannot build an inverted index on untyped lists.)");
        }
        type = listType.getItemType();
    }
    return type;
}
Also used : ATypeTag(org.apache.asterix.om.types.ATypeTag) AbstractCollectionType(org.apache.asterix.om.types.AbstractCollectionType) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) IAType(org.apache.asterix.om.types.IAType)

Example 4 with AbstractCollectionType

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

the class NonTaggedGetItemResultType method getResultType.

@Override
protected IAType getResultType(ILogicalExpression expr, IAType... strippedInputTypes) throws AlgebricksException {
    IAType type = strippedInputTypes[0];
    if (type.getTypeTag() == ATypeTag.ANY) {
        return BuiltinType.ANY;
    }
    IAType itemType = ((AbstractCollectionType) type).getItemType();
    if (itemType.getTypeTag() == ATypeTag.ANY) {
        return itemType;
    }
    // Could have out-of-bound access or null elements.
    return AUnionType.createUnknownableType(itemType);
}
Also used : AbstractCollectionType(org.apache.asterix.om.types.AbstractCollectionType) IAType(org.apache.asterix.om.types.IAType)

Example 5 with AbstractCollectionType

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

the class TypeResolverUtilTest method testOrderedListType.

@Test
public void testOrderedListType() {
    // Constructs input types.
    ARecordType leftRecordType = new ARecordType("null", new String[] { "a", "b" }, new IAType[] { BuiltinType.ASTRING, BuiltinType.AINT32 }, true, Collections.singleton("d"));
    AOrderedListType leftListType = new AOrderedListType(leftRecordType, "null");
    ARecordType rightRecordType = new ARecordType("null", new String[] { "b", "c" }, new IAType[] { BuiltinType.AINT32, BuiltinType.ABINARY }, true, Collections.singleton("e"));
    AOrderedListType rightListType = new AOrderedListType(rightRecordType, "null");
    // Gets the actual resolved type.
    List<IAType> inputTypes = new ArrayList<>();
    inputTypes.add(leftListType);
    inputTypes.add(rightListType);
    AbstractCollectionType resolvedType = (AbstractCollectionType) TypeResolverUtil.resolve(inputTypes);
    ARecordType resolvedRecordType = (ARecordType) resolvedType.getItemType();
    // Gets the expected generalized type.
    Set<String> possibleAdditionalFields = new HashSet<>();
    possibleAdditionalFields.add("a");
    possibleAdditionalFields.add("c");
    possibleAdditionalFields.add("d");
    possibleAdditionalFields.add("e");
    ARecordType expectedRecordType = new ARecordType(null, new String[] { "b" }, new IAType[] { BuiltinType.AINT32 }, true, possibleAdditionalFields);
    AOrderedListType expectedListType = new AOrderedListType(expectedRecordType, null);
    // Compares the resolved type and the expected type.
    Assert.assertEquals(resolvedType, expectedListType);
    Assert.assertEquals(resolvedRecordType.getAllPossibleAdditonalFieldNames(), expectedRecordType.getAllPossibleAdditonalFieldNames());
}
Also used : AOrderedListType(org.apache.asterix.om.types.AOrderedListType) ArrayList(java.util.ArrayList) AbstractCollectionType(org.apache.asterix.om.types.AbstractCollectionType) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

AbstractCollectionType (org.apache.asterix.om.types.AbstractCollectionType)12 IAType (org.apache.asterix.om.types.IAType)10 ATypeTag (org.apache.asterix.om.types.ATypeTag)4 ArrayList (java.util.ArrayList)3 ARecordType (org.apache.asterix.om.types.ARecordType)3 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)3 AUnionType (org.apache.asterix.om.types.AUnionType)2 TypeSignature (org.apache.asterix.om.types.TypeSignature)2 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)2 Test (org.junit.Test)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 IARecordBuilder (org.apache.asterix.builders.IARecordBuilder)1 OrderedListBuilder (org.apache.asterix.builders.OrderedListBuilder)1 Datatype (org.apache.asterix.metadata.entities.Datatype)1 TypeMismatchException (org.apache.asterix.om.exceptions.TypeMismatchException)1 AListPointable (org.apache.asterix.om.pointables.nonvisitor.AListPointable)1 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)1 AUnorderedListType (org.apache.asterix.om.types.AUnorderedListType)1