Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
address comment
  • Loading branch information
Ngone51 committed Jun 26, 2024
commit 4244864f1b3f8f0bef9039dfdd468b8adeeea1a4
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.Map;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoSerializable;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import org.apache.spark.SparkIllegalArgumentException;
import org.apache.spark.SparkUnsupportedOperationException;
import org.apache.spark.sql.catalyst.InternalRow;
import org.apache.spark.sql.catalyst.types.*;
Expand Down Expand Up @@ -159,9 +161,10 @@ public void pointTo(Object baseObject, long baseOffset, int sizeInBytes) {
int offsetInByteArray = (int) (baseOffset - Platform.BYTE_ARRAY_OFFSET);
if (offsetInByteArray < 0 || sizeInBytes < 0 ||
bytes.length < offsetInByteArray + sizeInBytes) {
throw new ArrayIndexOutOfBoundsException(
"byte array length: " + bytes.length +
", offset: " + offsetInByteArray + ", size: " + sizeInBytes
throw new SparkIllegalArgumentException(
"INTERNAL_ERROR",
Map.of("message", "Invalid byte array backed UnsafeRow: byte array length=" +
bytes.length + ", offset=" + offsetInByteArray + ", byte size=" + sizeInBytes)
);
}
}
Expand Down
25 changes: 17 additions & 8 deletions sql/core/src/test/scala/org/apache/spark/sql/UnsafeRowSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package org.apache.spark.sql

import java.io.ByteArrayOutputStream

import org.apache.spark.{SparkConf, SparkFunSuite}
import org.apache.spark.{SparkConf, SparkFunSuite, SparkIllegalArgumentException}
import org.apache.spark.serializer.{JavaSerializer, KryoSerializer}
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.{UnsafeProjection, UnsafeRow}
Expand Down Expand Up @@ -189,29 +189,38 @@ class UnsafeRowSuite extends SparkFunSuite {
assert(unsafeRow.getDecimal(0, 38, 18) === null)
}

test("SPARK-48713: throw ArrayIndexOutOfBoundsException for illegal UnsafeRow.pointTo") {
test("SPARK-48713: throw SparkIllegalArgumentException for illegal UnsafeRow.pointTo") {
val emptyRow = UnsafeRow.createFromByteArray(64, 2)
val byteArray = new Array[Byte](64)

// Out of bounds
var errorMsg = intercept[ArrayIndexOutOfBoundsException] {
var errorMsg = intercept[SparkIllegalArgumentException] {
emptyRow.pointTo(byteArray, Platform.BYTE_ARRAY_OFFSET + 50, 32)
}.getMessage
assert(errorMsg.contains("byte array length: 64, offset: 50, size: 32"))
assert(
errorMsg.contains(
"Invalid byte array backed UnsafeRow: byte array length=64, offset=50, byte size=32"
)
)

// Negative size
errorMsg = intercept[ArrayIndexOutOfBoundsException] {
errorMsg = intercept[SparkIllegalArgumentException] {
emptyRow.pointTo(byteArray, Platform.BYTE_ARRAY_OFFSET + 50, -32)
}.getMessage
assert(errorMsg.contains("byte array length: 64, offset: 50, size: -32"))
assert(
errorMsg.contains(
"Invalid byte array backed UnsafeRow: byte array length=64, offset=50, byte size=-32"
)
)

// Negative offset
errorMsg = intercept[ArrayIndexOutOfBoundsException] {
errorMsg = intercept[SparkIllegalArgumentException] {
emptyRow.pointTo(byteArray, -5, 32)
}.getMessage
assert(
errorMsg.contains(
s"byte array length: 64, offset: ${-5 - Platform.BYTE_ARRAY_OFFSET}, size: 32"
s"Invalid byte array backed UnsafeRow: byte array length=64, " +
s"offset=${-5 - Platform.BYTE_ARRAY_OFFSET}, byte size=32"
)
)
}
Expand Down