-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-10216][SQL] Avoid creating empty files during overwriting with group by query #12855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8018455
do not make empty file when insert overwrite into Hive table
sirpkt 46f085a
Merge branch 'master' into NoEmptyInsert
sirpkt 689252a
Merge remote-tracking branch 'upstream/master' into SPARK-10216
sirpkt e2749d7
change test name to reflect issue number and name
sirpkt acdc537
Stash chagnes
HyukjinKwon 9a89ed1
Rebase upstream
HyukjinKwon 57f2ecc
Add the function in hiveWriterContainers and polish test codes
HyukjinKwon 294b447
Edit variable names
HyukjinKwon ab2d092
Appropriate checking in test codes
HyukjinKwon dee6a4e
Do not write empty files for internal datasources as well.
HyukjinKwon b595b7f
Rename test in HadoopFsRelationTest
HyukjinKwon 857ba4d
Resolve conflicts
HyukjinKwon 24e16b7
Remove unused import
HyukjinKwon 9a61837
Fetch upstream
HyukjinKwon 5f780a7
Explicitly set shuffle partition
HyukjinKwon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -239,48 +239,50 @@ private[sql] class DefaultWriterContainer( | |
| extends BaseWriterContainer(relation, job, isAppend) { | ||
|
|
||
| def writeRows(taskContext: TaskContext, iterator: Iterator[InternalRow]): Unit = { | ||
| executorSideSetup(taskContext) | ||
| val configuration = taskAttemptContext.getConfiguration | ||
| configuration.set("spark.sql.sources.output.path", outputPath) | ||
| var writer = newOutputWriter(getWorkPath) | ||
| writer.initConverter(dataSchema) | ||
|
|
||
| // If anything below fails, we should abort the task. | ||
| try { | ||
| Utils.tryWithSafeFinallyAndFailureCallbacks { | ||
| while (iterator.hasNext) { | ||
| val internalRow = iterator.next() | ||
| writer.writeInternal(internalRow) | ||
| } | ||
| commitTask() | ||
| }(catchBlock = abortTask()) | ||
| } catch { | ||
| case t: Throwable => | ||
| throw new SparkException("Task failed while writing rows", t) | ||
| } | ||
| if (iterator.hasNext) { | ||
| executorSideSetup(taskContext) | ||
| val configuration = taskAttemptContext.getConfiguration | ||
| configuration.set("spark.sql.sources.output.path", outputPath) | ||
| var writer = newOutputWriter(getWorkPath) | ||
| writer.initConverter(dataSchema) | ||
|
|
||
| def commitTask(): Unit = { | ||
| // If anything below fails, we should abort the task. | ||
| try { | ||
| if (writer != null) { | ||
| writer.close() | ||
| writer = null | ||
| } | ||
| super.commitTask() | ||
| Utils.tryWithSafeFinallyAndFailureCallbacks { | ||
| while (iterator.hasNext) { | ||
| val internalRow = iterator.next() | ||
| writer.writeInternal(internalRow) | ||
| } | ||
| commitTask() | ||
| }(catchBlock = abortTask()) | ||
| } catch { | ||
| case cause: Throwable => | ||
| // This exception will be handled in `InsertIntoHadoopFsRelation.insert$writeRows`, and | ||
| // will cause `abortTask()` to be invoked. | ||
| throw new RuntimeException("Failed to commit task", cause) | ||
| case t: Throwable => | ||
| throw new SparkException("Task failed while writing rows", t) | ||
| } | ||
| } | ||
|
|
||
| def abortTask(): Unit = { | ||
| try { | ||
| if (writer != null) { | ||
| writer.close() | ||
| def commitTask(): Unit = { | ||
| try { | ||
| if (writer != null) { | ||
| writer.close() | ||
| writer = null | ||
| } | ||
| super.commitTask() | ||
| } catch { | ||
| case cause: Throwable => | ||
| // This exception will be handled in `InsertIntoHadoopFsRelation.insert$writeRows`, and | ||
| // will cause `abortTask()` to be invoked. | ||
| throw new RuntimeException("Failed to commit task", cause) | ||
| } | ||
| } | ||
|
|
||
| def abortTask(): Unit = { | ||
| try { | ||
| if (writer != null) { | ||
| writer.close() | ||
| } | ||
| } finally { | ||
| super.abortTask() | ||
| } | ||
| } finally { | ||
| super.abortTask() | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -363,84 +365,87 @@ private[sql] class DynamicPartitionWriterContainer( | |
| } | ||
|
|
||
| def writeRows(taskContext: TaskContext, iterator: Iterator[InternalRow]): Unit = { | ||
| executorSideSetup(taskContext) | ||
|
|
||
| // We should first sort by partition columns, then bucket id, and finally sorting columns. | ||
| val sortingExpressions: Seq[Expression] = partitionColumns ++ bucketIdExpression ++ sortColumns | ||
| val getSortingKey = UnsafeProjection.create(sortingExpressions, inputSchema) | ||
|
|
||
| val sortingKeySchema = StructType(sortingExpressions.map { | ||
| case a: Attribute => StructField(a.name, a.dataType, a.nullable) | ||
| // The sorting expressions are all `Attribute` except bucket id. | ||
| case _ => StructField("bucketId", IntegerType, nullable = false) | ||
| }) | ||
|
|
||
| // Returns the data columns to be written given an input row | ||
| val getOutputRow = UnsafeProjection.create(dataColumns, inputSchema) | ||
|
|
||
| // Returns the partition path given a partition key. | ||
| val getPartitionString = | ||
| UnsafeProjection.create(Concat(partitionStringExpression) :: Nil, partitionColumns) | ||
|
|
||
| // Sorts the data before write, so that we only need one writer at the same time. | ||
| // TODO: inject a local sort operator in planning. | ||
| val sorter = new UnsafeKVExternalSorter( | ||
| sortingKeySchema, | ||
| StructType.fromAttributes(dataColumns), | ||
| SparkEnv.get.blockManager, | ||
| SparkEnv.get.serializerManager, | ||
| TaskContext.get().taskMemoryManager().pageSizeBytes) | ||
|
|
||
| while (iterator.hasNext) { | ||
| val currentRow = iterator.next() | ||
| sorter.insertKV(getSortingKey(currentRow), getOutputRow(currentRow)) | ||
| } | ||
| logInfo(s"Sorting complete. Writing out partition files one at a time.") | ||
|
|
||
| val getBucketingKey: InternalRow => InternalRow = if (sortColumns.isEmpty) { | ||
| identity | ||
| } else { | ||
| UnsafeProjection.create(sortingExpressions.dropRight(sortColumns.length).zipWithIndex.map { | ||
| case (expr, ordinal) => BoundReference(ordinal, expr.dataType, expr.nullable) | ||
| if (iterator.hasNext) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here as well. Simply added |
||
| executorSideSetup(taskContext) | ||
|
|
||
| // We should first sort by partition columns, then bucket id, and finally sorting columns. | ||
| val sortingExpressions: Seq[Expression] = | ||
| partitionColumns ++ bucketIdExpression ++ sortColumns | ||
| val getSortingKey = UnsafeProjection.create(sortingExpressions, inputSchema) | ||
|
|
||
| val sortingKeySchema = StructType(sortingExpressions.map { | ||
| case a: Attribute => StructField(a.name, a.dataType, a.nullable) | ||
| // The sorting expressions are all `Attribute` except bucket id. | ||
| case _ => StructField("bucketId", IntegerType, nullable = false) | ||
| }) | ||
| } | ||
|
|
||
| val sortedIterator = sorter.sortedIterator() | ||
| // Returns the data columns to be written given an input row | ||
| val getOutputRow = UnsafeProjection.create(dataColumns, inputSchema) | ||
|
|
||
| // Returns the partition path given a partition key. | ||
| val getPartitionString = | ||
| UnsafeProjection.create(Concat(partitionStringExpression) :: Nil, partitionColumns) | ||
|
|
||
| // Sorts the data before write, so that we only need one writer at the same time. | ||
| // TODO: inject a local sort operator in planning. | ||
| val sorter = new UnsafeKVExternalSorter( | ||
| sortingKeySchema, | ||
| StructType.fromAttributes(dataColumns), | ||
| SparkEnv.get.blockManager, | ||
| SparkEnv.get.serializerManager, | ||
| TaskContext.get().taskMemoryManager().pageSizeBytes) | ||
|
|
||
| while (iterator.hasNext) { | ||
| val currentRow = iterator.next() | ||
| sorter.insertKV(getSortingKey(currentRow), getOutputRow(currentRow)) | ||
| } | ||
| logInfo(s"Sorting complete. Writing out partition files one at a time.") | ||
|
|
||
| val getBucketingKey: InternalRow => InternalRow = if (sortColumns.isEmpty) { | ||
| identity | ||
| } else { | ||
| UnsafeProjection.create(sortingExpressions.dropRight(sortColumns.length).zipWithIndex.map { | ||
| case (expr, ordinal) => BoundReference(ordinal, expr.dataType, expr.nullable) | ||
| }) | ||
| } | ||
|
|
||
| // If anything below fails, we should abort the task. | ||
| var currentWriter: OutputWriter = null | ||
| try { | ||
| Utils.tryWithSafeFinallyAndFailureCallbacks { | ||
| var currentKey: UnsafeRow = null | ||
| while (sortedIterator.next()) { | ||
| val nextKey = getBucketingKey(sortedIterator.getKey).asInstanceOf[UnsafeRow] | ||
| if (currentKey != nextKey) { | ||
| if (currentWriter != null) { | ||
| currentWriter.close() | ||
| currentWriter = null | ||
| } | ||
| currentKey = nextKey.copy() | ||
| logDebug(s"Writing partition: $currentKey") | ||
| val sortedIterator = sorter.sortedIterator() | ||
|
|
||
| currentWriter = newOutputWriter(currentKey, getPartitionString) | ||
| // If anything below fails, we should abort the task. | ||
| var currentWriter: OutputWriter = null | ||
| try { | ||
| Utils.tryWithSafeFinallyAndFailureCallbacks { | ||
| var currentKey: UnsafeRow = null | ||
| while (sortedIterator.next()) { | ||
| val nextKey = getBucketingKey(sortedIterator.getKey).asInstanceOf[UnsafeRow] | ||
| if (currentKey != nextKey) { | ||
| if (currentWriter != null) { | ||
| currentWriter.close() | ||
| currentWriter = null | ||
| } | ||
| currentKey = nextKey.copy() | ||
| logDebug(s"Writing partition: $currentKey") | ||
|
|
||
| currentWriter = newOutputWriter(currentKey, getPartitionString) | ||
| } | ||
| currentWriter.writeInternal(sortedIterator.getValue) | ||
| } | ||
| if (currentWriter != null) { | ||
| currentWriter.close() | ||
| currentWriter = null | ||
| } | ||
| currentWriter.writeInternal(sortedIterator.getValue) | ||
| } | ||
| if (currentWriter != null) { | ||
| currentWriter.close() | ||
| currentWriter = null | ||
| } | ||
|
|
||
| commitTask() | ||
| }(catchBlock = { | ||
| if (currentWriter != null) { | ||
| currentWriter.close() | ||
| } | ||
| abortTask() | ||
| }) | ||
| } catch { | ||
| case t: Throwable => | ||
| throw new SparkException("Task failed while writing rows", t) | ||
| commitTask() | ||
| }(catchBlock = { | ||
| if (currentWriter != null) { | ||
| currentWriter.close() | ||
| } | ||
| abortTask() | ||
| }) | ||
| } catch { | ||
| case t: Throwable => | ||
| throw new SparkException("Task failed while writing rows", t) | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simply added
iterator.hasNextcheck.