Skip to content

Commit a384427

Browse files
authored
chore: wire shiftrightunsigned (apache#4375)
1 parent d6c402a commit a384427

5 files changed

Lines changed: 63 additions & 12 deletions

File tree

docs/source/contributor-guide/spark_expressions_support.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
- [x] bit_get
166166
- [x] getbit
167167
- [x] shiftright
168-
- [ ] shiftrightunsigned
168+
- [x] shiftrightunsigned
169169
- [x] `|`
170170
- [x] `~`
171171

docs/source/user-guide/latest/expressions.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,17 @@ of expressions that be disabled.
216216

217217
## Bitwise Expressions
218218

219-
| Expression | SQL |
220-
| ------------ | ---- |
221-
| BitwiseAnd | `&` |
222-
| BitwiseCount | |
223-
| BitwiseGet | |
224-
| BitwiseOr | `\|` |
225-
| BitwiseNot | `~` |
226-
| BitwiseXor | `^` |
227-
| ShiftLeft | `<<` |
228-
| ShiftRight | `>>` |
219+
| Expression | SQL |
220+
| ------------------ | ----- |
221+
| BitwiseAnd | `&` |
222+
| BitwiseCount | |
223+
| BitwiseGet | |
224+
| BitwiseOr | `\|` |
225+
| BitwiseNot | `~` |
226+
| BitwiseXor | `^` |
227+
| ShiftLeft | `<<` |
228+
| ShiftRight | `>>` |
229+
| ShiftRightUnsigned | `>>>` |
229230

230231
## Aggregate Expressions
231232

native/core/src/execution/jni_api.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use datafusion_comet_proto::spark_operator::Operator;
4545
use datafusion_spark::function::array::array_contains::SparkArrayContains;
4646
use datafusion_spark::function::bitwise::bit_count::SparkBitCount;
4747
use datafusion_spark::function::bitwise::bit_get::SparkBitGet;
48+
use datafusion_spark::function::bitwise::bit_shift::SparkBitShift;
4849
use datafusion_spark::function::bitwise::bitwise_not::SparkBitwiseNot;
4950
use datafusion_spark::function::datetime::date_add::SparkDateAdd;
5051
use datafusion_spark::function::datetime::date_sub::SparkDateSub;
@@ -607,6 +608,7 @@ fn register_datafusion_spark_function(session_ctx: &SessionContext) {
607608
session_ctx.register_udf(ScalarUDF::new_from_impl(SparkFactorial::default()));
608609
session_ctx.register_udf(ScalarUDF::new_from_impl(SparkSec::default()));
609610
session_ctx.register_udf(ScalarUDF::new_from_impl(SparkRint::default()));
611+
session_ctx.register_udf(ScalarUDF::new_from_impl(SparkBitShift::right_unsigned()));
610612
}
611613

612614
/// Prepares arrow arrays for output.

spark/src/main/scala/org/apache/comet/serde/QueryPlanSerde.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ object QueryPlanSerde extends Logging with CometExprShim with CometTypeShim {
215215
classOf[BitwiseNot] -> CometBitwiseNot,
216216
classOf[BitwiseXor] -> CometBitwiseXor,
217217
classOf[ShiftLeft] -> CometShiftLeft,
218-
classOf[ShiftRight] -> CometShiftRight)
218+
classOf[ShiftRight] -> CometShiftRight,
219+
classOf[ShiftRightUnsigned] -> CometScalarFunction("shiftrightunsigned"))
219220

220221
private[comet] val temporalExpressions: Map[Class[_ <: Expression], CometExpressionSerde[_]] =
221222
Map(

spark/src/test/resources/sql-tests/expressions/bitwise/bitwise.sql

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,50 @@ SELECT shiftright(col1, 2), shiftright(col1, col2) FROM test
4444
query
4545
SELECT shiftleft(col1, 2), shiftleft(col1, col2) FROM test
4646

47+
-- ShiftRightUnsigned: first arg is Int or Long, second is Int. Returns the
48+
-- same integer type as the first argument. Shift amount is normalized to the
49+
-- bit width (Java semantics) for negative and large shifts.
50+
statement
51+
CREATE TABLE test_shiftrightunsigned_int(v int, s int) USING parquet
52+
53+
statement
54+
INSERT INTO test_shiftrightunsigned_int VALUES
55+
(1, 1),
56+
(-1, 1),
57+
(8, 2),
58+
(2147483647, 1),
59+
(-2147483648, 1),
60+
(0, 0),
61+
(1, 0),
62+
(1, 31),
63+
(1, 32),
64+
(1, 33),
65+
(1, -1),
66+
(NULL, 1),
67+
(1, NULL)
68+
69+
query
70+
SELECT shiftrightunsigned(v, s) FROM test_shiftrightunsigned_int
71+
72+
statement
73+
CREATE TABLE test_shiftrightunsigned_long(v bigint, s int) USING parquet
74+
75+
statement
76+
INSERT INTO test_shiftrightunsigned_long VALUES
77+
(1, 1),
78+
(-1, 1),
79+
(9223372036854775807, 1),
80+
(-9223372036854775808, 1),
81+
(0, 0),
82+
(1, 63),
83+
(1, 64),
84+
(1, -1),
85+
(NULL, 1),
86+
(1, NULL)
87+
88+
query
89+
SELECT shiftrightunsigned(v, s) FROM test_shiftrightunsigned_long
90+
4791
query
4892
SELECT ~(11), ~col1, ~col2 FROM test
4993

@@ -79,3 +123,6 @@ SELECT bit_get(11, 0), bit_get(11, 1), bit_get(11, 2), bit_get(11, 3)
79123

80124
query
81125
SELECT shiftright(1111, 2), shiftleft(1111, 2)
126+
127+
query
128+
SELECT shiftrightunsigned(1, 1), shiftrightunsigned(-1, 1), shiftrightunsigned(2147483647, 1), shiftrightunsigned(cast(-1 as bigint), 1)

0 commit comments

Comments
 (0)