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

    https://github.com/apache/spark/pull/10335#discussion_r47828311
  
    --- 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 --
    
    @hvanhovell Thank you! You are right, let me move it to the construction of 
the Iterator. 
    
    When writing a prototype, I used a 16 bytes array. I am just not sure if 
the code should just use the existing library here. Thus, I changed it to 
bufferHolder. 


---
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