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