/
ThriftServerQueryTestSuite.scala
360 lines (326 loc) · 13.2 KB
/
ThriftServerQueryTestSuite.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.sql.hive.thriftserver
import java.io.File
import java.sql.{DriverManager, SQLException, Statement, Timestamp}
import java.util.Locale
import scala.util.{Random, Try}
import scala.util.control.NonFatal
import org.apache.hadoop.hive.conf.HiveConf.ConfVars
import org.apache.hive.service.cli.HiveSQLException
import org.scalatest.Ignore
import org.apache.spark.sql.{AnalysisException, SQLQueryTestSuite}
import org.apache.spark.sql.catalyst.util.fileToString
import org.apache.spark.sql.execution.HiveResult
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types._
/**
* Re-run all the tests in SQLQueryTestSuite via Thrift Server.
* Note that this TestSuite does not support maven.
*
* TODO:
* 1. Support UDF testing.
* 2. Support DESC command.
* 3. Support SHOW command.
*/
@Ignore
class ThriftServerQueryTestSuite extends SQLQueryTestSuite {
private var hiveServer2: HiveThriftServer2 = _
override def beforeEach(): Unit = {
// Chooses a random port between 10000 and 19999
var listeningPort = 10000 + Random.nextInt(10000)
// Retries up to 3 times with different port numbers if the server fails to start
(1 to 3).foldLeft(Try(startThriftServer(listeningPort, 0))) { case (started, attempt) =>
started.orElse {
listeningPort += 1
Try(startThriftServer(listeningPort, attempt))
}
}.recover {
case cause: Throwable =>
throw cause
}.get
logInfo("HiveThriftServer2 started successfully")
}
override def afterEach(): Unit = {
hiveServer2.stop()
}
override val isTestWithConfigSets = false
/** List of test cases to ignore, in lower cases. */
override def blackList: Set[String] = Set(
"blacklist.sql", // Do NOT remove this one. It is here to test the blacklist functionality.
// Missing UDF
"pgSQL/boolean.sql",
"pgSQL/case.sql",
// SPARK-28624
"date.sql",
// SPARK-28619
"pgSQL/aggregates_part1.sql",
"group-by.sql",
// SPARK-28620
"pgSQL/float4.sql",
// SPARK-28636
"decimalArithmeticOperations.sql",
"literals.sql",
"subquery/scalar-subquery/scalar-subquery-predicate.sql",
"subquery/in-subquery/in-limit.sql",
"subquery/in-subquery/simple-in.sql",
"subquery/in-subquery/in-order-by.sql",
"subquery/in-subquery/in-set-operations.sql",
// SPARK-28637
"cast.sql",
"ansi/interval.sql"
)
override def runQueries(
queries: Seq[String],
testCase: TestCase,
configSet: Option[Seq[(String, String)]]): Unit = {
// We do not test with configSet.
withJdbcStatement { statement =>
loadTestData(statement)
testCase match {
case _: PgSQLTest =>
// PostgreSQL enabled cartesian product by default.
statement.execute(s"SET ${SQLConf.CROSS_JOINS_ENABLED.key} = true")
statement.execute(s"SET ${SQLConf.ANSI_SQL_PARSER.key} = true")
statement.execute(s"SET ${SQLConf.PREFER_INTEGRAL_DIVISION.key} = true")
case _ =>
}
// Run the SQL queries preparing them for comparison.
val outputs: Seq[QueryOutput] = queries.map { sql =>
val output = getNormalizedResult(statement, sql)
// We might need to do some query canonicalization in the future.
QueryOutput(
sql = sql,
schema = "",
output = output.mkString("\n").replaceAll("\\s+$", ""))
}
// Read back the golden file.
val expectedOutputs: Seq[QueryOutput] = {
val goldenOutput = fileToString(new File(testCase.resultFile))
val segments = goldenOutput.split("-- !query.+\n")
// each query has 3 segments, plus the header
assert(segments.size == outputs.size * 3 + 1,
s"Expected ${outputs.size * 3 + 1} blocks in result file but got ${segments.size}. " +
"Try regenerate the result files.")
Seq.tabulate(outputs.size) { i =>
val sql = segments(i * 3 + 1).trim
val originalOut = segments(i * 3 + 3)
val output = if (isNeedSort(sql)) {
originalOut.split("\n").sorted.mkString("\n")
} else {
originalOut
}
QueryOutput(
sql = sql,
schema = "",
output = output.replaceAll("\\s+$", "")
)
}
}
// Compare results.
assertResult(expectedOutputs.size, s"Number of queries should be ${expectedOutputs.size}") {
outputs.size
}
outputs.zip(expectedOutputs).zipWithIndex.foreach { case ((output, expected), i) =>
assertResult(expected.sql, s"SQL query did not match for query #$i\n${expected.sql}") {
output.sql
}
expected match {
// Skip desc command, see HiveResult.hiveResultString
case d if d.sql.toUpperCase(Locale.ROOT).startsWith("DESC ")
|| d.sql.toUpperCase(Locale.ROOT).startsWith("DESC\n")
|| d.sql.toUpperCase(Locale.ROOT).startsWith("DESCRIBE ")
|| d.sql.toUpperCase(Locale.ROOT).startsWith("DESCRIBE\n") =>
// Skip show command, see HiveResult.hiveResultString
case s if s.sql.toUpperCase(Locale.ROOT).startsWith("SHOW ")
|| s.sql.toUpperCase(Locale.ROOT).startsWith("SHOW\n") =>
// AnalysisException should exactly match.
// SQLException should not exactly match. We only assert the result contains Exception.
case _ if output.output.startsWith(classOf[SQLException].getName) =>
assert(expected.output.contains("Exception"),
s"Exception did not match for query #$i\n${expected.sql}, " +
s"expected: ${expected.output}, but got: ${output.output}")
// HiveSQLException is usually a feature that our ThriftServer cannot support.
// Please add SQL to blackList.
case _ if output.output.startsWith(classOf[HiveSQLException].getName) =>
assert(false, s"${output.output} for query #$i\n${expected.sql}")
case _ =>
assertResult(expected.output, s"Result did not match for query #$i\n${expected.sql}") {
output.output
}
}
}
}
}
override def createScalaTestCase(testCase: TestCase): Unit = {
if (blackList.exists(t =>
testCase.name.toLowerCase(Locale.ROOT).contains(t.toLowerCase(Locale.ROOT)))) {
// Create a test case to ignore this case.
ignore(testCase.name) { /* Do nothing */ }
} else {
// Create a test case to run this case.
test(testCase.name) {
runTest(testCase)
}
}
}
override def listTestCases(): Seq[TestCase] = {
listFilesRecursively(new File(inputFilePath)).flatMap { file =>
val resultFile = file.getAbsolutePath.replace(inputFilePath, goldenFilePath) + ".out"
val absPath = file.getAbsolutePath
val testCaseName = absPath.stripPrefix(inputFilePath).stripPrefix(File.separator)
if (file.getAbsolutePath.startsWith(s"$inputFilePath${File.separator}udf")) {
Seq.empty
} else if (file.getAbsolutePath.startsWith(s"$inputFilePath${File.separator}pgSQL")) {
PgSQLTestCase(testCaseName, absPath, resultFile) :: Nil
} else {
RegularTestCase(testCaseName, absPath, resultFile) :: Nil
}
}
}
test("Check if ThriftServer can work") {
withJdbcStatement { statement =>
val rs = statement.executeQuery("select 1L")
rs.next()
assert(rs.getLong(1) === 1L)
}
}
private def getNormalizedResult(statement: Statement, sql: String): Seq[String] = {
try {
val rs = statement.executeQuery(sql)
val cols = rs.getMetaData.getColumnCount
val buildStr = () => (for (i <- 1 to cols) yield {
getHiveResult(rs.getObject(i))
}).mkString("\t")
val answer = Iterator.continually(rs.next()).takeWhile(identity).map(_ => buildStr()).toSeq
.map(replaceNotIncludedMsg)
if (isNeedSort(sql)) {
answer.sorted
} else {
answer
}
} catch {
case a: AnalysisException =>
// Do not output the logical plan tree which contains expression IDs.
// Also implement a crude way of masking expression IDs in the error message
// with a generic pattern "###".
val msg = if (a.plan.nonEmpty) a.getSimpleMessage else a.getMessage
Seq(a.getClass.getName, msg.replaceAll("#\\d+", "#x")).sorted
case NonFatal(e) =>
// If there is an exception, put the exception class followed by the message.
Seq(e.getClass.getName, e.getMessage)
}
}
private def startThriftServer(port: Int, attempt: Int): Unit = {
logInfo(s"Trying to start HiveThriftServer2: port=$port, attempt=$attempt")
val sqlContext = spark.newSession().sqlContext
sqlContext.setConf(ConfVars.HIVE_SERVER2_THRIFT_PORT.varname, port.toString)
hiveServer2 = HiveThriftServer2.startWithContext(sqlContext)
}
private def withJdbcStatement(fs: (Statement => Unit)*) {
val user = System.getProperty("user.name")
val serverPort = hiveServer2.getHiveConf.get(ConfVars.HIVE_SERVER2_THRIFT_PORT.varname)
val connections =
fs.map { _ => DriverManager.getConnection(s"jdbc:hive2://localhost:$serverPort", user, "") }
val statements = connections.map(_.createStatement())
try {
statements.zip(fs).foreach { case (s, f) => f(s) }
} finally {
statements.foreach(_.close())
connections.foreach(_.close())
}
}
/** Load built-in test tables. */
private def loadTestData(statement: Statement): Unit = {
// Prepare the data
statement.execute(
"""
|CREATE OR REPLACE TEMPORARY VIEW testdata as
|SELECT id AS key, CAST(id AS string) AS value FROM range(1, 101)
""".stripMargin)
statement.execute(
"""
|CREATE OR REPLACE TEMPORARY VIEW arraydata as
|SELECT * FROM VALUES
|(ARRAY(1, 2, 3), ARRAY(ARRAY(1, 2, 3))),
|(ARRAY(2, 3, 4), ARRAY(ARRAY(2, 3, 4))) AS v(arraycol, nestedarraycol)
""".stripMargin)
statement.execute(
"""
|CREATE OR REPLACE TEMPORARY VIEW mapdata as
|SELECT * FROM VALUES
|MAP(1, 'a1', 2, 'b1', 3, 'c1', 4, 'd1', 5, 'e1'),
|MAP(1, 'a2', 2, 'b2', 3, 'c2', 4, 'd2'),
|MAP(1, 'a3', 2, 'b3', 3, 'c3'),
|MAP(1, 'a4', 2, 'b4'),
|MAP(1, 'a5') AS v(mapcol)
""".stripMargin)
statement.execute(
s"""
|CREATE TEMPORARY VIEW aggtest
| (a int, b float)
|USING csv
|OPTIONS (path '${baseResourcePath.getParent}/test-data/postgresql/agg.data',
| header 'false', delimiter '\t')
""".stripMargin)
statement.execute(
s"""
|CREATE OR REPLACE TEMPORARY VIEW onek
| (unique1 int, unique2 int, two int, four int, ten int, twenty int, hundred int,
| thousand int, twothousand int, fivethous int, tenthous int, odd int, even int,
| stringu1 string, stringu2 string, string4 string)
|USING csv
|OPTIONS (path '${baseResourcePath.getParent}/test-data/postgresql/onek.data',
| header 'false', delimiter '\t')
""".stripMargin)
statement.execute(
s"""
|CREATE OR REPLACE TEMPORARY VIEW tenk1
| (unique1 int, unique2 int, two int, four int, ten int, twenty int, hundred int,
| thousand int, twothousand int, fivethous int, tenthous int, odd int, even int,
| stringu1 string, stringu2 string, string4 string)
|USING csv
| OPTIONS (path '${baseResourcePath.getParent}/test-data/postgresql/tenk.data',
| header 'false', delimiter '\t')
""".stripMargin)
}
// Returns true if sql is retrieving data.
private def isNeedSort(sql: String): Boolean = {
val upperCase = sql.toUpperCase(Locale.ROOT)
upperCase.startsWith("SELECT ") || upperCase.startsWith("SELECT\n") ||
upperCase.startsWith("WITH ") || upperCase.startsWith("WITH\n") ||
upperCase.startsWith("VALUES ") || upperCase.startsWith("VALUES\n") ||
// pgSQL/union.sql
upperCase.startsWith("(")
}
private def getHiveResult(obj: Object): String = {
obj match {
case null =>
HiveResult.toHiveString((null, StringType))
case d: java.sql.Date =>
HiveResult.toHiveString((d, DateType))
case t: Timestamp =>
HiveResult.toHiveString((t, TimestampType))
case d: java.math.BigDecimal =>
HiveResult.toHiveString((d, DecimalType.fromBigDecimal(d)))
case bin: Array[Byte] =>
HiveResult.toHiveString((bin, BinaryType))
case other =>
other.toString
}
}
}