Skip to content

Commit

Permalink
Use a more concise and human readable format.
Browse files Browse the repository at this point in the history
  • Loading branch information
petermaxlee committed Aug 3, 2016
1 parent 9b360da commit a1e1b57
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-- Automatically generated by org.apache.spark.sql.SQLQueryTestSuite
-- Number of queries: 4


-- !query 0
select 1, -1
-- !query 0 schema
int, int
-- !query 0 output
+---+----+
| 1|(-1)|
+---+----+
| 1| -1|
+---+----+


-- !query 1
select 2147483648, -2147483649
-- !query 1 schema
bigint, bigint
-- !query 1 output
+----------+-------------+
|2147483648|(-2147483649)|
+----------+-------------+
|2147483648| -2147483649|
+----------+-------------+


-- !query 2
select 9223372036854775808, -9223372036854775809
-- !query 2 schema
decimal(19,0), decimal(19,0)
-- !query 2 output
+-------------------+----------------------+
|9223372036854775808|(-9223372036854775809)|
+-------------------+----------------------+
|9223372036854775808| -9223372036854775809|
+-------------------+----------------------+


-- !query 3
select 0.3, -0.8, .5, -.18
-- !query 3 schema
decimal(1,1), decimal(1,1), decimal(1,1), decimal(2,2)
-- !query 3 output
+---+------+---+-------+
|0.3|(-0.8)|0.5|(-0.18)|
+---+------+---+-------+
|0.3| -0.8|0.5| -0.18|
+---+------+---+-------+

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,28 @@ import org.apache.spark.sql.test.SharedSQLContext
* For example:
* {{{
* -- this is a comment
* select 1 + 2;
* select 1, -1;
* select current_date;
* }}}
*
* Result files are encoded as XMLs.
* The format for golden result files look roughly like:
* {{{
* -- some header information
*
* -- !query 0
* select 1, -1
* -- !query 0 schema
* int, int
* -- !query 0 output
* +---+----+
* | 1|(-1)|
* +---+----+
* | 1| -1|
* +---+----+
*
* -- !query 1
* ...
* }}}
*/
class SQLQueryTestSuite extends QueryTest with SharedSQLContext {

Expand All @@ -68,15 +85,14 @@ class SQLQueryTestSuite extends QueryTest with SharedSQLContext {

/** A single SQL query's output. */
private case class QueryOutput(sql: String, schema: String, output: String) {
def toXML: String = {
// We are explicitly not using multi-line string due to stripMargin removing |s,
// and not using XML interpolation because there is no simple way to indent outputs nicely
// (scala.xml.PrettyPrinter has issue with tabs).
"<query>\n" +
s" <sql><![CDATA[$sql]]></sql>\n" +
s" <schema><![CDATA[$schema]]></schema>\n" +
s" <output><![CDATA[\n$output]]></output>\n" +
s"</query>"
def toString(queryIndex: Int): String = {
// We are explicitly not using multi-line string due to stripMargin removing "|" in output.
s"-- !query $queryIndex\n" +
sql + "\n" +
s"-- !query $queryIndex schema\n" +
schema + "\n" +
s"-- !query $queryIndex output\n" +
output
}
}

Expand All @@ -96,7 +112,9 @@ class SQLQueryTestSuite extends QueryTest with SharedSQLContext {

// List of SQL queries to run
val queries: Seq[String] = {
val cleaned = input.split("\n").filterNot(_.matches("--.*(?<=[^\\\\]);")).mkString("\n")
// val cleaned = input.split("\n").filterNot(_.matches("--.*(?<=[^\\\\]);")).mkString("\n")
val cleaned = input.split("\n").filterNot(_.startsWith("--")).mkString("\n")
// note: this is not a robust way to split queries using semicolon, but works for now.
cleaned.split("(?<=[^\\\\]);").map(_.trim).filterNot(q => q == "").toSeq
}

Expand All @@ -111,27 +129,26 @@ class SQLQueryTestSuite extends QueryTest with SharedSQLContext {
}

if (regenerateGoldenFiles) {
// If generate golden file flag is on, create the golden file.
// Again, we are explicitly not using multi-line string due to stripMargin removing |s,
// and not using XML interpolation because there is no simple way to indent outputs nicely
// (scala.xml.PrettyPrinter has issue with tabs).
val xmlOutput = {
"<testcase>\n" +
"<!-- Automatically generated by ${getClass.getName} -->\n" +
outputs.map(_.toXML).mkString("\n") +
"\n</testcase>\n"
// Again, we are explicitly not using multi-line string due to stripMargin removing "|".
val goldenOutput = {
s"-- Automatically generated by ${getClass.getName}\n" +
s"-- Number of queries: ${outputs.size}\n\n\n" +
outputs.zipWithIndex.map{case (qr, i) => qr.toString(i)}.mkString("\n\n\n") + "\n"
}
stringToFile(new File(testCase.resultFile), xmlOutput)
stringToFile(new File(testCase.resultFile), goldenOutput)
}

// Read back the golden file.
val expectedOutputs: Seq[QueryOutput] = {
val xml = scala.xml.XML.loadString(fileToString(new File(testCase.resultFile)))
(xml \ "query").map { q =>
val goldenOutput = fileToString(new File(testCase.resultFile))
val segments = goldenOutput.split("-- !query.+\n")
assert(segments.size == outputs.size * 3 + 1) // each query has 3 segments, plus the header
Seq.tabulate(outputs.size) { i =>
QueryOutput(
sql = (q \ "sql").text,
schema = (q \ "schema").text,
output = (q \ "output").text.trim)
sql = segments(i * 3 + 1).trim,
schema = segments(i * 3 + 2).trim,
output = segments(i * 3 + 3).trim
)
}
}

Expand All @@ -149,7 +166,7 @@ class SQLQueryTestSuite extends QueryTest with SharedSQLContext {

private def listTestCases(): Seq[TestCase] = {
listFilesRecursively(new File(inputFilePath)).map { file =>
val resultFile = file.getAbsolutePath.replace(inputFilePath, goldenFilePath) + ".xml"
val resultFile = file.getAbsolutePath.replace(inputFilePath, goldenFilePath) + ".out"
TestCase(file.getName, file.getAbsolutePath, resultFile)
}
}
Expand Down

0 comments on commit a1e1b57

Please sign in to comment.