Search in sources :

Example 51 with DataOutput

use of java.io.DataOutput in project asterixdb by apache.

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

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

                // possible returning types
                private AMutableInt64 aInt64 = new AMutableInt64(0);

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<AInt64> int64Serde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.AINT64);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval.evaluate(tuple, argPtr);
                    byte[] bytes = argPtr.getByteArray();
                    int offset = argPtr.getStartOffset();
                    if (bytes[offset] != ATypeTag.SERIALIZED_TIME_TYPE_TAG) {
                        throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_TIME_TYPE_TAG);
                    }
                    long timeChronon = ATimeSerializerDeserializer.getChronon(bytes, offset + 1);
                    aInt64.setValue(timeChronon);
                    int64Serde.serialize(aInt64, out);
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) 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) AMutableInt64(org.apache.asterix.om.base.AMutableInt64)

Example 52 with DataOutput

use of java.io.DataOutput in project asterixdb by apache.

the class DurationFromMillisecondsDescriptor 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 IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ADuration> durationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADURATION);

                AMutableDuration aDuration = new AMutableDuration(0, 0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    byte[] bytes = argPtr0.getByteArray();
                    int offset = argPtr0.getStartOffset();
                    ATypeTag argPtrTypeTag = ATypeTag.VALUE_TYPE_MAPPING[bytes[offset]];
                    switch(argPtrTypeTag) {
                        case TINYINT:
                            aDuration.setValue(0, AInt8SerializerDeserializer.getByte(bytes, offset + 1));
                            break;
                        case SMALLINT:
                            aDuration.setValue(0, AInt16SerializerDeserializer.getShort(bytes, offset + 1));
                            break;
                        case INTEGER:
                            aDuration.setValue(0, AInt32SerializerDeserializer.getInt(bytes, offset + 1));
                            break;
                        case BIGINT:
                            aDuration.setValue(0, AInt64SerializerDeserializer.getLong(bytes, offset + 1));
                            break;
                        default:
                            throw new TypeMismatchException(getIdentifier(), 0, bytes[offset], ATypeTag.SERIALIZED_INT8_TYPE_TAG, ATypeTag.SERIALIZED_INT16_TYPE_TAG, ATypeTag.SERIALIZED_INT32_TYPE_TAG, ATypeTag.SERIALIZED_INT64_TYPE_TAG);
                    }
                    durationSerde.serialize(aDuration, out);
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) AMutableDuration(org.apache.asterix.om.base.AMutableDuration) TypeMismatchException(org.apache.asterix.runtime.exceptions.TypeMismatchException) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) ATypeTag(org.apache.asterix.om.types.ATypeTag) 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 53 with DataOutput

use of java.io.DataOutput in project asterixdb by apache.

the class DurationFromMonthsDescriptor 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 IScalarEvaluator eval0 = args[0].createScalarEvaluator(ctx);

                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ADuration> durationSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADURATION);

                AMutableDuration aDuration = new AMutableDuration(0, 0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval0.evaluate(tuple, argPtr0);
                    byte[] bytes = argPtr0.getByteArray();
                    int offset = argPtr0.getStartOffset();
                    aDuration.setValue(ATypeHierarchy.getIntegerValue(getIdentifier().getName(), 0, bytes, offset), 0);
                    durationSerde.serialize(aDuration, out);
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AMutableDuration(org.apache.asterix.om.base.AMutableDuration) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) IPointable(org.apache.hyracks.data.std.api.IPointable) IScalarEvaluator(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)

Example 54 with DataOutput

use of java.io.DataOutput in project asterixdb by apache.

the class AMurmurHash3BinaryHashFunctionFamily method createBinaryHashFunction.

// This hash function family is used to promote a numeric type to a DOUBLE numeric type
// to return same hash value for the original numeric value, regardless of the numeric type.
// (e.g., h( int64("1") )  =  h( double("1.0") )
@Override
public IBinaryHashFunction createBinaryHashFunction(final int seed) {
    return new IBinaryHashFunction() {

        private ArrayBackedValueStorage fieldValueBuffer = new ArrayBackedValueStorage();

        private DataOutput fieldValueBufferOutput = fieldValueBuffer.getDataOutput();

        private ATypeTag sourceTag = null;

        private boolean numericTypePromotionApplied = false;

        @Override
        public int hash(byte[] bytes, int offset, int length) throws HyracksDataException {
            // If a numeric type is encountered, then we promote each numeric type to the DOUBLE type.
            fieldValueBuffer.reset();
            sourceTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes[offset]);
            switch(sourceTag) {
                case TINYINT:
                case SMALLINT:
                case INTEGER:
                case BIGINT:
                    try {
                        IntegerToDoubleTypeConvertComputer.getInstance().convertType(bytes, offset + 1, length - 1, fieldValueBufferOutput);
                    } catch (IOException e) {
                        throw new HyracksDataException("A numeric type promotion error has occurred before doing hash(). Can't continue process. Detailed Error message:" + e.getMessage());
                    }
                    numericTypePromotionApplied = true;
                    break;
                case FLOAT:
                    try {
                        FloatToDoubleTypeConvertComputer.getInstance().convertType(bytes, offset + 1, length - 1, fieldValueBufferOutput);
                    } catch (IOException e) {
                        throw new HyracksDataException("A numeric type promotion error has occurred before doing hash(). Can't continue process. Detailed Error message:" + e.getMessage());
                    }
                    numericTypePromotionApplied = true;
                    break;
                default:
                    numericTypePromotionApplied = false;
                    break;
            }
            // If a numeric type promotion happened
            if (numericTypePromotionApplied) {
                return MurmurHash3BinaryHash.hash(fieldValueBuffer.getByteArray(), fieldValueBuffer.getStartOffset(), fieldValueBuffer.getLength(), seed);
            } else {
                // Usual case for non numeric types and the DOBULE numeric type
                return MurmurHash3BinaryHash.hash(bytes, offset, length, seed);
            }
        }
    };
}
Also used : DataOutput(java.io.DataOutput) IBinaryHashFunction(org.apache.hyracks.api.dataflow.value.IBinaryHashFunction) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) ATypeTag(org.apache.asterix.om.types.ATypeTag) IOException(java.io.IOException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 55 with DataOutput

use of java.io.DataOutput in project asterixdb by apache.

the class IntegerParserFactory method createValueParser.

@Override
public IValueParser createValueParser() {
    return new IValueParser() {

        @Override
        public void parse(char[] buffer, int start, int length, DataOutput out) throws HyracksDataException {
            int n = 0;
            int sign = 1;
            int i = 0;
            boolean pre = true;
            for (; pre && i < length; ++i) {
                char ch = buffer[i + start];
                switch(ch) {
                    case ' ':
                    case '\t':
                    case '\n':
                    case '\r':
                    case '\f':
                        break;
                    case '-':
                        sign = -1;
                        pre = false;
                        break;
                    case '0':
                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9':
                        pre = false;
                        n = n * 10 + (ch - '0');
                        break;
                    default:
                        String errorString = new String(buffer, i + start, length - i);
                        throw new HyracksDataException("Integer Parser - a digit is expected. But, encountered this character: " + ch + " in the incoming input: " + errorString);
                }
            }
            boolean post = false;
            for (; !post && i < length; ++i) {
                char ch = buffer[i + start];
                switch(ch) {
                    case '0':
                    case '1':
                    case '2':
                    case '3':
                    case '4':
                    case '5':
                    case '6':
                    case '7':
                    case '8':
                    case '9':
                        n = n * 10 + (ch - '0');
                        break;
                    default:
                        String errorString = new String(buffer, i + start, length - i);
                        throw new HyracksDataException("Integer Parser - a digit is expected. But, encountered this character: " + ch + " in the incoming input: " + errorString);
                }
            }
            for (; i < length; ++i) {
                char ch = buffer[i + start];
                switch(ch) {
                    case ' ':
                    case '\t':
                    case '\n':
                    case '\r':
                    case '\f':
                        break;
                    default:
                        String errorString = new String(buffer, i + start, length - i);
                        throw new HyracksDataException("Integer Parser - a whitespace, tab, new line, or " + "form-feed expected. But, encountered this character: " + ch + " in the incoming input: " + errorString);
                }
            }
            try {
                out.writeInt(n * sign);
            } catch (IOException e) {
                throw new HyracksDataException(e);
            }
        }
    };
}
Also used : DataOutput(java.io.DataOutput) IOException(java.io.IOException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Aggregations

DataOutput (java.io.DataOutput)295 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)140 IPointable (org.apache.hyracks.data.std.api.IPointable)135 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)134 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)133 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)129 IOException (java.io.IOException)127 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)126 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)125 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)124 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)116 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)96 DataOutputStream (java.io.DataOutputStream)79 ByteArrayOutputStream (java.io.ByteArrayOutputStream)60 Test (org.junit.Test)56 InvalidDataFormatException (org.apache.asterix.runtime.exceptions.InvalidDataFormatException)48 DataInputStream (java.io.DataInputStream)40 ByteArrayInputStream (java.io.ByteArrayInputStream)39 UTF8StringPointable (org.apache.hyracks.data.std.primitive.UTF8StringPointable)35 ArrayTupleBuilder (org.apache.hyracks.dataflow.common.comm.io.ArrayTupleBuilder)33