Search in sources :

Example 1 with Datatype

use of org.apache.asterix.metadata.entities.Datatype in project asterixdb by apache.

the class QueryTranslator method handleTypeDropStatement.

protected void handleTypeDropStatement(MetadataProvider metadataProvider, Statement stmt) throws Exception {
    TypeDropStatement stmtTypeDrop = (TypeDropStatement) stmt;
    String dataverseName = getActiveDataverse(stmtTypeDrop.getDataverseName());
    String typeName = stmtTypeDrop.getTypeName().getValue();
    MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
    metadataProvider.setMetadataTxnContext(mdTxnCtx);
    MetadataLockManager.INSTANCE.dropTypeBegin(metadataProvider.getLocks(), dataverseName, dataverseName + "." + typeName);
    try {
        Datatype dt = MetadataManager.INSTANCE.getDatatype(mdTxnCtx, dataverseName, typeName);
        if (dt == null) {
            if (!stmtTypeDrop.getIfExists()) {
                throw new AlgebricksException("There is no datatype with this name " + typeName + ".");
            }
        } else {
            MetadataManager.INSTANCE.dropDatatype(mdTxnCtx, dataverseName, typeName);
        }
        MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
    } catch (Exception e) {
        abort(e, e, mdTxnCtx);
        throw e;
    } finally {
        metadataProvider.getLocks().unlock();
    }
}
Also used : AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) TypeDropStatement(org.apache.asterix.lang.common.statement.TypeDropStatement) 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)

Example 2 with Datatype

use of org.apache.asterix.metadata.entities.Datatype in project asterixdb by apache.

the class DatatypeTupleTranslator method createDataTypeFromARecord.

private Datatype createDataTypeFromARecord(ARecord datatypeRecord) throws MetadataException {
    String dataverseName = ((AString) datatypeRecord.getValueByPos(MetadataRecordTypes.DATATYPE_ARECORD_DATAVERSENAME_FIELD_INDEX)).getStringValue();
    String datatypeName = ((AString) datatypeRecord.getValueByPos(MetadataRecordTypes.DATATYPE_ARECORD_DATATYPENAME_FIELD_INDEX)).getStringValue();
    IAType type = BuiltinTypeMap.getBuiltinType(datatypeName);
    if (type == null) {
        // Derived Type
        ARecord derivedTypeRecord = (ARecord) datatypeRecord.getValueByPos(MetadataRecordTypes.DATATYPE_ARECORD_DERIVED_FIELD_INDEX);
        DerivedTypeTag tag = DerivedTypeTag.valueOf(((AString) derivedTypeRecord.getValueByPos(MetadataRecordTypes.DERIVEDTYPE_ARECORD_TAG_FIELD_INDEX)).getStringValue());
        boolean isAnonymous = ((ABoolean) derivedTypeRecord.getValueByPos(MetadataRecordTypes.DERIVEDTYPE_ARECORD_ISANONYMOUS_FIELD_INDEX)).getBoolean();
        switch(tag) {
            case RECORD:
                {
                    ARecord recordType = (ARecord) derivedTypeRecord.getValueByPos(MetadataRecordTypes.DERIVEDTYPE_ARECORD_RECORD_FIELD_INDEX);
                    boolean isOpen = ((ABoolean) recordType.getValueByPos(MetadataRecordTypes.RECORDTYPE_ARECORD_ISOPEN_FIELD_INDEX)).getBoolean().booleanValue();
                    int numberOfFields = ((AOrderedList) recordType.getValueByPos(MetadataRecordTypes.RECORDTYPE_ARECORD_FIELDS_FIELD_INDEX)).size();
                    IACursor cursor = ((AOrderedList) recordType.getValueByPos(MetadataRecordTypes.RECORDTYPE_ARECORD_FIELDS_FIELD_INDEX)).getCursor();
                    String[] fieldNames = new String[numberOfFields];
                    IAType[] fieldTypes = new IAType[numberOfFields];
                    int fieldId = 0;
                    String fieldTypeName;
                    while (cursor.next()) {
                        ARecord field = (ARecord) cursor.get();
                        fieldNames[fieldId] = ((AString) field.getValueByPos(MetadataRecordTypes.FIELD_ARECORD_FIELDNAME_FIELD_INDEX)).getStringValue();
                        fieldTypeName = ((AString) field.getValueByPos(MetadataRecordTypes.FIELD_ARECORD_FIELDTYPE_FIELD_INDEX)).getStringValue();
                        boolean isNullable = ((ABoolean) field.getValueByPos(MetadataRecordTypes.FIELD_ARECORD_ISNULLABLE_FIELD_INDEX)).getBoolean().booleanValue();
                        fieldTypes[fieldId] = BuiltinTypeMap.getTypeFromTypeName(metadataNode, jobId, dataverseName, fieldTypeName, isNullable);
                        fieldId++;
                    }
                    return new Datatype(dataverseName, datatypeName, new ARecordType(datatypeName, fieldNames, fieldTypes, isOpen), isAnonymous);
                }
            case UNORDEREDLIST:
                {
                    String unorderedlistTypeName = ((AString) derivedTypeRecord.getValueByPos(MetadataRecordTypes.DERIVEDTYPE_ARECORD_UNORDEREDLIST_FIELD_INDEX)).getStringValue();
                    return new Datatype(dataverseName, datatypeName, new AUnorderedListType(BuiltinTypeMap.getTypeFromTypeName(metadataNode, jobId, dataverseName, unorderedlistTypeName, false), datatypeName), isAnonymous);
                }
            case ORDEREDLIST:
                {
                    String orderedlistTypeName = ((AString) derivedTypeRecord.getValueByPos(MetadataRecordTypes.DERIVEDTYPE_ARECORD_ORDEREDLIST_FIELD_INDEX)).getStringValue();
                    return new Datatype(dataverseName, datatypeName, new AOrderedListType(BuiltinTypeMap.getTypeFromTypeName(metadataNode, jobId, dataverseName, orderedlistTypeName, false), datatypeName), isAnonymous);
                }
            default:
                throw new UnsupportedOperationException("Unsupported derived type: " + tag);
        }
    }
    return new Datatype(dataverseName, datatypeName, type, false);
}
Also used : ABoolean(org.apache.asterix.om.base.ABoolean) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) AString(org.apache.asterix.om.base.AString) IACursor(org.apache.asterix.om.base.IACursor) AUnorderedListType(org.apache.asterix.om.types.AUnorderedListType) Datatype(org.apache.asterix.metadata.entities.Datatype) ARecord(org.apache.asterix.om.base.ARecord) AString(org.apache.asterix.om.base.AString) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 3 with Datatype

use of org.apache.asterix.metadata.entities.Datatype in project asterixdb by apache.

the class FeedMetadataUtil method getOutputType.

public static ARecordType getOutputType(IFeed feed, Map<String, String> configuration, String key) throws MetadataException {
    ARecordType outputType = null;
    String fqOutputType = configuration.get(key);
    if (fqOutputType == null) {
        return null;
    }
    String[] dataverseAndType = fqOutputType.split("[.]");
    String dataverseName;
    String datatypeName;
    if (dataverseAndType.length == 1) {
        datatypeName = dataverseAndType[0];
        dataverseName = feed.getDataverseName();
    } else if (dataverseAndType.length == 2) {
        dataverseName = dataverseAndType[0];
        datatypeName = dataverseAndType[1];
    } else {
        throw new IllegalArgumentException("Invalid value for the parameter " + key);
    }
    MetadataTransactionContext ctx = null;
    MetadataManager.INSTANCE.acquireReadLatch();
    try {
        ctx = MetadataManager.INSTANCE.beginTransaction();
        Datatype t = MetadataManager.INSTANCE.getDatatype(ctx, dataverseName, datatypeName);
        if (t == null || t.getDatatype().getTypeTag() != ATypeTag.OBJECT) {
            throw new MetadataException(ErrorCode.FEED_METADATA_UTIL_UNEXPECTED_FEED_DATATYPE, datatypeName);
        }
        outputType = (ARecordType) t.getDatatype();
        MetadataManager.INSTANCE.commitTransaction(ctx);
    } catch (ACIDException | RemoteException e) {
        if (ctx != null) {
            try {
                MetadataManager.INSTANCE.abortTransaction(ctx);
            } catch (ACIDException | RemoteException e2) {
                e.addSuppressed(e2);
            }
            throw new MetadataException(ErrorCode.FEED_CREATE_FEED_DATATYPE_ERROR, e, datatypeName);
        }
    } finally {
        MetadataManager.INSTANCE.releaseReadLatch();
    }
    return outputType;
}
Also used : MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) RemoteException(java.rmi.RemoteException) ARecordType(org.apache.asterix.om.types.ARecordType) MetadataException(org.apache.asterix.metadata.MetadataException) Datatype(org.apache.asterix.metadata.entities.Datatype) ACIDException(org.apache.asterix.common.exceptions.ACIDException)

Example 4 with Datatype

use of org.apache.asterix.metadata.entities.Datatype in project asterixdb by apache.

the class MetadataNode method getDataverseDatatypes.

private List<Datatype> getDataverseDatatypes(JobId jobId, String dataverseName) throws MetadataException, RemoteException {
    try {
        ITupleReference searchKey = createTuple(dataverseName);
        DatatypeTupleTranslator tupleReaderWriter = tupleTranslatorProvider.getDataTypeTupleTranslator(jobId, this, false);
        IValueExtractor<Datatype> valueExtractor = new MetadataEntityValueExtractor<>(tupleReaderWriter);
        List<Datatype> results = new ArrayList<>();
        searchIndex(jobId, MetadataPrimaryIndexes.DATATYPE_DATASET, searchKey, valueExtractor, results);
        return results;
    } catch (HyracksDataException e) {
        throw new MetadataException(e);
    }
}
Also used : MetadataEntityValueExtractor(org.apache.asterix.metadata.valueextractors.MetadataEntityValueExtractor) DatatypeTupleTranslator(org.apache.asterix.metadata.entitytupletranslators.DatatypeTupleTranslator) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) ArrayList(java.util.ArrayList) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) Datatype(org.apache.asterix.metadata.entities.Datatype)

Example 5 with Datatype

use of org.apache.asterix.metadata.entities.Datatype in project asterixdb by apache.

the class MetadataNode method getAllDatatypes.

public List<Datatype> getAllDatatypes(JobId jobId) throws MetadataException, RemoteException {
    try {
        ITupleReference searchKey = null;
        DatatypeTupleTranslator tupleReaderWriter = tupleTranslatorProvider.getDataTypeTupleTranslator(jobId, this, false);
        IValueExtractor<Datatype> valueExtractor = new MetadataEntityValueExtractor<>(tupleReaderWriter);
        List<Datatype> results = new ArrayList<>();
        searchIndex(jobId, MetadataPrimaryIndexes.DATATYPE_DATASET, searchKey, valueExtractor, results);
        return results;
    } catch (HyracksDataException e) {
        throw new MetadataException(e);
    }
}
Also used : MetadataEntityValueExtractor(org.apache.asterix.metadata.valueextractors.MetadataEntityValueExtractor) DatatypeTupleTranslator(org.apache.asterix.metadata.entitytupletranslators.DatatypeTupleTranslator) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) ArrayList(java.util.ArrayList) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) Datatype(org.apache.asterix.metadata.entities.Datatype)

Aggregations

Datatype (org.apache.asterix.metadata.entities.Datatype)18 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)9 ArrayList (java.util.ArrayList)8 ARecordType (org.apache.asterix.om.types.ARecordType)8 IAType (org.apache.asterix.om.types.IAType)8 ACIDException (org.apache.asterix.common.exceptions.ACIDException)7 RemoteException (java.rmi.RemoteException)6 ITupleReference (org.apache.hyracks.dataflow.common.data.accessors.ITupleReference)6 MetadataException (org.apache.asterix.metadata.MetadataException)5 MetadataTransactionContext (org.apache.asterix.metadata.MetadataTransactionContext)5 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)5 IOException (java.io.IOException)4 AsterixException (org.apache.asterix.common.exceptions.AsterixException)4 CompilationException (org.apache.asterix.common.exceptions.CompilationException)4 Dataset (org.apache.asterix.metadata.entities.Dataset)4 List (java.util.List)3 DatatypeTupleTranslator (org.apache.asterix.metadata.entitytupletranslators.DatatypeTupleTranslator)3 MetadataEntityValueExtractor (org.apache.asterix.metadata.valueextractors.MetadataEntityValueExtractor)3 Date (java.util.Date)2 IDataset (org.apache.asterix.common.metadata.IDataset)2