Search in sources :

Example 1 with TypeReferenceExpression

use of org.apache.asterix.lang.common.expression.TypeReferenceExpression in project asterixdb by apache.

the class TypeTranslator method computeRecordType.

private static ARecordType computeRecordType(TypeSignature typeSignature, RecordTypeDefinition rtd, Map<TypeSignature, IAType> typeMap, Map<String, Map<ARecordType, List<Integer>>> incompleteFieldTypes, Map<TypeSignature, List<AbstractCollectionType>> incompleteItemTypes, String defaultDataverse) throws AsterixException {
    List<String> names = rtd.getFieldNames();
    int n = names.size();
    String[] fldNames = new String[n];
    IAType[] fldTypes = new IAType[n];
    int i = 0;
    for (String s : names) {
        fldNames[i++] = s;
    }
    boolean isOpen = rtd.getRecordKind() == RecordKind.OPEN;
    ARecordType recType = new ARecordType(typeSignature == null ? null : typeSignature.getName(), fldNames, fldTypes, isOpen);
    List<IRecordFieldDataGen> fieldDataGen = rtd.getFieldDataGen();
    if (fieldDataGen.size() == n) {
        IRecordFieldDataGen[] rfdg = new IRecordFieldDataGen[n];
        rfdg = fieldDataGen.toArray(rfdg);
        recType.getAnnotations().add(new RecordDataGenAnnotation(rfdg, rtd.getUndeclaredFieldsDataGen()));
    }
    for (int j = 0; j < n; j++) {
        TypeExpression texpr = rtd.getFieldTypes().get(j);
        switch(texpr.getTypeKind()) {
            case TYPEREFERENCE:
                {
                    TypeReferenceExpression tre = (TypeReferenceExpression) texpr;
                    TypeSignature signature = new TypeSignature(tre.getIdent().first == null ? defaultDataverse : tre.getIdent().first.getValue(), tre.getIdent().second.getValue());
                    IAType tref = solveTypeReference(signature, typeMap);
                    if (tref != null) {
                        if (!rtd.getOptionableFields().get(j)) {
                            // not nullable
                            fldTypes[j] = tref;
                        } else {
                            // optional
                            fldTypes[j] = AUnionType.createUnknownableType(tref);
                        }
                    } else {
                        addIncompleteFieldTypeReference(recType, j, tre, incompleteFieldTypes);
                        if (rtd.getOptionableFields().get(j)) {
                            fldTypes[j] = AUnionType.createUnknownableType(null);
                        }
                    }
                    break;
                }
            case RECORD:
                {
                    RecordTypeDefinition recTypeDef2 = (RecordTypeDefinition) texpr;
                    IAType t2 = computeRecordType(null, recTypeDef2, typeMap, incompleteFieldTypes, incompleteItemTypes, defaultDataverse);
                    if (!rtd.getOptionableFields().get(j)) {
                        // not nullable
                        fldTypes[j] = t2;
                    } else {
                        // nullable
                        fldTypes[j] = AUnionType.createUnknownableType(t2);
                    }
                    break;
                }
            case ORDEREDLIST:
                {
                    OrderedListTypeDefinition oltd = (OrderedListTypeDefinition) texpr;
                    IAType t2 = computeOrderedListType(null, oltd, typeMap, incompleteItemTypes, incompleteFieldTypes, defaultDataverse);
                    fldTypes[j] = rtd.getOptionableFields().get(j) ? AUnionType.createUnknownableType(t2) : t2;
                    break;
                }
            case UNORDEREDLIST:
                {
                    UnorderedListTypeDefinition ultd = (UnorderedListTypeDefinition) texpr;
                    IAType t2 = computeUnorderedListType(null, ultd, typeMap, incompleteItemTypes, incompleteFieldTypes, defaultDataverse);
                    fldTypes[j] = rtd.getOptionableFields().get(j) ? AUnionType.createUnknownableType(t2) : t2;
                    break;
                }
            default:
                {
                    throw new IllegalStateException();
                }
        }
    }
    return recType;
}
Also used : RecordDataGenAnnotation(org.apache.asterix.common.annotations.RecordDataGenAnnotation) TypeExpression(org.apache.asterix.lang.common.expression.TypeExpression) OrderedListTypeDefinition(org.apache.asterix.lang.common.expression.OrderedListTypeDefinition) TypeSignature(org.apache.asterix.om.types.TypeSignature) TypeReferenceExpression(org.apache.asterix.lang.common.expression.TypeReferenceExpression) RecordTypeDefinition(org.apache.asterix.lang.common.expression.RecordTypeDefinition) UnorderedListTypeDefinition(org.apache.asterix.lang.common.expression.UnorderedListTypeDefinition) IRecordFieldDataGen(org.apache.asterix.common.annotations.IRecordFieldDataGen) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Aggregations

IRecordFieldDataGen (org.apache.asterix.common.annotations.IRecordFieldDataGen)1 RecordDataGenAnnotation (org.apache.asterix.common.annotations.RecordDataGenAnnotation)1 OrderedListTypeDefinition (org.apache.asterix.lang.common.expression.OrderedListTypeDefinition)1 RecordTypeDefinition (org.apache.asterix.lang.common.expression.RecordTypeDefinition)1 TypeExpression (org.apache.asterix.lang.common.expression.TypeExpression)1 TypeReferenceExpression (org.apache.asterix.lang.common.expression.TypeReferenceExpression)1 UnorderedListTypeDefinition (org.apache.asterix.lang.common.expression.UnorderedListTypeDefinition)1 ARecordType (org.apache.asterix.om.types.ARecordType)1 IAType (org.apache.asterix.om.types.IAType)1 TypeSignature (org.apache.asterix.om.types.TypeSignature)1