|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql |
| 19 | + |
| 20 | +import java.io.File |
| 21 | +import java.util.{Locale, TimeZone} |
| 22 | + |
| 23 | +import org.apache.spark.sql.catalyst.rules.RuleExecutor |
| 24 | +import org.apache.spark.sql.catalyst.util.{fileToString, stringToFile} |
| 25 | +import org.apache.spark.sql.test.SharedSQLContext |
| 26 | + |
| 27 | +/** |
| 28 | + * End-to-end test cases for SQL queries. |
| 29 | + * |
| 30 | + * Each case is loaded from a file in "spark/sql/core/src/test/resources/sql-tests/inputs". |
| 31 | + * Each case has a golden result file in "spark/sql/core/src/test/resources/sql-tests/results". |
| 32 | + * |
| 33 | + * To re-generate golden files, run: |
| 34 | + * {{{ |
| 35 | + * SPARK_GENERATE_GOLDEN_FILES=1 build/sbt "sql/test-only *SQLQueryTestSuite" |
| 36 | + * }}} |
| 37 | + * |
| 38 | + * The format for input files is simple: |
| 39 | + * 1. A list of SQL queries separated by semicolon. |
| 40 | + * 2. Lines starting with -- are treated as comments and ignored. |
| 41 | + * |
| 42 | + * For example: |
| 43 | + * {{{ |
| 44 | + * -- this is a comment |
| 45 | + * select 1, -1; |
| 46 | + * select current_date; |
| 47 | + * }}} |
| 48 | + * |
| 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 | + * struct<...schema...> |
| 57 | + * -- !query 0 output |
| 58 | + * ... data row 1 ... |
| 59 | + * ... data row 2 ... |
| 60 | + * ... |
| 61 | + * |
| 62 | + * -- !query 1 |
| 63 | + * ... |
| 64 | + * }}} |
| 65 | + */ |
| 66 | +class SQLQueryTestSuite extends QueryTest with SharedSQLContext { |
| 67 | + |
| 68 | + private val regenerateGoldenFiles: Boolean = System.getenv("SPARK_GENERATE_GOLDEN_FILES") == "1" |
| 69 | + |
| 70 | + private val baseResourcePath = { |
| 71 | + // If regenerateGoldenFiles is true, we must be running this in SBT and we use hard-coded |
| 72 | + // relative path. Otherwise, we use classloader's getResource to find the location. |
| 73 | + if (regenerateGoldenFiles) { |
| 74 | + java.nio.file.Paths.get("src", "test", "resources", "sql-tests").toFile |
| 75 | + } else { |
| 76 | + val res = getClass.getClassLoader.getResource("sql-tests") |
| 77 | + new File(res.getFile) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private val inputFilePath = new File(baseResourcePath, "inputs").getAbsolutePath |
| 82 | + private val goldenFilePath = new File(baseResourcePath, "results").getAbsolutePath |
| 83 | + |
| 84 | + /** List of test cases to ignore, in lower cases. */ |
| 85 | + private val blackList = Set( |
| 86 | + "blacklist.sql" // Do NOT remove this one. It is here to test the blacklist functionality. |
| 87 | + ) |
| 88 | + |
| 89 | + // Create all the test cases. |
| 90 | + listTestCases().foreach(createScalaTestCase) |
| 91 | + |
| 92 | + /** A test case. */ |
| 93 | + private case class TestCase(name: String, inputFile: String, resultFile: String) |
| 94 | + |
| 95 | + /** A single SQL query's output. */ |
| 96 | + private case class QueryOutput(sql: String, schema: String, output: String) { |
| 97 | + def toString(queryIndex: Int): String = { |
| 98 | + // We are explicitly not using multi-line string due to stripMargin removing "|" in output. |
| 99 | + s"-- !query $queryIndex\n" + |
| 100 | + sql + "\n" + |
| 101 | + s"-- !query $queryIndex schema\n" + |
| 102 | + schema + "\n" + |
| 103 | + s"-- !query $queryIndex output\n" + |
| 104 | + output |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private def createScalaTestCase(testCase: TestCase): Unit = { |
| 109 | + if (blackList.contains(testCase.name.toLowerCase)) { |
| 110 | + // Create a test case to ignore this case. |
| 111 | + ignore(testCase.name) { /* Do nothing */ } |
| 112 | + } else { |
| 113 | + // Create a test case to run this case. |
| 114 | + test(testCase.name) { runTest(testCase) } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + /** Run a test case. */ |
| 119 | + private def runTest(testCase: TestCase): Unit = { |
| 120 | + val input = fileToString(new File(testCase.inputFile)) |
| 121 | + |
| 122 | + // List of SQL queries to run |
| 123 | + val queries: Seq[String] = { |
| 124 | + val cleaned = input.split("\n").filterNot(_.startsWith("--")).mkString("\n") |
| 125 | + // note: this is not a robust way to split queries using semicolon, but works for now. |
| 126 | + cleaned.split("(?<=[^\\\\]);").map(_.trim).filter(_ != "").toSeq |
| 127 | + } |
| 128 | + |
| 129 | + // Run the SQL queries preparing them for comparison. |
| 130 | + val outputs: Seq[QueryOutput] = queries.map { sql => |
| 131 | + val df = spark.sql(sql) |
| 132 | + // We might need to do some query canonicalization in the future. |
| 133 | + QueryOutput( |
| 134 | + sql = sql, |
| 135 | + schema = df.schema.catalogString, |
| 136 | + output = df.queryExecution.hiveResultString().mkString("\n")) |
| 137 | + } |
| 138 | + |
| 139 | + if (regenerateGoldenFiles) { |
| 140 | + // Again, we are explicitly not using multi-line string due to stripMargin removing "|". |
| 141 | + val goldenOutput = { |
| 142 | + s"-- Automatically generated by ${getClass.getName}\n" + |
| 143 | + s"-- Number of queries: ${outputs.size}\n\n\n" + |
| 144 | + outputs.zipWithIndex.map{case (qr, i) => qr.toString(i)}.mkString("\n\n\n") + "\n" |
| 145 | + } |
| 146 | + stringToFile(new File(testCase.resultFile), goldenOutput) |
| 147 | + } |
| 148 | + |
| 149 | + // Read back the golden file. |
| 150 | + val expectedOutputs: Seq[QueryOutput] = { |
| 151 | + val goldenOutput = fileToString(new File(testCase.resultFile)) |
| 152 | + val segments = goldenOutput.split("-- !query.+\n") |
| 153 | + |
| 154 | + // each query has 3 segments, plus the header |
| 155 | + assert(segments.size == outputs.size * 3 + 1, |
| 156 | + s"Expected ${outputs.size * 3 + 1} blocks in result file but got ${segments.size}. " + |
| 157 | + s"Try regenerate the result files.") |
| 158 | + Seq.tabulate(outputs.size) { i => |
| 159 | + QueryOutput( |
| 160 | + sql = segments(i * 3 + 1).trim, |
| 161 | + schema = segments(i * 3 + 2).trim, |
| 162 | + output = segments(i * 3 + 3).trim |
| 163 | + ) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // Compare results. |
| 168 | + assertResult(expectedOutputs.size, s"Number of queries should be ${expectedOutputs.size}") { |
| 169 | + outputs.size |
| 170 | + } |
| 171 | + |
| 172 | + outputs.zip(expectedOutputs).zipWithIndex.foreach { case ((output, expected), i) => |
| 173 | + assertResult(expected.sql, s"SQL query should match for query #$i") { output.sql } |
| 174 | + assertResult(expected.schema, s"Schema should match for query #$i") { output.schema } |
| 175 | + assertResult(expected.output, s"Result should match for query #$i") { output.output } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private def listTestCases(): Seq[TestCase] = { |
| 180 | + listFilesRecursively(new File(inputFilePath)).map { file => |
| 181 | + val resultFile = file.getAbsolutePath.replace(inputFilePath, goldenFilePath) + ".out" |
| 182 | + TestCase(file.getName, file.getAbsolutePath, resultFile) |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + /** Returns all the files (not directories) in a directory, recursively. */ |
| 187 | + private def listFilesRecursively(path: File): Seq[File] = { |
| 188 | + val (dirs, files) = path.listFiles().partition(_.isDirectory) |
| 189 | + files ++ dirs.flatMap(listFilesRecursively) |
| 190 | + } |
| 191 | + |
| 192 | + private val originalTimeZone = TimeZone.getDefault |
| 193 | + private val originalLocale = Locale.getDefault |
| 194 | + |
| 195 | + override def beforeAll(): Unit = { |
| 196 | + super.beforeAll() |
| 197 | + // Timezone is fixed to America/Los_Angeles for those timezone sensitive tests (timestamp_*) |
| 198 | + TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles")) |
| 199 | + // Add Locale setting |
| 200 | + Locale.setDefault(Locale.US) |
| 201 | + RuleExecutor.resetTime() |
| 202 | + } |
| 203 | + |
| 204 | + override def afterAll(): Unit = { |
| 205 | + try { |
| 206 | + TimeZone.setDefault(originalTimeZone) |
| 207 | + Locale.setDefault(originalLocale) |
| 208 | + |
| 209 | + // For debugging dump some statistics about how much time was spent in various optimizer rules |
| 210 | + logWarning(RuleExecutor.dumpTimeSpent()) |
| 211 | + } finally { |
| 212 | + super.afterAll() |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments