Github user hvanhovell commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10335#discussion_r47834560
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/basicOperators.scala ---
    @@ -126,6 +127,69 @@ case class Sample(
       }
     }
     
    +case class Range(
    +    start: Long,
    +    step: Long,
    +    numSlices: Int,
    +    numElements: BigInt,
    +    output: Seq[Attribute])
    +  extends LeafNode
    +{
    +  override def outputsUnsafeRows: Boolean = true
    +
    +  protected override def doExecute(): RDD[InternalRow] = {
    +    sqlContext
    +      .sparkContext
    +      .parallelize(0 until numSlices, numSlices)
    +      .mapPartitionsWithIndex((i, _) => {
    +        val partitionStart = (i * numElements) / numSlices * step + start
    +        val partitionEnd = (((i + 1) * numElements) / numSlices) * step + 
start
    +        def getSafeMargin(bi: BigInt): Long =
    +          if (bi.isValidLong) {
    +            bi.toLong
    +          } else if (bi > 0) {
    +            Long.MaxValue
    +          } else {
    +            Long.MinValue
    +          }
    +        val safePartitionStart = getSafeMargin(partitionStart)
    +        val safePartitionEnd = getSafeMargin(partitionEnd)
    +        val bufferHolder = new BufferHolder(LongType.defaultSize)
    +        val unsafeRow = new UnsafeRow
    +
    +        new Iterator[InternalRow] {
    +          private[this] var number: Long = safePartitionStart
    +          private[this] var overflow: Boolean = false
    +
    +          override def hasNext =
    +            if (!overflow) {
    +              if (step > 0) {
    +                number < safePartitionEnd
    +              } else {
    +                number > safePartitionEnd
    +              }
    +            } else false
    +
    +          override def next() = {
    +            val ret = number
    +            number += step
    +            if (number < ret ^ step < 0) {
    +              // we have Long.MaxValue + Long.MaxValue < Long.MaxValue
    +              // and Long.MinValue + Long.MinValue > Long.MinValue, so iff 
the step causes a step
    +              // back, we are pretty sure that we have an overflow.
    +              overflow = true
    +            }
    +
    +            bufferHolder.reset()
    +            unsafeRow.pointTo(bufferHolder.buffer, 1, 
bufferHolder.totalSize())
    --- End diff --
    
    I am fine with both, as long as we move out of the ```next``` call.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to