Skip to content
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

Ensure column names are valid when writing benchmark query results to file #1247

Merged
merged 3 commits into from
Dec 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ object BenchUtils {
resultsAction match {
case Collect() => df.collect()
case WriteCsv(path, mode, options) =>
df.write.mode(mode).options(options).csv(path)
ensureValidColumnNames(df).write.mode(mode).options(options).csv(path)
case WriteOrc(path, mode, options) =>
df.write.mode(mode).options(options).orc(path)
ensureValidColumnNames(df).write.mode(mode).options(options).orc(path)
case WriteParquet(path, mode, options) =>
df.write.mode(mode).options(options).parquet(path)
ensureValidColumnNames(df).write.mode(mode).options(options).parquet(path)
}

val end = System.nanoTime()
Expand Down Expand Up @@ -305,6 +305,27 @@ object BenchUtils {
report
}

/**
* Replace any invalid column names with c0, c1, and so on so that there are no columns with
* names based on expressions such as "round((sun_sales1 / sun_sales2), 2)". This is necessary
* when writing query output to Parquet.
*/
private def ensureValidColumnNames(df: DataFrame): DataFrame = {
def isColumnStart(ch: Char) = ch.isLetter || ch == '_'
def isColumnPart(ch: Char) = ch.isLetterOrDigit || ch == '_'
def isValid(name: String) = name.length > 0 &&
isColumnStart(name.charAt(0)) &&
name.substring(1).toCharArray.forall(isColumnPart)
val renameColumnExprs = df.columns.zipWithIndex.map {
case (name, i) => if (isValid(name)) {
col(name)
} else {
col(name).as("c" + i)
}
}
df.select(renameColumnExprs: _*)
}

def readReport(file: File): BenchmarkReport = {
implicit val formats = DefaultFormats
val json = parse(file)
Expand Down