TAJO-1793: result row count unmatched for UNION ALL.

Closes #965
diff --git a/CHANGES b/CHANGES
index 1abac19..664747d 100644
--- a/CHANGES
+++ b/CHANGES
@@ -108,6 +108,8 @@
 
   BUG FIXES
 
+    TAJO-1793: result row count unmatched for UNION ALL. (jaehwa)
+
     TAJO-2082: Aggregation on a derived table which includes union can cause 
     incorrect result. (jihoon)
 
diff --git a/tajo-core-tests/src/test/java/org/apache/tajo/engine/query/TestUnionQuery.java b/tajo-core-tests/src/test/java/org/apache/tajo/engine/query/TestUnionQuery.java
index 7795d39..4cce141 100644
--- a/tajo-core-tests/src/test/java/org/apache/tajo/engine/query/TestUnionQuery.java
+++ b/tajo-core-tests/src/test/java/org/apache/tajo/engine/query/TestUnionQuery.java
@@ -18,15 +18,23 @@
 
 package org.apache.tajo.engine.query;
 
-import org.apache.tajo.IntegrationTest;
-import org.apache.tajo.QueryTestCaseBase;
-import org.apache.tajo.TajoConstants;
+import org.apache.hadoop.fs.ContentSummary;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.tajo.*;
+import org.apache.tajo.catalog.TableDesc;
+import org.apache.tajo.catalog.statistics.TableStats;
+import org.apache.tajo.conf.TajoConf;
+import org.apache.tajo.jdbc.TajoResultSetBase;
+import org.apache.tajo.master.QueryInfo;
+import org.junit.AfterClass;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
-import java.sql.ResultSet;
+import java.util.Optional;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 /*
  * Notations
@@ -41,660 +49,450 @@
 
   public TestUnionQuery() {
     super(TajoConstants.DEFAULT_DATABASE_NAME);
+    conf.setBoolVar(TajoConf.ConfVars.$DEBUG_ENABLED, true);
   }
 
-  @Test
+  @AfterClass
+  public static void tearDown() throws Exception {
+    conf.setBoolVar(TajoConf.ConfVars.$DEBUG_ENABLED, false);
+  }
+
   /**
    * S (SA U SA) O
    */
+  @Test
+  @SimpleTest
   public final void testUnionAll1() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 8L);
   }
 
-  @Test
   /**
    * S (S U S) O
    */
+  @Test
+  @SimpleTest
   public final void testUnionAll2() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 10L);
   }
 
-  @Test
   /**
    * S O ((S G) U (S G))
    */
+  @Test
+  @SimpleTest
   public final void testUnionAll3() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 2L);
   }
 
-  @Test
   /**
    * S G (S G)
    */
+  @Test
+  @SimpleTest
   public final void testUnionAll4() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 1L);
   }
 
-  @Test
   /**
    * S G (S F G)
    */
+  @Test
+  @SimpleTest
   public final void testUnionAll5() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 1L);
   }
 
-  @Test
   /**
    * S G (SA)
    */
+  @Test
+  @SimpleTest
   public final void testUnionAll6() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 1L);
   }
 
-  @Test
   /**
    * S (SA)
    */
+  @Test
+  @SimpleTest
   public final void testUnionAll7() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 10L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionAll8() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 1L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionAll9() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 5L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionAll10() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 20L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionAll11() throws Exception {
     // test filter pushdown
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 1L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionAll12() throws Exception {
     // test filter pushdown
     // with stage in union query
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 5L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionAll13() throws Exception {
     // test filter pushdown
     // with stage in union query
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 5L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionAll14() throws Exception {
     // test filter pushdown
     // with group by stage in union query
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 7L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionAll15() throws Exception {
     // test filter pushdown
     // with group by out of union query and join in union query
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 3L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionAll16() throws Exception {
     // test filter pushdown
     // with count distinct out of union query and join in union query
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 3L);
   }
 
-  @Test
   /**
    * S (SA U SA) O
    */
+  @Test
+  @SimpleTest
   public final void testUnion1() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 5L);
   }
 
-  @Test
   /**
    * S (S U S) O
    */
+  @Test
+  @SimpleTest
   public final void testUnion2() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 3L);
   }
 
-  @Test
   /**
    * S O ((S G) U (S G))
    */
+  @Test
+  @SimpleTest
   public final void testUnion3() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 2L);
   }
 
-  @Test
   /**
    * S G (S G)
    */
+  @Test
+  @SimpleTest
   public final void testUnion4() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 1L);
   }
 
-  @Test
   /**
    * S G (S F G)
    */
+  @Test
+  @SimpleTest
   public final void testUnion5() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 1L);
   }
 
-  @Test
   /**
    * S G (SA)
    */
+  @Test
+  @SimpleTest
   public final void testUnion6() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 1L);
   }
 
-  @Test
   /**
    * S (SA)
    */
+  @Test
+  @SimpleTest
   public final void testUnion7() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 3L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnion8() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 1L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnion9() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 5L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnion10() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 5L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnion11() throws Exception {
     // test filter pushdown
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 1L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnion12() throws Exception {
     // test filter pushdown
     // with stage in union query
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 5L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnion13() throws Exception {
     // test filter pushdown
     // with stage in union query
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 5L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnion14() throws Exception {
     // test filter pushdown
     // with group by stage in union query
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 7L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnion15() throws Exception {
     // test filter pushdown
     // with group by out of union query and join in union query
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 3L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnion16() throws Exception {
     // test filter pushdown
     // with count distinct out of union query and join in union query
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 3L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionAllWithSameAliasNames() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 10L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionAllWithDifferentAlias() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 2L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionAllWithDifferentAliasAndFunction() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 5L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionWithSameAliasNames() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 3L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionWithDifferentAlias() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 2L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionWithDifferentAliasAndFunction() throws Exception {
-    ResultSet res = executeQuery();
-    assertResultSet(res);
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 5L);
   }
 
   @Test
+  @SimpleTest
   public final void testLeftUnionWithJoin() throws Exception {
     // https://issues.apache.org/jira/browse/TAJO-881
-    ResultSet res = executeString(
-        "select * from ( " +
-        "  select a.id, b.c_name, a.code from ( " +
-        "    select l_orderkey as id, 'lineitem' as code from lineitem " +
-        "    union all " +
-        "    select o_orderkey as id, 'order' as code from orders " +
-         "  ) a " +
-         "  join customer b on a.id = b.c_custkey" +
-        ") c order by id, code"
-    );
-
-    String expected =
-        "id,c_name,code\n" +
-            "-------------------------------\n" +
-            "1,Customer#000000001,lineitem\n" +
-            "1,Customer#000000001,lineitem\n" +
-            "1,Customer#000000001,order\n" +
-            "2,Customer#000000002,lineitem\n" +
-            "2,Customer#000000002,order\n" +
-            "3,Customer#000000003,lineitem\n" +
-            "3,Customer#000000003,lineitem\n" +
-            "3,Customer#000000003,order\n";
-
-    assertEquals(expected, resultSetToString(res));
-
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 8L);
   }
 
   @Test
+  @SimpleTest
   public final void testRightUnionWithJoin() throws Exception {
     // https://issues.apache.org/jira/browse/TAJO-881
-    ResultSet res = executeString(
-            "select * from ( " +
-            "  select a.id, b.c_name, a.code from customer b " +
-            "  join ( " +
-            "    select l_orderkey as id, 'lineitem' as code from lineitem " +
-            "    union all " +
-            "    select o_orderkey as id, 'order' as code from orders " +
-            "  ) a on a.id = b.c_custkey" +
-            ") c order by id, code"
-    );
-
-    String expected =
-        "id,c_name,code\n" +
-            "-------------------------------\n" +
-            "1,Customer#000000001,lineitem\n" +
-            "1,Customer#000000001,lineitem\n" +
-            "1,Customer#000000001,order\n" +
-            "2,Customer#000000002,lineitem\n" +
-            "2,Customer#000000002,order\n" +
-            "3,Customer#000000003,lineitem\n" +
-            "3,Customer#000000003,lineitem\n" +
-            "3,Customer#000000003,order\n";
-
-    assertEquals(expected, resultSetToString(res));
-
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 8L);
   }
 
   @Test
+  @SimpleTest
   public final void testAllUnionWithJoin() throws Exception {
     // https://issues.apache.org/jira/browse/TAJO-881
-    ResultSet res = executeString(
-        "select * from ( " +
-        "  select a.id, a.code as code, b.name, b.code as code2 from ( " +
-        "    select l_orderkey as id, 'lineitem' as code from lineitem " +
-        "    union all " +
-        "    select o_orderkey as id, 'order' as code from orders " +
-        "  ) a " +
-        "  join ( " +
-        "    select c_custkey as id, c_name as name, 'customer' as code from customer " +
-        "    union all " +
-        "    select p_partkey as id, p_name as name, 'part' as code from part " +
-        "  ) b on a.id = b.id" +
-        ") c order by id, code, code2"
-    );
-
-    String expected =
-        "id,code,name,code2\n" +
-            "-------------------------------\n" +
-            "1,lineitem,Customer#000000001,customer\n" +
-            "1,lineitem,Customer#000000001,customer\n" +
-            "1,lineitem,goldenrod lavender spring chocolate lace,part\n" +
-            "1,lineitem,goldenrod lavender spring chocolate lace,part\n" +
-            "1,order,Customer#000000001,customer\n" +
-            "1,order,goldenrod lavender spring chocolate lace,part\n" +
-            "2,lineitem,Customer#000000002,customer\n" +
-            "2,lineitem,blush thistle blue yellow saddle,part\n" +
-            "2,order,Customer#000000002,customer\n" +
-            "2,order,blush thistle blue yellow saddle,part\n" +
-            "3,lineitem,Customer#000000003,customer\n" +
-            "3,lineitem,Customer#000000003,customer\n" +
-            "3,lineitem,spring green yellow purple cornsilk,part\n" +
-            "3,lineitem,spring green yellow purple cornsilk,part\n" +
-            "3,order,Customer#000000003,customer\n" +
-            "3,order,spring green yellow purple cornsilk,part\n";
-
-    assertEquals(expected, resultSetToString(res));
-
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 16L);
   }
 
   @Test
+  @SimpleTest
   public final void testUnionWithCrossJoin() throws Exception {
     // https://issues.apache.org/jira/browse/TAJO-881
-    ResultSet res = executeString(
-        "select * from ( " +
-        "  select a.id, b.c_name, a.code from ( " +
-            "    select l_orderkey as id, 'lineitem' as code from lineitem " +
-            "    union all " +
-            "    select o_orderkey as id, 'order' as code from orders " +
-            "  ) a, " +
-            "  customer b " +
-        ") c order by id, code, c_name"
-    );
-
-    String expected =
-        "id,c_name,code\n" +
-            "-------------------------------\n" +
-            "1,Customer#000000001,lineitem\n" +
-            "1,Customer#000000001,lineitem\n" +
-            "1,Customer#000000002,lineitem\n" +
-            "1,Customer#000000002,lineitem\n" +
-            "1,Customer#000000003,lineitem\n" +
-            "1,Customer#000000003,lineitem\n" +
-            "1,Customer#000000004,lineitem\n" +
-            "1,Customer#000000004,lineitem\n" +
-            "1,Customer#000000005,lineitem\n" +
-            "1,Customer#000000005,lineitem\n" +
-            "1,Customer#000000001,order\n" +
-            "1,Customer#000000002,order\n" +
-            "1,Customer#000000003,order\n" +
-            "1,Customer#000000004,order\n" +
-            "1,Customer#000000005,order\n" +
-            "2,Customer#000000001,lineitem\n" +
-            "2,Customer#000000002,lineitem\n" +
-            "2,Customer#000000003,lineitem\n" +
-            "2,Customer#000000004,lineitem\n" +
-            "2,Customer#000000005,lineitem\n" +
-            "2,Customer#000000001,order\n" +
-            "2,Customer#000000002,order\n" +
-            "2,Customer#000000003,order\n" +
-            "2,Customer#000000004,order\n" +
-            "2,Customer#000000005,order\n" +
-            "3,Customer#000000001,lineitem\n" +
-            "3,Customer#000000001,lineitem\n" +
-            "3,Customer#000000002,lineitem\n" +
-            "3,Customer#000000002,lineitem\n" +
-            "3,Customer#000000003,lineitem\n" +
-            "3,Customer#000000003,lineitem\n" +
-            "3,Customer#000000004,lineitem\n" +
-            "3,Customer#000000004,lineitem\n" +
-            "3,Customer#000000005,lineitem\n" +
-            "3,Customer#000000005,lineitem\n" +
-            "3,Customer#000000001,order\n" +
-            "3,Customer#000000002,order\n" +
-            "3,Customer#000000003,order\n" +
-            "3,Customer#000000004,order\n" +
-            "3,Customer#000000005,order\n";
-
-    assertEquals(expected, resultSetToString(res));
-
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 40L);
   }
 
   @Test
+  @SimpleTest
   public final void testThreeJoinInUnion() throws Exception {
     // https://issues.apache.org/jira/browse/TAJO-881
-    ResultSet res = executeString(
-      "select o_orderkey from (select orders.o_orderkey \n" +
-          "from orders\n" +
-          "join lineitem on orders.o_orderkey = lineitem.l_orderkey\n" +
-          "join customer on orders.o_custkey =  customer.c_custkey\n" +
-          "union all \n" +
-          "select nation.n_nationkey from nation) t order by o_orderkey"
-    );
-    String expected =
-        "o_orderkey\n" +
-            "-------------------------------\n" +
-            "0\n" +
-            "1\n" +
-            "1\n" +
-            "1\n" +
-            "2\n" +
-            "2\n" +
-            "3\n" +
-            "3\n" +
-            "3\n" +
-            "4\n" +
-            "5\n" +
-            "6\n" +
-            "7\n" +
-            "8\n" +
-            "9\n" +
-            "10\n" +
-            "11\n" +
-            "12\n" +
-            "13\n" +
-            "14\n" +
-            "15\n" +
-            "16\n" +
-            "17\n" +
-            "18\n" +
-            "19\n" +
-            "20\n" +
-            "21\n" +
-            "22\n" +
-            "23\n" +
-            "24\n";
-
-    assertEquals(expected, resultSetToString(res));
-
-    cleanupQuery(res);
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 30L);
   }
 
   @Test
+  @SimpleTest
   public void testUnionCaseOfFirstEmptyAndJoin() throws Exception {
-    ResultSet res = executeString(
-        "select a.c_custkey, b.c_custkey from " +
-            "  (select c_custkey, c_nationkey from customer where c_nationkey < 0 " +
-            "   union all " +
-            "   select c_custkey, c_nationkey from customer where c_nationkey > 0 " +
-            ") a " +
-            "left outer join customer b " +
-            "on a.c_custkey = b.c_custkey "
-    );
-
-    String expected =
-        "c_custkey,c_custkey\n" +
-            "-------------------------------\n" +
-            "1,1\n" +
-            "2,2\n" +
-            "3,3\n" +
-            "4,4\n" +
-            "5,5\n";
-
-    assertEquals(expected, resultSetToString(res));
-    res.close();
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 5L);
   }
 
   @Test
+  @SimpleTest
   public void testTajo1368Case1() throws Exception {
-    ResultSet res = executeString(
-        "select * from " +
-            "   (select c_custkey, c_nationkey from customer where c_nationkey < 0 " +
-            "   union all " +
-            "   select c_custkey, c_nationkey from customer where c_nationkey > 0 " +
-            ") a " +
-            "union all " +
-            "select * from " +
-            "   (select c_custkey, c_nationkey from customer where c_nationkey < 0 " +
-            "   union all " +
-            "   select c_custkey, c_nationkey from customer where c_nationkey > 0 " +
-            ") b ");
-
-    String expected = "c_custkey,c_nationkey\n" +
-        "-------------------------------\n" +
-        "1,15\n" +
-        "2,13\n" +
-        "3,1\n" +
-        "4,4\n" +
-        "5,3\n" +
-        "1,15\n" +
-        "2,13\n" +
-        "3,1\n" +
-        "4,4\n" +
-        "5,3\n";
-
-    assertEquals(expected, resultSetToString(res));
-    res.close();
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 10L);
   }
 
   @Test
+  @SimpleTest
   public void testTajo1368Case2() throws Exception {
-    ResultSet res = executeString("select * from ( "+
-        "select c_custkey, c_nationkey from ( " +
-        "select c_custkey, c_nationkey from ( " +
-        "select c_custkey, c_nationkey from customer) a " +
-        "union all " +
-        "select c_custkey, c_nationkey from ( " +
-        "select c_custkey, c_nationkey from customer) a " +
-        " ) a " +
-        " ) a ");
-
-    String expected = "c_custkey,c_nationkey\n" +
-        "-------------------------------\n" +
-        "1,15\n" +
-        "2,13\n" +
-        "3,1\n" +
-        "4,4\n" +
-        "5,3\n" +
-        "1,15\n" +
-        "2,13\n" +
-        "3,1\n" +
-        "4,4\n" +
-        "5,3\n";
-
-    assertEquals(expected, resultSetToString(res));
-    res.close();
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 10L);
   }
 
   @Test
   @Option(withExplain = true, withExplainGlobal = true)
   @SimpleTest
   public void testComplexUnion1() throws Exception {
-    runSimpleTests();
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 4L);
   }
 
   @Test
   @Option(withExplain = true, withExplainGlobal = true, sort = true)
   @SimpleTest
   public void testComplexUnion2() throws Exception {
-    runSimpleTests();
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 5L);
   }
 
   @Test
   @Option(withExplain =  true, sort = true)
   @SimpleTest
   public void testUnionAndFilter() throws Exception {
-    runSimpleTests();
+    Optional<TajoResultSetBase[]> existing = runSimpleTests();
+    verifyResultStats(existing, 5L);
   }
+
+  private void verifyResultStats(Optional<TajoResultSetBase[]> existing, long numRows) throws Exception {
+    assertTrue(existing.isPresent());
+
+    // Get TableStats using TajoResultSetBase.
+    TajoResultSetBase[] resultSet = existing.get();
+    QueryId qid = resultSet[0].getQueryId();
+    QueryInfo queryInfo = testingCluster.getMaster().getContext().getQueryJobManager().getFinishedQuery(qid);
+    TableDesc desc = queryInfo.getResultDesc();
+    TableStats stats = desc.getStats();
+
+    // Compare specified number of rows to the number of rows in TableStats.
+    assertEquals(numRows, stats.getNumRows().longValue());
+
+    // Compare the volume number of directRaw to the number of rows in TableStats.
+    FileSystem fs = FileSystem.get(conf);
+    Path path = new Path(desc.getUri());
+    assertTrue(fs.exists(path));
+    ContentSummary summary = fs.getContentSummary(path);
+    assertEquals(summary.getLength(), stats.getNumBytes().longValue());
+
+    closeResultSets(resultSet);
+  }
+
 }
diff --git a/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testAllUnionWithJoin.sql b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testAllUnionWithJoin.sql
new file mode 100644
index 0000000..38066d0
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testAllUnionWithJoin.sql
@@ -0,0 +1,12 @@
+select * from ( 
+  select a.id, a.code as code, b.name, b.code as code2 from ( 
+    select l_orderkey as id, 'lineitem' as code from lineitem 
+    union all 
+    select o_orderkey as id, 'order' as code from orders 
+  ) a 
+  join ( 
+    select c_custkey as id, c_name as name, 'customer' as code from customer 
+    union all 
+    select p_partkey as id, p_name as name, 'part' as code from part 
+  ) b on a.id = b.id
+) c order by id, code, code2
\ No newline at end of file
diff --git a/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testLeftUnionWithJoin.sql b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testLeftUnionWithJoin.sql
new file mode 100644
index 0000000..83c5f2f
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testLeftUnionWithJoin.sql
@@ -0,0 +1,8 @@
+select * from ( 
+  select a.id, b.c_name, a.code from ( 
+    select l_orderkey as id, 'lineitem' as code from lineitem 
+    union all 
+    select o_orderkey as id, 'order' as code from orders 
+   ) a 
+   join customer b on a.id = b.c_custkey
+) c order by id, code
\ No newline at end of file
diff --git a/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testRightUnionWithJoin.sql b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testRightUnionWithJoin.sql
new file mode 100644
index 0000000..de42007
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testRightUnionWithJoin.sql
@@ -0,0 +1,8 @@
+select * from ( 
+  select a.id, b.c_name, a.code from customer b 
+  join ( 
+    select l_orderkey as id, 'lineitem' as code from lineitem 
+    union all 
+    select o_orderkey as id, 'order' as code from orders 
+  ) a on a.id = b.c_custkey
+) c order by id, code
\ No newline at end of file
diff --git a/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testTajo1368Case1.sql b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testTajo1368Case1.sql
new file mode 100644
index 0000000..23db995
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testTajo1368Case1.sql
@@ -0,0 +1,11 @@
+select * from 
+       (select c_custkey, c_nationkey from customer where c_nationkey < 0 
+       union all 
+       select c_custkey, c_nationkey from customer where c_nationkey > 0 
+    ) a 
+    union all 
+    select * from 
+       (select c_custkey, c_nationkey from customer where c_nationkey < 0 
+       union all 
+       select c_custkey, c_nationkey from customer where c_nationkey > 0 
+    ) b
diff --git a/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testTajo1368Case2.sql b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testTajo1368Case2.sql
new file mode 100644
index 0000000..f051b1c
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testTajo1368Case2.sql
@@ -0,0 +1,9 @@
+select * from ( 
+  select c_custkey, c_nationkey from ( 
+  select c_custkey, c_nationkey from ( 
+  select c_custkey, c_nationkey from customer) a 
+  union all 
+  select c_custkey, c_nationkey from ( 
+  select c_custkey, c_nationkey from customer) a 
+   ) a 
+   ) a 
\ No newline at end of file
diff --git a/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testThreeJoinInUnion.sql b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testThreeJoinInUnion.sql
new file mode 100644
index 0000000..2b151a8
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testThreeJoinInUnion.sql
@@ -0,0 +1,7 @@
+select o_orderkey from (select orders.o_orderkey 
+    from orders
+    join lineitem on orders.o_orderkey = lineitem.l_orderkey
+    join customer on orders.o_custkey =  customer.c_custkey
+    union all 
+    select nation.n_nationkey from nation
+) t order by o_orderkey
\ No newline at end of file
diff --git a/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testUnionCaseOfFirstEmptyAndJoin.sql b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testUnionCaseOfFirstEmptyAndJoin.sql
new file mode 100644
index 0000000..8adcc7b
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testUnionCaseOfFirstEmptyAndJoin.sql
@@ -0,0 +1,7 @@
+select a.c_custkey, b.c_custkey from 
+      (select c_custkey, c_nationkey from customer where c_nationkey < 0 
+       union all 
+       select c_custkey, c_nationkey from customer where c_nationkey > 0 
+    ) a 
+    left outer join customer b 
+    on a.c_custkey = b.c_custkey 
\ No newline at end of file
diff --git a/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testUnionWithCrossJoin.sql b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testUnionWithCrossJoin.sql
new file mode 100644
index 0000000..1b43844
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/queries/TestUnionQuery/testUnionWithCrossJoin.sql
@@ -0,0 +1,8 @@
+select * from ( 
+  select a.id, b.c_name, a.code from ( 
+      select l_orderkey as id, 'lineitem' as code from lineitem 
+      union all 
+      select o_orderkey as id, 'order' as code from orders 
+    ) a, 
+    customer b 
+) c order by id, code, c_name
\ No newline at end of file
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testAllUnionWithJoin.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testAllUnionWithJoin.result
new file mode 100644
index 0000000..6473605
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testAllUnionWithJoin.result
@@ -0,0 +1,18 @@
+id,code,name,code2
+-------------------------------
+1,lineitem,Customer#000000001,customer
+1,lineitem,Customer#000000001,customer
+1,lineitem,goldenrod lavender spring chocolate lace,part
+1,lineitem,goldenrod lavender spring chocolate lace,part
+1,order,Customer#000000001,customer
+1,order,goldenrod lavender spring chocolate lace,part
+2,lineitem,Customer#000000002,customer
+2,lineitem,blush thistle blue yellow saddle,part
+2,order,Customer#000000002,customer
+2,order,blush thistle blue yellow saddle,part
+3,lineitem,Customer#000000003,customer
+3,lineitem,Customer#000000003,customer
+3,lineitem,spring green yellow purple cornsilk,part
+3,lineitem,spring green yellow purple cornsilk,part
+3,order,Customer#000000003,customer
+3,order,spring green yellow purple cornsilk,part
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testLeftUnionWithJoin.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testLeftUnionWithJoin.result
new file mode 100644
index 0000000..5e6ba56
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testLeftUnionWithJoin.result
@@ -0,0 +1,10 @@
+id,c_name,code
+-------------------------------
+1,Customer#000000001,lineitem
+1,Customer#000000001,lineitem
+1,Customer#000000001,order
+2,Customer#000000002,lineitem
+2,Customer#000000002,order
+3,Customer#000000003,lineitem
+3,Customer#000000003,lineitem
+3,Customer#000000003,order
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testRightUnionWithJoin.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testRightUnionWithJoin.result
new file mode 100644
index 0000000..5e6ba56
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testRightUnionWithJoin.result
@@ -0,0 +1,10 @@
+id,c_name,code
+-------------------------------
+1,Customer#000000001,lineitem
+1,Customer#000000001,lineitem
+1,Customer#000000001,order
+2,Customer#000000002,lineitem
+2,Customer#000000002,order
+3,Customer#000000003,lineitem
+3,Customer#000000003,lineitem
+3,Customer#000000003,order
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testTajo1368Case1.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testTajo1368Case1.result
new file mode 100644
index 0000000..17f8c54
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testTajo1368Case1.result
@@ -0,0 +1,12 @@
+c_custkey,c_nationkey
+-------------------------------
+1,15
+2,13
+3,1
+4,4
+5,3
+1,15
+2,13
+3,1
+4,4
+5,3
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testTajo1368Case2.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testTajo1368Case2.result
new file mode 100644
index 0000000..17f8c54
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testTajo1368Case2.result
@@ -0,0 +1,12 @@
+c_custkey,c_nationkey
+-------------------------------
+1,15
+2,13
+3,1
+4,4
+5,3
+1,15
+2,13
+3,1
+4,4
+5,3
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testThreeJoinInUnion.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testThreeJoinInUnion.result
new file mode 100644
index 0000000..28fe466
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testThreeJoinInUnion.result
@@ -0,0 +1,32 @@
+o_orderkey
+-------------------------------
+0
+1
+1
+1
+2
+2
+3
+3
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion1.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion1.result
index a5282c5..e748798 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion1.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion1.result
@@ -4,4 +4,4 @@
 2
 3
 4
-5
\ No newline at end of file
+5
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion10.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion10.result
index 279446e..379e202 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion10.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion10.result
@@ -4,4 +4,4 @@
 5,ETHIOPIA
 14,KENYA
 15,MOROCCO
-16,MOZAMBIQUE
\ No newline at end of file
+16,MOZAMBIQUE
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion11.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion11.result
index 6e8d2cd..940d66b 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion11.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion11.result
@@ -1,3 +1,3 @@
 col1,col2,col3
 -------------------------------
-R,46796.47,1993-11-24F
\ No newline at end of file
+R,46796.47,1993-11-24F
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion12.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion12.result
index 4050574..4a4a2ad 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion12.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion12.result
@@ -4,4 +4,4 @@
 PROMO BURNISHED COPPER,90100.0,1993goldenrod lavender spring chocolate lace
 LARGE BRUSHED BRASS,90200.0,1993blush thistle blue yellow saddle
 STANDARD POLISHED BRASS,90300.0,1993spring green yellow purple cornsilk
-SMALL PLATED BRASS,90400.0,1993cornflower chocolate smoke green pink
\ No newline at end of file
+SMALL PLATED BRASS,90400.0,1993cornflower chocolate smoke green pink
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion13.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion13.result
index 4050574..4a4a2ad 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion13.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion13.result
@@ -4,4 +4,4 @@
 PROMO BURNISHED COPPER,90100.0,1993goldenrod lavender spring chocolate lace
 LARGE BRUSHED BRASS,90200.0,1993blush thistle blue yellow saddle
 STANDARD POLISHED BRASS,90300.0,1993spring green yellow purple cornsilk
-SMALL PLATED BRASS,90400.0,1993cornflower chocolate smoke green pink
\ No newline at end of file
+SMALL PLATED BRASS,90400.0,1993cornflower chocolate smoke green pink
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion14.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion14.result
index 3838ab4..bde5eb1 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion14.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion14.result
@@ -6,4 +6,4 @@
 3,5
 4,5
 N,3
-R,2
\ No newline at end of file
+R,2
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion15.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion15.result
index 4e4d9e9..6877fd1 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion15.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion15.result
@@ -2,4 +2,4 @@
 -------------------------------
 3,5
 4,5
-N,3
\ No newline at end of file
+N,3
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion16.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion16.result
index 38be978..829799e 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion16.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion16.result
@@ -2,4 +2,4 @@
 -------------------------------
 3,5
 4,5
-N,2
\ No newline at end of file
+N,2
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion2.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion2.result
index df55931..80d5d1c 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion2.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion2.result
@@ -2,4 +2,4 @@
 -------------------------------
 1
 2
-3
\ No newline at end of file
+3
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion3.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion3.result
index 2562bd4..9df1273 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion3.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion3.result
@@ -1,4 +1,4 @@
 total
 -------------------------------
 3
-5
\ No newline at end of file
+5
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion4.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion4.result
index 22227ba..277e846 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion4.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion4.result
@@ -1,3 +1,3 @@
 ?count
 -------------------------------
-2
\ No newline at end of file
+2
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion5.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion5.result
index 22227ba..277e846 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion5.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion5.result
@@ -1,3 +1,3 @@
 ?count
 -------------------------------
-2
\ No newline at end of file
+2
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion6.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion6.result
index 19336a7..7044801 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion6.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion6.result
@@ -1,3 +1,3 @@
 ?count
 -------------------------------
-5
\ No newline at end of file
+5
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion7.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion7.result
index fe633a4..d2c94c7 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion7.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion7.result
@@ -2,4 +2,4 @@
 -------------------------------
 1
 2
-3
\ No newline at end of file
+3
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion8.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion8.result
index ba35aa1..79a5ee2 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion8.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion8.result
@@ -1,3 +1,3 @@
 l_returnflag,l_linestatus
 -------------------------------
-R,F
\ No newline at end of file
+R,F
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion9.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion9.result
index 482dd52..f8109d6 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion9.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnion9.result
@@ -4,4 +4,4 @@
 15,MOROCCO
 14,KENYA
 5,ETHIOPIA
-0,ALGERIA
\ No newline at end of file
+0,ALGERIA
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll1.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll1.result
index f384e9f6..a8ca9a8 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll1.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll1.result
@@ -7,4 +7,4 @@
 3
 4
 4
-5
\ No newline at end of file
+5
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll10.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll10.result
index effe340..fa7a538 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll10.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll10.result
@@ -19,4 +19,4 @@
 16,MOZAMBIQUE
 16,MOZAMBIQUE
 16,MOZAMBIQUE
-16,MOZAMBIQUE
\ No newline at end of file
+16,MOZAMBIQUE
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll11.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll11.result
index 6e8d2cd..940d66b 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll11.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll11.result
@@ -1,3 +1,3 @@
 col1,col2,col3
 -------------------------------
-R,46796.47,1993-11-24F
\ No newline at end of file
+R,46796.47,1993-11-24F
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll12.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll12.result
index 4050574..4a4a2ad 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll12.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll12.result
@@ -4,4 +4,4 @@
 PROMO BURNISHED COPPER,90100.0,1993goldenrod lavender spring chocolate lace
 LARGE BRUSHED BRASS,90200.0,1993blush thistle blue yellow saddle
 STANDARD POLISHED BRASS,90300.0,1993spring green yellow purple cornsilk
-SMALL PLATED BRASS,90400.0,1993cornflower chocolate smoke green pink
\ No newline at end of file
+SMALL PLATED BRASS,90400.0,1993cornflower chocolate smoke green pink
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll13.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll13.result
index 4050574..4a4a2ad 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll13.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll13.result
@@ -4,4 +4,4 @@
 PROMO BURNISHED COPPER,90100.0,1993goldenrod lavender spring chocolate lace
 LARGE BRUSHED BRASS,90200.0,1993blush thistle blue yellow saddle
 STANDARD POLISHED BRASS,90300.0,1993spring green yellow purple cornsilk
-SMALL PLATED BRASS,90400.0,1993cornflower chocolate smoke green pink
\ No newline at end of file
+SMALL PLATED BRASS,90400.0,1993cornflower chocolate smoke green pink
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll14.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll14.result
index 3838ab4..bde5eb1 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll14.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll14.result
@@ -6,4 +6,4 @@
 3,5
 4,5
 N,3
-R,2
\ No newline at end of file
+R,2
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll15.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll15.result
index 4e4d9e9..6877fd1 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll15.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll15.result
@@ -2,4 +2,4 @@
 -------------------------------
 3,5
 4,5
-N,3
\ No newline at end of file
+N,3
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll16.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll16.result
index 38be978..829799e 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll16.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll16.result
@@ -2,4 +2,4 @@
 -------------------------------
 3,5
 4,5
-N,2
\ No newline at end of file
+N,2
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll2.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll2.result
index d269baa..966974a 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll2.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll2.result
@@ -9,4 +9,4 @@
 3
 3
 3
-3
\ No newline at end of file
+3
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll3.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll3.result
index 2562bd4..9df1273 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll3.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll3.result
@@ -1,4 +1,4 @@
 total
 -------------------------------
 3
-5
\ No newline at end of file
+5
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll4.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll4.result
index 22227ba..277e846 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll4.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll4.result
@@ -1,3 +1,3 @@
 ?count
 -------------------------------
-2
\ No newline at end of file
+2
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll5.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll5.result
index 22227ba..277e846 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll5.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll5.result
@@ -1,3 +1,3 @@
 ?count
 -------------------------------
-2
\ No newline at end of file
+2
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll6.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll6.result
index 7ccc8c2..b6b8744 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll6.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll6.result
@@ -1,3 +1,3 @@
 ?count
 -------------------------------
-10
\ No newline at end of file
+10
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll7.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll7.result
index 6a3fbd5..1566a65 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll7.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll7.result
@@ -9,4 +9,4 @@
 3
 3
 3
-3
\ No newline at end of file
+3
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll8.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll8.result
index ba35aa1..79a5ee2 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll8.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll8.result
@@ -1,3 +1,3 @@
 l_returnflag,l_linestatus
 -------------------------------
-R,F
\ No newline at end of file
+R,F
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll9.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll9.result
index 482dd52..f8109d6 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll9.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAll9.result
@@ -4,4 +4,4 @@
 15,MOROCCO
 14,KENYA
 5,ETHIOPIA
-0,ALGERIA
\ No newline at end of file
+0,ALGERIA
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAlias.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAlias.result
index 7415ec4..4cb03f2 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAlias.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAlias.result
@@ -1,4 +1,4 @@
 col1,col2
 -------------------------------
 N,O
-R,F
\ No newline at end of file
+R,F
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAliasAndFunction.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAliasAndFunction.result
index 7e6f6cd..4ff63d8 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAliasAndFunction.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAliasAndFunction.result
@@ -4,4 +4,4 @@
 N,O1996-04-12
 N,O1997-01-28
 R1993-11-09,F
-R1994-02-02,F
\ No newline at end of file
+R1994-02-02,F
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAllWithSameAliasNames.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAllWithSameAliasNames.result
index d269baa..966974a 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAllWithSameAliasNames.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionAllWithSameAliasNames.result
@@ -9,4 +9,4 @@
 3
 3
 3
-3
\ No newline at end of file
+3
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionCaseOfFirstEmptyAndJoin.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionCaseOfFirstEmptyAndJoin.result
new file mode 100644
index 0000000..c31d5e7
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionCaseOfFirstEmptyAndJoin.result
@@ -0,0 +1,7 @@
+c_custkey,c_custkey
+-------------------------------
+1,1
+2,2
+3,3
+4,4
+5,5
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithCrossJoin.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithCrossJoin.result
new file mode 100644
index 0000000..551dcb4
--- /dev/null
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithCrossJoin.result
@@ -0,0 +1,42 @@
+id,c_name,code
+-------------------------------
+1,Customer#000000001,lineitem
+1,Customer#000000001,lineitem
+1,Customer#000000002,lineitem
+1,Customer#000000002,lineitem
+1,Customer#000000003,lineitem
+1,Customer#000000003,lineitem
+1,Customer#000000004,lineitem
+1,Customer#000000004,lineitem
+1,Customer#000000005,lineitem
+1,Customer#000000005,lineitem
+1,Customer#000000001,order
+1,Customer#000000002,order
+1,Customer#000000003,order
+1,Customer#000000004,order
+1,Customer#000000005,order
+2,Customer#000000001,lineitem
+2,Customer#000000002,lineitem
+2,Customer#000000003,lineitem
+2,Customer#000000004,lineitem
+2,Customer#000000005,lineitem
+2,Customer#000000001,order
+2,Customer#000000002,order
+2,Customer#000000003,order
+2,Customer#000000004,order
+2,Customer#000000005,order
+3,Customer#000000001,lineitem
+3,Customer#000000001,lineitem
+3,Customer#000000002,lineitem
+3,Customer#000000002,lineitem
+3,Customer#000000003,lineitem
+3,Customer#000000003,lineitem
+3,Customer#000000004,lineitem
+3,Customer#000000004,lineitem
+3,Customer#000000005,lineitem
+3,Customer#000000005,lineitem
+3,Customer#000000001,order
+3,Customer#000000002,order
+3,Customer#000000003,order
+3,Customer#000000004,order
+3,Customer#000000005,order
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithDifferentAlias.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithDifferentAlias.result
index 7415ec4..4cb03f2 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithDifferentAlias.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithDifferentAlias.result
@@ -1,4 +1,4 @@
 col1,col2
 -------------------------------
 N,O
-R,F
\ No newline at end of file
+R,F
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithDifferentAliasAndFunction.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithDifferentAliasAndFunction.result
index 7e6f6cd..4ff63d8 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithDifferentAliasAndFunction.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithDifferentAliasAndFunction.result
@@ -4,4 +4,4 @@
 N,O1996-04-12
 N,O1997-01-28
 R1993-11-09,F
-R1994-02-02,F
\ No newline at end of file
+R1994-02-02,F
diff --git a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithSameAliasNames.result b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithSameAliasNames.result
index df55931..80d5d1c 100644
--- a/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithSameAliasNames.result
+++ b/tajo-core-tests/src/test/resources/results/TestUnionQuery/testUnionWithSameAliasNames.result
@@ -2,4 +2,4 @@
 -------------------------------
 1
 2
-3
\ No newline at end of file
+3
diff --git a/tajo-core/src/main/java/org/apache/tajo/querymaster/Query.java b/tajo-core/src/main/java/org/apache/tajo/querymaster/Query.java
index e370a29..d57040e 100644
--- a/tajo-core/src/main/java/org/apache/tajo/querymaster/Query.java
+++ b/tajo-core/src/main/java/org/apache/tajo/querymaster/Query.java
@@ -37,6 +37,7 @@
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.PartitionDescProto;
 import org.apache.tajo.catalog.proto.CatalogProtos.UpdateTableStatsProto;
+import org.apache.tajo.catalog.statistics.StatisticsUtil;
 import org.apache.tajo.catalog.statistics.TableStats;
 import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.engine.planner.global.ExecutionBlock;
@@ -402,6 +403,30 @@
     }
   }
 
+  public boolean hasUnionPlan() throws Exception {
+    boolean result = false;
+
+    // In case of UNION statement or UNION ALL statement, the terminal block has two more child blocks.
+    ExecutionBlock terminalBlock = plan.getTerminalBlock();
+    if (plan.getChilds(terminalBlock.getId()).size() >= 2) {
+      result = true;
+    }
+    return result;
+  }
+
+  public TableStats aggregateTableStatsOfTerminalBlock() throws Exception {
+    TableStats aggregated = new TableStats();
+
+    ExecutionBlock terminalBlock = plan.getTerminalBlock();
+    List<ExecutionBlock> childBlocks = plan.getChilds(terminalBlock.getId());
+    for(ExecutionBlock childBlock : childBlocks) {
+      Stage childStage = getStage(childBlock.getId());
+      StatisticsUtil.aggregateTableStat(aggregated, childStage.getResultStats());
+    }
+
+    return aggregated;
+  }
+
   /* non-blocking call for client API */
   public QueryState getState() {
     return queryState;
@@ -653,13 +678,19 @@
         TableDesc resultTableDesc =
             new TableDesc(
                 query.getId().toString(),
-                lastStage.getOutSchema(),
+              lastStage.getOutSchema(),
                 meta,
                 finalOutputDir.toUri());
         resultTableDesc.setExternal(true);
 
-        stats.setNumBytes(getTableVolume(query.systemConf, finalOutputDir));
-        resultTableDesc.setStats(stats);
+        if (query.hasUnionPlan()) {
+          TableStats aggregated = query.aggregateTableStatsOfTerminalBlock();
+          resultTableDesc.setStats(aggregated);
+        } else {
+          stats.setNumBytes(getTableVolume(query.systemConf, finalOutputDir));
+          resultTableDesc.setStats(stats);
+        }
+
         query.setResultDesc(resultTableDesc);
       }
     }
@@ -695,10 +726,15 @@
           tableDescTobeCreated.setPartitionMethod(createTableNode.getPartitionMethod());
         }
 
-        stats.setNumBytes(getTableVolume(query.systemConf, finalOutputDir));
-        tableDescTobeCreated.setStats(stats);
-        query.setResultDesc(tableDescTobeCreated);
+        if (query.hasUnionPlan()) {
+          TableStats aggregated = query.aggregateTableStatsOfTerminalBlock();
+          tableDescTobeCreated.setStats(aggregated);
+        } else {
+          stats.setNumBytes(getTableVolume(query.systemConf, finalOutputDir));
+          tableDescTobeCreated.setStats(stats);
+        }
 
+        query.setResultDesc(tableDescTobeCreated);
         catalog.createTable(tableDescTobeCreated);
       }
     }
@@ -733,14 +769,19 @@
           finalTable = new TableDesc(tableName, lastStage.getOutSchema(), meta, finalOutputDir.toUri());
         }
 
-        long volume = getTableVolume(query.systemConf, finalOutputDir);
-        stats.setNumBytes(volume);
-        finalTable.setStats(stats);
+        if (query.hasUnionPlan()) {
+          TableStats aggregated = query.aggregateTableStatsOfTerminalBlock();
+          finalTable.setStats(aggregated);
+        } else {
+          long volume = getTableVolume(query.systemConf, finalOutputDir);
+          stats.setNumBytes(volume);
+          finalTable.setStats(stats);
+        }
 
         if (insertNode.hasTargetTable()) {
           UpdateTableStatsProto.Builder builder = UpdateTableStatsProto.newBuilder();
           builder.setTableName(finalTable.getName());
-          builder.setStats(stats.getProto());
+          builder.setStats(finalTable.getStats().getProto());
 
           catalog.updateTableStats(builder.build());
         }