package org.qcri.rheem.spark.operators;

import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.qcri.rheem.basic.operators.SortOperator;
import org.qcri.rheem.core.function.TransformationDescriptor;
import org.qcri.rheem.core.optimizer.OptimizationContext;
import org.qcri.rheem.core.plan.rheemplan.ExecutionOperator;
import org.qcri.rheem.core.platform.ChannelDescriptor;
import org.qcri.rheem.core.platform.ChannelInstance;
import org.qcri.rheem.core.platform.lineage.ExecutionLineageNode;
import org.qcri.rheem.core.types.DataSetType;
import org.qcri.rheem.core.util.Tuple;
import org.qcri.rheem.spark.channels.RddChannel;
import org.qcri.rheem.spark.execution.SparkExecutor;
import scala.Tuple2;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Spark implementation of the {@link SortOperator}.
 */
public class SparkSortOperator<Type, Key>
        extends SortOperator<Type, Key>
        implements SparkExecutionOperator {


    /**
     * Creates a new instance.
     *
     * @param type type of the dataset elements
     */
    public SparkSortOperator(TransformationDescriptor<Type, Key> keyDescriptor, DataSetType<Type> type) {
        super(keyDescriptor, type);
    }

    /**
     * Copies an instance (exclusive of broadcasts).
     *
     * @param that that should be copied
     */
    public SparkSortOperator(SortOperator<Type, Key> that) {
        super(that);
    }

    @Override
    public Tuple<Collection<ExecutionLineageNode>, Collection<ChannelInstance>> evaluate(
            ChannelInstance[] inputs,
            ChannelInstance[] outputs,
            SparkExecutor sparkExecutor,
            OptimizationContext.OperatorContext operatorContext) {
        assert inputs.length == this.getNumInputs();
        assert outputs.length == this.getNumOutputs();

        RddChannel.Instance input = (RddChannel.Instance) inputs[0];
        RddChannel.Instance output = (RddChannel.Instance) outputs[0];

        final JavaRDD<Type> inputRdd = input.provideRdd();

        // TODO: Better sort function!
        final JavaPairRDD<Type, Boolean> keyedRdd = inputRdd.mapToPair(x -> new Tuple2<>(x, true));
        this.name(keyedRdd);
        final JavaPairRDD<Type, Boolean> sortedKeyedRdd = keyedRdd.sortByKey(true, sparkExecutor.getNumDefaultPartitions());
        this.name(sortedKeyedRdd);
        final JavaRDD<Type> outputRdd = sortedKeyedRdd.map(y -> y._1);
        this.name(outputRdd);

        output.accept(outputRdd, sparkExecutor);

        return ExecutionOperator.modelLazyExecution(inputs, outputs, operatorContext);
    }

    @Override
    protected ExecutionOperator createCopy() {
        return new SparkSortOperator<>(this.getKeyDescriptor(), this.getInputType());
    }

    @Override
    public String getLoadProfileEstimatorConfigurationKey() {
        return "rheem.spark.sort.load";
    }

    @Override
    public List<ChannelDescriptor> getSupportedInputChannels(int index) {
        assert index <= this.getNumInputs() || (index == 0 && this.getNumInputs() == 0);
        return Arrays.asList(RddChannel.UNCACHED_DESCRIPTOR, RddChannel.CACHED_DESCRIPTOR);
    }

    @Override
    public List<ChannelDescriptor> getSupportedOutputChannels(int index) {
        assert index <= this.getNumOutputs() || (index == 0 && this.getNumOutputs() == 0);
        return Collections.singletonList(RddChannel.UNCACHED_DESCRIPTOR);
    }

    @Override
    public boolean containsAction() {
        return false;
    }

}


@@ -118,24 +118,80 @@
 va.JavaRDD;%0A
+import org.apache.spark.api.java.function.PairFunction;%0A
 import org.q
@@ -719,16 +719,71 @@
 hannel;%0A
+import org.qcri.rheem.spark.compiler.FunctionCompiler;%0A
 import o
@@ -2205,47 +2205,193 @@
 ();%0A
-%0A        // TODO: Better sort function!
+        %0A        FunctionCompiler compiler = sparkExecutor.getCompiler();%0A        final PairFunction%3CType, Key, Type%3E keyExtractor = compiler.compileToKeyExtractor(this.keyDescriptor);%0A
 %0A   
@@ -2413,29 +2413,25 @@
 PairRDD%3C
-Type, Boolean
+Key, Type
 %3E keyedR
@@ -2458,34 +2458,20 @@
 air(
-x -%3E new Tuple2%3C%3E(x, true)
+keyExtractor
 );%0A 
@@ -2528,21 +2528,17 @@
 RDD%3C
-Type, Boolean
+Key, Type
 %3E so
@@ -2725,9 +2725,9 @@
  y._
-1
+2
 );%0A 

Fix for SparkSortOperator