Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
beliefer committed Feb 12, 2020
1 parent a512664 commit 38005e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 6
-- Number of queries: 7


-- !query
Expand Down Expand Up @@ -99,3 +99,17 @@ Hoo boy. Still two deep...
Now just one deep...
*/
'deeply nested example' AS sixth


-- !query
/* and this is the end of the file */
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.catalyst.parser.ParseException

mismatched input '<EOF>' expecting {'(', 'ADD', 'ALTER', 'ANALYZE', 'CACHE', 'CLEAR', 'COMMENT', 'COMMIT', 'CREATE', 'DELETE', 'DESC', 'DESCRIBE', 'DFS', 'DROP', 'EXPLAIN', 'EXPORT', 'FROM', 'GRANT', 'IMPORT', 'INSERT', 'LIST', 'LOAD', 'LOCK', 'MAP', 'MERGE', 'MSCK', 'REDUCE', 'REFRESH', 'REPLACE', 'RESET', 'REVOKE', 'ROLLBACK', 'SELECT', 'SET', 'SHOW', 'START', 'TABLE', 'TRUNCATE', 'UNCACHE', 'UNLOCK', 'UPDATE', 'USE', 'VALUES', 'WITH'}(line 1, pos 37)

== SQL ==
/* and this is the end of the file */
-------------------------------------^^^
Original file line number Diff line number Diff line change
Expand Up @@ -271,33 +271,37 @@ class SQLQueryTestSuite extends QueryTest with SharedSparkSession {
}
}.flatten

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

// List of SQL queries to run
Expand Down

0 comments on commit 38005e8

Please sign in to comment.