-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#678 Add a method to convert integrals to decimals in schema accordin…
…g to the metadata.
- Loading branch information
Showing
8 changed files
with
322 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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
33 changes: 33 additions & 0 deletions
33
spark-cobol/src/main/scala_2.11/za/co/absa/cobrix/spark/cobol/utils/impl/HofsWrapper.scala
This file contains 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,33 @@ | ||
/* | ||
* 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.utils.impl | ||
|
||
import org.apache.spark.sql.Column | ||
|
||
object HofsWrapper { | ||
/** | ||
* Applies the function `f` to every element in the `array`. The method is an equivalent to the `map` function | ||
* from functional programming. | ||
* | ||
* The method is not available in Scala 2.11 and Spark < 3.0 | ||
*/ | ||
def transform( | ||
array: Column, | ||
f: Column => Column): Column = { | ||
throw new IllegalArgumentException("Array transformation is not available for Scala 2.11 and Spark < 3.0.") | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
spark-cobol/src/main/scala_2.12/za/co/absa/cobrix/spark/cobol/utils/impl/HofsWrapper.scala
This file contains 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,38 @@ | ||
/* | ||
* 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.utils.impl | ||
|
||
import org.apache.spark.sql.Column | ||
import org.apache.spark.sql.functions.{transform => sparkTransform} | ||
|
||
object HofsWrapper { | ||
/** | ||
* Applies the function `f` to every element in the `array`. The method is an equivalent to the `map` function | ||
* from functional programming. | ||
* | ||
* (The idea comes from https://github.com/AbsaOSS/spark-hats/blob/v0.3.0/src/main/scala_2.12/za/co/absa/spark/hats/HofsWrapper.scala) | ||
* | ||
* @param array A column of arrays | ||
* @param f A function transforming individual elements of the array | ||
* @return A column of arrays with transformed elements | ||
*/ | ||
def transform( | ||
array: Column, | ||
f: Column => Column): Column = { | ||
sparkTransform(array, f) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
spark-cobol/src/main/scala_2.13/za/co/absa/cobrix/spark/cobol/utils/impl/HofsWrapper.scala
This file contains 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,38 @@ | ||
/* | ||
* 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.utils.impl | ||
|
||
import org.apache.spark.sql.Column | ||
import org.apache.spark.sql.functions.{transform => sparkTransform} | ||
|
||
object HofsWrapper { | ||
/** | ||
* Applies the function `f` to every element in the `array`. The method is an equivalent to the `map` function | ||
* from functional programming. | ||
* | ||
* (The idea comes from https://github.com/AbsaOSS/spark-hats/blob/v0.3.0/src/main/scala_2.12/za/co/absa/spark/hats/HofsWrapper.scala) | ||
* | ||
* @param array A column of arrays | ||
* @param f A function transforming individual elements of the array | ||
* @return A column of arrays with transformed elements | ||
*/ | ||
def transform( | ||
array: Column, | ||
f: Column => Column): Column = { | ||
sparkTransform(array, f) | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
.../src/test/scala/za/co/absa/cobrix/spark/cobol/source/fixtures/TextComparisonFixture.scala
This file contains 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,85 @@ | ||
/* | ||
* 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.source.fixtures | ||
|
||
import org.scalatest.{Assertion, Suite} | ||
|
||
trait TextComparisonFixture { | ||
this: Suite => | ||
|
||
protected def compareText(actual: String, expected: String): Assertion = { | ||
if (actual.replaceAll("[\r\n]", "") != expected.replaceAll("[\r\n]", "")) { | ||
fail(renderTextDifference(actual, expected)) | ||
} else { | ||
succeed | ||
} | ||
} | ||
|
||
protected def compareTextVertical(actual: String, expected: String): Unit = { | ||
if (actual.replaceAll("[\r\n]", "") != expected.replaceAll("[\r\n]", "")) { | ||
fail(s"ACTUAL:\n$actual\nEXPECTED: \n$expected") | ||
} | ||
} | ||
|
||
protected def renderTextDifference(textActual: String, textExpected: String): String = { | ||
val t1 = textActual.replaceAll("\\r\\n", "\\n").split('\n') | ||
val t2 = textExpected.replaceAll("\\r\\n", "\\n").split('\n') | ||
|
||
val maxLen = Math.max(getMaxStrLen(t1), getMaxStrLen(t2)) | ||
val header = s" ${rightPad("ACTUAL:", maxLen)} ${rightPad("EXPECTED:", maxLen)}\n" | ||
|
||
val stringBuilder = new StringBuilder | ||
stringBuilder.append(header) | ||
|
||
val linesCount = Math.max(t1.length, t2.length) | ||
var i = 0 | ||
while (i < linesCount) { | ||
val a = if (i < t1.length) t1(i) else "" | ||
val b = if (i < t2.length) t2(i) else "" | ||
|
||
val marker1 = if (a != b) ">" else " " | ||
val marker2 = if (a != b) "<" else " " | ||
|
||
val comparisonText = s"$marker1${rightPad(a, maxLen)} ${rightPad(b, maxLen)}$marker2\n" | ||
stringBuilder.append(comparisonText) | ||
|
||
i += 1 | ||
} | ||
|
||
val footer = s"\nACTUAL:\n$textActual" | ||
stringBuilder.append(footer) | ||
stringBuilder.toString() | ||
} | ||
|
||
def getMaxStrLen(text: Seq[String]): Int = { | ||
if (text.isEmpty) { | ||
0 | ||
} else { | ||
text.maxBy(_.length).length | ||
} | ||
} | ||
|
||
def rightPad(s: String, length: Int): String = { | ||
if (s.length < length) { | ||
s + " " * (length - s.length) | ||
} else if (s.length > length) { | ||
s.take(length) | ||
} else { | ||
s | ||
} | ||
} | ||
} |
This file contains 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