beliefer commented on a change in pull request #31170:
URL: https://github.com/apache/spark/pull/31170#discussion_r557012703
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/bitwiseExpressions.scala
##########
@@ -204,3 +206,51 @@ case class BitwiseCount(child: Expression)
case LongType => java.lang.Long.bitCount(input.asInstanceOf[Long])
}
}
+
+object BitwiseGet {
+ def calculate(target: Int, pos: Int): Int = {
+ val source = target.toBinaryString
+ if (pos < 0) {
+ throw new IllegalArgumentException(s"Invalid bit position: $pos less
than zero")
+ } else if (source.length <= pos) {
+ throw new IllegalArgumentException(s"Invalid bit position: $pos exceeds
the " +
+ "bit length of the target integer")
+ }
+ val subString =
UTF8String.fromString(source).substringSQL(pos.asInstanceOf[Int], 1)
+ val intWrapper = new UTF8String.IntWrapper()
+ subString.toInt(intWrapper)
+ intWrapper.value
+ }
+}
+
+@ExpressionDescription(
+ usage = "_FUNC_(expr, pos) - Returns the value of the bit (0 or 1) at the
specified position.",
+ examples = """
+ Examples:
+ > SELECT _FUNC_(11, 0);
+ 1
+ > SELECT _FUNC_(11, 2);
+ 0
+ """,
+ since = "3.2.0",
+ group = "bitwise_funcs")
+case class BitwiseGet(left: Expression, right: Expression)
+ extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant {
+
+ override def inputTypes: Seq[IntegerType] = Seq(IntegerType, IntegerType)
Review comment:
Impala give the definition `GETBIT(integer_type a, INT position)`.
snowflake give the description
```
integer_expr
This expression must evaluate to a data type that can be cast to INTEGER.
```
Because `BitwiseGet` uses the ImplicitCastInputTypes, so byte, short, int
could be casted as Integral.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]