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
|
/*
* 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
import java.util
import java.util.{Locale, TimeZone}
import org.apache.hadoop.hive.ql.udf.UDAFPercentile
import org.apache.hadoop.hive.serde2.io.DoubleWritable
import org.apache.hadoop.hive.serde2.objectinspector.{ObjectInspector, ObjectInspectorFactory, StructObjectInspector}
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.ObjectInspectorOptions
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory
import org.apache.hadoop.io.LongWritable
import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.Literal
import org.apache.spark.sql.catalyst.util.{ArrayBasedMapData, GenericArrayData, MapData}
import org.apache.spark.sql.types._
import org.apache.spark.sql.Row
class HiveInspectorSuite extends SparkFunSuite with HiveInspectors {
def unwrap(data: Any, oi: ObjectInspector): Any = {
val unwrapper = unwrapperFor(oi)
unwrapper(data)
}
test("Test wrap SettableStructObjectInspector") {
val udaf = new UDAFPercentile.PercentileLongEvaluator()
udaf.init()
udaf.iterate(new LongWritable(1), 0.1)
udaf.iterate(new LongWritable(1), 0.1)
val state = udaf.terminatePartial()
val soi = ObjectInspectorFactory.getReflectionObjectInspector(
classOf[UDAFPercentile.State],
ObjectInspectorOptions.JAVA).asInstanceOf[StructObjectInspector]
val a = unwrap(state, soi).asInstanceOf[InternalRow]
val dt = new StructType()
.add("counts", MapType(LongType, LongType))
.add("percentiles", ArrayType(DoubleType))
val b = wrap(a, soi, dt).asInstanceOf[UDAFPercentile.State]
val sfCounts = soi.getStructFieldRef("counts")
val sfPercentiles = soi.getStructFieldRef("percentiles")
assert(2 === soi.getStructFieldData(b, sfCounts)
.asInstanceOf[util.Map[LongWritable, LongWritable]]
.get(new LongWritable(1L))
.get())
assert(0.1 === soi.getStructFieldData(b, sfPercentiles)
.asInstanceOf[util.ArrayList[DoubleWritable]]
.get(0)
.get())
}
// Timezone is fixed to America/Los_Angeles for those timezone sensitive tests (timestamp_*)
TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"))
// Add Locale setting
Locale.setDefault(Locale.US)
val data =
Literal(true) ::
Literal(null) ::
Literal(0.asInstanceOf[Byte]) ::
Literal(0.asInstanceOf[Short]) ::
Literal(0) ::
Literal(0.asInstanceOf[Long]) ::
Literal(0.asInstanceOf[Float]) ::
Literal(0.asInstanceOf[Double]) ::
Literal("0") ::
Literal(java.sql.Date.valueOf("2014-09-23")) ::
Literal(Decimal(BigDecimal(123.123))) ::
Literal(new java.sql.Timestamp(123123)) ::
Literal(Array[Byte](1, 2, 3)) ::
Literal.create(Seq[Int](1, 2, 3), ArrayType(IntegerType)) ::
Literal.create(Map[Int, Int](1 -> 2, 2 -> 1), MapType(IntegerType, IntegerType)) ::
Literal.create(Row(1, 2.0d, 3.0f),
StructType(StructField("c1", IntegerType) ::
StructField("c2", DoubleType) ::
StructField("c3", FloatType) :: Nil)) ::
Nil
val row = data.map(_.eval(null))
val dataTypes = data.map(_.dataType)
def toWritableInspector(dataType: DataType): ObjectInspector = dataType match {
case ArrayType(tpe, _) =>
ObjectInspectorFactory.getStandardListObjectInspector(toWritableInspector(tpe))
case MapType(keyType, valueType, _) =>
ObjectInspectorFactory.getStandardMapObjectInspector(
toWritableInspector(keyType), toWritableInspector(valueType))
case StringType => PrimitiveObjectInspectorFactory.writableStringObjectInspector
case IntegerType => PrimitiveObjectInspectorFactory.writableIntObjectInspector
case DoubleType => PrimitiveObjectInspectorFactory.writableDoubleObjectInspector
case BooleanType => PrimitiveObjectInspectorFactory.writableBooleanObjectInspector
case LongType => PrimitiveObjectInspectorFactory.writableLongObjectInspector
case FloatType => PrimitiveObjectInspectorFactory.writableFloatObjectInspector
case ShortType => PrimitiveObjectInspectorFactory.writableShortObjectInspector
case ByteType => PrimitiveObjectInspectorFactory.writableByteObjectInspector
case NullType => PrimitiveObjectInspectorFactory.writableVoidObjectInspector
case BinaryType => PrimitiveObjectInspectorFactory.writableBinaryObjectInspector
case DateType => PrimitiveObjectInspectorFactory.writableDateObjectInspector
case TimestampType => PrimitiveObjectInspectorFactory.writableTimestampObjectInspector
case DecimalType() => PrimitiveObjectInspectorFactory.writableHiveDecimalObjectInspector
case StructType(fields) =>
ObjectInspectorFactory.getStandardStructObjectInspector(
java.util.Arrays.asList(fields.map(f => f.name) : _*),
java.util.Arrays.asList(fields.map(f => toWritableInspector(f.dataType)) : _*))
}
def checkDataType(dt1: Seq[DataType], dt2: Seq[DataType]): Unit = {
dt1.zip(dt2).foreach { case (dd1, dd2) =>
assert(dd1.getClass === dd2.getClass) // DecimalType doesn't has the default precision info
}
}
def checkValues(row1: Seq[Any], row2: Seq[Any]): Unit = {
row1.zip(row2).foreach { case (r1, r2) =>
checkValue(r1, r2)
}
}
def checkValues(row1: Seq[Any], row2: InternalRow, row2Schema: StructType): Unit = {
row1.zip(row2.toSeq(row2Schema)).foreach { case (r1, r2) =>
checkValue(r1, r2)
}
}
def checkValue(v1: Any, v2: Any): Unit = {
(v1, v2) match {
case (r1: Decimal, r2: Decimal) =>
// Ignore the Decimal precision
assert(r1.compare(r2) === 0)
case (r1: Array[Byte], r2: Array[Byte])
if r1 != null && r2 != null && r1.length == r2.length =>
r1.zip(r2).foreach { case (b1, b2) => assert(b1 === b2) }
// We don't support equality & ordering for map type, so skip it.
case (r1: MapData, r2: MapData) =>
case (r1, r2) => assert(r1 === r2)
}
}
test("oi => datatype => oi") {
val ois = dataTypes.map(toInspector)
checkDataType(ois.map(inspectorToDataType), dataTypes)
checkDataType(dataTypes.map(toWritableInspector).map(inspectorToDataType), dataTypes)
}
test("wrap / unwrap null, constant null and writables") {
val writableOIs = dataTypes.map(toWritableInspector)
val nullRow = data.map(d => null)
checkValues(nullRow, nullRow.zip(writableOIs).zip(dataTypes).map {
case ((d, oi), dt) => unwrap(wrap(d, oi, dt), oi)
})
// struct couldn't be constant, sweep it out
val constantExprs = data.filter(!_.dataType.isInstanceOf[StructType])
val constantTypes = constantExprs.map(_.dataType)
val constantData = constantExprs.map(_.eval())
val constantNullData = constantData.map(_ => null)
val constantWritableOIs = constantExprs.map(e => toWritableInspector(e.dataType))
val constantNullWritableOIs =
constantExprs.map(e => toInspector(Literal.create(null, e.dataType)))
checkValues(constantData, constantData.zip(constantWritableOIs).zip(constantTypes).map {
case ((d, oi), dt) => unwrap(wrap(d, oi, dt), oi)
})
checkValues(constantNullData, constantData.zip(constantNullWritableOIs).zip(constantTypes).map {
case ((d, oi), dt) => unwrap(wrap(d, oi, dt), oi)
})
checkValues(constantNullData, constantNullData.zip(constantWritableOIs).zip(constantTypes).map {
case ((d, oi), dt) => unwrap(wrap(d, oi, dt), oi)
})
}
test("wrap / unwrap primitive writable object inspector") {
val writableOIs = dataTypes.map(toWritableInspector)
checkValues(row, row.zip(writableOIs).zip(dataTypes).map {
case ((data, oi), dt) => unwrap(wrap(data, oi, dt), oi)
})
}
test("wrap / unwrap primitive java object inspector") {
val ois = dataTypes.map(toInspector)
checkValues(row, row.zip(ois).zip(dataTypes).map {
case ((data, oi), dt) => unwrap(wrap(data, oi, dt), oi)
})
}
test("wrap / unwrap Struct Type") {
val dt = StructType(dataTypes.zipWithIndex.map {
case (t, idx) => StructField(s"c_$idx", t)
})
val inspector = toInspector(dt)
checkValues(
row,
unwrap(wrap(InternalRow.fromSeq(row), inspector, dt), inspector).asInstanceOf[InternalRow],
dt)
checkValue(null, unwrap(wrap(null, toInspector(dt), dt), toInspector(dt)))
}
test("wrap / unwrap Array Type") {
val dt = ArrayType(dataTypes(0))
val d = new GenericArrayData(Array(row(0), row(0)))
checkValue(d, unwrap(wrap(d, toInspector(dt), dt), toInspector(dt)))
checkValue(null, unwrap(wrap(null, toInspector(dt), dt), toInspector(dt)))
checkValue(d,
unwrap(wrap(d, toInspector(Literal.create(d, dt)), dt), toInspector(Literal.create(d, dt))))
checkValue(d,
unwrap(wrap(null, toInspector(Literal.create(d, dt)), dt),
toInspector(Literal.create(d, dt))))
}
test("wrap / unwrap Map Type") {
val dt = MapType(dataTypes(0), dataTypes(1))
val d = ArrayBasedMapData(Array(row(0)), Array(row(1)))
checkValue(d, unwrap(wrap(d, toInspector(dt), dt), toInspector(dt)))
checkValue(null, unwrap(wrap(null, toInspector(dt), dt), toInspector(dt)))
checkValue(d,
unwrap(wrap(d, toInspector(Literal.create(d, dt)), dt), toInspector(Literal.create(d, dt))))
checkValue(d,
unwrap(wrap(null, toInspector(Literal.create(d, dt)), dt),
toInspector(Literal.create(d, dt))))
}
}
|