Search in sources :

Example 1 with TypeDecl

use of org.apache.asterix.lang.common.statement.TypeDecl 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 2 with TypeDecl

use of org.apache.asterix.lang.common.statement.TypeDecl 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

TypeDecl (org.apache.asterix.lang.common.statement.TypeDecl)2 IAType (org.apache.asterix.om.types.IAType)2 TypeSignature (org.apache.asterix.om.types.TypeSignature)2 IOException (java.io.IOException)1 RemoteException (java.rmi.RemoteException)1 TypeDataGen (org.apache.asterix.common.annotations.TypeDataGen)1 ACIDException (org.apache.asterix.common.exceptions.ACIDException)1 AsterixException (org.apache.asterix.common.exceptions.AsterixException)1 CompilationException (org.apache.asterix.common.exceptions.CompilationException)1 Statement (org.apache.asterix.lang.common.base.Statement)1 MetadataException (org.apache.asterix.metadata.MetadataException)1 MetadataTransactionContext (org.apache.asterix.metadata.MetadataTransactionContext)1 Datatype (org.apache.asterix.metadata.entities.Datatype)1 Dataverse (org.apache.asterix.metadata.entities.Dataverse)1 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)1 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)1