/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.spark.sql.execution.python

import scala.jdk.CollectionConverters._

import org.apache.spark.{JobArtifactSet, SparkException, TaskContext}
import org.apache.spark.api.python.{ChainedPythonFunctions, PythonEvalType}
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.errors.QueryExecutionErrors
import org.apache.spark.sql.execution.SparkPlan
import org.apache.spark.sql.execution.metric.SQLMetric
import org.apache.spark.sql.execution.python.EvalPythonExec.ArgumentMetadata
import org.apache.spark.sql.types.{StructType, UserDefinedType}
import org.apache.spark.sql.types.DataType.equalsIgnoreCompatibleCollation

/**
 * Grouped a iterator into batches.
 * This is similar to iter.grouped but returns Iterator[T] instead of Seq[T].
 * This is necessary because sometimes we cannot hold reference of input rows
 * because the some input rows are mutable and can be reused.
 */
private[spark] class BatchIterator[T](iter: Iterator[T], batchSize: Int)
  extends Iterator[Iterator[T]] {

  override def hasNext: Boolean = iter.hasNext

  override def next(): Iterator[T] = {
    new Iterator[T] {
      var count = 0

      override def hasNext: Boolean = iter.hasNext && count < batchSize

      override def next(): T = {
        if (!hasNext) {
          Iterator.empty.next()
        } else {
          count += 1
          iter.next()
        }
      }
    }
  }
}

/**
 * A physical plan that evaluates a vectorized UDF.
 * Following eval types are supported:
 *
 * <ul>
 *   <li> SQL_ARROW_BATCHED_UDF for Arrow Optimized Python UDF
 *   <li> SQL_SCALAR_ARROW_UDF for Scalar Arrow UDF
 *   <li> SQL_SCALAR_ARROW_ITER_UDF for Scalar Iterator Arrow UDF
 *   <li> SQL_SCALAR_PANDAS_UDF for Scalar Pandas UDF
 *   <li> SQL_SCALAR_PANDAS_ITER_UDF for Scalar Iterator Pandas UDF
 * </ul>
 *
 */
case class ArrowEvalPythonExec(
    udfs: Seq[PythonUDF],
    resultAttrs: Seq[Attribute],
    child: SparkPlan,
    evalType: Int) extends EvalPythonExec with PythonSQLMetrics {
  if (!supportedPythonEvalTypes.contains(evalType)) {
    throw SparkException.internalError(s"Unexpected eval type $evalType")
  }

  private[this] val jobArtifactUUID = JobArtifactSet.getCurrentJobArtifactState.map(_.uuid)
  private[this] val sessionUUID = {
    Option(session).collect {
      case session if session.sessionState.conf.pythonWorkerLoggingEnabled =>
        session.sessionUUID
    }
  }

  override protected def evaluatorFactory: EvalPythonEvaluatorFactory = {
    new ArrowEvalPythonEvaluatorFactory(
      child.output,
      udfs,
      output,
      conf.arrowMaxRecordsPerBatch,
      evalType,
      conf.sessionLocalTimeZone,
      conf.arrowUseLargeVarTypes,
      ArrowPythonRunner.getPythonRunnerConfMap(conf),
      pythonMetrics,
      jobArtifactUUID,
      sessionUUID,
      conf.pythonUDFProfiler)
  }

  override protected def withNewChildInternal(newChild: SparkPlan): SparkPlan =
    copy(child = newChild)

  private def supportedPythonEvalTypes: Array[Int] =
    Array(
      PythonEvalType.SQL_ARROW_BATCHED_UDF,
      PythonEvalType.SQL_SCALAR_ARROW_UDF,
      PythonEvalType.SQL_SCALAR_ARROW_ITER_UDF,
      PythonEvalType.SQL_SCALAR_PANDAS_UDF,
      PythonEvalType.SQL_SCALAR_PANDAS_ITER_UDF)
}

class ArrowEvalPythonEvaluatorFactory(
    childOutput: Seq[Attribute],
    udfs: Seq[PythonUDF],
    output: Seq[Attribute],
    batchSize: Int,
    evalType: Int,
    sessionLocalTimeZone: String,
    largeVarTypes: Boolean,
    pythonRunnerConf: Map[String, String],
    pythonMetrics: Map[String, SQLMetric],
    jobArtifactUUID: Option[String],
    sessionUUID: Option[String],
    profiler: Option[String])
  extends EvalPythonEvaluatorFactory(childOutput, udfs, output) {

  override def evaluate(
      funcs: Seq[(ChainedPythonFunctions, Long)],
      argMetas: Array[Array[ArgumentMetadata]],
      iter: Iterator[InternalRow],
      schema: StructType,
      context: TaskContext): Iterator[InternalRow] = {

    val outputTypes = output.drop(childOutput.length).map(_.dataType.transformRecursively {
      case udt: UserDefinedType[_] => udt.sqlType
    })

    val batchIter = Iterator(iter)

    val pyRunner = new ArrowPythonWithNamedArgumentRunner(
      funcs,
      evalType,
      argMetas,
      schema,
      sessionLocalTimeZone,
      largeVarTypes,
      pythonRunnerConf,
      pythonMetrics,
      jobArtifactUUID,
      sessionUUID,
      profiler) with BatchedPythonArrowInput
    val columnarBatchIter = pyRunner.compute(batchIter, context.partitionId(), context)

    columnarBatchIter.flatMap { batch =>
      val actualDataTypes = (0 until batch.numCols()).map(i => batch.column(i).dataType())
      if (!equalsIgnoreCompatibleCollation(outputTypes, actualDataTypes)) {
        throw QueryExecutionErrors.arrowDataTypeMismatchError(
          "pandas_udf()", outputTypes, actualDataTypes)
      }
      batch.rowIterator.asScala
    }
  }
}
