|
| 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.execution.joins |
| 19 | + |
| 20 | +import org.scalatest.FunSuite |
| 21 | + |
| 22 | +import org.apache.spark.sql.catalyst.expressions.{Projection, Row} |
| 23 | +import org.apache.spark.util.collection.CompactBuffer |
| 24 | + |
| 25 | + |
| 26 | +class HashedRelationSuite extends FunSuite { |
| 27 | + |
| 28 | + // Key is simply the record itself |
| 29 | + private val keyProjection = new Projection { |
| 30 | + override def apply(row: Row): Row = row |
| 31 | + } |
| 32 | + |
| 33 | + test("GeneralHashedRelation") { |
| 34 | + val data = Array(Row(0), Row(1), Row(2), Row(2)) |
| 35 | + val hashed = HashedRelation(data.iterator, keyProjection) |
| 36 | + assert(hashed.isInstanceOf[GeneralHashedRelation]) |
| 37 | + |
| 38 | + assert(hashed.get(data(0)) == CompactBuffer[Row](data(0))) |
| 39 | + assert(hashed.get(data(1)) == CompactBuffer[Row](data(1))) |
| 40 | + |
| 41 | + val data2 = CompactBuffer[Row](data(2)) |
| 42 | + data2 += data(2) |
| 43 | + assert(hashed.get(data(2)) == data2) |
| 44 | + } |
| 45 | + |
| 46 | + test("UniqueKeyHashedRelation") { |
| 47 | + val data = Array(Row(0), Row(1), Row(2)) |
| 48 | + val hashed = HashedRelation(data.iterator, keyProjection) |
| 49 | + assert(hashed.isInstanceOf[UniqueKeyHashedRelation]) |
| 50 | + |
| 51 | + assert(hashed.get(data(0)) == CompactBuffer[Row](data(0))) |
| 52 | + assert(hashed.get(data(1)) == CompactBuffer[Row](data(1))) |
| 53 | + assert(hashed.get(data(2)) == CompactBuffer[Row](data(2))) |
| 54 | + |
| 55 | + val uniqHashed = hashed.asInstanceOf[UniqueKeyHashedRelation] |
| 56 | + assert(uniqHashed.getValue(data(0)) == data(0)) |
| 57 | + assert(uniqHashed.getValue(data(1)) == data(1)) |
| 58 | + assert(uniqHashed.getValue(data(2)) == data(2)) |
| 59 | + } |
| 60 | +} |
0 commit comments