Skip to content

Commit 39013cd

Browse files
committed
#826 Fix PR suggestions regarding some edge use cases.
1 parent 84bcc54 commit 39013cd

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

spark-cobol/src/main/scala/za/co/absa/cobrix/spark/cobol/writer/BasicRecordCombiner.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,18 @@ class BasicRecordCombiner extends RecordCombiner {
7979
val ar = new Array[Byte](size)
8080

8181
if (hasRdw) {
82-
val recordLength = cobolSchema.getRecordSize + adjustment1 + adjustment2
82+
val recordLengthLong = cobolSchema.getRecordSize.toLong + adjustment1.toLong + adjustment2.toLong
83+
if (recordLengthLong < 0) {
84+
throw new IllegalArgumentException(
85+
s"Invalid RDW length $recordLengthLong. Check 'is_rdw_part_of_record_length' and 'rdw_adjustment'."
86+
)
87+
}
88+
if (isRdwBigEndian && recordLengthLong > 0xFFFFL) {
89+
throw new IllegalArgumentException(
90+
s"RDW length $recordLengthLong exceeds 65535 and cannot be encoded in big-endian mode."
91+
)
92+
}
93+
val recordLength = recordLengthLong.toInt
8394
if (isRdwBigEndian) {
8495
ar(0) = ((recordLength >> 8) & 0xFF).toByte
8596
ar(1) = (recordLength & 0xFF).toByte

spark-cobol/src/test/scala/za/co/absa/cobrix/spark/cobol/writer/VariableLengthEbcdicWriterSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class VariableLengthEbcdicWriterSuite extends AnyWordSpec with SparkTestBase wit
8282
}
8383
}
8484

85-
"write simple variable -record-length EBCDIC data files with little-endian RDWs and RDW adjustment" in {
85+
"write simple variable -record-length EBCDIC data files with little-endian RDWs and RDW being part of record length" in {
8686
withTempDirectory("cobol_writer1") { tempDir =>
8787
val df = List(("A", "First"), ("B", "Scnd"), ("C", "Last")).toDF("A", "B")
8888

0 commit comments

Comments
 (0)