Search in sources :

Example 1 with APrintVisitor

use of org.apache.asterix.om.pointables.printer.adm.APrintVisitor in project asterixdb by apache.

the class AObjectPrinterFactory method createPrinter.

@Override
public IPrinter createPrinter() {
    final ARecordVisitablePointable rPointable = new ARecordVisitablePointable(DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE);
    final AListVisitablePointable olPointable = new AListVisitablePointable(DefaultOpenFieldType.NESTED_OPEN_AORDERED_LIST_TYPE);
    final AListVisitablePointable ulPointable = new AListVisitablePointable(DefaultOpenFieldType.NESTED_OPEN_AUNORDERED_LIST_TYPE);
    final Pair<PrintStream, ATypeTag> streamTag = new Pair<>(null, null);
    final IPrintVisitor visitor = new APrintVisitor();
    return (byte[] b, int s, int l, PrintStream ps) -> {
        ATypeTag typeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(b[s]);
        if (!printFlatValue(typeTag, b, s, l, ps)) {
            streamTag.first = ps;
            streamTag.second = typeTag;
            switch(typeTag) {
                case OBJECT:
                    rPointable.set(b, s, l);
                    visitor.visit(rPointable, streamTag);
                    break;
                case ARRAY:
                    olPointable.set(b, s, l);
                    visitor.visit(olPointable, streamTag);
                    break;
                case MULTISET:
                    ulPointable.set(b, s, l);
                    visitor.visit(ulPointable, streamTag);
                    break;
                default:
                    throw new HyracksDataException("No printer for type " + typeTag);
            }
        }
    };
}
Also used : PrintStream(java.io.PrintStream) ARecordVisitablePointable(org.apache.asterix.om.pointables.ARecordVisitablePointable) AListVisitablePointable(org.apache.asterix.om.pointables.AListVisitablePointable) ATypeTag(org.apache.asterix.om.types.ATypeTag) IPrintVisitor(org.apache.asterix.om.pointables.printer.IPrintVisitor) APrintVisitor(org.apache.asterix.om.pointables.printer.adm.APrintVisitor) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 2 with APrintVisitor

use of org.apache.asterix.om.pointables.printer.adm.APrintVisitor in project asterixdb by apache.

the class AOrderedlistPrinterFactory method createPrinter.

@Override
public IPrinter createPrinter() {
    final PointableAllocator allocator = new PointableAllocator();
    final IAType inputType = orderedlistType == null ? DefaultOpenFieldType.getDefaultOpenFieldType(ATypeTag.ARRAY) : orderedlistType;
    final IVisitablePointable listAccessor = allocator.allocateListValue(inputType);
    final APrintVisitor printVisitor = new APrintVisitor();
    final Pair<PrintStream, ATypeTag> arg = new Pair<>(null, null);
    return new IPrinter() {

        @Override
        public void init() {
            arg.second = inputType.getTypeTag();
        }

        @Override
        public void print(byte[] b, int start, int l, PrintStream ps) throws HyracksDataException {
            listAccessor.set(b, start, l);
            arg.first = ps;
            listAccessor.accept(printVisitor, arg);
        }
    };
}
Also used : PrintStream(java.io.PrintStream) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) ATypeTag(org.apache.asterix.om.types.ATypeTag) APrintVisitor(org.apache.asterix.om.pointables.printer.adm.APrintVisitor) IPrinter(org.apache.hyracks.algebricks.data.IPrinter) PointableAllocator(org.apache.asterix.om.pointables.PointableAllocator) IAType(org.apache.asterix.om.types.IAType) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 3 with APrintVisitor

use of org.apache.asterix.om.pointables.printer.adm.APrintVisitor in project asterixdb by apache.

the class AUnorderedlistPrinterFactory method createPrinter.

@Override
public IPrinter createPrinter() {
    final PointableAllocator allocator = new PointableAllocator();
    final IAType inputType = unorderedlistType == null ? DefaultOpenFieldType.getDefaultOpenFieldType(ATypeTag.MULTISET) : unorderedlistType;
    final IVisitablePointable listAccessor = allocator.allocateListValue(inputType);
    final APrintVisitor printVisitor = new APrintVisitor();
    final Pair<PrintStream, ATypeTag> arg = new Pair<>(null, null);
    return new IPrinter() {

        @Override
        public void init() {
            arg.second = inputType.getTypeTag();
        }

        @Override
        public void print(byte[] b, int start, int l, PrintStream ps) throws HyracksDataException {
            listAccessor.set(b, start, l);
            arg.first = ps;
            listAccessor.accept(printVisitor, arg);
        }
    };
}
Also used : PrintStream(java.io.PrintStream) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) ATypeTag(org.apache.asterix.om.types.ATypeTag) APrintVisitor(org.apache.asterix.om.pointables.printer.adm.APrintVisitor) IPrinter(org.apache.hyracks.algebricks.data.IPrinter) PointableAllocator(org.apache.asterix.om.pointables.PointableAllocator) IAType(org.apache.asterix.om.types.IAType) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 4 with APrintVisitor

use of org.apache.asterix.om.pointables.printer.adm.APrintVisitor in project asterixdb by apache.

the class ARecordPrinterFactory method createPrinter.

@Override
public IPrinter createPrinter() {
    final PointableAllocator allocator = new PointableAllocator();
    final IAType inputType = recType == null ? DefaultOpenFieldType.getDefaultOpenFieldType(ATypeTag.OBJECT) : recType;
    final IVisitablePointable recAccessor = allocator.allocateRecordValue(inputType);
    final APrintVisitor printVisitor = new APrintVisitor();
    final Pair<PrintStream, ATypeTag> arg = new Pair<>(null, null);
    return new IPrinter() {

        @Override
        public void init() {
            arg.second = inputType.getTypeTag();
        }

        @Override
        public void print(byte[] b, int start, int l, PrintStream ps) throws HyracksDataException {
            recAccessor.set(b, start, l);
            arg.first = ps;
            recAccessor.accept(printVisitor, arg);
        }
    };
}
Also used : PrintStream(java.io.PrintStream) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) ATypeTag(org.apache.asterix.om.types.ATypeTag) APrintVisitor(org.apache.asterix.om.pointables.printer.adm.APrintVisitor) IPrinter(org.apache.hyracks.algebricks.data.IPrinter) PointableAllocator(org.apache.asterix.om.pointables.PointableAllocator) IAType(org.apache.asterix.om.types.IAType) Pair(org.apache.hyracks.algebricks.common.utils.Pair)

Example 5 with APrintVisitor

use of org.apache.asterix.om.pointables.printer.adm.APrintVisitor in project asterixdb by apache.

the class ARecordCaster method matchClosedPart.

private void matchClosedPart(List<IVisitablePointable> fieldNames, List<IVisitablePointable> fieldTypeTags) throws HyracksDataException {
    // sort-merge based match
    quickSort(fieldNamesSortedIndex, fieldNames, 0, numInputFields - 1);
    int fnStart = 0;
    int reqFnStart = 0;
    while (fnStart < numInputFields && reqFnStart < reqFieldNames.size()) {
        int fnPos = fieldNamesSortedIndex[fnStart];
        int reqFnPos = reqFieldNamesSortedIndex[reqFnStart];
        int c = compare(fieldNames.get(fnPos), reqFieldNames.get(reqFnPos));
        if (c == 0) {
            IVisitablePointable fieldTypeTag = fieldTypeTags.get(fnPos);
            IVisitablePointable reqFieldTypeTag = reqFieldTypeTags.get(reqFnPos);
            if (fieldTypeTag.equals(reqFieldTypeTag) || (// match the null type of optional field
            optionalFields[reqFnPos] && (fieldTypeTag.equals(nullTypeTag)) || fieldTypeTag.equals(missingTypeTag))) {
                fieldPermutation[reqFnPos] = fnPos;
                openFields[fnPos] = false;
            } else {
                // if mismatch, check whether input type can be promoted to the required type
                ATypeTag inputTypeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(fieldTypeTag.getByteArray()[fieldTypeTag.getStartOffset()]);
                ATypeTag requiredTypeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(reqFieldTypeTag.getByteArray()[reqFieldTypeTag.getStartOffset()]);
                if (ATypeHierarchy.canPromote(inputTypeTag, requiredTypeTag) || ATypeHierarchy.canDemote(inputTypeTag, requiredTypeTag)) {
                    fieldPermutation[reqFnPos] = fnPos;
                    openFields[fnPos] = false;
                } else {
                    throw new HyracksDataException(ErrorCode.ASTERIX, ErrorCode.CASTING_FIELD, "Field type %1$s can't be promoted to type %2$s", inputTypeTag, requiredTypeTag);
                }
            }
            fnStart++;
            reqFnStart++;
        }
        if (c > 0) {
            reqFnStart++;
        }
        if (c < 0) {
            fnStart++;
        }
    }
    // check unmatched fields in the input type
    for (int i = 0; i < openFields.length; i++) {
        if (openFields[i] && !cachedReqType.isOpen()) {
            //print the field name
            IVisitablePointable fieldName = fieldNames.get(i);
            ByteArrayOutputStream fieldBos = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(fieldBos);
            APrintVisitor printVisitor = new APrintVisitor();
            Pair<PrintStream, ATypeTag> visitorArg = new Pair<>(ps, ATypeTag.STRING);
            fieldName.accept(printVisitor, visitorArg);
            //print the colon
            ps.print(":");
            //print the field type
            IVisitablePointable fieldType = fieldTypeTags.get(i);
            ATypeTag typeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(fieldType.getByteArray()[fieldType.getStartOffset()]);
            ps.print(typeTag);
            //collect the output message and throw the exception
            throw new HyracksDataException("type mismatch: including an extra field " + fieldBos.toString());
        }
    }
    // check unmatched fields in the required type
    for (int i = 0; i < fieldPermutation.length; i++) {
        if (fieldPermutation[i] < 0) {
            IAType t = cachedReqType.getFieldTypes()[i];
            if (!NonTaggedFormatUtil.isOptional(t)) {
                // no matched field in the input for a required closed field
                throw new HyracksDataException("type mismatch: missing a required closed field " + cachedReqType.getFieldNames()[i] + ": " + t.getTypeName());
            }
        }
    }
}
Also used : PrintStream(java.io.PrintStream) IVisitablePointable(org.apache.asterix.om.pointables.base.IVisitablePointable) ATypeTag(org.apache.asterix.om.types.ATypeTag) APrintVisitor(org.apache.asterix.om.pointables.printer.adm.APrintVisitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResettableByteArrayOutputStream(org.apache.asterix.om.utils.ResettableByteArrayOutputStream) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) Pair(org.apache.hyracks.algebricks.common.utils.Pair) IAType(org.apache.asterix.om.types.IAType)

Aggregations

PrintStream (java.io.PrintStream)5 APrintVisitor (org.apache.asterix.om.pointables.printer.adm.APrintVisitor)5 ATypeTag (org.apache.asterix.om.types.ATypeTag)5 Pair (org.apache.hyracks.algebricks.common.utils.Pair)5 IVisitablePointable (org.apache.asterix.om.pointables.base.IVisitablePointable)4 IAType (org.apache.asterix.om.types.IAType)4 PointableAllocator (org.apache.asterix.om.pointables.PointableAllocator)3 IPrinter (org.apache.hyracks.algebricks.data.IPrinter)3 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 AListVisitablePointable (org.apache.asterix.om.pointables.AListVisitablePointable)1 ARecordVisitablePointable (org.apache.asterix.om.pointables.ARecordVisitablePointable)1 IPrintVisitor (org.apache.asterix.om.pointables.printer.IPrintVisitor)1 ResettableByteArrayOutputStream (org.apache.asterix.om.utils.ResettableByteArrayOutputStream)1