Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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 @@ -236,7 +236,10 @@ object FileFormatWriter extends Logging {
committer.setupTask(taskAttemptContext)

val writeTask =
if (description.partitionColumns.isEmpty && description.bucketIdExpression.isEmpty) {
if (sparkPartitionId != 0 && !iterator.hasNext) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess this might be okay in that sense we are guaranteed partitions to be started from 0 up to my knowledge. Could you take a look and see if it makes sense to you cc @cloud-fan if you don't mind? I am not confident enough to proceed reviewing and leave a sign-off.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a little hacky but is the simplest fix I think.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

cc @yhuai too who reviewed my similar PR before.

// In case of empty job, leave first partition to save meta for file format like parquet.
new EmptyDirectoryWriteTask
} else if (description.partitionColumns.isEmpty && description.bucketIdExpression.isEmpty) {
new SingleDirectoryWriteTask(description, taskAttemptContext, committer)
} else {
new DynamicPartitionWriteTask(description, taskAttemptContext, committer)
Expand Down Expand Up @@ -301,6 +304,20 @@ object FileFormatWriter extends Logging {
}
}

/** ExecuteWriteTask for empty partitions */
private class EmptyDirectoryWriteTask extends ExecuteWriteTask {

override def execute(iter: Iterator[InternalRow]): ExecutedWriteSummary = {
ExecutedWriteSummary(
updatedPartitions = Set.empty,
numOutputFile = 0,
numOutputBytes = 0,
numOutputRows = 0)
}

override def releaseResources(): Unit = {}
}

/** Writes data to a single directory (used for non-dynamic-partition writes). */
private class SingleDirectoryWriteTask(
description: WriteJobDescription,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.{File, FilenameFilter}

import org.apache.spark.sql.QueryTest
import org.apache.spark.sql.test.SharedSQLContext

class FileFormatWriterSuite extends QueryTest with SharedSQLContext {

test("empty file should be skipped while write to file") {
withTempDir { dir =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

withTempPath can be used instead I believe.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1

dir.delete()
spark.range(10000).repartition(10).write.parquet(dir.toString)
val df = spark.read.parquet(dir.toString)
val allFiles = dir.listFiles(new FilenameFilter {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we just do this simpler? for example,

.listFiles().filter { f =>
  f.isFile && !f.getName.startsWith(".") && !f.getName.startsWith("_")
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Both is ok I think, just copy this from HadoopFsRelationSuite.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yea. If both are okay, let's go for the shorter one.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1 for the shorter one

override def accept(dir: File, name: String): Boolean = {
!name.startsWith(".") && !name.startsWith("_")
}
})
assert(allFiles.length == 10)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could I ask what this test targets? I think I am lost around here ...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Just make sure the source dir have many files, and the output dir only have 2 files.
If this make people confuse, just leave a notes and delete the assert?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

but I guess this one (the latter) does not test this change? If this test passes regardless of this PR change, I would rather remove this one.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

OK, I'll remove this assert and leave a note.


withTempDir { dst_dir =>
dst_dir.delete()
df.where("id = 50").write.parquet(dst_dir.toString)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would explicitly repartition here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why we need repartition?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was thinking just in order to make sure the (previous) number of files written out.

@HyukjinKwon HyukjinKwon Jul 18, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I mean.. for example, if we happen to have few partitions in the df in any event, I guess this test can become invalid ...

val allFiles = dst_dir.listFiles(new FilenameFilter {
override def accept(dir: File, name: String): Boolean = {
!name.startsWith(".") && !name.startsWith("_")
}
})
// First partition file and the data file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ideally we only need the first partition file if all other partitions are empty, but this is hard to do right now.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Can't agree more, firstly I try to implement like this but the FileFormatWriter.write can only see the iterator of each task self.

assert(allFiles.length == 2)
}
}
}
}