@@ -1261,6 +1261,41 @@ case class Pow(left: Expression, right: Expression)
1261
1261
newLeft : Expression , newRight : Expression ): Expression = copy(left = newLeft, right = newRight)
1262
1262
}
1263
1263
1264
+ sealed trait BitShiftOperation
1265
+ extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant {
1266
+
1267
+ def symbol : String
1268
+ def shiftInt : (Int , Int ) => Int
1269
+ def shiftLong : (Long , Int ) => Long
1270
+
1271
+ override def inputTypes : Seq [AbstractDataType ] =
1272
+ Seq (TypeCollection (IntegerType , LongType ), IntegerType )
1273
+
1274
+ override def dataType : DataType = left.dataType
1275
+
1276
+ override protected def doGenCode (ctx : CodegenContext , ev : ExprCode ): ExprCode = {
1277
+ defineCodeGen(ctx, ev, (left, right) => s " $left $symbol $right" )
1278
+ }
1279
+
1280
+ override protected def nullSafeEval (input1 : Any , input2 : Any ): Any = input1 match {
1281
+ case l : jl.Long => shiftLong(l, input2.asInstanceOf [Int ])
1282
+ case i : jl.Integer => shiftInt(i, input2.asInstanceOf [Int ])
1283
+ }
1284
+
1285
+ override def toString : String = {
1286
+ getTagValue(FunctionRegistry .FUNC_ALIAS ).getOrElse(symbol) match {
1287
+ case alias if alias == symbol => s " ( $left $symbol $right) "
1288
+ case _ => super .toString
1289
+ }
1290
+ }
1291
+
1292
+ override def sql : String = {
1293
+ getTagValue(FunctionRegistry .FUNC_ALIAS ).getOrElse(symbol) match {
1294
+ case alias if alias == symbol => s " ( ${left.sql} $symbol ${right.sql}) "
1295
+ case _ => super .sql
1296
+ }
1297
+ }
1298
+ }
1264
1299
1265
1300
/**
1266
1301
* Bitwise left shift.
@@ -1269,111 +1304,80 @@ case class Pow(left: Expression, right: Expression)
1269
1304
* @param right number of bits to left shift.
1270
1305
*/
1271
1306
@ ExpressionDescription (
1272
- usage = " _FUNC_( base, expr) - Bitwise left shift." ,
1307
+ usage = " base << exp - Bitwise left shift." ,
1273
1308
examples = """
1274
1309
Examples:
1275
- > SELECT _FUNC_(2, 1);
1310
+ > SELECT shiftleft(2, 1);
1311
+ 4
1312
+ > SELECT 2 << 1;
1276
1313
4
1277
1314
""" ,
1315
+ note = """
1316
+ `<<` operator is added in Spark 4.0.0 as an alias for `shiftleft`.
1317
+ """ ,
1278
1318
since = " 1.5.0" ,
1279
1319
group = " bitwise_funcs" )
1280
- case class ShiftLeft (left : Expression , right : Expression )
1281
- extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant {
1282
-
1283
- override def inputTypes : Seq [AbstractDataType ] =
1284
- Seq (TypeCollection (IntegerType , LongType ), IntegerType )
1285
-
1286
- override def dataType : DataType = left.dataType
1287
-
1288
- protected override def nullSafeEval (input1 : Any , input2 : Any ): Any = {
1289
- input1 match {
1290
- case l : jl.Long => l << input2.asInstanceOf [jl.Integer ]
1291
- case i : jl.Integer => i << input2.asInstanceOf [jl.Integer ]
1292
- }
1293
- }
1294
-
1295
- override protected def doGenCode (ctx : CodegenContext , ev : ExprCode ): ExprCode = {
1296
- defineCodeGen(ctx, ev, (left, right) => s " $left << $right" )
1297
- }
1298
-
1320
+ case class ShiftLeft (left : Expression , right : Expression ) extends BitShiftOperation {
1321
+ override def symbol : String = " <<"
1322
+ override def shiftInt : (Int , Int ) => Int = (x : Int , y : Int ) => x << y
1323
+ override def shiftLong : (Long , Int ) => Long = (x : Long , y : Int ) => x << y
1324
+ val shift : (Number , Int ) => Any = (x : Number , y : Int ) => x.longValue() << y
1299
1325
override protected def withNewChildrenInternal (
1300
1326
newLeft : Expression , newRight : Expression ): ShiftLeft = copy(left = newLeft, right = newRight)
1301
1327
}
1302
1328
1303
-
1304
1329
/**
1305
1330
* Bitwise (signed) right shift.
1306
1331
*
1307
1332
* @param left the base number to shift.
1308
1333
* @param right number of bits to right shift.
1309
1334
*/
1310
1335
@ ExpressionDescription (
1311
- usage = " _FUNC_( base, expr) - Bitwise (signed) right shift." ,
1336
+ usage = " base >> expr - Bitwise (signed) right shift." ,
1312
1337
examples = """
1313
1338
Examples:
1314
- > SELECT _FUNC_(4, 1);
1339
+ > SELECT shiftright(4, 1);
1340
+ 2
1341
+ > SELECT 4 >> 1;
1315
1342
2
1316
1343
""" ,
1344
+ note = """
1345
+ `>>` operator is added in Spark 4.0.0 as an alias for `shiftright`.
1346
+ """ ,
1317
1347
since = " 1.5.0" ,
1318
1348
group = " bitwise_funcs" )
1319
- case class ShiftRight (left : Expression , right : Expression )
1320
- extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant {
1321
-
1322
- override def inputTypes : Seq [AbstractDataType ] =
1323
- Seq (TypeCollection (IntegerType , LongType ), IntegerType )
1324
-
1325
- override def dataType : DataType = left.dataType
1326
-
1327
- protected override def nullSafeEval (input1 : Any , input2 : Any ): Any = {
1328
- input1 match {
1329
- case l : jl.Long => l >> input2.asInstanceOf [jl.Integer ]
1330
- case i : jl.Integer => i >> input2.asInstanceOf [jl.Integer ]
1331
- }
1332
- }
1333
-
1334
- override protected def doGenCode (ctx : CodegenContext , ev : ExprCode ): ExprCode = {
1335
- defineCodeGen(ctx, ev, (left, right) => s " $left >> $right" )
1336
- }
1337
-
1349
+ case class ShiftRight (left : Expression , right : Expression ) extends BitShiftOperation {
1350
+ override def symbol : String = " >>"
1351
+ override def shiftInt : (Int , Int ) => Int = (x : Int , y : Int ) => x >> y
1352
+ override def shiftLong : (Long , Int ) => Long = (x : Long , y : Int ) => x >> y
1338
1353
override protected def withNewChildrenInternal (
1339
1354
newLeft : Expression , newRight : Expression ): ShiftRight = copy(left = newLeft, right = newRight)
1340
1355
}
1341
1356
1342
-
1343
1357
/**
1344
1358
* Bitwise unsigned right shift, for integer and long data type.
1345
1359
*
1346
1360
* @param left the base number.
1347
1361
* @param right the number of bits to right shift.
1348
1362
*/
1349
1363
@ ExpressionDescription (
1350
- usage = " _FUNC_( base, expr) - Bitwise unsigned right shift." ,
1364
+ usage = " base >>> expr - Bitwise unsigned right shift." ,
1351
1365
examples = """
1352
1366
Examples:
1353
- > SELECT _FUNC_ (4, 1);
1367
+ > SELECT shiftrightunsigned (4, 1);
1354
1368
2
1369
+ > SELECT 4 >>> 1;
1370
+ 2
1371
+ """ ,
1372
+ note = """
1373
+ `>>>` operator is added in Spark 4.0.0 as an alias for `shiftrightunsigned`.
1355
1374
""" ,
1356
1375
since = " 1.5.0" ,
1357
1376
group = " bitwise_funcs" )
1358
- case class ShiftRightUnsigned (left : Expression , right : Expression )
1359
- extends BinaryExpression with ImplicitCastInputTypes with NullIntolerant {
1360
-
1361
- override def inputTypes : Seq [AbstractDataType ] =
1362
- Seq (TypeCollection (IntegerType , LongType ), IntegerType )
1363
-
1364
- override def dataType : DataType = left.dataType
1365
-
1366
- protected override def nullSafeEval (input1 : Any , input2 : Any ): Any = {
1367
- input1 match {
1368
- case l : jl.Long => l >>> input2.asInstanceOf [jl.Integer ]
1369
- case i : jl.Integer => i >>> input2.asInstanceOf [jl.Integer ]
1370
- }
1371
- }
1372
-
1373
- override protected def doGenCode (ctx : CodegenContext , ev : ExprCode ): ExprCode = {
1374
- defineCodeGen(ctx, ev, (left, right) => s " $left >>> $right" )
1375
- }
1376
-
1377
+ case class ShiftRightUnsigned (left : Expression , right : Expression ) extends BitShiftOperation {
1378
+ override def symbol : String = " >>>"
1379
+ override def shiftInt : (Int , Int ) => Int = (x : Int , y : Int ) => x >>> y
1380
+ override def shiftLong : (Long , Int ) => Long = (x : Long , y : Int ) => x >>> y
1377
1381
override protected def withNewChildrenInternal (
1378
1382
newLeft : Expression , newRight : Expression ): ShiftRightUnsigned =
1379
1383
copy(left = newLeft, right = newRight)
0 commit comments