Search in sources :

Example 1 with UTF8StringWriter

use of org.apache.hyracks.util.string.UTF8StringWriter in project asterixdb by apache.

the class PrintDateTimeDescriptor 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 ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable argPtr0 = new VoidPointable();

                private IPointable argPtr1 = new VoidPointable();

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

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

                private StringBuilder sbder = new StringBuilder();

                private UTF8StringWriter utf8Writer = new UTF8StringWriter();

                private UTF8StringPointable utf8Ptr = new UTF8StringPointable();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    byte[] bytes0 = argPtr0.getByteArray();
                    int offset0 = argPtr0.getStartOffset();
                    byte[] bytes1 = argPtr1.getByteArray();
                    int offset1 = argPtr1.getStartOffset();
                    int len1 = argPtr1.getLength();
                    try {
                        if (bytes0[offset0] != ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
                        }
                        if (bytes1[offset1] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        long chronon = ADateTimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
                        utf8Ptr.set(bytes1, offset1 + 1, len1 - 1);
                        int formatLength = utf8Ptr.getUTF8Length();
                        sbder.delete(0, sbder.length());
                        DT_UTILS.printDateTime(chronon, 0, utf8Ptr.getByteArray(), utf8Ptr.getCharStartOffset(), formatLength, sbder, DateTimeParseMode.DATETIME);
                        out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        utf8Writer.writeUTF8(sbder.toString(), out);
                    } catch (IOException ex) {
                        throw new HyracksDataException(ex);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) 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) UTF8StringWriter(org.apache.hyracks.util.string.UTF8StringWriter)

Example 2 with UTF8StringWriter

use of org.apache.hyracks.util.string.UTF8StringWriter in project asterixdb by apache.

the class PrintTimeDescriptor 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 ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable argPtr0 = new VoidPointable();

                private IPointable argPtr1 = new VoidPointable();

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

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

                private StringBuilder sbder = new StringBuilder();

                private final UTF8StringWriter writer = new UTF8StringWriter();

                private final UTF8StringPointable utf8Ptr = new UTF8StringPointable();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    byte[] bytes0 = argPtr0.getByteArray();
                    int offset0 = argPtr0.getStartOffset();
                    byte[] bytes1 = argPtr1.getByteArray();
                    int offset1 = argPtr1.getStartOffset();
                    int len1 = argPtr1.getLength();
                    try {
                        if (bytes0[offset0] != ATypeTag.SERIALIZED_TIME_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_TIME_TYPE_TAG);
                        }
                        if (bytes1[offset1] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        long chronon = ATimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
                        utf8Ptr.set(bytes1, offset1 + 1, len1 - 1);
                        int formatLength = utf8Ptr.getUTF8Length();
                        sbder.delete(0, sbder.length());
                        DT_UTILS.printDateTime(chronon, 0, utf8Ptr.getByteArray(), utf8Ptr.getCharStartOffset(), formatLength, sbder, DateTimeParseMode.TIME_ONLY);
                        out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        writer.writeUTF8(sbder.toString(), out);
                    } catch (IOException ex) {
                        throw new HyracksDataException(ex);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) 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) UTF8StringWriter(org.apache.hyracks.util.string.UTF8StringWriter)

Example 3 with UTF8StringWriter

use of org.apache.hyracks.util.string.UTF8StringWriter in project asterixdb by apache.

the class PrintDateDescriptor 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 ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable argPtr0 = new VoidPointable();

                private IPointable argPtr1 = new VoidPointable();

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

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

                private StringBuilder sbder = new StringBuilder();

                private final UTF8StringWriter utf8Writer = new UTF8StringWriter();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    byte[] bytes0 = argPtr0.getByteArray();
                    int offset0 = argPtr0.getStartOffset();
                    byte[] bytes1 = argPtr1.getByteArray();
                    int offset1 = argPtr1.getStartOffset();
                    try {
                        if (bytes0[offset0] != ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DATE_TYPE_TAG);
                        }
                        if (bytes1[offset1] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        long chronon = ADateSerializerDeserializer.getChronon(bytes0, offset0 + 1) * GregorianCalendarSystem.CHRONON_OF_DAY;
                        int formatLength = UTF8StringUtil.getUTFLength(bytes1, offset1 + 1);
                        int offset = UTF8StringUtil.getNumBytesToStoreLength(formatLength);
                        sbder.delete(0, sbder.length());
                        DT_UTILS.printDateTime(chronon, 0, bytes1, offset1 + 1 + offset, formatLength, sbder, DateTimeParseMode.DATE_ONLY);
                        out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        utf8Writer.writeUTF8(sbder.toString(), out);
                    } catch (IOException ex) {
                        throw new HyracksDataException(ex);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) 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) UTF8StringWriter(org.apache.hyracks.util.string.UTF8StringWriter)

Example 4 with UTF8StringWriter

use of org.apache.hyracks.util.string.UTF8StringWriter in project asterixdb by apache.

the class PrintBinaryDescriptor 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 AbstractBinaryScalarEvaluator(ctx, args) {

                private StringBuilder stringBuilder = new StringBuilder();

                private final ByteArrayPointable byteArrayPtr = new ByteArrayPointable();

                private final UTF8StringPointable formatPointable = new UTF8StringPointable();

                private final UTF8StringWriter writer = new UTF8StringWriter();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    evaluators[0].evaluate(tuple, pointables[0]);
                    evaluators[1].evaluate(tuple, pointables[1]);
                    try {
                        ATypeTag arg0Tag = ATypeTag.VALUE_TYPE_MAPPING[pointables[0].getByteArray()[pointables[0].getStartOffset()]];
                        ATypeTag arg1Tag = ATypeTag.VALUE_TYPE_MAPPING[pointables[1].getByteArray()[pointables[1].getStartOffset()]];
                        checkTypeMachingThrowsIfNot(getIdentifier().getName(), EXPECTED_INPUT_TAGS, arg0Tag, arg1Tag);
                        byteArrayPtr.set(pointables[0].getByteArray(), pointables[0].getStartOffset() + 1, pointables[0].getLength());
                        formatPointable.set(pointables[1].getByteArray(), pointables[1].getStartOffset() + 1, pointables[1].getLength());
                        int lengthBinary = byteArrayPtr.getContentLength();
                        stringBuilder.setLength(0);
                        if (HEX_FORMAT.ignoreCaseCompareTo(formatPointable) == 0) {
                            HexPrinter.printHexString(byteArrayPtr.getByteArray(), byteArrayPtr.getContentStartOffset(), lengthBinary, stringBuilder);
                        } else if (BASE64_FORMAT.ignoreCaseCompareTo(formatPointable) == 0) {
                            Base64Printer.printBase64Binary(byteArrayPtr.getByteArray(), byteArrayPtr.getContentStartOffset(), lengthBinary, stringBuilder);
                        } else {
                            throw new UnsupportedItemTypeException(getIdentifier(), arg1Tag.serialize());
                        }
                        dataOutput.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        writer.writeUTF8(stringBuilder.toString(), dataOutput);
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) UnsupportedItemTypeException(org.apache.asterix.runtime.exceptions.UnsupportedItemTypeException) ByteArrayPointable(org.apache.hyracks.data.std.primitive.ByteArrayPointable) ATypeTag(org.apache.asterix.om.types.ATypeTag) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) UTF8StringWriter(org.apache.hyracks.util.string.UTF8StringWriter)

Example 5 with UTF8StringWriter

use of org.apache.hyracks.util.string.UTF8StringWriter in project asterixdb by apache.

the class AdjustDateTimeForTimeZoneDescriptor method createEvaluatorFactory.

/* (non-Javadoc)
     * @see org.apache.asterix.runtime.base.IScalarFunctionDynamicDescriptor#createEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.ICopyEvaluatorFactory[])
     */
@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 ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable argPtr0 = new VoidPointable();

                private IPointable argPtr1 = new VoidPointable();

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

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

                // possible output types
                private GregorianCalendarSystem calInstance = GregorianCalendarSystem.getInstance();

                private final UTF8StringPointable utf8Ptr = new UTF8StringPointable();

                private final UTF8StringWriter utf8Writer = new UTF8StringWriter();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    eval1.evaluate(tuple, argPtr1);
                    byte[] bytes0 = argPtr0.getByteArray();
                    int offset0 = argPtr0.getStartOffset();
                    byte[] bytes1 = argPtr1.getByteArray();
                    int offset1 = argPtr1.getStartOffset();
                    int len1 = argPtr1.getLength();
                    try {
                        if (bytes0[offset0] != ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes0[offset0], ATypeTag.SERIALIZED_DATETIME_TYPE_TAG);
                        }
                        if (bytes1[offset1] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 1, bytes1[offset1], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        utf8Ptr.set(bytes1, offset1 + 1, len1 - 1);
                        int timezone = ATimeParserFactory.parseTimezonePart(utf8Ptr.getByteArray(), utf8Ptr.getCharStartOffset());
                        if (!calInstance.validateTimeZone(timezone)) {
                            throw new InvalidDataFormatException(getIdentifier(), "timezone");
                        }
                        long chronon = ADateTimeSerializerDeserializer.getChronon(bytes0, offset0 + 1);
                        chronon = calInstance.adjustChrononByTimezone(chronon, timezone);
                        StringBuilder sbder = new StringBuilder();
                        calInstance.getExtendStringRepUntilField(chronon, timezone, sbder, Fields.YEAR, Fields.MILLISECOND, true);
                        out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        utf8Writer.writeUTF8(sbder.toString(), out);
                    } catch (IOException e1) {
                        throw new HyracksDataException(e1);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) GregorianCalendarSystem(org.apache.asterix.om.base.temporal.GregorianCalendarSystem) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) 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) InvalidDataFormatException(org.apache.asterix.runtime.exceptions.InvalidDataFormatException) 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) UTF8StringWriter(org.apache.hyracks.util.string.UTF8StringWriter)

Aggregations

IOException (java.io.IOException)6 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)6 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)6 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)6 IPointable (org.apache.hyracks.data.std.api.IPointable)6 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)6 UTF8StringWriter (org.apache.hyracks.util.string.UTF8StringWriter)6 DataOutput (java.io.DataOutput)5 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)5 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)5 UTF8StringPointable (org.apache.hyracks.data.std.primitive.UTF8StringPointable)5 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)5 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)5 GregorianCalendarSystem (org.apache.asterix.om.base.temporal.GregorianCalendarSystem)2 InvalidDataFormatException (org.apache.asterix.runtime.exceptions.InvalidDataFormatException)2 ATypeTag (org.apache.asterix.om.types.ATypeTag)1 UnsupportedItemTypeException (org.apache.asterix.runtime.exceptions.UnsupportedItemTypeException)1 ByteArrayPointable (org.apache.hyracks.data.std.primitive.ByteArrayPointable)1