Search in sources :

Example 6 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)

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