Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-48596][SQL] Perf improvement for calculating hex string for long #46952

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -1018,9 +1018,9 @@ case class Bin(child: Expression)
}

object Hex {
val hexDigits = Array[Char](
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
).map(_.toByte)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

private final val hexDigits =
Array[Byte]('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
private final val ZERO_UTF8 = UTF8String.fromBytes(Array[Byte]('0'))

// lookup table to translate '0' -> 0 ... 'F'/'f' -> 15
val unhexDigits = {
Expand All @@ -1036,24 +1036,26 @@ object Hex {
val value = new Array[Byte](length * 2)
var i = 0
while (i < length) {
value(i * 2) = Hex.hexDigits((bytes(i) & 0xF0) >> 4)
value(i * 2 + 1) = Hex.hexDigits(bytes(i) & 0x0F)
value(i * 2) = hexDigits((bytes(i) & 0xF0) >> 4)
value(i * 2 + 1) = hexDigits(bytes(i) & 0x0F)
i += 1
}
UTF8String.fromBytes(value)
}

def hex(num: Long): UTF8String = {
// Extract the hex digits of num into value[] from right to left
val value = new Array[Byte](16)
val zeros = jl.Long.numberOfLeadingZeros(num)
if (zeros == jl.Long.SIZE) return ZERO_UTF8
val len = (jl.Long.SIZE - zeros + 3) / 4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add comments to explain the arithmetic expression ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the necessity for commenting on such a pretty common expression

var numBuf = num
var len = 0
do {
len += 1
value(value.length - len) = Hex.hexDigits((numBuf & 0xF).toInt)
val value = new Array[Byte](len)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value -> bytes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytes might be good but the naming is currently consistent with another variant.

var i = len - 1
while (i >= 0) {
value(i) = hexDigits((numBuf & 0xF).asInstanceOf[Int])
numBuf >>>= 4
} while (numBuf != 0)
UTF8String.fromBytes(java.util.Arrays.copyOfRange(value, value.length - len, value.length))
i -= 1
}
UTF8String.fromBytes(value)
}

def unhex(bytes: Array[Byte]): Array[Byte] = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql.catalyst.expressions

import org.apache.spark.SparkFunSuite

class HexSuite extends SparkFunSuite {
test("SPARK-48596: hex long values") {
assert(Hex.hex(0).toString === "0")
assert(Hex.hex(1).toString === "1")
assert(Hex.hex(15).toString === "F")
assert(Hex.hex(16).toString === "10")
assert(Hex.hex(255).toString === "FF")
assert(Hex.hex(256).toString === "100")
assert(Hex.hex(4095).toString === "FFF")
assert(Hex.hex(4096).toString === "1000")
assert(Hex.hex(65535).toString === "FFFF")
assert(Hex.hex(65536).toString === "10000")
assert(Hex.hex(1048575).toString === "FFFFF")
assert(Hex.hex(1048576).toString === "100000")
assert(Hex.hex(-1).toString === "FFFFFFFFFFFFFFFF")
assert(Hex.hex(Long.MinValue).toString === "8000000000000000")
assert(Hex.hex(Long.MaxValue).toString === "7FFFFFFFFFFFFFFF")
}
}