Search in sources :

Example 6 with TypeSignature

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

the class TypeTranslator method computeTypes.

public static Map<TypeSignature, IAType> computeTypes(MetadataTransactionContext mdTxnCtx, TypeExpression typeExpr, String typeName, String typeDataverse, Map<TypeSignature, IAType> typeMap) throws AlgebricksException {
    Map<String, Map<ARecordType, List<Integer>>> incompleteFieldTypes = new HashMap<>();
    Map<TypeSignature, List<AbstractCollectionType>> incompleteItemTypes = new HashMap<>();
    Map<TypeSignature, List<TypeSignature>> incompleteTopLevelTypeReferences = new HashMap<>();
    firstPass(typeExpr, typeName, typeMap, incompleteFieldTypes, incompleteItemTypes, incompleteTopLevelTypeReferences, typeDataverse);
    secondPass(mdTxnCtx, typeMap, incompleteFieldTypes, incompleteItemTypes, incompleteTopLevelTypeReferences, typeDataverse);
    for (IAType type : typeMap.values()) {
        if (type.getTypeTag().isDerivedType()) {
            ((AbstractComplexType) type).generateNestedDerivedTypeNames();
        }
    }
    return typeMap;
}
Also used : TypeSignature(org.apache.asterix.om.types.TypeSignature) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AbstractComplexType(org.apache.asterix.om.types.AbstractComplexType) HashMap(java.util.HashMap) Map(java.util.Map) BuiltinTypeMap(org.apache.asterix.metadata.entities.BuiltinTypeMap) IAType(org.apache.asterix.om.types.IAType)

Example 7 with TypeSignature

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

the class TypeTranslator method secondPass.

private static void secondPass(MetadataTransactionContext mdTxnCtx, Map<TypeSignature, IAType> typeMap, Map<String, Map<ARecordType, List<Integer>>> incompleteFieldTypes, Map<TypeSignature, List<AbstractCollectionType>> incompleteItemTypes, Map<TypeSignature, List<TypeSignature>> incompleteTopLevelTypeReferences, String typeDataverse) throws AlgebricksException {
    // solve remaining top level references
    for (TypeSignature typeSignature : incompleteTopLevelTypeReferences.keySet()) {
        IAType t;
        Datatype dt = MetadataManager.INSTANCE.getDatatype(mdTxnCtx, typeSignature.getNamespace(), typeSignature.getName());
        if (dt == null) {
            throw new AlgebricksException("Could not resolve type " + typeSignature);
        } else {
            t = dt.getDatatype();
        }
        for (TypeSignature sign : incompleteTopLevelTypeReferences.get(typeSignature)) {
            typeMap.put(sign, t);
        }
    }
    // solve remaining field type references
    for (String trefName : incompleteFieldTypes.keySet()) {
        IAType t;
        Datatype dt = MetadataManager.INSTANCE.getDatatype(mdTxnCtx, typeDataverse, trefName);
        if (dt == null) {
            dt = MetadataManager.INSTANCE.getDatatype(mdTxnCtx, MetadataConstants.METADATA_DATAVERSE_NAME, trefName);
        }
        if (dt == null) {
            throw new AlgebricksException("Could not resolve type " + trefName);
        } else {
            t = dt.getDatatype();
        }
        Map<ARecordType, List<Integer>> fieldsToFix = incompleteFieldTypes.get(trefName);
        for (ARecordType recType : fieldsToFix.keySet()) {
            List<Integer> positions = fieldsToFix.get(recType);
            IAType[] fldTypes = recType.getFieldTypes();
            for (Integer pos : positions) {
                if (fldTypes[pos] == null) {
                    fldTypes[pos] = t;
                } else {
                    // nullable
                    AUnionType nullableUnion = (AUnionType) fldTypes[pos];
                    nullableUnion.setActualType(t);
                }
            }
        }
    }
    // solve remaining item type references
    for (TypeSignature typeSignature : incompleteItemTypes.keySet()) {
        IAType t;
        Datatype dt;
        if (MetadataManager.INSTANCE != null) {
            dt = MetadataManager.INSTANCE.getDatatype(mdTxnCtx, typeSignature.getNamespace(), typeSignature.getName());
            if (dt == null) {
                throw new AlgebricksException("Could not resolve type " + typeSignature);
            }
            t = dt.getDatatype();
        } else {
            t = typeMap.get(typeSignature);
        }
        for (AbstractCollectionType act : incompleteItemTypes.get(typeSignature)) {
            act.setItemType(t);
        }
    }
}
Also used : AUnionType(org.apache.asterix.om.types.AUnionType) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) Datatype(org.apache.asterix.metadata.entities.Datatype) TypeSignature(org.apache.asterix.om.types.TypeSignature) AbstractCollectionType(org.apache.asterix.om.types.AbstractCollectionType) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 8 with TypeSignature

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

the class QueryTranslator method handleCreateTypeStatement.

protected void handleCreateTypeStatement(MetadataProvider metadataProvider, Statement stmt) throws Exception {
    TypeDecl stmtCreateType = (TypeDecl) stmt;
    String dataverseName = getActiveDataverse(stmtCreateType.getDataverseName());
    String typeName = stmtCreateType.getIdent().getValue();
    MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
    metadataProvider.setMetadataTxnContext(mdTxnCtx);
    MetadataLockManager.INSTANCE.createTypeBegin(metadataProvider.getLocks(), dataverseName, dataverseName + "." + typeName);
    try {
        Dataverse dv = MetadataManager.INSTANCE.getDataverse(mdTxnCtx, dataverseName);
        if (dv == null) {
            throw new AlgebricksException("Unknown dataverse " + dataverseName);
        }
        Datatype dt = MetadataManager.INSTANCE.getDatatype(mdTxnCtx, dataverseName, typeName);
        if (dt != null) {
            if (!stmtCreateType.getIfNotExists()) {
                throw new AlgebricksException("A datatype with this name " + typeName + " already exists.");
            }
        } else {
            if (BuiltinTypeMap.getBuiltinType(typeName) != null) {
                throw new AlgebricksException("Cannot redefine builtin type " + typeName + ".");
            } else {
                Map<TypeSignature, IAType> typeMap = TypeTranslator.computeTypes(mdTxnCtx, stmtCreateType.getTypeDef(), stmtCreateType.getIdent().getValue(), dataverseName);
                TypeSignature typeSignature = new TypeSignature(dataverseName, typeName);
                IAType type = typeMap.get(typeSignature);
                MetadataManager.INSTANCE.addDatatype(mdTxnCtx, new Datatype(dataverseName, typeName, type, false));
            }
        }
        MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
    } catch (Exception e) {
        abort(e, e, mdTxnCtx);
        throw e;
    } finally {
        metadataProvider.getLocks().unlock();
    }
}
Also used : TypeSignature(org.apache.asterix.om.types.TypeSignature) TypeDecl(org.apache.asterix.lang.common.statement.TypeDecl) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) Dataverse(org.apache.asterix.metadata.entities.Dataverse) ACIDException(org.apache.asterix.common.exceptions.ACIDException) MetadataException(org.apache.asterix.metadata.MetadataException) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) CompilationException(org.apache.asterix.common.exceptions.CompilationException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) AsterixException(org.apache.asterix.common.exceptions.AsterixException) Datatype(org.apache.asterix.metadata.entities.Datatype) IAType(org.apache.asterix.om.types.IAType)

Example 9 with TypeSignature

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

the class ADGenDmlTranslator method translate.

public void translate() throws AsterixException, MetadataException, AlgebricksException {
    String defaultDataverse = getDefaultDataverse();
    types = new HashMap<TypeSignature, IAType>();
    typeDataGenMap = new HashMap<TypeSignature, TypeDataGen>();
    for (Statement stmt : aqlStatements) {
        if (stmt.getKind() == Statement.Kind.TYPE_DECL) {
            TypeDecl td = (TypeDecl) stmt;
            String typeDataverse = td.getDataverseName() == null ? defaultDataverse : td.getDataverseName().getValue();
            Map<TypeSignature, IAType> typeInStmt = TypeTranslator.computeTypes(mdTxnCtx, td.getTypeDef(), td.getIdent().getValue(), typeDataverse, types);
            types.putAll(typeInStmt);
            TypeSignature signature = new TypeSignature(typeDataverse, td.getIdent().getValue());
            TypeDataGen tdg = td.getDatagenAnnotation();
            if (tdg != null) {
                typeDataGenMap.put(signature, tdg);
            }
        }
    }
}
Also used : TypeSignature(org.apache.asterix.om.types.TypeSignature) Statement(org.apache.asterix.lang.common.base.Statement) TypeDecl(org.apache.asterix.lang.common.statement.TypeDecl) TypeDataGen(org.apache.asterix.common.annotations.TypeDataGen) IAType(org.apache.asterix.om.types.IAType)

Aggregations

TypeSignature (org.apache.asterix.om.types.TypeSignature)9 IAType (org.apache.asterix.om.types.IAType)7 ARecordType (org.apache.asterix.om.types.ARecordType)4 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ACIDException (org.apache.asterix.common.exceptions.ACIDException)3 AsterixException (org.apache.asterix.common.exceptions.AsterixException)3 MetadataException (org.apache.asterix.metadata.MetadataException)3 Datatype (org.apache.asterix.metadata.entities.Datatype)3 RemoteException (java.rmi.RemoteException)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 RecordDataGenAnnotation (org.apache.asterix.common.annotations.RecordDataGenAnnotation)2 TypeDataGen (org.apache.asterix.common.annotations.TypeDataGen)2 CompilationException (org.apache.asterix.common.exceptions.CompilationException)2 TypeExpression (org.apache.asterix.lang.common.expression.TypeExpression)2 TypeDecl (org.apache.asterix.lang.common.statement.TypeDecl)2