Skip to content

Commit fc616d5

Browse files
Jakub DubovskýJoshRosen
authored andcommitted
[SPARK-3121] Wrong implementation of implicit bytesWritableConverter
val path = ... //path to seq file with BytesWritable as type of both key and value val file = sc.sequenceFile[Array[Byte],Array[Byte]](path) file.take(1)(0)._1 This prints incorrect content of byte array. Actual content starts with correct one and some "random" bytes and zeros are appended. BytesWritable has two methods: getBytes() - return content of all internal array which is often longer then actual value stored. It usually contains the rest of previous longer values copyBytes() - return just begining of internal array determined by internal length property It looks like in implicit conversion between BytesWritable and Array[byte] getBytes is used instead of correct copyBytes. dbtsai Author: Jakub Dubovský <james64@inMail.sk> Author: Dubovsky Jakub <dubovsky@avast.com> Closes #2712 from james64/3121-bugfix and squashes the following commits: f85d24c [Jakub Dubovský] Test name changed, comments added 1b20d51 [Jakub Dubovský] Import placed correctly 406e26c [Jakub Dubovský] Scala style fixed f92ffa6 [Dubovsky Jakub] performance tuning 480f9cd [Dubovsky Jakub] Bug 3121 fixed
1 parent c86c976 commit fc616d5

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

core/src/main/scala/org/apache/spark/SparkContext.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import scala.language.implicitConversions
2121

2222
import java.io._
2323
import java.net.URI
24+
import java.util.Arrays
2425
import java.util.concurrent.atomic.AtomicInteger
2526
import java.util.{Properties, UUID}
2627
import java.util.UUID.randomUUID
@@ -1429,7 +1430,10 @@ object SparkContext extends Logging {
14291430
simpleWritableConverter[Boolean, BooleanWritable](_.get)
14301431

14311432
implicit def bytesWritableConverter(): WritableConverter[Array[Byte]] = {
1432-
simpleWritableConverter[Array[Byte], BytesWritable](_.getBytes)
1433+
simpleWritableConverter[Array[Byte], BytesWritable](bw =>
1434+
// getBytes method returns array which is longer then data to be returned
1435+
Arrays.copyOfRange(bw.getBytes, 0, bw.getLength)
1436+
)
14331437
}
14341438

14351439
implicit def stringWritableConverter(): WritableConverter[String] =
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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
19+
20+
import org.scalatest.FunSuite
21+
22+
import org.apache.hadoop.io.BytesWritable
23+
24+
class SparkContextSuite extends FunSuite {
25+
//Regression test for SPARK-3121
26+
test("BytesWritable implicit conversion is correct") {
27+
val bytesWritable = new BytesWritable()
28+
val inputArray = (1 to 10).map(_.toByte).toArray
29+
bytesWritable.set(inputArray, 0, 10)
30+
bytesWritable.set(inputArray, 0, 5)
31+
32+
val converter = SparkContext.bytesWritableConverter()
33+
val byteArray = converter.convert(bytesWritable)
34+
assert(byteArray.length === 5)
35+
36+
bytesWritable.set(inputArray, 0, 0)
37+
val byteArray2 = converter.convert(bytesWritable)
38+
assert(byteArray2.length === 0)
39+
}
40+
}

0 commit comments

Comments
 (0)