diff --git a/presto-main/src/main/java/com/facebook/presto/operator/TableWriterOperator.java b/presto-main/src/main/java/com/facebook/presto/operator/TableWriterOperator.java
index <HASH>..<HASH> 100644
--- a/presto-main/src/main/java/com/facebook/presto/operator/TableWriterOperator.java
+++ b/presto-main/src/main/java/com/facebook/presto/operator/TableWriterOperator.java
@@ -129,8 +129,7 @@ public class TableWriterOperator
         protected void doClose()
         {
             if (!closed.getAndSet(true)) {
-                checkState(!sourceIterator.hasNext(), "writer closed with source iterator still having data!");
-
+                checkState(operatorStats.isDone() || !sourceIterator.hasNext(), "Writer was closed while source iterator still has data and the operator is not done!");
                 commitFileHandle(fileHandle);
                 sourceIterator.close();
                 output.set(ImmutableSet.of(new TableWriterResult(input.get().getShardId(), nodeIdentifier)));
Fix close exception in table writer.

Only report a bad close (with sources still left) if it was not caused
by the operatorStats declare the query done anyway.