From 3674cf0800e9e96067225a21b88fbe6736559449 Mon Sep 17 00:00:00 2001 From: Juan Pulido Date: Fri, 17 Nov 2023 07:59:05 -0500 Subject: [PATCH] Changed non-idiomatic expressions in scala --- README.md | 2 +- .../scala/mrpowers/jodie/DeltaHelpers.scala | 30 ++++--------------- .../mrpowers/jodie/DeltaHelperSpec.scala | 2 +- 3 files changed, 7 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 5740f45..a0089c4 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/main/scala/mrpowers/jodie/DeltaHelpers.scala b/src/main/scala/mrpowers/jodie/DeltaHelpers.scala index cf8d1fa..5987f99 100644 --- a/src/main/scala/mrpowers/jodie/DeltaHelpers.scala +++ b/src/main/scala/mrpowers/jodie/DeltaHelpers.scala @@ -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) diff --git a/src/test/scala/mrpowers/jodie/DeltaHelperSpec.scala b/src/test/scala/mrpowers/jodie/DeltaHelperSpec.scala index fe813cf..351d25d 100644 --- a/src/test/scala/mrpowers/jodie/DeltaHelperSpec.scala +++ b/src/test/scala/mrpowers/jodie/DeltaHelperSpec.scala @@ -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()