-
Notifications
You must be signed in to change notification settings - Fork 0
#19 ColumnImplicits and StructFieldImplicits #21
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9d43738
#19 ColumnImplicits and StructFieldImplicits
AdrianOlosutean 330c1fa
#19 tests and small code fix
AdrianOlosutean 32021cf
#22 headers
AdrianOlosutean 25f1983
Merge remote-tracking branch 'origin/master' into feature/19-add-colu…
AdrianOlosutean 1a5b004
#19 feedback
AdrianOlosutean 2110f30
#19 renames
AdrianOlosutean e58d3aa
#19 other feedback
AdrianOlosutean d9533d2
Merge remote-tracking branch 'origin/master' into feature/19-add-colu…
AdrianOlosutean 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
65 changes: 65 additions & 0 deletions
65
src/main/scala/za/co/absa/spark/commons/implicits/ColumnImplicits.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,65 @@ | ||
| /* | ||
| * Copyright 2021 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.spark.commons.implicits | ||
|
|
||
| import org.apache.spark.sql.Column | ||
| import org.apache.spark.sql.functions._ | ||
|
|
||
| object ColumnImplicits { | ||
| implicit class ColumnEnhancements(column: Column) { | ||
| def isInfinite: Column = { | ||
| column.isin(Double.PositiveInfinity, Double.NegativeInfinity) | ||
| } | ||
|
|
||
| /** | ||
| * Spark strings are based on 1 unlike scala. The function shifts the substring indexation to be in accordance with | ||
| * Scala/ Java. | ||
| * Another enhancement is, that the function allows a negative index, denoting counting of the index from back | ||
| * This version takes the substring from the startPos until the end. | ||
| * | ||
| * @param startPos the index (zero based) where to start the substring from, if negative it's counted from end | ||
| * @return column with requested substring | ||
| */ | ||
| def zeroBasedSubstr(startPos: Int): Column = { | ||
| if (startPos >= 0) { | ||
| zeroBasedSubstr(startPos, Int.MaxValue - startPos) | ||
| } else { | ||
| zeroBasedSubstr(startPos, -startPos) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Spark strings are base on 1 unlike scala. The function shifts the substring indexation to be in accordance with | ||
| * Scala/ Java. | ||
| * Another enhancement is, that the function allows a negative index, denoting counting of the index from back | ||
| * This version takes the substring from the startPos and takes up to the given number of characters (less. | ||
| * | ||
| * @param startPos the index (zero based) where to start the substring from, if negative it's counted from end | ||
| * @param len length of the desired substring, if longer then the rest of the string, all the remaining characters are taken | ||
| * @return column with requested substring | ||
| */ | ||
| def zeroBasedSubstr(startPos: Int, len: Int): Column = { | ||
| if (startPos >= 0) { | ||
| column.substr(startPos + 1, len) | ||
| } else { | ||
| val startPosColumn = greatest(length(column) + startPos + 1, lit(1)) | ||
| val lenColumn = lit(len) + when(length(column) + startPos <= 0, length(column) + startPos).otherwise(0) | ||
| column.substr(startPosColumn, lenColumn) | ||
| } | ||
| } | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
src/main/scala/za/co/absa/spark/commons/implicits/StructFieldImplicits.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,47 @@ | ||
| /* | ||
| * Copyright 2021 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.spark.commons.implicits | ||
|
|
||
| import org.apache.spark.sql.types._ | ||
| import scala.util.Try | ||
|
|
||
| object StructFieldImplicits { | ||
| implicit class StructFieldMetadataEnhancement(val metadata: Metadata) { | ||
| def getOptString(key: String): Option[String] = { | ||
| Try(metadata.getString(key)).toOption | ||
| } | ||
|
|
||
| def getOptChar(key: String): Option[Char] = { | ||
| val resultString = Try(metadata.getString(key)).toOption | ||
| resultString.flatMap { s => | ||
| if (s != null && s.length == 1) { | ||
| Option(s(0)) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| def getOptStringAsBoolean(key: String): Option[Boolean] = { | ||
| Try(metadata.getString(key).toBoolean).toOption | ||
| } | ||
|
|
||
| def hasKey(key: String): Boolean = { | ||
| metadata.contains(key) | ||
| } | ||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
src/test/scala/za/co/absa/spark/commons/implicits/ColumnImplicitsTest.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,45 @@ | ||
| /* | ||
| * Copyright 2021 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.spark.commons.implicits | ||
|
|
||
| import org.apache.spark.sql.Column | ||
| import org.apache.spark.sql.functions.lit | ||
| import org.scalatest.funsuite.AnyFunSuite | ||
| import za.co.absa.spark.commons.implicits.ColumnImplicits.ColumnEnhancements | ||
|
|
||
| class ColumnImplicitsTest extends AnyFunSuite{ | ||
|
|
||
| private val column: Column = lit("abcdefgh") | ||
|
|
||
| test("zeroBasedSubstr with startPos") { | ||
| assertResult("cdefgh")(column.zeroBasedSubstr(2).expr.eval().toString) | ||
| assertResult("gh")(column.zeroBasedSubstr(-2).expr.eval().toString) | ||
| assertResult("")(column.zeroBasedSubstr(Int.MaxValue).expr.eval().toString) | ||
| assertResult("abcdefgh")(column.zeroBasedSubstr(Int.MinValue).expr.eval().toString) | ||
| } | ||
|
|
||
| test("zeroBasedSubstr with startPos and len") { | ||
| assertResult("cde")(column.zeroBasedSubstr(2, 3).expr.eval().toString) | ||
| assertResult("gh")(column.zeroBasedSubstr(-2, 7).expr.eval().toString) | ||
| assertResult("")(column.zeroBasedSubstr(Int.MaxValue, 1).expr.eval().toString) | ||
| assertResult("")(column.zeroBasedSubstr(Int.MaxValue, -3).expr.eval().toString) | ||
Zejnilovic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| assertResult("")(column.zeroBasedSubstr(4, -3).expr.eval().toString) | ||
| assertResult("")(column.zeroBasedSubstr(Int.MinValue,2).expr.eval().toString) | ||
| assertResult("")(column.zeroBasedSubstr(Int.MinValue,-3).expr.eval().toString) | ||
| } | ||
|
|
||
| } | ||
62 changes: 62 additions & 0 deletions
62
src/test/scala/za/co/absa/spark/commons/implicits/StructFieldImplicitsTest.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,62 @@ | ||
| /* | ||
| * Copyright 2021 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.spark.commons.implicits | ||
|
|
||
| import org.apache.spark.sql.types.{Metadata, StringType, StructField} | ||
| import org.scalatest.funsuite.AnyFunSuite | ||
| import za.co.absa.spark.commons.implicits.StructFieldImplicits.StructFieldMetadataEnhancement | ||
|
|
||
| class StructFieldImplicitsTest extends AnyFunSuite { | ||
|
|
||
| def fieldWith(value123: String) = { | ||
| val value1 = s"""{ "a" : ${value123} }""" | ||
| StructField("uu", StringType, true, Metadata.fromJson(value1)) | ||
| } | ||
|
|
||
| test("getOptString") { | ||
| assertResult(Some(""))(fieldWith("\"\"").metadata.getOptString("a")) | ||
| assertResult(None)(fieldWith("123").metadata.getOptString("a")) | ||
| assertResult(Some("ffbfg"))(fieldWith("\"ffbfg\"").metadata.getOptString("a")) | ||
| assertResult(Some(null))(fieldWith("null").metadata.getOptString("a")) | ||
| } | ||
|
|
||
| test("getOptChar") { | ||
| assertResult(None)(fieldWith("\"\"").metadata.getOptChar("a")) | ||
| assertResult(None)(fieldWith("123").metadata.getOptChar("a")) | ||
| assertResult(Some('g'))(fieldWith("\"g\"").metadata.getOptChar("a")) | ||
| assertResult(None)(fieldWith("\"abc\"").metadata.getOptChar("a")) | ||
| assertResult(None)(fieldWith("null").metadata.getOptChar("a")) | ||
| } | ||
|
|
||
| test("getOptStringAsBoolean") { | ||
| assertResult(None)(fieldWith("\"\"").metadata.getOptStringAsBoolean("a")) | ||
| assertResult(None)(fieldWith("123").metadata.getOptStringAsBoolean("a")) | ||
| assertResult(Some(true))(fieldWith("\"true\"").metadata.getOptStringAsBoolean("a")) | ||
| assertResult(Some(false))(fieldWith("\"false\"").metadata.getOptStringAsBoolean("a")) | ||
| assertResult(None)(fieldWith("false").metadata.getOptStringAsBoolean("a")) | ||
| assertResult(None)(fieldWith("true").metadata.getOptStringAsBoolean("a")) | ||
| assertResult(None)(fieldWith("null").metadata.getOptStringAsBoolean("a")) | ||
| } | ||
|
|
||
| test("hasKey") { | ||
| assertResult(true)(fieldWith("\"\"").metadata.hasKey("a")) | ||
| assertResult(false)(fieldWith("123").metadata.hasKey("b")) | ||
| assertResult(true)(fieldWith("\"hvh\"").metadata.hasKey("a")) | ||
| assertResult(true)(fieldWith("null").metadata.hasKey("a")) | ||
| } | ||
|
|
||
| } |
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.
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 a
StructFieldImplicitsnow it isMetadataImplicitsThere 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.
Missed this one #21 (comment)
So 1 object 2 implicit classes?
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.
Yes, since Metadata is related to StructType