You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by hvanhovell <gi...@git.apache.org> on 2016/02/02 23:04:46 UTC

[GitHub] spark pull request: [SPARK-12706] [SQL] grouping() and grouping_id...

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

    https://github.com/apache/spark/pull/10677#discussion_r51643536
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/bitwiseExpressions.scala ---
    @@ -124,3 +124,47 @@ case class BitwiseNot(child: Expression) extends UnaryExpression with ExpectsInp
     
       protected override def nullSafeEval(input: Any): Any = not(input)
     }
    +
    +/**
    +  * A function that reverse the lowest N bits of a integer.
    +  *
    +  * Note: this is only used for grouping_id()
    +  */
    +case class BitwiseReverse(child: Expression, width: Int)
    +  extends UnaryExpression with ExpectsInputTypes {
    +
    +  override def inputTypes: Seq[AbstractDataType] = Seq(IntegerType)
    +
    +  override def dataType: DataType = IntegerType
    +
    +  override def toString: String = s"^$child"
    +
    +  override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
    +    nullSafeCodeGen(ctx, ev, c => {
    +      val v = ctx.freshName("v")
    +      val i = ctx.freshName("i")
    +      s"""
    +         | int $v = $c;
    +         | ${ev.value} = 0;
    +         | for (int $i = 0; $i < $width; $i ++) {
    +         |   ${ev.value} <<= 1;
    +         |   ${ev.value} |= $v & 1;
    +         |   $v >>>= 1;
    +         | }
    +       """.stripMargin
    +    })
    +  }
    +
    +  protected override def nullSafeEval(input: Any): Any = {
    +    var v = input.asInstanceOf[Int]
    --- End diff --
    
    Nit: We could also use ```Integer.reverse``` and a mask here. It saves some code and a branch.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org