Search in sources :

Example 1 with AMutableDate

use of org.apache.asterix.om.base.AMutableDate in project asterixdb by apache.

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

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

                private AMutableDate aDate = new AMutableDate(0);

                // possible returning types
                @SuppressWarnings("unchecked")
                private ISerializerDeserializer<ADate> dateSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATE);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    resultStorage.reset();
                    eval.evaluate(tuple, argPtr);
                    aDate.setValue(ATypeHierarchy.getIntegerValue(getIdentifier().getName(), 0, argPtr.getByteArray(), argPtr.getStartOffset()));
                    dateSerde.serialize(aDate, out);
                    result.set(resultStorage);
                }
            };
        }
    };
}
Also used : DataOutput(java.io.DataOutput) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AMutableDate(org.apache.asterix.om.base.AMutableDate) 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 2 with AMutableDate

use of org.apache.asterix.om.base.AMutableDate in project asterixdb by apache.

the class ADMDataParserTest method test.

@Test
public void test() throws IOException {
    String[] dates = { "-9537-08-04", "9656-06-03", "-9537-04-04", "9656-06-04", "-9537-10-04", "9626-09-05" };
    AMutableDate[] parsedDates = new AMutableDate[] { new AMutableDate(-4202630), new AMutableDate(2807408), new AMutableDate(-4202752), new AMutableDate(2807409), new AMutableDate(-4202569), new AMutableDate(2796544) };
    String[] times = { "12:04:45.689Z", "12:41:59.002Z", "12:10:45.169Z", "15:37:48.736Z", "04:16:42.321Z", "12:22:56.816Z" };
    AMutableTime[] parsedTimes = new AMutableTime[] { new AMutableTime(43485689), new AMutableTime(45719002), new AMutableTime(43845169), new AMutableTime(56268736), new AMutableTime(15402321), new AMutableTime(44576816) };
    String[] dateTimes = { "-2640-10-11T17:32:15.675Z", "4104-02-01T05:59:11.902Z", "0534-12-08T08:20:31.487Z", "6778-02-16T22:40:21.653Z", "2129-12-12T13:18:35.758Z", "8647-07-01T13:10:19.691Z" };
    AMutableDateTime[] parsedDateTimes = new AMutableDateTime[] { new AMutableDateTime(-145452954464325L), new AMutableDateTime(67345192751902L), new AMutableDateTime(-45286270768513L), new AMutableDateTime(151729886421653L), new AMutableDateTime(5047449515758L), new AMutableDateTime(210721439419691L) };
    Thread[] threads = new Thread[16];
    AtomicInteger errorCount = new AtomicInteger(0);
    for (int i = 0; i < threads.length; ++i) {
        threads[i] = new Thread(new Runnable() {

            ADMDataParser parser = new ADMDataParser(null, true);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            DataOutput dos = new DataOutputStream(bos);

            @Override
            public void run() {
                try {
                    int round = 0;
                    while (round++ < 10000) {
                        // Test parseDate.
                        for (int index = 0; index < dates.length; ++index) {
                            PA.invokeMethod(parser, "parseDate(java.lang.String, java.io.DataOutput)", dates[index], dos);
                            AMutableDate aDate = (AMutableDate) PA.getValue(parser, "aDate");
                            Assert.assertTrue(aDate.equals(parsedDates[index]));
                        }
                        // Tests parseTime.
                        for (int index = 0; index < times.length; ++index) {
                            PA.invokeMethod(parser, "parseTime(java.lang.String, java.io.DataOutput)", times[index], dos);
                            AMutableTime aTime = (AMutableTime) PA.getValue(parser, "aTime");
                            Assert.assertTrue(aTime.equals(parsedTimes[index]));
                        }
                        // Tests parseDateTime.
                        for (int index = 0; index < dateTimes.length; ++index) {
                            PA.invokeMethod(parser, "parseDateTime(java.lang.String, java.io.DataOutput)", dateTimes[index], dos);
                            AMutableDateTime aDateTime = (AMutableDateTime) PA.getValue(parser, "aDateTime");
                            Assert.assertTrue(aDateTime.equals(parsedDateTimes[index]));
                        }
                    }
                } catch (Exception e) {
                    errorCount.incrementAndGet();
                    e.printStackTrace();
                }
            }
        });
        // Kicks off test threads.
        threads[i].start();
    }
    // Joins all the threads.
    try {
        for (int i = 0; i < threads.length; ++i) {
            threads[i].join();
        }
    } catch (InterruptedException e) {
        throw new IllegalStateException(e);
    }
    // Asserts no failure.
    Assert.assertTrue(errorCount.get() == 0);
}
Also used : DataOutput(java.io.DataOutput) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ADMDataParser(org.apache.asterix.external.parser.ADMDataParser) AMutableDateTime(org.apache.asterix.om.base.AMutableDateTime) AMutableDate(org.apache.asterix.om.base.AMutableDate) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AMutableTime(org.apache.asterix.om.base.AMutableTime) Test(org.junit.Test)

Example 3 with AMutableDate

use of org.apache.asterix.om.base.AMutableDate in project asterixdb by apache.

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

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

                // possible output
                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<ADate> dateSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATE);

                private final AMutableDate aDate = new AMutableDate(0);

                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<ADateTime> datetimeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATETIME);

                private final AMutableDateTime aDateTime = new AMutableDateTime(0);

                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<ATime> timeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ATIME);

                private final AMutableTime aTime = new AMutableTime(0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    eval.evaluate(tuple, argPtr);
                    byte[] bytes = argPtr.getByteArray();
                    int startOffset = argPtr.getStartOffset();
                    resultStorage.reset();
                    try {
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG) {
                            byte timeType = AIntervalSerializerDeserializer.getIntervalTimeType(bytes, startOffset + 1);
                            long endTime = AIntervalSerializerDeserializer.getIntervalEnd(bytes, startOffset + 1);
                            if (timeType == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
                                aDate.setValue((int) (endTime));
                                dateSerde.serialize(aDate, out);
                            } else if (timeType == ATypeTag.SERIALIZED_TIME_TYPE_TAG) {
                                aTime.setValue((int) (endTime));
                                timeSerde.serialize(aTime, out);
                            } else if (timeType == ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
                                aDateTime.setValue(endTime);
                                datetimeSerde.serialize(aDateTime, out);
                            } else {
                                throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                            }
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes[startOffset], ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                        }
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                    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) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) InvalidDataFormatException(org.apache.asterix.runtime.exceptions.InvalidDataFormatException) AMutableDateTime(org.apache.asterix.om.base.AMutableDateTime) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AMutableDate(org.apache.asterix.om.base.AMutableDate) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableTime(org.apache.asterix.om.base.AMutableTime)

Example 4 with AMutableDate

use of org.apache.asterix.om.base.AMutableDate in project asterixdb by apache.

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

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

                // possible output
                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<ADate> dateSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATE);

                private final AMutableDate aDate = new AMutableDate(0);

                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<ADateTime> datetimeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATETIME);

                private final AMutableDateTime aDateTime = new AMutableDateTime(0);

                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<ATime> timeSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ATIME);

                private final AMutableTime aTime = new AMutableTime(0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    eval.evaluate(tuple, argPtr);
                    byte[] bytes = argPtr.getByteArray();
                    int startOffset = argPtr.getStartOffset();
                    resultStorage.reset();
                    try {
                        if (bytes[startOffset] == ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG) {
                            byte timeType = AIntervalSerializerDeserializer.getIntervalTimeType(bytes, startOffset + 1);
                            long startTime = AIntervalSerializerDeserializer.getIntervalStart(bytes, startOffset + 1);
                            if (timeType == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
                                aDate.setValue((int) (startTime));
                                dateSerde.serialize(aDate, out);
                            } else if (timeType == ATypeTag.SERIALIZED_TIME_TYPE_TAG) {
                                aTime.setValue((int) (startTime));
                                timeSerde.serialize(aTime, out);
                            } else if (timeType == ATypeTag.SERIALIZED_DATETIME_TYPE_TAG) {
                                aDateTime.setValue(startTime);
                                datetimeSerde.serialize(aDateTime, out);
                            } else {
                                throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                            }
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes[startOffset], ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                        }
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                    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) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) IScalarEvaluatorFactory(org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory) InvalidDataFormatException(org.apache.asterix.runtime.exceptions.InvalidDataFormatException) AMutableDateTime(org.apache.asterix.om.base.AMutableDateTime) ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) AMutableDate(org.apache.asterix.om.base.AMutableDate) IHyracksTaskContext(org.apache.hyracks.api.context.IHyracksTaskContext) VoidPointable(org.apache.hyracks.data.std.primitive.VoidPointable) IFrameTupleReference(org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference) AMutableTime(org.apache.asterix.om.base.AMutableTime)

Example 5 with AMutableDate

use of org.apache.asterix.om.base.AMutableDate in project asterixdb by apache.

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

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

                // possible output
                @SuppressWarnings("unchecked")
                private final ISerializerDeserializer<ADate> dateSerde = SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADATE);

                private final AMutableDate aDate = new AMutableDate(0);

                @Override
                public void evaluate(IFrameTupleReference tuple, IPointable result) throws HyracksDataException {
                    eval.evaluate(tuple, argPtr);
                    byte[] bytes = argPtr.getByteArray();
                    int startOffset = argPtr.getStartOffset();
                    resultStorage.reset();
                    try {
                        if (bytes[startOffset] == SERIALIZED_INTERVAL_TYPE_TAG) {
                            byte timeType = AIntervalSerializerDeserializer.getIntervalTimeType(bytes, startOffset + 1);
                            long startTime = AIntervalSerializerDeserializer.getIntervalStart(bytes, startOffset + 1);
                            if (timeType == ATypeTag.SERIALIZED_DATE_TYPE_TAG) {
                                aDate.setValue((int) (startTime));
                                dateSerde.serialize(aDate, out);
                            } else {
                                throw new InvalidDataFormatException(getIdentifier(), ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                            }
                        } else {
                            throw new TypeMismatchException(getIdentifier(), 0, bytes[startOffset], ATypeTag.SERIALIZED_INTERVAL_TYPE_TAG);
                        }
                    } catch (IOException e) {
                        throw new HyracksDataException(e);
                    }
                    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) ISerializerDeserializer(org.apache.hyracks.api.dataflow.value.ISerializerDeserializer) 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) AMutableDate(org.apache.asterix.om.base.AMutableDate) 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

DataOutput (java.io.DataOutput)11 AMutableDate (org.apache.asterix.om.base.AMutableDate)11 IScalarEvaluator (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator)10 IScalarEvaluatorFactory (org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory)10 IHyracksTaskContext (org.apache.hyracks.api.context.IHyracksTaskContext)10 ISerializerDeserializer (org.apache.hyracks.api.dataflow.value.ISerializerDeserializer)10 IPointable (org.apache.hyracks.data.std.api.IPointable)10 ArrayBackedValueStorage (org.apache.hyracks.data.std.util.ArrayBackedValueStorage)10 IFrameTupleReference (org.apache.hyracks.dataflow.common.data.accessors.IFrameTupleReference)10 VoidPointable (org.apache.hyracks.data.std.primitive.VoidPointable)9 TypeMismatchException (org.apache.asterix.runtime.exceptions.TypeMismatchException)8 IOException (java.io.IOException)6 InvalidDataFormatException (org.apache.asterix.runtime.exceptions.InvalidDataFormatException)6 AMutableDateTime (org.apache.asterix.om.base.AMutableDateTime)4 AMutableTime (org.apache.asterix.om.base.AMutableTime)4 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)4 UTF8StringPointable (org.apache.hyracks.data.std.primitive.UTF8StringPointable)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1