Skip to content

Commit

Permalink
update whitespace formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPowers committed Feb 24, 2021
1 parent 33d5385 commit 5cc072a
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private[sql] class DataFrameColumnsAbsence(df: DataFrame, prohibitedColNames: Se

def validateAbsenceOfColumns(): Unit = {
if (extraColNames.nonEmpty) {
throw new ProhibitedDataFrameColumnsException(extraColumnsMessage())
throw ProhibitedDataFrameColumnsException(extraColumnsMessage())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private[sql] class DataFrameColumnsChecker(df: DataFrame, requiredColNames: Seq[

def validatePresenceOfColumns(): Unit = {
if (missingColumns.nonEmpty) {
throw new MissingDataFrameColumnsException(missingColumnsMessage())
throw MissingDataFrameColumnsException(missingColumnsMessage())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ object DataFrameExt {
* The `actualDF` will have the `greeting` column first, then the `team` column then the `cats` column.
*/
def reorderColumns(colNames: Seq[String]): DataFrame = {
val cols = colNames.map(col(_))
val cols = colNames.map(col)
df.select(cols: _*)
}

Expand Down Expand Up @@ -211,7 +211,7 @@ object DataFrameExt {
def trans(customTransform: CustomTransform): DataFrame = {
// make sure df doesn't already have the columns that will be added
if (df.columns.toSeq.exists((c: String) => customTransform.addedColumns.contains(c))) {
throw new DataFrameColumnsException(
throw DataFrameColumnsException(
s"The DataFrame already contains the columns your transformation will add. The DataFrame has these columns: [${df.columns
.mkString(", ")}]. You've asserted that your transformation will add these columns: [${customTransform.addedColumns
.mkString(", ")}]"
Expand Down Expand Up @@ -243,7 +243,7 @@ object DataFrameExt {
// make sure the columns have been added
val actualColumnsAdded = transformedDF.columnDiff(df)
if (!actualColumnsAdded.equals(customTransform.addedColumns)) {
throw new DataFrameColumnsException(
throw DataFrameColumnsException(
s"The [${actualColumnsAdded.mkString(", ")}] columns were actually added, but you specified that these columns should have been added [${customTransform.addedColumns
.mkString(", ")}]"
)
Expand All @@ -252,7 +252,7 @@ object DataFrameExt {
// make sure the columns have been removed
val actualColumnsRemoved = df.columnDiff(transformedDF)
if (!actualColumnsRemoved.equals(customTransform.removedColumns)) {
throw new DataFrameColumnsException(
throw DataFrameColumnsException(
s"The [${actualColumnsRemoved.mkString(", ")}] columns were actually removed, but you specified that these columns should have been removed [${customTransform.removedColumns
.mkString(", ")}]"
)
Expand Down Expand Up @@ -368,12 +368,7 @@ object DataFrameExt {
* Here is how to trim all the columns df.renameColumns(_.trim)
*/
def renameColumns(f: String => String): DataFrame =
df.columns.foldLeft(df)((tempDf, c) =>
tempDf.withColumnRenamed(
c,
f(c)
)
)
df.columns.foldLeft(df)((tempDf, c) => tempDf.withColumnRenamed(c, f(c)))

/**
* Drops multiple columns that satisfy the conditions of a function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,10 @@ object DataFrameHelpers extends DataFrameValidator {
def twoColumnsToMap[keyType: TypeTag, valueType: TypeTag](df: DataFrame, keyColName: String, valueColName: String): Map[keyType, valueType] = {
validatePresenceOfColumns(
df,
Seq(
keyColName,
valueColName
)
Seq(keyColName, valueColName)
)
df.select(
keyColName,
valueColName
).collect()
df.select(keyColName, valueColName)
.collect()
.map(r => (r(0).asInstanceOf[keyType], r(1).asInstanceOf[valueType]))
.toMap
}
Expand Down Expand Up @@ -121,10 +116,7 @@ object DataFrameHelpers extends DataFrameValidator {
* }}}
*/
def columnToList[T: ClassTag](df: DataFrame, colName: String): List[T] = {
columnToArray[T](
df,
colName
).toList
columnToArray[T](df, colName).toList
}

/**
Expand Down Expand Up @@ -235,17 +227,11 @@ object DataFrameHelpers extends DataFrameValidator {

if (overwriteLatest) {
val latestData = Seq(
Row(
outputPath
)
Row(outputPath)
)

val latestSchema = List(
StructField(
"latest_path",
StringType,
false
)
StructField("latest_path", StringType, false)
)

val latestDF = spark.createDataFrame(
Expand All @@ -254,29 +240,17 @@ object DataFrameHelpers extends DataFrameValidator {
)

latestDF.write
.option(
"header",
"false"
)
.option(
"delimiter",
","
)
.option("header", "false")
.option("delimiter", ",")
.mode(SaveMode.Overwrite)
.csv(outputDirname + "/latest")
}
}

def readTimestamped(dirname: String): DataFrame = {
val latestDF = spark.read
.option(
"header",
"false"
)
.option(
"delimiter",
","
)
.option("header", "false")
.option("delimiter", ",")
.csv(dirname + "/latest")

val latestPath = latestDF.head().getString(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private[sql] class DataFrameSchemaChecker(df: DataFrame, requiredSchema: StructT

def validateSchema(): Unit = {
if (missingStructFields.nonEmpty) {
throw new InvalidDataFrameSchemaException(missingStructFieldsMessage())
throw InvalidDataFrameSchemaException(missingStructFieldsMessage())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ trait DataFrameValidator {
* > com.github.mrpowers.spark.daria.sql.MissingDataFrameColumnsException: The [country, city] columns are not included in the DataFrame with the following columns [team, sport]
*/
def validatePresenceOfColumns(df: DataFrame, requiredColNames: Seq[String]): Unit = {
val c = new DataFrameColumnsChecker(
df,
requiredColNames
)
val c = new DataFrameColumnsChecker(df, requiredColNames)
c.validatePresenceOfColumns()
}

Expand Down Expand Up @@ -71,10 +68,7 @@ trait DataFrameValidator {
* > com.github.mrpowers.spark.daria.sql.InvalidDataFrameSchemaException: The [StructField(name,StringType,true)] StructFields are not included in the DataFrame with the following StructFields [StructType(StructField(num1,IntegerType,true), StructField(num2,IntegerType,true))]
*/
def validateSchema(df: DataFrame, requiredSchema: StructType): Unit = {
val c = new DataFrameSchemaChecker(
df,
requiredSchema
)
val c = new DataFrameSchemaChecker(df, requiredSchema)
c.validateSchema()
}

Expand All @@ -98,10 +92,7 @@ trait DataFrameValidator {
* > com.github.mrpowers.spark.daria.sql.ProhibitedDataFrameColumnsException: The [team, sport] columns are not allowed to be included in the DataFrame with the following columns [team, sport]
*/
def validateAbsenceOfColumns(df: DataFrame, prohibitedColNames: Seq[String]): Unit = {
val c = new DataFrameColumnsAbsence(
df,
prohibitedColNames
)
val c = new DataFrameColumnsAbsence(df, prohibitedColNames)
c.validateAbsenceOfColumns()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ object FunctionsAsColumnExt {
def lower(): Column = t(F.lower)

def regexp_replace(pattern: String, replacement: String): Column =
F.regexp_replace(
col,
pattern,
replacement
)
F.regexp_replace(col, pattern, replacement)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object transformations {
} else {
val message =
s"The sort order must be 'asc' or 'desc'. Your sort order was '$order'."
throw new InvalidColumnSortOrderException(message)
throw InvalidColumnSortOrderException(message)
}
val cols = colNames.map(col(_))
df.select(cols: _*)
Expand Down Expand Up @@ -175,11 +175,7 @@ object transformations {
memoDF
.withColumn(
col.toString(),
regexp_replace(
col,
pattern,
replacement
)
regexp_replace(col, pattern, replacement)
)
}
}
Expand Down Expand Up @@ -207,11 +203,7 @@ object transformations {
}
.toList

multiRegexpReplace(
cols,
pattern,
replacement
)(df)
multiRegexpReplace(cols, pattern, replacement)(df)
}

/**
Expand All @@ -235,10 +227,7 @@ object transformations {
if (memoDF.schema.fieldNames.contains(colName)) {
memoDF.withColumn(
colName,
truncate(
col(colName),
length
)
truncate(col(colName), length)
)
} else {
memoDF
Expand Down Expand Up @@ -304,10 +293,7 @@ object transformations {
def extractFromJson(colName: String, outputColName: String, jsonSchema: StructType)(df: DataFrame): DataFrame = {
df.withColumn(
outputColName,
from_json(
col(colName),
jsonSchema
)
from_json(col(colName), jsonSchema)
)
}

Expand Down Expand Up @@ -341,10 +327,7 @@ object transformations {
def extractFromJson(colName: String, outputColName: String, path: String)(df: DataFrame): DataFrame = {
df.withColumn(
outputColName,
get_json_object(
col(colName),
path
)
get_json_object(col(colName), path)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ object ArrayHelpers {
def regexpString(strs: Array[String], charsToEscape: List[String] = StringHelpers.sqlCharsToEscape): String = {
val t = strs.filter(_ != null).map { str: String =>
StringHelpers
.escapeForSqlRegexp(
str,
charsToEscape
)
.escapeForSqlRegexp(str, charsToEscape)
.getOrElse(None)
}
t.mkString("|")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@ object StringHelpers {

Some(charsToEscape.foldLeft(str) {
case (res, pattern) =>
res.replaceAll(
pattern,
"\\" + pattern
)
res.replaceAll(pattern, "\\" + pattern)
})
}

def toSnakeCase(str: String): String = {
str
.replaceAll(
"\\s+",
"_"
)
.replaceAll("\\s+", "_")
.toLowerCase
}

Expand All @@ -48,22 +42,13 @@ object StringHelpers {
*/
def snakify(name: String): String =
name
.replaceAll(
"([A-Z]+)([A-Z][a-z])",
"$1_$2"
)
.replaceAll(
"([a-z\\d])([A-Z])",
"$1_$2"
)
.replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2")
.replaceAll("([a-z\\d])([A-Z])", "$1_$2")
.toLowerCase

def camelCaseToSnakeCase(str: String): String = {
str
.replaceAll(
"([A-Z]+)",
"_$1"
)
.replaceAll("([A-Z]+)", "_$1")
.toLowerCase
.stripPrefix("_")
}
Expand Down

0 comments on commit 5cc072a

Please sign in to comment.