@@ -42,11 +42,28 @@ import org.apache.spark.sql.test.SharedSQLContext
4242 * For example:
4343 * {{{
4444 * -- this is a comment
45- * select 1 + 2 ;
45+ * select 1, -1 ;
4646 * select current_date;
4747 * }}}
4848 *
49- * Result files are encoded as XMLs.
49+ * The format for golden result files look roughly like:
50+ * {{{
51+ * -- some header information
52+ *
53+ * -- !query 0
54+ * select 1, -1
55+ * -- !query 0 schema
56+ * int, int
57+ * -- !query 0 output
58+ * +---+----+
59+ * | 1|(-1)|
60+ * +---+----+
61+ * | 1| -1|
62+ * +---+----+
63+ *
64+ * -- !query 1
65+ * ...
66+ * }}}
5067 */
5168class SQLQueryTestSuite extends QueryTest with SharedSQLContext {
5269
@@ -68,15 +85,14 @@ class SQLQueryTestSuite extends QueryTest with SharedSQLContext {
6885
6986 /** A single SQL query's output. */
7087 private case class QueryOutput (sql : String , schema : String , output : String ) {
71- def toXML : String = {
72- // We are explicitly not using multi-line string due to stripMargin removing |s,
73- // and not using XML interpolation because there is no simple way to indent outputs nicely
74- // (scala.xml.PrettyPrinter has issue with tabs).
75- " <query>\n " +
76- s " <sql><![CDATA[ $sql]]></sql> \n " +
77- s " <schema><![CDATA[ $schema]]></schema> \n " +
78- s " <output><![CDATA[ \n $output]]></output> \n " +
79- s " </query> "
88+ def toString (queryIndex : Int ): String = {
89+ // We are explicitly not using multi-line string due to stripMargin removing "|" in output.
90+ s " -- !query $queryIndex\n " +
91+ sql + " \n " +
92+ s " -- !query $queryIndex schema \n " +
93+ schema + " \n " +
94+ s " -- !query $queryIndex output \n " +
95+ output
8096 }
8197 }
8298
@@ -96,7 +112,9 @@ class SQLQueryTestSuite extends QueryTest with SharedSQLContext {
96112
97113 // List of SQL queries to run
98114 val queries : Seq [String ] = {
99- val cleaned = input.split(" \n " ).filterNot(_.matches(" --.*(?<=[^\\\\ ]);" )).mkString(" \n " )
115+ // val cleaned = input.split("\n").filterNot(_.matches("--.*(?<=[^\\\\]);")).mkString("\n")
116+ val cleaned = input.split(" \n " ).filterNot(_.startsWith(" --" )).mkString(" \n " )
117+ // note: this is not a robust way to split queries using semicolon, but works for now.
100118 cleaned.split(" (?<=[^\\\\ ]);" ).map(_.trim).filterNot(q => q == " " ).toSeq
101119 }
102120
@@ -111,27 +129,26 @@ class SQLQueryTestSuite extends QueryTest with SharedSQLContext {
111129 }
112130
113131 if (regenerateGoldenFiles) {
114- // If generate golden file flag is on, create the golden file.
115- // Again, we are explicitly not using multi-line string due to stripMargin removing |s,
116- // and not using XML interpolation because there is no simple way to indent outputs nicely
117- // (scala.xml.PrettyPrinter has issue with tabs).
118- val xmlOutput = {
119- " <testcase>\n " +
120- " <!-- Automatically generated by ${getClass.getName} -->\n " +
121- outputs.map(_.toXML).mkString(" \n " ) +
122- " \n </testcase>\n "
132+ // Again, we are explicitly not using multi-line string due to stripMargin removing "|".
133+ val goldenOutput = {
134+ s " -- Automatically generated by ${getClass.getName}\n " +
135+ s " -- Number of queries: ${outputs.size}\n\n\n " +
136+ outputs.zipWithIndex.map{case (qr, i) => qr.toString(i)}.mkString(" \n\n\n " ) + " \n "
123137 }
124- stringToFile(new File (testCase.resultFile), xmlOutput )
138+ stringToFile(new File (testCase.resultFile), goldenOutput )
125139 }
126140
127141 // Read back the golden file.
128142 val expectedOutputs : Seq [QueryOutput ] = {
129- val xml = scala.xml.XML .loadString(fileToString(new File (testCase.resultFile)))
130- (xml \ " query" ).map { q =>
143+ val goldenOutput = fileToString(new File (testCase.resultFile))
144+ val segments = goldenOutput.split(" -- !query.+\n " )
145+ assert(segments.size == outputs.size * 3 + 1 ) // each query has 3 segments, plus the header
146+ Seq .tabulate(outputs.size) { i =>
131147 QueryOutput (
132- sql = (q \ " sql" ).text,
133- schema = (q \ " schema" ).text,
134- output = (q \ " output" ).text.trim)
148+ sql = segments(i * 3 + 1 ).trim,
149+ schema = segments(i * 3 + 2 ).trim,
150+ output = segments(i * 3 + 3 ).trim
151+ )
135152 }
136153 }
137154
@@ -149,7 +166,7 @@ class SQLQueryTestSuite extends QueryTest with SharedSQLContext {
149166
150167 private def listTestCases (): Seq [TestCase ] = {
151168 listFilesRecursively(new File (inputFilePath)).map { file =>
152- val resultFile = file.getAbsolutePath.replace(inputFilePath, goldenFilePath) + " .xml "
169+ val resultFile = file.getAbsolutePath.replace(inputFilePath, goldenFilePath) + " .out "
153170 TestCase (file.getName, file.getAbsolutePath, resultFile)
154171 }
155172 }
0 commit comments