private SqlTaskExecution(
      TaskStateMachine taskStateMachine,
      TaskContext taskContext,
      SharedBuffer sharedBuffer,
      PlanFragment fragment,
      LocalExecutionPlanner planner,
      TaskExecutor taskExecutor,
      QueryMonitor queryMonitor,
      Executor notificationExecutor) {
    this.taskStateMachine = checkNotNull(taskStateMachine, "taskStateMachine is null");
    this.taskId = taskStateMachine.getTaskId();
    this.taskContext = checkNotNull(taskContext, "taskContext is null");
    this.sharedBuffer = checkNotNull(sharedBuffer, "sharedBuffer is null");

    this.taskExecutor = checkNotNull(taskExecutor, "driverExecutor is null");
    this.notificationExecutor = checkNotNull(notificationExecutor, "notificationExecutor is null");

    this.queryMonitor = checkNotNull(queryMonitor, "queryMonitor is null");

    try (SetThreadName ignored = new SetThreadName("Task-%s", taskId)) {
      List<DriverFactory> driverFactories;
      try {
        OutputFactory outputOperatorFactory;
        if (fragment.getOutputPartitioning() == OutputPartitioning.NONE) {
          outputOperatorFactory = new TaskOutputFactory(sharedBuffer);
        } else if (fragment.getOutputPartitioning() == OutputPartitioning.HASH) {
          outputOperatorFactory = new PartitionedOutputFactory(sharedBuffer);
        } else {
          throw new PrestoException(
              NOT_SUPPORTED,
              format("OutputPartitioning %s is not supported", fragment.getOutputPartitioning()));
        }

        LocalExecutionPlan localExecutionPlan =
            planner.plan(
                taskContext.getSession(),
                fragment.getRoot(),
                fragment.getOutputLayout(),
                fragment.getSymbols(),
                fragment.getDistribution(),
                outputOperatorFactory);
        driverFactories = localExecutionPlan.getDriverFactories();
      } catch (Throwable e) {
        // planning failed
        taskStateMachine.failed(e);
        throw Throwables.propagate(e);
      }

      // index driver factories
      DriverSplitRunnerFactory partitionedDriverFactory = null;
      ImmutableList.Builder<DriverSplitRunnerFactory> unpartitionedDriverFactories =
          ImmutableList.builder();
      for (DriverFactory driverFactory : driverFactories) {
        if (driverFactory.getSourceIds().contains(fragment.getPartitionedSource())) {
          checkState(
              partitionedDriverFactory == null, "multiple partitioned sources are not supported");
          partitionedDriverFactory = new DriverSplitRunnerFactory(driverFactory);
        } else {
          unpartitionedDriverFactories.add(new DriverSplitRunnerFactory(driverFactory));
        }
      }
      this.unpartitionedDriverFactories = unpartitionedDriverFactories.build();

      if (fragment.getDistribution() == PlanDistribution.SOURCE) {
        checkArgument(
            partitionedDriverFactory != null,
            "Fragment is partitioned, but no partitioned driver found");
      }
      this.partitionedSourceId = fragment.getPartitionedSource();
      this.partitionedDriverFactory = partitionedDriverFactory;

      // don't register the task if it is already completed (most likely failed during planning
      // above)
      if (!taskStateMachine.getState().isDone()) {
        taskHandle = taskExecutor.addTask(taskId);
        taskStateMachine.addStateChangeListener(
            new RemoveTaskHandleWhenDone(taskExecutor, taskHandle));
      } else {
        taskHandle = null;
      }

      sharedBuffer.addStateChangeListener(
          new CheckTaskCompletionOnBufferFinish(SqlTaskExecution.this));
    }
  }