Search in sources :

Example 96 with ARecordType

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

the class ARecordSerializerDeserializer method deserialize.

@Override
public ARecord deserialize(DataInput in) throws HyracksDataException {
    try {
        boolean isExpanded = isExpandedRecord(in);
        IAObject[] schemaFields = getValuesForSchemaFields(in);
        if (isExpanded) {
            int numberOfOpenFields = in.readInt();
            String[] fieldNames = new String[numberOfOpenFields];
            IAType[] fieldTypes = new IAType[numberOfOpenFields];
            IAObject[] openFields = new IAObject[numberOfOpenFields];
            for (int i = 0; i < numberOfOpenFields; i++) {
                in.readInt();
                in.readInt();
            }
            for (int i = 0; i < numberOfOpenFields; i++) {
                fieldNames[i] = AStringSerializerDeserializer.INSTANCE.deserialize(in).getStringValue();
                openFields[i] = AObjectSerializerDeserializer.INSTANCE.deserialize(in);
                fieldTypes[i] = openFields[i].getType();
            }
            ARecordType openPartRecType = new ARecordType(null, fieldNames, fieldTypes, true);
            if (numberOfSchemaFields > 0) {
                ARecordType mergedRecordType = mergeRecordTypes(this.recordType, openPartRecType);
                IAObject[] mergedFields = mergeFields(schemaFields, openFields);
                return new ARecord(mergedRecordType, mergedFields);
            } else {
                return new ARecord(openPartRecType, openFields);
            }
        } else {
            return new ARecord(this.recordType, schemaFields);
        }
    } catch (IOException e) {
        throw new HyracksDataException(e);
    }
}
Also used : ARecord(org.apache.asterix.om.base.ARecord) IAObject(org.apache.asterix.om.base.IAObject) AString(org.apache.asterix.om.base.AString) IOException(java.io.IOException) ARecordType(org.apache.asterix.om.types.ARecordType) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IAType(org.apache.asterix.om.types.IAType)

Example 97 with ARecordType

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

the class RecordMergeDescriptor 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 {
            final PointableAllocator pa = new PointableAllocator();
            final IVisitablePointable vp0 = pa.allocateRecordValue(inRecType0);
            final IVisitablePointable vp1 = pa.allocateRecordValue(inRecType1);
            final IPointable argPtr0 = new VoidPointable();
            final IPointable argPtr1 = new VoidPointable();
            final IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);
            final IScalarEvaluator eval1 = args[1].createScalarEvaluator(ctx);
            final List<RecordBuilder> rbStack = new ArrayList<>();
            final ArrayBackedValueStorage tabvs = new ArrayBackedValueStorage();
            return new IScalarEvaluator() {

                private final RuntimeRecordTypeInfo runtimeRecordTypeInfo = new RuntimeRecordTypeInfo();

                private final DeepEqualAssessor deepEqualAssesor = new DeepEqualAssessor();

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    vp0.set(argPtr0);
                    vp1.set(argPtr1);
                    ARecordVisitablePointable rp0 = (ARecordVisitablePointable) vp0;
                    ARecordVisitablePointable rp1 = (ARecordVisitablePointable) vp1;
                    try {
                        mergeFields(outRecType, rp0, rp1, true, 0);
                        rbStack.get(0).write(out, true);
                    } catch (IOException | AsterixException e) {
                        throw new HyracksDataException(e);
                    }
                    result.set(resultStorage);
                }

                private void mergeFields(ARecordType combinedType, ARecordVisitablePointable leftRecord, ARecordVisitablePointable rightRecord, boolean openFromParent, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
                    if (rbStack.size() < (nestedLevel + 1)) {
                        rbStack.add(new RecordBuilder());
                    }
                    rbStack.get(nestedLevel).reset(combinedType);
                    rbStack.get(nestedLevel).init();
                    //Add all fields from left record
                    for (int i = 0; i < leftRecord.getFieldNames().size(); i++) {
                        IVisitablePointable leftName = leftRecord.getFieldNames().get(i);
                        IVisitablePointable leftValue = leftRecord.getFieldValues().get(i);
                        IVisitablePointable leftType = leftRecord.getFieldTypeTags().get(i);
                        boolean foundMatch = false;
                        for (int j = 0; j < rightRecord.getFieldNames().size(); j++) {
                            IVisitablePointable rightName = rightRecord.getFieldNames().get(j);
                            IVisitablePointable rightValue = rightRecord.getFieldValues().get(j);
                            IVisitablePointable rightType = rightRecord.getFieldTypeTags().get(j);
                            // Check if same fieldname
                            if (PointableHelper.isEqual(leftName, rightName) && !deepEqualAssesor.isEqual(leftValue, rightValue)) {
                                //Field was found on the right and are subrecords, merge them
                                if (PointableHelper.sameType(ATypeTag.OBJECT, rightType) && PointableHelper.sameType(ATypeTag.OBJECT, leftType)) {
                                    //We are merging two sub records
                                    addFieldToSubRecord(combinedType, leftName, leftValue, rightValue, openFromParent, nestedLevel);
                                    foundMatch = true;
                                } else {
                                    throw new RuntimeDataException(ErrorCode.DUPLICATE_FIELD_NAME, getIdentifier());
                                }
                            }
                        }
                        if (!foundMatch) {
                            addFieldToSubRecord(combinedType, leftName, leftValue, null, openFromParent, nestedLevel);
                        }
                    }
                    //Repeat for right side (ignoring duplicates this time)
                    for (int j = 0; j < rightRecord.getFieldNames().size(); j++) {
                        IVisitablePointable rightName = rightRecord.getFieldNames().get(j);
                        IVisitablePointable rightValue = rightRecord.getFieldValues().get(j);
                        boolean foundMatch = false;
                        for (int i = 0; i < leftRecord.getFieldNames().size(); i++) {
                            IVisitablePointable leftName = leftRecord.getFieldNames().get(i);
                            if (rightName.equals(leftName)) {
                                foundMatch = true;
                            }
                        }
                        if (!foundMatch) {
                            addFieldToSubRecord(combinedType, rightName, rightValue, null, openFromParent, nestedLevel);
                        }
                    }
                }

                /*
                     * Takes in a record type, field name, and the field values (which are record) from two records
                     * Merges them into one record of combinedType
                     * And adds that record as a field to the Record in subrb
                     * the second value can be null, indicated that you just add the value of left as a field to subrb
                     *
                     */
                private void addFieldToSubRecord(ARecordType combinedType, IVisitablePointable fieldNamePointable, IVisitablePointable leftValue, IVisitablePointable rightValue, boolean openFromParent, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
                    runtimeRecordTypeInfo.reset(combinedType);
                    int pos = runtimeRecordTypeInfo.getFieldIndex(fieldNamePointable.getByteArray(), fieldNamePointable.getStartOffset() + 1, fieldNamePointable.getLength() - 1);
                    //Add the merged field
                    if (combinedType != null && pos >= 0) {
                        if (rightValue == null) {
                            rbStack.get(nestedLevel).addField(pos, leftValue);
                        } else {
                            mergeFields((ARecordType) combinedType.getFieldTypes()[pos], (ARecordVisitablePointable) leftValue, (ARecordVisitablePointable) rightValue, false, nestedLevel + 1);
                            tabvs.reset();
                            rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
                            rbStack.get(nestedLevel).addField(pos, tabvs);
                        }
                    } else {
                        if (rightValue == null) {
                            rbStack.get(nestedLevel).addField(fieldNamePointable, leftValue);
                        } else {
                            mergeFields(DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE, (ARecordVisitablePointable) leftValue, (ARecordVisitablePointable) rightValue, false, nestedLevel + 1);
                            tabvs.reset();
                            rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
                            rbStack.get(nestedLevel).addField(fieldNamePointable, tabvs);
                        }
                    }
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) ArrayList(java.util.ArrayList) RecordBuilder(org.apache.asterix.builders.RecordBuilder) 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) ARecordVisitablePointable(org.apache.asterix.om.pointables.ARecordVisitablePointable) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AsterixException(org.apache.asterix.common.exceptions.AsterixException) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) DeepEqualAssessor(org.apache.asterix.runtime.evaluators.comparisons.DeepEqualAssessor) RuntimeRecordTypeInfo(org.apache.asterix.om.types.runtime.RuntimeRecordTypeInfo) ARecordType(org.apache.asterix.om.types.ARecordType) PointableAllocator(org.apache.asterix.om.pointables.PointableAllocator) RuntimeDataException(org.apache.asterix.common.exceptions.RuntimeDataException)

Example 98 with ARecordType

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

the class RecordRemoveFieldsEvalFactory method createScalarEvaluator.

@Override
public IScalarEvaluator createScalarEvaluator(final IHyracksTaskContext ctx) throws HyracksDataException {
    final PointableAllocator pa = new PointableAllocator();
    final IVisitablePointable vp0 = pa.allocateRecordValue(inputRecType);
    final IVisitablePointable vp1 = pa.allocateListValue(inputListType);
    final IPointable inputArg0 = new VoidPointable();
    final IPointable inputArg1 = new VoidPointable();
    final IScalarEvaluator eval0 = inputRecordEvalFactory.createScalarEvaluator(ctx);
    final IScalarEvaluator eval1 = removeFieldPathsFactory.createScalarEvaluator(ctx);
    return new IScalarEvaluator() {

        private final RuntimeRecordTypeInfo runtimeRecordTypeInfo = new RuntimeRecordTypeInfo();

        private final List<RecordBuilder> rbStack = new ArrayList<>();

        private final ArrayBackedValueStorage tabvs = new ArrayBackedValueStorage();

        private final Deque<IVisitablePointable> recordPath = new ArrayDeque<>();

        private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

        private DataOutput out = resultStorage.getDataOutput();

        @Override
        public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
            resultStorage.reset();
            eval0.evaluate(tuple, inputArg0);
            eval1.evaluate(tuple, inputArg1);
            byte inputTypeTag0 = inputArg0.getByteArray()[inputArg0.getStartOffset()];
            if (inputTypeTag0 != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                throw new TypeMismatchException(BuiltinFunctions.REMOVE_FIELDS, 0, inputTypeTag0, ATypeTag.SERIALIZED_INT32_TYPE_TAG);
            }
            byte inputTypeTag1 = inputArg1.getByteArray()[inputArg1.getStartOffset()];
            if (inputTypeTag1 != ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG) {
                throw new TypeMismatchException(BuiltinFunctions.REMOVE_FIELDS, 1, inputTypeTag1, ATypeTag.SERIALIZED_ORDEREDLIST_TYPE_TAG);
            }
            vp0.set(inputArg0);
            vp1.set(inputArg1);
            ARecordVisitablePointable recordPointable = (ARecordVisitablePointable) vp0;
            AListVisitablePointable listPointable = (AListVisitablePointable) vp1;
            try {
                recordPath.clear();
                rbStack.clear();
                processRecord(requiredRecType, recordPointable, listPointable, 0);
                rbStack.get(0).write(out, true);
            } catch (IOException | AsterixException e) {
                throw new HyracksDataException(e);
            }
            result.set(resultStorage);
        }

        private void processRecord(ARecordType requiredType, ARecordVisitablePointable srp, AListVisitablePointable inputList, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
            if (rbStack.size() < (nestedLevel + 1)) {
                rbStack.add(new RecordBuilder());
            }
            rbStack.get(nestedLevel).reset(requiredType);
            rbStack.get(nestedLevel).init();
            List<IVisitablePointable> fieldNames = srp.getFieldNames();
            List<IVisitablePointable> fieldValues = srp.getFieldValues();
            List<IVisitablePointable> fieldTypes = srp.getFieldTypeTags();
            for (int i = 0; i < fieldNames.size(); i++) {
                IVisitablePointable subRecFieldName = fieldNames.get(i);
                recordPath.push(subRecFieldName);
                if (isValidPath(inputList)) {
                    if (requiredType != null && requiredType.getTypeTag() != ATypeTag.ANY) {
                        addKeptFieldToSubRecord(requiredType, subRecFieldName, fieldValues.get(i), fieldTypes.get(i), inputList, nestedLevel);
                    } else {
                        addKeptFieldToSubRecord(DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE, subRecFieldName, fieldValues.get(i), fieldTypes.get(i), inputList, nestedLevel);
                    }
                }
                recordPath.pop();
            }
        }

        private void addKeptFieldToSubRecord(ARecordType requiredType, IVisitablePointable fieldNamePointable, IVisitablePointable fieldValuePointable, IVisitablePointable fieldTypePointable, AListVisitablePointable inputList, int nestedLevel) throws IOException, AsterixException, HyracksDataException {
            runtimeRecordTypeInfo.reset(requiredType);
            int pos = runtimeRecordTypeInfo.getFieldIndex(fieldNamePointable.getByteArray(), fieldNamePointable.getStartOffset() + 1, fieldNamePointable.getLength() - 1);
            if (pos >= 0) {
                // Closed field
                if (PointableHelper.sameType(ATypeTag.OBJECT, fieldTypePointable)) {
                    processRecord((ARecordType) requiredType.getFieldTypes()[pos], (ARecordVisitablePointable) fieldValuePointable, inputList, nestedLevel + 1);
                    tabvs.reset();
                    rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
                    rbStack.get(nestedLevel).addField(pos, tabvs);
                } else {
                    rbStack.get(nestedLevel).addField(pos, fieldValuePointable);
                }
            } else {
                // Open field
                if (PointableHelper.sameType(ATypeTag.OBJECT, fieldTypePointable)) {
                    processRecord(null, (ARecordVisitablePointable) fieldValuePointable, inputList, nestedLevel + 1);
                    tabvs.reset();
                    rbStack.get(nestedLevel + 1).write(tabvs.getDataOutput(), true);
                    rbStack.get(nestedLevel).addField(fieldNamePointable, tabvs);
                } else {
                    rbStack.get(nestedLevel).addField(fieldNamePointable, fieldValuePointable);
                }
            }
        }

        private boolean isValidPath(AListVisitablePointable inputList) throws HyracksDataException {
            List<IVisitablePointable> items = inputList.getItems();
            List<IVisitablePointable> typeTags = inputList.getItemTags();
            int pathLen = recordPath.size();
            for (int i = 0; i < items.size(); i++) {
                IVisitablePointable item = items.get(i);
                if (PointableHelper.sameType(ATypeTag.ARRAY, typeTags.get(i))) {
                    List<IVisitablePointable> inputPathItems = ((AListVisitablePointable) item).getItems();
                    if (pathLen == inputPathItems.size()) {
                        boolean match = true;
                        Iterator<IVisitablePointable> fpi = recordPath.iterator();
                        for (int j = inputPathItems.size() - 1; j >= 0; j--) {
                            match &= PointableHelper.isEqual(inputPathItems.get(j), fpi.next());
                            if (!match) {
                                break;
                            }
                        }
                        if (match) {
                            // Not a valid path for the output record
                            return false;
                        }
                    }
                } else {
                    if (PointableHelper.isEqual(recordPath.getFirst(), item)) {
                        return false;
                    }
                }
            }
            return true;
        }
    };
}
Also used : DataOutput(java.io.DataOutput) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ARecordVisitablePointable(org.apache.asterix.om.pointables.ARecordVisitablePointable) AListVisitablePointable(org.apache.asterix.om.pointables.AListVisitablePointable) AsterixException(org.apache.asterix.common.exceptions.AsterixException) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) ArrayList(java.util.ArrayList) List(java.util.List) PointableAllocator(org.apache.asterix.om.pointables.PointableAllocator) RecordBuilder(org.apache.asterix.builders.RecordBuilder) IOException(java.io.IOException) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) RuntimeRecordTypeInfo(org.apache.asterix.om.types.runtime.RuntimeRecordTypeInfo) ARecordType(org.apache.asterix.om.types.ARecordType)

Example 99 with ARecordType

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

the class FieldAccessNestedEvalFactory method createScalarEvaluator.

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

        private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

        private final DataOutput out = resultStorage.getDataOutput();

        private final ByteArrayAccessibleOutputStream subRecordTmpStream = new ByteArrayAccessibleOutputStream();

        private final IPointable inputArg0 = new VoidPointable();

        private final IScalarEvaluator eval0 = recordEvalFactory.createScalarEvaluator(ctx);

        private final IPointable[] fieldPointables = new VoidPointable[fieldPath.size()];

        private final RuntimeRecordTypeInfo[] recTypeInfos = new RuntimeRecordTypeInfo[fieldPath.size()];

        @SuppressWarnings("unchecked")
        private final ISerializerDeserializer<ANull> nullSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ANULL);

        @SuppressWarnings("unchecked")
        private final ISerializerDeserializer<AMissing> missingSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AMISSING);

        {
            generateFieldsPointables();
            for (int index = 0; index < fieldPath.size(); ++index) {
                recTypeInfos[index] = new RuntimeRecordTypeInfo();
            }
        }

        @SuppressWarnings("unchecked")
        private void generateFieldsPointables() throws HyracksDataException {
            for (int i = 0; i < fieldPath.size(); i++) {
                ArrayBackedValueStorage storage = new ArrayBackedValueStorage();
                DataOutput out = storage.getDataOutput();
                AString as = new AString(fieldPath.get(i));
                SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(as.getType()).serialize(as, out);
                fieldPointables[i] = new VoidPointable();
                fieldPointables[i].set(storage);
            }
        }

        @Override
        public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
            try {
                resultStorage.reset();
                eval0.evaluate(tuple, inputArg0);
                byte[] serRecord = inputArg0.getByteArray();
                int offset = inputArg0.getStartOffset();
                int start = offset;
                int len = inputArg0.getLength();
                if (serRecord[start] != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                    throw new TypeMismatchException(BuiltinFunctions.FIELD_ACCESS_NESTED, 0, serRecord[start], ATypeTag.SERIALIZED_RECORD_TYPE_TAG);
                }
                int subFieldIndex = -1;
                int subFieldOffset = -1;
                int subFieldLength = -1;
                int nullBitmapSize = -1;
                IAType subType = recordType;
                recTypeInfos[0].reset(recordType);
                ATypeTag subTypeTag = ATypeTag.MISSING;
                boolean openField = false;
                int pathIndex = 0;
                // Moving through closed fields first.
                for (; pathIndex < fieldPointables.length; pathIndex++) {
                    if (subType.getTypeTag().equals(ATypeTag.UNION)) {
                        //enforced SubType
                        subType = ((AUnionType) subType).getActualType();
                        byte serializedTypeTag = subType.getTypeTag().serialize();
                        if (serializedTypeTag != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                            throw new UnsupportedTypeException(BuiltinFunctions.FIELD_ACCESS_NESTED.getName(), serializedTypeTag);
                        }
                        if (subType.getTypeTag() == ATypeTag.OBJECT) {
                            recTypeInfos[pathIndex].reset((ARecordType) subType);
                        }
                    }
                    subFieldIndex = recTypeInfos[pathIndex].getFieldIndex(fieldPointables[pathIndex].getByteArray(), fieldPointables[pathIndex].getStartOffset() + 1, fieldPointables[pathIndex].getLength() - 1);
                    if (subFieldIndex == -1) {
                        break;
                    }
                    nullBitmapSize = RecordUtil.computeNullBitmapSize((ARecordType) subType);
                    subFieldOffset = ARecordSerializerDeserializer.getFieldOffsetById(serRecord, start, subFieldIndex, nullBitmapSize, ((ARecordType) subType).isOpen());
                    if (subFieldOffset == 0) {
                        // the field is null, we checked the null bit map
                        // any path after null will return null.
                        nullSerde.serialize(ANull.NULL, out);
                        result.set(resultStorage);
                        return;
                    }
                    if (subFieldOffset < 0) {
                        // the field is missing, we checked the missing bit map
                        // any path after missing will return null.
                        missingSerde.serialize(AMissing.MISSING, out);
                        result.set(resultStorage);
                        return;
                    }
                    subType = ((ARecordType) subType).getFieldTypes()[subFieldIndex];
                    if (subType.getTypeTag() == ATypeTag.OBJECT && pathIndex + 1 < fieldPointables.length) {
                        // Move to the next Depth
                        recTypeInfos[pathIndex + 1].reset((ARecordType) subType);
                    }
                    if (subType.getTypeTag().equals(ATypeTag.UNION)) {
                        subTypeTag = ((AUnionType) subType).getActualType().getTypeTag();
                        subFieldLength = NonTaggedFormatUtil.getFieldValueLength(serRecord, subFieldOffset, subTypeTag, false);
                    } else {
                        subTypeTag = subType.getTypeTag();
                        subFieldLength = NonTaggedFormatUtil.getFieldValueLength(serRecord, subFieldOffset, subTypeTag, false);
                    }
                    if (pathIndex < fieldPointables.length - 1) {
                        //setup next iteration
                        subRecordTmpStream.reset();
                        subRecordTmpStream.write(subTypeTag.serialize());
                        subRecordTmpStream.write(serRecord, subFieldOffset, subFieldLength);
                        serRecord = subRecordTmpStream.getByteArray();
                        start = 0;
                    }
                    // type check
                    if (pathIndex < fieldPointables.length - 1 && serRecord[start] != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                        throw new UnsupportedTypeException(BuiltinFunctions.FIELD_ACCESS_NESTED, serRecord[start]);
                    }
                }
                // Moving through open fields after we hit the first open field.
                for (; pathIndex < fieldPointables.length; pathIndex++) {
                    openField = true;
                    subFieldOffset = ARecordSerializerDeserializer.getFieldOffsetByName(serRecord, start, len, fieldPointables[pathIndex].getByteArray(), fieldPointables[pathIndex].getStartOffset());
                    if (subFieldOffset < 0) {
                        out.writeByte(ATypeTag.SERIALIZED_MISSING_TYPE_TAG);
                        result.set(resultStorage);
                        return;
                    }
                    subTypeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(serRecord[subFieldOffset]);
                    subFieldLength = NonTaggedFormatUtil.getFieldValueLength(serRecord, subFieldOffset, subTypeTag, true) + 1;
                    if (pathIndex >= fieldPointables.length - 1) {
                        continue;
                    }
                    //setup next iteration
                    start = subFieldOffset;
                    len = subFieldLength;
                    // type check
                    if (serRecord[start] == ATypeTag.SERIALIZED_MISSING_TYPE_TAG) {
                        missingSerde.serialize(AMissing.MISSING, out);
                        result.set(resultStorage);
                        return;
                    }
                    if (serRecord[start] != ATypeTag.SERIALIZED_RECORD_TYPE_TAG) {
                        throw new UnsupportedTypeException(BuiltinFunctions.FIELD_ACCESS_NESTED.getName(), serRecord[start]);
                    }
                }
                // emit the final result.
                if (openField) {
                    result.set(serRecord, subFieldOffset, subFieldLength);
                } else {
                    out.writeByte(subTypeTag.serialize());
                    out.write(serRecord, subFieldOffset, subFieldLength);
                    result.set(resultStorage);
                }
            } catch (IOException | AsterixException e) {
                throw new HyracksDataException(e);
            }
        }
    };
}
Also used : DataOutput(java.io.DataOutput) AUnionType(org.apache.asterix.om.types.AUnionType) ByteArrayAccessibleOutputStream(org.apache.hyracks.data.std.util.ByteArrayAccessibleOutputStream) 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) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AsterixException(org.apache.asterix.common.exceptions.AsterixException) ATypeTag(org.apache.asterix.om.types.ATypeTag) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) UnsupportedTypeException(org.apache.asterix.runtime.exceptions.UnsupportedTypeException) RuntimeRecordTypeInfo(org.apache.asterix.om.types.runtime.RuntimeRecordTypeInfo) AString(org.apache.asterix.om.base.AString) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 100 with ARecordType

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

the class DatasetUtil method createPrimaryIndexUpsertOp.

/**
     * Creates a primary index upsert operator for a given dataset.
     *
     * @param spec,
     *            the job specification.
     * @param metadataProvider,
     *            the metadata provider.
     * @param dataset,
     *            the dataset to upsert.
     * @param inputRecordDesc,the
     *            record descriptor for an input tuple.
     * @param fieldPermutation,
     *            the field permutation according to the input.
     * @param missingWriterFactory,
     *            the factory for customizing missing value serialization.
     * @return a primary index scan operator and its location constraints.
     * @throws AlgebricksException
     */
public static Pair<IOperatorDescriptor, AlgebricksPartitionConstraint> createPrimaryIndexUpsertOp(JobSpecification spec, MetadataProvider metadataProvider, Dataset dataset, RecordDescriptor inputRecordDesc, int[] fieldPermutation, IMissingWriterFactory missingWriterFactory) throws AlgebricksException {
    int numKeys = dataset.getPrimaryKeys().size();
    int numFilterFields = DatasetUtil.getFilterField(dataset) == null ? 0 : 1;
    ARecordType itemType = (ARecordType) metadataProvider.findType(dataset);
    ARecordType metaItemType = (ARecordType) metadataProvider.findMetaType(dataset);
    try {
        Index primaryIndex = metadataProvider.getIndex(dataset.getDataverseName(), dataset.getDatasetName(), dataset.getDatasetName());
        Pair<IFileSplitProvider, AlgebricksPartitionConstraint> splitsAndConstraint = metadataProvider.getSplitProviderAndConstraints(dataset);
        // prepare callback
        JobId jobId = ((JobEventListenerFactory) spec.getJobletEventListenerFactory()).getJobId();
        int[] primaryKeyFields = new int[numKeys];
        for (int i = 0; i < numKeys; i++) {
            primaryKeyFields[i] = i;
        }
        boolean hasSecondaries = metadataProvider.getDatasetIndexes(dataset.getDataverseName(), dataset.getDatasetName()).size() > 1;
        IStorageComponentProvider storageComponentProvider = metadataProvider.getStorageComponentProvider();
        IModificationOperationCallbackFactory modificationCallbackFactory = dataset.getModificationCallbackFactory(storageComponentProvider, primaryIndex, jobId, IndexOperation.UPSERT, primaryKeyFields);
        ISearchOperationCallbackFactory searchCallbackFactory = dataset.getSearchCallbackFactory(storageComponentProvider, primaryIndex, jobId, IndexOperation.UPSERT, primaryKeyFields);
        IIndexDataflowHelperFactory idfh = new IndexDataflowHelperFactory(storageComponentProvider.getStorageManager(), splitsAndConstraint.first);
        LSMPrimaryUpsertOperatorDescriptor op;
        ITypeTraits[] outputTypeTraits = new ITypeTraits[inputRecordDesc.getFieldCount() + (dataset.hasMetaPart() ? 2 : 1) + numFilterFields];
        ISerializerDeserializer<?>[] outputSerDes = new ISerializerDeserializer[inputRecordDesc.getFieldCount() + (dataset.hasMetaPart() ? 2 : 1) + numFilterFields];
        // add the previous record first
        int f = 0;
        outputSerDes[f] = FormatUtils.getDefaultFormat().getSerdeProvider().getSerializerDeserializer(itemType);
        f++;
        // add the previous meta second
        if (dataset.hasMetaPart()) {
            outputSerDes[f] = FormatUtils.getDefaultFormat().getSerdeProvider().getSerializerDeserializer(metaItemType);
            outputTypeTraits[f] = FormatUtils.getDefaultFormat().getTypeTraitProvider().getTypeTrait(metaItemType);
            f++;
        }
        // add the previous filter third
        int fieldIdx = -1;
        if (numFilterFields > 0) {
            String filterField = DatasetUtil.getFilterField(dataset).get(0);
            String[] fieldNames = itemType.getFieldNames();
            int i = 0;
            for (; i < fieldNames.length; i++) {
                if (fieldNames[i].equals(filterField)) {
                    break;
                }
            }
            fieldIdx = i;
            outputTypeTraits[f] = FormatUtils.getDefaultFormat().getTypeTraitProvider().getTypeTrait(itemType.getFieldTypes()[fieldIdx]);
            outputSerDes[f] = FormatUtils.getDefaultFormat().getSerdeProvider().getSerializerDeserializer(itemType.getFieldTypes()[fieldIdx]);
            f++;
        }
        for (int j = 0; j < inputRecordDesc.getFieldCount(); j++) {
            outputTypeTraits[j + f] = inputRecordDesc.getTypeTraits()[j];
            outputSerDes[j + f] = inputRecordDesc.getFields()[j];
        }
        RecordDescriptor outputRecordDesc = new RecordDescriptor(outputSerDes, outputTypeTraits);
        op = new LSMPrimaryUpsertOperatorDescriptor(spec, outputRecordDesc, fieldPermutation, idfh, missingWriterFactory, modificationCallbackFactory, searchCallbackFactory, dataset.getFrameOpCallbackFactory(), numKeys, itemType, fieldIdx, hasSecondaries);
        return new Pair<>(op, splitsAndConstraint.second);
    } catch (MetadataException me) {
        throw new AlgebricksException(me);
    }
}
Also used : LSMPrimaryUpsertOperatorDescriptor(org.apache.asterix.runtime.operators.LSMPrimaryUpsertOperatorDescriptor) IFileSplitProvider(org.apache.hyracks.dataflow.std.file.IFileSplitProvider) RecordDescriptor(org.apache.hyracks.api.dataflow.value.RecordDescriptor) Index(org.apache.asterix.metadata.entities.Index) AMutableString(org.apache.asterix.om.base.AMutableString) AString(org.apache.asterix.om.base.AString) MetadataException(org.apache.asterix.metadata.MetadataException) AlgebricksPartitionConstraint(org.apache.hyracks.algebricks.common.constraints.AlgebricksPartitionConstraint) IIndexDataflowHelperFactory(org.apache.hyracks.storage.am.common.dataflow.IIndexDataflowHelperFactory) IndexDataflowHelperFactory(org.apache.hyracks.storage.am.common.dataflow.IndexDataflowHelperFactory) JobId(org.apache.asterix.common.transactions.JobId) Pair(org.apache.hyracks.algebricks.common.utils.Pair) IStorageComponentProvider(org.apache.asterix.common.context.IStorageComponentProvider) ITypeTraits(org.apache.hyracks.api.dataflow.value.ITypeTraits) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) JobEventListenerFactory(org.apache.asterix.runtime.job.listener.JobEventListenerFactory) AlgebricksPartitionConstraint(org.apache.hyracks.algebricks.common.constraints.AlgebricksPartitionConstraint) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) ISearchOperationCallbackFactory(org.apache.hyracks.storage.am.common.api.ISearchOperationCallbackFactory) IIndexDataflowHelperFactory(org.apache.hyracks.storage.am.common.dataflow.IIndexDataflowHelperFactory) IModificationOperationCallbackFactory(org.apache.hyracks.storage.am.common.api.IModificationOperationCallbackFactory) ARecordType(org.apache.asterix.om.types.ARecordType)

Aggregations

ARecordType (org.apache.asterix.om.types.ARecordType)105 IAType (org.apache.asterix.om.types.IAType)73 ArrayList (java.util.ArrayList)48 List (java.util.List)24 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)22 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)20 Dataset (org.apache.asterix.metadata.entities.Dataset)19 AString (org.apache.asterix.om.base.AString)19 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)19 Test (org.junit.Test)16 AsterixException (org.apache.asterix.common.exceptions.AsterixException)15 Index (org.apache.asterix.metadata.entities.Index)15 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)15 CompilationException (org.apache.asterix.common.exceptions.CompilationException)13 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)13 Mutable (org.apache.commons.lang3.mutable.Mutable)13 IOException (java.io.IOException)12 MetadataException (org.apache.asterix.metadata.MetadataException)12 AUnionType (org.apache.asterix.om.types.AUnionType)11 Pair (org.apache.hyracks.algebricks.common.utils.Pair)10