package edu.uci.ics.asterix.runtime.evaluators.functions;

import java.util.UUID;

import edu.uci.ics.asterix.dataflow.data.nontagged.serde.AUUIDSerializerDeserializer;
import edu.uci.ics.asterix.om.base.AMutableUUID;
import edu.uci.ics.asterix.om.functions.AsterixBuiltinFunctions;
import edu.uci.ics.asterix.om.functions.IFunctionDescriptor;
import edu.uci.ics.asterix.om.functions.IFunctionDescriptorFactory;
import edu.uci.ics.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor;
import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
import edu.uci.ics.hyracks.algebricks.runtime.base.ICopyEvaluator;
import edu.uci.ics.hyracks.algebricks.runtime.base.ICopyEvaluatorFactory;
import edu.uci.ics.hyracks.api.exceptions.HyracksDataException;
import edu.uci.ics.hyracks.data.std.api.IDataOutputProvider;
import edu.uci.ics.hyracks.dataflow.common.data.accessors.IFrameTupleReference;

public class CreateUUIDDescriptor extends AbstractScalarFunctionDynamicDescriptor {

    private static final long serialVersionUID = 1L;

    public static final IFunctionDescriptorFactory FACTORY = new IFunctionDescriptorFactory() {
        public IFunctionDescriptor createFunctionDescriptor() {
            return new CreateUUIDDescriptor();
        }
    };

    @Override
    public ICopyEvaluatorFactory createEvaluatorFactory(ICopyEvaluatorFactory[] args) throws AlgebricksException {
        return new ICopyEvaluatorFactory() {

            private static final long serialVersionUID = 1L;

            @Override
            public ICopyEvaluator createEvaluator(final IDataOutputProvider output) throws AlgebricksException {
                return new ICopyEvaluator() {
                    final AMutableUUID uuid = new AMutableUUID(null);

                    @Override
                    public void evaluate(IFrameTupleReference tuple) throws AlgebricksException {
                        uuid.setValue(UUID.randomUUID());
                        try {
                            AUUIDSerializerDeserializer.INSTANCE.serialize(uuid, output.getDataOutput());
                        } catch (HyracksDataException e) {
                            throw new AlgebricksException(e);
                        }
                    }
                };
            }
        };
    }

    @Override
    public FunctionIdentifier getIdentifier() {
        return AsterixBuiltinFunctions.CREATE_UUID;
    }

}

@@ -107,21 +107,15 @@
 rix.
-dataflow.data
+formats
 .non
@@ -125,19 +125,11 @@
 ged.
-serde.AUUID
+Aql
 Seri
@@ -146,16 +146,24 @@
 rializer
+Provider
 ;%0Aimport
@@ -205,16 +205,58 @@
 leUUID;%0A
+import edu.uci.ics.asterix.om.base.AUUID;%0A
 import e
@@ -433,32 +433,81 @@
 criptorFactory;%0A
+import edu.uci.ics.asterix.om.types.BuiltinType;%0A
 import edu.uci.i
@@ -873,32 +873,103 @@
 aluatorFactory;%0A
+import edu.uci.ics.hyracks.api.dataflow.value.ISerializerDeserializer;%0A
 import edu.uci.i
@@ -1755,24 +1755,247 @@
 nUID = 1L;%0A%0A
+            @SuppressWarnings(%22unchecked%22)%0A            private final ISerializerDeserializer%3CAUUID%3E uuidSerDe = AqlSerializerDeserializerProvider.INSTANCE%0A                    .getSerializerDeserializer(BuiltinType.AUUID);%0A%0A
             
@@ -2482,44 +2482,17 @@
     
-AUUIDSerializerDeserializer.INSTANCE
+uuidSerDe
 .ser

use aqlserdeprovider when serializing uuids in create-uuid