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();
}
}
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);
}
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;
}
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);
}
}
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);
}
}
Aggregations