-
Notifications
You must be signed in to change notification settings - Fork 87
#826 Add support for writing VRL files with RDW headers #827
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
Merged
+292
−20
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
767e5ff
#626 Add support for writing VLR files with RDW headers.
yruslan 84bcc54
#826 Add support ofssets when setting primitive values for a COBOL re…
yruslan 39013cd
#826 Fix PR suggestions regarding some edge use cases.
yruslan 493c763
#826 Move record length out RDDs for fixed-record-length files.
yruslan 5de36fa
#826 Add a very rare edge case when a record created is bigger than 2…
yruslan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
...src/test/scala/za/co/absa/cobrix/spark/cobol/writer/VariableLengthEbcdicWriterSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| /* | ||
| * Copyright 2018 ABSA Group Limited | ||
| * | ||
| * Licensed 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 za.co.absa.cobrix.spark.cobol.writer | ||
|
|
||
| import org.apache.hadoop.fs.Path | ||
| import org.apache.spark.sql.SaveMode | ||
| import org.scalatest.Assertion | ||
| import org.scalatest.wordspec.AnyWordSpec | ||
| import za.co.absa.cobrix.spark.cobol.source.base.SparkTestBase | ||
| import za.co.absa.cobrix.spark.cobol.source.fixtures.{BinaryFileFixture, TextComparisonFixture} | ||
|
|
||
| class VariableLengthEbcdicWriterSuite extends AnyWordSpec with SparkTestBase with BinaryFileFixture with TextComparisonFixture { | ||
|
|
||
| import spark.implicits._ | ||
|
|
||
| private val copybookContents = | ||
| """ 01 RECORD. | ||
| 05 A PIC X(1). | ||
| 05 B PIC X(5). | ||
| """ | ||
|
|
||
| "cobol writer" should { | ||
| "write simple variable -record-length EBCDIC data files with big-endian RDWs" in { | ||
| withTempDirectory("cobol_writer1") { tempDir => | ||
| val df = List(("A", "First"), ("B", "Scnd"), ("C", "Last")).toDF("A", "B") | ||
|
|
||
| val path = new Path(tempDir, "writer1") | ||
|
|
||
| df.coalesce(1) | ||
| .orderBy("A") | ||
| .write | ||
| .format("cobol") | ||
| .mode(SaveMode.Overwrite) | ||
| .option("copybook_contents", copybookContents) | ||
| .option("record_format", "V") | ||
| .option("is_rdw_big_endian", "true") | ||
| .save(path.toString) | ||
|
|
||
| val fs = path.getFileSystem(spark.sparkContext.hadoopConfiguration) | ||
|
|
||
| assert(fs.exists(path), "Output directory should exist") | ||
| val files = fs.listStatus(path) | ||
| .filter(_.getPath.getName.startsWith("part-")) | ||
| assert(files.nonEmpty, "Output directory should contain part files") | ||
|
|
||
| val partFile = files.head.getPath | ||
| val data = fs.open(partFile) | ||
| val bytes = new Array[Byte](files.head.getLen.toInt) | ||
| data.readFully(bytes) | ||
| data.close() | ||
|
|
||
| // Expected EBCDIC data for sample test data | ||
| val expected = Array[Byte]( | ||
| 0x00.toByte, 0x06.toByte, 0x00.toByte, 0x00.toByte, // RDW1 | ||
| 0xC1.toByte, 0xC6.toByte, 0x89.toByte, 0x99.toByte, 0xa2.toByte, 0xa3.toByte, // A,First | ||
| 0x00.toByte, 0x06.toByte, 0x00.toByte, 0x00.toByte, // RDW2 | ||
| 0xC2.toByte, 0xE2.toByte, 0x83.toByte, 0x95.toByte, 0x84.toByte, 0x40.toByte, // B,Scnd_ | ||
| 0x00.toByte, 0x06.toByte, 0x00.toByte, 0x00.toByte, // RDW3 | ||
| 0xC3.toByte, 0xD3.toByte, 0x81.toByte, 0xa2.toByte, 0xa3.toByte, 0x40.toByte // C,Last_ | ||
| ) | ||
|
|
||
| if (!bytes.sameElements(expected)) { | ||
| println(s"Expected bytes: ${expected.map("%02X" format _).mkString(" ")}") | ||
| println(s"Actual bytes: ${bytes.map("%02X" format _).mkString(" ")}") | ||
|
|
||
| assert(bytes.sameElements(expected), "Written data should match expected EBCDIC encoding") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| "write simple variable -record-length EBCDIC data files with little-endian RDWs and RDW being part of record length" in { | ||
| withTempDirectory("cobol_writer1") { tempDir => | ||
| val df = List(("A", "First"), ("B", "Scnd"), ("C", "Last")).toDF("A", "B") | ||
|
|
||
| val path = new Path(tempDir, "writer1") | ||
|
|
||
| df.coalesce(1) | ||
| .orderBy("A") | ||
| .write | ||
| .format("cobol") | ||
| .mode(SaveMode.Overwrite) | ||
| .option("copybook_contents", copybookContents) | ||
| .option("record_format", "V") | ||
| .option("is_rdw_big_endian", "false") | ||
| .option("is_rdw_part_of_record_length", "true") | ||
| .save(path.toString) | ||
|
|
||
| val fs = path.getFileSystem(spark.sparkContext.hadoopConfiguration) | ||
|
|
||
| assert(fs.exists(path), "Output directory should exist") | ||
| val files = fs.listStatus(path) | ||
| .filter(_.getPath.getName.startsWith("part-")) | ||
| assert(files.nonEmpty, "Output directory should contain part files") | ||
|
|
||
| val partFile = files.head.getPath | ||
| val data = fs.open(partFile) | ||
| val bytes = new Array[Byte](files.head.getLen.toInt) | ||
| data.readFully(bytes) | ||
| data.close() | ||
|
|
||
| // Expected EBCDIC data for sample test data | ||
| val expected = Array[Byte]( | ||
| 0x0A.toByte, 0x00.toByte, 0x00.toByte, 0x00.toByte, // RDW1 | ||
| 0xC1.toByte, 0xC6.toByte, 0x89.toByte, 0x99.toByte, 0xa2.toByte, 0xa3.toByte, // A,First | ||
| 0x0A.toByte, 0x00.toByte, 0x00.toByte, 0x00.toByte, // RDW2 | ||
| 0xC2.toByte, 0xE2.toByte, 0x83.toByte, 0x95.toByte, 0x84.toByte, 0x40.toByte, // B,Scnd_ | ||
| 0x0A.toByte, 0x00.toByte, 0x00.toByte, 0x00.toByte, // RDW3 | ||
| 0xC3.toByte, 0xD3.toByte, 0x81.toByte, 0xa2.toByte, 0xa3.toByte, 0x40.toByte // C,Last_ | ||
| ) | ||
|
|
||
| if (!bytes.sameElements(expected)) { | ||
| println(s"Expected bytes: ${expected.map("%02X" format _).mkString(" ")}") | ||
| println(s"Actual bytes: ${bytes.map("%02X" format _).mkString(" ")}") | ||
|
|
||
| assert(bytes.sameElements(expected), "Written data should match expected EBCDIC encoding") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| "throw an exception on unexpected output record format" in { | ||
| withTempDirectory("cobol_writer2") { tempDir => | ||
| val df = List(("A", "First"), ("B", "Scnd"), ("C", "Last")).toDF("A", "B") | ||
|
|
||
| val path = new Path(tempDir, "writer2") | ||
|
|
||
| val exception = intercept[IllegalArgumentException] { | ||
| df.coalesce(1) | ||
| .orderBy("A") | ||
| .write | ||
| .format("cobol") | ||
| .mode(SaveMode.Overwrite) | ||
| .option("copybook_contents", copybookContents) | ||
| .option("record_format", "FB") // Not supported | ||
| .option("variable_size_occurs", "true") // Not supported | ||
| .option("file_start_offset", "2") // Not supported | ||
| .option("record_end_offset", "4") // Not supported | ||
| .save(path.toString) | ||
| } | ||
|
|
||
| assert(exception.getMessage.contains("Writer validation issues: Only 'F' and 'V' values for 'record_format' are supported for writing, provided value: 'FB';")) | ||
| assert(exception.getMessage.contains("Variable size OCCURS ('variable_size_occurs = true') is not supported for writing")) | ||
| assert(exception.getMessage.contains("'record_start_offset' and 'record_end_offset' are not supported for writing")) | ||
| assert(exception.getMessage.contains("'file_start_offset' and 'file_end_offset' are not supported for writing")) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| def assertArraysEqual(actual: Array[Byte], expected: Array[Byte]): Assertion = { | ||
| if (!actual.sameElements(expected)) { | ||
| val actualHex = actual.map(b => f"0x$b%02X").mkString(", ") | ||
| val expectedHex = expected.map(b => f"0x$b%02X").mkString(", ") | ||
| fail(s"Actual: $actualHex\nExpected: $expectedHex") | ||
| } else { | ||
| succeed | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: AbsaOSS/cobrix
Length of output: 151
🏁 Script executed:
Repository: AbsaOSS/cobrix
Length of output: 13006
🏁 Script executed:
Repository: AbsaOSS/cobrix
Length of output: 13042
🏁 Script executed:
Repository: AbsaOSS/cobrix
Length of output: 1375
🏁 Script executed:
Repository: AbsaOSS/cobrix
Length of output: 681
🏁 Script executed:
Repository: AbsaOSS/cobrix
Length of output: 372
Add
VariableBlockto the RDW check.Line 65 should also check for
RecordFormat.VariableBlocksince VB files use RDW headers for individual records within blocks, similar to V format files. Change:to:
Without this, VariableBlock files will be written without proper RDW headers, causing downstream reading failures.
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not applicable here. The coborl writer is not going to support variable-block headers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yruslan, thank you for the clarification! I understand that variable-block format support is intentionally not included in the writer implementation. I've noted this design decision for future reference.✏️ Learnings added
🧠 Learnings used