Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,25 @@ public void writeTo(ByteBuffer buffer) {
buffer.position(pos + numBytes);
}

public void writeTo(OutputStream out) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always good to have tests for the corner case codepaths here, as they are invariably the official home of off-by-one errors

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a few tests for this method.

if (base instanceof byte[] && offset >= BYTE_ARRAY_OFFSET) {
final byte[] bytes = (byte[]) base;

// the offset includes an object header... this is only needed for unsafe copies
final long arrayOffset = offset - BYTE_ARRAY_OFFSET;

// verify that the offset and length points somewhere inside the byte array
// and that the offset can safely be truncated to a 32-bit integer
if ((long) bytes.length < arrayOffset + numBytes) {
throw new ArrayIndexOutOfBoundsException();
}

out.write(bytes, (int) arrayOffset, numBytes);
} else {
out.write(getBytes());
}
}

/**
* Returns the number of bytes for a code point with the first byte as `b`
* @param b The first byte of a code point
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@

package org.apache.spark.unsafe.types;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

import com.google.common.collect.ImmutableMap;
import org.apache.spark.unsafe.Platform;
import org.junit.Test;

import static org.junit.Assert.*;

import static org.apache.spark.unsafe.Platform.BYTE_ARRAY_OFFSET;
import static org.apache.spark.unsafe.types.UTF8String.*;

public class UTF8StringSuite {
Expand Down Expand Up @@ -499,4 +506,106 @@ public void soundex() {
assertEquals(fromString("123").soundex(), fromString("123"));
assertEquals(fromString("世界千世").soundex(), fromString("世界千世"));
}

@Test
public void writeToOutputStreamUnderflow() throws IOException {
// offset underflow is apparently supported?
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final byte[] test = "01234567".getBytes(StandardCharsets.UTF_8);

for (int i = 1; i <= Platform.BYTE_ARRAY_OFFSET; ++i) {
UTF8String.fromAddress(test, Platform.BYTE_ARRAY_OFFSET - i, test.length + i)
.writeTo(outputStream);
final ByteBuffer buffer = ByteBuffer.wrap(outputStream.toByteArray(), i, test.length);
assertEquals("01234567", StandardCharsets.UTF_8.decode(buffer).toString());
outputStream.reset();
}
}

@Test
public void writeToOutputStreamSlice() throws IOException {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final byte[] test = "01234567".getBytes(StandardCharsets.UTF_8);

for (int i = 0; i < test.length; ++i) {
for (int j = 0; j < test.length - i; ++j) {
UTF8String.fromAddress(test, Platform.BYTE_ARRAY_OFFSET + i, j)
.writeTo(outputStream);

assertArrayEquals(Arrays.copyOfRange(test, i, i + j), outputStream.toByteArray());
outputStream.reset();
}
}
}

@Test
public void writeToOutputStreamOverflow() throws IOException {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final byte[] test = "01234567".getBytes(StandardCharsets.UTF_8);

final HashSet<Long> offsets = new HashSet<>();
for (int i = 0; i < 16; ++i) {
// touch more points around MAX_VALUE
offsets.add((long) Integer.MAX_VALUE - i);
// subtract off BYTE_ARRAY_OFFSET to avoid wrapping around to a negative value,
// which will hit the slower copy path instead of the optimized one
offsets.add(Long.MAX_VALUE - BYTE_ARRAY_OFFSET - i);
}

for (long i = 1; i > 0L; i <<= 1) {
for (long j = 0; j < 32L; ++j) {
offsets.add(i + j);
}
}

for (final long offset : offsets) {
try {
fromAddress(test, BYTE_ARRAY_OFFSET + offset, test.length)
.writeTo(outputStream);

throw new IllegalStateException(Long.toString(offset));
} catch (ArrayIndexOutOfBoundsException e) {
// ignore
} finally {
outputStream.reset();
}
}
}

@Test
public void writeToOutputStream() throws IOException {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
EMPTY_UTF8.writeTo(outputStream);
assertEquals("", outputStream.toString("UTF-8"));
outputStream.reset();

fromString("数据砖很重").writeTo(outputStream);
assertEquals(
"数据砖很重",
outputStream.toString("UTF-8"));
outputStream.reset();
}

@Test
public void writeToOutputStreamIntArray() throws IOException {
// verify that writes work on objects that are not byte arrays
final ByteBuffer buffer = StandardCharsets.UTF_8.encode("大千世界");
buffer.position(0);
buffer.order(ByteOrder.LITTLE_ENDIAN);

final int length = buffer.limit();
assertEquals(12, length);

final int ints = length / 4;
final int[] array = new int[ints];

for (int i = 0; i < ints; ++i) {
array[i] = buffer.getInt();
}

final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
fromAddress(array, Platform.INT_ARRAY_OFFSET, length)
.writeTo(outputStream);
assertEquals("大千世界", outputStream.toString("UTF-8"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ import java.io.IOException

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileStatus, Path}
import org.apache.hadoop.io.{NullWritable, Text}
import org.apache.hadoop.mapreduce.{Job, RecordWriter, TaskAttemptContext}
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat
import org.apache.hadoop.mapreduce.{Job, TaskAttemptContext}

import org.apache.spark.TaskContext
import org.apache.spark.ml.feature.LabeledPoint
Expand All @@ -35,7 +33,6 @@ import org.apache.spark.sql.catalyst.encoders.RowEncoder
import org.apache.spark.sql.catalyst.expressions.AttributeReference
import org.apache.spark.sql.catalyst.expressions.codegen.GenerateUnsafeProjection
import org.apache.spark.sql.execution.datasources._
import org.apache.spark.sql.execution.datasources.text.TextOutputWriter
import org.apache.spark.sql.sources._
import org.apache.spark.sql.types._
import org.apache.spark.util.SerializableConfiguration
Expand All @@ -46,30 +43,21 @@ private[libsvm] class LibSVMOutputWriter(
context: TaskAttemptContext)
extends OutputWriter {

private[this] val buffer = new Text()

private val recordWriter: RecordWriter[NullWritable, Text] = {
new TextOutputFormat[NullWritable, Text]() {
override def getDefaultWorkFile(context: TaskAttemptContext, extension: String): Path = {
new Path(path)
}
}.getRecordWriter(context)
}
private val writer = CodecStreams.createOutputStreamWriter(context, new Path(path))

override def write(row: Row): Unit = {
val label = row.get(0)
val vector = row.get(1).asInstanceOf[Vector]
val sb = new StringBuilder(label.toString)
writer.write(label.toString)
vector.foreachActive { case (i, v) =>
sb += ' '
sb ++= s"${i + 1}:$v"
writer.write(s" ${i + 1}:$v")
}
buffer.set(sb.mkString)
recordWriter.write(NullWritable.get(), buffer)

writer.write('\n')
}

override def close(): Unit = {
recordWriter.close(context)
writer.close()
}
}

Expand Down Expand Up @@ -136,7 +124,7 @@ private[libsvm] class LibSVMFileFormat extends TextBasedFileFormat with DataSour
}

override def getFileExtension(context: TaskAttemptContext): String = {
".libsvm" + TextOutputWriter.getCompressionExtension(context)
".libsvm" + CodecStreams.getCompressionExtension(context)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,8 @@ private[sql] class JacksonGenerator(
writeFields(row, schema, rootFieldWriters)
}
}

def writeLineEnding(): Unit = {
gen.writeRaw('\n')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TextOutputStream actually writes the UTF-8 version of a newline; don't know if that is relevant or not:

"\n".getBytes(StandardCharsets.UTF_8);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

7-bit ASCII is a subset of UTF-8, \n is the same in both.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably just a convoluted way to create a byte array containing the byte 0x0a then

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is my assumption. I'm also assuming that writing a single byte is slightly more efficient than writing an array of a single byte.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.execution.datasources

import java.io.{OutputStream, OutputStreamWriter}
import java.nio.charset.{Charset, StandardCharsets}

import org.apache.hadoop.fs.Path
import org.apache.hadoop.io.compress._
import org.apache.hadoop.mapreduce.JobContext
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat
import org.apache.hadoop.util.ReflectionUtils

object CodecStreams {
private def getCompressionCodec(
context: JobContext,
file: Option[Path] = None): Option[CompressionCodec] = {
if (FileOutputFormat.getCompressOutput(context)) {
val compressorClass = FileOutputFormat.getOutputCompressorClass(
context,
classOf[GzipCodec])

Some(ReflectionUtils.newInstance(compressorClass, context.getConfiguration))
} else {
file.flatMap { path =>
val compressionCodecs = new CompressionCodecFactory(context.getConfiguration)
Option(compressionCodecs.getCodec(path))
}
}
}

/**
* Create a new file and open it for writing.
* If compression is enabled in the [[JobContext]] the stream will write compressed data to disk.
* An exception will be thrown if the file already exists.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will "probably" be thrown; object stores have issues there

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a problem with Hadoop in general? The FileSystem docs also specify this behavior:

  /**
   * Create an FSDataOutputStream at the indicated Path.
   * @param f the file to create
   * @param overwrite if a file with this name already exists, then if true,
   *   the file will be overwritten, and if false an exception will be thrown.
   */

*/
def createOutputStream(context: JobContext, file: Path): OutputStream = {
val fs = file.getFileSystem(context.getConfiguration)
val outputStream: OutputStream = fs.create(file, false)

getCompressionCodec(context, Some(file))
.map(codec => codec.createOutputStream(outputStream))
.getOrElse(outputStream)
}

def createOutputStreamWriter(
context: JobContext,
file: Path,
charset: Charset = StandardCharsets.UTF_8): OutputStreamWriter = {
new OutputStreamWriter(createOutputStream(context, file), charset)
}

/** Returns the compression codec extension to be used in a file name, e.g. ".gzip"). */
def getCompressionExtension(context: JobContext): String = {
getCompressionCodec(context)
.map(_.getDefaultExtension)
.getOrElse("")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package org.apache.spark.sql.execution.datasources.csv

import java.io.{CharArrayWriter, StringReader}
import java.io.{CharArrayWriter, OutputStream, StringReader}
import java.nio.charset.StandardCharsets

import com.univocity.parsers.csv._

Expand Down Expand Up @@ -64,7 +65,10 @@ private[csv] class CsvReader(params: CSVOptions) {
* @param params Parameters object for configuration
* @param headers headers for columns
*/
private[csv] class LineCsvWriter(params: CSVOptions, headers: Seq[String]) extends Logging {
private[csv] class LineCsvWriter(
params: CSVOptions,
headers: Seq[String],
output: OutputStream) extends Logging {
private val writerSettings = new CsvWriterSettings
private val format = writerSettings.getFormat

Expand All @@ -80,21 +84,14 @@ private[csv] class LineCsvWriter(params: CSVOptions, headers: Seq[String]) exten
writerSettings.setHeaders(headers: _*)
writerSettings.setQuoteEscapingEnabled(params.escapeQuotes)

private val buffer = new CharArrayWriter()
private val writer = new CsvWriter(buffer, writerSettings)
private val writer = new CsvWriter(output, StandardCharsets.UTF_8, writerSettings)

def writeRow(row: Seq[String], includeHeader: Boolean): Unit = {
if (includeHeader) {
writer.writeHeaders()
}
writer.writeRow(row.toArray: _*)
}

def flush(): String = {
writer.flush()
val lines = buffer.toString.stripLineEnd
buffer.reset()
lines
writer.writeRow(row: _*)
}

def close(): Unit = {
Expand Down
Loading