Search in sources :

Example 6 with AOrderedListType

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

the class SerializerDeserializerTestUtils method generateEmployeeRecordType.

public static ARecordType generateEmployeeRecordType(ARecordType addrRecordType) {
    AOrderedListType addrListType = new AOrderedListType(addrRecordType, "address_list");
    String[] fieldNames = new String[] { "id", "name", "addresses_history" };
    IAType[] fieldTypes = new IAType[] { BuiltinType.AINT64, BuiltinType.ASTRING, addrListType };
    return new ARecordType("employee", fieldNames, fieldTypes, true);
}
Also used : AOrderedListType(org.apache.asterix.om.types.AOrderedListType) AString(org.apache.asterix.om.base.AString) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 7 with AOrderedListType

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

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

the class OverlapBinsDescriptor method createEvaluatorFactory.

@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
    return new IScalarEvaluatorFactory() {

        private static final long serialVersionUID = 1L;

        @Override
        public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
            return new IScalarEvaluator() {

                private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private final DataOutput out = resultStorage.getDataOutput();

                private final IPointable argPtr0 = new VoidPointable();

                private final IPointable argPtr1 = new VoidPointable();

                private final IPointable argPtr2 = new VoidPointable();

                private final IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);

                private final IScalarEvaluator eval1 = args[1].createScalarEvaluator(ctx);

                private final IScalarEvaluator eval2 = args[2].createScalarEvaluator(ctx);

                // for output
                private OrderedListBuilder listBuilder = new OrderedListBuilder();

                private ArrayBackedValueStorage listStorage = new ArrayBackedValueStorage();

                protected final AOrderedListType intListType = new AOrderedListType(BuiltinType.AINTERVAL, null);

                private final AMutableInterval aInterval = new AMutableInterval(0, 0, (byte) -1);

                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<AInterval> intervalSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINTERVAL);

                private final GregorianCalendarSystem gregCalSys = GregorianCalendarSystem.getInstance();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    eval2.evaluate(tuple, argPtr2);
                    byte[] bytes0 = argPtr0.getByteArray();
                    int offset0 = argPtr0.getStartOffset();
                    ATypeTag type0 = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes0[offset0]);
                    long intervalStart;
                    long intervalEnd;
                    byte intervalTypeTag;
                    if (type0 == ATypeTag.INTERVAL) {
                        intervalStart = AIntervalSerializerDeserializer.getIntervalStart(bytes0, offset0 + 1);
                        intervalEnd = AIntervalSerializerDeserializer.getIntervalEnd(bytes0, offset0 + 1);
                        intervalTypeTag = AIntervalSerializerDeserializer.getIntervalTimeType(bytes0, offset0 + 1);
                        if (intervalTypeTag == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
                            intervalStart = intervalStart * GregorianCalendarSystem.CHRONON_OF_DAY;
                        }
                    } else {
                        throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                    }
                    // get the anchor instance time
                    byte[] bytes1 = argPtr1.getByteArray();
                    int offset1 = argPtr1.getStartOffset();
                    ATypeTag type1 = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes1[offset1]);
                    if (intervalTypeTag != bytes1[offset1]) {
                        throw new IncompatibleTypeException(getIdentifier(), intervalTypeTag, bytes1[offset1]);
                    }
                    long anchorTime;
                    switch(type1) {
                        case DATE:
                            anchorTime = ADateSerializerDeserializer.getChronon(bytes1, offset1 + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                            break;
                        case TIME:
                            anchorTime = ATimeSerializerDeserializer.getChronon(bytes1, offset1 + 1);
                            break;
                        case DATETIME:
                            anchorTime = ADateTimeSerializerDeserializer.getChronon(bytes1, offset1 + 1);
                            break;
                        default:
                            throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_DATE_TYPE_TAG, ATypeTag.SERIALIZED_TIME_TYPE_TAG, ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
                    }
                    byte[] bytes2 = argPtr2.getByteArray();
                    int offset2 = argPtr2.getStartOffset();
                    ATypeTag type2 = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes2[offset2]);
                    int yearMonth = 0;
                    long dayTime = 0;
                    long firstBinIndex;
                    switch(type2) {
                        case YEARMONTHDURATION:
                            yearMonth = AYearMonthDurationSerializerDeserializer.getYearMonth(bytes2, offset2 + 1);
                            int yearStart = gregCalSys.getYear(anchorTime);
                            int monthStart = gregCalSys.getMonthOfYear(anchorTime, yearStart);
                            int yearToBin = gregCalSys.getYear(intervalStart);
                            int monthToBin = gregCalSys.getMonthOfYear(intervalStart, yearToBin);
                            int totalMonths = (yearToBin - yearStart) * 12 + (monthToBin - monthStart);
                            firstBinIndex = totalMonths / yearMonth + ((totalMonths < 0 && totalMonths % yearMonth != 0) ? -1 : 0);
                            if (firstBinIndex > Integer.MAX_VALUE) {
                                throw new OverflowException(getIdentifier());
                            }
                            if (firstBinIndex < Integer.MIN_VALUE) {
                                throw new UnderflowException(getIdentifier());
                            }
                            break;
                        case DAYTIMEDURATION:
                            dayTime = ADayTimeDurationSerializerDeserializer.getDayTime(bytes2, offset2 + 1);
                            long totalChronon = intervalStart - anchorTime;
                            firstBinIndex = totalChronon / dayTime + ((totalChronon < 0 && totalChronon % dayTime != 0) ? -1 : 0);
                            break;
                        default:
                            throw new TypeMismatchException(getIdentifier(), 2, bytes2[offset2], ATypeTag.SERIALIZED_YEAR_MONTH_DURATION_TYPE_TAG, ATypeTag.SERIALIZED_DAY_TIME_DURATION_TYPE_TAG);
                    }
                    long binStartChronon;
                    long binEndChronon;
                    int binOffset;
                    listBuilder.reset(intListType);
                    try {
                        if (intervalTypeTag == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
                            binOffset = 0;
                            do {
                                binStartChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * (int) (firstBinIndex + binOffset), dayTime * (firstBinIndex + binOffset), false);
                                binEndChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * ((int) (firstBinIndex + binOffset) + 1), dayTime * ((firstBinIndex + binOffset) + 1), false);
                                binStartChronon = binStartChronon / GregorianCalendarSystem.CHRONON_OF_DAY + ((binStartChronon < 0 && binStartChronon % GregorianCalendarSystem.CHRONON_OF_DAY != 0) ? -1 : 0);
                                binEndChronon = binEndChronon / GregorianCalendarSystem.CHRONON_OF_DAY + ((binEndChronon < 0 && binEndChronon % GregorianCalendarSystem.CHRONON_OF_DAY != 0) ? -1 : 0);
                                aInterval.setValue(binStartChronon, binEndChronon, intervalTypeTag);
                                listStorage.reset();
                                intervalSerde.serialize(aInterval, listStorage.getDataOutput());
                                listBuilder.addItem(listStorage);
                                binOffset++;
                            } while (binEndChronon < intervalEnd);
                        } else if (intervalTypeTag == ATypeTag.SERIALIZED_TIME_TYPE_TAG) {
                            if (yearMonth != 0) {
                                throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                            }
                            binOffset = 0;
                            binStartChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * (int) (firstBinIndex + binOffset), dayTime * (firstBinIndex + binOffset), true);
                            binEndChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * ((int) (firstBinIndex + binOffset) + 1), dayTime * ((firstBinIndex + binOffset) + 1), true);
                            if (binStartChronon < 0 || binStartChronon >= GregorianCalendarSystem.CHRONON_OF_DAY) {
                                // avoid the case where a time bin is before 00:00:00 or no early than 24:00:00
                                throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                            }
                            while (!((binStartChronon < intervalStart && binEndChronon <= intervalStart) || (binStartChronon >= intervalEnd && binEndChronon > intervalEnd))) {
                                aInterval.setValue(binStartChronon, binEndChronon, intervalTypeTag);
                                listStorage.reset();
                                intervalSerde.serialize(aInterval, listStorage.getDataOutput());
                                listBuilder.addItem(listStorage);
                                binOffset++;
                                binStartChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * (int) (firstBinIndex + binOffset), dayTime * (firstBinIndex + binOffset), true);
                                binEndChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * ((int) (firstBinIndex + binOffset) + 1), dayTime * ((firstBinIndex + binOffset) + 1), true);
                                if (binStartChronon == GregorianCalendarSystem.CHRONON_OF_DAY) {
                                    break;
                                }
                                if (binEndChronon < binStartChronon) {
                                    throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                                }
                            }
                        } else if (intervalTypeTag == ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
                            binOffset = 0;
                            do {
                                binStartChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * (int) (firstBinIndex + binOffset), dayTime * (firstBinIndex + binOffset), false);
                                binEndChronon = DurationArithmeticOperations.addDuration(anchorTime, yearMonth * ((int) (firstBinIndex + binOffset) + 1), dayTime * ((firstBinIndex + binOffset) + 1), false);
                                aInterval.setValue(binStartChronon, binEndChronon, intervalTypeTag);
                                listStorage.reset();
                                intervalSerde.serialize(aInterval, listStorage.getDataOutput());
                                listBuilder.addItem(listStorage);
                                binOffset++;
                            } while (binEndChronon < intervalEnd);
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DATE_TYPE_TAG, ATypeTag.SERIALIZED_TIME_TYPE_TAG, ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
                        }
                        listBuilder.write(out, true);
                    } catch (IOException e1) {
                        throw new HyracksDataException(e1);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) InvalidDataFormatException(org.apache.asterix.runtime.exceptions.InvalidDataFormatException) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IncompatibleTypeException(org.apache.asterix.runtime.exceptions.IncompatibleTypeException) GregorianCalendarSystem(org.apache.asterix.om.base.temporal.GregorianCalendarSystem) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) IOException(java.io.IOException) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) ATypeTag(org.apache.asterix.om.types.ATypeTag) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) UnderflowException(org.apache.asterix.runtime.exceptions.UnderflowException) AMutableInterval(org.apache.asterix.om.base.AMutableInterval) OverflowException(org.apache.asterix.runtime.exceptions.OverflowException)

Example 9 with AOrderedListType

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

the class JObjectUtil method getJType.

public static IJObject getJType(ATypeTag typeTag, IAType type, ByteArrayAccessibleDataInputStream dis, IObjectPool<IJObject, IAType> objectPool) throws IOException {
    IJObject jObject;
    switch(typeTag) {
        case INTEGER:
            {
                int v = dis.readInt();
                jObject = objectPool.allocate(BuiltinType.AINT32);
                ((JInt) jObject).setValue(v);
                break;
            }
        case FLOAT:
            {
                float v = dis.readFloat();
                jObject = objectPool.allocate(BuiltinType.AFLOAT);
                ((JFloat) jObject).setValue(v);
                break;
            }
        case DOUBLE:
            {
                double value = dis.readDouble();
                jObject = objectPool.allocate(BuiltinType.ADOUBLE);
                ((JDouble) jObject).setValue(value);
                break;
            }
        case STRING:
            {
                String v = dis.readUTF();
                jObject = objectPool.allocate(BuiltinType.ASTRING);
                ((JString) jObject).setValue(v);
                break;
            }
        case BOOLEAN:
            jObject = objectPool.allocate(BuiltinType.ABOOLEAN);
            ((JBoolean) jObject).setValue(dis.readBoolean());
            break;
        case DATE:
            {
                int d = dis.readInt();
                jObject = objectPool.allocate(BuiltinType.ADATE);
                ((JDate) jObject).setValue(d);
                break;
            }
        case DATETIME:
            {
                jObject = objectPool.allocate(BuiltinType.ADATETIME);
                long value = dis.readLong();
                ((JDateTime) jObject).setValue(value);
                break;
            }
        case DURATION:
            {
                jObject = objectPool.allocate(BuiltinType.ADURATION);
                int months = dis.readInt();
                long msecs = dis.readLong();
                ((JDuration) jObject).setValue(months, msecs);
                break;
            }
        case TIME:
            {
                jObject = objectPool.allocate(BuiltinType.ATIME);
                int time = dis.readInt();
                ((JTime) jObject).setValue(time);
                break;
            }
        case INTERVAL:
            {
                jObject = objectPool.allocate(BuiltinType.AINTERVAL);
                long start = dis.readLong();
                long end = dis.readLong();
                byte intervalType = dis.readByte();
                ((JInterval) jObject).setValue(start, end, intervalType);
                break;
            }
        case CIRCLE:
            {
                jObject = objectPool.allocate(BuiltinType.ACIRCLE);
                double x = dis.readDouble();
                double y = dis.readDouble();
                double radius = dis.readDouble();
                JPoint jpoint = (JPoint) objectPool.allocate(BuiltinType.APOINT);
                jpoint.setValue(x, y);
                ((JCircle) jObject).setValue(jpoint, radius);
                break;
            }
        case POINT:
            {
                jObject = objectPool.allocate(BuiltinType.APOINT);
                double x = dis.readDouble();
                double y = dis.readDouble();
                ((JPoint) jObject).setValue(x, y);
                break;
            }
        case POINT3D:
            {
                jObject = objectPool.allocate(BuiltinType.APOINT3D);
                double x = dis.readDouble();
                double y = dis.readDouble();
                double z = dis.readDouble();
                ((JPoint3D) jObject).setValue(x, y, z);
                break;
            }
        case LINE:
            {
                jObject = objectPool.allocate(BuiltinType.ALINE);
                double x1 = dis.readDouble();
                double y1 = dis.readDouble();
                double x2 = dis.readDouble();
                double y2 = dis.readDouble();
                JPoint jpoint1 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
                jpoint1.setValue(x1, y1);
                JPoint jpoint2 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
                jpoint2.setValue(x2, y2);
                ((JLine) jObject).setValue(jpoint1, jpoint2);
                break;
            }
        case POLYGON:
            {
                jObject = objectPool.allocate(BuiltinType.APOLYGON);
                short numberOfPoints = dis.readShort();
                List<JPoint> points = new ArrayList<JPoint>();
                for (int i = 0; i < numberOfPoints; i++) {
                    JPoint p1 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
                    p1.setValue(dis.readDouble(), dis.readDouble());
                    points.add(p1);
                }
                ((JPolygon) jObject).setValue(points.toArray(new APoint[] {}));
                break;
            }
        case RECTANGLE:
            {
                jObject = objectPool.allocate(BuiltinType.ARECTANGLE);
                double x1 = dis.readDouble();
                double y1 = dis.readDouble();
                double x2 = dis.readDouble();
                double y2 = dis.readDouble();
                JPoint jpoint1 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
                jpoint1.setValue(x1, y1);
                JPoint jpoint2 = (JPoint) objectPool.allocate(BuiltinType.APOINT);
                jpoint2.setValue(x2, y2);
                ((JRectangle) jObject).setValue(jpoint1, jpoint2);
                break;
            }
        case MULTISET:
            {
                AUnorderedListType listType = (AUnorderedListType) type;
                IAType elementType = listType.getItemType();
                jObject = objectPool.allocate(listType);
                boolean fixedSize = false;
                ATypeTag tag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(dis.readByte());
                switch(tag) {
                    case STRING:
                    case OBJECT:
                    case ARRAY:
                    case MULTISET:
                    case ANY:
                        fixedSize = false;
                        break;
                    default:
                        fixedSize = true;
                        break;
                }
                // list size
                dis.readInt();
                int numberOfitems;
                numberOfitems = dis.readInt();
                if (numberOfitems <= 0) {
                    break;
                }
                if (!fixedSize) {
                    for (int i = 0; i < numberOfitems; i++) {
                        dis.readInt();
                    }
                }
                for (int i = 0; i < numberOfitems; i++) {
                    IJObject v = getJType(elementType.getTypeTag(), elementType, dis, objectPool);
                    ((JUnorderedList) jObject).add(v);
                }
                break;
            }
        case ARRAY:
            {
                AOrderedListType listType = (AOrderedListType) type;
                IAType elementType = listType.getItemType();
                jObject = objectPool.allocate(listType);
                boolean fixedSize = false;
                ATypeTag tag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(dis.readByte());
                switch(tag) {
                    case STRING:
                    case OBJECT:
                    case ARRAY:
                    case MULTISET:
                    case ANY:
                        fixedSize = false;
                        break;
                    default:
                        fixedSize = true;
                        break;
                }
                // list size
                dis.readInt();
                int numberOfitems;
                numberOfitems = dis.readInt();
                if (numberOfitems <= 0) {
                    break;
                }
                if (!fixedSize) {
                    for (int i = 0; i < numberOfitems; i++) {
                        dis.readInt();
                    }
                }
                for (int i = 0; i < numberOfitems; i++) {
                    IJObject v = getJType(elementType.getTypeTag(), elementType, dis, objectPool);
                    ((JOrderedList) jObject).add(v);
                }
                break;
            }
        case OBJECT:
            ARecordType recordType = (ARecordType) type;
            int numberOfSchemaFields = recordType.getFieldTypes().length;
            byte[] recordBits = dis.getInputStream().getArray();
            boolean isExpanded = false;
            dis.getInputStream();
            int[] fieldOffsets = new int[numberOfSchemaFields];
            IJObject[] closedFields = new IJObject[numberOfSchemaFields];
            // reading length is not required.
            dis.skip(4);
            if (recordType.isOpen()) {
                isExpanded = dis.readBoolean();
                if (isExpanded) {
                    dis.readInt();
                } else {
                }
            } else {
            }
            if (numberOfSchemaFields > 0) {
                dis.readInt();
                int nullBitMapOffset = 0;
                boolean hasOptionalFields = NonTaggedFormatUtil.hasOptionalField(recordType);
                if (hasOptionalFields) {
                    nullBitMapOffset = dis.getInputStream().getPosition();
                    dis.getInputStream();
                } else {
                    dis.getInputStream();
                }
                for (int i = 0; i < numberOfSchemaFields; i++) {
                    fieldOffsets[i] = dis.readInt();
                }
                for (int fieldNumber = 0; fieldNumber < numberOfSchemaFields; fieldNumber++) {
                    if (hasOptionalFields) {
                        byte b1 = recordBits[nullBitMapOffset + fieldNumber / 8];
                        int p = 1 << (7 - (fieldNumber % 8));
                        if ((b1 & p) == 0) {
                            continue;
                        }
                    }
                    IAType[] fieldTypes = recordType.getFieldTypes();
                    ATypeTag fieldValueTypeTag = null;
                    IAType fieldType = fieldTypes[fieldNumber];
                    if (fieldTypes[fieldNumber].getTypeTag() == ATypeTag.UNION) {
                        if (((AUnionType) fieldTypes[fieldNumber]).isUnknownableType()) {
                            fieldType = ((AUnionType) fieldTypes[fieldNumber]).getActualType();
                            fieldValueTypeTag = fieldType.getTypeTag();
                        }
                    } else {
                        fieldValueTypeTag = fieldTypes[fieldNumber].getTypeTag();
                    }
                    closedFields[fieldNumber] = getJType(fieldValueTypeTag, fieldType, dis, objectPool);
                }
            }
            if (isExpanded) {
                int numberOfOpenFields = dis.readInt();
                String[] fieldNames = new String[numberOfOpenFields];
                IAType[] fieldTypes = new IAType[numberOfOpenFields];
                IJObject[] openFields = new IJObject[numberOfOpenFields];
                for (int i = 0; i < numberOfOpenFields; i++) {
                    dis.readInt();
                    dis.readInt();
                }
                for (int i = 0; i < numberOfOpenFields; i++) {
                    fieldNames[i] = AStringSerializerDeserializer.INSTANCE.deserialize(dis).getStringValue();
                    ATypeTag openFieldTypeTag = SerializerDeserializerUtil.deserializeTag(dis);
                    openFields[i] = getJType(openFieldTypeTag, null, dis, objectPool);
                    fieldTypes[i] = openFields[i].getIAObject().getType();
                }
                ARecordType openPartRecType = new ARecordType(null, fieldNames, fieldTypes, true);
                if (numberOfSchemaFields > 0) {
                    ARecordType mergedRecordType = mergeRecordTypes(recordType, openPartRecType);
                    IJObject[] mergedFields = mergeFields(closedFields, openFields);
                    jObject = objectPool.allocate(recordType);
                    return new JRecord(mergedRecordType, mergedFields);
                } else {
                    return new JRecord(recordType, openFields);
                }
            } else {
                return new JRecord(recordType, closedFields);
            }
        default:
            throw new RuntimeDataException(ErrorCode.LIBRARY_JOBJECT_UTIL_ILLEGAL_ARGU_TYPE, typeTag);
    }
    return jObject;
}
Also used : JRecord(org.apache.asterix.external.library.java.JObjects.JRecord) JPoint(org.apache.asterix.external.library.java.JObjects.JPoint) AUnionType(org.apache.asterix.om.types.AUnionType) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) IJObject(org.apache.asterix.external.api.IJObject) JString(org.apache.asterix.external.library.java.JObjects.JString) AUnorderedListType(org.apache.asterix.om.types.AUnorderedListType) JPoint(org.apache.asterix.external.library.java.JObjects.JPoint) APoint(org.apache.asterix.om.base.APoint) JBoolean(org.apache.asterix.external.library.java.JObjects.JBoolean) ATypeTag(org.apache.asterix.om.types.ATypeTag) JUnorderedList(org.apache.asterix.external.library.java.JObjects.JUnorderedList) ArrayList(java.util.ArrayList) JOrderedList(org.apache.asterix.external.library.java.JObjects.JOrderedList) List(java.util.List) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 10 with AOrderedListType

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

the class InternalDatasetDetails method writeDatasetDetailsRecordType.

@Override
public void writeDatasetDetailsRecordType(DataOutput out) throws HyracksDataException {
    IARecordBuilder internalRecordBuilder = new RecordBuilder();
    OrderedListBuilder listBuilder = new OrderedListBuilder();
    ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
    ArrayBackedValueStorage itemValue = new ArrayBackedValueStorage();
    OrderedListBuilder primaryKeyListBuilder = new OrderedListBuilder();
    AOrderedListType stringList = new AOrderedListType(BuiltinType.ASTRING, null);
    AOrderedListType int8List = new AOrderedListType(BuiltinType.AINT8, null);
    AOrderedListType heterogeneousList = new AOrderedListType(BuiltinType.ANY, null);
    internalRecordBuilder.reset(MetadataRecordTypes.INTERNAL_DETAILS_RECORDTYPE);
    AMutableString aString = new AMutableString("");
    AMutableInt8 aInt8 = new AMutableInt8((byte) 0);
    @SuppressWarnings("unchecked") ISerializerDeserializer<ABoolean> booleanSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ABOOLEAN);
    @SuppressWarnings("unchecked") ISerializerDeserializer<AString> stringSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ASTRING);
    @SuppressWarnings("unchecked") ISerializerDeserializer<AInt8> int8Serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT8);
    // write field 0
    fieldValue.reset();
    aString.setValue(getFileStructure().toString());
    stringSerde.serialize(aString, fieldValue.getDataOutput());
    internalRecordBuilder.addField(MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_FILESTRUCTURE_FIELD_INDEX, fieldValue);
    // write field 1
    fieldValue.reset();
    aString.setValue(getPartitioningStrategy().toString());
    stringSerde.serialize(aString, fieldValue.getDataOutput());
    internalRecordBuilder.addField(MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_PARTITIONSTRATEGY_FIELD_INDEX, fieldValue);
    // write field 2
    primaryKeyListBuilder.reset((AOrderedListType) MetadataRecordTypes.INTERNAL_DETAILS_RECORDTYPE.getFieldTypes()[MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_PARTITIONKEY_FIELD_INDEX]);
    for (List<String> field : partitioningKeys) {
        listBuilder.reset(stringList);
        for (String subField : field) {
            itemValue.reset();
            aString.setValue(subField);
            stringSerde.serialize(aString, itemValue.getDataOutput());
            listBuilder.addItem(itemValue);
        }
        itemValue.reset();
        listBuilder.write(itemValue.getDataOutput(), true);
        primaryKeyListBuilder.addItem(itemValue);
    }
    fieldValue.reset();
    primaryKeyListBuilder.write(fieldValue.getDataOutput(), true);
    internalRecordBuilder.addField(MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_PARTITIONKEY_FIELD_INDEX, fieldValue);
    // write field 3
    primaryKeyListBuilder.reset((AOrderedListType) MetadataRecordTypes.INTERNAL_DETAILS_RECORDTYPE.getFieldTypes()[MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_PRIMARYKEY_FIELD_INDEX]);
    for (List<String> field : primaryKeys) {
        listBuilder.reset(stringList);
        for (String subField : field) {
            itemValue.reset();
            aString.setValue(subField);
            stringSerde.serialize(aString, itemValue.getDataOutput());
            listBuilder.addItem(itemValue);
        }
        itemValue.reset();
        listBuilder.write(itemValue.getDataOutput(), true);
        primaryKeyListBuilder.addItem(itemValue);
    }
    fieldValue.reset();
    primaryKeyListBuilder.write(fieldValue.getDataOutput(), true);
    internalRecordBuilder.addField(MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_PRIMARYKEY_FIELD_INDEX, fieldValue);
    // write field 4
    fieldValue.reset();
    ABoolean b = isAutogenerated() ? ABoolean.TRUE : ABoolean.FALSE;
    booleanSerde.serialize(b, fieldValue.getDataOutput());
    internalRecordBuilder.addField(MetadataRecordTypes.INTERNAL_DETAILS_ARECORD_AUTOGENERATED_FIELD_INDEX, fieldValue);
    // write filter fields if any
    List<String> filterField = getFilterField();
    if (filterField != null) {
        listBuilder.reset(heterogeneousList);
        ArrayBackedValueStorage nameValue = new ArrayBackedValueStorage();
        nameValue.reset();
        aString.setValue(FILTER_FIELD_NAME);
        stringSerde.serialize(aString, nameValue.getDataOutput());
        for (String field : filterField) {
            itemValue.reset();
            aString.setValue(field);
            stringSerde.serialize(aString, itemValue.getDataOutput());
            listBuilder.addItem(itemValue);
        }
        fieldValue.reset();
        listBuilder.write(fieldValue.getDataOutput(), true);
        internalRecordBuilder.addField(nameValue, fieldValue);
    }
    // write key source indicators if any
    List<Integer> keySourceIndicator = getKeySourceIndicator();
    boolean needSerialization = false;
    if (keySourceIndicator != null) {
        for (int source : keySourceIndicator) {
            if (source != 0) {
                needSerialization = true;
                break;
            }
        }
    }
    if (needSerialization) {
        listBuilder.reset(int8List);
        ArrayBackedValueStorage nameValue = new ArrayBackedValueStorage();
        nameValue.reset();
        aString.setValue(KEY_FILD_SOURCE_INDICATOR_FIELD_NAME);
        stringSerde.serialize(aString, nameValue.getDataOutput());
        for (int source : keySourceIndicator) {
            itemValue.reset();
            aInt8.setValue((byte) source);
            int8Serde.serialize(aInt8, itemValue.getDataOutput());
            listBuilder.addItem(itemValue);
        }
        fieldValue.reset();
        listBuilder.write(fieldValue.getDataOutput(), true);
        internalRecordBuilder.addField(nameValue, fieldValue);
    }
    internalRecordBuilder.write(out, true);
}
Also used : OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) AMutableInt8(org.apache.asterix.om.base.AMutableInt8) ABoolean(org.apache.asterix.om.base.ABoolean) RecordBuilder(org.apache.asterix.builders.RecordBuilder) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) AMutableString(org.apache.asterix.om.base.AMutableString) AString(org.apache.asterix.om.base.AString) AMutableString(org.apache.asterix.om.base.AMutableString) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) AInt8(org.apache.asterix.om.base.AInt8) AString(org.apache.asterix.om.base.AString)

Aggregations

AOrderedListType (org.apache.asterix.om.types.AOrderedListType)26 IAType (org.apache.asterix.om.types.IAType)16 ARecordType (org.apache.asterix.om.types.ARecordType)13 ArrayList (java.util.ArrayList)9 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)8 OrderedListBuilder (org.apache.asterix.builders.OrderedListBuilder)7 DataOutput (java.io.DataOutput)6 AString (org.apache.asterix.om.base.AString)6 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)6 AOrderedList (org.apache.asterix.om.base.AOrderedList)5 AUnionType (org.apache.asterix.om.types.AUnionType)5 Test (org.junit.Test)5 IOException (java.io.IOException)4 ATypeTag (org.apache.asterix.om.types.ATypeTag)4 AUnorderedListType (org.apache.asterix.om.types.AUnorderedListType)4 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)4 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)4 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)4 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)4 IPointable (org.apache.hyracks.data.std.api.IPointable)4