tajo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hyun...@apache.org
Subject tajo git commit: TAJO-1514: Distinguish UNION and UNION ALL. (contributed by Keuntae Park, committed by hyunsik)
Date Fri, 17 Jul 2015 01:37:46 GMT
Repository: tajo
Updated Branches:
  refs/heads/master 9c99f2ba0 -> 92e3bc1b8


TAJO-1514: Distinguish UNION and UNION ALL. (contributed by Keuntae Park, committed by hyunsik)

Closes #505


Project: http://git-wip-us.apache.org/repos/asf/tajo/repo
Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/92e3bc1b
Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/92e3bc1b
Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/92e3bc1b

Branch: refs/heads/master
Commit: 92e3bc1b899036084bba29abe2ea82587cb76c12
Parents: 9c99f2b
Author: Hyunsik Choi <hyunsik@apache.org>
Authored: Fri Jul 17 10:35:24 2015 +0900
Committer: Hyunsik Choi <hyunsik@apache.org>
Committed: Fri Jul 17 10:35:24 2015 +0900

----------------------------------------------------------------------
 CHANGES                                         |   7 +-
 .../tajo/engine/planner/TestLogicalPlanner.java |   2 +-
 .../tajo/engine/query/TestUnionQuery.java       | 165 +++++++++++++++++++
 .../testDistinctAggregationWithUnion1.sql       |   2 +-
 .../TestJoinQuery/testComplexJoinCondition6.sql |   2 +-
 .../TestJoinQuery/testComplexJoinCondition7.sql |   2 +-
 .../testLeftOuterJoinWithEmptyTable4.sql        |   2 +-
 .../queries/TestTablePartitions/case2.sql       |   2 +-
 .../queries/TestUnionQuery/testUnion10.sql      |   8 +-
 .../queries/TestUnionQuery/testUnion11.sql      |   2 +-
 .../queries/TestUnionQuery/testUnion12.sql      |   3 +-
 .../queries/TestUnionQuery/testUnion13.sql      |   3 +-
 .../queries/TestUnionQuery/testUnion14.sql      |   2 +-
 .../queries/TestUnionQuery/testUnion15.sql      |   2 +-
 .../queries/TestUnionQuery/testUnion16.sql      |   2 +-
 .../queries/TestUnionQuery/testUnion6.sql       |   2 +-
 .../queries/TestUnionQuery/testUnion8.sql       |   2 +-
 .../queries/TestUnionQuery/testUnion9.sql       |   2 +-
 .../queries/TestUnionQuery/testUnionAll1.sql    |  19 +++
 .../queries/TestUnionQuery/testUnionAll10.sql   | 158 ++++++++++++++++++
 .../queries/TestUnionQuery/testUnionAll11.sql   |   8 +
 .../queries/TestUnionQuery/testUnionAll12.sql   |  13 ++
 .../queries/TestUnionQuery/testUnionAll13.sql   |  14 ++
 .../queries/TestUnionQuery/testUnionAll14.sql   |   8 +
 .../queries/TestUnionQuery/testUnionAll15.sql   |  15 ++
 .../queries/TestUnionQuery/testUnionAll16.sql   |  15 ++
 .../queries/TestUnionQuery/testUnionAll2.sql    |  18 ++
 .../queries/TestUnionQuery/testUnionAll3.sql    |  19 +++
 .../queries/TestUnionQuery/testUnionAll4.sql    |  16 ++
 .../queries/TestUnionQuery/testUnionAll5.sql    |  20 +++
 .../queries/TestUnionQuery/testUnionAll6.sql    |  15 ++
 .../queries/TestUnionQuery/testUnionAll7.sql    |  18 ++
 .../queries/TestUnionQuery/testUnionAll8.sql    |  24 +++
 .../queries/TestUnionQuery/testUnionAll9.sql    |  29 ++++
 .../testUnionAllWithDifferentAlias.sql          |  29 ++++
 ...estUnionAllWithDifferentAliasAndFunction.sql |  29 ++++
 .../testUnionAllWithSameAliasNames.sql          |  25 +++
 .../testUnionWithDifferentAlias.sql             |   2 +-
 .../testUnionWithDifferentAliasAndFunction.sql  |   2 +-
 .../results/TestUnionQuery/testUnion1.result    |   3 -
 .../results/TestUnionQuery/testUnion10.result   |  15 --
 .../results/TestUnionQuery/testUnion2.result    |   7 -
 .../results/TestUnionQuery/testUnion6.result    |   2 +-
 .../results/TestUnionQuery/testUnion7.result    |   7 -
 .../results/TestUnionQuery/testUnionAll1.result |  10 ++
 .../TestUnionQuery/testUnionAll10.result        |  22 +++
 .../TestUnionQuery/testUnionAll11.result        |   3 +
 .../TestUnionQuery/testUnionAll12.result        |   6 +
 .../TestUnionQuery/testUnionAll13.result        |   6 +
 .../TestUnionQuery/testUnionAll14.result        |   9 +
 .../TestUnionQuery/testUnionAll15.result        |   5 +
 .../TestUnionQuery/testUnionAll16.result        |   5 +
 .../results/TestUnionQuery/testUnionAll2.result |  12 ++
 .../results/TestUnionQuery/testUnionAll3.result |   4 +
 .../results/TestUnionQuery/testUnionAll4.result |   3 +
 .../results/TestUnionQuery/testUnionAll5.result |   3 +
 .../results/TestUnionQuery/testUnionAll6.result |   3 +
 .../results/TestUnionQuery/testUnionAll7.result |  12 ++
 .../results/TestUnionQuery/testUnionAll8.result |   3 +
 .../results/TestUnionQuery/testUnionAll9.result |   7 +
 .../testUnionAllWithDifferentAlias.result       |   4 +
 ...UnionAllWithDifferentAliasAndFunction.result |   7 +
 .../testUnionAllWithSameAliasNames.result       |  12 ++
 .../testUnionWithSameAliasNames.result          |   7 -
 .../java/org/apache/tajo/plan/LogicalPlan.java  |   9 +
 .../tajo/plan/LogicalPlanPreprocessor.java      |   1 +
 .../org/apache/tajo/plan/LogicalPlanner.java    |  88 +++++++++-
 .../tajo/plan/logical/SetOperationNode.java     |  36 ++++
 .../org/apache/tajo/plan/logical/UnionNode.java |   6 +-
 69 files changed, 950 insertions(+), 75 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index 252b8ed..1dc63b7 100644
--- a/CHANGES
+++ b/CHANGES
@@ -172,8 +172,8 @@ Release 0.11.0 - unreleased
 
   BUG FIXES
 
-    TAJO-1657: Tajo Rest API /database/{database-name]/tables should return table 
-    names only without invalid external table info. 
+    TAJO-1657: Tajo Rest API /database/{database-name]/tables should return 
+    table names only without invalid external table info. 
     (Contributed by  DaeMyung Kang, Committed by jihoon)
 
     TAJO-1644: When inserting empty data into a partitioned table,
@@ -370,6 +370,9 @@ Release 0.11.0 - unreleased
 
   SUB TASKS
 
+    TAJO-1514: Distinguish UNION and UNION ALL. (contributed by Keuntae Park,
+    committed by hyunsik)
+
     TAJO-1658: Filter push down to underlying storages. (hyunsik)
 
     TAJO-1616: Implement TablespaceManager to load Tablespaces. (hyunsik)

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/java/org/apache/tajo/engine/planner/TestLogicalPlanner.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/planner/TestLogicalPlanner.java b/tajo-core/src/test/java/org/apache/tajo/engine/planner/TestLogicalPlanner.java
index 0f37763..0cbecba 100644
--- a/tajo-core/src/test/java/org/apache/tajo/engine/planner/TestLogicalPlanner.java
+++ b/tajo-core/src/test/java/org/apache/tajo/engine/planner/TestLogicalPlanner.java
@@ -1050,7 +1050,7 @@ public class TestLogicalPlanner {
   }
 
   static final String setStatements [] = {
-    "select deptName from employee where deptName like 'data%' union select deptName from score where deptName like 'data%'",
+    "select deptName from employee where deptName like 'data%' union all select deptName from score where deptName like 'data%'",
   };
 
   @Test

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestUnionQuery.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestUnionQuery.java b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestUnionQuery.java
index 03c9c24..21500e9 100644
--- a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestUnionQuery.java
+++ b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestUnionQuery.java
@@ -47,6 +47,150 @@ public class TestUnionQuery extends QueryTestCaseBase {
   /**
    * S (SA U SA) O
    */
+  public final void testUnionAll1() throws Exception {
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  /**
+   * S (S U S) O
+   */
+  public final void testUnionAll2() throws Exception {
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  /**
+   * S O ((S G) U (S G))
+   */
+  public final void testUnionAll3() throws Exception {
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  /**
+   * S G (S G)
+   */
+  public final void testUnionAll4() throws Exception {
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  /**
+   * S G (S F G)
+   */
+  public final void testUnionAll5() throws Exception {
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  /**
+   * S G (SA)
+   */
+  public final void testUnionAll6() throws Exception {
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  /**
+   * S (SA)
+   */
+  public final void testUnionAll7() throws Exception {
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  public final void testUnionAll8() throws Exception {
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  public final void testUnionAll9() throws Exception {
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  public final void testUnionAll10() throws Exception {
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  public final void testUnionAll11() throws Exception {
+    // test filter pushdown
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  public final void testUnionAll12() throws Exception {
+    // test filter pushdown
+    // with stage in union query
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  public final void testUnionAll13() throws Exception {
+    // test filter pushdown
+    // with stage in union query
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  public final void testUnionAll14() throws Exception {
+    // test filter pushdown
+    // with group by stage in union query
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  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);
+  }
+
+  @Test
+  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);
+  }
+
+  @Test
+  /**
+   * S (SA U SA) O
+   */
   public final void testUnion1() throws Exception {
     ResultSet res = executeQuery();
     assertResultSet(res);
@@ -188,6 +332,27 @@ public class TestUnionQuery extends QueryTestCaseBase {
   }
 
   @Test
+  public final void testUnionAllWithSameAliasNames() throws Exception {
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  public final void testUnionAllWithDifferentAlias() throws Exception {
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
+  public final void testUnionAllWithDifferentAliasAndFunction() throws Exception {
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
+
+  @Test
   public final void testUnionWithSameAliasNames() throws Exception {
     ResultSet res = executeQuery();
     assertResultSet(res);

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestGroupByQuery/testDistinctAggregationWithUnion1.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestGroupByQuery/testDistinctAggregationWithUnion1.sql b/tajo-core/src/test/resources/queries/TestGroupByQuery/testDistinctAggregationWithUnion1.sql
index af161d6..a14f982 100644
--- a/tajo-core/src/test/resources/queries/TestGroupByQuery/testDistinctAggregationWithUnion1.sql
+++ b/tajo-core/src/test/resources/queries/TestGroupByQuery/testDistinctAggregationWithUnion1.sql
@@ -10,7 +10,7 @@ from
     from
       lineitem
 
-    union
+    union all
 
     select
       *

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestJoinQuery/testComplexJoinCondition6.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestJoinQuery/testComplexJoinCondition6.sql b/tajo-core/src/test/resources/queries/TestJoinQuery/testComplexJoinCondition6.sql
index 704b821..0ef3143 100644
--- a/tajo-core/src/test/resources/queries/TestJoinQuery/testComplexJoinCondition6.sql
+++ b/tajo-core/src/test/resources/queries/TestJoinQuery/testComplexJoinCondition6.sql
@@ -2,5 +2,5 @@ select
   n1.n_nationkey,
   substr(n1.n_name, 1, 4) name1,
   substr(n2.n_name, 1, 4) name2
-from nation n1 join (select * from nation union select * from nation) n2 on substr(n1.n_name, 1, 4) = substr(n2.n_name, 1, 4)
+from nation n1 join (select * from nation union all select * from nation) n2 on substr(n1.n_name, 1, 4) = substr(n2.n_name, 1, 4)
 order by n1.n_nationkey;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestJoinQuery/testComplexJoinCondition7.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestJoinQuery/testComplexJoinCondition7.sql b/tajo-core/src/test/resources/queries/TestJoinQuery/testComplexJoinCondition7.sql
index d2114cf..4931c83 100644
--- a/tajo-core/src/test/resources/queries/TestJoinQuery/testComplexJoinCondition7.sql
+++ b/tajo-core/src/test/resources/queries/TestJoinQuery/testComplexJoinCondition7.sql
@@ -2,5 +2,5 @@ select
   n1.n_nationkey,
   n1.n_name,
   n2.n_name
-from nation n1 join (select * from nation union select * from nation) n2 on substr(n1.n_name, 1, 4) = substr(n2.n_name, 1, 4)
+from nation n1 join (select * from nation union all select * from nation) n2 on substr(n1.n_name, 1, 4) = substr(n2.n_name, 1, 4)
 order by n1.n_nationkey,n2.n_name;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestJoinQuery/testLeftOuterJoinWithEmptyTable4.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestJoinQuery/testLeftOuterJoinWithEmptyTable4.sql b/tajo-core/src/test/resources/queries/TestJoinQuery/testLeftOuterJoinWithEmptyTable4.sql
index 5f91a18..f4e9da6 100644
--- a/tajo-core/src/test/resources/queries/TestJoinQuery/testLeftOuterJoinWithEmptyTable4.sql
+++ b/tajo-core/src/test/resources/queries/TestJoinQuery/testLeftOuterJoinWithEmptyTable4.sql
@@ -6,7 +6,7 @@
     max(orders.o_orderdate)
   from
     customer left outer join orders on c_custkey = o_orderkey
-  union
+  union all
   select
     max(c_custkey),
     sum(empty_orders.o_orderkey),

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestTablePartitions/case2.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestTablePartitions/case2.sql b/tajo-core/src/test/resources/queries/TestTablePartitions/case2.sql
index c510065..ee94778 100644
--- a/tajo-core/src/test/resources/queries/TestTablePartitions/case2.sql
+++ b/tajo-core/src/test/resources/queries/TestTablePartitions/case2.sql
@@ -11,7 +11,7 @@ from (
   where
     (key = 45.0 or key = 38.0) and null_col is null
 
-  union
+  union all
 
   select
     col1,

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion10.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion10.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion10.sql
index d644a44..2037564 100644
--- a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion10.sql
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion10.sql
@@ -16,7 +16,7 @@ SELECT * FROM (
         n_regionkey = 0
 
 
-      UNION ALL
+      UNION
 
       SELECT
         n_nationkey,
@@ -49,7 +49,7 @@ SELECT * FROM (
       WHERE
         n_regionkey = 0
 
-      UNION ALL
+      UNION
 
       SELECT
         n_nationkey,
@@ -93,7 +93,7 @@ SELECT * FROM (
         n_regionkey = 0
 
 
-      UNION ALL
+      UNION
 
       SELECT
         n_nationkey,
@@ -126,7 +126,7 @@ SELECT * FROM (
       WHERE
         n_regionkey = 0
 
-      UNION ALL
+      UNION
 
       SELECT
         n_nationkey,

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion11.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion11.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion11.sql
index ec1a430..b3b0feb 100644
--- a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion11.sql
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion11.sql
@@ -1,7 +1,7 @@
 select col1, col2, col3
 from (
     select L_RETURNFLAG as col1, L_EXTENDEDPRICE as col2, concat(L_RECEIPTDATE, L_LINESTATUS) as col3 from lineitem
-    union all
+    union
     select P_TYPE as col1, P_RETAILPRICE col2, P_NAME col3 from part
 ) a
 where col3 like '1993%' and col2 > 46796

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion12.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion12.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion12.sql
index 6b6a9ad..64ad0a7 100644
--- a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion12.sql
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion12.sql
@@ -7,7 +7,8 @@ from (
             L_RETURNFLAG as col1, L_EXTENDEDPRICE as col2, concat(L_RECEIPTDATE, L_LINESTATUS) as col3
         from
             lineitem) b
-    union all
+    union
     select P_TYPE as col1, P_RETAILPRICE * 100 col2, concat('1993', P_NAME) col3 from part
 ) a
 where col3 like '1993%' and col2 > 46796
+order by col2;

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion13.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion13.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion13.sql
index 70b0891..763eef9 100644
--- a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion13.sql
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion13.sql
@@ -8,7 +8,8 @@ from (
         from
             lineitem
         where col2 > 46796) b
-    union all
+    union
     select P_TYPE as col1, P_RETAILPRICE * 100 col2, concat('1993', P_NAME) col3 from part
 ) a
 where col3 like '1993%'
+order by col2

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion14.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion14.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion14.sql
index f47510e..79c8365 100644
--- a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion14.sql
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion14.sql
@@ -1,7 +1,7 @@
 select col1, cnt
 from (
     select L_RETURNFLAG as col1, count(*) as cnt from lineitem group by col1
-    union all
+    union
     select cast(n_regionkey as TEXT) as col1, count(*) as cnt from nation group by col1
 ) a
 where a.cnt > 1

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion15.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion15.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion15.sql
index 2e382d0..19a7eb7 100644
--- a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion15.sql
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion15.sql
@@ -5,7 +5,7 @@ from (
             join orders on l_orderkey = o_orderkey and o_custkey > 0
             group by l_returnflag) b
     where col1 = 'N'
-    union all
+    union
     select cast(n_regionkey as TEXT) as col1, count(*) as cnt from nation
     where n_regionkey > 2
     group by col1

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion16.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion16.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion16.sql
index 59e9c1f..dd9dca4 100644
--- a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion16.sql
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion16.sql
@@ -5,7 +5,7 @@ from (
             join orders on l_orderkey = o_orderkey and o_custkey > 0
             group by l_returnflag) b
     where col1 = 'N'
-    union all
+    union
     select cast(n_regionkey as TEXT) as col1, count(*) as cnt from nation
     where n_regionkey > 2
     group by col1

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion6.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion6.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion6.sql
index 8052632..300cb46 100644
--- a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion6.sql
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion6.sql
@@ -6,7 +6,7 @@ FROM (
   FROM
     lineitem
 
-  UNION ALL
+  UNION
 
   SELECT
     *

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion8.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion8.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion8.sql
index bf5fbee..aa8333d 100644
--- a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion8.sql
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion8.sql
@@ -10,7 +10,7 @@ FROM (
   WHERE
     l_returnflag = 'R'
 
-  UNION ALL
+  UNION
 
   SELECT
     *

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion9.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion9.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion9.sql
index 49ddca8..7b0811a 100644
--- a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion9.sql
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnion9.sql
@@ -11,7 +11,7 @@ FROM (
     n_regionkey = 0
 
 
-  UNION ALL
+  UNION
 
   SELECT
     *

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll1.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll1.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll1.sql
new file mode 100644
index 0000000..363512c
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll1.sql
@@ -0,0 +1,19 @@
+select
+  num
+from (
+
+select
+  o_custkey as num
+from
+  orders
+
+union all
+
+select
+  c_custkey as num
+from
+  customer
+) table1
+
+order by
+  num;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll10.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll10.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll10.sql
new file mode 100644
index 0000000..97c8364
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll10.sql
@@ -0,0 +1,158 @@
+SELECT * FROM (
+  SELECT
+    *
+  FROM (
+    SELECT
+      n_nationkey,
+      n_name
+
+    FROM (
+      SELECT
+        n_nationkey,
+        n_name
+      FROM
+        nation
+      WHERE
+        n_regionkey = 0
+
+
+      UNION ALL
+
+      SELECT
+        n_nationkey,
+        n_name
+      FROM
+        nation
+      WHERE
+        n_regionkey = 0
+
+    ) T1
+    GROUP BY
+      n_nationkey,
+      n_name
+    ORDER BY
+      n_nationkey desc,
+      n_name desc
+
+   UNION all
+
+   SELECT
+    n_nationkey,
+    n_name
+
+   FROM (
+      SELECT
+        n_nationkey,
+        n_name
+      FROM
+        nation
+      WHERE
+        n_regionkey = 0
+
+      UNION ALL
+
+      SELECT
+        n_nationkey,
+        n_name
+      FROM
+        nation
+      WHERE
+        n_regionkey = 0
+   ) T2
+
+   GROUP BY
+     n_nationkey,
+     n_name
+
+   ORDER BY
+     n_nationkey desc,
+     n_name desc
+
+  ) TABLE1
+
+  ORDER BY
+    n_nationkey,
+    n_name
+
+  UNION all
+
+  SELECT
+    *
+  FROM (
+    SELECT
+      n_nationkey,
+      n_name
+
+    FROM (
+      SELECT
+        n_nationkey,
+        n_name
+      FROM
+        nation
+      WHERE
+        n_regionkey = 0
+
+
+      UNION ALL
+
+      SELECT
+        n_nationkey,
+        n_name
+      FROM
+        nation
+      WHERE
+        n_regionkey = 0
+
+    ) T3
+    GROUP BY
+      n_nationkey,
+      n_name
+    ORDER BY
+      n_nationkey desc,
+      n_name desc
+
+   UNION all
+
+   SELECT
+    n_nationkey,
+    n_name
+
+   FROM (
+      SELECT
+        n_nationkey,
+        n_name
+      FROM
+        nation
+      WHERE
+        n_regionkey = 0
+
+      UNION ALL
+
+      SELECT
+        n_nationkey,
+        n_name
+      FROM
+        nation
+      WHERE
+        n_regionkey = 0
+   ) T4
+
+   GROUP BY
+     n_nationkey,
+     n_name
+
+   ORDER BY
+     n_nationkey desc,
+     n_name desc
+
+  ) TABLE2
+
+  ORDER BY
+    n_nationkey,
+    n_name
+
+) TABLE3
+
+ORDER BY
+  n_nationkey,
+  n_name;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll11.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll11.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll11.sql
new file mode 100644
index 0000000..ec1a430
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll11.sql
@@ -0,0 +1,8 @@
+select col1, col2, col3
+from (
+    select L_RETURNFLAG as col1, L_EXTENDEDPRICE as col2, concat(L_RECEIPTDATE, L_LINESTATUS) as col3 from lineitem
+    union all
+    select P_TYPE as col1, P_RETAILPRICE col2, P_NAME col3 from part
+) a
+where col3 like '1993%' and col2 > 46796
+

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll12.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll12.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll12.sql
new file mode 100644
index 0000000..6b6a9ad
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll12.sql
@@ -0,0 +1,13 @@
+select col1, col2, col3
+from (
+    select
+        col1, col2, col3
+    from
+        (select
+            L_RETURNFLAG as col1, L_EXTENDEDPRICE as col2, concat(L_RECEIPTDATE, L_LINESTATUS) as col3
+        from
+            lineitem) b
+    union all
+    select P_TYPE as col1, P_RETAILPRICE * 100 col2, concat('1993', P_NAME) col3 from part
+) a
+where col3 like '1993%' and col2 > 46796

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll13.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll13.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll13.sql
new file mode 100644
index 0000000..70b0891
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll13.sql
@@ -0,0 +1,14 @@
+select col1, col2, col3
+from (
+    select
+        col1, col2, col3
+    from
+        (select
+            L_RETURNFLAG as col1, L_EXTENDEDPRICE as col2, concat(L_RECEIPTDATE, L_LINESTATUS) as col3
+        from
+            lineitem
+        where col2 > 46796) b
+    union all
+    select P_TYPE as col1, P_RETAILPRICE * 100 col2, concat('1993', P_NAME) col3 from part
+) a
+where col3 like '1993%'

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll14.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll14.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll14.sql
new file mode 100644
index 0000000..f47510e
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll14.sql
@@ -0,0 +1,8 @@
+select col1, cnt
+from (
+    select L_RETURNFLAG as col1, count(*) as cnt from lineitem group by col1
+    union all
+    select cast(n_regionkey as TEXT) as col1, count(*) as cnt from nation group by col1
+) a
+where a.cnt > 1
+order by a.col1
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll15.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll15.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll15.sql
new file mode 100644
index 0000000..2e382d0
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll15.sql
@@ -0,0 +1,15 @@
+select col1, sum(cnt)
+from (
+    select col1, cnt
+    from (  select l_returnflag col1, count(*) cnt from lineitem
+            join orders on l_orderkey = o_orderkey and o_custkey > 0
+            group by l_returnflag) b
+    where col1 = 'N'
+    union all
+    select cast(n_regionkey as TEXT) as col1, count(*) as cnt from nation
+    where n_regionkey > 2
+    group by col1
+) a
+where round(cast(a.cnt as FLOAT4)) > 1.0
+group by a.col1
+order by a.col1

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll16.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll16.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll16.sql
new file mode 100644
index 0000000..59e9c1f
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll16.sql
@@ -0,0 +1,15 @@
+select col1, sum(cnt)
+from (
+    select col1, cnt
+    from (  select l_returnflag col1, count(distinct l_orderkey) cnt from lineitem
+            join orders on l_orderkey = o_orderkey and o_custkey > 0
+            group by l_returnflag) b
+    where col1 = 'N'
+    union all
+    select cast(n_regionkey as TEXT) as col1, count(*) as cnt from nation
+    where n_regionkey > 2
+    group by col1
+) a
+where round(cast(a.cnt as FLOAT4)) > 1.0
+group by a.col1
+order by a.col1

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll2.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll2.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll2.sql
new file mode 100644
index 0000000..e5fa5d2
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll2.sql
@@ -0,0 +1,18 @@
+select
+  l_orderkey
+from (
+  select
+    l_orderkey
+  from
+    lineitem l1
+
+  union all
+
+  select
+    l_orderkey
+  from
+    lineitem l2
+) table1
+
+order by
+  l_orderkey;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll3.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll3.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll3.sql
new file mode 100644
index 0000000..68266ec
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll3.sql
@@ -0,0 +1,19 @@
+select
+  total
+from (
+
+select
+  count(*) as total
+from
+  orders
+
+union all
+
+select
+  count(*) as total
+from
+  customer
+) table1
+
+order by
+  total;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll4.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll4.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll4.sql
new file mode 100644
index 0000000..31dce06
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll4.sql
@@ -0,0 +1,16 @@
+select
+  count(*)
+from (
+
+  select
+    count(*) as total
+  from
+    orders
+
+  union all
+
+  select
+    count(*) as total
+  from
+    customer
+) table1;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll5.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll5.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll5.sql
new file mode 100644
index 0000000..8a55799
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll5.sql
@@ -0,0 +1,20 @@
+select
+  count(*)
+from (
+
+  select
+    count(*) as total
+  from
+    orders
+  WHERE
+    o_orderkey > 0
+
+  union all
+
+  select
+    count(*) as total
+  from
+    customer
+  WHERE
+    c_custkey > 0
+) table1;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll6.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll6.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll6.sql
new file mode 100644
index 0000000..8052632
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll6.sql
@@ -0,0 +1,15 @@
+SELECT
+  count(*)
+FROM (
+  SELECT
+    *
+  FROM
+    lineitem
+
+  UNION ALL
+
+  SELECT
+    *
+  FROM
+    lineitem
+) T
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll7.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll7.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll7.sql
new file mode 100644
index 0000000..3592a6a
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll7.sql
@@ -0,0 +1,18 @@
+SELECT
+  orderkey
+FROM (
+  SELECT
+    l_orderkey as orderkey
+  FROM
+    lineitem
+
+  UNION all
+
+  SELECT
+    l_orderkey as orderkey
+  FROM
+    lineitem
+) T
+
+order by
+  orderkey;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll8.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll8.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll8.sql
new file mode 100644
index 0000000..bf5fbee
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll8.sql
@@ -0,0 +1,24 @@
+SELECT
+  l_returnflag,
+  l_linestatus
+
+FROM (
+  SELECT
+    *
+  FROM
+    lineitem
+  WHERE
+    l_returnflag = 'R'
+
+  UNION ALL
+
+  SELECT
+    *
+  FROM
+    lineitem
+  WHERE
+    l_returnflag = 'R'
+) T
+GROUP BY
+  l_returnflag,
+  l_linestatus;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll9.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll9.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll9.sql
new file mode 100644
index 0000000..49ddca8
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAll9.sql
@@ -0,0 +1,29 @@
+SELECT
+  n_nationkey,
+  n_name
+
+FROM (
+  SELECT
+    *
+  FROM
+    nation
+  WHERE
+    n_regionkey = 0
+
+
+  UNION ALL
+
+  SELECT
+    *
+  FROM
+    nation
+  WHERE
+    n_regionkey = 0
+
+) T
+GROUP BY
+  n_nationkey,
+	n_name
+ORDER BY
+  n_nationkey desc,
+  n_name desc;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAllWithDifferentAlias.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAllWithDifferentAlias.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAllWithDifferentAlias.sql
new file mode 100644
index 0000000..f41ee32
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAllWithDifferentAlias.sql
@@ -0,0 +1,29 @@
+SELECT
+  col1,
+  col2
+
+FROM (
+  SELECT
+    l_returnflag col1, l_linestatus col2, l_orderkey col3
+  FROM
+    lineitem
+  WHERE
+    l_returnflag = 'N'
+
+  UNION ALL
+
+  SELECT
+    l_returnflag col2, l_linestatus col5, l_orderkey col6
+  FROM
+    lineitem
+  WHERE
+    l_returnflag = 'R'
+) T
+
+GROUP BY
+  col1,
+	col2
+
+ORDER BY
+  col1,
+	col2
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAllWithDifferentAliasAndFunction.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAllWithDifferentAliasAndFunction.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAllWithDifferentAliasAndFunction.sql
new file mode 100644
index 0000000..598f399
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAllWithDifferentAliasAndFunction.sql
@@ -0,0 +1,29 @@
+SELECT
+  col1,
+  col2
+
+FROM (
+  SELECT
+    l_returnflag col1, concat(l_linestatus, l_shipdate) col2, l_orderkey col3
+  FROM
+    lineitem
+  WHERE
+    l_returnflag = 'N'
+
+  UNION ALL
+
+  SELECT
+    concat(l_returnflag, l_shipdate) col3, l_linestatus col4, l_orderkey col5
+  FROM
+    lineitem
+  WHERE
+    l_returnflag = 'R'
+) T
+
+GROUP BY
+  col1,
+	col2
+
+ORDER BY
+  col1,
+	col2
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAllWithSameAliasNames.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAllWithSameAliasNames.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAllWithSameAliasNames.sql
new file mode 100644
index 0000000..1516697
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionAllWithSameAliasNames.sql
@@ -0,0 +1,25 @@
+select
+  *
+from (
+  select
+    l_orderkey
+  from (
+    select
+      l_orderkey
+    from
+      lineitem
+  ) l1
+
+  union all
+
+  select
+    l_orderkey
+  from (
+    select
+      l_orderkey
+    from
+      lineitem
+  ) l1
+) t1
+order by
+  l_orderkey;

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionWithDifferentAlias.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionWithDifferentAlias.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionWithDifferentAlias.sql
index f41ee32..9fb0146 100644
--- a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionWithDifferentAlias.sql
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionWithDifferentAlias.sql
@@ -10,7 +10,7 @@ FROM (
   WHERE
     l_returnflag = 'N'
 
-  UNION ALL
+  UNION
 
   SELECT
     l_returnflag col2, l_linestatus col5, l_orderkey col6

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionWithDifferentAliasAndFunction.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionWithDifferentAliasAndFunction.sql b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionWithDifferentAliasAndFunction.sql
index 598f399..35048cc 100644
--- a/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionWithDifferentAliasAndFunction.sql
+++ b/tajo-core/src/test/resources/queries/TestUnionQuery/testUnionWithDifferentAliasAndFunction.sql
@@ -10,7 +10,7 @@ FROM (
   WHERE
     l_returnflag = 'N'
 
-  UNION ALL
+  UNION
 
   SELECT
     concat(l_returnflag, l_shipdate) col3, l_linestatus col4, l_orderkey col5

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion1.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnion1.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion1.result
index f384e9f..a5282c5 100644
--- a/tajo-core/src/test/resources/results/TestUnionQuery/testUnion1.result
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion1.result
@@ -2,9 +2,6 @@ num
 -------------------------------
 1
 2
-2
-3
 3
 4
-4
 5
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion10.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnion10.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion10.result
index effe340..279446e 100644
--- a/tajo-core/src/test/resources/results/TestUnionQuery/testUnion10.result
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion10.result
@@ -1,22 +1,7 @@
 n_nationkey,n_name
 -------------------------------
 0,ALGERIA
-0,ALGERIA
-0,ALGERIA
-0,ALGERIA
-5,ETHIOPIA
-5,ETHIOPIA
-5,ETHIOPIA
 5,ETHIOPIA
 14,KENYA
-14,KENYA
-14,KENYA
-14,KENYA
-15,MOROCCO
-15,MOROCCO
-15,MOROCCO
 15,MOROCCO
-16,MOZAMBIQUE
-16,MOZAMBIQUE
-16,MOZAMBIQUE
 16,MOZAMBIQUE
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion2.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnion2.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion2.result
index d269baa..df55931 100644
--- a/tajo-core/src/test/resources/results/TestUnionQuery/testUnion2.result
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion2.result
@@ -1,12 +1,5 @@
 l_orderkey
 -------------------------------
 1
-1
-1
-1
-2
 2
-3
-3
-3
 3
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion6.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnion6.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion6.result
index 7ccc8c2..19336a7 100644
--- a/tajo-core/src/test/resources/results/TestUnionQuery/testUnion6.result
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion6.result
@@ -1,3 +1,3 @@
 ?count
 -------------------------------
-10
\ No newline at end of file
+5
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion7.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnion7.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion7.result
index 6a3fbd5..fe633a4 100644
--- a/tajo-core/src/test/resources/results/TestUnionQuery/testUnion7.result
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnion7.result
@@ -1,12 +1,5 @@
 orderkey
 -------------------------------
 1
-1
-1
-1
-2
 2
-3
-3
-3
 3
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll1.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll1.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll1.result
new file mode 100644
index 0000000..f384e9f
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll1.result
@@ -0,0 +1,10 @@
+num
+-------------------------------
+1
+2
+2
+3
+3
+4
+4
+5
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll10.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll10.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll10.result
new file mode 100644
index 0000000..effe340
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll10.result
@@ -0,0 +1,22 @@
+n_nationkey,n_name
+-------------------------------
+0,ALGERIA
+0,ALGERIA
+0,ALGERIA
+0,ALGERIA
+5,ETHIOPIA
+5,ETHIOPIA
+5,ETHIOPIA
+5,ETHIOPIA
+14,KENYA
+14,KENYA
+14,KENYA
+14,KENYA
+15,MOROCCO
+15,MOROCCO
+15,MOROCCO
+15,MOROCCO
+16,MOZAMBIQUE
+16,MOZAMBIQUE
+16,MOZAMBIQUE
+16,MOZAMBIQUE
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll11.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll11.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll11.result
new file mode 100644
index 0000000..6e8d2cd
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll11.result
@@ -0,0 +1,3 @@
+col1,col2,col3
+-------------------------------
+R,46796.47,1993-11-24F
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll12.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll12.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll12.result
new file mode 100644
index 0000000..c130afa
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll12.result
@@ -0,0 +1,6 @@
+col1,col2,col3
+-------------------------------
+R,46796.47,1993-11-24F
+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
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll13.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll13.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll13.result
new file mode 100644
index 0000000..c130afa
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll13.result
@@ -0,0 +1,6 @@
+col1,col2,col3
+-------------------------------
+R,46796.47,1993-11-24F
+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
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll14.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll14.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll14.result
new file mode 100644
index 0000000..3838ab4
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll14.result
@@ -0,0 +1,9 @@
+col1,cnt
+-------------------------------
+0,5
+1,5
+2,5
+3,5
+4,5
+N,3
+R,2
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll15.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll15.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll15.result
new file mode 100644
index 0000000..4e4d9e9
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll15.result
@@ -0,0 +1,5 @@
+col1,?sum
+-------------------------------
+3,5
+4,5
+N,3
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll16.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll16.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll16.result
new file mode 100644
index 0000000..38be978
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll16.result
@@ -0,0 +1,5 @@
+col1,?sum
+-------------------------------
+3,5
+4,5
+N,2
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll2.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll2.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll2.result
new file mode 100644
index 0000000..d269baa
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll2.result
@@ -0,0 +1,12 @@
+l_orderkey
+-------------------------------
+1
+1
+1
+1
+2
+2
+3
+3
+3
+3
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll3.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll3.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll3.result
new file mode 100644
index 0000000..2562bd4
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll3.result
@@ -0,0 +1,4 @@
+total
+-------------------------------
+3
+5
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll4.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll4.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll4.result
new file mode 100644
index 0000000..22227ba
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll4.result
@@ -0,0 +1,3 @@
+?count
+-------------------------------
+2
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll5.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll5.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll5.result
new file mode 100644
index 0000000..22227ba
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll5.result
@@ -0,0 +1,3 @@
+?count
+-------------------------------
+2
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll6.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll6.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll6.result
new file mode 100644
index 0000000..7ccc8c2
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll6.result
@@ -0,0 +1,3 @@
+?count
+-------------------------------
+10
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll7.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll7.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll7.result
new file mode 100644
index 0000000..6a3fbd5
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll7.result
@@ -0,0 +1,12 @@
+orderkey
+-------------------------------
+1
+1
+1
+1
+2
+2
+3
+3
+3
+3
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll8.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll8.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll8.result
new file mode 100644
index 0000000..ba35aa1
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll8.result
@@ -0,0 +1,3 @@
+l_returnflag,l_linestatus
+-------------------------------
+R,F
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll9.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll9.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll9.result
new file mode 100644
index 0000000..482dd52
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAll9.result
@@ -0,0 +1,7 @@
+n_nationkey,n_name
+-------------------------------
+16,MOZAMBIQUE
+15,MOROCCO
+14,KENYA
+5,ETHIOPIA
+0,ALGERIA
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAlias.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAlias.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAlias.result
new file mode 100644
index 0000000..7415ec4
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAlias.result
@@ -0,0 +1,4 @@
+col1,col2
+-------------------------------
+N,O
+R,F
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAliasAndFunction.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAliasAndFunction.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAliasAndFunction.result
new file mode 100644
index 0000000..7e6f6cd
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAllWithDifferentAliasAndFunction.result
@@ -0,0 +1,7 @@
+col1,col2
+-------------------------------
+N,O1996-03-13
+N,O1996-04-12
+N,O1997-01-28
+R1993-11-09,F
+R1994-02-02,F
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAllWithSameAliasNames.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAllWithSameAliasNames.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAllWithSameAliasNames.result
new file mode 100644
index 0000000..d269baa
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionAllWithSameAliasNames.result
@@ -0,0 +1,12 @@
+l_orderkey
+-------------------------------
+1
+1
+1
+1
+2
+2
+3
+3
+3
+3
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionWithSameAliasNames.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionWithSameAliasNames.result b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionWithSameAliasNames.result
index d269baa..df55931 100644
--- a/tajo-core/src/test/resources/results/TestUnionQuery/testUnionWithSameAliasNames.result
+++ b/tajo-core/src/test/resources/results/TestUnionQuery/testUnionWithSameAliasNames.result
@@ -1,12 +1,5 @@
 l_orderkey
 -------------------------------
 1
-1
-1
-1
-2
 2
-3
-3
-3
 3
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlan.java
----------------------------------------------------------------------
diff --git a/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlan.java b/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlan.java
index 22f4781..a6a3dbe 100644
--- a/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlan.java
+++ b/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlan.java
@@ -53,10 +53,12 @@ public class LogicalPlan {
   /** it indicates the root block */
   public static final String ROOT_BLOCK = VIRTUAL_TABLE_PREFIX + "ROOT";
   public static final String NONAME_BLOCK_PREFIX = VIRTUAL_TABLE_PREFIX + "QB_";
+  public static final String NONAME_SUBQUERY_PREFIX = "?SubQuery_";
   private static final int NO_SEQUENCE_PID = -1;
   private int nextPid = 0;
   private Integer noNameBlockId = 0;
   private Integer noNameColumnId = 0;
+  private Integer noNameSubqueryId = 0;
 
   /** a map from between a block name to a block plan */
   private Map<String, QueryBlock> queryBlocks = new LinkedHashMap<String, QueryBlock>();
@@ -155,6 +157,13 @@ public class LogicalPlan {
   }
 
   /**
+   * It generates a unique table subquery name
+   */
+  public String generateUniqueSubQueryName() {
+    return NONAME_SUBQUERY_PREFIX + noNameSubqueryId++;
+  }
+
+  /**
    * It generates an unique column name from Expr. It is usually used for an expression or predicate without
    * a specified name (i.e., alias).
    */

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlanPreprocessor.java
----------------------------------------------------------------------
diff --git a/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlanPreprocessor.java b/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlanPreprocessor.java
index dced4d3..5fa5a21 100644
--- a/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlanPreprocessor.java
+++ b/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlanPreprocessor.java
@@ -337,6 +337,7 @@ public class LogicalPlanPreprocessor extends BaseAlgebraVisitor<LogicalPlanner.P
     unionNode.setRightChild(rightChild);
     unionNode.setInSchema(leftChild.getOutSchema());
     unionNode.setOutSchema(leftChild.getOutSchema());
+    unionNode.setDistinct(expr.isDistinct());
 
     return unionNode;
   }

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlanner.java
----------------------------------------------------------------------
diff --git a/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlanner.java b/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlanner.java
index c51d068..9d2161e 100644
--- a/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlanner.java
+++ b/tajo-plan/src/main/java/org/apache/tajo/plan/LogicalPlanner.java
@@ -1372,15 +1372,21 @@ public class LogicalPlanner extends BaseAlgebraVisitor<LogicalPlanner.PlanContex
 
   public TableSubQueryNode visitTableSubQuery(PlanContext context, Stack<Expr> stack, TablePrimarySubQuery expr)
       throws PlanningException {
-    QueryBlock block = context.queryBlock;
-
+    QueryBlock currentBlock = context.queryBlock;
     QueryBlock childBlock = context.plan.getBlock(context.plan.getBlockNameByExpr(expr.getSubQuery()));
+    context.plan.connectBlocks(childBlock, currentBlock, BlockType.TableSubQuery);
+
     PlanContext newContext = new PlanContext(context, childBlock);
     LogicalNode child = visit(newContext, new Stack<Expr>(), expr.getSubQuery());
-    TableSubQueryNode subQueryNode = context.queryBlock.getNodeFromExpr(expr);
-    context.plan.connectBlocks(childBlock, context.queryBlock, BlockType.TableSubQuery);
+    TableSubQueryNode subQueryNode = currentBlock.getNodeFromExpr(expr);
+
     subQueryNode.setSubQuery(child);
+    setTargetOfTableSubQuery(context, currentBlock, subQueryNode);
 
+    return subQueryNode;
+  }
+
+  private void setTargetOfTableSubQuery (PlanContext context, QueryBlock block, TableSubQueryNode subQueryNode) throws PlanningException {
     // Add additional expressions required in upper nodes.
     Set<String> newlyEvaluatedExprs = TUtil.newHashSet();
     for (NamedExpr rawTarget : block.namedExprsMgr.getAllNamedExprs()) {
@@ -1403,8 +1409,6 @@ public class LogicalPlanner extends BaseAlgebraVisitor<LogicalPlanner.PlanContex
     }
 
     subQueryNode.setTargets(targets.toArray(new Target[targets.size()]));
-
-    return subQueryNode;
   }
 
     /*===============================================================================================
@@ -1414,7 +1418,77 @@ public class LogicalPlanner extends BaseAlgebraVisitor<LogicalPlanner.PlanContex
   @Override
   public LogicalNode visitUnion(PlanContext context, Stack<Expr> stack, SetOperation setOperation)
       throws PlanningException {
-    return buildSetPlan(context, stack, setOperation);
+    UnionNode unionNode = (UnionNode)buildSetPlan(context, stack, setOperation);
+    LogicalNode resultingNode = unionNode;
+
+    /**
+     *  if the given node is Union (Distinct), it adds group by node
+     *    change
+     *     from
+     *             union
+     *
+     *       to
+     *           projection
+     *               |
+     *            group by
+     *               |
+     *         table subquery
+     *               |
+     *             union
+     */
+    if (unionNode.isDistinct()) {
+      return insertProjectionGroupbyBeforeSetOperation(context, unionNode);
+    }
+
+    return resultingNode;
+  }
+
+  private ProjectionNode insertProjectionGroupbyBeforeSetOperation(PlanContext context, SetOperationNode setOperationNode) throws PlanningException {
+    QueryBlock currentBlock = context.queryBlock;
+
+    // make table subquery node which has set operation as its subquery
+    TableSubQueryNode setOpTableSubQueryNode = context.plan.createNode(TableSubQueryNode.class);
+    setOpTableSubQueryNode.init(CatalogUtil.buildFQName(context.queryContext.get(SessionVars.CURRENT_DATABASE), context.plan.generateUniqueSubQueryName()), setOperationNode);
+    setTargetOfTableSubQuery(context, currentBlock, setOpTableSubQueryNode);
+    currentBlock.registerNode(setOpTableSubQueryNode);
+    currentBlock.addRelation(setOpTableSubQueryNode);
+
+    Schema setOpSchema = setOpTableSubQueryNode.getOutSchema();
+    Target[] setOpTarget = setOpTableSubQueryNode.getTargets();
+
+    // make group by node whose grouping keys are all columns of set operation
+    GroupbyNode setOpGroupbyNode = context.plan.createNode(GroupbyNode.class);
+    setOpGroupbyNode.setInSchema(setOpSchema);
+    setOpGroupbyNode.setGroupingColumns(setOpSchema.toArray());
+    setOpGroupbyNode.setTargets(setOpTarget);
+    setOpGroupbyNode.setChild(setOpTableSubQueryNode);
+    currentBlock.registerNode(setOpGroupbyNode);
+
+    // make projection node which projects all the union columns
+    ProjectionNode setOpProjectionNode = context.plan.createNode(ProjectionNode.class);
+    setOpProjectionNode.setInSchema(setOpSchema);
+    setOpProjectionNode.setTargets(setOpTarget);
+    setOpProjectionNode.setChild(setOpGroupbyNode);
+    currentBlock.registerNode(setOpProjectionNode);
+
+    // changing query block chain: at below, ( ) indicates query block
+    // (... + set operation ) - (...) ==> (... + projection + group by + table subquery) - (set operation) - (...)
+    QueryBlock setOpBlock = context.plan.newQueryBlock();
+    setOpBlock.registerNode(setOperationNode);
+    setOpBlock.setRoot(setOperationNode);
+
+    QueryBlock leftBlock = context.plan.getBlock(setOperationNode.getLeftChild());
+    QueryBlock rightBlock = context.plan.getBlock(setOperationNode.getRightChild());
+
+    context.plan.disconnectBlocks(leftBlock, context.queryBlock);
+    context.plan.disconnectBlocks(rightBlock, context.queryBlock);
+
+    context.plan.connectBlocks(setOpBlock, context.queryBlock, BlockType.TableSubQuery);
+    context.plan.connectBlocks(leftBlock, setOpBlock, BlockType.TableSubQuery);
+    context.plan.connectBlocks(rightBlock, setOpBlock, BlockType.TableSubQuery);
+
+    // projection node (not original set operation node) will be a new child of parent node
+    return setOpProjectionNode;
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-plan/src/main/java/org/apache/tajo/plan/logical/SetOperationNode.java
----------------------------------------------------------------------
diff --git a/tajo-plan/src/main/java/org/apache/tajo/plan/logical/SetOperationNode.java b/tajo-plan/src/main/java/org/apache/tajo/plan/logical/SetOperationNode.java
new file mode 100644
index 0000000..63d5296
--- /dev/null
+++ b/tajo-plan/src/main/java/org/apache/tajo/plan/logical/SetOperationNode.java
@@ -0,0 +1,36 @@
+/**
+ * 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.tajo.plan.logical;
+
+public abstract class SetOperationNode extends BinaryNode {
+
+  private boolean isDistinct = false;
+
+  public SetOperationNode(int pid, NodeType setType) {
+    super(pid, setType);
+  }
+
+  public boolean isDistinct() {
+    return isDistinct;
+  }
+
+  public void setDistinct(boolean isDistinct) {
+    this.isDistinct = isDistinct;
+  }
+}

http://git-wip-us.apache.org/repos/asf/tajo/blob/92e3bc1b/tajo-plan/src/main/java/org/apache/tajo/plan/logical/UnionNode.java
----------------------------------------------------------------------
diff --git a/tajo-plan/src/main/java/org/apache/tajo/plan/logical/UnionNode.java b/tajo-plan/src/main/java/org/apache/tajo/plan/logical/UnionNode.java
index 2839ab5..2f7479f 100644
--- a/tajo-plan/src/main/java/org/apache/tajo/plan/logical/UnionNode.java
+++ b/tajo-plan/src/main/java/org/apache/tajo/plan/logical/UnionNode.java
@@ -16,15 +16,11 @@
  * limitations under the License.
  */
 
-/**
- * 
- */
 package org.apache.tajo.plan.logical;
 
 import org.apache.tajo.plan.PlanString;
 
-public class UnionNode extends BinaryNode {
-
+public class UnionNode extends SetOperationNode {
   public UnionNode(int pid) {
     super(pid, NodeType.UNION);
   }


Mime
View raw message