Search in sources :

Example 1 with AUnorderedListType

use of org.apache.asterix.om.types.AUnorderedListType in project asterixdb by apache.

the class JSONDeserializerForTypes method convertFromJSON.

/**
     * Deserialize an arbitrary JSON representation of a type.
     *
     * @param typeInJSON
     *            the JSON representation of the type.
     * @return an valid AsterixDB type.
     * @throws Exception
     */
public static IAType convertFromJSON(JsonNode typeInJSON) throws Exception {
    String typeName = typeInJSON.get("type").asText();
    // Deals with ordered list.
    if (typeName.equals(AOrderedListType.class.getName())) {
        IAType itemType = convertFromJSON(typeInJSON.get("item-type"));
        return new AOrderedListType(itemType, "ordered-list");
    }
    // Deals with unordered list.
    if (typeName.equals(AUnorderedListType.class.getName())) {
        IAType itemType = convertFromJSON(typeInJSON.get("item-type"));
        return new AUnorderedListType(itemType, "unordered-list");
    }
    // Deals with Union Type.
    if (typeName.equals(AUnionType.class.getName())) {
        List<IAType> unionTypes = new ArrayList<IAType>();
        JsonNode fields = typeInJSON.get("fields");
        for (int i = 0; i < fields.size(); i++) {
            JsonNode fieldType = fields.get(i);
            unionTypes.add(convertFromJSON(fieldType));
        }
        return new AUnionType(unionTypes, "union");
    }
    // Deals with record types.
    if (typeName.equals(ARecordType.class.getName())) {
        String name = typeInJSON.get("name").asText();
        boolean openType = typeInJSON.get("open").asBoolean();
        JsonNode fields = typeInJSON.get("fields");
        String[] fieldNames = new String[fields.size()];
        IAType[] fieldTypes = new IAType[fields.size()];
        for (int i = 0; i < fields.size(); ++i) {
            JsonNode field = fields.get(i);
            List<String> names = Lists.newArrayList(field.fieldNames());
            String fieldName = names.get(0);
            fieldNames[i] = fieldName;
            fieldTypes[i] = convertFromJSON(field.get(fieldName));
        }
        return new ARecordType(name, fieldNames, fieldTypes, openType);
    }
    // Deals with primitive types.
    Class<?> cl = BuiltinType.class;
    Field typeField = cl.getDeclaredField(typeName.toUpperCase());
    return (IAType) typeField.get(null);
}
Also used : AUnionType(org.apache.asterix.om.types.AUnionType) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) AUnorderedListType(org.apache.asterix.om.types.AUnorderedListType) BuiltinType(org.apache.asterix.om.types.BuiltinType) Field(java.lang.reflect.Field) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 2 with AUnorderedListType

use of org.apache.asterix.om.types.AUnorderedListType in project asterixdb by apache.

the class AUnorderedListSerializerDeserializerTest method test.

@Test
public void test() {
    // Generates types.
    ARecordType addrRecordType = SerializerDeserializerTestUtils.generateAddressRecordType();
    ARecordType employeeType = SerializerDeserializerTestUtils.generateEmployeeRecordType(addrRecordType);
    AUnorderedListType employeeListType = new AUnorderedListType(employeeType, "employee_list");
    //Generates records.
    ARecord[] records = SerializerDeserializerTestUtils.generateRecords(addrRecordType, employeeType);
    // Generates lists
    AUnorderedList[] lists = new AUnorderedList[4];
    for (int index = 0; index < lists.length; ++index) {
        lists[index] = new AUnorderedList(employeeListType, Arrays.asList(records));
    }
    AUnorderedListSerializerDeserializer serde = new AUnorderedListSerializerDeserializer(employeeListType);
    // Run four test threads to serialize/deserialize lists concurrently.
    SerializerDeserializerTestUtils.concurrentSerDeTestRun(serde, lists);
}
Also used : ARecord(org.apache.asterix.om.base.ARecord) AUnorderedList(org.apache.asterix.om.base.AUnorderedList) ARecordType(org.apache.asterix.om.types.ARecordType) AUnorderedListType(org.apache.asterix.om.types.AUnorderedListType) Test(org.junit.Test)

Example 3 with AUnorderedListType

use of org.apache.asterix.om.types.AUnorderedListType 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 4 with AUnorderedListType

use of org.apache.asterix.om.types.AUnorderedListType in project asterixdb by apache.

the class JSONDeserializerForTypesTest method test.

@Test
public void test() throws Exception {
    // Tests a record type with primitive types.
    String[] fieldNames = { "a1", "a2", "a3" };
    IAType[] fieldTypes = { BuiltinType.ASTRING, BuiltinType.AINT16, BuiltinType.ABITARRAY };
    ARecordType recordType = new ARecordType("ARecord", fieldNames, fieldTypes, true);
    Assert.assertEquals(recordType, JSONDeserializerForTypes.convertFromJSON(recordType.toJSON()));
    // Tests a record type with a nested record type.
    String[] fieldNames2 = { "a1", "a2" };
    IAType[] fieldTypes2 = { BuiltinType.ABOOLEAN, recordType };
    ARecordType recordType2 = new ARecordType("ARecord2", fieldNames2, fieldTypes2, true);
    Assert.assertEquals(recordType2, JSONDeserializerForTypes.convertFromJSON(recordType2.toJSON()));
    // Tests a record type with a union type.
    String[] fieldNames3 = { "a1", "a2" };
    List<IAType> unionTypes = new ArrayList<IAType>();
    unionTypes.add(BuiltinType.ADURATION);
    unionTypes.add(recordType2);
    AUnionType unionType = new AUnionType(unionTypes, "union");
    IAType[] fieldTypes3 = { BuiltinType.ABOOLEAN, unionType };
    ARecordType recordType3 = new ARecordType("ARecord3", fieldNames3, fieldTypes3, true);
    Assert.assertEquals(recordType3, JSONDeserializerForTypes.convertFromJSON(recordType3.toJSON()));
    // Tests a record type with an ordered list.
    String[] fieldNames4 = { "a1", "a2" };
    IAType[] fieldTypes4 = { BuiltinType.ABOOLEAN, new AOrderedListType(BuiltinType.ADATETIME, "list") };
    ARecordType recordType4 = new ARecordType("ARecord4", fieldNames4, fieldTypes4, false);
    Assert.assertEquals(recordType4, JSONDeserializerForTypes.convertFromJSON(recordType4.toJSON()));
    // Tests a record type with an unordered list.
    String[] fieldNames5 = { "a1", "a2" };
    IAType[] fieldTypes5 = { BuiltinType.ABOOLEAN, new AUnorderedListType(recordType2, "list") };
    ARecordType recordType5 = new ARecordType("ARecord5", fieldNames5, fieldTypes5, false);
    Assert.assertEquals(recordType5, JSONDeserializerForTypes.convertFromJSON(recordType5.toJSON()));
}
Also used : AUnionType(org.apache.asterix.om.types.AUnionType) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) ArrayList(java.util.ArrayList) ARecordType(org.apache.asterix.om.types.ARecordType) AUnorderedListType(org.apache.asterix.om.types.AUnorderedListType) IAType(org.apache.asterix.om.types.IAType) Test(org.junit.Test)

Example 5 with AUnorderedListType

use of org.apache.asterix.om.types.AUnorderedListType in project asterixdb by apache.

the class TypeTranslator method computeUnorderedListType.

private static AUnorderedListType computeUnorderedListType(TypeSignature typeSignature, UnorderedListTypeDefinition ultd, Map<TypeSignature, IAType> typeMap, Map<TypeSignature, List<AbstractCollectionType>> incompleteItemTypes, Map<String, Map<ARecordType, List<Integer>>> incompleteFieldTypes, String defaulDataverse) throws AsterixException {
    TypeExpression tExpr = ultd.getItemTypeExpression();
    String typeName = typeSignature != null ? typeSignature.getName() : null;
    AUnorderedListType ault = new AUnorderedListType(null, typeName);
    setCollectionItemType(tExpr, typeMap, incompleteItemTypes, incompleteFieldTypes, ault, defaulDataverse);
    return ault;
}
Also used : TypeExpression(org.apache.asterix.lang.common.expression.TypeExpression) AUnorderedListType(org.apache.asterix.om.types.AUnorderedListType)

Aggregations

AUnorderedListType (org.apache.asterix.om.types.AUnorderedListType)10 IAType (org.apache.asterix.om.types.IAType)8 ARecordType (org.apache.asterix.om.types.ARecordType)7 ArrayList (java.util.ArrayList)5 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)5 AUnionType (org.apache.asterix.om.types.AUnionType)5 Test (org.junit.Test)3 IJObject (org.apache.asterix.external.api.IJObject)2 JBoolean (org.apache.asterix.external.library.java.JObjects.JBoolean)2 JOrderedList (org.apache.asterix.external.library.java.JObjects.JOrderedList)2 JPoint (org.apache.asterix.external.library.java.JObjects.JPoint)2 JRecord (org.apache.asterix.external.library.java.JObjects.JRecord)2 JString (org.apache.asterix.external.library.java.JObjects.JString)2 JUnorderedList (org.apache.asterix.external.library.java.JObjects.JUnorderedList)2 ARecord (org.apache.asterix.om.base.ARecord)2 AUnorderedList (org.apache.asterix.om.base.AUnorderedList)2 ATypeTag (org.apache.asterix.om.types.ATypeTag)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Field (java.lang.reflect.Field)1 List (java.util.List)1