-
Notifications
You must be signed in to change notification settings - Fork 28k
/
HiveResultSuite.scala
110 lines (100 loc) · 4.49 KB
/
HiveResultSuite.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
/*
* 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.execution
import org.apache.spark.sql.catalyst.util.DateTimeTestUtils
import org.apache.spark.sql.connector.InMemoryTableCatalog
import org.apache.spark.sql.execution.HiveResult._
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.test.{ExamplePoint, ExamplePointUDT, SharedSparkSession}
class HiveResultSuite extends SharedSparkSession {
import testImplicits._
test("date formatting in hive result") {
DateTimeTestUtils.outstandingTimezonesIds.foreach { zoneId =>
withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> zoneId) {
val dates = Seq("2018-12-28", "1582-10-03", "1582-10-04", "1582-10-15")
val df = dates.toDF("a").selectExpr("cast(a as date) as b")
val executedPlan1 = df.queryExecution.executedPlan
val result = hiveResultString(executedPlan1)
assert(result == dates)
val executedPlan2 = df.selectExpr("array(b)").queryExecution.executedPlan
val result2 = hiveResultString(executedPlan2)
assert(result2 == dates.map(x => s"[$x]"))
}
}
}
test("timestamp formatting in hive result") {
val timestamps = Seq(
"2018-12-28 01:02:03",
"1582-10-03 01:02:03",
"1582-10-04 01:02:03",
"1582-10-15 01:02:03")
val df = timestamps.toDF("a").selectExpr("cast(a as timestamp) as b")
val executedPlan1 = df.queryExecution.executedPlan
val result = hiveResultString(executedPlan1)
assert(result == timestamps)
val executedPlan2 = df.selectExpr("array(b)").queryExecution.executedPlan
val result2 = hiveResultString(executedPlan2)
assert(result2 == timestamps.map(x => s"[$x]"))
}
test("toHiveString correctly handles UDTs") {
val point = new ExamplePoint(50.0, 50.0)
val tpe = new ExamplePointUDT()
assert(toHiveString((point, tpe), false, getTimeFormatters) === "(50.0, 50.0)")
}
test("decimal formatting in hive result") {
val df = Seq(new java.math.BigDecimal("1")).toDS()
Seq(2, 6, 18).foreach { scala =>
val executedPlan =
df.selectExpr(s"CAST(value AS decimal(38, $scala))").queryExecution.executedPlan
val result = hiveResultString(executedPlan)
assert(result.head.split("\\.").last.length === scala)
}
val executedPlan = Seq(java.math.BigDecimal.ZERO).toDS()
.selectExpr(s"CAST(value AS decimal(38, 8))").queryExecution.executedPlan
val result = hiveResultString(executedPlan)
assert(result.head === "0.00000000")
}
test("SHOW TABLES in hive result") {
withSQLConf("spark.sql.catalog.testcat" -> classOf[InMemoryTableCatalog].getName) {
Seq(("testcat.ns", "tbl", "foo"), ("spark_catalog.default", "tbl", "csv")).foreach {
case (ns, tbl, source) =>
withTable(s"$ns.$tbl") {
spark.sql(s"CREATE TABLE $ns.$tbl (id bigint) USING $source")
val df = spark.sql(s"SHOW TABLES FROM $ns")
val executedPlan = df.queryExecution.executedPlan
assert(hiveResultString(executedPlan).head == tbl)
}
}
}
}
test("DESCRIBE TABLE in hive result") {
withSQLConf("spark.sql.catalog.testcat" -> classOf[InMemoryTableCatalog].getName) {
Seq(("testcat.ns", "tbl", "foo"), ("spark_catalog.default", "tbl", "csv")).foreach {
case (ns, tbl, source) =>
withTable(s"$ns.$tbl") {
spark.sql(s"CREATE TABLE $ns.$tbl (id bigint COMMENT 'col1') USING $source")
val df = spark.sql(s"DESCRIBE $ns.$tbl")
val executedPlan = df.queryExecution.executedPlan
val expected = "id " +
"\tbigint " +
"\tcol1 "
assert(hiveResultString(executedPlan).head == expected)
}
}
}
}
}