Search in sources :

Example 1 with RecordDataGenAnnotation

use of org.apache.asterix.common.annotations.RecordDataGenAnnotation in project asterixdb by apache.

the class AdmDataGen method dataGen.

public void dataGen() throws Exception {
    for (Map.Entry<TypeSignature, IAType> me : typeMap.entrySet()) {
        TypeSignature tn = me.getKey();
        TypeDataGen tdg = typeAnnotMap.get(tn);
        if (tdg.isDataGen()) {
            IAType t = me.getValue();
            if (t.getTypeTag() != ATypeTag.OBJECT) {
                throw new NotImplementedException();
            }
            ARecordType rt = (ARecordType) t;
            RecordDataGenAnnotation dga = firstDataGenAnnotation(rt);
            if (dga == null) {
                throw new Exception("No data generator annotations for type " + tn);
            }
            File outFile = new File(outputDir + File.separator + tdg.getOutputFileName());
            PrintStream outStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(outFile)));
            RecordGenerator rg = new RecordGenerator(rt, dga, "\n");
            rg.init(outStream, dgCtx);
            for (long i = 0; i < tdg.getNumValues(); i++) {
                rg.generate();
            }
            outStream.close();
        }
    }
}
Also used : PrintStream(java.io.PrintStream) RecordDataGenAnnotation(org.apache.asterix.common.annotations.RecordDataGenAnnotation) NotImplementedException(org.apache.hyracks.algebricks.common.exceptions.NotImplementedException) TypeDataGen(org.apache.asterix.common.annotations.TypeDataGen) NotImplementedException(org.apache.hyracks.algebricks.common.exceptions.NotImplementedException) ACIDException(org.apache.asterix.common.exceptions.ACIDException) MetadataException(org.apache.asterix.metadata.MetadataException) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) ParseException(org.apache.asterix.lang.aql.parser.ParseException) AsterixException(org.apache.asterix.common.exceptions.AsterixException) IOException(java.io.IOException) TypeSignature(org.apache.asterix.om.types.TypeSignature) FileOutputStream(java.io.FileOutputStream) Map(java.util.Map) HashMap(java.util.HashMap) ARecordType(org.apache.asterix.om.types.ARecordType) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) IAType(org.apache.asterix.om.types.IAType)

Example 2 with RecordDataGenAnnotation

use of org.apache.asterix.common.annotations.RecordDataGenAnnotation in project asterixdb by apache.

the class TypeTranslator method computeRecordType.

private static ARecordType computeRecordType(TypeSignature typeSignature, RecordTypeDefinition rtd, Map<TypeSignature, IAType> typeMap, Map<String, Map<ARecordType, List<Integer>>> incompleteFieldTypes, Map<TypeSignature, List<AbstractCollectionType>> incompleteItemTypes, String defaultDataverse) throws AsterixException {
    List<String> names = rtd.getFieldNames();
    int n = names.size();
    String[] fldNames = new String[n];
    IAType[] fldTypes = new IAType[n];
    int i = 0;
    for (String s : names) {
        fldNames[i++] = s;
    }
    boolean isOpen = rtd.getRecordKind() == RecordKind.OPEN;
    ARecordType recType = new ARecordType(typeSignature == null ? null : typeSignature.getName(), fldNames, fldTypes, isOpen);
    List<IRecordFieldDataGen> fieldDataGen = rtd.getFieldDataGen();
    if (fieldDataGen.size() == n) {
        IRecordFieldDataGen[] rfdg = new IRecordFieldDataGen[n];
        rfdg = fieldDataGen.toArray(rfdg);
        recType.getAnnotations().add(new RecordDataGenAnnotation(rfdg, rtd.getUndeclaredFieldsDataGen()));
    }
    for (int j = 0; j < n; j++) {
        TypeExpression texpr = rtd.getFieldTypes().get(j);
        switch(texpr.getTypeKind()) {
            case TYPEREFERENCE:
                {
                    TypeReferenceExpression tre = (TypeReferenceExpression) texpr;
                    TypeSignature signature = new TypeSignature(tre.getIdent().first == null ? defaultDataverse : tre.getIdent().first.getValue(), tre.getIdent().second.getValue());
                    IAType tref = solveTypeReference(signature, typeMap);
                    if (tref != null) {
                        if (!rtd.getOptionableFields().get(j)) {
                            // not nullable
                            fldTypes[j] = tref;
                        } else {
                            // optional
                            fldTypes[j] = AUnionType.createUnknownableType(tref);
                        }
                    } else {
                        addIncompleteFieldTypeReference(recType, j, tre, incompleteFieldTypes);
                        if (rtd.getOptionableFields().get(j)) {
                            fldTypes[j] = AUnionType.createUnknownableType(null);
                        }
                    }
                    break;
                }
            case RECORD:
                {
                    RecordTypeDefinition recTypeDef2 = (RecordTypeDefinition) texpr;
                    IAType t2 = computeRecordType(null, recTypeDef2, typeMap, incompleteFieldTypes, incompleteItemTypes, defaultDataverse);
                    if (!rtd.getOptionableFields().get(j)) {
                        // not nullable
                        fldTypes[j] = t2;
                    } else {
                        // nullable
                        fldTypes[j] = AUnionType.createUnknownableType(t2);
                    }
                    break;
                }
            case ORDEREDLIST:
                {
                    OrderedListTypeDefinition oltd = (OrderedListTypeDefinition) texpr;
                    IAType t2 = computeOrderedListType(null, oltd, typeMap, incompleteItemTypes, incompleteFieldTypes, defaultDataverse);
                    fldTypes[j] = rtd.getOptionableFields().get(j) ? AUnionType.createUnknownableType(t2) : t2;
                    break;
                }
            case UNORDEREDLIST:
                {
                    UnorderedListTypeDefinition ultd = (UnorderedListTypeDefinition) texpr;
                    IAType t2 = computeUnorderedListType(null, ultd, typeMap, incompleteItemTypes, incompleteFieldTypes, defaultDataverse);
                    fldTypes[j] = rtd.getOptionableFields().get(j) ? AUnionType.createUnknownableType(t2) : t2;
                    break;
                }
            default:
                {
                    throw new IllegalStateException();
                }
        }
    }
    return recType;
}
Also used : RecordDataGenAnnotation(org.apache.asterix.common.annotations.RecordDataGenAnnotation) TypeExpression(org.apache.asterix.lang.common.expression.TypeExpression) OrderedListTypeDefinition(org.apache.asterix.lang.common.expression.OrderedListTypeDefinition) TypeSignature(org.apache.asterix.om.types.TypeSignature) TypeReferenceExpression(org.apache.asterix.lang.common.expression.TypeReferenceExpression) RecordTypeDefinition(org.apache.asterix.lang.common.expression.RecordTypeDefinition) UnorderedListTypeDefinition(org.apache.asterix.lang.common.expression.UnorderedListTypeDefinition) IRecordFieldDataGen(org.apache.asterix.common.annotations.IRecordFieldDataGen) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Aggregations

RecordDataGenAnnotation (org.apache.asterix.common.annotations.RecordDataGenAnnotation)2 ARecordType (org.apache.asterix.om.types.ARecordType)2 IAType (org.apache.asterix.om.types.IAType)2 TypeSignature (org.apache.asterix.om.types.TypeSignature)2 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 IRecordFieldDataGen (org.apache.asterix.common.annotations.IRecordFieldDataGen)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 ParseException (org.apache.asterix.lang.aql.parser.ParseException)1 OrderedListTypeDefinition (org.apache.asterix.lang.common.expression.OrderedListTypeDefinition)1 RecordTypeDefinition (org.apache.asterix.lang.common.expression.RecordTypeDefinition)1 TypeExpression (org.apache.asterix.lang.common.expression.TypeExpression)1 TypeReferenceExpression (org.apache.asterix.lang.common.expression.TypeReferenceExpression)1