Skip to content

Commit d85eeff

Browse files
committed
Add basic sanity test for UnsafeFixedWidthAggregationMap
1 parent bade966 commit d85eeff

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.catalyst.expressions
19+
20+
import org.apache.spark.unsafe.memory.MemoryAllocator
21+
import org.scalatest.{FunSuite, Matchers}
22+
23+
import org.apache.spark.sql.types._
24+
25+
class UnsafeFixedWidthAggregationMapSuite extends FunSuite with Matchers {
26+
27+
private val groupKeySchema = StructType(StructField("product", StringType) :: Nil)
28+
private val aggBufferSchema = StructType(StructField("salePrice", IntegerType) :: Nil)
29+
private def emptyAggregationBuffer: Row = new GenericRow(Array[Any](0))
30+
31+
test("empty map") {
32+
val map = new UnsafeFixedWidthAggregationMap(
33+
emptyAggregationBuffer,
34+
aggBufferSchema,
35+
groupKeySchema,
36+
MemoryAllocator.HEAP,
37+
1024
38+
)
39+
assert(!map.iterator().hasNext)
40+
map.free()
41+
}
42+
43+
test("updating values for a single key") {
44+
val map = new UnsafeFixedWidthAggregationMap(
45+
emptyAggregationBuffer,
46+
aggBufferSchema,
47+
groupKeySchema,
48+
MemoryAllocator.HEAP,
49+
1024
50+
)
51+
val groupKey = new GenericRow(Array[Any](UTF8String("cats")))
52+
53+
// Looking up a key stores a zero-entry in the map (like Python Counters or DefaultDicts)
54+
map.getAggregationBuffer(groupKey)
55+
val iter = map.iterator()
56+
val entry = iter.next()
57+
assert(!iter.hasNext)
58+
entry.key.getString(0) should be ("cats")
59+
entry.value.getInt(0) should be (0)
60+
61+
// Modifications to rows retrieved from the map should update the values in the map
62+
entry.value.setInt(0, 42)
63+
map.getAggregationBuffer(groupKey).getInt(0) should be (42)
64+
65+
map.free()
66+
}
67+
68+
69+
}

0 commit comments

Comments
 (0)