-
Notifications
You must be signed in to change notification settings - Fork 87
#782 Add display type for the spark-cobol writer
#783
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
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c0c831b
#782 Add encoders for numbers having DISPLAY format.
yruslan 29c95bc
#782 Add display encoders to the encoder selector for the spark-cobol…
yruslan 128b79b
Update sbt and Maven dependencies.
yruslan a328804
#782 spark-cobol writer: Add support for sign position and ignore red…
yruslan 41566c4
#782 spark-cobol writer: DISPLAY numbers with sign separate always pu…
yruslan 236b555
#782 spark-cobol writer: DISPLAY numbers: fixed a unit test for unsig…
yruslan 13445ff
#782 spark-cobol writer: DISPLAY numbers: handle nulls properly.
yruslan e4b1633
#782 spark-cobol writer: DISPLAY numbers: fix a nitpick PR comment re…
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
216 changes: 216 additions & 0 deletions
216
cobol-parser/src/main/scala/za/co/absa/cobrix/cobol/parser/encoding/DisplayEncoders.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,216 @@ | ||
| /* | ||
| * 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.cobol.parser.encoding | ||
|
|
||
| import za.co.absa.cobrix.cobol.parser.position.Position | ||
|
|
||
| import java.math.RoundingMode | ||
|
|
||
| object DisplayEncoders { | ||
| def encodeDisplayNumberSignSeparate(number: java.math.BigDecimal, | ||
| signPosition: Option[Position], | ||
| outputSize: Int, | ||
| precision: Int, | ||
| scale: Int, | ||
| scaleFactor: Int, | ||
| explicitDecimalPoint: Boolean): Array[Byte] = { | ||
| val isSigned = signPosition.isDefined | ||
| val lengthAdjustment = if (isSigned) 1 else 0 | ||
| val isSignPositionRight = signPosition.contains(za.co.absa.cobrix.cobol.parser.position.Right) | ||
| val isNegative = number.signum() < 0 | ||
| val bytes = new Array[Byte](outputSize) | ||
|
|
||
| if (number == null || precision < 1 || scale < 0 || outputSize < 1 || (scaleFactor > 0 && scale > 0)) | ||
| return bytes | ||
|
|
||
| val num = if (explicitDecimalPoint) { | ||
| val shift = scaleFactor | ||
|
|
||
| val bigDecimal = if (shift == 0) | ||
| number.abs().setScale(scale, RoundingMode.HALF_EVEN) | ||
| else | ||
| number.abs().movePointLeft(shift).setScale(scale, RoundingMode.HALF_EVEN) | ||
|
|
||
| val bigDecimalValue1 = bigDecimal.toString | ||
|
|
||
| val bigDecimalValue = if (bigDecimalValue1.startsWith("0.")) | ||
| bigDecimalValue1.drop(1) | ||
| else | ||
| bigDecimalValue1 | ||
|
|
||
| val bigDecimalValueLen = bigDecimalValue.length + lengthAdjustment | ||
|
|
||
| if (bigDecimalValueLen > outputSize || (!isSigned && isNegative)) | ||
| return bytes | ||
|
|
||
| bigDecimalValue | ||
| } else { | ||
| val shift = scaleFactor - scale | ||
|
|
||
| val bigInt = if (shift == 0) | ||
| number.abs().setScale(0, RoundingMode.HALF_EVEN).toBigIntegerExact | ||
| else | ||
| number.abs().movePointLeft(shift).setScale(0, RoundingMode.HALF_EVEN).toBigIntegerExact | ||
|
|
||
| val intValue = bigInt.toString | ||
| val intValueLen = intValue.length + lengthAdjustment | ||
|
|
||
| if (intValueLen > outputSize || (!isSigned && isNegative)) | ||
| return bytes | ||
|
|
||
| intValue | ||
| } | ||
| setPaddedEbcdicNumberWithSignSeparate(num, isSigned, isNegative, isSignPositionRight, bytes) | ||
| bytes | ||
| } | ||
|
|
||
| def encodeDisplayNumberSignOverpunched(number: java.math.BigDecimal, | ||
| signPosition: Option[Position], | ||
| outputSize: Int, | ||
| precision: Int, | ||
| scale: Int, | ||
| scaleFactor: Int, | ||
| explicitDecimalPoint: Boolean): Array[Byte] = { | ||
| val isSigned = signPosition.isDefined | ||
| val isNegative = number.signum() < 0 | ||
| val bytes = new Array[Byte](outputSize) | ||
|
|
||
yruslan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (number == null || precision < 1 || scale < 0 || outputSize < 1 || (scaleFactor > 0 && scale > 0)) | ||
| return bytes | ||
|
|
||
| val num = if (explicitDecimalPoint) { | ||
| val shift = scaleFactor | ||
|
|
||
| val bigDecimal = if (shift == 0) | ||
| number.abs().setScale(scale, RoundingMode.HALF_EVEN) | ||
| else | ||
| number.abs().movePointLeft(shift).setScale(scale, RoundingMode.HALF_EVEN) | ||
|
|
||
| val bigDecimalValue1 = bigDecimal.toString | ||
|
|
||
| val bigDecimalValue = if (bigDecimalValue1.startsWith("0.")) | ||
| bigDecimalValue1.drop(1) | ||
| else | ||
| bigDecimalValue1 | ||
|
|
||
| val bigDecimalValueLen = bigDecimalValue.length | ||
|
|
||
| if (bigDecimalValueLen > outputSize || (!isSigned && isNegative)) | ||
| return bytes | ||
|
|
||
| bigDecimalValue | ||
| } else { | ||
| val shift = scaleFactor - scale | ||
|
|
||
| val bigInt = if (shift == 0) | ||
| number.abs().setScale(0, RoundingMode.HALF_EVEN).toBigIntegerExact | ||
| else | ||
| number.abs().movePointLeft(shift).setScale(0, RoundingMode.HALF_EVEN).toBigIntegerExact | ||
|
|
||
| val intValue = bigInt.toString | ||
| val intValueLen = intValue.length | ||
|
|
||
| if (intValueLen > outputSize || (!isSigned && isNegative)) | ||
| return bytes | ||
|
|
||
| intValue | ||
| } | ||
| setPaddedEbcdicNumberWithSignOverpunched(num, isSigned, isNegative, bytes) | ||
| bytes | ||
| } | ||
|
|
||
| def setPaddedEbcdicNumberWithSignOverpunched(num: String, isSigned: Boolean, isNegative: Boolean, array: Array[Byte]): Unit = { | ||
| val numLen = num.length | ||
| val arLen = array.length | ||
|
|
||
| if (numLen > arLen) | ||
| return | ||
|
|
||
| var i = 0 | ||
| while (i < arLen) { | ||
| var ebcdic = 0x40.toByte | ||
|
|
||
| if (i == 0) { | ||
| // Signal overpunching | ||
| val c = num(numLen - i - 1) | ||
| if (c >= '0' && c <= '9') { | ||
| val digit = c - '0' | ||
| val zone = if (!isSigned) { | ||
| 0xF | ||
| } else if (isNegative) { | ||
| 0xD | ||
| } else { | ||
| 0xC | ||
| } | ||
|
|
||
| ebcdic = ((zone << 4) | digit).toByte | ||
| } | ||
| } else if (i < numLen) { | ||
| val c = num(numLen - i - 1) | ||
| if (c >= '0' && c <= '9') { | ||
| ebcdic = ((c - '0') + 0xF0).toByte | ||
| } else if (c == '.' || c == ',') { | ||
| ebcdic = 0x4B | ||
| } | ||
yruslan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| array(arLen - i - 1) = ebcdic | ||
| i += 1 | ||
| } | ||
| } | ||
|
|
||
| def setPaddedEbcdicNumberWithSignSeparate(absNum: String, isSigned: Boolean, isNegative: Boolean, isSignPositionRight: Boolean, array: Array[Byte]): Unit = { | ||
| val num = if (isSigned) { | ||
| if (isNegative) { | ||
| if (isSignPositionRight) { | ||
| s"$absNum-" | ||
| } else { | ||
| s"-$absNum" | ||
| } | ||
| } else { | ||
| absNum | ||
| } | ||
| } else { | ||
| absNum | ||
| } | ||
| val numLen = num.length | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| val arLen = array.length | ||
|
|
||
| if (numLen > arLen) | ||
| return | ||
|
|
||
| var i = 0 | ||
| while (i < arLen) { | ||
| var ebcdic = 0x40.toByte | ||
|
|
||
| if (i < numLen) { | ||
| val c = num(numLen - i - 1) | ||
| if (c >= '0' && c <= '9') { | ||
| ebcdic = ((c - '0') + 0xF0).toByte | ||
| } else if (c == '.' || c == ',') { | ||
| ebcdic = 0x4B | ||
| } else if (c == '-') { | ||
| ebcdic = 0x60 | ||
| } | ||
| } | ||
|
|
||
| array(arLen - i - 1) = ebcdic | ||
| i += 1 | ||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.