Search in sources :

Example 1 with OrderedListBuilder

use of org.apache.asterix.builders.OrderedListBuilder 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)

Example 2 with OrderedListBuilder

use of org.apache.asterix.builders.OrderedListBuilder in project asterixdb by apache.

the class DatatypeTupleTranslator method writeRecordType.

private void writeRecordType(Datatype instance, AbstractComplexType type, DataOutput out) throws HyracksDataException {
    ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
    ArrayBackedValueStorage itemValue = new ArrayBackedValueStorage();
    IARecordBuilder recordRecordBuilder = new RecordBuilder();
    IARecordBuilder fieldRecordBuilder = new RecordBuilder();
    ARecordType recType = (ARecordType) type;
    OrderedListBuilder listBuilder = new OrderedListBuilder();
    listBuilder.reset(new AOrderedListType(MetadataRecordTypes.FIELD_RECORDTYPE, null));
    IAType fieldType = null;
    for (int i = 0; i < recType.getFieldNames().length; i++) {
        fieldType = recType.getFieldTypes()[i];
        boolean fieldIsNullable = false;
        if (NonTaggedFormatUtil.isOptional(fieldType)) {
            fieldIsNullable = true;
            fieldType = ((AUnionType) fieldType).getActualType();
        }
        if (fieldType.getTypeTag().isDerivedType()) {
            handleNestedDerivedType(fieldType.getTypeName(), (AbstractComplexType) fieldType, instance, instance.getDataverseName(), instance.getDatatypeName());
        }
        itemValue.reset();
        fieldRecordBuilder.reset(MetadataRecordTypes.FIELD_RECORDTYPE);
        // write field 0
        fieldValue.reset();
        aString.setValue(recType.getFieldNames()[i]);
        stringSerde.serialize(aString, fieldValue.getDataOutput());
        fieldRecordBuilder.addField(MetadataRecordTypes.FIELD_ARECORD_FIELDNAME_FIELD_INDEX, fieldValue);
        // write field 1
        fieldValue.reset();
        aString.setValue(fieldType.getTypeName());
        stringSerde.serialize(aString, fieldValue.getDataOutput());
        fieldRecordBuilder.addField(MetadataRecordTypes.FIELD_ARECORD_FIELDTYPE_FIELD_INDEX, fieldValue);
        // write field 2
        fieldValue.reset();
        booleanSerde.serialize(fieldIsNullable ? ABoolean.TRUE : ABoolean.FALSE, fieldValue.getDataOutput());
        fieldRecordBuilder.addField(MetadataRecordTypes.FIELD_ARECORD_ISNULLABLE_FIELD_INDEX, fieldValue);
        // write record
        fieldRecordBuilder.write(itemValue.getDataOutput(), true);
        // add item to the list of fields
        listBuilder.addItem(itemValue);
    }
    recordRecordBuilder.reset(MetadataRecordTypes.RECORD_RECORDTYPE);
    // write field 0
    fieldValue.reset();
    booleanSerde.serialize(recType.isOpen() ? ABoolean.TRUE : ABoolean.FALSE, fieldValue.getDataOutput());
    recordRecordBuilder.addField(MetadataRecordTypes.RECORDTYPE_ARECORD_ISOPEN_FIELD_INDEX, fieldValue);
    // write field 1
    fieldValue.reset();
    listBuilder.write(fieldValue.getDataOutput(), true);
    recordRecordBuilder.addField(MetadataRecordTypes.RECORDTYPE_ARECORD_FIELDS_FIELD_INDEX, fieldValue);
    // write record
    recordRecordBuilder.write(out, true);
}
Also used : ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) IARecordBuilder(org.apache.asterix.builders.IARecordBuilder) RecordBuilder(org.apache.asterix.builders.RecordBuilder) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 3 with OrderedListBuilder

use of org.apache.asterix.builders.OrderedListBuilder in project asterixdb by apache.

the class StringSplitDescriptor 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() {

                // Argument evaluators.
                private final IScalarEvaluator stringEval = args[0].createScalarEvaluator(ctx);

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

                // Argument pointers.
                private final IPointable argString = new VoidPointable();

                private final IPointable argPattern = new VoidPointable();

                private final UTF8StringPointable argStrPtr = new UTF8StringPointable();

                private final UTF8StringPointable argPatternPtr = new UTF8StringPointable();

                // For an output string item.
                private final ArrayBackedValueStorage itemStorge = new ArrayBackedValueStorage();

                private final DataOutput itemOut = itemStorge.getDataOutput();

                private final byte[] tempLengthArray = new byte[5];

                // For the output list of strings.
                private final AOrderedListType intListType = new AOrderedListType(BuiltinType.ASTRING, null);

                private final OrderedListBuilder listBuilder = new OrderedListBuilder();

                private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private final DataOutput out = resultStorage.getDataOutput();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    try {
                        resultStorage.reset();
                        // Calls argument evaluators.
                        stringEval.evaluate(tuple, argString);
                        patternEval.evaluate(tuple, argPattern);
                        // Gets the bytes of the source string.
                        byte[] srcString = argString.getByteArray();
                        int srcOffset = argString.getStartOffset();
                        int srcLen = argString.getLength();
                        // Type check for the first argument.
                        if (srcString[srcOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 0, srcString[srcOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        // Gets the bytes of the pattern string.
                        byte[] patternString = argPattern.getByteArray();
                        int patternOffset = argPattern.getStartOffset();
                        int patternLen = argPattern.getLength();
                        // Type check for the second argument.
                        if (patternString[patternOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 1, patternString[patternOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        // Sets the UTF8 String pointables.
                        argStrPtr.set(srcString, srcOffset + 1, srcLen - 1);
                        argPatternPtr.set(patternString, patternOffset + 1, patternLen - 1);
                        // Gets the string length of the source string.
                        int inputStringLen = UTF8StringUtil.getUTFLength(srcString, srcOffset + 1);
                        int inputStringStart = srcOffset + 1 + UTF8StringUtil.getNumBytesToStoreLength(inputStringLen);
                        // Gets the string length of the pattern string.
                        int inputPatternLen = UTF8StringUtil.getUTFLength(patternString, patternOffset + 1);
                        // Handles the case that the pattern is "".
                        boolean emptyStringPattern = inputPatternLen == 0;
                        // Builds a list of strings.
                        listBuilder.reset(intListType);
                        int itemStrStart = 0;
                        int nextMatchStart;
                        while (itemStrStart < inputStringLen && (nextMatchStart = UTF8StringPointable.find(argStrPtr, argPatternPtr, false, itemStrStart)) >= 0) {
                            // Adds an item string.
                            addItemString(srcString, inputStringStart, itemStrStart, emptyStringPattern ? nextMatchStart + 1 : nextMatchStart);
                            itemStrStart = nextMatchStart + (emptyStringPattern ? 1 : inputPatternLen);
                        }
                        if (!emptyStringPattern) {
                            addItemString(srcString, inputStringStart, itemStrStart, inputStringLen);
                        }
                        listBuilder.write(out, true);
                        result.set(resultStorage);
                    } catch (IOException e1) {
                        throw new HyracksDataException(e1);
                    }
                }

                private void addItemString(byte[] srcString, int inputStringStart, int itemStartOffset, int nextMatchStart) throws IOException {
                    int itemLen = nextMatchStart - itemStartOffset;
                    int cbytes = UTF8StringUtil.encodeUTF8Length(itemLen, tempLengthArray, 0);
                    itemStorge.reset();
                    itemOut.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                    itemOut.write(tempLengthArray, 0, cbytes);
                    if (itemLen > 0) {
                        itemOut.write(srcString, inputStringStart + itemStartOffset, itemLen);
                    }
                    listBuilder.addItem(itemStorge);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)

Example 4 with OrderedListBuilder

use of org.apache.asterix.builders.OrderedListBuilder in project asterixdb by apache.

the class StringToCodePointDescriptor 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() {

                protected final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                protected final DataOutput out = resultStorage.getDataOutput();

                protected final IPointable argPtr = new VoidPointable();

                protected final IScalarEvaluator stringEval = args[0].createScalarEvaluator(ctx);

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

                private OrderedListBuilder listBuilder = new OrderedListBuilder();

                private ArrayBackedValueStorage inputVal = new ArrayBackedValueStorage();

                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<AInt64> int64Serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);

                private final AMutableInt64 aInt64 = new AMutableInt64(0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    try {
                        resultStorage.reset();
                        stringEval.evaluate(tuple, argPtr);
                        byte[] serString = argPtr.getByteArray();
                        int offset = argPtr.getStartOffset();
                        if (serString[offset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            int len = UTF8StringUtil.getUTFLength(serString, offset + 1);
                            int start = offset + 1 + UTF8StringUtil.getNumBytesToStoreLength(len);
                            int pos = 0;
                            listBuilder.reset(intListType);
                            while (pos < len) {
                                int codePoint = UTF8StringUtil.UTF8ToCodePoint(serString, start + pos);
                                pos += UTF8StringUtil.charSize(serString, start + pos);
                                inputVal.reset();
                                aInt64.setValue(codePoint);
                                int64Serde.serialize(aInt64, inputVal.getDataOutput());
                                listBuilder.addItem(inputVal);
                            }
                            listBuilder.write(out, true);
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, serString[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        result.set(resultStorage);
                    } catch (IOException e1) {
                        throw new HyracksDataException(e1);
                    }
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) 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) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Example 5 with OrderedListBuilder

use of org.apache.asterix.builders.OrderedListBuilder 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)

Aggregations

OrderedListBuilder (org.apache.asterix.builders.OrderedListBuilder)20 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)18 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)7 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)7 IOException (java.io.IOException)6 AString (org.apache.asterix.om.base.AString)6 IAType (org.apache.asterix.om.types.IAType)6 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)6 IPointable (org.apache.hyracks.data.std.api.IPointable)6 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)6 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)6 DataOutput (java.io.DataOutput)5 IARecordBuilder (org.apache.asterix.builders.IARecordBuilder)5 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)5 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)5 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)5 RecordBuilder (org.apache.asterix.builders.RecordBuilder)4 AMutableString (org.apache.asterix.om.base.AMutableString)3 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)3 HashMap (java.util.HashMap)2