/
HiveOrcQuerySuite.scala
318 lines (281 loc) · 12 KB
/
HiveOrcQuerySuite.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
/*
* 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.orc
import java.io.File
import com.google.common.io.Files
import org.apache.spark.sql.{AnalysisException, Row}
import org.apache.spark.sql.catalyst.TableIdentifier
import org.apache.spark.sql.catalyst.catalog.HiveTableRelation
import org.apache.spark.sql.execution.datasources.{HadoopFsRelation, LogicalRelation}
import org.apache.spark.sql.execution.datasources.orc.OrcQueryTest
import org.apache.spark.sql.hive.{HiveSessionCatalog, HiveUtils}
import org.apache.spark.sql.hive.test.TestHiveSingleton
import org.apache.spark.sql.internal.SQLConf
class HiveOrcQuerySuite extends OrcQueryTest with TestHiveSingleton {
import testImplicits._
override val orcImp: String = "hive"
test("SPARK-8501: Avoids discovery schema from empty ORC files") {
withTempPath { dir =>
val path = dir.getCanonicalPath
withTable("empty_orc") {
withTempView("empty", "single") {
spark.sql(
s"""CREATE TABLE empty_orc(key INT, value STRING)
|STORED AS ORC
|LOCATION '${dir.toURI}'
""".stripMargin)
val emptyDF = Seq.empty[(Int, String)].toDF("key", "value").coalesce(1)
emptyDF.createOrReplaceTempView("empty")
// This creates 1 empty ORC file with Hive ORC SerDe. We are using this trick because
// Spark SQL ORC data source always avoids write empty ORC files.
spark.sql(
s"""INSERT INTO TABLE empty_orc
|SELECT key, value FROM empty
""".stripMargin)
val errorMessage = intercept[AnalysisException] {
spark.read.orc(path)
}.getMessage
assert(errorMessage.contains("Unable to infer schema for ORC"))
val singleRowDF = Seq((0, "foo")).toDF("key", "value").coalesce(1)
singleRowDF.createOrReplaceTempView("single")
spark.sql(
s"""INSERT INTO TABLE empty_orc
|SELECT key, value FROM single
""".stripMargin)
val df = spark.read.orc(path)
assert(df.schema === singleRowDF.schema.asNullable)
checkAnswer(df, singleRowDF)
}
}
}
}
test("Verify the ORC conversion parameter: CONVERT_METASTORE_ORC") {
withTempView("single") {
val singleRowDF = Seq((0, "foo")).toDF("key", "value")
singleRowDF.createOrReplaceTempView("single")
Seq("true", "false").foreach { orcConversion =>
withSQLConf(HiveUtils.CONVERT_METASTORE_ORC.key -> orcConversion) {
withTable("dummy_orc") {
withTempPath { dir =>
val path = dir.getCanonicalPath
spark.sql(
s"""
|CREATE TABLE dummy_orc(key INT, value STRING)
|STORED AS ORC
|LOCATION '${dir.toURI}'
""".stripMargin)
spark.sql(
s"""
|INSERT INTO TABLE dummy_orc
|SELECT key, value FROM single
""".stripMargin)
val df = spark.sql("SELECT * FROM dummy_orc WHERE key=0")
checkAnswer(df, singleRowDF)
val queryExecution = df.queryExecution
if (orcConversion == "true") {
queryExecution.analyzed.collectFirst {
case _: LogicalRelation => ()
}.getOrElse {
fail(s"Expecting the query plan to convert orc to data sources, " +
s"but got:\n$queryExecution")
}
} else {
queryExecution.analyzed.collectFirst {
case _: HiveTableRelation => ()
}.getOrElse {
fail(s"Expecting no conversion from orc to data sources, " +
s"but got:\n$queryExecution")
}
}
}
}
}
}
}
}
test("converted ORC table supports resolving mixed case field") {
withSQLConf(HiveUtils.CONVERT_METASTORE_ORC.key -> "true") {
withTable("dummy_orc") {
withTempPath { dir =>
val df = spark.range(5).selectExpr("id", "id as valueField", "id as partitionValue")
df.write
.partitionBy("partitionValue")
.mode("overwrite")
.orc(dir.getAbsolutePath)
spark.sql(s"""
|create external table dummy_orc (id long, valueField long)
|partitioned by (partitionValue int)
|stored as orc
|location "${dir.toURI}"""".stripMargin)
spark.sql(s"msck repair table dummy_orc")
checkAnswer(spark.sql("select * from dummy_orc"), df)
}
}
}
}
test("SPARK-20728 Make ORCFileFormat configurable between sql/hive and sql/core") {
Seq(
("native", classOf[org.apache.spark.sql.execution.datasources.orc.OrcFileFormat]),
("hive", classOf[org.apache.spark.sql.hive.orc.OrcFileFormat])).foreach {
case (orcImpl, format) =>
withSQLConf(SQLConf.ORC_IMPLEMENTATION.key -> orcImpl) {
withTable("spark_20728") {
sql("CREATE TABLE spark_20728(a INT) USING ORC")
val fileFormat = sql("SELECT * FROM spark_20728").queryExecution.analyzed.collectFirst {
case l: LogicalRelation =>
l.relation.asInstanceOf[HadoopFsRelation].fileFormat.getClass
}
assert(fileFormat == Some(format))
}
}
}
}
// Since Hive 1.2.1 library code path still has this problem, users may hit this
// when spark.sql.hive.convertMetastoreOrc=false. However, after SPARK-22279,
// Apache Spark with the default configuration doesn't hit this bug.
test("SPARK-22267 Spark SQL incorrectly reads ORC files when column order is different") {
Seq("native", "hive").foreach { orcImpl =>
withSQLConf(SQLConf.ORC_IMPLEMENTATION.key -> orcImpl) {
withTempPath { f =>
val path = f.getCanonicalPath
Seq(1 -> 2).toDF("c1", "c2").write.orc(path)
checkAnswer(spark.read.orc(path), Row(1, 2))
withSQLConf(HiveUtils.CONVERT_METASTORE_ORC.key -> "true") { // default since 2.3.0
withTable("t") {
sql(s"CREATE EXTERNAL TABLE t(c2 INT, c1 INT) STORED AS ORC LOCATION '$path'")
checkAnswer(spark.table("t"), Row(2, 1))
}
}
}
}
}
}
// Since Hive 1.2.1 library code path still has this problem, users may hit this
// when spark.sql.hive.convertMetastoreOrc=false. However, after SPARK-22279,
// Apache Spark with the default configuration doesn't hit this bug.
test("SPARK-19809 NullPointerException on zero-size ORC file") {
Seq("native", "hive").foreach { orcImpl =>
withSQLConf(SQLConf.ORC_IMPLEMENTATION.key -> orcImpl) {
withTempPath { dir =>
withTable("spark_19809") {
sql(s"CREATE TABLE spark_19809(a int) STORED AS ORC LOCATION '$dir'")
Files.touch(new File(s"${dir.getCanonicalPath}", "zero.orc"))
withSQLConf(HiveUtils.CONVERT_METASTORE_ORC.key -> "true") { // default since 2.3.0
checkAnswer(spark.table("spark_19809"), Seq.empty)
}
}
}
}
}
}
// SPARK-28885 String value is not allowed to be stored as numeric type with
// ANSI store assignment policy.
// TODO: re-enable the test case when SPARK-29462 is fixed.
ignore("SPARK-23340 Empty float/double array columns raise EOFException") {
withSQLConf(HiveUtils.CONVERT_METASTORE_ORC.key -> "false") {
withTable("spark_23340") {
sql("CREATE TABLE spark_23340(a array<float>, b array<double>) STORED AS ORC")
sql("INSERT INTO spark_23340 VALUES (array(), array())")
checkAnswer(spark.table("spark_23340"), Seq(Row(Array.empty[Float], Array.empty[Double])))
}
}
}
test("SPARK-26437 Can not query decimal type when value is 0") {
withSQLConf(HiveUtils.CONVERT_METASTORE_ORC.key -> "false") {
withTable("spark_26437") {
sql("CREATE TABLE spark_26437 STORED AS ORCFILE AS SELECT 0.00 AS c1")
checkAnswer(spark.table("spark_26437"), Seq(Row(0.00)))
}
}
}
private def getCachedDataSourceTable(table: TableIdentifier) = {
spark.sessionState.catalog.asInstanceOf[HiveSessionCatalog].metastoreCatalog
.getCachedDataSourceTable(table)
}
private def checkCached(tableIdentifier: TableIdentifier): Unit = {
getCachedDataSourceTable(tableIdentifier) match {
case null => fail(s"Converted ${tableIdentifier.table} should be cached in the cache.")
case LogicalRelation(_: HadoopFsRelation, _, _, _) => // OK
case other =>
fail(
s"The cached ${tableIdentifier.table} should be a HadoopFsRelation. " +
s"However, $other is returned form the cache.")
}
}
test("SPARK-28573 ORC conversation could be applied for partitioned table insertion") {
withTempView("single") {
val singleRowDF = Seq((0, "foo")).toDF("key", "value")
singleRowDF.createOrReplaceTempView("single")
Seq("true", "false").foreach { conversion =>
withSQLConf(HiveUtils.CONVERT_METASTORE_ORC.key -> "true",
HiveUtils.CONVERT_INSERTING_PARTITIONED_TABLE.key -> conversion) {
withTable("dummy_orc_partitioned") {
spark.sessionState.refreshTable("dummy_orc_partitioned")
spark.sql(
s"""
|CREATE TABLE dummy_orc_partitioned(key INT, value STRING)
|PARTITIONED by (`date` STRING)
|STORED AS ORC
""".stripMargin)
spark.sql(
s"""
|INSERT INTO TABLE dummy_orc_partitioned
|PARTITION (`date` = '2019-04-01')
|SELECT key, value FROM single
""".stripMargin)
val orcPartitionedTable = TableIdentifier("dummy_orc_partitioned", Some("default"))
if (conversion == "true") {
// if converted, we refresh the cached relation.
assert(getCachedDataSourceTable(orcPartitionedTable) === null)
} else {
// otherwise, not cached.
assert(getCachedDataSourceTable(orcPartitionedTable) === null)
}
val df = spark.sql("SELECT key, value FROM dummy_orc_partitioned WHERE key=0")
checkAnswer(df, singleRowDF)
}
}
}
}
}
test("SPARK-32234 read ORC table with column names all starting with '_col'") {
Seq("native", "hive").foreach { orcImpl =>
Seq("false", "true").foreach { vectorized =>
withSQLConf(
SQLConf.ORC_IMPLEMENTATION.key -> orcImpl,
SQLConf.ORC_VECTORIZED_READER_ENABLED.key -> vectorized) {
withTable("test_hive_orc_impl") {
spark.sql(
s"""
| CREATE TABLE test_hive_orc_impl
| (_col1 INT, _col2 STRING, _col3 INT)
| STORED AS ORC
""".stripMargin)
spark.sql(
s"""
| INSERT INTO
| test_hive_orc_impl
| VALUES(9, '12', 2020)
""".stripMargin)
val df = spark.sql("SELECT _col2 FROM test_hive_orc_impl")
checkAnswer(df, Row("12"))
}
}
}
}
}
}