Search in sources :

Example 16 with UTF8StringPointable

use of org.apache.hyracks.data.std.primitive.UTF8StringPointable in project asterixdb by apache.

the class AFloatConstructorDescriptor method createEvaluatorFactory.

@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
    return new IScalarEvaluatorFactory() {

        private static final long serialVersionUID = 1L;

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

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable inputArg = new VoidPointable();

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

                private final byte[] POSITIVE_INF = UTF8StringUtil.writeStringToBytes("INF");

                private final byte[] NEGATIVE_INF = UTF8StringUtil.writeStringToBytes("-INF");

                private final byte[] NAN = UTF8StringUtil.writeStringToBytes("NaN");

                private IBinaryComparator utf8BinaryComparator = BinaryComparatorFactoryProvider.UTF8STRING_POINTABLE_INSTANCE.createBinaryComparator();

                private AMutableFloat aFloat = new AMutableFloat(0);

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<AFloat> floatSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AFLOAT);

                private final UTF8StringPointable utf8Ptr = new UTF8StringPointable();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    try {
                        resultStorage.reset();
                        eval.evaluate(tuple, inputArg);
                        byte[] serString = inputArg.getByteArray();
                        int offset = inputArg.getStartOffset();
                        int len = inputArg.getLength();
                        if (serString[offset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            if (utf8BinaryComparator.compare(serString, offset + 1, len - 1, POSITIVE_INF, 0, 5) == 0) {
                                aFloat.setValue(Float.POSITIVE_INFINITY);
                            } else if (utf8BinaryComparator.compare(serString, offset + 1, len - 1, NEGATIVE_INF, 0, 6) == 0) {
                                aFloat.setValue(Float.NEGATIVE_INFINITY);
                            } else if (utf8BinaryComparator.compare(serString, offset + 1, len - 1, NAN, 0, 5) == 0) {
                                aFloat.setValue(Float.NaN);
                            } else {
                                utf8Ptr.set(serString, offset + 1, len - 1);
                                aFloat.setValue(Float.parseFloat(utf8Ptr.toString()));
                            }
                            floatSerde.serialize(aFloat, out);
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, serString[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        result.set(resultStorage);
                    } catch (IOException e) {
                        throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_FLOAT_TYPE_TAG);
                    }
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IBinaryComparator(org.apache.hyracks.api.dataflow.value.IBinaryComparator) 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) 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) AMutableFloat(org.apache.asterix.om.base.AMutableFloat)

Example 17 with UTF8StringPointable

use of org.apache.hyracks.data.std.primitive.UTF8StringPointable in project asterixdb by apache.

the class AInt16ConstructorDescriptor method createEvaluatorFactory.

@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
    return new IScalarEvaluatorFactory() {

        private static final long serialVersionUID = 1L;

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

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable inputArg = new VoidPointable();

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

                private short value;

                private int offset;

                private boolean positive;

                private AMutableInt16 aInt16 = new AMutableInt16((short) 0);

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<AInt16> int16Serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT16);

                private final UTF8StringPointable utf8Ptr = new UTF8StringPointable();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    try {
                        resultStorage.reset();
                        eval.evaluate(tuple, inputArg);
                        byte[] serString = inputArg.getByteArray();
                        int startOffset = inputArg.getStartOffset();
                        int len = inputArg.getLength();
                        if (serString[startOffset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            utf8Ptr.set(serString, startOffset + 1, len - 1);
                            offset = utf8Ptr.getCharStartOffset();
                            //accumulating value in negative domain
                            //otherwise Short.MIN_VALUE = -(Short.MAX_VALUE + 1) would have caused overflow
                            value = 0;
                            positive = true;
                            short limit = -Short.MAX_VALUE;
                            if (serString[offset] == '+') {
                                offset++;
                            } else if (serString[offset] == '-') {
                                offset++;
                                positive = false;
                                limit = Short.MIN_VALUE;
                            }
                            int end = startOffset + len;
                            for (; offset < end; offset++) {
                                int digit;
                                if (serString[offset] >= '0' && serString[offset] <= '9') {
                                    value = (short) (value * 10);
                                    digit = serString[offset] - '0';
                                } else if (serString[offset] == 'i' && serString[offset + 1] == '1' && serString[offset + 2] == '6' && offset + 3 == end) {
                                    break;
                                } else {
                                    throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT16_TYPE_TAG);
                                }
                                if (value < limit + digit) {
                                    throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT16_TYPE_TAG);
                                }
                                value = (short) (value - digit);
                            }
                            if (value > 0) {
                                throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INT16_TYPE_TAG);
                            }
                            if (value < 0 && positive) {
                                value *= -1;
                            }
                            aInt16.setValue(value);
                            int16Serde.serialize(aInt16, out);
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, serString[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        result.set(resultStorage);
                    } catch (IOException e) {
                        throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_INT16_TYPE_TAG);
                    }
                }
            };
        }
    };
}
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) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) 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) AMutableInt16(org.apache.asterix.om.base.AMutableInt16)

Example 18 with UTF8StringPointable

use of org.apache.hyracks.data.std.primitive.UTF8StringPointable in project asterixdb by apache.

the class ACircleConstructorDescriptor method createEvaluatorFactory.

@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
    return new IScalarEvaluatorFactory() {

        private static final long serialVersionUID = 1L;

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

                private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private final DataOutput out = resultStorage.getDataOutput();

                private final IPointable inputArg = new VoidPointable();

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

                private final AMutablePoint aPoint = new AMutablePoint(0, 0);

                private AMutableCircle aCircle = new AMutableCircle(null, 0);

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ACircle> circleSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ACIRCLE);

                private final UTF8StringPointable utf8Ptr = new UTF8StringPointable();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    try {
                        resultStorage.reset();
                        eval.evaluate(tuple, inputArg);
                        byte[] serString = inputArg.getByteArray();
                        int offset = inputArg.getStartOffset();
                        int len = inputArg.getLength();
                        if (serString[offset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            utf8Ptr.set(serString, offset + 1, len - 1);
                            String s = utf8Ptr.toString();
                            int commaIndex = s.indexOf(',');
                            int spaceIndex = s.indexOf(' ', commaIndex + 1);
                            aPoint.setValue(Double.parseDouble(s.substring(0, commaIndex)), Double.parseDouble(s.substring(commaIndex + 1, spaceIndex)));
                            aCircle.setValue(aPoint, Double.parseDouble(s.substring(spaceIndex + 1, s.length())));
                            circleSerde.serialize(aCircle, out);
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, serString[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        result.set(resultStorage);
                    } catch (IOException e) {
                        throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_CIRCLE_TYPE_TAG);
                    }
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) AMutableCircle(org.apache.asterix.om.base.AMutableCircle) 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) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) AMutablePoint(org.apache.asterix.om.base.AMutablePoint) 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) AMutablePoint(org.apache.asterix.om.base.AMutablePoint)

Example 19 with UTF8StringPointable

use of org.apache.hyracks.data.std.primitive.UTF8StringPointable in project asterixdb by apache.

the class ALineConstructorDescriptor method createEvaluatorFactory.

@Override
public IScalarEvaluatorFactory createEvaluatorFactory(final IScalarEvaluatorFactory[] args) {
    return new IScalarEvaluatorFactory() {

        private static final long serialVersionUID = 1L;

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

                private ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private DataOutput out = resultStorage.getDataOutput();

                private IPointable inputArg = new VoidPointable();

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

                private AMutableLine aLine = new AMutableLine(null, null);

                private AMutablePoint[] aPoint = { new AMutablePoint(0, 0), new AMutablePoint(0, 0) };

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ALine> lineSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ALINE);

                private final UTF8StringPointable utf8Ptr = new UTF8StringPointable();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    try {
                        resultStorage.reset();
                        eval.evaluate(tuple, inputArg);
                        byte[] serString = inputArg.getByteArray();
                        int offset = inputArg.getStartOffset();
                        int len = inputArg.getLength();
                        if (serString[offset] == ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            utf8Ptr.set(serString, offset + 1, len - 1);
                            String s = utf8Ptr.toString();
                            int commaIndex = s.indexOf(',');
                            int spaceIndex = s.indexOf(' ', commaIndex + 1);
                            aPoint[0].setValue(Double.parseDouble(s.substring(0, commaIndex)), Double.parseDouble(s.substring(commaIndex + 1, spaceIndex)));
                            commaIndex = s.indexOf(',', spaceIndex + 1);
                            aPoint[1].setValue(Double.parseDouble(s.substring(spaceIndex + 1, commaIndex)), Double.parseDouble(s.substring(commaIndex + 1, s.length())));
                            aLine.setValue(aPoint[0], aPoint[1]);
                            lineSerde.serialize(aLine, out);
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, serString[offset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        result.set(resultStorage);
                    } catch (IOException e) {
                        throw new InvalidDataFormatException(getIdentifier(), e, ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                    }
                }
            };
        }
    };
}
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) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) AMutablePoint(org.apache.asterix.om.base.AMutablePoint) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) InvalidDataFormatException(org.apache.asterix.runtime.exceptions.InvalidDataFormatException) AMutableLine(org.apache.asterix.om.base.AMutableLine) 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) AMutablePoint(org.apache.asterix.om.base.AMutablePoint)

Example 20 with UTF8StringPointable

use of org.apache.hyracks.data.std.primitive.UTF8StringPointable in project asterixdb by apache.

the class UTF8StringBuilderTest method testOneString.

public void testOneString(String testString, int estimateLength) throws IOException {
    storage.reset();
    utf8StringBuilder.reset(storage, estimateLength);
    for (char c : testString.toCharArray()) {
        utf8StringBuilder.appendChar(c);
    }
    utf8StringBuilder.finish();
    assertEquals(testString, UTF8StringUtil.toString(new StringBuilder(), storage.getByteArray(), 0).toString());
    UTF8StringPointable hyracksUtf = new UTF8StringPointable();
    hyracksUtf.set(storage.getByteArray(), 0, storage.getLength());
    GrowableArray storage2 = new GrowableArray();
    utf8StringBuilder.reset(storage2, estimateLength);
    utf8StringBuilder.appendUtf8StringPointable(hyracksUtf);
    utf8StringBuilder.finish();
    assertEquals(testString, UTF8StringUtil.toString(new StringBuilder(), storage.getByteArray(), 0).toString());
}
Also used : UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable)

Aggregations

UTF8StringPointable (org.apache.hyracks.data.std.primitive.UTF8StringPointable)44 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)40 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)40 IPointable (org.apache.hyracks.data.std.api.IPointable)37 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)37 DataOutput (java.io.DataOutput)35 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)35 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)35 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)35 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)33 IOException (java.io.IOException)31 InvalidDataFormatException (org.apache.asterix.runtime.exceptions.InvalidDataFormatException)27 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)26 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)13 UTF8StringWriter (org.apache.hyracks.util.string.UTF8StringWriter)5 AMutableDuration (org.apache.asterix.om.base.AMutableDuration)4 AMutablePoint (org.apache.asterix.om.base.AMutablePoint)4 GrowableArray (org.apache.hyracks.data.std.util.GrowableArray)4 UTF8StringBuilder (org.apache.hyracks.data.std.util.UTF8StringBuilder)4 AMutableInterval (org.apache.asterix.om.base.AMutableInterval)3