diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/VectorizedHashMapGenerator.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/VectorizedHashMapGenerator.scala
index 8a3f466ccfef3207f508f2e7f2f7cb69a2eb9924..b4a90592995399d34fd3e908e609fc70b9f12b9c 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/VectorizedHashMapGenerator.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/VectorizedHashMapGenerator.scala
@@ -313,10 +313,12 @@ class VectorizedHashMapGenerator(
     def hashLong(l: String): String = s"long $result = $l;"
     def hashBytes(b: String): String = {
       val hash = ctx.freshName("hash")
+      val bytes = ctx.freshName("bytes")
       s"""
          |int $result = 0;
-         |for (int i = 0; i < $b.length; i++) {
-         |  ${genComputeHash(ctx, s"$b[i]", ByteType, hash)}
+         |byte[] $bytes = $b;
+         |for (int i = 0; i < $bytes.length; i++) {
+         |  ${genComputeHash(ctx, s"$bytes[i]", ByteType, hash)}
          |  $result = ($result ^ (0x9e3779b9)) + $hash + ($result << 6) + ($result >>> 2);
          |}
        """.stripMargin