Skip to content

Commit a1e1b57

Browse files
committed
Use a more concise and human readable format.
1 parent 9b360da commit a1e1b57

File tree

3 files changed

+95
-77
lines changed

3 files changed

+95
-77
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
-- Automatically generated by org.apache.spark.sql.SQLQueryTestSuite
2+
-- Number of queries: 4
3+
4+
5+
-- !query 0
6+
select 1, -1
7+
-- !query 0 schema
8+
int, int
9+
-- !query 0 output
10+
+---+----+
11+
| 1|(-1)|
12+
+---+----+
13+
| 1| -1|
14+
+---+----+
15+
16+
17+
-- !query 1
18+
select 2147483648, -2147483649
19+
-- !query 1 schema
20+
bigint, bigint
21+
-- !query 1 output
22+
+----------+-------------+
23+
|2147483648|(-2147483649)|
24+
+----------+-------------+
25+
|2147483648| -2147483649|
26+
+----------+-------------+
27+
28+
29+
-- !query 2
30+
select 9223372036854775808, -9223372036854775809
31+
-- !query 2 schema
32+
decimal(19,0), decimal(19,0)
33+
-- !query 2 output
34+
+-------------------+----------------------+
35+
|9223372036854775808|(-9223372036854775809)|
36+
+-------------------+----------------------+
37+
|9223372036854775808| -9223372036854775809|
38+
+-------------------+----------------------+
39+
40+
41+
-- !query 3
42+
select 0.3, -0.8, .5, -.18
43+
-- !query 3 schema
44+
decimal(1,1), decimal(1,1), decimal(1,1), decimal(2,2)
45+
-- !query 3 output
46+
+---+------+---+-------+
47+
|0.3|(-0.8)|0.5|(-0.18)|
48+
+---+------+---+-------+
49+
|0.3| -0.8|0.5| -0.18|
50+
+---+------+---+-------+

sql/core/src/test/resources/sql-tests/results/number-format.sql.xml

Lines changed: 0 additions & 49 deletions
This file was deleted.

sql/core/src/test/scala/org/apache/spark/sql/SQLQueryTestSuite.scala

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
5168
class 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

Comments
 (0)