Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
respond to code review comments
  • Loading branch information
dtenedor committed Mar 16, 2023
commit 26e7a31730222f891d49d12f6078170510e47931
111 changes: 0 additions & 111 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQueryTestHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package org.apache.spark.sql

import java.io.File

import scala.collection.mutable.ArrayBuffer
import scala.util.control.NonFatal

import org.apache.spark.{SparkException, SparkThrowable}
Expand All @@ -28,7 +27,6 @@ import org.apache.spark.SparkThrowableHelper.getMessage
import org.apache.spark.internal.Logging
import org.apache.spark.sql.catalyst.planning.PhysicalOperation
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.catalyst.util.fileToString
import org.apache.spark.sql.execution.HiveResult.hiveResultString
import org.apache.spark.sql.execution.SQLExecution
import org.apache.spark.sql.execution.command.{DescribeColumnCommand, DescribeCommandBase}
Expand Down Expand Up @@ -129,115 +127,6 @@ trait SQLQueryTestHelper extends Logging {
}
}

/** A test case. */
protected trait TestCase {
val name: String
val inputFile: String
val resultFile: String
}

/** Run a test case. */
protected def runSqlTestCase(
testCase: TestCase,
listTestCases: Seq[TestCase],
runQueries: (
Seq[String], // queries
TestCase, // test case
Seq[(String, String)] // config set
) => Unit): Unit = {
val input = fileToString(new File(testCase.inputFile))

val (comments, code) = splitCommentsAndCodes(input)

// If `--IMPORT` found, load code from another test case file, then insert them
// into the head in this test.
val importedTestCaseName = comments.filter(_.startsWith("--IMPORT ")).map(_.substring(9))
val importedCode = importedTestCaseName.flatMap { testCaseName =>
listTestCases.find(_.name == testCaseName).map { testCase =>
val input = fileToString(new File(testCase.inputFile))
val (_, code) = splitCommentsAndCodes(input)
code
}
}.flatten

val allCode = importedCode ++ code
val tempQueries = if (allCode.exists(_.trim.startsWith("--QUERY-DELIMITER"))) {
// Although the loop is heavy, only used for bracketed comments test.
val queries = new ArrayBuffer[String]
val otherCodes = new ArrayBuffer[String]
var tempStr = ""
var start = false
for (c <- allCode) {
if (c.trim.startsWith("--QUERY-DELIMITER-START")) {
start = true
queries ++= splitWithSemicolon(otherCodes.toSeq)
otherCodes.clear()
} else if (c.trim.startsWith("--QUERY-DELIMITER-END")) {
start = false
queries += s"\n${tempStr.stripSuffix(";")}"
tempStr = ""
} else if (start) {
tempStr += s"\n$c"
} else {
otherCodes += c
}
}
if (otherCodes.nonEmpty) {
queries ++= splitWithSemicolon(otherCodes.toSeq)
}
queries
} else {
splitWithSemicolon(allCode.toSeq).toSeq
}

// List of SQL queries to run
val queries = tempQueries.map(_.trim).filter(_ != "")
// Fix misplacement when comment is at the end of the query.
.map(_.split("\n").filterNot(_.startsWith("--")).mkString("\n")).map(_.trim).filter(_ != "")

val settingLines = comments.filter(_.startsWith("--SET ")).map(_.substring(6))
val settings = settingLines.flatMap(_.split(",").map { kv =>
val (conf, value) = kv.span(_ != '=')
conf.trim -> value.substring(1).trim
})

val regenerateGoldenFiles: Boolean = System.getenv("SPARK_GENERATE_GOLDEN_FILES") == "1"
if (regenerateGoldenFiles) {
runQueries(queries, testCase, settings)
} else {
// A config dimension has multiple config sets, and a config set has multiple configs.
// - config dim: Seq[Seq[(String, String)]]
// - config set: Seq[(String, String)]
// - config: (String, String))
// We need to do cartesian product for all the config dimensions, to get a list of
// config sets, and run the query once for each config set.
val configDimLines = comments.filter(_.startsWith("--CONFIG_DIM")).map(_.substring(12))
val configDims = configDimLines.groupBy(_.takeWhile(_ != ' ')).mapValues { lines =>
lines.map(_.dropWhile(_ != ' ').substring(1)).map(_.split(",").map { kv =>
val (conf, value) = kv.span(_ != '=')
conf.trim -> value.substring(1).trim
}.toSeq).toSeq
}

val configSets = configDims.values.foldLeft(Seq(Seq[(String, String)]())) { (res, dim) =>
dim.flatMap { configSet => res.map(_ ++ configSet) }
}

configSets.foreach { configSet =>
try {
runQueries(queries, testCase, settings ++ configSet)
} catch {
case e: Throwable =>
val configs = configSet.map {
case (k, v) => s"$k=$v"
}
logError(s"Error using configs: ${configs.mkString(",")}")
throw e
}
}
}
}

protected def splitWithSemicolon(seq: Seq[String]): Array[String] = {
seq.mkString("\n").split("(?<=[^\\\\]);")
}
Expand Down
111 changes: 111 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLQueryTestSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import java.io.File
import java.net.URI
import java.util.Locale

import scala.collection.mutable.ArrayBuffer

import org.apache.spark.{SparkConf, TestUtils}
import org.apache.spark.sql.catalyst.expressions.codegen.CodeGenerator
import org.apache.spark.sql.catalyst.plans.SQLHelper
Expand Down Expand Up @@ -264,6 +266,115 @@ class SQLQueryTestSuite extends QueryTest with SharedSparkSession with SQLHelper
}
}

/** A test case. */
protected trait TestCase {
val name: String
val inputFile: String
val resultFile: String
}

/** Run a test case. */
protected def runSqlTestCase(
testCase: TestCase,
listTestCases: Seq[TestCase],
runQueries: (
Seq[String], // queries
TestCase, // test case
Seq[(String, String)] // config set
) => Unit): Unit = {
val input = fileToString(new File(testCase.inputFile))

val (comments, code) = splitCommentsAndCodes(input)

// If `--IMPORT` found, load code from another test case file, then insert them
// into the head in this test.
val importedTestCaseName = comments.filter(_.startsWith("--IMPORT ")).map(_.substring(9))
val importedCode = importedTestCaseName.flatMap { testCaseName =>
listTestCases.find(_.name == testCaseName).map { testCase =>
val input = fileToString(new File(testCase.inputFile))
val (_, code) = splitCommentsAndCodes(input)
code
}
}.flatten

val allCode = importedCode ++ code
val tempQueries = if (allCode.exists(_.trim.startsWith("--QUERY-DELIMITER"))) {
// Although the loop is heavy, only used for bracketed comments test.
val queries = new ArrayBuffer[String]
val otherCodes = new ArrayBuffer[String]
var tempStr = ""
var start = false
for (c <- allCode) {
if (c.trim.startsWith("--QUERY-DELIMITER-START")) {
start = true
queries ++= splitWithSemicolon(otherCodes.toSeq)
otherCodes.clear()
} else if (c.trim.startsWith("--QUERY-DELIMITER-END")) {
start = false
queries += s"\n${tempStr.stripSuffix(";")}"
tempStr = ""
} else if (start) {
tempStr += s"\n$c"
} else {
otherCodes += c
}
}
if (otherCodes.nonEmpty) {
queries ++= splitWithSemicolon(otherCodes.toSeq)
}
queries
} else {
splitWithSemicolon(allCode.toSeq).toSeq
}

// List of SQL queries to run
val queries = tempQueries.map(_.trim).filter(_ != "")
// Fix misplacement when comment is at the end of the query.
.map(_.split("\n").filterNot(_.startsWith("--")).mkString("\n")).map(_.trim).filter(_ != "")

val settingLines = comments.filter(_.startsWith("--SET ")).map(_.substring(6))
val settings = settingLines.flatMap(_.split(",").map { kv =>
val (conf, value) = kv.span(_ != '=')
conf.trim -> value.substring(1).trim
})

val regenerateGoldenFiles: Boolean = System.getenv("SPARK_GENERATE_GOLDEN_FILES") == "1"
if (regenerateGoldenFiles) {
runQueries(queries, testCase, settings)
} else {
// A config dimension has multiple config sets, and a config set has multiple configs.
// - config dim: Seq[Seq[(String, String)]]
// - config set: Seq[(String, String)]
// - config: (String, String))
// We need to do cartesian product for all the config dimensions, to get a list of
// config sets, and run the query once for each config set.
val configDimLines = comments.filter(_.startsWith("--CONFIG_DIM")).map(_.substring(12))
val configDims = configDimLines.groupBy(_.takeWhile(_ != ' ')).mapValues { lines =>
lines.map(_.dropWhile(_ != ' ').substring(1)).map(_.split(",").map { kv =>
val (conf, value) = kv.span(_ != '=')
conf.trim -> value.substring(1).trim
}.toSeq).toSeq
}

val configSets = configDims.values.foldLeft(Seq(Seq[(String, String)]())) { (res, dim) =>
dim.flatMap { configSet => res.map(_ ++ configSet) }
}

configSets.foreach { configSet =>
try {
runQueries(queries, testCase, settings ++ configSet)
} catch {
case e: Throwable =>
val configs = configSet.map {
case (k, v) => s"$k=$v"
}
logError(s"Error using configs: ${configs.mkString(",")}")
throw e
}
}
}
}

protected def runQueries(
queries: Seq[String],
testCase: TestCase,
Expand Down