Search in sources :

Example 1 with UTF8StringPointable

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

the class ParseBinaryDescriptor 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) {

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ABinary> binarySerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ABINARY);

                private AMutableBinary aBinary = new AMutableBinary(new byte[0], 0, 0);

                private final UTF8StringPointable stringPointable = new UTF8StringPointable();

                private final UTF8StringPointable formatPointable = new UTF8StringPointable();

                private final HexParser hexParser = new HexParser();

                private final Base64Parser base64Parser = new Base64Parser();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    evaluators[0].evaluate(tuple, pointables[0]);
                    evaluators[1].evaluate(tuple, pointables[1]);
                    ATypeTag binaryTag = ATypeTag.VALUE_TYPE_MAPPING[pointables[0].getByteArray()[pointables[0].getStartOffset()]];
                    ATypeTag formatTag = ATypeTag.VALUE_TYPE_MAPPING[pointables[1].getByteArray()[pointables[1].getStartOffset()]];
                    checkTypeMachingThrowsIfNot(getIdentifier().getName(), EXPECTED_INPUT_TAGS, binaryTag, formatTag);
                    stringPointable.set(pointables[0].getByteArray(), pointables[0].getStartOffset() + 1, pointables[0].getLength());
                    formatPointable.set(pointables[1].getByteArray(), pointables[1].getStartOffset() + 1, pointables[1].getLength());
                    if (HEX_FORMAT.ignoreCaseCompareTo(formatPointable) == 0) {
                        hexParser.generateByteArrayFromHexString(stringPointable.getByteArray(), stringPointable.getCharStartOffset(), stringPointable.getUTF8Length());
                        aBinary.setValue(hexParser.getByteArray(), 0, hexParser.getLength());
                    } else if (BASE64_FORMAT.ignoreCaseCompareTo(formatPointable) == 0) {
                        base64Parser.generatePureByteArrayFromBase64String(stringPointable.getByteArray(), stringPointable.getCharStartOffset(), stringPointable.getUTF8Length());
                        aBinary.setValue(base64Parser.getByteArray(), 0, base64Parser.getLength());
                    } else {
                        throw new UnsupportedItemTypeException(getIdentifier(), formatTag.serialize());
                    }
                    binarySerde.serialize(aBinary, dataOutput);
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : UnsupportedItemTypeException(org.apache.asterix.runtime.exceptions.UnsupportedItemTypeException) ATypeTag(org.apache.asterix.om.types.ATypeTag) AMutableBinary(org.apache.asterix.om.base.AMutableBinary) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) HexParser(org.apache.hyracks.util.bytes.HexParser) Base64Parser(org.apache.hyracks.util.bytes.Base64Parser) IPointable(org.apache.hyracks.data.std.api.IPointable) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)

Example 2 with UTF8StringPointable

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

the class StringSplitDescriptor 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() {

                // Argument evaluators.
                private final IScalarEvaluator stringEval = args[0].createScalarEvaluator(ctx);

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

                // Argument pointers.
                private final IPointable argString = new VoidPointable();

                private final IPointable argPattern = new VoidPointable();

                private final UTF8StringPointable argStrPtr = new UTF8StringPointable();

                private final UTF8StringPointable argPatternPtr = new UTF8StringPointable();

                // For an output string item.
                private final ArrayBackedValueStorage itemStorge = new ArrayBackedValueStorage();

                private final DataOutput itemOut = itemStorge.getDataOutput();

                private final byte[] tempLengthArray = new byte[5];

                // For the output list of strings.
                private final AOrderedListType intListType = new AOrderedListType(BuiltinType.ASTRING, null);

                private final OrderedListBuilder listBuilder = new OrderedListBuilder();

                private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();

                private final DataOutput out = resultStorage.getDataOutput();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    try {
                        resultStorage.reset();
                        // Calls argument evaluators.
                        stringEval.evaluate(tuple, argString);
                        patternEval.evaluate(tuple, argPattern);
                        // Gets the bytes of the source string.
                        byte[] srcString = argString.getByteArray();
                        int srcOffset = argString.getStartOffset();
                        int srcLen = argString.getLength();
                        // Type check for the first argument.
                        if (srcString[srcOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 0, srcString[srcOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        // Gets the bytes of the pattern string.
                        byte[] patternString = argPattern.getByteArray();
                        int patternOffset = argPattern.getStartOffset();
                        int patternLen = argPattern.getLength();
                        // Type check for the second argument.
                        if (patternString[patternOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                            throw new TypeMismatchException(getIdentifier(), 1, patternString[patternOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        }
                        // Sets the UTF8 String pointables.
                        argStrPtr.set(srcString, srcOffset + 1, srcLen - 1);
                        argPatternPtr.set(patternString, patternOffset + 1, patternLen - 1);
                        // Gets the string length of the source string.
                        int inputStringLen = UTF8StringUtil.getUTFLength(srcString, srcOffset + 1);
                        int inputStringStart = srcOffset + 1 + UTF8StringUtil.getNumBytesToStoreLength(inputStringLen);
                        // Gets the string length of the pattern string.
                        int inputPatternLen = UTF8StringUtil.getUTFLength(patternString, patternOffset + 1);
                        // Handles the case that the pattern is "".
                        boolean emptyStringPattern = inputPatternLen == 0;
                        // Builds a list of strings.
                        listBuilder.reset(intListType);
                        int itemStrStart = 0;
                        int nextMatchStart;
                        while (itemStrStart < inputStringLen && (nextMatchStart = UTF8StringPointable.find(argStrPtr, argPatternPtr, false, itemStrStart)) >= 0) {
                            // Adds an item string.
                            addItemString(srcString, inputStringStart, itemStrStart, emptyStringPattern ? nextMatchStart + 1 : nextMatchStart);
                            itemStrStart = nextMatchStart + (emptyStringPattern ? 1 : inputPatternLen);
                        }
                        if (!emptyStringPattern) {
                            addItemString(srcString, inputStringStart, itemStrStart, inputStringLen);
                        }
                        listBuilder.write(out, true);
                        result.set(resultStorage);
                    } catch (IOException e1) {
                        throw new HyracksDataException(e1);
                    }
                }

                private void addItemString(byte[] srcString, int inputStringStart, int itemStartOffset, int nextMatchStart) throws IOException {
                    int itemLen = nextMatchStart - itemStartOffset;
                    int cbytes = UTF8StringUtil.encodeUTF8Length(itemLen, tempLengthArray, 0);
                    itemStorge.reset();
                    itemOut.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                    itemOut.write(tempLengthArray, 0, cbytes);
                    if (itemLen > 0) {
                        itemOut.write(srcString, inputStringStart + itemStartOffset, itemLen);
                    }
                    listBuilder.addItem(itemStorge);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) OrderedListBuilder(org.apache.asterix.builders.OrderedListBuilder) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) 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)

Example 3 with UTF8StringPointable

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

the class StringTrim2Descriptor 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 AbstractBinaryStringStringEval(ctx, args[0], args[1], StringTrim2Descriptor.this.getIdentifier()) {

                private StringTrimmer stringTrimmer = new StringTrimmer(resultBuilder, resultArray);

                @Override
                protected void compute(UTF8StringPointable srcPtr, UTF8StringPointable patternPtr, UTF8StringPointable resultStrPtr) throws IOException {
                    stringTrimmer.build(patternPtr);
                    stringTrimmer.trim(srcPtr, resultStrPtr, true, true);
                }
            };
        }
    };
}
Also used : UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) StringTrimmer(org.apache.asterix.runtime.evaluators.functions.utils.StringTrimmer) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)

Example 4 with UTF8StringPointable

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

the class StringRTrim2Descriptor 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 AbstractBinaryStringStringEval(ctx, args[0], args[1], StringRTrim2Descriptor.this.getIdentifier()) {

                private StringTrimmer stringTrimmer = new StringTrimmer(resultBuilder, resultArray);

                @Override
                protected void compute(UTF8StringPointable srcPtr, UTF8StringPointable patternPtr, UTF8StringPointable resultStrPtr) throws IOException {
                    stringTrimmer.build(patternPtr);
                    stringTrimmer.trim(srcPtr, resultStrPtr, false, true);
                }
            };
        }
    };
}
Also used : UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) StringTrimmer(org.apache.asterix.runtime.evaluators.functions.utils.StringTrimmer) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)

Example 5 with UTF8StringPointable

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

the class SubstringAfterDescriptor 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 array0 = new VoidPointable();

                private IPointable array1 = new VoidPointable();

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

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

                private final GrowableArray array = new GrowableArray();

                private final UTF8StringBuilder builder = new UTF8StringBuilder();

                private final UTF8StringPointable stringPtr = new UTF8StringPointable();

                private final UTF8StringPointable patternPtr = new UTF8StringPointable();

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    evalString.evaluate(tuple, array0);
                    byte[] src = array0.getByteArray();
                    int srcOffset = array0.getStartOffset();
                    int srcLen = array0.getLength();
                    evalPattern.evaluate(tuple, array1);
                    byte[] pattern = array1.getByteArray();
                    int patternOffset = array1.getStartOffset();
                    int patternLen = array1.getLength();
                    if (src[srcOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, src[srcOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                    }
                    if (pattern[patternOffset] != ATypeTag.SERIALIZED_STRING_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 1, pattern[patternOffset], ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                    }
                    try {
                        stringPtr.set(src, srcOffset + 1, srcLen - 1);
                        patternPtr.set(pattern, patternOffset + 1, patternLen - 1);
                        array.reset();
                        UTF8StringPointable.substrAfter(stringPtr, patternPtr, builder, array);
                        out.writeByte(ATypeTag.SERIALIZED_STRING_TYPE_TAG);
                        out.write(array.getByteArray(), 0, array.getLength());
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) UTF8StringPointable(org.apache.hyracks.data.std.primitive.UTF8StringPointable) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) GrowableArray(org.apache.hyracks.data.std.util.GrowableArray) IPointable(org.apache.hyracks.data.std.api.IPointable) IOException(java.io.IOException) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) UTF8StringBuilder(org.apache.hyracks.data.std.util.UTF8StringBuilder) 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)

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