Search in sources :

Example 6 with AListVisitablePointable

use of org.apache.asterix.om.pointables.AListVisitablePointable in project asterixdb by apache.

the class ListDeepEqualityChecker method accessList.

public boolean accessList(IVisitablePointable listPointableLeft, IVisitablePointable listPointableRight, DeepEqualityVisitor visitor) throws HyracksDataException {
    this.visitor = visitor;
    AListVisitablePointable listLeft = (AListVisitablePointable) listPointableLeft;
    List<IVisitablePointable> itemsLeft = listLeft.getItems();
    List<IVisitablePointable> itemTagTypesLeft = listLeft.getItemTags();
    AListVisitablePointable listRight = (AListVisitablePointable) listPointableRight;
    List<IVisitablePointable> itemsRight = listRight.getItems();
    List<IVisitablePointable> itemTagTypesRight = listRight.getItemTags();
    if (itemsLeft.size() != itemsRight.size())
        return false;
    boolean isOrderedRight = listLeft.ordered();
    if (isOrderedRight != listRight.ordered())
        return false;
    if (isOrderedRight) {
        return processOrderedList(itemsLeft, itemTagTypesLeft, itemsRight, itemTagTypesRight);
    } else {
        return processUnorderedList(itemsLeft, itemTagTypesLeft, itemsRight, itemTagTypesRight);
    }
}
Also used : AListVisitablePointable(org.apache.asterix.om.pointables.AListVisitablePointable) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable)

Example 7 with AListVisitablePointable

use of org.apache.asterix.om.pointables.AListVisitablePointable 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)

Aggregations

AListVisitablePointable (org.apache.asterix.om.pointables.AListVisitablePointable)7 ARecordVisitablePointable (org.apache.asterix.om.pointables.ARecordVisitablePointable)6 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)5 IVisitablePointable (org.apache.asterix.om.pointables.base.IVisitablePointable)4 PrintStream (java.io.PrintStream)3 IPrintVisitor (org.apache.asterix.om.pointables.printer.IPrintVisitor)3 ATypeTag (org.apache.asterix.om.types.ATypeTag)3 Pair (org.apache.hyracks.algebricks.common.utils.Pair)3 DataOutput (java.io.DataOutput)2 RecordBuilder (org.apache.asterix.builders.RecordBuilder)2 AsterixException (org.apache.asterix.common.exceptions.AsterixException)2 RuntimeDataException (org.apache.asterix.common.exceptions.RuntimeDataException)2 PointableAllocator (org.apache.asterix.om.pointables.PointableAllocator)2 RuntimeRecordTypeInfo (org.apache.asterix.om.types.runtime.RuntimeRecordTypeInfo)2 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)2 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)2 IPointable (org.apache.hyracks.data.std.api.IPointable)2 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)2 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)2 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)2