Skip to content

Commit

Permalink
Changed non-idiomatic expressions in scala
Browse files Browse the repository at this point in the history
  • Loading branch information
jjpulidos committed Nov 17, 2023
1 parent 39c12c6 commit 3674cf0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ You cannot append the following DataFrame which contains the required columns, b
+----+----+----+
```

Here's the error you'll get when you attempt this write: "The column col5 is not part of the current Delta table. If you want to add the column to the table, you must set the optionalCols parameter"
Here's the error you'll get when you attempt this write: "The following columns are not part of the current Delta table. If you want to add these columns to the table, you must set the optionalCols parameter: List(col5)"

You also cannot append the following DataFrame which is missing one of the required columns.

Expand Down
30 changes: 5 additions & 25 deletions src/main/scala/mrpowers/jodie/DeltaHelpers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -357,37 +357,17 @@ object DeltaHelpers {
requiredCols: List[String],
optionalCols: List[String]
): Unit = {
// Check if deltaTable is an instance of DeltaTable
if (!deltaTable.isInstanceOf[DeltaTable]) {
throw new IllegalArgumentException("An existing delta table must be specified.")
}

// Check if appendDF is an instance of DataFrame
if (!appendDF.isInstanceOf[DataFrame]) {
throw new IllegalArgumentException("You must provide a DataFrame that is to be appended.")
}

val appendDataColumns = appendDF.columns
val tableColumns = deltaTable.toDF.columns

// Check if all required columns are present in appendDF
for (requiredColumn <- requiredCols) {
if (!appendDataColumns.contains(requiredColumn)) {
throw new IllegalArgumentException(
s"The base Delta table has these columns ${appendDataColumns.mkString("List(", ", ", ")")}, but these columns are required $requiredCols"
)
}
}

val tableColumns = deltaTable.toDF.columns
val missingColumns = requiredCols.filterNot(appendDataColumns.contains)
require(missingColumns.isEmpty, s"The base Delta table has these columns ${appendDataColumns.mkString("List(", ", ", ")")}, but these columns are required $requiredCols")

// Check if all columns in appendDF are part of the current Delta table or optional
for (column <- appendDataColumns) {
if (!tableColumns.contains(column) && !optionalCols.contains(column)) {
throw new IllegalArgumentException(
s"The column $column is not part of the current Delta table. If you want to add the column to the table, you must set the optionalCols parameter."
)
}
}
val invalidColumns = appendDataColumns.filterNot(column => tableColumns.contains(column) || optionalCols.contains(column))
require(invalidColumns.isEmpty, s"The following columns are not part of the current Delta table. If you want to add these columns to the table, you must set the optionalCols parameter: ${invalidColumns.mkString("List(", ", ", ")")}")

val details = deltaTable.detail().select("location").collect().head.getString(0)

Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/mrpowers/jodie/DeltaHelperSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ class DeltaHelperSpec
List("col4") )
}.getMessage

assert(exceptionMessage.contains("The column col5 is not part of the current Delta table. If you want to add the column to the table, you must set the optionalCols parameter"))
assert(exceptionMessage.contains("The following columns are not part of the current Delta table. If you want to add these columns to the table, you must set the optionalCols parameter: List(col5)"))
}
it("should fail to append dataframes with missing required columns"){
val path = (os.pwd / "tmp" / "delta-lake-validate-missing-required-cols").toString()
Expand Down

0 comments on commit 3674cf0

Please sign in to comment.