Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,26 @@ case class BitwiseCount(child: Expression)

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = child.dataType match {
case BooleanType => defineCodeGen(ctx, ev, c => s"($c) ? 1 : 0")
case _ => defineCodeGen(ctx, ev, c => s"java.lang.Long.bitCount($c)")
case ByteType => defineCodeGen(ctx, ev, c =>
s"java.lang.Integer.bitCount(java.lang.Byte.toUnsignedInt($c))")
case ShortType => defineCodeGen(ctx, ev, c =>
s"java.lang.Integer.bitCount(java.lang.Short.toUnsignedInt($c))")
case IntegerType =>
defineCodeGen(ctx, ev, c => s"java.lang.Integer.bitCount($c)")
case LongType =>
defineCodeGen(ctx, ev, c => s"java.lang.Long.bitCount($c)")
}

protected override def nullSafeEval(input: Any): Any = child.dataType match {
case BooleanType => if (input.asInstanceOf[Boolean]) 1 else 0
case ByteType => java.lang.Long.bitCount(input.asInstanceOf[Byte])
case ShortType => java.lang.Long.bitCount(input.asInstanceOf[Short])
case IntegerType => java.lang.Long.bitCount(input.asInstanceOf[Int])
case LongType => java.lang.Long.bitCount(input.asInstanceOf[Long])
case ByteType => java.lang.Integer.bitCount(
java.lang.Byte.toUnsignedInt(input.asInstanceOf[Byte]))
case ShortType => java.lang.Integer.bitCount(
java.lang.Short.toUnsignedInt(input.asInstanceOf[Short]))
case IntegerType =>
java.lang.Integer.bitCount(input.asInstanceOf[Int])
case LongType =>
java.lang.Long.bitCount(input.asInstanceOf[Long])
}

override protected def withNewChildInternal(newChild: Expression): BitwiseCount =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,27 @@ class BitwiseExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(BitwiseCount(Literal(-9223372036854775808L)), 1)
}

test("BitCount should respect input integer type bit width") {
// BIT_COUNT(-1) should return the number of bits in the type, not always 64
checkEvaluation(BitwiseCount(Literal((-1).toByte)), 8)
checkEvaluation(BitwiseCount(Literal((-1).toShort)), 16)
checkEvaluation(BitwiseCount(Literal(-1)), 32)
checkEvaluation(BitwiseCount(Literal(-1L)), 64)

// Additional cases with specific negative values
checkEvaluation(BitwiseCount(Literal(Int.MinValue)), 1)
checkEvaluation(BitwiseCount(Literal(-65536)), 16)
checkEvaluation(BitwiseCount(Literal(-256)), 24)
checkEvaluation(BitwiseCount(Literal(Byte.MinValue)), 1)
checkEvaluation(BitwiseCount(Literal((-256).toShort)), 8)
checkEvaluation(BitwiseCount(Literal(Short.MinValue)), 1)
checkEvaluation(BitwiseCount(Literal(Long.MinValue)), 1)

DataTypeTestUtils.integralType.foreach { dt =>
checkConsistencyBetweenInterpretedAndCodegen(BitwiseCount, dt)
}
}

test("BitGet") {
val nullLongLiteral = Literal.create(null, LongType)
val nullIntLiteral = Literal.create(null, IntegerType)
Expand Down