forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 2
/
HiveThriftServer2Suites.scala
1380 lines (1151 loc) · 47.7 KB
/
HiveThriftServer2Suites.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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, FilenameFilter}
import java.net.URL
import java.nio.charset.StandardCharsets
import java.sql.{Date, DriverManager, SQLException, Statement}
import java.util.{Locale, UUID}
import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.{ExecutionContext, Future, Promise}
import scala.concurrent.duration._
import scala.io.Source
import scala.util.{Random, Try}
import com.google.common.io.Files
import org.apache.hadoop.hive.conf.HiveConf.ConfVars
import org.apache.hive.jdbc.HiveDriver
import org.apache.hive.service.auth.PlainSaslHelper
import org.apache.hive.service.cli.{FetchOrientation, FetchType, GetInfoType, RowSet}
import org.apache.hive.service.cli.thrift.ThriftCLIServiceClient
import org.apache.thrift.protocol.TBinaryProtocol
import org.apache.thrift.transport.TSocket
import org.scalatest.BeforeAndAfterAll
import org.apache.spark.{SparkException, SparkFunSuite}
import org.apache.spark.internal.Logging
import org.apache.spark.sql.hive.HiveUtils
import org.apache.spark.sql.hive.test.HiveTestJars
import org.apache.spark.sql.internal.StaticSQLConf.HIVE_THRIFT_SERVER_SINGLESESSION
import org.apache.spark.sql.test.ProcessTestUtils.ProcessOutputCapturer
import org.apache.spark.util.{ThreadUtils, Utils}
object TestData {
def getTestDataFilePath(name: String): URL = {
Thread.currentThread().getContextClassLoader.getResource(s"data/files/$name")
}
val smallKv = getTestDataFilePath("small_kv.txt")
val smallKvWithNull = getTestDataFilePath("small_kv_with_null.txt")
}
class HiveThriftBinaryServerSuite extends HiveThriftJdbcTest {
override def mode: ServerMode.Value = ServerMode.binary
private def withCLIServiceClient(f: ThriftCLIServiceClient => Unit): Unit = {
// Transport creation logic below mimics HiveConnection.createBinaryTransport
val rawTransport = new TSocket("localhost", serverPort)
val user = System.getProperty("user.name")
val transport = PlainSaslHelper.getPlainTransport(user, "anonymous", rawTransport)
val protocol = new TBinaryProtocol(transport)
val client = new ThriftCLIServiceClient(new ThriftserverShimUtils.Client(protocol))
transport.open()
try f(client) finally transport.close()
}
test("GetInfo Thrift API") {
withCLIServiceClient { client =>
val user = System.getProperty("user.name")
val sessionHandle = client.openSession(user, "")
assertResult("Spark SQL", "Wrong GetInfo(CLI_DBMS_NAME) result") {
client.getInfo(sessionHandle, GetInfoType.CLI_DBMS_NAME).getStringValue
}
assertResult("Spark SQL", "Wrong GetInfo(CLI_SERVER_NAME) result") {
client.getInfo(sessionHandle, GetInfoType.CLI_SERVER_NAME).getStringValue
}
assertResult(true, "Spark version shouldn't be \"Unknown\"") {
val version = client.getInfo(sessionHandle, GetInfoType.CLI_DBMS_VER).getStringValue
logInfo(s"Spark version: $version")
version != "Unknown"
}
}
}
test("SPARK-16563 ThriftCLIService FetchResults repeat fetching result") {
withCLIServiceClient { client =>
val user = System.getProperty("user.name")
val sessionHandle = client.openSession(user, "")
withJdbcStatement("test_16563") { statement =>
val queries = Seq(
"CREATE TABLE test_16563(key INT, val STRING) USING hive",
s"LOAD DATA LOCAL INPATH '${TestData.smallKv}' OVERWRITE INTO TABLE test_16563")
queries.foreach(statement.execute)
val confOverlay = new java.util.HashMap[java.lang.String, java.lang.String]
val operationHandle = client.executeStatement(
sessionHandle,
"SELECT * FROM test_16563",
confOverlay)
// Fetch result first time
assertResult(5, "Fetching result first time from next row") {
val rows_next = client.fetchResults(
operationHandle,
FetchOrientation.FETCH_NEXT,
1000,
FetchType.QUERY_OUTPUT)
rows_next.numRows()
}
// Fetch result second time from first row
assertResult(5, "Repeat fetching result from first row") {
val rows_first = client.fetchResults(
operationHandle,
FetchOrientation.FETCH_FIRST,
1000,
FetchType.QUERY_OUTPUT)
rows_first.numRows()
}
}
}
}
test("Support beeline --hiveconf and --hivevar") {
withJdbcStatement() { statement =>
executeTest(hiveConfList)
executeTest(hiveVarList)
def executeTest(hiveList: String): Unit = {
hiveList.split(";").foreach{ m =>
val kv = m.split("=")
val k = kv(0)
val v = kv(1)
val modValue = s"${v}_MOD_VALUE"
// select '${a}'; ---> avalue
val resultSet = statement.executeQuery(s"select '$${$k}'")
resultSet.next()
assert(resultSet.getString(1) === v)
statement.executeQuery(s"set $k=$modValue")
val modResultSet = statement.executeQuery(s"select '$${$k}'")
modResultSet.next()
assert(modResultSet.getString(1) === s"$modValue")
}
}
}
}
test("JDBC query execution") {
withJdbcStatement("test") { statement =>
val queries = Seq(
"SET spark.sql.shuffle.partitions=3",
"CREATE TABLE test(key INT, val STRING) USING hive",
s"LOAD DATA LOCAL INPATH '${TestData.smallKv}' OVERWRITE INTO TABLE test",
"CACHE TABLE test")
queries.foreach(statement.execute)
assertResult(5, "Row count mismatch") {
val resultSet = statement.executeQuery("SELECT COUNT(*) FROM test")
resultSet.next()
resultSet.getInt(1)
}
}
}
test("Checks Hive version") {
withJdbcStatement() { statement =>
val resultSet = statement.executeQuery("SET spark.sql.hive.version")
resultSet.next()
assert(resultSet.getString(1) === "spark.sql.hive.version")
assert(resultSet.getString(2) === HiveUtils.builtinHiveVersion)
}
}
test("SPARK-3004 regression: result set containing NULL") {
withJdbcStatement("test_null") { statement =>
val queries = Seq(
"CREATE TABLE test_null(key INT, val STRING) USING hive",
s"LOAD DATA LOCAL INPATH '${TestData.smallKvWithNull}' OVERWRITE INTO TABLE test_null")
queries.foreach(statement.execute)
val resultSet = statement.executeQuery("SELECT * FROM test_null WHERE key IS NULL")
(0 until 5).foreach { _ =>
resultSet.next()
assert(resultSet.getInt(1) === 0)
assert(resultSet.wasNull())
}
assert(!resultSet.next())
}
}
test("SPARK-4292 regression: result set iterator issue") {
withJdbcStatement("test_4292") { statement =>
val queries = Seq(
"CREATE TABLE test_4292(key INT, val STRING) USING hive",
s"LOAD DATA LOCAL INPATH '${TestData.smallKv}' OVERWRITE INTO TABLE test_4292")
queries.foreach(statement.execute)
val resultSet = statement.executeQuery("SELECT key FROM test_4292")
Seq(238, 86, 311, 27, 165).foreach { key =>
resultSet.next()
assert(resultSet.getInt(1) === key)
}
}
}
test("SPARK-4309 regression: Date type support") {
withJdbcStatement("test_date") { statement =>
val queries = Seq(
"CREATE TABLE test_date(key INT, value STRING) USING hive",
s"LOAD DATA LOCAL INPATH '${TestData.smallKv}' OVERWRITE INTO TABLE test_date")
queries.foreach(statement.execute)
assertResult(Date.valueOf("2011-01-01")) {
val resultSet = statement.executeQuery(
"SELECT CAST('2011-01-01' as date) FROM test_date LIMIT 1")
resultSet.next()
resultSet.getDate(1)
}
}
}
test("SPARK-4407 regression: Complex type support") {
withJdbcStatement("test_map") { statement =>
val queries = Seq(
"CREATE TABLE test_map(key INT, value STRING) USING hive",
s"LOAD DATA LOCAL INPATH '${TestData.smallKv}' OVERWRITE INTO TABLE test_map")
queries.foreach(statement.execute)
assertResult("""{238:"val_238"}""") {
val resultSet = statement.executeQuery("SELECT MAP(key, value) FROM test_map LIMIT 1")
resultSet.next()
resultSet.getString(1)
}
assertResult("""["238","val_238"]""") {
val resultSet = statement.executeQuery(
"SELECT ARRAY(CAST(key AS STRING), value) FROM test_map LIMIT 1")
resultSet.next()
resultSet.getString(1)
}
}
}
test("SPARK-12143 regression: Binary type support") {
withJdbcStatement("test_binary") { statement =>
val queries = Seq(
"CREATE TABLE test_binary(key INT, value STRING) USING hive",
s"LOAD DATA LOCAL INPATH '${TestData.smallKv}' OVERWRITE INTO TABLE test_binary")
queries.foreach(statement.execute)
val expected: Array[Byte] = "val_238".getBytes
assertResult(expected) {
val resultSet = statement.executeQuery(
"SELECT CAST(value as BINARY) FROM test_binary LIMIT 1")
resultSet.next()
resultSet.getObject(1)
}
}
}
test("test multiple session") {
import org.apache.spark.sql.internal.SQLConf
var defaultV1: String = null
var defaultV2: String = null
var data: ArrayBuffer[Int] = null
withMultipleConnectionJdbcStatement("test_map", "db1.test_map2")(
// create table
{ statement =>
val queries = Seq(
"CREATE TABLE test_map(key INT, value STRING) USING hive",
s"LOAD DATA LOCAL INPATH '${TestData.smallKv}' OVERWRITE INTO TABLE test_map",
"CACHE TABLE test_table AS SELECT key FROM test_map ORDER BY key DESC",
"CREATE DATABASE db1")
queries.foreach(statement.execute)
val plan = statement.executeQuery("explain select * from test_table")
plan.next()
plan.next()
assert(plan.getString(1).contains("Scan In-memory table `test_table`"))
val rs1 = statement.executeQuery("SELECT key FROM test_table ORDER BY KEY DESC")
val buf1 = new collection.mutable.ArrayBuffer[Int]()
while (rs1.next()) {
buf1 += rs1.getInt(1)
}
rs1.close()
val rs2 = statement.executeQuery("SELECT key FROM test_map ORDER BY KEY DESC")
val buf2 = new collection.mutable.ArrayBuffer[Int]()
while (rs2.next()) {
buf2 += rs2.getInt(1)
}
rs2.close()
assert(buf1 === buf2)
data = buf1
},
// first session, we get the default value of the session status
{ statement =>
val rs1 = statement.executeQuery(s"SET ${SQLConf.SHUFFLE_PARTITIONS.key}")
rs1.next()
defaultV1 = rs1.getString(1)
assert(defaultV1 != "200")
rs1.close()
val rs2 = statement.executeQuery("SET hive.cli.print.header")
rs2.next()
defaultV2 = rs2.getString(1)
assert(defaultV1 != "true")
rs2.close()
},
// second session, we update the session status
{ statement =>
val queries = Seq(
s"SET ${SQLConf.SHUFFLE_PARTITIONS.key}=291",
"SET hive.cli.print.header=true"
)
queries.map(statement.execute)
val rs1 = statement.executeQuery(s"SET ${SQLConf.SHUFFLE_PARTITIONS.key}")
rs1.next()
assert("spark.sql.shuffle.partitions" === rs1.getString(1))
assert("291" === rs1.getString(2))
rs1.close()
val rs2 = statement.executeQuery("SET hive.cli.print.header")
rs2.next()
assert("hive.cli.print.header" === rs2.getString(1))
assert("true" === rs2.getString(2))
rs2.close()
},
// third session, we get the latest session status, supposed to be the
// default value
{ statement =>
val rs1 = statement.executeQuery(s"SET ${SQLConf.SHUFFLE_PARTITIONS.key}")
rs1.next()
assert(defaultV1 === rs1.getString(1))
rs1.close()
val rs2 = statement.executeQuery("SET hive.cli.print.header")
rs2.next()
assert(defaultV2 === rs2.getString(1))
rs2.close()
},
// try to access the cached data in another session
{ statement =>
// Cached temporary table can't be accessed by other sessions
intercept[SQLException] {
statement.executeQuery("SELECT key FROM test_table ORDER BY KEY DESC")
}
val plan = statement.executeQuery("explain select key from test_map ORDER BY key DESC")
plan.next()
plan.next()
assert(plan.getString(1).contains("Scan In-memory table `test_table`"))
val rs = statement.executeQuery("SELECT key FROM test_map ORDER BY KEY DESC")
val buf = new collection.mutable.ArrayBuffer[Int]()
while (rs.next()) {
buf += rs.getInt(1)
}
rs.close()
assert(buf === data)
},
// switch another database
{ statement =>
statement.execute("USE db1")
// there is no test_map table in db1
intercept[SQLException] {
statement.executeQuery("SELECT key FROM test_map ORDER BY KEY DESC")
}
statement.execute("CREATE TABLE test_map2(key INT, value STRING)")
},
// access default database
{ statement =>
// current database should still be `default`
intercept[SQLException] {
statement.executeQuery("SELECT key FROM test_map2")
}
statement.execute("USE db1")
// access test_map2
statement.executeQuery("SELECT key from test_map2")
}
)
}
// This test often hangs and then times out, leaving the hanging processes.
// Let's ignore it and improve the test.
ignore("test jdbc cancel") {
withJdbcStatement("test_map") { statement =>
val queries = Seq(
"CREATE TABLE test_map(key INT, value STRING)",
s"LOAD DATA LOCAL INPATH '${TestData.smallKv}' OVERWRITE INTO TABLE test_map")
queries.foreach(statement.execute)
implicit val ec = ExecutionContext.fromExecutorService(
ThreadUtils.newDaemonSingleThreadExecutor("test-jdbc-cancel"))
try {
// Start a very-long-running query that will take hours to finish, then cancel it in order
// to demonstrate that cancellation works.
val f = Future {
statement.executeQuery(
"SELECT COUNT(*) FROM test_map " +
List.fill(10)("join test_map").mkString(" "))
}
// Note that this is slightly race-prone: if the cancel is issued before the statement
// begins executing then we'll fail with a timeout. As a result, this fixed delay is set
// slightly more conservatively than may be strictly necessary.
Thread.sleep(1000)
statement.cancel()
val e = intercept[SparkException] {
ThreadUtils.awaitResult(f, 3.minute)
}.getCause
assert(e.isInstanceOf[SQLException])
assert(e.getMessage.contains("cancelled"))
// Cancellation is a no-op if spark.sql.hive.thriftServer.async=false
statement.executeQuery("SET spark.sql.hive.thriftServer.async=false")
try {
val sf = Future {
statement.executeQuery(
"SELECT COUNT(*) FROM test_map " +
List.fill(4)("join test_map").mkString(" ")
)
}
// Similarly, this is also slightly race-prone on fast machines where the query above
// might race and complete before we issue the cancel.
Thread.sleep(1000)
statement.cancel()
val rs1 = ThreadUtils.awaitResult(sf, 3.minute)
rs1.next()
assert(rs1.getInt(1) === math.pow(5, 5))
rs1.close()
val rs2 = statement.executeQuery("SELECT COUNT(*) FROM test_map")
rs2.next()
assert(rs2.getInt(1) === 5)
rs2.close()
} finally {
statement.executeQuery("SET spark.sql.hive.thriftServer.async=true")
}
} finally {
ec.shutdownNow()
}
}
}
test("test add jar") {
withMultipleConnectionJdbcStatement("smallKV", "addJar")(
{
statement =>
val jarFile = HiveTestJars.getHiveHcatalogCoreJar().getCanonicalPath
statement.executeQuery(s"ADD JAR $jarFile")
},
{
statement =>
val queries = Seq(
"CREATE TABLE smallKV(key INT, val STRING) USING hive",
s"LOAD DATA LOCAL INPATH '${TestData.smallKv}' OVERWRITE INTO TABLE smallKV",
"""CREATE TABLE addJar(key string)
|ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe'
""".stripMargin)
queries.foreach(statement.execute)
statement.executeQuery(
"""
|INSERT INTO TABLE addJar SELECT 'k1' as key FROM smallKV limit 1
""".stripMargin)
val actualResult =
statement.executeQuery("SELECT key FROM addJar")
val actualResultBuffer = new collection.mutable.ArrayBuffer[String]()
while (actualResult.next()) {
actualResultBuffer += actualResult.getString(1)
}
actualResult.close()
val expectedResult =
statement.executeQuery("SELECT 'k1'")
val expectedResultBuffer = new collection.mutable.ArrayBuffer[String]()
while (expectedResult.next()) {
expectedResultBuffer += expectedResult.getString(1)
}
expectedResult.close()
assert(expectedResultBuffer === actualResultBuffer)
}
)
}
test("Checks Hive version via SET -v") {
withJdbcStatement() { statement =>
val resultSet = statement.executeQuery("SET -v")
val conf = mutable.Map.empty[String, String]
while (resultSet.next()) {
conf += resultSet.getString(1) -> resultSet.getString(2)
}
if (HiveUtils.isHive23) {
assert(conf.get(HiveUtils.FAKE_HIVE_VERSION.key) === Some("2.3.7"))
} else {
assert(conf.get(HiveUtils.FAKE_HIVE_VERSION.key) === Some("1.2.1"))
}
}
}
test("Checks Hive version via SET") {
withJdbcStatement() { statement =>
val resultSet = statement.executeQuery("SET")
val conf = mutable.Map.empty[String, String]
while (resultSet.next()) {
conf += resultSet.getString(1) -> resultSet.getString(2)
}
if (HiveUtils.isHive23) {
assert(conf.get(HiveUtils.FAKE_HIVE_VERSION.key) === Some("2.3.7"))
} else {
assert(conf.get(HiveUtils.FAKE_HIVE_VERSION.key) === Some("1.2.1"))
}
}
}
test("SPARK-11595 ADD JAR with input path having URL scheme") {
withJdbcStatement("test_udtf") { statement =>
try {
val jarPath = "../hive/src/test/resources/TestUDTF.jar"
val jarURL = s"file://${System.getProperty("user.dir")}/$jarPath"
Seq(
s"ADD JAR $jarURL",
s"""CREATE TEMPORARY FUNCTION udtf_count2
|AS 'org.apache.spark.sql.hive.execution.GenericUDTFCount2'
""".stripMargin
).foreach(statement.execute)
val rs1 = statement.executeQuery("DESCRIBE FUNCTION udtf_count2")
assert(rs1.next())
assert(rs1.getString(1) === "Function: udtf_count2")
assert(rs1.next())
assertResult("Class: org.apache.spark.sql.hive.execution.GenericUDTFCount2") {
rs1.getString(1)
}
assert(rs1.next())
assert(rs1.getString(1) === "Usage: N/A.")
val dataPath = "../hive/src/test/resources/data/files/kv1.txt"
Seq(
"CREATE TABLE test_udtf(key INT, value STRING) USING hive",
s"LOAD DATA LOCAL INPATH '$dataPath' OVERWRITE INTO TABLE test_udtf"
).foreach(statement.execute)
val rs2 = statement.executeQuery(
"SELECT key, cc FROM test_udtf LATERAL VIEW udtf_count2(value) dd AS cc")
assert(rs2.next())
assert(rs2.getInt(1) === 97)
assert(rs2.getInt(2) === 500)
assert(rs2.next())
assert(rs2.getInt(1) === 97)
assert(rs2.getInt(2) === 500)
} finally {
statement.executeQuery("DROP TEMPORARY FUNCTION udtf_count2")
}
}
}
test("SPARK-11043 check operation log root directory") {
val expectedLine =
"Operation log root directory is created: " + operationLogPath.getAbsoluteFile
val bufferSrc = Source.fromFile(logPath)
Utils.tryWithSafeFinally {
assert(bufferSrc.getLines().exists(_.contains(expectedLine)))
} {
bufferSrc.close()
}
}
test("SPARK-23547 Cleanup the .pipeout file when the Hive Session closed") {
def pipeoutFileList(sessionID: UUID): Array[File] = {
lScratchDir.listFiles(new FilenameFilter {
override def accept(dir: File, name: String): Boolean = {
name.startsWith(sessionID.toString) && name.endsWith(".pipeout")
}
})
}
withCLIServiceClient { client =>
val user = System.getProperty("user.name")
val sessionHandle = client.openSession(user, "")
val sessionID = sessionHandle.getSessionId
if (HiveUtils.isHive23) {
assert(pipeoutFileList(sessionID).length == 2)
} else {
assert(pipeoutFileList(sessionID).length == 1)
}
client.closeSession(sessionHandle)
assert(pipeoutFileList(sessionID).length == 0)
}
}
test("SPARK-24829 Checks cast as float") {
withJdbcStatement() { statement =>
val resultSet = statement.executeQuery("SELECT CAST('4.56' AS FLOAT)")
resultSet.next()
assert(resultSet.getString(1) === "4.56")
}
}
test("SPARK-28463: Thriftserver throws BigDecimal incompatible with HiveDecimal") {
withJdbcStatement() { statement =>
val rs = statement.executeQuery("SELECT CAST(1 AS decimal(38, 18))")
assert(rs.next())
assert(rs.getBigDecimal(1) === new java.math.BigDecimal("1.000000000000000000"))
}
}
test("Support interval type") {
withJdbcStatement() { statement =>
val rs = statement.executeQuery("SELECT interval 3 months 1 hours")
assert(rs.next())
assert(rs.getString(1) === "3 months 1 hours")
}
// Invalid interval value
withJdbcStatement() { statement =>
val e = intercept[SQLException] {
statement.executeQuery("SELECT interval 3 months 1 hou")
}
assert(e.getMessage.contains("org.apache.spark.sql.catalyst.parser.ParseException"))
}
}
test("ThriftCLIService FetchResults FETCH_FIRST, FETCH_NEXT, FETCH_PRIOR") {
def checkResult(rows: RowSet, start: Long, end: Long): Unit = {
assert(rows.getStartOffset() == start)
assert(rows.numRows() == end - start)
rows.iterator.asScala.zip((start until end).iterator).foreach { case (row, v) =>
assert(row(0).asInstanceOf[Long] === v)
}
}
withCLIServiceClient { client =>
val user = System.getProperty("user.name")
val sessionHandle = client.openSession(user, "")
val confOverlay = new java.util.HashMap[java.lang.String, java.lang.String]
val operationHandle = client.executeStatement(
sessionHandle,
"SELECT * FROM range(10)",
confOverlay) // 10 rows result with sequence 0, 1, 2, ..., 9
var rows: RowSet = null
// Fetch 5 rows with FETCH_NEXT
rows = client.fetchResults(
operationHandle, FetchOrientation.FETCH_NEXT, 5, FetchType.QUERY_OUTPUT)
checkResult(rows, 0, 5) // fetched [0, 5)
// Fetch another 2 rows with FETCH_NEXT
rows = client.fetchResults(
operationHandle, FetchOrientation.FETCH_NEXT, 2, FetchType.QUERY_OUTPUT)
checkResult(rows, 5, 7) // fetched [5, 7)
// FETCH_PRIOR 3 rows
rows = client.fetchResults(
operationHandle, FetchOrientation.FETCH_PRIOR, 3, FetchType.QUERY_OUTPUT)
checkResult(rows, 2, 5) // fetched [2, 5)
// FETCH_PRIOR again will scroll back to 0, and then the returned result
// may overlap the results of previous FETCH_PRIOR
rows = client.fetchResults(
operationHandle, FetchOrientation.FETCH_PRIOR, 3, FetchType.QUERY_OUTPUT)
checkResult(rows, 0, 3) // fetched [0, 3)
// FETCH_PRIOR again will stay at 0
rows = client.fetchResults(
operationHandle, FetchOrientation.FETCH_PRIOR, 4, FetchType.QUERY_OUTPUT)
checkResult(rows, 0, 4) // fetched [0, 4)
// FETCH_NEXT will continue moving forward from offset 4
rows = client.fetchResults(
operationHandle, FetchOrientation.FETCH_NEXT, 10, FetchType.QUERY_OUTPUT)
checkResult(rows, 4, 10) // fetched [4, 10) until the end of results
// FETCH_NEXT is at end of results
rows = client.fetchResults(
operationHandle, FetchOrientation.FETCH_NEXT, 5, FetchType.QUERY_OUTPUT)
checkResult(rows, 10, 10) // fetched empty [10, 10) (at end of results)
// FETCH_NEXT is at end of results again
rows = client.fetchResults(
operationHandle, FetchOrientation.FETCH_NEXT, 2, FetchType.QUERY_OUTPUT)
checkResult(rows, 10, 10) // fetched empty [10, 10) (at end of results)
// FETCH_PRIOR 1 rows yet again
rows = client.fetchResults(
operationHandle, FetchOrientation.FETCH_PRIOR, 1, FetchType.QUERY_OUTPUT)
checkResult(rows, 9, 10) // fetched [9, 10)
// FETCH_NEXT will return 0 yet again
rows = client.fetchResults(
operationHandle, FetchOrientation.FETCH_NEXT, 5, FetchType.QUERY_OUTPUT)
checkResult(rows, 10, 10) // fetched empty [10, 10) (at end of results)
// FETCH_FIRST results from first row
rows = client.fetchResults(
operationHandle, FetchOrientation.FETCH_FIRST, 3, FetchType.QUERY_OUTPUT)
checkResult(rows, 0, 3) // fetch [0, 3)
// Fetch till the end rows with FETCH_NEXT"
rows = client.fetchResults(
operationHandle, FetchOrientation.FETCH_NEXT, 1000, FetchType.QUERY_OUTPUT)
checkResult(rows, 3, 10) // fetched [3, 10)
client.closeOperation(operationHandle)
client.closeSession(sessionHandle)
}
}
test("SPARK-29492: use add jar in sync mode") {
withCLIServiceClient { client =>
val user = System.getProperty("user.name")
val sessionHandle = client.openSession(user, "")
withJdbcStatement("smallKV", "addJar") { statement =>
val confOverlay = new java.util.HashMap[java.lang.String, java.lang.String]
val jarFile = HiveTestJars.getHiveHcatalogCoreJar().getCanonicalPath
Seq(s"ADD JAR $jarFile",
"CREATE TABLE smallKV(key INT, val STRING) USING hive",
s"LOAD DATA LOCAL INPATH '${TestData.smallKv}' OVERWRITE INTO TABLE smallKV")
.foreach(query => client.executeStatement(sessionHandle, query, confOverlay))
client.executeStatement(sessionHandle,
"""CREATE TABLE addJar(key string)
|ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe'
""".stripMargin, confOverlay)
client.executeStatement(sessionHandle,
"INSERT INTO TABLE addJar SELECT 'k1' as key FROM smallKV limit 1", confOverlay)
val operationHandle = client.executeStatement(
sessionHandle,
"SELECT key FROM addJar",
confOverlay)
// Fetch result first time
assertResult(1, "Fetching result first time from next row") {
val rows_next = client.fetchResults(
operationHandle,
FetchOrientation.FETCH_NEXT,
1000,
FetchType.QUERY_OUTPUT)
rows_next.numRows()
}
}
}
}
test("SPARK-31859 Thriftserver works with spark.sql.datetime.java8API.enabled=true") {
withJdbcStatement() { st =>
st.execute("set spark.sql.datetime.java8API.enabled=true")
val rs = st.executeQuery("select date '2020-05-28', timestamp '2020-05-28 00:00:00'")
rs.next()
assert(rs.getDate(1).toString() == "2020-05-28")
assert(rs.getTimestamp(2).toString() == "2020-05-28 00:00:00.0")
}
}
test("SPARK-31861 Thriftserver respects spark.sql.session.timeZone") {
withJdbcStatement() { st =>
st.execute("set spark.sql.session.timeZone=+03:15") // different than Thriftserver's JVM tz
val rs = st.executeQuery("select timestamp '2020-05-28 10:00:00'")
rs.next()
// The timestamp as string is the same as the literal
assert(rs.getString(1) == "2020-05-28 10:00:00.0")
// Parsing it to java.sql.Timestamp in the client will always result in a timestamp
// in client default JVM timezone. The string value of the Timestamp will match the literal,
// but if the JDBC application cares about the internal timezone and UTC offset of the
// Timestamp object, it should set spark.sql.session.timeZone to match its client JVM tz.
assert(rs.getTimestamp(1).toString() == "2020-05-28 10:00:00.0")
}
}
test("SPARK-31863 Session conf should persist between Thriftserver worker threads") {
val iter = 20
withJdbcStatement() { statement =>
// date 'now' is resolved during parsing, and relies on SQLConf.get to
// obtain the current set timezone. We exploit this to run this test.
// If the timezones are set correctly to 25 hours apart across threads,
// the dates should reflect this.
// iterate a few times for the odd chance the same thread is selected
for (_ <- 0 until iter) {
statement.execute("SET spark.sql.session.timeZone=GMT-12")
val firstResult = statement.executeQuery("SELECT date 'now'")
firstResult.next()
val beyondDateLineWest = firstResult.getDate(1)
statement.execute("SET spark.sql.session.timeZone=GMT+13")
val secondResult = statement.executeQuery("SELECT date 'now'")
secondResult.next()
val dateLineEast = secondResult.getDate(1)
assert(
dateLineEast after beyondDateLineWest,
"SQLConf changes should persist across execution threads")
}
}
}
test("SPARK-30808: use Java 8 time API and Proleptic Gregorian calendar by default") {
withJdbcStatement() { st =>
// Proleptic Gregorian calendar has no gap in the range 1582-10-04..1582-10-15
val date = "1582-10-10"
val rs = st.executeQuery(s"select date '$date'")
rs.next()
val expected = java.sql.Date.valueOf(date)
assert(rs.getDate(1) === expected)
assert(rs.getString(1) === expected.toString)
}
}
test("SPARK-26533: Support query auto timeout cancel on thriftserver") {
withJdbcStatement() { statement =>
if (HiveUtils.isHive23) {
statement.setQueryTimeout(1)
val e = intercept[SQLException] {
statement.execute("select java_method('java.lang.Thread', 'sleep', 3000L)")
}.getMessage
assert(e.contains("Query timed out after"))
statement.setQueryTimeout(0)
val rs1 = statement.executeQuery(
"select 'test', java_method('java.lang.Thread', 'sleep', 3000L)")
rs1.next()
assert(rs1.getString(1) == "test")
statement.setQueryTimeout(-1)
val rs2 = statement.executeQuery(
"select 'test', java_method('java.lang.Thread', 'sleep', 3000L)")
rs2.next()
assert(rs2.getString(1) == "test")
} else {
val e = intercept[SQLException] {
statement.setQueryTimeout(1)
}.getMessage
assert(e.contains("Method not supported"))
}
}
}
}
class SingleSessionSuite extends HiveThriftJdbcTest {
override def mode: ServerMode.Value = ServerMode.binary
override protected def extraConf: Seq[String] =
s"--conf ${HIVE_THRIFT_SERVER_SINGLESESSION.key}=true" :: Nil
test("share the temporary functions across JDBC connections") {
withMultipleConnectionJdbcStatement("test_udtf")(
{ statement =>
val jarPath = "../hive/src/test/resources/TestUDTF.jar"
val jarURL = s"file://${System.getProperty("user.dir")}/$jarPath"
// Configurations and temporary functions added in this session should be visible to all
// the other sessions.
Seq(
"SET foo=bar",
s"ADD JAR $jarURL",
"CREATE TABLE test_udtf(key INT, value STRING) USING hive",
s"LOAD DATA LOCAL INPATH '${TestData.smallKv}' OVERWRITE INTO TABLE test_udtf",
s"""CREATE TEMPORARY FUNCTION udtf_count2
|AS 'org.apache.spark.sql.hive.execution.GenericUDTFCount2'
""".stripMargin
).foreach(statement.execute)
},
{ statement =>
try {
val rs1 = statement.executeQuery("SET foo")
assert(rs1.next())
assert(rs1.getString(1) === "foo")
assert(rs1.getString(2) === "bar")
val rs2 = statement.executeQuery("DESCRIBE FUNCTION udtf_count2")
assert(rs2.next())
assert(rs2.getString(1) === "Function: udtf_count2")
assert(rs2.next())
assertResult("Class: org.apache.spark.sql.hive.execution.GenericUDTFCount2") {
rs2.getString(1)
}
assert(rs2.next())
assert(rs2.getString(1) === "Usage: N/A.")
val rs3 = statement.executeQuery(
"SELECT key, cc FROM test_udtf LATERAL VIEW udtf_count2(value) dd AS cc")
assert(rs3.next())
assert(rs3.getInt(1) === 165)
assert(rs3.getInt(2) === 5)
assert(rs3.next())
assert(rs3.getInt(1) === 165)
assert(rs3.getInt(2) === 5)
} finally {
statement.executeQuery("DROP TEMPORARY FUNCTION udtf_count2")
}
}
)
}
test("unable to changing spark.sql.hive.thriftServer.singleSession using JDBC connections") {
withJdbcStatement() { statement =>
// JDBC connections are not able to set the conf spark.sql.hive.thriftServer.singleSession
val e = intercept[SQLException] {
statement.executeQuery("SET spark.sql.hive.thriftServer.singleSession=false")
}.getMessage
assert(e.contains(
"Cannot modify the value of a static config: spark.sql.hive.thriftServer.singleSession"))
}
}
test("share the current database and temporary tables across JDBC connections") {
withMultipleConnectionJdbcStatement()(
{ statement =>
statement.execute("CREATE DATABASE IF NOT EXISTS db1")
},
{ statement =>
val rs1 = statement.executeQuery("SELECT current_database()")
assert(rs1.next())
assert(rs1.getString(1) === "default")
statement.execute("USE db1")
val rs2 = statement.executeQuery("SELECT current_database()")
assert(rs2.next())
assert(rs2.getString(1) === "db1")
statement.execute("CREATE TEMP VIEW tempView AS SELECT 123")
},