You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tajo.apache.org by hy...@apache.org on 2013/07/29 06:03:30 UTC

[01/13] TAJO-87: Integration of tajo algebra module and SQL parser

Updated Branches:
  refs/heads/master ed25aa85a -> 00c3ee2bd


http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-frontend/tajo-frontend-sql/src/test/java/org/apache/tajo/frontend/sql/TestSQLParser.java
----------------------------------------------------------------------
diff --git a/tajo-frontend/tajo-frontend-sql/src/test/java/org/apache/tajo/frontend/sql/TestSQLParser.java b/tajo-frontend/tajo-frontend-sql/src/test/java/org/apache/tajo/frontend/sql/TestSQLParser.java
deleted file mode 100644
index da99795..0000000
--- a/tajo-frontend/tajo-frontend-sql/src/test/java/org/apache/tajo/frontend/sql/TestSQLParser.java
+++ /dev/null
@@ -1,914 +0,0 @@
-/**
- * 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.frontend.sql;
-
-import org.antlr.runtime.ANTLRStringStream;
-import org.antlr.runtime.CommonTokenStream;
-import org.antlr.runtime.RecognitionException;
-import org.antlr.runtime.tree.CommonTree;
-import org.antlr.runtime.tree.Tree;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-
-/**
- * Unit Test Class for SQLParser.g generated by ANTLR
- */
-public class TestSQLParser {
-  private static Log LOG = LogFactory.getLog(TestSQLParser.class);
-
-  static final String[] selectQuery = {
-      "select id, name, age, gender from people", // 0
-      "select title, ISBN from Books", // 1
-      "select studentId from students", // 2
-      "session clear", // 3
-      "select id, name, age, gender from people as p, students as s", // 4
-      "select name, addr, sum(score) from students group by name, addr", // 5
-      "select name, addr, age from people where age > 30", // 6
-      "select name, addr, age from people where age = 30", // 7
-      "select name as n, sum(score, 3+4, 3>4) as total, 3+4 as id from people where age = 30", // 8
-      "select ipv4:src_ip from test", // 9
-      "select distinct id, name, age, gender from people", // 10
-      "select all id, name, age, gender from people", // 11
-  };
-
-  static final String[] insertQueries = {
-      "insert into people values (1, 'hyunsik', 32)",
-      "insert into people (id, name, age) values (1, 'hyunsik', 32)" };
-
-  public static Tree parseQuery(String query) {
-    ANTLRStringStream input = new ANTLRStringStream(query);
-    SQLLexer lexer = new SQLLexer(input);
-    CommonTokenStream tokens = new CommonTokenStream(lexer);
-    SQLParser parser = new SQLParser(tokens);
-
-    Tree tree = null;
-    try {
-      tree = ((Tree) parser.statement().getTree());
-    } catch (RecognitionException e) {
-      LOG.error(e.getMessage(), e);
-    }
-
-    return tree;
-  }
-
-  @Test
-  public void testSelectClause() {
-    Tree tree = parseQuery(selectQuery[0]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-  }
-  
-  private final String groupbyQuery[] = {
-      "select col0, col1, col2, col3, sum(col4) as total, avg(col5) from base group by col0, cube (col1, col2), rollup(col3) having total > 100.0"
-  };
-  
-  @Test
-  public void testCubeByClause() {
-    Tree ast = parseQuery(groupbyQuery[0]);
-    assertEquals(SQLParser.SELECT, ast.getType());
-    int idx = 0;    
-    assertEquals(SQLParser.FROM, ast.getChild(idx++).getType());
-    assertEquals(SQLParser.SEL_LIST, ast.getChild(idx++).getType());
-    assertEquals(SQLParser.GROUP_BY, ast.getChild(idx).getType());
-    
-    Tree groupby = ast.getChild(idx);
-    int grpIdx = 0;
-    assertEquals(SQLParser.FIELD_NAME, groupby.getChild(grpIdx++).getType());
-    assertEquals(SQLParser.CUBE, groupby.getChild(grpIdx).getType());
-    
-    int cubeIdx = 0;
-    Tree cube = groupby.getChild(grpIdx);
-    assertEquals(SQLParser.FIELD_NAME, cube.getChild(cubeIdx++).getType());
-    assertEquals(SQLParser.FIELD_NAME, cube.getChild(cubeIdx++).getType());
-    grpIdx++;
-    assertEquals(SQLParser.ROLLUP, groupby.getChild(grpIdx).getType());
-    
-    int rollupIdx = 0;
-    Tree rollup = groupby.getChild(grpIdx);
-    assertEquals(SQLParser.FIELD_NAME, rollup.getChild(rollupIdx++).getType());
-    
-    idx++;
-    assertEquals(SQLParser.HAVING, ast.getChild(idx).getType());
-  }
-
-  @Test
-  public void testColumnFamily() {
-    Tree ast = parseQuery(selectQuery[9]);
-    assertEquals(SQLParser.SELECT, ast.getType());
-    assertEquals(SQLParser.SEL_LIST, ast.getChild(1).getType());
-    assertEquals(SQLParser.COLUMN, ast.getChild(1).getChild(0).getType());
-    assertEquals(SQLParser.FIELD_NAME, ast.getChild(1).getChild(0).getChild(0)
-        .getType());
-    FieldName fieldName = new FieldName(ast.getChild(1).getChild(0).getChild(0));
-    assertEquals("ipv4", fieldName.getFamilyName());
-    assertEquals("src_ip", fieldName.getSimpleName());
-    assertEquals("ipv4:src_ip", fieldName.getName());
-  }
-
-  @Test
-  public void testSetQualifier() {
-    Tree ast = parseQuery(selectQuery[10]);
-    assertEquals(SQLParser.SELECT, ast.getType());
-    assertEquals(SQLParser.SET_QUALIFIER, ast.getChild(1).getType());
-    assertEquals(SQLParser.DISTINCT, ast.getChild(1).getChild(0).getType());
-    assertSetListofSetQualifierTest(ast);
-
-    ast = parseQuery(selectQuery[11]);
-    assertEquals(SQLParser.SELECT, ast.getType());
-    assertEquals(SQLParser.SET_QUALIFIER, ast.getChild(1).getType());
-    assertEquals(SQLParser.ALL, ast.getChild(1).getChild(0).getType());
-    assertSetListofSetQualifierTest(ast);
-  }
-
-  private void assertSetListofSetQualifierTest(Tree ast) {
-    assertEquals(SQLParser.SEL_LIST, ast.getChild(2).getType());
-    assertEquals(SQLParser.COLUMN, ast.getChild(2).getChild(0).getType());
-    assertEquals(SQLParser.FIELD_NAME, ast.getChild(2).getChild(0).getChild(0)
-        .getType());
-  }
-
-  @Test
-  public void testSessionClear() {
-    Tree tree = parseQuery(selectQuery[3]);
-    assertEquals(tree.getType(), SQLParser.SESSION_CLEAR);
-  }
-
-  @Test
-  public void testWhereClause() {
-    Tree tree = parseQuery(selectQuery[6]);
-
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    tree = tree.getChild(2).getChild(0);
-
-    assertEquals(SQLParser.Greater_Than_Operator, tree.getType());
-    assertEquals(SQLParser.FIELD_NAME, tree.getChild(0).getType());
-    FieldName fieldName = new FieldName(tree.getChild(0));
-    assertEquals("age", fieldName.getName());
-    assertEquals(SQLParser.Unsigned_Integer, tree.getChild(1).getType());
-    assertEquals("30", tree.getChild(1).getText());
-  }
-
-  @Test
-  public void testInsertClause() {
-    Tree tree = parseQuery(insertQueries[0]);
-    assertEquals(tree.getType(), SQLParser.INSERT);
-  }
-  
-  static String [] joinQueries = {
-    "select name, addr from people natural join student natural join professor", // 0
-    "select name, addr from people inner join student on people.name = student.name", // 1
-    "select name, addr from people inner join student using (id, name)", // 2
-    "select name, addr from people join student using (id, name)", // 3
-    "select name, addr from people cross join student", // 4
-    "select name, addr from people left outer join student on people.name = student.name", // 5
-    "select name, addr from people right outer join student on people.name = student.name", // 6
-    "select * from table1 " +
-        "cross join table2 " +
-        "join table3 on table1.id = table3.id " +
-        "inner join table4 on table1.id = table4.id " +
-        "left outer join table5 on table1.id = table5.id " +
-        "right outer join table6 on table1.id = table6.id " +
-        "full outer join table7 on table1.id = table7.id " +
-        "natural join table8 " +
-        "natural inner join table9 " +
-        "natural left outer join table10 " +
-        "natural right outer join table11 " +
-        "natural full outer join table12 ",
-         // 7 - all possible join clauses*/
-    "select s_acctbal, s_name, n_name, p_partkey, p_mfgr, s_address, s_phone, s_comment, ps_supplycost " + // 8
-      "from region join nation on n_regionkey = r_regionkey and r_name = 'EUROPE' " +
-      "join supplier on s_nationekey = n_nationkey " +
-      "join partsupp on s_suppkey = ps_ps_suppkey " +
-      "join part on p_partkey = ps_partkey and p_type like '%BRASS' and p_size = 15"
-  };
-  
-  @Test
-  /**
-   *                 from
-   *        ---------------------
-   *       /     |               \
-   *      /      \                \
-   *   TABLE     join              join
-   *   /       /     \            /     \
-   *  people  NATURAL TABLE     NATURAL TABLE
-   *                    /                 /
-   *                  student          professor
-   */
-  public void testNaturalJoinClause() {
-    Tree tree = parseQuery(joinQueries[0]);
-    System.out.println(tree.toStringTree());
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.FROM, tree.getChild(0).getType());
-    CommonTree fromAST = (CommonTree) tree.getChild(0);
-    assertEquals(SQLParser.TABLE, fromAST.getChild(0).getType());
-    assertEquals("people", fromAST.getChild(0).getChild(0).getText());
-    assertEquals(SQLParser.JOIN, fromAST.getChild(1).getType());
-    assertEquals(SQLParser.NATURAL, fromAST.getChild(1).getChild(0).getType());
-    assertEquals("student", fromAST.getChild(1).getChild(1).getChild(0).getText());
-    assertEquals(SQLParser.JOIN, fromAST.getChild(2).getType());
-    assertEquals(SQLParser.NATURAL, fromAST.getChild(2).getChild(0).getType());
-    assertEquals("professor", fromAST.getChild(2).getChild(1).getChild(0).getText());
-  }
-  
-  @Test
-  public void testInnerJoinClause() {
-    Tree tree = parseQuery(joinQueries[1]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.FROM, tree.getChild(0).getType());
-    CommonTree fromAST = (CommonTree) tree.getChild(0);
-    assert2WayJoinAST(fromAST, SQLParser.INNER);
-    assertEquals(SQLParser.ON, fromAST.getChild(1).getChild(2).getType());
-    
-    tree = parseQuery(joinQueries[2]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    fromAST = (CommonTree) tree.getChild(0);
-    assert2WayJoinAST(fromAST, SQLParser.INNER);
-    assertEquals(SQLParser.USING, fromAST.getChild(1).getChild(2).getType());
-    assertEquals(2, fromAST.getChild(1).getChild(2).getChildCount());
-
-    /**
-     *       from
-     *       /     \
-     *     TABLE          join
-     *      /       /             \
-     *    people   TABLE        using
-     *              /       /              \
-     *            student  FIELD_NAME  FIELD_NAME
-     *                      /              \
-     *                     id              name
-     */
-    tree = parseQuery(joinQueries[3]);
-    fromAST = (CommonTree) tree.getChild(0);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.TABLE, fromAST.getChild(0).getType());
-    assertEquals("people", fromAST.getChild(0).getChild(0).getText());
-    assertEquals(SQLParser.JOIN, fromAST.getChild(1).getType());
-    assertEquals(SQLParser.TABLE, fromAST.getChild(1).getChild(0).getType());
-    assertEquals("student", fromAST.getChild(1).getChild(0).getChild(0).getText());
-    assertEquals(SQLParser.USING, fromAST.getChild(1).getChild(1).getType());
-    assertEquals(2, fromAST.getChild(1).getChild(1).getChildCount());
-  }
-
-  private void assert2WayJoinAST(Tree fromAST, int joinType) {
-    assertEquals(SQLParser.TABLE, fromAST.getChild(0).getType());
-    assertEquals("people", fromAST.getChild(0).getChild(0).getText());
-    assertEquals(SQLParser.JOIN, fromAST.getChild(1).getType());
-    assertEquals(joinType, fromAST.getChild(1).getChild(0).getType());
-    assertEquals(SQLParser.TABLE, fromAST.getChild(1).getChild(1).getType());
-    assertEquals("student", fromAST.getChild(1).getChild(1).getChild(0).getText());
-  }
-  
-  @Test
-  public void testCrossJoinClause() {
-    Tree tree = parseQuery(joinQueries[4]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.FROM, tree.getChild(0).getType());
-    CommonTree fromAST = (CommonTree) tree.getChild(0);
-    assert2WayJoinAST(fromAST, SQLParser.CROSS);
-  }
-  
-  @Test
-  public void testLeftOuterJoinClause() {
-    Tree tree = parseQuery(joinQueries[5]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.FROM, tree.getChild(0).getType());
-    CommonTree fromAST = (CommonTree) tree.getChild(0);
-    assert2WayJoinAST(fromAST, SQLParser.OUTER);
-    assertEquals(SQLParser.LEFT, fromAST.getChild(1).getChild(0).getChild(0).getType());
-    assertEquals(SQLParser.ON, fromAST.getChild(1).getChild(2).getType());
-    assertEquals(1, fromAST.getChild(1).getChild(2).getChildCount());
-  }
-  
-  @Test
-  public void testRightOuterJoinClause() {
-    Tree tree = parseQuery(joinQueries[6]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.FROM, tree.getChild(0).getType());
-    CommonTree fromAST = (CommonTree) tree.getChild(0);
-    assert2WayJoinAST(fromAST, SQLParser.OUTER);
-    assertEquals(SQLParser.RIGHT, fromAST.getChild(1).getChild(0).getChild(0).getType());
-    assertEquals(SQLParser.ON, fromAST.getChild(1).getChild(2).getType());
-    assertEquals(1, fromAST.getChild(1).getChild(2).getChildCount());
-  }
-
-  @Test
-  public void testAllJoinTypes() {
-    Tree tree = parseQuery(joinQueries[7]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.FROM, tree.getChild(0).getType());
-    CommonTree fromAST = (CommonTree) tree.getChild(0);
-
-    assertEquals(12,fromAST.getChildCount());
-    assertEquals(SQLParser.TABLE, fromAST.getChild(0).getType());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(1).getType());
-    Tree join = fromAST.getChild(1);
-    assertEquals(SQLParser.CROSS, join.getChild(0).getType());
-    assertEquals("table2", join.getChild(1).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(2).getType());
-    join = fromAST.getChild(2);
-    assertEquals(SQLParser.TABLE, join.getChild(0).getType());
-    assertEquals("table3", join.getChild(0).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(3).getType());
-    join = fromAST.getChild(3);
-    assertEquals(SQLParser.INNER, join.getChild(0).getType());
-    assertEquals("table4", join.getChild(1).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(4).getType());
-    join = fromAST.getChild(4);
-    assertEquals(SQLParser.OUTER, join.getChild(0).getType());
-    assertEquals(SQLParser.LEFT, join.getChild(0).getChild(0).getType());
-    assertEquals("table5", join.getChild(1).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(5).getType());
-    join = fromAST.getChild(5);
-    assertEquals(SQLParser.OUTER, join.getChild(0).getType());
-    assertEquals(SQLParser.RIGHT, join.getChild(0).getChild(0).getType());
-    assertEquals("table6", join.getChild(1).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(6).getType());
-    join = fromAST.getChild(6);
-    assertEquals(SQLParser.OUTER, join.getChild(0).getType());
-    assertEquals(SQLParser.FULL, join.getChild(0).getChild(0).getType());
-    assertEquals("table7", join.getChild(1).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(7).getType());
-    join = fromAST.getChild(7);
-    assertEquals(SQLParser.NATURAL, join.getChild(0).getType());
-    assertEquals("table8", join.getChild(1).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(8).getType());
-    join = fromAST.getChild(8);
-    assertEquals(SQLParser.NATURAL, join.getChild(0).getType());
-    assertEquals(SQLParser.INNER, join.getChild(1).getType());
-    assertEquals("table9", join.getChild(2).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(9).getType());
-    join = fromAST.getChild(9);
-    assertEquals(SQLParser.NATURAL, join.getChild(0).getType());
-    assertEquals(SQLParser.OUTER, join.getChild(1).getType());
-    assertEquals(SQLParser.LEFT, join.getChild(1).getChild(0).getType());
-    assertEquals("table10", join.getChild(2).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(10).getType());
-    join = fromAST.getChild(10);
-    assertEquals(SQLParser.NATURAL, join.getChild(0).getType());
-    assertEquals(SQLParser.OUTER, join.getChild(1).getType());
-    assertEquals(SQLParser.RIGHT, join.getChild(1).getChild(0).getType());
-    assertEquals("table11", join.getChild(2).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(11).getType());
-    join = fromAST.getChild(11);
-    assertEquals(SQLParser.NATURAL, join.getChild(0).getType());
-    assertEquals(SQLParser.OUTER, join.getChild(1).getType());
-    assertEquals(SQLParser.FULL, join.getChild(1).getChild(0).getType());
-    assertEquals("table12", join.getChild(2).getChild(0).getText());
-  }
-  
-  private final static String setQueries[] = {
-    "select a,b,c from table1 union select a,b,c from table1", // 0
-    "select a,b,c from table1 union all select a,b,c from table1", // 1
-    "select a,b,c from table1 union distinct select a,b,c from table1", // 2
-    "select a,b,c from table1 except select a,b,c from table1", // 3
-    "select a,b,c from table1 except all select a,b,c from table1", // 4
-    "select a,b,c from table1 except distinct select a,b,c from table1", // 5
-    "select a,b,c from table1 union select a,b,c from table1 union select a,b,c from table2", // 6
-    "select a,b,c from table1 union select a,b,c from table1 intersect select a,b,c from table2" // 7
-  };
-  
-  @Test
-  public void testUnionClause() {
-    Tree tree = parseQuery(setQueries[0]);
-    assertEquals(SQLParser.UNION, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(1).getType());
-    
-    tree = parseQuery(setQueries[1]);
-    assertEquals(SQLParser.UNION, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.ALL, tree.getChild(1).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(2).getType());
-    
-    tree = parseQuery(setQueries[2]);
-    assertEquals(SQLParser.UNION, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.DISTINCT, tree.getChild(1).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(2).getType());
-    
-    tree = parseQuery(setQueries[3]);
-    assertEquals(SQLParser.EXCEPT, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(1).getType());
-    
-    tree = parseQuery(setQueries[4]);
-    assertEquals(SQLParser.EXCEPT, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.ALL, tree.getChild(1).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(2).getType());
-    
-    tree = parseQuery(setQueries[5]);
-    assertEquals(SQLParser.EXCEPT, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.DISTINCT, tree.getChild(1).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(2).getType());
-    
-    tree = parseQuery(setQueries[6]);
-    assertEquals(SQLParser.UNION, tree.getType());
-    assertEquals(SQLParser.UNION, tree.getChild(0).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(1).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getChild(0).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getChild(1).getType());
-    
-    tree = parseQuery(setQueries[7]);
-    assertEquals(SQLParser.UNION, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.INTERSECT, tree.getChild(1).getType());
-  }
-
-  static String[] ddlStatements = {
-    "drop table abc",
-    "create table name as select * from test",
-    "create external table table1 (name varchar, age int, earn long, score float) using csv location '/tmp/data'"
-  };
-
-  @Test
-  public void testCreateTableAsSelect() {
-    Tree ast = parseQuery(ddlStatements[1]);
-    assertEquals(ast.getType(), SQLParser.CREATE_TABLE);
-    assertEquals(ast.getChild(0).getType(), SQLParser.Regular_Identifier);
-    assertEquals(SQLParser.AS, ast.getChild(1).getType());
-  }
-  
-  @Test
-  public void testCreateTableDef() {
-    Tree ast = parseQuery(ddlStatements[2]);
-    System.out.println(ast.toStringTree());
-    assertEquals(SQLParser.CREATE_TABLE, ast.getType());
-    assertEquals(SQLParser.Regular_Identifier, ast.getChild(0).getType());
-    assertEquals(SQLParser.EXTERNAL, ast.getChild(1).getType());
-    assertEquals(SQLParser.TABLE_DEF, ast.getChild(2).getType());
-    assertEquals(SQLParser.USING, ast.getChild(3).getType());
-    assertEquals(SQLParser.Regular_Identifier, ast.getChild(3).getChild(0).getType());
-    assertEquals(SQLParser.LOCATION, ast.getChild(4).getType());
-    assertEquals(SQLParser.Character_String_Literal, ast.getChild(4).getChild(0).getType());
-  }
-
-  @Test
-  public void testDropTable() {
-    Tree ast = parseQuery(ddlStatements[0]);
-    assertEquals(ast.getType(), SQLParser.DROP_TABLE);
-    assertEquals(ast.getChild(0).getText(), "abc");
-  }
-
-  static String[] expressions = {
-      "1 + 2", // 0
-      "3 - 4", // 1
-      "5 * 6", // 2
-      "7 / 8", // 3
-      "10 % 2", // 4
-      "1 * 2 > 3 / 4", // 5
-      "1 * 2 < 3 / 4", // 6
-      "1 * 2 = 3 / 4", // 7
-      "1 * 2 != 3 / 4", // 8
-      "1 * 2 <> 3 / 4", // 9
-      "(3, 4)", // 10
-      "('male', 'female')", // 11
-      "gender in ('male', 'female')", // 12
-      "gender not in ('male', 'female')", // 13
-      "score > 90 and age < 20", // 14
-      "score > 90 and age < 20 and name != 'hyunsik'", // 15
-      "score > 90 or age < 20", // 16
-      "score > 90 or age < 20 and name != 'hyunsik'", // 17
-      //"(((a+3 > 1) or 1=1) and (3 <> (abc + 4) and type in (3,4))", // 18
-      "(((a + 3) > 1) or 1=1) and (3 <> (3 + 4) and type in (3, 4))", // 18
-      "3", // 19
-      "1.2", // 20
-      "sum(age)", // 21
-      "now()", // 22
-      "not (90 > 100.0)", // 23
-      "type like '%top'", // 24
-      "type not like 'top%'", // 25
-      "col = 'value'", // 26
-      "col is null", // 27
-      "col is not null", // 28
-      "col = null", // 29
-      "col != null", // 30
-      "(3 > 1) = (2 > 1)" // 31
-  };
-
-  public static SQLParser parseExpr(String expr) {
-    ANTLRStringStream input = new ANTLRStringStream(expr);
-    SQLLexer lexer = new SQLLexer(input);
-    CommonTokenStream tokens = new CommonTokenStream(lexer);
-    SQLParser parser = new SQLParser(tokens);
-    return parser;
-  }
-
-  static String [] arithmeticExprs = {
-      "1 + 2", // 0
-      "3 - 4", // 1
-      "5 * 6", // 2
-      "7 / 8", // 3
-      "10 % 2", // 4
-      "(1 + 2) * (6 / 3)" // 5
-  };
-
-  @Test
-  public void testArithEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(arithmeticExprs[0]);
-    CommonTree node = (CommonTree) p.numeric_value_expression().getTree();
-
-    assertEquals(node.getText(), "+");
-    assertEquals(node.getChild(0).getText(), "1");
-    assertEquals(node.getChild(1).getText(), "2");
-
-    p = parseExpr(arithmeticExprs[1]);
-    node = (CommonTree) p.numeric_value_expression().getTree();
-    assertEquals(node.getText(), "-");
-    assertEquals(node.getChild(0).getText(), "3");
-    assertEquals(node.getChild(1).getText(), "4");
-
-    p = parseExpr(arithmeticExprs[2]);
-    node = (CommonTree) p.numeric_value_expression().getTree();
-    assertEquals(node.getText(), "*");
-    assertEquals(node.getChild(0).getText(), "5");
-    assertEquals(node.getChild(1).getText(), "6");
-
-    p = parseExpr(arithmeticExprs[3]);
-    node = (CommonTree) p.numeric_value_expression().getTree();
-    assertEquals(node.getText(), "/");
-    assertEquals(node.getChild(0).getText(), "7");
-    assertEquals(node.getChild(1).getText(), "8");
-
-    p = parseExpr(arithmeticExprs[4]);
-    node = (CommonTree) p.numeric_value_expression().getTree();
-    assertEquals(node.getText(), "%");
-    assertEquals(node.getChild(0).getText(), "10");
-    assertEquals(node.getChild(1).getText(), "2");
-  }
-
-  static String [] comparisonExpr = {
-      "1 * 2 > 3 / 4", // 5
-      "1 * 2 < 3 / 4", // 6
-      "1 * 2 = 3 / 4", // 7
-      "1 * 2 != 3 / 4", // 8
-      "1 * 2 <> 3 / 4", // 9
-  };
-
-  @Test
-  public void testCompEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(comparisonExpr[0]);
-    CommonTree node = (CommonTree) p.comparison_predicate().getTree();
-    assertEquals(node.getText(), ">");
-    assertEquals("*", node.getChild(0).getText());
-    assertEquals("/", node.getChild(1).getText());
-
-    p = parseExpr(comparisonExpr[1]);
-    node = (CommonTree) p.comparison_predicate().getTree();
-    assertEquals(node.getText(), "<");
-    assertEquals(node.getChild(0).getText(), "*");
-    assertEquals(node.getChild(1).getText(), "/");
-
-    p = parseExpr(comparisonExpr[2]);
-    node = (CommonTree) p.comparison_predicate().getTree();
-    assertEquals(node.getText(), "=");
-    assertEquals(node.getChild(0).getText(), "*");
-    assertEquals(node.getChild(1).getText(), "/");
-
-    p = parseExpr(comparisonExpr[3]);
-    node = (CommonTree) p.comparison_predicate().getTree();
-    assertEquals(node.getText(), "!=");
-    assertEquals(node.getChild(0).getText(), "*");
-    assertEquals(node.getChild(1).getText(), "/");
-
-    p = parseExpr(comparisonExpr[4]);
-    node = (CommonTree) p.comparison_predicate().getTree();
-    assertEquals(node.getText(), "<>");
-    assertEquals(node.getChild(0).getText(), "*");
-    assertEquals(node.getChild(1).getText(), "/");
-
-    p = parseExpr(expressions[31]);
-    node = (CommonTree) p.comparison_predicate().getTree();
-    System.out.println(node.toStringTree());
-    assertEquals(node.getText(), "=");
-    assertEquals(node.getChild(0).getText(), ">");
-    assertEquals(node.getChild(1).getText(), ">");
-  }
-
-  @Test
-  public void testAtomArray() throws RecognitionException {
-    SQLParser p = parseExpr(expressions[10]);
-    CommonTree node = (CommonTree) p.array().getTree();
-    assertEquals(node.getChild(0).getText(), "3");
-    assertEquals(node.getChild(1).getText(), "4");
-
-    p = parseExpr(expressions[11]);
-    node = (CommonTree) p.array().getTree();
-    assertEquals(node.getChild(0).getText(), "male");
-    assertEquals(node.getChild(1).getText(), "female");
-  }
-
-  @Test
-  public void testInEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(expressions[12]);
-    CommonTree node = (CommonTree) p.in_predicate().getTree();
-    assertEquals(node.getType(), SQLParser.IN);
-    assertEquals(node.getChild(1).getText(), "male");
-    assertEquals(node.getChild(2).getText(), "female");
-
-    p = parseExpr(expressions[13]);
-    node = (CommonTree) p.in_predicate().getTree();
-    assertEquals(node.getType(), SQLParser.IN);
-    assertEquals(node.getChild(0).getType(), SQLParser.FIELD_NAME);
-    FieldName fieldName = new FieldName(node.getChild(0));
-    assertEquals(fieldName.getName(), "gender");
-    assertEquals(node.getChild(1).getText(), "male");
-    assertEquals(node.getChild(2).getText(), "female");
-    assertEquals(node.getChild(3).getType(), SQLParser.NOT);
-  }
-
-  @Test
-  public void testAndEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(expressions[14]);
-    CommonTree node = (CommonTree) p.boolean_value_expression().getTree();
-    assertEquals(node.getText(), "and");
-    assertEquals(node.getChild(0).getText(), ">");
-    assertEquals(node.getChild(1).getText(), "<");
-
-    p = parseExpr(expressions[15]);
-    node = (CommonTree) p.boolean_value_expression().getTree();
-    assertEquals(node.getText(), "and");
-    assertEquals(node.getChild(0).getText(), "and");
-    assertEquals(node.getChild(1).getText(), "!=");
-  }
-
-  @Test
-  public void testOrEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(expressions[16]);
-    CommonTree node = (CommonTree) p.boolean_value_expression().getTree();
-    assertEquals(node.getText(), "or");
-    assertEquals(node.getChild(0).getText(), ">");
-    assertEquals(node.getChild(1).getText(), "<");
-
-    p = parseExpr(expressions[17]);
-    node = (CommonTree) p.boolean_value_expression().getTree();
-    assertEquals(node.getText(), "or");
-    assertEquals(node.getChild(0).getText(), ">");
-    assertEquals(node.getChild(1).getText(), "and");
-  }
-
-  @Test
-  public void testComplexEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(expressions[18]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(node.getText(), "and");
-    assertEquals(node.getChild(0).getText(), "or");
-    assertEquals(node.getChild(1).getText(), "and");
-  }
-  
-  @Test
-  public void testNotEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(expressions[23]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(node.getType(), SQLParser.NOT);
-    assertEquals(node.getChild(0).getType(), SQLParser.Greater_Than_Operator);
-    CommonTree gth = (CommonTree) node.getChild(0);
-    assertEquals(gth.getChild(0).getType(), SQLParser.Unsigned_Integer);
-    assertEquals(gth.getChild(1).getType(), SQLParser.Unsigned_Float);
-  }
-
-  @Test
-  public void testFuncCallEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(expressions[21]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(node.getType(), SQLParser.FUNCTION);
-    assertEquals(node.getText(), "sum");
-    assertEquals(node.getChild(0).getType(), SQLParser.FIELD_NAME);
-    FieldName fieldName = new FieldName(node.getChild(0));
-    assertEquals(fieldName.getName(), "age");
-
-    p = parseExpr(expressions[22]);
-    node = (CommonTree) p.search_condition().getTree();
-    assertEquals(node.getType(), SQLParser.FUNCTION);
-    assertEquals(node.getText(), "now");
-    assertNull(node.getChild(1));
-  }
-  
-  @Test
-  public void testLikeEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(expressions[24]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(SQLParser.LIKE, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(0).getType());
-    FieldName fieldName = new FieldName(node.getChild(0));
-    assertEquals(fieldName.getName(), "type");
-    assertEquals(SQLParser.Character_String_Literal, node.getChild(1).getType());
-    
-    p = parseExpr(expressions[25]);
-    node = (CommonTree) p.search_condition().getTree();    
-    assertEquals(SQLParser.NOT, node.getChild(0).getType());
-    assertEquals(SQLParser.LIKE, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(1).getType());
-    fieldName = new FieldName(node.getChild(1));
-    assertEquals(fieldName.getName(), "type");
-    assertEquals(SQLParser.Character_String_Literal, node.getChild(2).getType());
-  }
-
-  @Test
-  /**
-   * TODO - needs more tests
-   */
-  public void testConstEval() throws RecognitionException {
-    SQLParser p = parseExpr(expressions[26]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(SQLParser.Equals_Operator, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(0).getType());
-    assertEquals(SQLParser.Character_String_Literal, node.getChild(1).getType());
-  }
-
-  @Test
-  public void testIsNull() throws RecognitionException {
-    SQLParser p = parseExpr(expressions[27]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(SQLParser.IS, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(0).getType());
-    assertEquals(SQLParser.NULL, node.getChild(1).getType());
-  }
-
-  @Test
-  public void testIsNotNull() throws RecognitionException {
-    SQLParser p = parseExpr(expressions[28]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    System.out.println(node.toStringTree());
-    assertEquals(SQLParser.IS, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(0).getType());
-    assertEquals(SQLParser.NULL, node.getChild(1).getType());
-    assertEquals(SQLParser.NOT, node.getChild(2).getType());
-  }
-
-  @Test
-  public void testEqualNull() throws RecognitionException {
-    SQLParser p = parseExpr(expressions[29]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(SQLParser.Equals_Operator, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(0).getType());
-    assertEquals(SQLParser.NULL, node.getChild(1).getType());
-  }
-
-  @Test
-  public void testNotEqualNull() throws RecognitionException {
-    SQLParser p = parseExpr(expressions[30]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(SQLParser.Not_Equals_Operator, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(0).getType());
-    assertEquals(SQLParser.NULL, node.getChild(1).getType());
-  }
-
-  public static String [] caseStatements = {
-      "select " +
-          "case " +
-          "when p_type like 'PROMO%' then l_extendedprice * (1 - l_discount) " +
-          "when p_type = 'MOCC' then l_extendedprice - 100 " +
-          "else 0 end " +
-          "as cond " +
-          "from lineitem"
-  };
-
-  @Test
-  public void testCaseWhen() throws RecognitionException {
-    SQLParser p = parseExpr(caseStatements[0]);
-    CommonTree node = (CommonTree) p.statement().getTree();
-
-    assertEquals(SQLParser.SEL_LIST, node.getChild(1).getType());
-    CommonTree selList = (CommonTree) node.getChild(1);
-    assertEquals(1, selList.getChildCount());
-    assertEquals(SQLParser.COLUMN, selList.getChild(0).getType());
-
-    CommonTree column = (CommonTree) selList.getChild(0);
-    assertEquals(SQLParser.CASE, column.getChild(0).getType());
-    CommonTree caseStatement = (CommonTree) column.getChild(0);
-
-    assertEquals(3, caseStatement.getChildCount());
-    assertEquals(SQLParser.WHEN, caseStatement.getChild(0).getType());
-    assertEquals(SQLParser.WHEN, caseStatement.getChild(1).getType());
-    assertEquals(SQLParser.ELSE, caseStatement.getChild(2).getType());
-
-    CommonTree cond1 = (CommonTree) caseStatement.getChild(0);
-    CommonTree cond2 = (CommonTree) caseStatement.getChild(1);
-    CommonTree elseStmt = (CommonTree) caseStatement.getChild(2);
-
-    assertEquals(SQLParser.LIKE, cond1.getChild(0).getType());
-    assertEquals(SQLParser.Equals_Operator, cond2.getChild(0).getType());
-    assertEquals(SQLParser.Unsigned_Integer, elseStmt.getChild(0).getType());
-  }
-
-  public class FieldName {
-    private final String tableName;
-    private final String familyName;
-    private final String fieldName;
-
-    public FieldName(Tree tree) {
-      if(tree.getChild(1) == null) {
-        this.tableName = null;
-      } else {
-        this.tableName = tree.getChild(1).getText();
-      }
-
-      String name = tree.getChild(0).getText();
-      if(name.contains(":")) {
-        String [] splits = name.split(":");
-        this.familyName = splits[0];
-        this.fieldName = splits[1];
-      } else {
-        this.familyName = null;
-        this.fieldName = name;
-      }
-    }
-
-    public boolean hasTableName() {
-      return this.tableName != null;
-    }
-
-    public String getTableName(){
-      return this.tableName;
-    }
-
-    public boolean hasFamilyName() {
-      return this.familyName != null;
-    }
-
-    public String getFamilyName() {
-      return this.familyName;
-    }
-
-    public String getFullName() {
-      StringBuilder sb = new StringBuilder();
-      if(tableName != null) {
-        sb.append(tableName)
-            .append(".");
-      }
-      if(familyName != null) {
-        sb.append(familyName).
-            append(":");
-      }
-      sb.append(fieldName);
-
-      return sb.toString();
-    }
-
-    public String getName() {
-      if(familyName == null) {
-        return fieldName;
-      } else
-        return familyName+":"+fieldName;
-    }
-
-    public String getSimpleName() {
-      return this.fieldName;
-    }
-
-    public String toString() {
-      return getName();
-    }
-  }
-
-  public static String [] tableSubQueries = {
-      "select c1, c2, c3 from (select c1, c2, c3 from employee) as test",
-      "select c1, c2, c3 from table1 where c3 < (select c4 from table2)"
-  };
-
-  @Test
-  public void testTableSubQuery1() throws RecognitionException {
-    SQLParser p = parseExpr(tableSubQueries[0]);
-    CommonTree node = (CommonTree) p.statement().getTree();
-    System.out.println(node.toStringTree());
-  }
-
-  @Test
-  public void testTableSubQuery2() throws RecognitionException {
-    SQLParser p = parseExpr(tableSubQueries[1]);
-    CommonTree node = (CommonTree) p.statement().getTree();
-    System.out.println(node.toStringTree());
-  }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-project/pom.xml
----------------------------------------------------------------------
diff --git a/tajo-project/pom.xml b/tajo-project/pom.xml
index c2dc0eb..137fcc1 100644
--- a/tajo-project/pom.xml
+++ b/tajo-project/pom.xml
@@ -325,8 +325,8 @@
         </plugin>
         <plugin>
           <groupId>org.antlr</groupId>
-          <artifactId>antlr3-maven-plugin</artifactId>
-          <version>3.4</version>
+          <artifactId>antlr4-maven-plugin</artifactId>
+          <version>4.0</version>
         </plugin>
         <!--This plugin's configuration is used to store Eclipse m2e settings only. 
           It has no influence on the Maven build itself. -->
@@ -671,15 +671,15 @@
       </dependency>
       <dependency>
         <groupId>org.antlr</groupId>
-        <artifactId>antlr</artifactId>
-        <version>3.4</version>
+        <artifactId>antlr4</artifactId>
+        <version>4.0</version>
         <type>jar</type>
         <scope>compile</scope>
       </dependency>
       <dependency>
         <groupId>org.antlr</groupId>
-        <artifactId>antlr-runtime</artifactId>
-        <version>3.4</version>
+        <artifactId>antlr4-runtime</artifactId>
+        <version>4.0</version>
         <type>jar</type>
         <scope>compile</scope>
       </dependency>


[02/13] TAJO-87: Integration of tajo algebra module and SQL parser

Posted by hy...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/SQLAnalyzer.java
----------------------------------------------------------------------
diff --git a/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/SQLAnalyzer.java b/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/SQLAnalyzer.java
deleted file mode 100644
index 9157d44..0000000
--- a/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/SQLAnalyzer.java
+++ /dev/null
@@ -1,932 +0,0 @@
-/**
- * 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.frontend.sql;
-
-import com.google.common.base.Preconditions;
-import org.antlr.runtime.ANTLRStringStream;
-import org.antlr.runtime.CommonTokenStream;
-import org.antlr.runtime.RecognitionException;
-import org.antlr.runtime.tree.CommonTree;
-import org.antlr.runtime.tree.Tree;
-import org.apache.tajo.algebra.*;
-import org.apache.tajo.algebra.Aggregation.GroupElement;
-import org.apache.tajo.algebra.Aggregation.GroupType;
-import org.apache.tajo.algebra.LiteralExpr.LiteralType;
-import org.apache.tajo.algebra.Sort.SortSpec;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import static org.apache.tajo.algebra.CreateTable.ColumnDefinition;
-
-public class SQLAnalyzer {
-
-  public Expr parse(String sql) throws SQLSyntaxError {
-    ParsingContext context = new ParsingContext(sql);
-    CommonTree tree = parseSQL(sql);
-    return transform(context, tree);
-  }
-
-  private static class ParsingContext {
-    private String rawQuery;
-    public ParsingContext(String sql) {
-      this.rawQuery = sql;
-    }
-  }
-
-  public static CommonTree parseSQL(String sql) {
-    ANTLRStringStream input = new ANTLRStringStream(sql);
-    SQLLexer lexer = new SQLLexer(input);
-    CommonTokenStream tokens = new CommonTokenStream(lexer);
-    SQLParser parser = new SQLParser(tokens);
-
-    CommonTree ast;
-    try {
-      ast = ((CommonTree) parser.statement().getTree());
-    } catch (RecognitionException e) {
-      throw new SQLParseError(e.getMessage());
-    }
-
-    return ast;
-  }
-
-  Expr transform(ParsingContext context, CommonTree ast) throws SQLSyntaxError {
-    switch (ast.getType()) {
-      case SQLParser.SELECT:
-        return parseSelectStatement(context, ast);
-
-      case SQLParser.UNION:
-      case SQLParser.EXCEPT:
-      case SQLParser.INTERSECT:
-        return parseSetStatement(context, ast);
-
-      case SQLParser.INSERT:
-
-      case SQLParser.CREATE_INDEX:
-
-      case SQLParser.CREATE_TABLE:
-        return parseCreateStatement(context, ast);
-
-      case SQLParser.DROP_TABLE:
-
-      default:
-        return null;
-    }
-  }
-
-  /**
-   * t=table ASSIGN select_stmt -> ^(CREATE_TABLE $t select_stmt)
-   * | CREATE TABLE t=table AS select_stmt -> ^(CREATE_TABLE $t select_stmt)
-   *
-   * @param ast
-   * @return
-   */
-  private CreateTable parseCreateStatement(final ParsingContext context,
-                                               final CommonTree ast) throws SQLSyntaxError {
-    CreateTable createTable;
-
-    int idx = 0;
-    CommonTree node;
-    String tableName = ast.getChild(idx).getText();
-    idx++;
-
-    boolean external = false;
-    ColumnDefinition [] tableElements = null;
-    String storeType = null;
-    Map<String, String> params = null;
-    Expr nestedAlgebra = null;
-    String location = null;
-    while(idx < ast.getChildCount()) {
-      node = (CommonTree) ast.getChild(idx);
-      switch (node.getType()) {
-        case SQLParser.EXTERNAL:
-          external = true;
-          break;
-
-        case SQLParser.TABLE_DEF:
-          tableElements = parseTableElementList(node);
-          break;
-
-        case SQLParser.USING:
-          storeType = node.getChild(0).getText();
-          break;
-
-        case SQLParser.PARAMS:
-          params = parseParams(node);
-          break;
-
-        case SQLParser.AS:
-          nestedAlgebra = parseSelectStatement(context, node.getChild(0));
-          break;
-
-        case SQLParser.LOCATION:
-          location = node.getChild(0).getText();
-          break;
-
-        default:
-          throw new SQLSyntaxError(context.rawQuery, "ERROR: not yet supported query");
-      }
-      idx++;
-    }
-
-    if (nestedAlgebra != null) {
-      createTable = new CreateTable(tableName, nestedAlgebra);
-    } else {
-      createTable = new CreateTable(tableName);
-      if (external) {
-        if (location != null) {
-          createTable.setLocation(location);
-        }
-      }
-    }
-
-    if (tableElements != null) {
-      createTable.setTableElements(tableElements);
-    }
-
-    if (storeType != null) {
-      createTable.setStorageType(storeType);
-      if (params != null) {
-        createTable.setParams(params);
-      }
-    }
-
-    // constraints
-    if (external && location == null) {
-      throw new SQLSyntaxError(context.rawQuery,
-          "CREATE EXTERNAL TABLE statement requires LOCATION clause.");
-    }
-    if (!external && location != null) {
-      throw new SQLSyntaxError(context.rawQuery,
-          "LOCATION clause can be only used in CREATE EXTERNAL TABLE statement.");
-    }
-    if (tableElements == null && location != null) {
-      throw new SQLSyntaxError(context.rawQuery,
-          "LOCATION clause requires a schema definition.");
-    }
-
-    return createTable;
-  }
-
-  private ColumnDefinition [] parseTableElementList(final CommonTree ast) {
-    ColumnDefinition [] tableDef = new ColumnDefinition[ast.getChildCount()];
-    for (int i = 0; i < ast.getChildCount(); i++) {
-      tableDef[i] = new ColumnDefinition(ast.getChild(i).getChild(0).getText(),
-          ast.getChild(i).getChild(1).getText());;
-    }
-
-    return tableDef;
-  }
-
-  /**
-   * Should be given Params Node
-   *
-   * EBNF: WITH LEFT_PAREN param (COMMA param)* RIGHT_PAREN
-   * AST: ^(PARAMS param+)
-   *
-   * @param ast
-   * @return
-   */
-  private static Map<String,String> parseParams(final CommonTree ast) {
-    Map<String, String> params = new HashMap<String, String>();
-
-    Tree child;
-    for (int i = 0; i < ast.getChildCount(); i++) {
-      child = ast.getChild(i);
-      params.put(child.getChild(0).getText(), child.getChild(1).getText());
-    }
-    return params;
-  }
-
-  private Expr parseSelectStatement(final ParsingContext context, final Tree ast) throws SQLSyntaxError {
-    CommonTree node;
-    QueryBlock block = new QueryBlock();
-    for (int cur = 0; cur < ast.getChildCount(); cur++) {
-      node = (CommonTree) ast.getChild(cur);
-
-      switch (node.getType()) {
-        case SQLParser.FROM:
-          Expr fromClause = parseFromClause(context, node);
-          block.setTableExpression(fromClause);
-          break;
-
-        case SQLParser.SET_QUALIFIER:
-          if (parseSetQualifier(node)) {
-            block.setDistinct();
-          }
-          break;
-
-        case SQLParser.SEL_LIST:
-          Projection projection = parseSelectList(context, node);
-          block.setProjection(projection);
-          break;
-
-        case SQLParser.WHERE:
-          block.setSearchCondition(parseWhereClause(context, node));
-          break;
-
-        case SQLParser.GROUP_BY:
-          Aggregation aggregation = parseGroupByClause(node);
-          block.setAggregation(aggregation);
-          break;
-
-        case SQLParser.HAVING:
-          Expr expr = parseHavingClause(context, node);
-          block.setHavingCondition(expr);
-          break;
-
-        case SQLParser.ORDER_BY:
-          SortSpec [] sortSpecs = parseSortSpecifiers(node.getChild(0));
-          block.setSort(new Sort(sortSpecs));
-          break;
-
-        case SQLParser.LIMIT:
-          block.setLimit(parseLimitClause(context, node));
-          break;
-
-        default:
-      }
-    }
-
-    return transformQueryBlock(context, block);
-  }
-
-  /**
-   * There is an algebraic order of a QueryBlock.
-   * Relation (or Join) -> Selection -> Aggregation -> Sort -> Limit
-   *
-   * @param context
-   * @param block
-   * @return
-   */
-  private Expr transformQueryBlock(ParsingContext context, QueryBlock block) throws SQLSyntaxError {
-    Expr current = null;
-
-    // Selection
-    if (block.hasTableExpression()) {
-      current = block.getTableExpression();
-    }
-
-    if (block.hasSearchCondition()) {
-      if (current == null) {
-        throw new SQLSyntaxError(context.rawQuery,
-            "No TableExpression, but there exists search condition");
-      }
-      Selection selection = new Selection(current, block.getSearchCondition());
-      current = selection;
-    }
-
-    // Aggregation
-    Aggregation aggregation = null;
-    if (block.hasAggregation()) {
-      if (current == null) {
-        throw new SQLSyntaxError(context.rawQuery,
-            "No TableExpression, but there exists a group-by clause");
-      }
-      aggregation = block.getAggregation();
-
-      if (block.hasAggregation()) {
-        aggregation.setHavingCondition(block.getHavingCondition());
-      }
-
-      aggregation.setChild(current);
-      current = aggregation;
-    }
-
-    if (block.hasSort()) {
-      if (current == null) {
-        throw new SQLSyntaxError(context.rawQuery,
-            "No TableExpression, but there exists a sort clause");
-      }
-      Sort sort = block.getSort();
-      sort.setChild(current);
-      current = sort;
-    }
-
-    if (block.hasLimit()) {
-      if (current == null) {
-        throw new SQLSyntaxError(context.rawQuery,
-            "No TableExpression, but there exists a limit clause");
-      }
-      Limit limit = block.getLimit();
-      limit.setChild(current);
-      current = limit;
-    }
-
-    Projection projection = block.getProjection();
-    if (block.hasAggregation()) {
-      aggregation.setTargets(projection.getTargets());
-    } else {
-      if (block.isDistinct()) {
-        projection.setDistinct();
-      }
-      if (current != null) {
-        projection.setChild(current);
-      }
-      current = projection;
-    }
-
-    return current;
-  }
-
-  private Expr parseSetStatement(final ParsingContext context,
-                                 final CommonTree ast) throws SQLSyntaxError {
-    Expr left;
-    Expr right;
-
-    ExprType type = tokenToExprType(ast.getType());
-
-    int idx = 0;
-    left = transform(context, (CommonTree) ast.getChild(idx));
-    idx++;
-    int nodeType = ast.getChild(idx).getType();
-    boolean distinct = true; // distinct is default in ANSI SQL standards
-    if (nodeType == SQLParser.ALL) {
-      distinct = true;
-      idx++;
-    } else if (nodeType == SQLParser.DISTINCT) {
-      distinct = false;
-      idx++;
-    }
-    right = transform(context, (CommonTree) ast.getChild(idx));
-    return new SetOperation(type, left, right, distinct);
-  }
-
-  /**
-   * @return true if SELECT DISTINCT. Otherwise, it will be false.
-   */
-  private boolean parseSetQualifier(final CommonTree ast) {
-    int idx = 0;
-
-    if (ast.getChild(idx).getType() == SQLParser.DISTINCT) {
-      return true;
-    } else {
-      return false;
-    }
-  }
-
-  private Expr parseHavingClause(final ParsingContext context, final CommonTree ast)
-      throws SQLSyntaxError {
-    return createExpression(context, ast.getChild(0));
-  }
-
-  private Limit parseLimitClause(final ParsingContext context, final CommonTree ast)
-      throws SQLSyntaxError {
-    Expr expr = createExpression(context, ast.getChild(0));
-
-    if (expr instanceof LiteralExpr) {
-      Limit limitClause = new Limit(expr);
-      return limitClause;
-    }
-
-    throw new SQLSyntaxError(context.rawQuery, "LIMIT clause cannot have the parameter "
-        + expr);
-  }
-
-  /**
-   * Should be given SortSpecifiers Node
-   *
-   * EBNF: sort_specifier (COMMA sort_specifier)* -> sort_specifier+
-   *
-   * @param ast
-   */
-  private SortSpec[] parseSortSpecifiers(final Tree ast) {
-    int numSortKeys = ast.getChildCount();
-    SortSpec[] sortSpecs = new SortSpec[numSortKeys];
-    CommonTree node;
-    ColumnReferenceExpr column;
-
-    // Each child has the following EBNF and AST:
-    // EBNF: fn=fieldName a=order_specification? o=null_ordering?
-    // AST: ^(SORT_KEY $fn $a? $o?)
-    for (int i = 0; i < numSortKeys; i++) {
-      node = (CommonTree) ast.getChild(i);
-      column = checkAndGetColumnByAST(node.getChild(0));
-      sortSpecs[i] = new SortSpec(column);
-
-      if (node.getChildCount() > 1) {
-        Tree child;
-        for (int j = 1; j < node.getChildCount(); j++) {
-          child = node.getChild(j);
-
-          // AST: ^(ORDER ASC) | ^(ORDER DESC)
-          if (child.getType() == SQLParser.ORDER) {
-            if (child.getChild(0).getType() == SQLParser.DESC) {
-              sortSpecs[i].setDescending();
-            }
-          } else if (child.getType() == SQLParser.NULL_ORDER) {
-            // AST: ^(NULL_ORDER FIRST) | ^(NULL_ORDER LAST)
-            if (child.getChild(0).getType() == SQLParser.FIRST) {
-              sortSpecs[i].setNullFirst();
-            }
-          }
-        }
-      }
-    }
-
-    return sortSpecs;
-  }
-
-  /**
-   * See 'groupby_clause' rule in SQL.g
-   *
-   * @param ast
-   */
-  private Aggregation parseGroupByClause(final CommonTree ast) {
-    int idx = 0;
-    Aggregation clause = new Aggregation();
-    if (ast.getChild(idx).getType() == SQLParser.EMPTY_GROUPING_SET) {
-
-    } else {
-      // the remain ones are grouping fields.
-      Tree group;
-      List<ColumnReferenceExpr> columnRefs = new ArrayList<ColumnReferenceExpr>();
-      ColumnReferenceExpr[] columns;
-      ColumnReferenceExpr column;
-      List<GroupElement> groups = new ArrayList<GroupElement>();
-      for (; idx < ast.getChildCount(); idx++) {
-        group = ast.getChild(idx);
-        switch (group.getType()) {
-          case SQLParser.CUBE:
-            columns = parseColumnReferences((CommonTree) group);
-            GroupElement cube = new GroupElement(GroupType.CUBE, columns);
-            groups.add(cube);
-            break;
-
-          case SQLParser.ROLLUP:
-            columns = parseColumnReferences((CommonTree) group);
-            GroupElement rollup = new GroupElement(GroupType.ROLLUP, columns);
-            groups.add(rollup);
-            break;
-
-          case SQLParser.FIELD_NAME:
-            column = checkAndGetColumnByAST(group);
-            columnRefs.add(column);
-            break;
-        }
-      }
-
-      if (columnRefs.size() > 0) {
-        ColumnReferenceExpr[] groupingFields = columnRefs.toArray(new ColumnReferenceExpr[columnRefs.size()]);
-        GroupElement g = new GroupElement(GroupType.GROUPBY, groupingFields);
-        groups.add(g);
-      }
-
-      clause.setGroups(groups.toArray(new GroupElement[groups.size()]));
-    }
-    return clause;
-  }
-
-
-
-  /**
-   * It parses the below EBNF.
-   * <pre>
-   * column_reference
-   * : fieldName (COMMA fieldName)* -> fieldName+
-   * ;
-   * </pre>
-   * @param parent
-   * @return
-   */
-  private ColumnReferenceExpr[] parseColumnReferences(final CommonTree parent) {
-    ColumnReferenceExpr[] columns = new ColumnReferenceExpr[parent.getChildCount()];
-
-    for (int i = 0; i < columns.length; i++) {
-      columns[i] = checkAndGetColumnByAST(parent.getChild(i));
-    }
-
-    return columns;
-  }
-
-  private Expr parseWhereClause(final ParsingContext context, final CommonTree ast)
-      throws SQLSyntaxError {
-    return createExpression(context, ast.getChild(0));
-  }
-
-  /**
-   * This method parses the select list of a query statement.
-   * <pre>
-   * EBNF:
-   *
-   * selectList
-   * : MULTIPLY -> ^(SEL_LIST ALL)
-   * | derivedColumn (COMMA derivedColumn)* -> ^(SEL_LIST derivedColumn+)
-   * ;
-   *
-   * derivedColumn
-   * : bool_expr asClause? -> ^(COLUMN bool_expr asClause?)
-   * ;
-   *
-   * @param ast
-   */
-  private Projection parseSelectList(ParsingContext context, final CommonTree ast)
-      throws SQLSyntaxError {
-    Projection projection = new Projection();
-    if (ast.getChild(0).getType() == SQLParser.ALL) {
-      projection.setAll();
-    } else {
-      CommonTree node;
-      int numTargets = ast.getChildCount();
-      Target [] targets = new Target[numTargets];
-      Expr evalTree;
-      String alias;
-
-      // the final one for each target is the alias
-      // EBNF: bool_expr AS? fieldName
-      for (int i = 0; i < ast.getChildCount(); i++) {
-        node = (CommonTree) ast.getChild(i);
-        evalTree = createExpression(context, node);
-        targets[i] = new Target(evalTree);
-        if (node.getChildCount() > 1) {
-          alias = node.getChild(node.getChildCount() - 1).getChild(0).getText();
-          targets[i].setAlias(alias);
-        }
-      }
-      projection.setTargets(targets);
-    }
-
-    return projection;
-  }
-
-  /**
-   * EBNF: table_list -> tableRef (COMMA tableRef)
-   * @param ast
-   */
-  private Expr parseFromClause(ParsingContext context, final CommonTree ast) throws SQLSyntaxError {
-    Expr previous = null;
-    CommonTree node;
-    for (int i = 0; i < ast.getChildCount(); i++) {
-      node = (CommonTree) ast.getChild(i);
-
-      switch (node.getType()) {
-
-        case SQLParser.TABLE:
-          // table (AS ID)?
-          // 0 - a table name, 1 - table alias
-          if (previous != null) {
-            Expr inner = parseTable(node);
-            Join newJoin = new Join(JoinType.INNER);
-            newJoin.setLeft(previous);
-            newJoin.setRight(inner);
-            previous = newJoin;
-          } else {
-            previous = parseTable(node);
-          }
-          break;
-        case SQLParser.JOIN:
-          Join newJoin = parseExplicitJoinClause(context, node);
-          newJoin.setLeft(previous);
-          previous = newJoin;
-          break;
-
-        case SQLParser.SUBQUERY:
-          Expr nestedAlgebra = parseSelectStatement(context, node.getChild(0));
-          String alias = node.getChild(1).getText();
-          previous = new TableSubQuery(alias, nestedAlgebra);
-          break;
-
-        default:
-          throw new SQLSyntaxError(context.rawQuery, "Wrong From Clause");
-      } // switch
-    } // for each derievedTable
-
-    return previous;
-  }
-
-  private Relation parseTable(final CommonTree tableAST) {
-    String tableName = tableAST.getChild(0).getText();
-    Relation table = new Relation(tableName);
-
-    if (tableAST.getChildCount() > 1) {
-      table.setAlias(tableAST.getChild(1).getText());
-    }
-
-    return table;
-  }
-
-  private Join parseExplicitJoinClause(ParsingContext ctx, final CommonTree ast)
-      throws SQLSyntaxError {
-
-    int idx = 0;
-    int parsedJoinType = ast.getChild(idx).getType();
-
-    Join joinClause;
-
-    switch (parsedJoinType) {
-      case SQLParser.CROSS:
-      case SQLParser.UNION:
-        joinClause = parseCrossAndUnionJoin(ast);
-        break;
-
-      case SQLParser.NATURAL:
-        joinClause = parseNaturalJoinClause(ctx, ast);
-        break;
-
-      case SQLParser.INNER:
-      case SQLParser.OUTER:
-        joinClause = parseQualifiedJoinClause(ctx, ast, 0);
-        break;
-
-      default: // default join (without join type) is inner join
-        joinClause = parseQualifiedJoinClause(ctx, ast, 0);
-    }
-
-    return joinClause;
-  }
-
-  private Join parseNaturalJoinClause(ParsingContext ctx, Tree ast) throws SQLSyntaxError {
-    Join join = parseQualifiedJoinClause(ctx, ast, 1);
-    join.setNatural();
-    return join;
-  }
-
-  private Join parseQualifiedJoinClause(ParsingContext context, final Tree ast, final int idx)
-      throws SQLSyntaxError {
-    int childIdx = idx;
-    Join join = null;
-
-    if (ast.getChild(childIdx).getType() == SQLParser.TABLE) { // default join
-      join = new Join(JoinType.INNER);
-      join.setRight(parseTable((CommonTree) ast.getChild(childIdx)));
-
-    } else {
-
-      if (ast.getChild(childIdx).getType() == SQLParser.INNER) {
-        join = new Join(JoinType.INNER);
-
-      } else if (ast.getChild(childIdx).getType() == SQLParser.OUTER) {
-
-        switch (ast.getChild(childIdx).getChild(0).getType()) {
-          case SQLParser.LEFT:
-            join = new Join(JoinType.LEFT_OUTER);
-            break;
-          case SQLParser.RIGHT:
-            join = new Join(JoinType.RIGHT_OUTER);
-            break;
-          case SQLParser.FULL:
-            join = new Join(JoinType.FULL_OUTER);
-            break;
-          default:
-            throw new SQLSyntaxError(context.rawQuery, "Unknown Join Type");
-        }
-      }
-
-      childIdx++;
-      join.setRight(parseTable((CommonTree) ast.getChild(childIdx)));
-    }
-
-    childIdx++;
-
-    if (ast.getChildCount() > childIdx) {
-      CommonTree joinQual = (CommonTree) ast.getChild(childIdx);
-
-      if (joinQual.getType() == SQLParser.ON) {
-        Expr joinCond = parseJoinCondition(context, joinQual);
-        join.setQual(joinCond);
-
-      } else if (joinQual.getType() == SQLParser.USING) {
-        ColumnReferenceExpr[] joinColumns = parseJoinColumns(joinQual);
-        join.setJoinColumns(joinColumns);
-      }
-    }
-
-    return join;
-  }
-
-  private Join parseCrossAndUnionJoin(Tree ast) {
-    JoinType joinType;
-
-    if (ast.getChild(0).getType() == SQLParser.CROSS) {
-      joinType = JoinType.CROSS_JOIN;
-    } else if (ast.getChild(0).getType() == SQLParser.UNION) {
-      joinType = JoinType.UNION;
-    } else {
-      throw new IllegalStateException("Neither the AST has cross join or union join:\n"
-          + ast.toStringTree());
-    }
-
-    Join join = new Join(joinType);
-    Preconditions.checkState(ast.getChild(1).getType() == SQLParser.TABLE);
-    join.setRight(parseTable((CommonTree) ast.getChild(1)));
-
-    return join;
-  }
-
-  private ColumnReferenceExpr[] parseJoinColumns(final CommonTree ast) {
-    ColumnReferenceExpr[] joinColumns = new ColumnReferenceExpr[ast.getChildCount()];
-
-    for (int i = 0; i < ast.getChildCount(); i++) {
-      joinColumns[i] = checkAndGetColumnByAST(ast.getChild(i));
-    }
-    return joinColumns;
-  }
-
-  private Expr parseJoinCondition(ParsingContext context, CommonTree ast) throws SQLSyntaxError {
-    return createExpression(context, ast.getChild(0));
-  }
-
-  private ColumnReferenceExpr checkAndGetColumnByAST(final Tree fieldNode) {
-    Preconditions.checkArgument(SQLParser.FIELD_NAME == fieldNode.getType());
-
-    String columnName = fieldNode.getChild(0).getText();
-    ColumnReferenceExpr column = new ColumnReferenceExpr(columnName);
-
-    String tableName = null;
-    if (fieldNode.getChildCount() > 1) {
-      tableName = fieldNode.getChild(1).getText();
-      column.setRelationName(tableName);
-    }
-
-    return column;
-  }
-
-  /**
-   * The EBNF of case statement
-   * <pre>
-   * searched_case
-   * : CASE s=searched_when_clauses e=else_clause END -> ^(CASE $s $e)
-   * ;
-   *
-   * searched_when_clauses
-   * : searched_when_clause searched_when_clause* -> searched_when_clause+
-   * ;
-   *
-   * searched_when_clause
-   * : WHEN c=search_condition THEN r=result -> ^(WHEN $c $r)
-   * ;
-   *
-   * else_clause
-   * : ELSE r=result -> ^(ELSE $r)
-   * ;
-   * </pre>
-   * @param tree
-   * @return
-   */
-  public CaseWhenExpr parseCaseWhen(ParsingContext context, final Tree tree) throws SQLSyntaxError {
-    int idx = 0;
-
-    CaseWhenExpr caseEval = new CaseWhenExpr();
-    Expr cond;
-    Expr thenResult;
-    Tree when;
-
-    for (; idx < tree.getChildCount() &&
-        tree.getChild(idx).getType() == SQLParser.WHEN; idx++) {
-
-      when = tree.getChild(idx);
-      cond = createExpression(context, when.getChild(0));
-      thenResult = createExpression(context, when.getChild(1));
-      caseEval.addWhen(cond, thenResult);
-    }
-
-    if (tree.getChild(idx) != null &&
-        tree.getChild(idx).getType() == SQLParser.ELSE) {
-      Expr elseResult = createExpression(context, tree.getChild(idx).getChild(0));
-      caseEval.setElseResult(elseResult);
-    }
-
-    return caseEval;
-  }
-
-  /**
-   * <pre>
-   * like_predicate : fieldName NOT? LIKE string_value_expr
-   * -> ^(LIKE NOT? fieldName string_value_expr)
-   * </pre>
-   * @param tree
-   * @return
-   */
-  private LikeExpr parseLike(ParsingContext context, final Tree tree) throws SQLSyntaxError {
-    int idx = 0;
-
-    boolean not = false;
-    if (tree.getChild(idx).getType() == SQLParser.NOT) {
-      not = true;
-      idx++;
-    }
-
-    ColumnReferenceExpr field = (ColumnReferenceExpr) createExpression(context, tree.getChild(idx));
-    idx++;
-    Expr pattern = createExpression(context, tree.getChild(idx));
-
-    return new LikeExpr(not, field, pattern);
-  }
-
-  public Expr createExpression(final ParsingContext context, final Tree ast) throws SQLSyntaxError {
-    switch(ast.getType()) {
-
-      // constants
-      case SQLParser.Unsigned_Integer:
-        return new LiteralExpr(ast.getText(), LiteralType.Unsigned_Integer);
-      case SQLParser.Unsigned_Float:
-        return new LiteralExpr(ast.getText(), LiteralType.Unsigned_Float);
-      case SQLParser.Unsigned_Large_Integer:
-        return new LiteralExpr(ast.getText(), LiteralType.Unsigned_Large_Integer);
-
-      case SQLParser.Character_String_Literal:
-        return new LiteralExpr(ast.getText(), LiteralType.String);
-
-      // unary expression
-      case SQLParser.NOT:
-        ;
-
-      // binary expressions
-      case SQLParser.LIKE:
-        return parseLike(context, ast);
-
-      case SQLParser.IS:
-        break;
-
-      case SQLParser.AND:
-      case SQLParser.OR:
-      case SQLParser.Equals_Operator:
-      case SQLParser.Not_Equals_Operator:
-      case SQLParser.Less_Than_Operator:
-      case SQLParser.Less_Or_Equals_Operator:
-      case SQLParser.Greater_Than_Operator:
-      case SQLParser.Greater_Or_Equals_Operator:
-      case SQLParser.Plus_Sign:
-      case SQLParser.Minus_Sign:
-      case SQLParser.Asterisk:
-      case SQLParser.Slash:
-      case SQLParser.Percent:
-        return new BinaryOperator(tokenToExprType(ast.getType()),
-            createExpression(context, ast.getChild(0)),
-            createExpression(context, ast.getChild(1)));
-
-      // others
-      case SQLParser.COLUMN:
-        return createExpression(context, ast.getChild(0));
-
-      case SQLParser.FIELD_NAME:
-        return checkAndGetColumnByAST(ast);
-
-      case SQLParser.FUNCTION:
-        String signature = ast.getText();
-        FunctionExpr func = new FunctionExpr(signature);
-        Expr[] givenArgs = new Expr[ast.getChildCount()];
-
-        for (int i = 0; i < ast.getChildCount(); i++) {
-          givenArgs[i] = createExpression(context, ast.getChild(i));
-        }
-        func.setParams(givenArgs);
-
-        break;
-      case SQLParser.COUNT_VAL:
-
-      case SQLParser.COUNT_ROWS:
-
-
-      case SQLParser.CASE:
-        return parseCaseWhen(context, ast);
-
-      case SQLParser.SUBQUERY: // ^(SUBQUERY subquery)
-        return new ScalarSubQuery(parseSelectStatement(context, ast.getChild(0)));
-
-      default:
-    }
-    return null;
-  }
-
-  public static ExprType tokenToExprType(int tokenId) {
-    switch (tokenId) {
-      case SQLParser.UNION: return ExprType.Union;
-      case SQLParser.EXCEPT: return ExprType.Except;
-      case SQLParser.INTERSECT: return ExprType.Intersect;
-
-      case SQLParser.AND: return ExprType.And;
-      case SQLParser.OR: return ExprType.Or;
-      case SQLParser.Equals_Operator: return ExprType.Equals;
-      case SQLParser.Less_Than_Operator: return ExprType.LessThan;
-      case SQLParser.Less_Or_Equals_Operator: return ExprType.LessThan;
-      case SQLParser.Greater_Than_Operator: return ExprType.GreaterThan;
-      case SQLParser.Greater_Or_Equals_Operator: return ExprType.GreaterThanOrEquals;
-      case SQLParser.Plus_Sign: return ExprType.Plus;
-      case SQLParser.Minus_Sign: return ExprType.Minus;
-      case SQLParser.Asterisk: return ExprType.Multiply;
-      case SQLParser.Slash: return ExprType.Divide;
-      case SQLParser.Percent: return ExprType.Mod;
-
-      default: throw new RuntimeException("Unknown Token Id: " + tokenId);
-    }
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/SQLParseError.java
----------------------------------------------------------------------
diff --git a/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/SQLParseError.java b/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/SQLParseError.java
deleted file mode 100644
index d3d6d91..0000000
--- a/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/SQLParseError.java
+++ /dev/null
@@ -1,27 +0,0 @@
-/**
- * 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.frontend.sql;
-
-
-@SuppressWarnings("UnusedDeclaration")
-public class SQLParseError extends RuntimeException {
-  public SQLParseError(String parseErrorMessage) {
-    super(parseErrorMessage);
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/SQLSyntaxError.java
----------------------------------------------------------------------
diff --git a/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/SQLSyntaxError.java b/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/SQLSyntaxError.java
deleted file mode 100644
index 0aa1276..0000000
--- a/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/SQLSyntaxError.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/**
- * 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.frontend.sql;
-
-
-public class SQLSyntaxError extends Exception {
-	private static final long serialVersionUID = 5388279335175632066L;
-
-  private String query;
-  private String errorMessage;
-  private String detailedMessage;
-
-	public SQLSyntaxError(String query, String errorMessage) {
-    this.query = query;
-    this.errorMessage = errorMessage;
-	}
-
-  @Override
-  public String getMessage() {
-    if (detailedMessage == null) {
-      StringBuilder sb = new StringBuilder();
-      sb.append("ERROR: " + errorMessage).append("\n");
-      sb.append("LINE: " + query);
-      detailedMessage = sb.toString();
-    }
-
-    return detailedMessage;
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-frontend/tajo-frontend-sql/src/test/java/org/apache/tajo/frontend/sql/TestSQLAnalyzer.java
----------------------------------------------------------------------
diff --git a/tajo-frontend/tajo-frontend-sql/src/test/java/org/apache/tajo/frontend/sql/TestSQLAnalyzer.java b/tajo-frontend/tajo-frontend-sql/src/test/java/org/apache/tajo/frontend/sql/TestSQLAnalyzer.java
deleted file mode 100644
index 7f3755c..0000000
--- a/tajo-frontend/tajo-frontend-sql/src/test/java/org/apache/tajo/frontend/sql/TestSQLAnalyzer.java
+++ /dev/null
@@ -1,572 +0,0 @@
-/**
- * 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.frontend.sql;
-
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.apache.tajo.algebra.*;
-import org.apache.tajo.algebra.Aggregation.GroupElement;
-
-import static junit.framework.Assert.*;
-import static org.apache.tajo.algebra.Aggregation.GroupType;
-
-public class TestSQLAnalyzer {
-  private static SQLAnalyzer analyzer = null;
-
-  @BeforeClass
-  public static void setup() {
-    analyzer = new SQLAnalyzer();
-  }
-
-  private String[] QUERIES = {
-      "select id, name, score, age from people", // 0
-      "select name, score, age from people where score > 30", // 1
-      "select name, score, age from people where 3 + 5 * 3", // 2
-      "select age, sumtest(score) as total from people group by age having sumtest(score) > 30", // 3
-      "select p.id, s.id, score, dept from people as p, student as s where p.id = s.id", // 4
-      "select name, score from people order by score asc, age desc null first", // 5
-      // only expr
-      "select 7 + 8 as total", // 6
-      // limit test
-      "select id, name, score, age from people limit 3" // 7
-  };
-
-  @Test
-  public final void testSelectStatement() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(QUERIES[0]);
-    assertEquals(ExprType.Projection, expr.getType());
-    Projection projection = (Projection) expr;
-    assertEquals(ExprType.Relation, projection.getChild().getType());
-    Relation relation = (Relation) projection.getChild();
-    assertEquals("people", relation.getName());
-  }
-
-  @Test
-  public final void testSelectStatementWithAlias() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(QUERIES[4]);
-    assertEquals(ExprType.Projection, expr.getType());
-    Projection projection = (Projection) expr;
-    assertEquals(ExprType.Selection, projection.getChild().getType());
-    Selection selection = (Selection) projection.getChild();
-    assertEquals(ExprType.Join, selection.getChild().getType());
-    Join join = (Join) selection.getChild();
-    assertEquals(ExprType.Relation, join.getLeft().getType());
-    Relation outer = (Relation) join.getLeft();
-    assertEquals("p", outer.getAlias());
-    assertEquals(ExprType.Relation, join.getRight().getType());
-    Relation inner = (Relation) join.getRight();
-    assertEquals("s", inner.getAlias());
-  }
-
-  @Test
-  public final void testOrderByClause() throws SQLSyntaxError {
-    Expr block = analyzer.parse(QUERIES[5]);
-    testOrderByCluse(block);
-  }
-
-  @Test
-  public final void testOnlyExpr() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(QUERIES[6]);
-    assertEquals(ExprType.Projection, expr.getType());
-    Projection projection = (Projection) expr;
-    assertEquals(1, projection.getTargets().length);
-    Target target = projection.getTargets()[0];
-    assertEquals("total", target.getAlias());
-    assertEquals(ExprType.Plus, target.getExpr().getType());
-  }
-
-  @Test
-  public void testLimit() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(QUERIES[7]);
-    assertEquals(ExprType.Projection, expr.getType());
-    Projection projection = (Projection) expr;
-    assertEquals(ExprType.Limit, projection.getChild().getType());
-  }
-
-  private String[] GROUP_BY = {
-      "select age, sumtest(score) as total from people group by age having sumtest(score) > 30", // 0
-      "select name, age, sumtest(score) total from people group by cube (name,age)", // 1
-      "select name, age, sumtest(score) total from people group by rollup (name,age)", // 2
-      "select id, name, age, sumtest(score) total from people group by id, cube (name), rollup (age)", // 3
-      "select id, name, age, sumtest(score) total from people group by ()", // 4
-  };
-
-  @Test
-  public final void testGroupByStatement() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(GROUP_BY[0]);
-    assertEquals(ExprType.Aggregation, expr.getType());
-    Aggregation aggregation = (Aggregation) expr;
-
-    assertEquals(1, aggregation.getGroupSet().length);
-    assertEquals("age", aggregation.getGroupSet()[0].getColumns()[0].getName());
-    assertTrue(aggregation.hasHavingCondition());
-    assertEquals(ExprType.GreaterThan, aggregation.getHavingCondition().getType());
-  }
-
-  @Test
-  public final void testCubeByStatement() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(GROUP_BY[1]);
-    assertEquals(ExprType.Aggregation, expr.getType());
-    Aggregation aggregation = (Aggregation) expr;
-    assertEquals(1, aggregation.getGroupSet().length);
-    assertEquals(GroupType.CUBE, aggregation.getGroupSet()[0].getType());
-    GroupElement[] groups = aggregation.getGroupSet();
-    assertEquals("name", groups[0].getColumns()[0].getName());
-    assertEquals("age", groups[0].getColumns()[1].getName());
-  }
-
-  @Test
-  public final void testRollUpStatement() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(GROUP_BY[2]);
-    assertEquals(ExprType.Aggregation, expr.getType());
-    Aggregation aggregation = (Aggregation) expr;
-
-    assertEquals(1, aggregation.getGroupSet().length);
-    assertEquals(GroupType.ROLLUP, aggregation.getGroupSet()[0].getType());
-    GroupElement [] groups = aggregation.getGroupSet();
-    assertEquals("name", groups[0].getColumns()[0].getName());
-    assertEquals("age", groups[0].getColumns()[1].getName());
-  }
-
-  @Test
-  public final void testMixedGroupByStatement() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(GROUP_BY[3]);
-    assertEquals(ExprType.Aggregation, expr.getType());
-    Aggregation aggregation = (Aggregation) expr;
-    assertEquals(3, aggregation.getGroupSet().length);
-    int gid = 0;
-    GroupElement group = aggregation.getGroupSet()[gid++];
-    assertEquals(GroupType.CUBE, group.getType());
-    assertEquals("name", group.getColumns()[0].getName());
-    group = aggregation.getGroupSet()[gid++];
-    assertEquals(GroupType.ROLLUP, group.getType());
-    assertEquals("age", group.getColumns()[0].getName());
-    group = aggregation.getGroupSet()[gid++];
-    assertEquals(GroupType.GROUPBY, group.getType());
-    assertEquals("id", group.getColumns()[0].getName());
-  }
-
-  @Test
-  public final void testEmptyGroupSetStatement() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(GROUP_BY[4]);
-    assertEquals(ExprType.Aggregation, expr.getType());
-    Aggregation block = (Aggregation) expr;
-    assertTrue(block.isEmptyGrouping());
-  }
-
-
-  static String [] JOINS = {
-      "select p.id, name, branch_name from people as p natural join student natural join branch", // 0
-      "select name, dept from people as p inner join student as s on p.id = s.people_id", // 1
-      "select name, dept from people as p inner join student as s using (p.id)", // 2
-      "select p.id, name, branch_name from people as p cross join student cross join branch", // 3
-      "select p.id, dept from people as p left outer join student as s on p.id = s.people_id", // 4
-      "select p.id, dept from people as p right outer join student as s on p.id = s.people_id", // 5
-      "select p.id, dept from people as p join student as s on p.id = s.people_id", // 6
-      "select p.id, dept from people as p left join student as s on p.id = s.people_id", // 7
-      "select p.id, dept from people as p right join student as s on p.id= s.people_id", // 8
-      "select * from table1 " +
-          "cross join table2 " +
-          "join table3 on table1.id = table3.id " +
-          "inner join table4 on table1.id = table4.id " +
-          "left outer join table5 on table1.id = table5.id " +
-          "right outer join table6 on table1.id = table6.id " +
-          "full outer join table7 on table1.id = table7.id " +
-          "natural join table8 " +
-          "natural inner join table9 " +
-          "natural left outer join table10 " +
-          "natural right outer join table11 " +
-          "natural full outer join table12 ", // 9 - all possible join clauses*/
-      "select s_acctbal, s_name, n_name, p_partkey, p_mfgr, s_address, s_phone, s_comment, ps_supplycost " + // 10
-          "from region join nation on n_regionkey = r_regionkey and r_name = 'EUROPE' " +
-          "join supplier on s_nationekey = n_nationkey " +
-          "join partsupp on s_suppkey = ps_ps_suppkey " +
-          "join part on p_partkey = ps_partkey and p_type like '%BRASS' and p_size = 15"
-
-  };
-
-  @Test
-  /**
-   *           join
-   *          /    \
-   *       join    branch
-   *     /     \
-   * people  student
-   *
-   */
-  public final void testNaturalJoinClause() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(JOINS[0]);
-
-
-    Join join = commonJoinTest(expr);
-    assertEquals(JoinType.INNER, join.getJoinType());
-    assertTrue(join.isNatural());
-    Relation branch = (Relation) join.getRight();
-    assertEquals("branch", branch.getName());
-    assertEquals(ExprType.Join, join.getLeft().getType());
-    Join leftJoin = (Join) join.getLeft();
-    Relation people = (Relation) leftJoin.getLeft();
-    Relation student = (Relation) leftJoin.getRight();
-    assertEquals("people", people.getName());
-    assertEquals("student", student.getName());
-  }
-
-  private Join commonJoinTest(Expr expr) {
-    assertEquals(ExprType.Projection, expr.getType());
-    Projection projection = (Projection) expr;
-
-    return (Join) projection.getChild();
-  }
-
-  @Test
-  /**
-   *       join
-   *     /      \
-   * people   student
-   *
-   */
-  public final void testInnerJoinClause() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(JOINS[1]);
-
-    Join join = commonJoinTest(expr);
-    assertEquals(JoinType.INNER, join.getJoinType());
-    Relation people = (Relation) join.getLeft();
-    assertEquals("people", people.getName());
-    assertEquals("p", people.getAlias());
-
-    Relation student = (Relation) join.getRight();
-    assertEquals("student", student.getName());
-    assertEquals("s", student.getAlias());
-    assertTrue(join.hasQual());
-    assertEquals(ExprType.Equals, join.getQual().getType());
-
-    Expr expr2 = analyzer.parse(JOINS[2]);
-    join = commonJoinTest(expr2);
-    assertEquals(JoinType.INNER, join.getJoinType());
-    Relation people2 = (Relation) join.getLeft();
-    assertEquals("people", people2.getName());
-    assertEquals("p", people2.getAlias());
-    Relation student2 = (Relation) join.getRight();
-    assertEquals("student", student2.getName());
-    assertEquals("s", student2.getAlias());
-    assertTrue(join.hasJoinColumns());
-    assertEquals("id", join.getJoinColumns()[0].getName());
-  }
-
-  @Test
-  public final void testCrossJoinClause() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(JOINS[3]);
-
-    Join join = commonJoinTest(expr);
-    assertEquals(JoinType.CROSS_JOIN, join.getJoinType());
-    Relation branch = (Relation) join.getRight();
-    assertEquals("branch", branch.getName());
-    assertEquals(ExprType.Join, join.getLeft().getType());
-    Join leftJoin = (Join) join.getLeft();
-    Relation people = (Relation) leftJoin.getLeft();
-    Relation student = (Relation) leftJoin.getRight();
-    assertEquals("people", people.getName());
-    assertEquals("student", student.getName());
-  }
-
-  @Test
-  public final void testLeftOuterJoinClause() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(JOINS[4]);
-
-    Join join = commonJoinTest(expr);
-    assertEquals(JoinType.LEFT_OUTER, join.getJoinType());
-    Relation people = (Relation) join.getLeft();
-    assertEquals("people", people.getName());
-    assertEquals("p", people.getAlias());
-    Relation student = (Relation) join.getRight();
-    assertEquals("student", student.getName());
-    assertEquals("s", student.getAlias());
-    assertTrue(join.hasQual());
-    assertEquals(ExprType.Equals, join.getQual().getType());
-  }
-
-  @Test
-  public final void testRightOuterJoinClause() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(JOINS[5]);
-
-    Join join = commonJoinTest(expr);
-    assertEquals(JoinType.RIGHT_OUTER, join.getJoinType());
-    Relation people = (Relation) join.getLeft();
-    assertEquals("people", people.getName());
-    assertEquals("p", people.getAlias());
-    Relation student = (Relation) join.getRight();
-    assertEquals("student", student.getName());
-    assertEquals("s", student.getAlias());
-    assertTrue(join.hasQual());
-    assertEquals(ExprType.Equals, join.getQual().getType());
-  }
-
-  @Test
-  public final void testLeftJoinClause() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(JOINS[7]);
-
-    Join join = commonJoinTest(expr);
-    assertEquals(JoinType.LEFT_OUTER, join.getJoinType());
-    Relation people = (Relation) join.getLeft();
-    assertEquals("people", people.getName());
-    assertEquals("p", people.getAlias());
-    Relation student = (Relation) join.getRight();
-    assertEquals("student", student.getName());
-    assertEquals("s", student.getAlias());
-    assertTrue(join.hasQual());
-    assertEquals(ExprType.Equals, join.getQual().getType());
-  }
-
-  @Test
-  public final void testRightJoinClause() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(JOINS[8]);
-
-    Join join = commonJoinTest(expr);
-    assertEquals(JoinType.RIGHT_OUTER, join.getJoinType());
-    Relation people = (Relation) join.getLeft();
-    assertEquals("people", people.getName());
-    assertEquals("p", people.getAlias());
-    Relation student = (Relation) join.getRight();
-    assertEquals("student", student.getName());
-    assertEquals("s", student.getAlias());
-    assertTrue(join.hasQual());
-    assertEquals(ExprType.Equals, join.getQual().getType());
-  }
-
-  private final String [] setClauses = {
-      "select id, people_id from student union select id, people_id from branch",
-      "select id, people_id from student union select id, people_id from branch " +
-          "intersect select id, people_id from branch as b"
-  };
-
-  @Test
-  public final void testUnionClause() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(setClauses[0]);
-    assertEquals(ExprType.Union, expr.getType());
-    SetOperation union = (SetOperation) expr;
-    Expr left = union.getLeft();
-    Expr right = union.getRight();
-
-    assertEquals(ExprType.Projection, left.getType());
-    Projection leftProj = (Projection) left;
-    Relation student = (Relation) leftProj.getChild();
-
-    assertEquals(ExprType.Projection, right.getType());
-    Projection rightProj = (Projection) right;
-    Relation branch = (Relation) rightProj.getChild();
-
-    assertEquals("student", student.getName());
-    assertEquals("branch", branch.getName());
-
-    // multiple set statements
-    expr = analyzer.parse(setClauses[1]);
-    assertEquals(ExprType.Union, expr.getType());
-    union = (SetOperation) expr;
-
-    assertEquals(ExprType.Projection, union.getLeft().getType());
-    assertEquals(ExprType.Intersect, union.getRight().getType());
-    leftProj = (Projection) union.getLeft();
-    student = (Relation) leftProj.getChild();
-    assertEquals("student", student.getName());
-
-    SetOperation intersect = (SetOperation) union.getRight();
-    Relation branch2 = (Relation) ((Projection)intersect.getLeft()).getChild();
-    Relation branch3 = (Relation) ((Projection)intersect.getRight()).getChild();
-    assertEquals("branch", branch2.getName());
-    assertFalse(branch2.hasAlias());
-    assertEquals("branch", branch3.getName());
-    assertEquals("b", branch3.getAlias());
-  }
-
-  @Test
-  public void testCaseWhen() throws SQLSyntaxError {
-    Expr tree = analyzer.parse(
-        "select case when p_type like 'PROMO%' then l_extendedprice * (1 - l_discount) " +
-          "when p_type = 'MOCC' then l_extendedprice - 100 else 0 end as cond from lineitem, part");
-
-    assertEquals(ExprType.Projection, tree.getType());
-    Projection projection = (Projection) tree;
-    assertTrue(projection.getTargets()[0].hasAlias());
-    assertEquals(ExprType.CaseWhen, projection.getTargets()[0].getExpr().getType());
-    assertEquals("cond", projection.getTargets()[0].getAlias());
-    CaseWhenExpr caseWhen = (CaseWhenExpr) projection.getTargets()[0].getExpr();
-    assertEquals(2, caseWhen.getWhens().size());
-  }
-
-  public static String [] subQueries = {
-      "select c1, c2, c3 from (select c1, c2, c3 from employee) as test",
-      "select c1, c2, c3 from table1 where c3 < (select c4 from table2)"
-  };
-
-  @Test
-  public void testTableSubQuery() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(subQueries[0]);
-    assertEquals(ExprType.Projection, expr.getType());
-    Projection projection = (Projection) expr;
-    assertEquals(ExprType.TableSubQuery, projection.getChild().getType());
-  }
-
-  @Test
-  public void testScalarSubQuery() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(subQueries[1]);
-    assertEquals(ExprType.Projection, expr.getType());
-    Projection projection = (Projection) expr;
-    assertEquals(ExprType.Selection, projection.getChild().getType());
-  }
-
-  static final String [] setQualifier = {
-      "select id, people_id from student",
-      "select distinct id, people_id from student",
-      "select all id, people_id from student",
-  };
-
-  @Test
-  public final void testSetQulaifier() throws SQLSyntaxError {
-    Expr expr = analyzer.parse(setQualifier[0]);
-    assertEquals(ExprType.Projection, expr.getType());
-    Projection projection = (Projection) expr;
-    assertFalse(projection.isDistinct());
-
-    expr = analyzer.parse(setQualifier[1]);
-    assertEquals(ExprType.Projection, expr.getType());
-    projection = (Projection) expr;
-    assertTrue(projection.isDistinct());
-
-    expr = analyzer.parse(setQualifier[2]);
-    assertEquals(ExprType.Projection, expr.getType());
-    projection = (Projection) expr;
-    assertFalse(projection.isDistinct());
-  }
-
-  static final String [] createTableStmts = {
-      "create table table1 (name varchar, age int)",
-      "create table table1 (name string, age int) using rcfile",
-      "create table table1 (name string, age int) using rcfile with ('rcfile.buffer' = 4096)",
-      // create table test
-      "create table store1 as select name, score from people order by score asc, age desc null first",// 0
-      // create table test
-      "create table store1 (c1 string, c2 long) as select name, score from people order by score asc, age desc null first",// 1
-      // create table test
-      "create table store2 using rcfile with ('rcfile.buffer' = 4096) as select name, score from people order by score asc, age desc null first", // 2
-      // create table def
-      "create table table1 (name string, age int, earn long, score float) using rcfile with ('rcfile.buffer' = 4096)", // 4
-      // create table def with location
-      "create external table table1 (name string, age int, earn long, score float) using csv with ('csv.delimiter'='|') location '/tmp/data'" // 5
-  };
-
-  @Test
-  public final void testCreateTable1() throws SQLSyntaxError {
-    CreateTable stmt = (CreateTable) analyzer.parse(createTableStmts[0]);
-    assertEquals("table1", stmt.getRelationName());
-    assertTrue(stmt.hasTableElements());
-
-    stmt = (CreateTable) analyzer.parse(createTableStmts[1]);
-    assertEquals("table1", stmt.getRelationName());
-    assertTrue(stmt.hasTableElements());
-    assertTrue(stmt.hasStorageType());
-    assertEquals("rcfile", stmt.getStorageType());
-
-    stmt = (CreateTable) analyzer.parse(createTableStmts[2]);
-    assertEquals("table1", stmt.getRelationName());
-    assertTrue(stmt.hasTableElements());
-    assertTrue(stmt.hasStorageType());
-    assertEquals("rcfile", stmt.getStorageType());
-    assertTrue(stmt.hasParams());
-    assertEquals("4096", stmt.getParams().get("rcfile.buffer"));
-  }
-
-  @Test
-  public final void testCreateTableAsSelect() throws SQLSyntaxError {
-    CreateTable stmt = (CreateTable) analyzer.parse(createTableStmts[3]);
-    assertEquals("store1", stmt.getRelationName());
-    assertTrue(stmt.hasSubQuery());
-    testOrderByCluse(stmt.getSubQuery());
-
-    stmt = (CreateTable) analyzer.parse(createTableStmts[4]);
-    assertEquals("store1", stmt.getRelationName());
-    assertTrue(stmt.hasSubQuery());
-    testOrderByCluse(stmt.getSubQuery());
-    assertTrue(stmt.hasTableElements());
-
-    stmt = (CreateTable) analyzer.parse(createTableStmts[5]);
-    assertEquals("store2", stmt.getRelationName());
-    assertEquals("rcfile", stmt.getStorageType());
-    assertEquals("4096", stmt.getParams().get("rcfile.buffer"));
-    testOrderByCluse(stmt.getSubQuery());
-  }
-
-  private static void testOrderByCluse(Expr block) {
-    Projection projection = (Projection) block;
-    Sort sort = (Sort) projection.getChild();
-
-    assertEquals(2, sort.getSortSpecs().length);
-    Sort.SortSpec spec1 = sort.getSortSpecs()[0];
-    assertEquals("score", spec1.getKey().getName());
-    assertEquals(true, spec1.isAscending());
-    assertEquals(false, spec1.isNullFirst());
-    Sort.SortSpec spec2 = sort.getSortSpecs()[1];
-    assertEquals("age", spec2.getKey().getName());
-    assertEquals(false, spec2.isAscending());
-    assertEquals(true, spec2.isNullFirst());
-  }
-
-  @Test
-  public final void testCreateTableDef1() throws SQLSyntaxError {
-    CreateTable stmt = (CreateTable) analyzer.parse(createTableStmts[6]);
-    assertEquals("table1", stmt.getRelationName());
-    CreateTable.ColumnDefinition[] elements = stmt.getTableElements();
-    assertEquals("name", elements[0].getColumnName());
-    assertEquals("string", elements[0].getDataType());
-    assertEquals("age", elements[1].getColumnName());
-    assertEquals("int", elements[1].getDataType());
-    assertEquals("earn", elements[2].getColumnName());
-    assertEquals("long", elements[2].getDataType());
-    assertEquals("score", elements[3].getColumnName());
-    assertEquals("float", elements[3].getDataType());
-    assertEquals("rcfile", stmt.getStorageType());
-    assertFalse(stmt.hasLocation());
-    assertTrue(stmt.hasParams());
-    assertEquals("4096", stmt.getParams().get("rcfile.buffer"));
-  }
-
-  @Test
-  public final void testCreateTableDef2() throws SQLSyntaxError {
-    CreateTable expr = (CreateTable) analyzer.parse(createTableStmts[7]);
-    _testCreateTableDef2(expr);
-    CreateTable restored = (CreateTable) AlgebraTestingUtil.testJsonSerializer(expr);
-    _testCreateTableDef2(restored);
-  }
-
-  private void _testCreateTableDef2(CreateTable expr) {
-    assertEquals("table1", expr.getRelationName());
-    CreateTable.ColumnDefinition[] elements = expr.getTableElements();
-    assertEquals("name", elements[0].getColumnName());
-    assertEquals("string", elements[0].getDataType());
-    assertEquals("age", elements[1].getColumnName());
-    assertEquals("int", elements[1].getDataType());
-    assertEquals("earn", elements[2].getColumnName());
-    assertEquals("long", elements[2].getDataType());
-    assertEquals("score", elements[3].getColumnName());
-    assertEquals("float", elements[3].getDataType());
-    assertEquals("csv", expr.getStorageType());
-    assertEquals("/tmp/data", expr.getLocation());
-    assertTrue(expr.hasParams());
-    assertEquals("|", expr.getParams().get("csv.delimiter"));
-  }
-}


[07/13] TAJO-87: Integration of tajo algebra module and SQL parser

Posted by hy...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalPlannerUtils.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalPlannerUtils.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalPlannerUtils.java
index 76c5afd..f78dc21 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalPlannerUtils.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalPlannerUtils.java
@@ -21,177 +21,25 @@ package org.apache.tajo.master;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.fs.Path;
-import org.apache.tajo.catalog.*;
+import org.apache.tajo.catalog.CatalogUtil;
+import org.apache.tajo.catalog.Schema;
+import org.apache.tajo.catalog.TableDesc;
+import org.apache.tajo.catalog.TableMeta;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
-import org.apache.tajo.engine.eval.EvalNode;
-import org.apache.tajo.engine.parser.QueryBlock;
-import org.apache.tajo.engine.planner.logical.GroupbyNode;
+import org.apache.tajo.engine.planner.FromTable;
 import org.apache.tajo.engine.planner.logical.ScanNode;
-import org.apache.tajo.engine.planner.logical.StoreTableNode;
-
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.Map;
 
 public class GlobalPlannerUtils {
   private static Log LOG = LogFactory.getLog(GlobalPlannerUtils.class);
 
-  static class WorkerComparatorByNumOfQueryUnits implements Comparator {
-    Map base;
-    public WorkerComparatorByNumOfQueryUnits(Map base) {
-      this.base = base;
-    }
-    public int compare(Object a, Object b) {
-      Collection<QueryUnit> l1 = (Collection<QueryUnit>)base.get(a);
-      Collection<QueryUnit> l2 = (Collection<QueryUnit>)base.get(b);
-      if (l1.size() > l2.size()) {
-        return 1;
-      } else {
-        return -1;
-      }
-    }
-  }
-
- /* public static QueryUnit[] buildQueryDistributionPlan(
-      Map<Fragment, FragmentServingInfo> servingMap,
-      Map<String, List<String>> DNSNameToHostsMap,
-//      Set<String> failedHost,
-      QueryUnit[] queryUnits
-  ) throws UnknownWorkerException {
-    Map<String, Collection<QueryUnit>> map = Maps.newHashMap();
-    ListMultimap<String, QueryUnit> distStatus =
-        Multimaps.newListMultimap(map,
-            new Supplier<List<QueryUnit>>() {
-              @Override
-              public List<QueryUnit> get() {
-                return Lists.newArrayList();
-              }
-            });
-
-    String host;
-    Fragment fragment;
-    FragmentServingInfo servingInfo = null;
-    // build the initial query distribution status
-    for (QueryUnit unit : queryUnits) {
-      Preconditions.checkState(unit.getScanNodes().length == 1);
-      fragment = unit.getFragment(unit.getScanNodes()[0].getTableId());
-      if (servingMap.containsKey(fragment)) {
-        servingInfo = servingMap.get(fragment);
-      } else {
-        servingInfo = null;
-        // error
-      }
-      host = servingInfo.getPrimaryHost();
-      distStatus.put(host, unit);
-    }
-
-    *//*LOG.info("===== before re-balancing =====");
-    for (Map.Entry<String, Collection<QueryUnit>> e : map.entrySet()) {
-      LOG.info(e.getKey() + " : " + e.getValue().size());
-    }
-    LOG.info("\n");*//*
-
-    // re-balancing the query distribution
-    Preconditions.checkState(queryUnits.length >= servingMap.size());
-    int threshold = 0;
-    int mean = queryUnits.length / map.size();
-    int maxQueryUnitNum = mean + threshold;
-    WorkerComparatorByNumOfQueryUnits comp =
-        new WorkerComparatorByNumOfQueryUnits(map);
-    TreeMap<String, Collection<QueryUnit>> sortedMap =
-        Maps.newTreeMap(comp);
-    sortedMap.putAll(map);
-
-    Collection<QueryUnit> fromUnits;
-    Collection<QueryUnit> toUnits;
-    QueryUnit moved;
-    int moveNum = 0;
-    List<Map.Entry<String, Collection<QueryUnit>>> list =
-        Lists.newArrayList(sortedMap.entrySet());
-    int fromIdx = list.size()-1, toIdx = 0;
-    while (fromIdx > toIdx) {
-      toUnits = list.get(toIdx).getValue();
-      fromUnits = list.get(fromIdx).getValue();
-
-      do{
-        moved = fromUnits.iterator().next();
-        toUnits.add(moved);
-        fromUnits.remove(moved);
-        moveNum++;
-      } while (toUnits.size() < maxQueryUnitNum &&
-          fromUnits.size() > maxQueryUnitNum);
-      if (fromUnits.size() <= maxQueryUnitNum) {
-        fromIdx--;
-      }
-      if (toUnits.size() >= maxQueryUnitNum) {
-        toIdx++;
-      }
-    }
-
-    *//*LOG.info("===== after re-balancing " + maxQueryUnitNum + " =====");
-    for (Map.Entry<String, Collection<QueryUnit>> e : list) {
-      LOG.info(e.getKey() + " : " + e.getValue().size());
-    }
-    LOG.info("\n");*//*
-
-    LOG.info(moveNum + " query units among " +
-        queryUnits.length + " are moved!");
-
-    List<String> hosts;
-    int rrIdx = 0;
-    for (Map.Entry<String, Collection<QueryUnit>> e : list) {
-      hosts = DNSNameToHostsMap.get(e.getKey());
-      if (hosts == null) {
-        throw new UnknownWorkerException(e.getKey() + "");
-      }
-      for (QueryUnit unit : e.getValue()) {
-*//*
-        while (failedHost.contains(hosts.get(rrIdx))) {
-          if (++rrIdx == hosts.size()) {
-            rrIdx = 0;
-          }
-        }
-*//*
-        unit.setHost(hosts.get(rrIdx++));
-        if (rrIdx == hosts.size()) {
-          rrIdx = 0;
-        }
-      }
-    }
-
-    return queryUnits;
-  }*/
-
-  public static StoreTableNode newStorePlan(Schema outputSchema,
-                                            String outputTableId) {
-    StoreTableNode store = new StoreTableNode(outputTableId);
-    store.setInSchema(outputSchema);
-    store.setOutSchema(outputSchema);
-    return store;
-  }
-
   public static ScanNode newScanPlan(Schema inputSchema,
                                      String inputTableId,
                                      Path inputPath) {
     TableMeta meta = CatalogUtil.newTableMeta(inputSchema, StoreType.CSV);
     TableDesc desc = CatalogUtil.newTableDesc(inputTableId, meta, inputPath);
-    ScanNode newScan = new ScanNode(new QueryBlock.FromTable(desc));
+    ScanNode newScan = new ScanNode(new FromTable(desc));
     newScan.setInSchema(desc.getMeta().getSchema());
     newScan.setOutSchema(desc.getMeta().getSchema());
     return newScan;
   }
-
-  public static GroupbyNode newGroupbyPlan(Schema inputSchema,
-                                           Schema outputSchema,
-                                           Column[] keys,
-                                           EvalNode havingCondition,
-                                           QueryBlock.Target[] targets) {
-    GroupbyNode groupby = new GroupbyNode(keys);
-    groupby.setInSchema(inputSchema);
-    groupby.setOutSchema(outputSchema);
-    groupby.setHavingCondition(havingCondition);
-    groupby.setTargetList(targets);
-
-    return groupby;
-  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/Query.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/Query.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/Query.java
index 197ff11..6e7092c 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/Query.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/Query.java
@@ -31,16 +31,12 @@ import org.apache.tajo.QueryConf;
 import org.apache.tajo.QueryId;
 import org.apache.tajo.SubQueryId;
 import org.apache.tajo.TajoProtos.QueryState;
-import org.apache.tajo.catalog.*;
-import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
-import org.apache.tajo.engine.json.GsonCreator;
+import org.apache.tajo.catalog.TableDesc;
+import org.apache.tajo.catalog.TableDescImpl;
 import org.apache.tajo.engine.planner.global.MasterPlan;
-import org.apache.tajo.engine.planner.logical.ExprType;
-import org.apache.tajo.engine.planner.logical.IndexWriteNode;
 import org.apache.tajo.master.QueryMaster.QueryContext;
 import org.apache.tajo.master.event.*;
 import org.apache.tajo.storage.StorageManager;
-import org.apache.tajo.util.IndexUtil;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -408,25 +404,6 @@ public class Query implements EventHandler<QueryEvent> {
   private void writeStat(Path outputPath, SubQuery subQuery)
       throws IOException {
     ExecutionBlock execBlock = subQuery.getBlock();
-    if (execBlock.getPlan().getType() == ExprType.CREATE_INDEX) {
-      IndexWriteNode index = (IndexWriteNode) execBlock.getPlan();
-      Path indexPath = new Path(sm.getTablePath(index.getTableName()), "index");
-      TableMeta meta;
-      if (sm.getFileSystem().exists(new Path(indexPath, ".meta"))) {
-        meta = sm.getTableMeta(indexPath);
-      } else {
-        meta = CatalogUtil
-            .newTableMeta(execBlock.getOutputSchema(), StoreType.CSV);
-      }
-      String indexName = IndexUtil.getIndexName(index.getTableName(),
-          index.getSortSpecs());
-      String json = GsonCreator.getInstance().toJson(index.getSortSpecs());
-      meta.putOption(indexName, json);
-
-      sm.writeTableMeta(indexPath, meta);
-
-    } else {
-      sm.writeTableMeta(outputPath, subQuery.getTableMeta());
-    }
+    sm.writeTableMeta(outputPath, subQuery.getTableMeta());
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/QueryUnit.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/QueryUnit.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/QueryUnit.java
index 8489e6c..8eb26bc 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/QueryUnit.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/QueryUnit.java
@@ -148,11 +148,8 @@ public class QueryUnit implements EventHandler<TaskEvent> {
         plan.getType() == ExprType.CREATE_INDEX);
     
 	  this.plan = plan;
-	  if (plan instanceof StoreTableNode) {
-      store = (StoreTableNode) plan;      
-    } else {
-      store = (StoreTableNode) ((IndexWriteNode)plan).getSubNode();
-    }
+    store = (StoreTableNode) plan;
+
 	  LogicalNode node = plan;
 	  ArrayList<LogicalNode> s = new ArrayList<LogicalNode>();
 	  s.add(node);

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/BackendTestingUtil.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/BackendTestingUtil.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/BackendTestingUtil.java
index ea711b8..361a560 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/BackendTestingUtil.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/BackendTestingUtil.java
@@ -25,12 +25,13 @@ import com.google.common.collect.Lists;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
+import org.apache.tajo.algebra.Expr;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
 import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.datum.DatumFactory;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
 import org.apache.tajo.engine.planner.*;
 import org.apache.tajo.engine.planner.logical.LogicalNode;
 import org.apache.tajo.engine.planner.physical.PhysicalExec;
@@ -95,17 +96,17 @@ public class BackendTestingUtil {
 
   private TajoConf conf;
   private CatalogService catalog;
-  private QueryAnalyzer analyzer;
+  private SQLAnalyzer analyzer;
   private LogicalPlanner planner;
   public BackendTestingUtil(TajoConf conf) throws IOException {
     this.conf = conf;
     this.catalog = new LocalCatalog(conf);
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
   }
 
   public ResultSet run(String [] tableNames, File [] tables, Schema [] schemas, String query)
-      throws IOException {
+      throws IOException, CloneNotSupportedException {
     Path workDir = createTmpTestDir();
     StorageManager sm = StorageManager.get(new TajoConf(), workDir);
     List<Fragment> frags = Lists.newArrayList();
@@ -119,11 +120,11 @@ public class BackendTestingUtil {
     TaskAttemptContext ctx = new TaskAttemptContext(conf,
         TUtil.newQueryUnitAttemptId(),
         frags.toArray(new Fragment[frags.size()]), workDir);
-    PlanningContext context = analyzer.parse(query);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr EXPR = analyzer.parse(query);
+    LogicalPlan plan = planner.createPlan(EXPR);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf, sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
 
     return new ResultSetImpl(conf, new Path(workDir, "out"));
   }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/benchmark/TestTPCH.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/benchmark/TestTPCH.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/benchmark/TestTPCH.java
index 9e6ad8d..b47c635 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/benchmark/TestTPCH.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/benchmark/TestTPCH.java
@@ -19,12 +19,12 @@
 package org.apache.tajo.benchmark;
 
 import com.google.common.collect.Maps;
+import org.apache.tajo.IntegrationTest;
+import org.apache.tajo.TpchTestBase;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
-import org.apache.tajo.IntegrationTest;
-import org.apache.tajo.TpchTestBase;
 
 import java.io.IOException;
 import java.sql.ResultSet;
@@ -65,7 +65,7 @@ public class TestTPCH {
   }
 
   @Test
-  public void testQ2FiveWayJoin() throws Exception {
+  public void testQ2FourJoins() throws Exception {
     ResultSet res = tpch.execute(
         "select s_acctbal, s_name, n_name, p_partkey, p_mfgr, s_address, s_phone, s_comment, ps_supplycost, " +
             "r_name, p_type, p_size " +
@@ -85,7 +85,7 @@ public class TestTPCH {
   @Test
   public void testTPCH14Expr() throws Exception {
     ResultSet res = tpch.execute("select 100 * sum(" +
-        "case when p_type like 'PROMO%' then l_extendedprice else 0 end) / sum(l_extendedprice * (1 - l_discount)) "
+        "case when p_type like 'PROMO%' then l_extendedprice else 0.0 end) / sum(l_extendedprice * (1 - l_discount)) "
         + "as promo_revenue from lineitem, part where l_partkey = p_partkey");
 
     res.next();

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/eval/TestEvalTree.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/eval/TestEvalTree.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/eval/TestEvalTree.java
index f1402d0..6b3d14b 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/eval/TestEvalTree.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/eval/TestEvalTree.java
@@ -19,10 +19,8 @@
 package org.apache.tajo.engine.eval;
 
 import org.apache.hadoop.fs.Path;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
 import org.apache.tajo.TajoTestingCluster;
+import org.apache.tajo.algebra.Expr;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.function.GeneralFunction;
 import org.apache.tajo.catalog.proto.CatalogProtos.FunctionType;
@@ -32,19 +30,25 @@ import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.DatumFactory;
 import org.apache.tajo.engine.eval.EvalNode.Type;
 import org.apache.tajo.engine.json.GsonCreator;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
-import org.apache.tajo.engine.parser.QueryBlock;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
+import org.apache.tajo.engine.planner.LogicalPlan;
+import org.apache.tajo.engine.planner.LogicalPlanner;
+import org.apache.tajo.engine.planner.Target;
 import org.apache.tajo.master.TajoMaster;
 import org.apache.tajo.storage.Tuple;
 import org.apache.tajo.storage.VTuple;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
-import static org.junit.Assert.*;
 import static org.apache.tajo.common.TajoDataTypes.Type.*;
+import static org.junit.Assert.*;
 
 public class TestEvalTree {
   private static TajoTestingCluster util;
   private static CatalogService cat;
-  private static QueryAnalyzer analyzer;
+  private static SQLAnalyzer analyzer;
+  private static LogicalPlanner planner;
   private static Tuple [] tuples = new Tuple[3];
   
   @BeforeClass
@@ -70,7 +74,8 @@ public class TestEvalTree {
         CatalogUtil.newDataTypesWithoutLen(INT4, INT4));
     cat.registerFunction(funcMeta);
 
-    analyzer = new QueryAnalyzer(cat);
+    analyzer = new SQLAnalyzer();
+    planner = new LogicalPlanner(cat);
     
     tuples[0] = new VTuple(3);
     tuples[0].put(new Datum[] {
@@ -124,6 +129,18 @@ public class TestEvalTree {
       "select name from people where NOT (20 > 30)", // 5
   };
 
+  public static Target[] getRawTargets(String query) {
+    Expr expr = analyzer.parse(query);
+    LogicalPlan plan = planner.createPlan(expr);
+    return plan.getRootBlock().getTargetListManager().getUnEvaluatedTargets();
+  }
+
+  public static EvalNode getRootSelection(String query) {
+    Expr block = analyzer.parse(query);
+    LogicalPlan plan = planner.createPlan(block);
+    return plan.getRootBlock().getSelectionNode().getQual();
+  }
+
   @Test
   public final void testFunctionEval() throws Exception {    
     Tuple tuple = new VTuple(3);
@@ -133,41 +150,29 @@ public class TestEvalTree {
           DatumFactory.createInt4(500),
           DatumFactory.createInt4(30)});
 
-    QueryBlock block;
     EvalNode expr;
 
     Schema peopleSchema = cat.getTableDesc("people").getMeta().getSchema();
-    block = (QueryBlock) analyzer.parse(QUERIES[0]).getParseTree();
     EvalContext evalCtx;
-    expr = block.getWhereCondition();
+    expr = getRootSelection(QUERIES[0]);
     evalCtx = expr.newContext();
     expr.eval(evalCtx, peopleSchema, tuple);
     assertEquals(true, expr.terminate(evalCtx).asBool());
 
-    block = (QueryBlock) analyzer.parse(QUERIES[1]).getParseTree();
-    expr = block.getWhereCondition();
+    expr = getRootSelection(QUERIES[1]);
     evalCtx = expr.newContext();
     expr.eval(evalCtx, peopleSchema, tuple);
     assertEquals(15000, expr.terminate(evalCtx).asInt4());
     assertCloneEqual(expr);
 
-    block = (QueryBlock) analyzer.parse(QUERIES[2]).getParseTree();
-    expr = block.getWhereCondition();
-    evalCtx = expr.newContext();
-    expr.eval(evalCtx, peopleSchema, tuple);
-    assertEquals(15050, expr.terminate(evalCtx).asInt4());
-    assertCloneEqual(expr);
-    
-    block = (QueryBlock) analyzer.parse(QUERIES[2]).getParseTree();
-    expr = block.getWhereCondition();
+    expr = getRootSelection(QUERIES[2]);
     evalCtx = expr.newContext();
     expr.eval(evalCtx, peopleSchema, tuple);
     assertEquals(15050, expr.terminate(evalCtx).asInt4());
     assertCloneEqual(expr);
     
     // Aggregation function test
-    block = (QueryBlock) analyzer.parse(QUERIES[4]).getParseTree();
-    expr = block.getTargetList()[0].getEvalTree();
+    expr = getRawTargets(QUERIES[4])[0].getEvalTree();
     evalCtx = expr.newContext();
     
     final int tuplenum = 10;
@@ -372,7 +377,6 @@ public class TestEvalTree {
     assertFalse(expr.terminate(evalCtx).asBool());
 
     BinaryEval plus = new BinaryEval(Type.PLUS, e1, e2);
-    evalCtx = expr.newContext();
     expr = new BinaryEval(Type.LTH, e1, plus);
     evalCtx = expr.newContext();
     expr.eval(evalCtx, null, null);
@@ -598,10 +602,8 @@ public class TestEvalTree {
     assertTrue(not.terminate(evalCtx).asBool());
     
     // Evaluation Test
-    QueryBlock block;
     Schema peopleSchema = cat.getTableDesc("people").getMeta().getSchema();
-    block = (QueryBlock) analyzer.parse(NOT[0]).getParseTree();
-    expr = block.getWhereCondition();
+    expr = getRootSelection(NOT[0]);
     evalCtx = expr.newContext();
     expr.eval(evalCtx, peopleSchema, tuples[0]);
     assertTrue(expr.terminate(evalCtx).asBool());
@@ -619,12 +621,10 @@ public class TestEvalTree {
   
   @Test
   public final void testLike() {
-    QueryBlock block;
     EvalNode expr;
 
     Schema peopleSchema = cat.getTableDesc("people").getMeta().getSchema();
-    block = (QueryBlock) analyzer.parse(LIKE[0]).getParseTree();
-    expr = block.getWhereCondition();
+    expr = getRootSelection(LIKE[0]);
     EvalContext evalCtx = expr.newContext();
     expr.eval(evalCtx, peopleSchema, tuples[0]);
     assertTrue(expr.terminate(evalCtx).asBool());
@@ -634,8 +634,7 @@ public class TestEvalTree {
     assertTrue(expr.terminate(evalCtx).asBool());
     
     // prefix
-    block = (QueryBlock) analyzer.parse(LIKE[1]).getParseTree();
-    expr = block.getWhereCondition();
+    expr = getRootSelection(LIKE[1]);
     evalCtx = expr.newContext();
     expr.eval(evalCtx, peopleSchema, tuples[0]);
     assertTrue(expr.terminate(evalCtx).asBool());
@@ -645,8 +644,7 @@ public class TestEvalTree {
     assertFalse(expr.terminate(evalCtx).asBool());
 
     // Not Test
-    block = (QueryBlock) analyzer.parse(LIKE[2]).getParseTree();
-    expr = block.getWhereCondition();
+    expr = getRootSelection(LIKE[2]);
     evalCtx = expr.newContext();
     expr.eval(evalCtx, peopleSchema, tuples[0]);
     assertFalse(expr.terminate(evalCtx).asBool());
@@ -663,16 +661,13 @@ public class TestEvalTree {
 
   @Test
   public void testIsNullEval() {
-    QueryBlock block;
     EvalNode expr;
 
-    block = (QueryBlock) analyzer.parse(IS_NULL[0]).getParseTree();
-    expr = block.getWhereCondition();
+    expr = getRootSelection(IS_NULL[0]);
 
     assertIsNull(expr);
 
-    block = (QueryBlock) analyzer.parse(IS_NULL[1]).getParseTree();
-    expr = block.getWhereCondition();
+    expr = getRootSelection(IS_NULL[1]);
 
     IsNullEval nullEval = (IsNullEval) expr;
     assertTrue(nullEval.isNot());

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/eval/TestEvalTreeUtil.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/eval/TestEvalTreeUtil.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/eval/TestEvalTreeUtil.java
index 033578b..3638352 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/eval/TestEvalTreeUtil.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/eval/TestEvalTreeUtil.java
@@ -20,10 +20,8 @@ package org.apache.tajo.engine.eval;
 
 import com.google.common.collect.Sets;
 import org.apache.hadoop.fs.Path;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
 import org.apache.tajo.TajoTestingCluster;
+import org.apache.tajo.algebra.Expr;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.FunctionType;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
@@ -31,12 +29,17 @@ import org.apache.tajo.common.TajoDataTypes;
 import org.apache.tajo.datum.DatumFactory;
 import org.apache.tajo.engine.eval.EvalNode.Type;
 import org.apache.tajo.engine.eval.TestEvalTree.TestSum;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
-import org.apache.tajo.engine.parser.QueryBlock;
-import org.apache.tajo.engine.parser.QueryBlock.Target;
 import org.apache.tajo.engine.planner.LogicalPlanner;
+import org.apache.tajo.engine.planner.Target;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
+import org.apache.tajo.engine.planner.LogicalPlan;
+import org.apache.tajo.engine.planner.logical.EvalExprNode;
+import org.apache.tajo.engine.planner.logical.ExprType;
 import org.apache.tajo.exception.InternalException;
 import org.apache.tajo.master.TajoMaster;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
 import java.util.Collection;
 import java.util.List;
@@ -51,7 +54,7 @@ public class TestEvalTreeUtil {
   static EvalNode expr1;
   static EvalNode expr2;
   static EvalNode expr3;
-  static QueryAnalyzer analyzer;
+  static SQLAnalyzer analyzer;
   static LogicalPlanner planner;
 
 
@@ -79,19 +82,12 @@ public class TestEvalTreeUtil {
         CatalogUtil.newDataTypesWithoutLen(TajoDataTypes.Type.INT4, TajoDataTypes.Type.INT4));
     catalog.registerFunction(funcMeta);
 
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
-    
-    QueryBlock block;
 
-    block = (QueryBlock) analyzer.parse(TestEvalTree.QUERIES[0]).getParseTree();
-    expr1 = block.getWhereCondition();
-
-    block = (QueryBlock) analyzer.parse(TestEvalTree.QUERIES[1]).getParseTree();
-    expr2 = block.getWhereCondition();
-    
-    block = (QueryBlock) analyzer.parse(TestEvalTree.QUERIES[2]).getParseTree();
-    expr3 = block.getWhereCondition();
+    expr1 = getRootSelection(TestEvalTree.QUERIES[0]);
+    expr2 = getRootSelection(TestEvalTree.QUERIES[1]);
+    expr3 = getRootSelection(TestEvalTree.QUERIES[2]);
   }
 
   @AfterClass
@@ -99,6 +95,22 @@ public class TestEvalTreeUtil {
     util.shutdownCatalogCluster();
   }
 
+  public static Target [] getRawTargets(String query) {
+    Expr expr = analyzer.parse(query);
+    LogicalPlan plan = planner.createPlan(expr);
+    if (plan.getRootBlock().getRoot().getType() == ExprType.EXPRS) {
+      return ((EvalExprNode)plan.getRootBlock().getRoot()).getExprs();
+    } else {
+      return plan.getRootBlock().getTargetListManager().getUnEvaluatedTargets();
+    }
+  }
+
+  public static EvalNode getRootSelection(String query) {
+    Expr block = analyzer.parse(query);
+    LogicalPlan plan = planner.createPlan(block);
+    return plan.getRootBlock().getSelectionNode().getQual();
+  }
+
   @Test
   public final void testChangeColumnRef() throws CloneNotSupportedException {
     EvalNode copy = (EvalNode)expr1.clone();
@@ -151,9 +163,8 @@ public class TestEvalTreeUtil {
   
   @Test
   public final void testGetSchemaFromTargets() throws InternalException {
-    QueryBlock block = (QueryBlock) analyzer.parse(QUERIES[0]).getParseTree();
-    Schema schema = 
-        EvalTreeUtil.getSchemaByTargets(null, block.getTargetList());
+    Target [] targets = getRawTargets(QUERIES[0]);
+    Schema schema = EvalTreeUtil.getSchemaByTargets(null, targets);
     Column col1 = schema.getColumn(0);
     Column col2 = schema.getColumn(1);
     assertEquals("plus", col1.getColumnName());
@@ -164,9 +175,9 @@ public class TestEvalTreeUtil {
   
   @Test
   public final void testGetContainExprs() throws CloneNotSupportedException {
-    QueryBlock block = (QueryBlock) analyzer.parse(QUERIES[1]).getParseTree();
-    Target [] targets = block.getTargetList();
-    
+    Expr expr = analyzer.parse(QUERIES[1]);
+    LogicalPlan plan = planner.createPlan(expr);
+    Target [] targets = plan.getRootBlock().getTargetListManager().getUnEvaluatedTargets();
     Column col1 = new Column("people.score", TajoDataTypes.Type.INT4);
     Collection<EvalNode> exprs =
         EvalTreeUtil.getContainExpr(targets[0].getEvalTree(), col1);
@@ -186,8 +197,7 @@ public class TestEvalTreeUtil {
   @Test
   public final void testGetCNF() {
     // "select score from people where score < 10 and 4 < score "
-    QueryBlock block = (QueryBlock) analyzer.parse(QUERIES[5]).getParseTree();
-    EvalNode node = block.getWhereCondition();
+    EvalNode node = getRootSelection(QUERIES[5]);
     EvalNode [] cnf = EvalTreeUtil.getConjNormalForm(node);
     
     Column col1 = new Column("people.score", TajoDataTypes.Type.INT4);
@@ -214,8 +224,7 @@ public class TestEvalTreeUtil {
   @Test
   public final void testTransformCNF2Singleton() {
     // "select score from people where score < 10 and 4 < score "
-    QueryBlock block = (QueryBlock) analyzer.parse(QUERIES[6]).getParseTree();
-    EvalNode node = block.getWhereCondition();
+    EvalNode node = getRootSelection(QUERIES[6]);
     EvalNode [] cnf1 = EvalTreeUtil.getConjNormalForm(node);
     assertEquals(3, cnf1.length);
     
@@ -229,8 +238,7 @@ public class TestEvalTreeUtil {
   
   @Test
   public final void testSimplify() {
-    QueryBlock block = (QueryBlock) analyzer.parse(QUERIES[0]).getParseTree();
-    Target [] targets = block.getTargetList();
+    Target [] targets = getRawTargets(QUERIES[0]);
     EvalNode node = AlgebraicUtil.simplify(targets[0].getEvalTree());
     EvalContext nodeCtx = node.newContext();
     assertEquals(Type.CONST, node.getType());
@@ -242,35 +250,30 @@ public class TestEvalTreeUtil {
     node.eval(nodeCtx, null, null);
     assertTrue(7.0d == node.terminate(nodeCtx).asFloat8());
 
-    block = (QueryBlock) analyzer.parse(QUERIES[1]).getParseTree();
-    targets = block.getTargetList();
+    Expr expr = analyzer.parse(QUERIES[1]);
+    LogicalPlan plan = planner.createPlan(expr);
+    targets = plan.getRootBlock().getTargetListManager().getUnEvaluatedTargets();
     Column col1 = new Column("people.score", TajoDataTypes.Type.INT4);
     Collection<EvalNode> exprs =
         EvalTreeUtil.getContainExpr(targets[0].getEvalTree(), col1);
     node = exprs.iterator().next();
-    System.out.println(AlgebraicUtil.simplify(node));
   }
   
   @Test
   public final void testConatainSingleVar() {
-    QueryBlock block = (QueryBlock) analyzer.parse(QUERIES[2]).getParseTree();
-    EvalNode node = block.getWhereCondition();
+    EvalNode node = getRootSelection(QUERIES[2]);
     assertEquals(true, AlgebraicUtil.containSingleVar(node));
-    
-    block = (QueryBlock) analyzer.parse(QUERIES[3]).getParseTree();
-    node = block.getWhereCondition();
+    node = getRootSelection(QUERIES[3]);
     assertEquals(true, AlgebraicUtil.containSingleVar(node));
   }
   
   @Test
   public final void testTranspose() {
-    QueryBlock block = (QueryBlock) analyzer.parse(QUERIES[2]).getParseTree();
-    EvalNode node = block.getWhereCondition();
-    assertEquals(true, AlgebraicUtil.containSingleVar(node));
+    //EvalNode node = getRootSelection(QUERIES[2]);
+    //assertEquals(true, AlgebraicUtil.containSingleVar(node));
     
     Column col1 = new Column("people.score", TajoDataTypes.Type.INT4);
-    block = (QueryBlock) analyzer.parse(QUERIES[3]).getParseTree();
-    node = block.getWhereCondition();    
+    EvalNode node = getRootSelection(QUERIES[3]);
     // we expect that score < 3
     EvalNode transposed = AlgebraicUtil.transpose(node, col1);
     assertEquals(Type.GTH, transposed.getType());
@@ -280,8 +283,7 @@ public class TestEvalTreeUtil {
     transposed.getRightExpr().eval(evalCtx, null, null);
     assertEquals(1, transposed.getRightExpr().terminate(evalCtx).asInt4());
 
-    block = (QueryBlock) analyzer.parse(QUERIES[4]).getParseTree();
-    node = block.getWhereCondition();    
+    node = getRootSelection(QUERIES[4]);
     // we expect that score < 3
     transposed = AlgebraicUtil.transpose(node, col1);
     assertEquals(Type.LTH, transposed.getType());
@@ -294,11 +296,10 @@ public class TestEvalTreeUtil {
 
   @Test
   public final void testFindDistinctAggFunctions() {
-
-    QueryBlock block = (QueryBlock) analyzer.parse(
-        "select sum(score) + max(age) from people").getParseTree();
+    String query = "select sum(score) + max(age) from people";
+    Target [] targets = getRawTargets(query);
     List<AggFuncCallEval> list = EvalTreeUtil.
-        findDistinctAggFunction(block.getTargetList()[0].getEvalTree());
+        findDistinctAggFunction(targets[0].getEvalTree());
     assertEquals(2, list.size());
     Set<String> result = Sets.newHashSet("max", "sum");
     for (AggFuncCallEval eval : list) {

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/parser/TestQueryAnalyzer.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/parser/TestQueryAnalyzer.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/parser/TestQueryAnalyzer.java
deleted file mode 100644
index 04eab36..0000000
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/parser/TestQueryAnalyzer.java
+++ /dev/null
@@ -1,653 +0,0 @@
-/**
- * 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.engine.parser;
-
-import org.apache.hadoop.fs.Path;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.apache.tajo.TajoTestingCluster;
-import org.apache.tajo.benchmark.TPCH;
-import org.apache.tajo.catalog.*;
-import org.apache.tajo.catalog.proto.CatalogProtos.FunctionType;
-import org.apache.tajo.catalog.proto.CatalogProtos.IndexMethod;
-import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
-import org.apache.tajo.common.TajoDataTypes;
-import org.apache.tajo.datum.DatumFactory;
-import org.apache.tajo.engine.eval.ConstEval;
-import org.apache.tajo.engine.eval.EvalNode;
-import org.apache.tajo.engine.eval.EvalNode.Type;
-import org.apache.tajo.engine.eval.TestEvalTree.TestSum;
-import org.apache.tajo.engine.parser.QueryBlock.GroupElement;
-import org.apache.tajo.engine.parser.QueryBlock.GroupType;
-import org.apache.tajo.engine.parser.QueryBlock.JoinClause;
-import org.apache.tajo.engine.planner.JoinType;
-import org.apache.tajo.engine.query.exception.InvalidQueryException;
-
-import java.util.Iterator;
-import java.util.List;
-
-import static org.junit.Assert.*;
-
-/**
- * This unit test examines the correctness of QueryAnalyzer that analyzes 
- * an abstract syntax tree built from Antlr and generates a QueryBlock instance.
- * 
- * @see QueryAnalyzer
- * @see org.apache.tajo.engine.parser.QueryBlock
- */
-public class TestQueryAnalyzer {
-  private static TajoTestingCluster util;
-  private static CatalogService cat = null;
-  private static Schema schema1 = null;
-  private static QueryAnalyzer analyzer = null;
-  
-  @BeforeClass
-  public static void setUp() throws Exception {
-    util = new TajoTestingCluster();
-    util.startCatalogCluster();
-    cat = util.getMiniCatalogCluster().getCatalog();
-    
-    schema1 = new Schema();
-    schema1.addColumn("id", TajoDataTypes.Type.INT4);
-    schema1.addColumn("name", TajoDataTypes.Type.TEXT);
-    schema1.addColumn("score", TajoDataTypes.Type.INT4);
-    schema1.addColumn("age", TajoDataTypes.Type.INT4);
-    
-    Schema schema2 = new Schema();
-    schema2.addColumn("id", TajoDataTypes.Type.INT4);
-    schema2.addColumn("people_id", TajoDataTypes.Type.INT4);
-    schema2.addColumn("dept", TajoDataTypes.Type.TEXT);
-    schema2.addColumn("year", TajoDataTypes.Type.INT4);
-    
-    Schema schema3 = new Schema();
-    schema3.addColumn("id", TajoDataTypes.Type.INT4);
-    schema3.addColumn("people_id", TajoDataTypes.Type.INT4);
-    schema3.addColumn("class", TajoDataTypes.Type.TEXT);
-    schema3.addColumn("branch_name", TajoDataTypes.Type.TEXT);
-
-    Schema schema4 = new Schema();
-    schema4.addColumn("char_col", TajoDataTypes.Type.CHAR);
-    schema4.addColumn("short_col", TajoDataTypes.Type.INT2);
-    schema4.addColumn("int_col", TajoDataTypes.Type.INT4);
-    schema4.addColumn("long_col", TajoDataTypes.Type.INT8);
-    schema4.addColumn("float_col", TajoDataTypes.Type.FLOAT4);
-    schema4.addColumn("double_col", TajoDataTypes.Type.FLOAT8);
-    schema4.addColumn("string_col", TajoDataTypes.Type.TEXT);
-
-    TableMeta meta = CatalogUtil.newTableMeta(schema1, StoreType.CSV);
-    TableDesc people = new TableDescImpl("people", meta, new Path("file:///"));
-    cat.addTable(people);
-    
-    TableDesc student = CatalogUtil.newTableDesc("student", schema2, StoreType.CSV,
-        new Options(),
-        new Path("file:///"));
-    cat.addTable(student);
-    
-    TableDesc branch = CatalogUtil.newTableDesc("branch", schema3, StoreType.CSV,
-        new Options(),
-        new Path("file:///"));
-    cat.addTable(branch);
-
-    TableDesc allType = CatalogUtil.newTableDesc("alltype", schema4, StoreType.CSV,
-        new Options(),
-        new Path("file:///"));
-    cat.addTable(allType);
-
-    TPCH tpch = new TPCH();
-    tpch.loadSchemas();
-    Schema lineitemSchema = tpch.getSchema("lineitem");
-    Schema partSchema = tpch.getSchema("part");
-    TableDesc lineitem = CatalogUtil.newTableDesc("lineitem", lineitemSchema, StoreType.CSV,
-        new Options(),
-        new Path("file:///"));
-    TableDesc part = CatalogUtil.newTableDesc("part", partSchema, StoreType.CSV,
-        new Options(),
-        new Path("file:///"));
-    cat.addTable(lineitem);
-    cat.addTable(part);
-    
-    FunctionDesc funcMeta = new FunctionDesc("sumtest", TestSum.class, FunctionType.GENERAL,
-        CatalogUtil.newDataTypesWithoutLen(TajoDataTypes.Type.INT4),
-        CatalogUtil.newDataTypesWithoutLen(TajoDataTypes.Type.INT4));
-
-    cat.registerFunction(funcMeta);
-    
-    analyzer = new QueryAnalyzer(cat);
-  }
-
-  @AfterClass
-  public static void tearDown() throws Exception {
-    util.shutdownCatalogCluster();
-  }
-
-  private String[] QUERIES = { 
-      "select id, name, score, age from people", // 0
-      "select name, score, age from people where score > 30", // 1
-      "select name, score, age from people where 3 + 5 * 3", // 2
-      "select age, sumtest(score) as total from people group by age having sumtest(score) > 30", // 3
-      "select p.id, s.id, score, dept from people as p, student as s where p.id = s.id", // 4
-      "select name, score from people order by score asc, age desc null first", // 5
-      // only expr
-      "select 7 + 8", // 6
-      // limit test
-      "select id, name, score, age from people limit 3" // 7
-
-  };
- 
-  @Test
-  public final void testSelectStatement() {
-    QueryBlock block = (QueryBlock) analyzer.parse(QUERIES[0]).getParseTree();
-    assertEquals(1, block.getFromTables().length);
-    assertEquals("people", block.getFromTables()[0].getTableName());
-  }
-
-  @Test
-  public final void testOnlyExpr() {
-    QueryBlock block = (QueryBlock) analyzer.parse(QUERIES[6]).getParseTree();
-    EvalNode node = block.getTargetList()[0].getEvalTree();
-    assertEquals(Type.PLUS, node.getType());
-  }
-
-  @Test
-  public void testLimit() {
-    QueryBlock queryBlock =
-        (QueryBlock) analyzer.parse(QUERIES[7]).getParseTree();
-    assertEquals(3, queryBlock.getLimitClause().getLimitRow());
-  }
-
-  private String[] GROUP_BY = { 
-      "select age, sumtest(score) as total from people group by age having sumtest(score) > 30", // 0
-      "select name, age, sumtest(score) total from people group by cube (name,age)", // 1
-      "select name, age, sumtest(score) total from people group by rollup (name,age)", // 2
-      "select id, name, age, sumtest(score) total from people group by id, cube (name), rollup (age)", // 3
-      "select id, name, age, sumtest(score) total from people group by ()", // 4
-  };
-  
-  @Test
-  public final void testGroupByStatement() {
-    ParseTree tree = analyzer.parse(GROUP_BY[0]).getParseTree();
-    assertEquals(StatementType.SELECT, tree.getType());
-    QueryBlock block = (QueryBlock) tree;
-    assertTrue(block.hasGroupbyClause());
-    assertEquals(1, block.getGroupByClause().getGroupSet().size());
-    assertEquals("age", block.getGroupByClause().getGroupSet().get(0).getColumns()[0].getColumnName());
-    assertTrue(block.hasHavingCond());
-    assertEquals(Type.GTH, block.getHavingCond().getType());
-  }
-  
-  @Test
-  public final void testCubeByStatement() {
-    ParseTree tree = analyzer.parse(GROUP_BY[1]).getParseTree();
-    assertEquals(StatementType.SELECT, tree.getType());
-    QueryBlock block = (QueryBlock) tree;
-    assertTrue(block.hasGroupbyClause());
-    assertEquals(1, block.getGroupByClause().getGroupSet().size());
-    assertEquals(GroupType.CUBE, block.getGroupByClause().
-        getGroupSet().get(0).getType());
-    List<GroupElement> groups = block.getGroupByClause().getGroupSet();
-    assertEquals("people.name", groups.get(0).getColumns()[0].getQualifiedName());
-    assertEquals("people.age", groups.get(0).getColumns()[1].getQualifiedName());
-  }
-  
-  @Test
-  public final void testRollUpStatement() {
-    ParseTree tree = analyzer.parse(GROUP_BY[2]).getParseTree();
-    assertEquals(StatementType.SELECT, tree.getType());
-    QueryBlock block = (QueryBlock) tree;
-    assertTrue(block.hasGroupbyClause());
-    assertEquals(1, block.getGroupByClause().getGroupSet().size());
-    assertEquals(GroupType.ROLLUP, block.getGroupByClause().
-        getGroupSet().get(0).getType());
-    List<GroupElement> groups = block.getGroupByClause().getGroupSet();
-    assertEquals("people.name", groups.get(0).getColumns()[0].getQualifiedName());
-    assertEquals("people.age", groups.get(0).getColumns()[1].getQualifiedName());
-  }
-  
-  @Test
-  public final void testMixedGroupByStatement() {
-    ParseTree tree = analyzer.parse(GROUP_BY[3]).getParseTree();
-    assertEquals(StatementType.SELECT, tree.getType());
-    QueryBlock block = (QueryBlock) tree;
-    assertTrue(block.hasGroupbyClause());
-    assertEquals(3, block.getGroupByClause().getGroupSet().size());
-    Iterator<GroupElement> it = block.getGroupByClause().getGroupSet().iterator();
-    GroupElement group = it.next();
-    assertEquals(GroupType.CUBE, group.getType());    
-    assertEquals("people.name", group.getColumns()[0].getQualifiedName());
-    group = it.next();
-    assertEquals(GroupType.ROLLUP, group.getType());    
-    assertEquals("people.age", group.getColumns()[0].getQualifiedName());
-    group = it.next();
-    assertEquals(GroupType.GROUPBY, group.getType());
-    assertEquals("people.id", group.getColumns()[0].getQualifiedName());
-  }
-  
-  @Test
-  public final void testEmptyGroupSetStatement() {
-    ParseTree tree = analyzer.parse(GROUP_BY[4]).getParseTree();
-    assertEquals(StatementType.SELECT, tree.getType());
-    QueryBlock block = (QueryBlock) tree;
-    assertTrue(block.hasGroupbyClause());
-    assertTrue(block.getGroupByClause().isEmptyGroupSet());
-  }
-  
-  @Test
-  public final void testSelectStatementWithAlias() {
-    QueryBlock block = (QueryBlock) analyzer.parse(QUERIES[4]).getParseTree();
-    assertEquals(2, block.getFromTables().length);
-    assertEquals("people", block.getFromTables()[0].getTableName());
-    assertEquals("student", block.getFromTables()[1].getTableName());
-  }
-  
-  @Test
-  public final void testOrderByClause() {
-    QueryBlock block = (QueryBlock) analyzer.parse(QUERIES[5]).getParseTree();
-    testOrderByCluse(block);
-  }
-  
-  private static void testOrderByCluse(QueryBlock block) {
-    assertEquals(2, block.getSortKeys().length);
-    assertEquals("people.score", block.getSortKeys()[0].getSortKey().getQualifiedName());
-    assertEquals(true, block.getSortKeys()[0].isAscending());
-    assertEquals(false, block.getSortKeys()[0].isNullFirst());
-    assertEquals("people.age", block.getSortKeys()[1].getSortKey().getQualifiedName());
-    assertEquals(false, block.getSortKeys()[1].isAscending());
-    assertEquals(true, block.getSortKeys()[1].isNullFirst());
-  }
-
-  static final String [] createTableStmts = {
-    "create table table1 (name text, age int)", // 0
-    "create table table1 (name text, age int) using rcfile", // 1
-    "create table table1 (name text, age int) using rcfile with ('rcfile.buffer'=4096)", // 2
-    // create table test
-    "create table store1 as select name, score from people order by score asc, age desc null first",// 3
-    // create table test
-    "create table store1 (c1 text, c2 bigint) as select name, score from people order by score asc, age desc null first",// 4
-    // create table test
-    "create table store2 using rcfile with ('rcfile.buffer' = 4096) as select name, score from people order by score asc, age desc null first", // 5
-    // create table def
-    "create table table1 (name text, age int, earn float(10), score float(30)) using rcfile with ('rcfile.buffer' = 4096)", // 6
-    // create table def with location
-    "create external table table1 (name text, age int, earn bigint, score float) using csv with ('csv.delimiter'='|') location '/tmp/data'" // 7
-  };
-
-  @Test
-  public final void testCreateTable1() {
-    CreateTableStmt stmt = (CreateTableStmt) analyzer.parse(createTableStmts[0]).getParseTree();
-    assertEquals("table1", stmt.getTableName());
-    assertTrue(stmt.hasDefinition());
-
-    stmt = (CreateTableStmt) analyzer.parse(createTableStmts[1]).getParseTree();
-    assertEquals("table1", stmt.getTableName());
-    assertTrue(stmt.hasDefinition());
-    assertTrue(stmt.hasStoreType());
-    assertEquals(StoreType.RCFILE, stmt.getStoreType());
-
-    stmt = (CreateTableStmt) analyzer.parse(createTableStmts[2]).getParseTree();
-    assertEquals("table1", stmt.getTableName());
-    assertTrue(stmt.hasDefinition());
-    assertTrue(stmt.hasStoreType());
-    assertEquals(StoreType.RCFILE, stmt.getStoreType());
-    assertTrue(stmt.hasOptions());
-    assertEquals("4096", stmt.getOptions().get("rcfile.buffer"));
-  }
-  
-  @Test
-  public final void testCreateTableAsSelect() {
-    CreateTableStmt stmt = (CreateTableStmt) analyzer.parse(createTableStmts[3]).getParseTree();
-    assertEquals("store1", stmt.getTableName());
-    testOrderByCluse(stmt.getSelectStmt());
-
-    stmt = (CreateTableStmt) analyzer.parse(createTableStmts[4]).getParseTree();
-    assertEquals("store1", stmt.getTableName());
-    testOrderByCluse(stmt.getSelectStmt());
-    assertTrue(stmt.hasTableDef());
-
-    stmt = (CreateTableStmt) analyzer.parse(createTableStmts[5]).getParseTree();
-    assertEquals("store2", stmt.getTableName());
-    assertEquals(StoreType.RCFILE, stmt.getStoreType());
-    assertEquals("4096", stmt.getOptions().get("rcfile.buffer"));
-    testOrderByCluse(stmt.getSelectStmt());
-  }
-
-  @Test
-  public final void testCreateTableDef1() {
-    CreateTableStmt stmt = (CreateTableStmt) analyzer.parse(createTableStmts[6]).getParseTree();
-    assertEquals("table1", stmt.getTableName());
-    Schema def = stmt.getTableDef();
-    assertEquals("name", def.getColumn(0).getColumnName());
-    assertEquals(TajoDataTypes.Type.TEXT, def.getColumn(0).getDataType().getType());
-    assertEquals("age", def.getColumn(1).getColumnName());
-    assertEquals(TajoDataTypes.Type.INT4, def.getColumn(1).getDataType().getType());
-    assertEquals("earn", def.getColumn(2).getColumnName());
-    assertEquals(TajoDataTypes.Type.FLOAT4, def.getColumn(2).getDataType().getType()); // float(10)
-    assertEquals("score", def.getColumn(3).getColumnName());
-    assertEquals(TajoDataTypes.Type.FLOAT8, def.getColumn(3).getDataType().getType()); // float(30)
-    assertEquals(StoreType.RCFILE, stmt.getStoreType());
-    assertFalse(stmt.hasPath());
-    assertTrue(stmt.hasOptions());
-    assertEquals("4096", stmt.getOptions().get("rcfile.buffer"));
-  }
-  
-  @Test
-  public final void testCreateTableDef2() {
-    CreateTableStmt stmt = (CreateTableStmt) analyzer.parse(createTableStmts[7]).getParseTree();
-    assertEquals("table1", stmt.getTableName());
-    Schema def = stmt.getTableDef();
-    assertEquals("name", def.getColumn(0).getColumnName());
-    assertEquals(TajoDataTypes.Type.TEXT, def.getColumn(0).getDataType().getType());
-    assertEquals("age", def.getColumn(1).getColumnName());
-    assertEquals(TajoDataTypes.Type.INT4, def.getColumn(1).getDataType().getType());
-    assertEquals("earn", def.getColumn(2).getColumnName());
-    assertEquals(TajoDataTypes.Type.INT8, def.getColumn(2).getDataType().getType());
-    assertEquals("score", def.getColumn(3).getColumnName());
-    assertEquals(TajoDataTypes.Type.FLOAT8, def.getColumn(3).getDataType().getType()); // float
-    assertEquals(StoreType.CSV, stmt.getStoreType());    
-    assertEquals("/tmp/data", stmt.getPath().toString());
-    assertTrue(stmt.hasOptions());
-    assertEquals("|", stmt.getOptions().get("csv.delimiter"));
-  }
-
-  // create index
-  final static String createIndexStmt =
-      "create unique index score_idx on people using hash (score, age desc null first) with ('fillfactor' = 70)";
-  
-  @Test 
-  public final void testCreateIndex() {
-    CreateIndexStmt stmt = (CreateIndexStmt) analyzer.parse(createIndexStmt).getParseTree();
-    assertEquals("score_idx", stmt.getIndexName());
-    assertTrue(stmt.isUnique());
-    assertEquals("people", stmt.getTableName());
-    assertEquals(IndexMethod.HASH, stmt.getMethod());
-    
-    SortSpec [] sortKeys = stmt.getSortSpecs();
-    assertEquals(2, sortKeys.length);
-    assertEquals("score", sortKeys[0].getSortKey().getColumnName());
-    assertEquals(TajoDataTypes.Type.INT4, sortKeys[0].getSortKey().getDataType().getType());
-    assertEquals("age", sortKeys[1].getSortKey().getColumnName());
-    assertEquals(TajoDataTypes.Type.INT4, sortKeys[1].getSortKey().getDataType().getType());
-    assertEquals(false, sortKeys[1].isAscending());
-    assertEquals(true, sortKeys[1].isNullFirst());
-    
-    assertTrue(stmt.hasParams());
-    assertEquals("70", stmt.getParams().get("fillfactor"));
-  }
-  
-  private String [] INVALID_QUERIES = {
-      "select * from invalid", // 0 - when a given table does not exist
-      "select time, age from people", // 1 - when a given column does not exist
-      "select age from people group by age2" // 2 - when a grouping field does not eixst
-  };
-  @Test(expected = InvalidQueryException.class)
-  public final void testNoSuchTables()  {
-    analyzer.parse(INVALID_QUERIES[0]);
-  }
-  
-  @Test(expected = InvalidQueryException.class)
-  public final void testNoSuchFields()  {
-    analyzer.parse(INVALID_QUERIES[1]);
-  }
-  
-  @Test(expected = InvalidQueryException.class)
-  public final void testInvalidGroupFields() {
-    QueryBlock block = (QueryBlock) analyzer.parse(INVALID_QUERIES[2]).getParseTree();
-    assertEquals("age", block.getGroupByClause().getGroupSet().get(0).getColumns()[0].getQualifiedName());
-  }
-  
-  static String [] JOINS = {
-    "select p.id, name, branch_name from people as p natural join student natural join branch", // 0
-    "select name, dept from people as p inner join student as s on p.id = s.people_id", // 1
-    "select name, dept from people as p inner join student as s using (p.id)", // 2
-    "select p.id, name, branch_name from people as p cross join student cross join branch", // 3
-    "select p.id, dept from people as p left outer join student as s on p.id = s.people_id", // 4
-    "select p.id, dept from people as p right outer join student as s on p.id = s.people_id", // 5
-    "select p.id, dept from people as p join student as s on p.id = s.people_id", // 6
-    "select p.id, dept from people as p left join student as s on p.id = s.people_id", // 7
-    "select p.id, dept from people as p right join student as s on p.id= s.people_id" // 8
-  };
-  
-  @Test
-  /**
-   *           join
-   *          /    \
-   *       join    branch
-   *    /       \
-   * people  student
-   */
-  public final void testNaturalJoinClause() {
-    QueryBlock block = (QueryBlock) analyzer.parse(JOINS[0]).getParseTree();
-    JoinClause join = block.getJoinClause();
-    assertEquals(JoinType.INNER, join.getJoinType());
-    assertTrue(join.isNatural());
-    assertEquals("branch", join.getRight().getTableName());
-    assertTrue(join.hasLeftJoin());
-    assertEquals("people", join.getLeftJoin().getLeft().getTableName());
-    assertEquals("student", join.getLeftJoin().getRight().getTableName());
-  }
-  
-  @Test
-  /**
-   *       join
-   *    /       \
-   * people student
-   */
-  public final void testInnerJoinClause() {
-    QueryBlock block = (QueryBlock) analyzer.parse(JOINS[1]).getParseTree();
-    JoinClause join = block.getJoinClause();
-    assertEquals(JoinType.INNER, join.getJoinType());
-    assertEquals("people", join.getLeft().getTableName());
-    assertEquals("p", join.getLeft().getAlias());
-    assertEquals("student", join.getRight().getTableName());
-    assertEquals("s", join.getRight().getAlias());
-    assertTrue(join.hasJoinQual());
-    assertEquals(EvalNode.Type.EQUAL, join.getJoinQual().getType());
-
-    block = (QueryBlock) analyzer.parse(JOINS[2]).getParseTree();
-    join = block.getJoinClause();
-    assertEquals(JoinType.INNER, join.getJoinType());
-    assertEquals("people", join.getLeft().getTableName());
-    assertEquals("p", join.getLeft().getAlias());
-    assertEquals("student", join.getRight().getTableName());
-    assertEquals("s", join.getRight().getAlias());
-    assertTrue(join.hasJoinColumns());
-    assertEquals("id", join.getJoinColumns()[0].getColumnName());
-  }
-  
-  @Test
-  public final void testJoinClause() {
-    QueryBlock block = (QueryBlock) analyzer.parse(JOINS[6]).getParseTree();
-    JoinClause join = block.getJoinClause();
-    assertEquals(JoinType.INNER, join.getJoinType());
-    assertEquals("people", join.getLeft().getTableName());
-    assertEquals("p", join.getLeft().getAlias());
-    assertEquals("student", join.getRight().getTableName());
-    assertEquals("s", join.getRight().getAlias());
-    assertTrue(join.hasJoinQual());
-    assertEquals(EvalNode.Type.EQUAL, join.getJoinQual().getType());
-  }
-  
-  @Test
-  public final void testCrossJoinClause() {
-    QueryBlock block = (QueryBlock) analyzer.parse(JOINS[3]).getParseTree();
-    JoinClause join = block.getJoinClause();
-    assertEquals(JoinType.CROSS_JOIN, join.getJoinType());
-    assertEquals("branch", join.getRight().getTableName());
-    assertTrue(join.hasLeftJoin());
-    assertEquals("people", join.getLeftJoin().getLeft().getTableName());
-    assertEquals("student", join.getLeftJoin().getRight().getTableName());
-  }
-  
-  @Test
-  public final void testLeftOuterJoinClause() {
-    QueryBlock block = (QueryBlock) analyzer.parse(JOINS[4]).getParseTree();
-    JoinClause join = block.getJoinClause();
-    assertEquals(JoinType.LEFT_OUTER, join.getJoinType());
-    assertEquals("people", join.getLeft().getTableName());
-    assertEquals("p", join.getLeft().getAlias());
-    assertEquals("student", join.getRight().getTableName());
-    assertEquals("s", join.getRight().getAlias());
-    assertTrue(join.hasJoinQual());
-    assertEquals(EvalNode.Type.EQUAL, join.getJoinQual().getType());
-  }
-  
-  @Test
-  public final void testLeftJoinClause() {
-    QueryBlock block = (QueryBlock) analyzer.parse(JOINS[7]).getParseTree();
-    JoinClause join = block.getJoinClause();
-    assertEquals(JoinType.LEFT_OUTER, join.getJoinType());
-    assertEquals("people", join.getLeft().getTableName());
-    assertEquals("p", join.getLeft().getAlias());
-    assertEquals("student", join.getRight().getTableName());
-    assertEquals("s", join.getRight().getAlias());
-    assertTrue(join.hasJoinQual());
-    assertEquals(EvalNode.Type.EQUAL, join.getJoinQual().getType());
-  }
-  
-  @Test
-  public final void testRightOuterJoinClause() {
-    QueryBlock block = (QueryBlock) analyzer.parse(JOINS[5]).getParseTree();
-    JoinClause join = block.getJoinClause();
-    assertEquals(JoinType.RIGHT_OUTER, join.getJoinType());
-    assertEquals("people", join.getLeft().getTableName());
-    assertEquals("p", join.getLeft().getAlias());
-    assertEquals("student", join.getRight().getTableName());
-    assertEquals("s", join.getRight().getAlias());
-    assertTrue(join.hasJoinQual());
-    assertEquals(EvalNode.Type.EQUAL, join.getJoinQual().getType());
-  }
-  
-  @Test
-  public final void testRightJoinClause() {
-    QueryBlock block = (QueryBlock) analyzer.parse(JOINS[8]).getParseTree();
-    JoinClause join = block.getJoinClause();
-    assertEquals(JoinType.RIGHT_OUTER, join.getJoinType());
-    assertEquals("people", join.getLeft().getTableName());
-    assertEquals("p", join.getLeft().getAlias());
-    assertEquals("student", join.getRight().getTableName());
-    assertEquals("s", join.getRight().getAlias());
-    assertTrue(join.hasJoinQual());
-    assertEquals(EvalNode.Type.EQUAL, join.getJoinQual().getType());
-  }
-  
-  private final String [] setClauses = {
-      "select id, people_id from student union select id, people_id from branch",
-      "select id, people_id from student union select id, people_id from branch intersect select id, people_id from branch as b"
-  };
-  
-  @Test
-  public final void testUnionClause() {
-    ParseTree tree = analyzer.parse(setClauses[0]).getParseTree();
-    assertEquals(StatementType.UNION, tree.getType());
-    SetStmt union = (SetStmt) tree;
-    assertEquals(StatementType.SELECT, union.getLeftTree().getType());
-    assertEquals(StatementType.SELECT, union.getRightTree().getType());
-    QueryBlock left = (QueryBlock) union.getLeftTree();
-    assertEquals("student", left.getFromTables()[0].getTableName());
-    QueryBlock right = (QueryBlock) union.getRightTree();
-    assertEquals("branch", right.getFromTables()[0].getTableName());
-    
-    // multiple set statements
-    tree = analyzer.parse(setClauses[1]).getParseTree();
-    assertEquals(StatementType.UNION, tree.getType());
-    union = (SetStmt) tree;
-    assertEquals(StatementType.SELECT, union.getLeftTree().getType());
-    assertEquals(StatementType.INTERSECT, union.getRightTree().getType());
-    left = (QueryBlock) union.getLeftTree();
-    assertEquals("student", left.getFromTables()[0].getTableName());
-    SetStmt rightSet = (SetStmt) union.getRightTree();
-    left = (QueryBlock) rightSet.getLeftTree();
-    assertEquals("branch", left.getFromTables()[0].getTableName());
-    right = (QueryBlock) rightSet.getRightTree();
-    assertEquals("b", right.getFromTables()[0].getAlias());
-  }
-
-  static final String [] setQualifier = {
-      "select id, people_id from student",
-      "select distinct id, people_id from student",
-      "select all id, people_id from student",
-  };
-
-  @Test
-  public final void testSetQulaifier() {
-    ParseTree tree = analyzer.parse(setQualifier[0]).getParseTree();
-    assertEquals(StatementType.SELECT, tree.getType());
-    QueryBlock block = (QueryBlock) tree;
-    assertFalse(block.isDistinct());
-
-    tree = analyzer.parse(setQualifier[1]).getParseTree();
-    assertEquals(StatementType.SELECT, tree.getType());
-    block = (QueryBlock) tree;
-    assertTrue(block.isDistinct());
-
-    tree = analyzer.parse(setQualifier[2]).getParseTree();
-    assertEquals(StatementType.SELECT, tree.getType());
-    block = (QueryBlock) tree;
-    assertFalse(block.isDistinct());
-  }
-
-  @Test
-  public final void testTypeInferring() {
-    QueryBlock block = (QueryBlock) analyzer.parse("select 1 from alltype where char_col = 'a'").getParseTree();
-    assertEquals(TajoDataTypes.Type.CHAR, block.getWhereCondition().getRightExpr().getValueType()[0].getType());
-
-    block = (QueryBlock) analyzer.parse("select 1 from alltype where short_col = 1").getParseTree();
-    assertEquals(TajoDataTypes.Type.INT2, block.getWhereCondition().getRightExpr().getValueType()[0].getType());
-
-    block = (QueryBlock) analyzer.parse("select 1 from alltype where int_col = 1").getParseTree();
-    assertEquals(TajoDataTypes.Type.INT4, block.getWhereCondition().getRightExpr().getValueType()[0].getType());
-
-    block = (QueryBlock) analyzer.parse("select 1 from alltype where long_col = 1").getParseTree();
-    assertEquals(TajoDataTypes.Type.INT8, block.getWhereCondition().getRightExpr().getValueType()[0].getType());
-
-    block = (QueryBlock) analyzer.parse("select 1 from alltype where float_col = 1").getParseTree();
-    assertEquals(TajoDataTypes.Type.INT4, block.getWhereCondition().getRightExpr().getValueType()[0].getType());
-
-    block = (QueryBlock) analyzer.parse("select 1 from alltype where float_col = 1.0").getParseTree();
-    assertEquals(TajoDataTypes.Type.FLOAT4, block.getWhereCondition().getRightExpr().getValueType()[0].getType());
-
-    block = (QueryBlock) analyzer.parse("select 1 from alltype where int_col = 1.0").getParseTree();
-    assertEquals(TajoDataTypes.Type.FLOAT4, block.getWhereCondition().getRightExpr().getValueType()[0].getType());
-
-    block = (QueryBlock) analyzer.parse("select 1 from alltype where double_col = 1.0").getParseTree();
-    assertEquals(TajoDataTypes.Type.FLOAT4, block.getWhereCondition().getRightExpr().getValueType()[0].getType());
-
-    block = (QueryBlock) analyzer.parse("select 1 from alltype where string_col = 'a'").getParseTree();
-    assertEquals(TajoDataTypes.Type.TEXT, block.getWhereCondition().getRightExpr().getValueType()[0].getType());
-  }
-
-  @Test
-  public void testCaseWhen() {
-    ParseTree tree = analyzer.parse(
-        "select case when p_type like 'PROMO%' then l_extendedprice * (1 - l_discount) "+
-        "when p_type = 'MOCC' then l_extendedprice - 100 else 0 end as cond from lineitem, part").getParseTree();
-    assertEquals(StatementType.SELECT, tree.getType());
-    QueryBlock block = (QueryBlock) tree;
-    assertTrue(block.getTargetList()[0].hasAlias());
-    assertEquals("cond", block.getTargetList()[0].getAlias());
-    assertEquals(TajoDataTypes.Type.FLOAT8, block.getTargetList()[0].getEvalTree().getValueType()[0].getType());
-  }
-
-  @Test
-  public void testTarget() throws CloneNotSupportedException {
-    QueryBlock.Target t1 = new QueryBlock.Target(new ConstEval(DatumFactory.createInt4(5)), 0);
-    QueryBlock.Target t2 = (QueryBlock.Target) t1.clone();
-    assertEquals(t1,t2);
-  }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/parser/TestSQLAnalyzer.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/parser/TestSQLAnalyzer.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/parser/TestSQLAnalyzer.java
new file mode 100644
index 0000000..549cd1e
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/parser/TestSQLAnalyzer.java
@@ -0,0 +1,297 @@
+/**
+ * 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.engine.parser;
+
+import org.antlr.v4.runtime.ANTLRInputStream;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.apache.tajo.algebra.Expr;
+import org.apache.tajo.engine.parser.SQLParser.Boolean_value_expressionContext;
+import org.apache.tajo.engine.parser.SQLParser.SqlContext;
+import org.apache.tajo.util.FileUtil;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * This unit tests uses a number of query files located in tajo/tajo-core/tajo-core-backend/test/queries.
+ * So, you must set tajo/tajo-core/tajo-core-backend as the working directory.
+ */
+public class TestSQLAnalyzer {
+
+  public static Expr parseQuery(String sql) {
+    ANTLRInputStream input = new ANTLRInputStream(sql);
+    SQLLexer lexer = new SQLLexer(input);
+    CommonTokenStream tokens = new CommonTokenStream(lexer);
+    SQLParser parser = new SQLParser(tokens);
+    parser.setBuildParseTree(true);
+    SQLAnalyzer visitor = new SQLAnalyzer();
+    SqlContext context = parser.sql();
+    return visitor.visitSql(context);
+  }
+
+
+  @Test
+  public void testSelect1() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/select_1.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testSelect2() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/select_2.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testSelect3() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/select_3.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testSelect4() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/select_4.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testSelect5() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/select_5.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testInsert1() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/insert_1.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testInsert2() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/insert_2.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testGroupby1() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/groupby_1.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testJoin1() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/join_1.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testJoin2() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/join_2.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testJoin3() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/join_3.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testJoin4() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/join_4.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testJoin5() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/join_5.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testJoin6() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/join_6.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testJoin7() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/join_7.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testJoin8() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/join_8.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testJoin9() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/join_9.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testJoin10() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/join_10.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testJoin11() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/join_11.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testSet1() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/set_1.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testSet2() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/set_2.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testSet3() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/set_3.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testDropTable() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/drop_table.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testCreateTable1() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/create_table_1.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testCreateTable2() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/create_table_2.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testCreateTable3() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/create_table_3.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testCreateTable4() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/create_table_4.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testCreateTable5() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/create_table_5.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testCreateTable6() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/create_table_6.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testCreateTable7() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/create_table_7.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testCreateTable8() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/create_table_8.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testCreateTable9() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/create_table_9.sql"));
+    parseQuery(sql);
+  }
+
+  @Test
+  public void testCreateTable10() throws IOException {
+    String sql = FileUtil.readTextFile(new File("src/test/queries/create_table_10.sql"));
+    parseQuery(sql);
+  }
+
+  static String[] exprs = {
+      "1 + 2", // 0
+      "3 - 4", // 1
+      "5 * 6", // 2
+      "7 / 8", // 3
+      "10 % 2", // 4
+      "1 * 2 > 3 / 4", // 5
+      "1 * 2 < 3 / 4", // 6
+      "1 * 2 = 3 / 4", // 7
+      "1 * 2 != 3 / 4", // 8
+      "1 * 2 <> 3 / 4", // 9
+      "gender in ('male', 'female')", // 10
+      "gender not in ('male', 'female')", // 11
+      "score > 90 and age < 20", // 12
+      "score > 90 and age < 20 and name != 'hyunsik'", // 13
+      "score > 90 or age < 20", // 14
+      "score > 90 or age < 20 and name != 'hyunsik'", // 15
+      "((a+3 > 1) or 1=1) and (3 != (abc + 4) and type in (3,4))", // 16
+      "3", // 17
+      "1.2", // 18
+      "sum(age)", // 19
+      "now()", // 20
+      "not (90 > 100)", // 21
+      "type like '%top'", // 22
+      "type not like 'top%'", // 23
+      "col = 'value'", // 24
+      "col is null", // 25
+      "col is not null", // 26
+      "col = null", // 27
+      "col != null" // 38
+  };
+
+  public static Expr parseExpr(String sql) {
+    ANTLRInputStream input = new ANTLRInputStream(sql);
+    SQLLexer lexer = new SQLLexer(input);
+    CommonTokenStream tokens = new CommonTokenStream(lexer);
+    SQLParser parser = new SQLParser(tokens);
+    parser.setBuildParseTree(true);
+    SQLAnalyzer visitor = new SQLAnalyzer();
+    Boolean_value_expressionContext context = parser.boolean_value_expression();
+    return visitor.visitBoolean_value_expression(context);
+  }
+
+  @Test
+  public void testExprs() {
+    for (int i = 0; i < exprs.length; i++) {
+      parseExpr(exprs[i]);
+    }
+  }
+}


[08/13] TAJO-87: Integration of tajo algebra module and SQL parser

Posted by hy...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PlannerUtil.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PlannerUtil.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PlannerUtil.java
index 50b539a..dc647a9 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PlannerUtil.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PlannerUtil.java
@@ -30,17 +30,26 @@ import org.apache.tajo.catalog.SortSpec;
 import org.apache.tajo.common.TajoDataTypes.DataType;
 import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.engine.eval.*;
-import org.apache.tajo.engine.parser.QueryBlock;
 import org.apache.tajo.engine.planner.logical.*;
 import org.apache.tajo.engine.query.exception.InvalidQueryException;
 import org.apache.tajo.storage.TupleComparator;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
 public class PlannerUtil {
   private static final Log LOG = LogFactory.getLog(PlannerUtil.class);
+
+  public static boolean checkIfDDLPlan(LogicalNode node) {
+    LogicalNode baseNode = node;
+    if (node instanceof LogicalRootNode) {
+      baseNode = ((LogicalRootNode) node).getSubNode();
+    }
+
+    return baseNode.getType() == ExprType.CREATE_TABLE || baseNode.getType() == ExprType.DROP_TABLE;
+  }
   
   public static String [] getLineage(LogicalNode node) {
     LogicalNode [] scans =  PlannerUtil.findAllNodes(node, ExprType.SCAN);
@@ -48,11 +57,7 @@ public class PlannerUtil {
     ScanNode scan;
     for (int i = 0; i < scans.length; i++) {
       scan = (ScanNode) scans[i];
-      /*if (scan.hasAlias()) {
-        tableNames[i] = scan.getAlias();
-      } else {*/
-        tableNames[i] = scan.getTableId();
-      //}
+      tableNames[i] = scan.getTableId();
     }
     return tableNames;
   }
@@ -73,25 +78,37 @@ public class PlannerUtil {
   }
   
   /**
-   * Delete the child of a given parent operator.
-   * 
-   * @param parent Must be a unary logical operator.
-   * @return input parent node
+   * Delete the logical node from a plan.
+   *
+   * @param parent this node must be a parent node of one node to be removed.
+   * @param tobeRemoved this node must be a child node of the parent.
    */
-  public static LogicalNode deleteNode(LogicalNode parent) {
+  public static LogicalNode deleteNode(LogicalNode parent, LogicalNode tobeRemoved) {
+    Preconditions.checkArgument(tobeRemoved instanceof UnaryNode,
+        "ERROR: the logical node to be removed must be unary node.");
+
+    UnaryNode child = (UnaryNode) tobeRemoved;
+    LogicalNode grandChild = child.getSubNode();
     if (parent instanceof UnaryNode) {
-      UnaryNode unary = (UnaryNode) parent;
-      if (unary.getSubNode() instanceof UnaryNode) {
-        UnaryNode child = (UnaryNode) unary.getSubNode();
-        LogicalNode grandChild = child.getSubNode();
-        unary.setSubNode(grandChild);
+      UnaryNode unaryParent = (UnaryNode) parent;
+
+      Preconditions.checkArgument(unaryParent.getSubNode() == child,
+          "ERROR: both logical node must be parent and child nodes");
+      unaryParent.setSubNode(grandChild);
+
+    } else if (parent instanceof BinaryNode) {
+      BinaryNode binaryParent = (BinaryNode) parent;
+      if (binaryParent.getOuterNode().equals(child)) {
+        binaryParent.setOuter(grandChild);
+      } else if (binaryParent.getInnerNode().equals(child)) {
+        binaryParent.setInner(grandChild);
       } else {
-        throw new InvalidQueryException("Unexpected logical plan: " + parent);
+        throw new IllegalStateException("ERROR: both logical node must be parent and child nodes");
       }
     } else {
       throw new InvalidQueryException("Unexpected logical plan: " + parent);
     }    
-    return parent;
+    return child;
   }
   
   public static void replaceNode(LogicalNode plan, LogicalNode newNode, ExprType type) {
@@ -163,13 +180,13 @@ public class PlannerUtil {
       // cloning groupby node
       GroupbyNode child = (GroupbyNode) gp.clone();
 
-      List<QueryBlock.Target> newChildTargets = Lists.newArrayList();
-      QueryBlock.Target[] secondTargets = gp.getTargets();
-      QueryBlock.Target[] firstTargets = child.getTargets();
+      List<Target> newChildTargets = Lists.newArrayList();
+      Target[] secondTargets = gp.getTargets();
+      Target[] firstTargets = child.getTargets();
 
-      QueryBlock.Target second;
-      QueryBlock.Target first;
-      int firstTargetId = 0;
+      Target second;
+      Target first;
+      int targetId =  0;
       for (int i = 0; i < firstTargets.length; i++) {
         second = secondTargets[i];
         first = firstTargets[i];
@@ -180,14 +197,13 @@ public class PlannerUtil {
 
         if (firstFuncs.size() == 0) {
           newChildTargets.add(first);
-          firstTargetId++;
+          targetId++;
         } else {
           for (AggFuncCallEval func : firstFuncs) {
             func.setFirstPhase();
-            QueryBlock.Target
-                newTarget = new QueryBlock.Target(func, firstTargetId++);
-            newTarget.setAlias("column_"+ firstTargetId);
-
+            Target newTarget = new Target(func);
+            String targetName = "column_" + (targetId++);
+            newTarget.setAlias(targetName);
 
             AggFuncCallEval secondFunc = null;
             for (AggFuncCallEval sf : secondFuncs) {
@@ -197,19 +213,18 @@ public class PlannerUtil {
               }
             }
             if (func.getValueType().length > 1) { // hack for partial result
-              secondFunc.setArgs(new EvalNode[] {new FieldEval(
-                  new Column("column_"+firstTargetId, Type.ARRAY))});
+              secondFunc.setArgs(new EvalNode[] {new FieldEval(new Column(targetName, Type.ARRAY))});
             } else {
               secondFunc.setArgs(new EvalNode [] {new FieldEval(
-                  new Column("column_"+firstTargetId, newTarget.getEvalTree().getValueType()[0]))});
+                  new Column(targetName, newTarget.getEvalTree().getValueType()[0]))});
             }
             newChildTargets.add(newTarget);
           }
         }
       }
 
-      QueryBlock.Target[] targetArray = newChildTargets.toArray(new QueryBlock.Target[newChildTargets.size()]);
-      child.setTargetList(targetArray);
+      Target[] targetArray = newChildTargets.toArray(new Target[newChildTargets.size()]);
+      child.setTargets(targetArray);
       child.setOutSchema(PlannerUtil.targetToSchema(targetArray));
       // set the groupby chaining
       gp.setSubNode(child);
@@ -316,7 +331,69 @@ public class PlannerUtil {
     }
     return finder.getFoundNodes().get(0);
   }
-  
+
+  public static boolean canBeEvaluated(EvalNode eval, LogicalNode node) {
+    Set<Column> columnRefs = EvalTreeUtil.findDistinctRefColumns(eval);
+
+    if (node.getType() == ExprType.JOIN) {
+      JoinNode joinNode = (JoinNode) node;
+      Set<String> tableIds = Sets.newHashSet();
+      // getting distinct table references
+      for (Column col : columnRefs) {
+        if (!tableIds.contains(col.getTableName())) {
+          tableIds.add(col.getTableName());
+        }
+      }
+
+      // if the references only indicate two relation, the condition can be
+      // pushed into a join operator.
+      if (tableIds.size() != 2) {
+        return false;
+      }
+
+      String [] outer = getLineage(joinNode.getOuterNode());
+      String [] inner = getLineage(joinNode.getInnerNode());
+
+      Set<String> o = Sets.newHashSet(outer);
+      Set<String> i = Sets.newHashSet(inner);
+      if (outer == null || inner == null) {
+        throw new InvalidQueryException("ERROR: Unexpected logical plan");
+      }
+      Iterator<String> it = tableIds.iterator();
+      if (o.contains(it.next()) && i.contains(it.next())) {
+        return true;
+      }
+
+      it = tableIds.iterator();
+
+      return i.contains(it.next()) && o.contains(it.next());
+
+    } else {
+      if (node instanceof ScanNode) {
+        ScanNode scan = (ScanNode) node;
+
+        for (Column col : columnRefs) {
+          if (scan.getTableId().equals(col.getTableName())) {
+            Column found = node.getInSchema().getColumnByName(col.getColumnName());
+            if (found == null) {
+              return false;
+            }
+          } else {
+            return false;
+          }
+        }
+      } else {
+        for (Column col : columnRefs) {
+          if (!node.getInSchema().contains(col.getQualifiedName())) {
+            return false;
+          }
+        }
+      }
+
+      return true;
+    }
+  }
+
   private static class LogicalNodeFinder implements LogicalNodeVisitor {
     private List<LogicalNode> list = new ArrayList<LogicalNode>();
     private final ExprType [] tofind;
@@ -400,7 +477,7 @@ public class PlannerUtil {
       case PROJECTION:
         ProjectionNode projNode = (ProjectionNode) node;
 
-        for (QueryBlock.Target t : projNode.getTargets()) {
+        for (Target t : projNode.getTargets()) {
           temp = EvalTreeUtil.findDistinctRefColumns(t.getEvalTree());
           if (!temp.isEmpty()) {
             collected.addAll(temp);
@@ -421,7 +498,7 @@ public class PlannerUtil {
       case GROUP_BY:
         GroupbyNode groupByNode = (GroupbyNode)node;
         collected.addAll(Lists.newArrayList(groupByNode.getGroupingColumns()));
-        for (QueryBlock.Target t : groupByNode.getTargets()) {
+        for (Target t : groupByNode.getTargets()) {
           temp = EvalTreeUtil.findDistinctRefColumns(t.getEvalTree());
           if (!temp.isEmpty()) {
             collected.addAll(temp);
@@ -472,13 +549,27 @@ public class PlannerUtil {
     }
   }
 
-  public static QueryBlock.Target[] schemaToTargets(Schema schema) {
-    QueryBlock.Target[] targets = new QueryBlock.Target[schema.getColumnNum()];
+  /**
+   * fill targets with FieldEvals from a given schema
+   *
+   * @param schema to be transformed to targets
+   * @param targets to be filled
+   */
+  public static void schemaToTargets(Schema schema, Target [] targets) {
+    FieldEval eval;
+    for (int i = 0; i < schema.getColumnNum(); i++) {
+      eval = new FieldEval(schema.getColumn(i));
+      targets[i] = new Target(eval);
+    }
+  }
+
+  public static Target[] schemaToTargets(Schema schema) {
+    Target[] targets = new Target[schema.getColumnNum()];
 
     FieldEval eval;
     for (int i = 0; i < schema.getColumnNum(); i++) {
       eval = new FieldEval(schema.getColumn(i));
-      targets[i] = new QueryBlock.Target(eval, i);
+      targets[i] = new Target(eval);
     }
     return targets;
   }
@@ -504,8 +595,8 @@ public class PlannerUtil {
 
   /**
    * is it join qual or not?
-   * TODO - this method does not support the self join (NTA-740)
-   * @param qual
+   *
+   * @param qual  The condition to be checked
    * @return true if two operands refers to columns and the operator is comparison,
    */
   public static boolean isJoinQual(EvalNode qual) {
@@ -585,9 +676,9 @@ public class PlannerUtil {
     }
   }
 
-  public static Schema targetToSchema(QueryBlock.Target[] targets) {
+  public static Schema targetToSchema(Target[] targets) {
     Schema schema = new Schema();
-    for(QueryBlock.Target t : targets) {
+    for(Target t : targets) {
       DataType type;
       if (t.getEvalTree().getValueType().length > 1) {
         type = CatalogUtil.newDataTypeWithoutLen(Type.ARRAY);
@@ -606,11 +697,28 @@ public class PlannerUtil {
     return schema;
   }
 
-  public static EvalNode [] columnsToEvals(Column [] columns) {
-    EvalNode [] exprs = new EvalNode[columns.length];
-    for (int i = 0; i < columns.length; i++) {
-      exprs[i] = new FieldEval(columns[i]);
+  /**
+   * It removes all table names from FieldEvals in targets
+   *
+   * @param sourceTargets The targets to be stripped
+   * @return The stripped targets
+   */
+  public static Target [] stripTarget(Target [] sourceTargets) {
+    Target [] copy = new Target[sourceTargets.length];
+    for(int i = 0; i < sourceTargets.length; i++) {
+      try {
+        copy[i] = (Target) sourceTargets[i].clone();
+      } catch (CloneNotSupportedException e) {
+        throw new InternalError(e.getMessage());
+      }
+      if (copy[i].getEvalTree().getType() == EvalNode.Type.FIELD) {
+        FieldEval fieldEval = (FieldEval) copy[i].getEvalTree();
+        if (fieldEval.getColumnRef().isQualified()) {
+          fieldEval.getColumnRef().setName(fieldEval.getColumnName());
+        }
+      }
     }
-    return exprs;
+
+    return copy;
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PlanningContext.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PlanningContext.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PlanningContext.java
deleted file mode 100644
index 595f41a..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PlanningContext.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/**
- * 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.engine.planner;
-
-import org.antlr.runtime.tree.Tree;
-import org.apache.tajo.engine.parser.ParseTree;
-
-public interface PlanningContext {
-  String getRawQuery();
-
-  Tree getAST();
-
-  ParseTree getParseTree();
-
-  //TableMap getTableMap();
-
-  String getGeneratedColumnName();
-
-  String getGeneratedColumnName(String prefix);
-
-  String getExplicitOutputTable();
-
-  boolean hasExplicitOutputTable();
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PlanningContextImpl.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PlanningContextImpl.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PlanningContextImpl.java
deleted file mode 100644
index 34ea73a..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PlanningContextImpl.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/**
- * 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.engine.planner;
-
-import org.antlr.runtime.tree.Tree;
-import org.apache.tajo.engine.parser.ParseTree;
-
-public class PlanningContextImpl implements PlanningContext  {
-  private String rawQuery;
-  // private TableMap tableMap = new TableMap();
-  private ParseTree parseTree;
-  private Tree ast;
-  private String outputTableName;
-
-  private static final String DEFAULT_TABLE_GEN_PREFIX = "table";
-  private static final String DEFAULT_COLUMN_GEN_PREFIX = "column";
-  private static final String SEPARATOR = "_";
-
-  private int generatedTableId = 1;
-  private int generatedColumnId = 1;
-
-
-  public PlanningContextImpl(String rawQuery) {
-    this.rawQuery = rawQuery;
-  }
-
-  @Override
-  public String getRawQuery() {
-    return rawQuery;
-  }
-
-  @Override
-  public Tree getAST() {
-    return ast;
-  }
-
-  public void setAST(Tree ast) {
-    this.ast = ast;
-  }
-
-  @Override
-  public ParseTree getParseTree() {
-    return parseTree;
-  }
-
-//  @Override
-//  public TableMap getTableMap() {
-//    return tableMap;
-//  }
-
-  @Override
-  public String getGeneratedColumnName() {
-    return DEFAULT_COLUMN_GEN_PREFIX + SEPARATOR + (generatedColumnId++);
-  }
-
-  @Override
-  public synchronized String getGeneratedColumnName(String prefix) {
-    return prefix + SEPARATOR + (generatedColumnId++);
-  }
-
-  @Override
-  public String getExplicitOutputTable() {
-    return outputTableName;
-  }
-
-  @Override
-  public boolean hasExplicitOutputTable() {
-    return outputTableName != null;
-  }
-
-  public void setOutputTableName(String outputTableName) {
-    this.outputTableName = outputTableName;
-  }
-
-  public void setParseTree(ParseTree parseTree) {
-    this.parseTree = parseTree;
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/Projector.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/Projector.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/Projector.java
index da1b43c..543bbd7 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/Projector.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/Projector.java
@@ -22,12 +22,10 @@ import org.apache.tajo.catalog.Column;
 import org.apache.tajo.catalog.Schema;
 import org.apache.tajo.engine.eval.EvalContext;
 import org.apache.tajo.engine.eval.EvalNode;
-import org.apache.tajo.engine.parser.QueryBlock.Target;
 import org.apache.tajo.storage.Tuple;
 
 public class Projector {
   private final Schema inSchema;
-  private final Schema outSchema;
 
   // for projection
   private final int targetNum;
@@ -39,7 +37,6 @@ public class Projector {
 
   public Projector(Schema inSchema, Schema outSchema, Target [] targets) {
     this.inSchema = inSchema;
-    this.outSchema = outSchema;
 
     this.targetNum = targets != null ? targets.length : 0;
 

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/Target.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/Target.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/Target.java
new file mode 100644
index 0000000..b2651a1
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/Target.java
@@ -0,0 +1,115 @@
+/**
+ * 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.engine.planner;
+
+import com.google.gson.annotations.Expose;
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.common.TajoDataTypes.DataType;
+import org.apache.tajo.common.TajoDataTypes.Type;
+import org.apache.tajo.engine.eval.EvalNode;
+import org.apache.tajo.engine.json.GsonCreator;
+import org.apache.tajo.util.TUtil;
+
+/**
+ * A Target contains how to evaluate an expression and its alias name.
+ */
+public class Target implements Cloneable {
+  @Expose private EvalNode expr;
+  @Expose private Column column;
+  @Expose private String alias = null;
+
+  public Target(EvalNode expr) {
+    this.expr = expr;
+    if (expr.getType() == EvalNode.Type.AGG_FUNCTION &&
+        expr.getValueType().length > 1) { // hack for partial result
+      this.column = new Column(expr.getName(), Type.ARRAY);
+    } else {
+      this.column = new Column(expr.getName(), expr.getValueType()[0]);
+    }
+  }
+
+  public Target(final EvalNode eval, final String alias) {
+    this(eval);
+    this.alias = alias;
+  }
+
+  public final void setAlias(String alias) {
+    this.alias = alias;
+  }
+
+  public final String getAlias() {
+    return alias;
+  }
+
+  public final boolean hasAlias() {
+    return alias != null;
+  }
+
+  public DataType getDataType() {
+    return column.getDataType();
+  }
+
+  public EvalNode getEvalTree() {
+    return this.expr;
+  }
+
+  public Column getColumnSchema() {
+    return this.column;
+  }
+
+  public String toString() {
+    StringBuilder sb = new StringBuilder(expr.toString());
+    if(hasAlias()) {
+      sb.append(", alias=").append(alias);
+    }
+    return sb.toString();
+  }
+
+  public boolean equals(Object obj) {
+    if(obj instanceof Target) {
+      Target other = (Target) obj;
+
+      boolean b1 = expr.equals(other.expr);
+      boolean b2 = column.equals(other.column);
+      boolean b3 = TUtil.checkEquals(alias, other.alias);
+
+      return b1 && b2 && b3;
+    } else {
+      return false;
+    }
+  }
+
+  public int hashCode() {
+    return this.expr.getName().hashCode();
+  }
+
+  @Override
+  public Object clone() throws CloneNotSupportedException {
+    Target target = (Target) super.clone();
+    target.expr = (EvalNode) expr.clone();
+    target.column = (Column) column.clone();
+    target.alias = alias != null ? alias : null;
+
+    return target;
+  }
+
+  public String toJSON() {
+    return GsonCreator.getInstance().toJson(this, Target.class);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/TargetListManager.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/TargetListManager.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/TargetListManager.java
new file mode 100644
index 0000000..739a23d
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/TargetListManager.java
@@ -0,0 +1,168 @@
+/**
+ * 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.engine.planner;
+
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.catalog.Schema;
+import org.apache.tajo.engine.eval.EvalTreeUtil;
+import org.apache.tajo.engine.eval.FieldEval;
+
+import java.util.Collection;
+
+/**
+ * It manages a list of targets.
+ */
+public class TargetListManager {
+  private LogicalPlan plan;
+  private boolean [] evaluatedFlags;
+  private Target [] targets;
+  private Target [] unevaluatedTargets;
+
+  public TargetListManager(LogicalPlan plan, int targetNum) {
+    this.plan = plan;
+    if (targetNum == 0) {
+      evaluatedFlags = new boolean[0];
+    } else {
+      evaluatedFlags = new boolean[targetNum];
+    }
+    this.targets = new Target[targetNum];
+    this.unevaluatedTargets = new Target[targetNum];
+  }
+
+  public TargetListManager(LogicalPlan plan, Target [] original) {
+    this.plan = plan;
+
+    targets = new Target[original.length];
+    unevaluatedTargets = new Target[original.length];
+    for (int i = 0; i < original.length; i++) {
+      try {
+        targets[i] = (Target) original[i].clone();
+        unevaluatedTargets[i] = (Target) original[i].clone();
+      } catch (CloneNotSupportedException e) {
+        e.printStackTrace();
+      }
+    }
+    evaluatedFlags = new boolean[original.length];
+  }
+
+  public TargetListManager(LogicalPlan plan, String blockName) {
+    this(plan, plan.getBlock(blockName).getTargetListManager().getUnEvaluatedTargets());
+  }
+
+  public Target getTarget(int id) {
+    return targets[id];
+  }
+
+  public Target [] getTargets() {
+    return this.targets;
+  }
+
+  public Target [] getUnEvaluatedTargets() {
+    return this.unevaluatedTargets;
+  }
+
+  public void updateTarget(int id, Target target) {
+    this.targets[id] = target;
+    this.unevaluatedTargets[id] = target;
+  }
+
+  public int size() {
+    return targets.length;
+  }
+
+  public void setEvaluated(int id) {
+    evaluatedFlags[id] = true;
+  }
+
+  public void setEvaluatedAll() {
+    for (int i = 0; i < evaluatedFlags.length; i++) {
+      evaluatedFlags[i] = true;
+    }
+  }
+
+  public boolean isEvaluated(int id) {
+    return evaluatedFlags[id];
+  }
+
+  public Target [] getUpdatedTarget() throws CloneNotSupportedException {
+    Target [] updated = new Target[targets.length];
+    for (int i = 0; i < targets.length; i++) {
+      if (targets[i] == null) { // if it is not created
+        continue;
+      }
+
+      if (evaluatedFlags[i]) { // if this target was evaluated, it becomes a column target.
+        Column col = getEvaluatedColumn(i);
+        updated[i] = new Target(new FieldEval(col));
+      } else {
+        updated[i] = (Target) targets[i].clone();
+      }
+    }
+    targets = updated;
+    return updated;
+  }
+
+  public Schema getUpdatedSchema() {
+    Schema schema = new Schema();
+    for (int i = 0; i < evaluatedFlags.length; i++) {
+      if (evaluatedFlags[i]) {
+        Column col = getEvaluatedColumn(i);
+        if (!schema.contains(col.getQualifiedName()))
+        schema.addColumn(col);
+      } else {
+        Collection<Column> cols = getColumnRefs(i);
+        for (Column col : cols) {
+          if (!schema.contains(col.getQualifiedName())) {
+            schema.addColumn(col);
+          }
+        }
+      }
+    }
+
+    return schema;
+  }
+
+  public Collection<Column> getColumnRefs(int id) {
+    return EvalTreeUtil.findDistinctRefColumns(targets[id].getEvalTree());
+  }
+
+  public Column getEvaluatedColumn(int id) {
+    Target t = targets[id];
+    String name;
+    if (t.hasAlias()) {
+      name = t.getAlias();
+    } else if (t.getEvalTree().getName().equals("?")) {
+      name = plan.newAnonymousColumnName();
+    } else {
+      name = t.getEvalTree().getName();
+    }
+    return new Column(name, t.getEvalTree().getValueType()[0]);
+  }
+
+  public boolean isAllEvaluated() {
+    for (boolean isEval : evaluatedFlags) {
+      if (!isEval) {
+        return false;
+      }
+    }
+
+    return true;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/VerifyException.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/VerifyException.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/VerifyException.java
new file mode 100644
index 0000000..a18dd8f
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/VerifyException.java
@@ -0,0 +1,25 @@
+/*
+ * 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.engine.planner;
+
+public class VerifyException extends Exception {
+  public VerifyException(String message) {
+    super(message);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/CreateTableNode.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/CreateTableNode.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/CreateTableNode.java
index 30b0e12..c7a6ad2 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/CreateTableNode.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/CreateTableNode.java
@@ -122,7 +122,7 @@ public class CreateTableNode extends LogicalNode implements Cloneable {
   
   public String toString() {
     StringBuilder sb = new StringBuilder();
-    sb.append("\"Store\": {\"table\": \""+tableName+"\",");
+    sb.append("\"CreateTable\": {\"table\": \""+tableName+"\",");
     if (partitionKeys != null) {
       sb.append("\"partition keys: [");
       for (int i = 0; i < partitionKeys.length; i++) {

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/DropTableNode.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/DropTableNode.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/DropTableNode.java
new file mode 100644
index 0000000..877b805
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/DropTableNode.java
@@ -0,0 +1,71 @@
+/*
+ * 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.engine.planner.logical;
+
+import org.apache.tajo.engine.json.GsonCreator;
+
+public class DropTableNode extends LogicalNode {
+  private String tableName;
+
+  public DropTableNode(String tableName) {
+    super(ExprType.DROP_TABLE);
+    this.tableName = tableName;
+  }
+
+  public String getTableName() {
+    return this.tableName;
+  }
+
+  public boolean equals(Object obj) {
+    if (obj instanceof DropTableNode) {
+      DropTableNode other = (DropTableNode) obj;
+      return super.equals(other) && this.tableName.equals(other.tableName);
+    } else {
+      return false;
+    }
+  }
+
+  @Override
+  public Object clone() throws CloneNotSupportedException {
+    DropTableNode dropTableNode = (DropTableNode) super.clone();
+    dropTableNode.tableName = tableName;
+    return dropTableNode;
+  }
+
+  @Override
+  public String toString() {
+    return "DROP TABLE " + tableName;
+  }
+
+
+  @Override
+  public String toJSON() {
+    return GsonCreator.getInstance().toJson(this, LogicalNode.class);
+  }
+
+  @Override
+  public void preOrder(LogicalNodeVisitor visitor) {
+    visitor.visit(this);
+  }
+
+  @Override
+  public void postOrder(LogicalNodeVisitor visitor) {
+    visitor.visit(this);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/EvalExprNode.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/EvalExprNode.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/EvalExprNode.java
index c44db13..7bcf168 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/EvalExprNode.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/EvalExprNode.java
@@ -24,12 +24,12 @@ package org.apache.tajo.engine.planner.logical;
 import com.google.gson.Gson;
 import com.google.gson.annotations.Expose;
 import org.apache.tajo.engine.json.GsonCreator;
-import org.apache.tajo.engine.parser.QueryBlock;
+import org.apache.tajo.engine.planner.Target;
 
-public class EvalExprNode extends LogicalNode {
-  @Expose private QueryBlock.Target[] exprs;
+public class EvalExprNode extends LogicalNode implements Projectable {
+  @Expose private Target[] exprs;
 
-  public EvalExprNode(QueryBlock.Target[] exprs) {
+  public EvalExprNode(Target[] exprs) {
     super(ExprType.EXPRS);
     this.exprs = exprs;
   }
@@ -39,8 +39,23 @@ public class EvalExprNode extends LogicalNode {
     Gson gson = GsonCreator.getInstance();
     return gson.toJson(this);
   }
-  
-  public QueryBlock.Target[] getExprs() {
+
+  @Override
+  public boolean hasTargets() {
+    return true;
+  }
+
+  @Override
+  public void setTargets(Target[] targets) {
+    this.exprs = targets;
+  }
+
+  @Override
+  public Target[] getTargets() {
+    return exprs;
+  }
+
+  public Target[] getExprs() {
     return this.exprs;
   }
   

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ExprType.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ExprType.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ExprType.java
index 8eb70cf..7255146 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ExprType.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ExprType.java
@@ -25,7 +25,8 @@ public enum ExprType {
   BST_INDEX_SCAN,
   CREATE_INDEX,
   CREATE_TABLE,	
-	DESC_TABLE,
+	DROP_TABLE,
+  DESC_TABLE,
 	EXCEPT,
 	EXPRS,
 	GROUP_BY,

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/GroupbyNode.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/GroupbyNode.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/GroupbyNode.java
index d35db32..9c1c116 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/GroupbyNode.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/GroupbyNode.java
@@ -22,18 +22,18 @@ import com.google.gson.annotations.Expose;
 import org.apache.tajo.catalog.Column;
 import org.apache.tajo.engine.eval.EvalNode;
 import org.apache.tajo.engine.json.GsonCreator;
-import org.apache.tajo.engine.parser.QueryBlock;
+import org.apache.tajo.engine.planner.Target;
 import org.apache.tajo.util.TUtil;
 
 import java.util.Arrays;
 
-public class GroupbyNode extends UnaryNode implements Cloneable {
+public class GroupbyNode extends UnaryNode implements Projectable, Cloneable {
 	@Expose
 	private Column[] columns;
 	@Expose
 	private EvalNode havingCondition = null;
 	@Expose
-	private QueryBlock.Target[] targets;
+	private Target[] targets;
 	
 	public GroupbyNode() {
 		super();
@@ -65,17 +65,20 @@ public class GroupbyNode extends UnaryNode implements Cloneable {
 	public final void setHavingCondition(final EvalNode evalTree) {
 	  this.havingCondition = evalTree;
 	}
-	
-  public boolean hasTargetList() {
+
+  @Override
+  public boolean hasTargets() {
     return this.targets != null;
   }
 
-  public QueryBlock.Target[] getTargets() {
-    return this.targets;
+  @Override
+  public void setTargets(Target[] targets) {
+    this.targets = targets;
   }
 
-  public void setTargetList(QueryBlock.Target[] targets) {
-    this.targets = targets;
+  @Override
+  public Target[] getTargets() {
+    return this.targets;
   }
   
   public void setSubNode(LogicalNode subNode) {
@@ -91,9 +94,9 @@ public class GroupbyNode extends UnaryNode implements Cloneable {
     }
     
     if(hasHavingCondition()) {
-      sb.append("], \"having qual\": \""+havingCondition+"\"");
+      sb.append("], \"having qual\": \"").append(havingCondition).append("\"");
     }
-    if(hasTargetList()) {
+    if(hasTargets()) {
       sb.append(", \"target\": [");
       for (int i = 0; i < targets.length; i++) {
         sb.append("\"").append(targets[i]).append("\"");
@@ -137,9 +140,9 @@ public class GroupbyNode extends UnaryNode implements Cloneable {
     grp.havingCondition = (EvalNode) (havingCondition != null 
         ? havingCondition.clone() : null);    
     if (targets != null) {
-      grp.targets = new QueryBlock.Target[targets.length];
+      grp.targets = new Target[targets.length];
       for (int i = 0; i < targets.length; i++) {
-        grp.targets[i] = (QueryBlock.Target) targets[i].clone();
+        grp.targets[i] = (Target) targets[i].clone();
       }
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/IndexWriteNode.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/IndexWriteNode.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/IndexWriteNode.java
deleted file mode 100644
index fde8d7f..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/IndexWriteNode.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/**
- * 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.engine.planner.logical;
-
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-import com.google.gson.annotations.Expose;
-import org.apache.tajo.catalog.Options;
-import org.apache.tajo.catalog.SortSpec;
-import org.apache.tajo.catalog.proto.CatalogProtos.IndexMethod;
-import org.apache.tajo.engine.json.GsonCreator;
-import org.apache.tajo.engine.parser.CreateIndexStmt;
-
-public class IndexWriteNode extends UnaryNode {
-  @Expose private String indexName;
-  @Expose private boolean unique = false;
-  @Expose private String tableName;
-  @Expose private IndexMethod method = IndexMethod.TWO_LEVEL_BIN_TREE;
-  @Expose private SortSpec[] sortSpecs;
-  @Expose private Options params = null;
-
-  public IndexWriteNode(CreateIndexStmt stmt) {
-    super(ExprType.CREATE_INDEX);
-    this.indexName = stmt.getIndexName();
-    this.unique = stmt.isUnique();
-    this.tableName = stmt.getTableName();
-    this.method = stmt.getMethod();
-    this.sortSpecs = stmt.getSortSpecs();
-    this.params = stmt.hasParams() ? stmt.getParams() : null;
-  }
-  
-  public String getIndexName() {
-    return this.indexName;
-  }
-  
-  public boolean isUnique() {
-    return this.unique;
-  }
-  
-  public void setUnique() {
-    this.unique = true;
-  }
-  
-  public String getTableName() {
-    return this.tableName;
-  }
-  
-  public IndexMethod getMethod() {
-    return this.method;
-  }
-  
-  public SortSpec[] getSortSpecs() {
-    return this.sortSpecs;
-  }
-  
-  public boolean hasParams() {
-    return this.params != null;
-  }
-  
-  public Options getParams() {
-    return this.params;
-  }
-
-  public String toJSON() {
-    for( int i = 0 ; i < this.sortSpecs.length ; i ++ ) {
-      sortSpecs[i].getSortKey().initFromProto();
-    }
-    return GsonCreator.getInstance().toJson(this, LogicalNode.class);
-  }
-  
-  @Override
-  public String toString() {
-    Gson gson = new GsonBuilder().setPrettyPrinting().create();
-    return gson.toJson(this);
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/JoinNode.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/JoinNode.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/JoinNode.java
index c12e89e..a5c288c 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/JoinNode.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/JoinNode.java
@@ -22,15 +22,15 @@
 package org.apache.tajo.engine.planner.logical;
 
 import com.google.gson.annotations.Expose;
+import org.apache.tajo.algebra.JoinType;
 import org.apache.tajo.engine.eval.EvalNode;
 import org.apache.tajo.engine.json.GsonCreator;
-import org.apache.tajo.engine.parser.QueryBlock;
-import org.apache.tajo.engine.planner.JoinType;
+import org.apache.tajo.engine.planner.Target;
 
-public class JoinNode extends BinaryNode implements Cloneable {
+public class JoinNode extends BinaryNode implements Projectable, Cloneable {
   @Expose private JoinType joinType;
   @Expose private EvalNode joinQual;
-  @Expose private QueryBlock.Target[] targets;
+  @Expose private Target[] targets;
 
   public JoinNode(JoinType joinType, LogicalNode left) {
     super(ExprType.JOIN);
@@ -65,15 +65,18 @@ public class JoinNode extends BinaryNode implements Cloneable {
     return this.joinQual;
   }
 
-  public boolean hasTargetList() {
+  @Override
+  public boolean hasTargets() {
     return this.targets != null;
   }
 
-  public QueryBlock.Target[] getTargets() {
+  @Override
+  public Target[] getTargets() {
     return this.targets;
   }
 
-  public void setTargetList(QueryBlock.Target[] targets) {
+  @Override
+  public void setTargets(Target[] targets) {
     this.targets = targets;
   }
 
@@ -97,11 +100,27 @@ public class JoinNode extends BinaryNode implements Cloneable {
   }
 
   public String toString() {
-    return "\"Join\": \"joinType\": \"" + joinType +"\""
-        + (joinQual != null ? ", \"qual\": " + joinQual : "")
-        + "\n\"out schema: " + getOutSchema()
-        + "\n\"in schema: " + getInSchema()
-    		+ "\n" + getOuterNode().toString() + " and " + getInnerNode();
+    StringBuilder sb = new StringBuilder();
+    sb.append("\"Join\": \"joinType\": \" ").append(joinType).append("\"");
+    if (joinQual != null) {
+      sb.append(", \"qual\": ").append(joinQual);
+    }
+    if (targets != null) {
+      sb.append(", \"target list\": ");
+      boolean first = true;
+      for (Target target : targets) {
+        if (!first) {
+          sb.append(", ");
+        }
+        sb.append(target);
+        first = false;
+      }
+    }
+
+    sb.append("\n\"out schema: ").append(getOutSchema());
+    sb.append("\n\"in schema: ").append(getInSchema());
+    sb.append("\n" + getOuterNode().toString()).append(" and ").append(getInnerNode());
+    return sb.toString();
   }
 
   public String toJSON() {

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/LimitNode.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/LimitNode.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/LimitNode.java
index 966e4a8..ceef9d0 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/LimitNode.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/LimitNode.java
@@ -20,24 +20,22 @@ package org.apache.tajo.engine.planner.logical;
 
 import com.google.gson.annotations.Expose;
 import org.apache.tajo.engine.json.GsonCreator;
-import org.apache.tajo.engine.parser.QueryBlock.LimitClause;
-import org.apache.tajo.util.TUtil;
 
 public final class LimitNode extends UnaryNode implements Cloneable {
 	@Expose
-  private LimitClause limitClause;
+  private long fetchFirstNum;
 
 	public LimitNode() {
 		super();
 	}
 
-  public LimitNode(LimitClause limitClause) {
+  public LimitNode(long fetchFirstNum) {
     super(ExprType.LIMIT);
-    this.limitClause = limitClause;
+    this.fetchFirstNum = fetchFirstNum;
   }
   
   public long getFetchFirstNum() {
-    return this.limitClause.getLimitRow();
+    return fetchFirstNum;
   }
   
   @Override 
@@ -45,7 +43,7 @@ public final class LimitNode extends UnaryNode implements Cloneable {
     if (obj instanceof LimitNode) {
       LimitNode other = (LimitNode) obj;
       return super.equals(other)
-          && TUtil.checkEquals(limitClause, other.limitClause)
+          && fetchFirstNum == other.fetchFirstNum
           && subExpr.equals(other.subExpr);
     } else {
       return false;
@@ -55,17 +53,18 @@ public final class LimitNode extends UnaryNode implements Cloneable {
   @Override
   public Object clone() throws CloneNotSupportedException {
     LimitNode newLimitNode = (LimitNode) super.clone();
-    newLimitNode.limitClause = (LimitClause) limitClause.clone();
+    newLimitNode.fetchFirstNum = fetchFirstNum;
     return newLimitNode;
   }
 
   public String toString() {
-    StringBuilder sb = new StringBuilder(limitClause.toString());
+    StringBuilder sb = new StringBuilder("LIMIT ").append(fetchFirstNum);
 
-    sb.append("\n\"out schema: " + getOutSchema()
-        + "\n\"in schema: " + getInSchema());
-    return sb.toString()+"\n"
-        + getSubNode().toString();
+    sb.append("\n\"out schema: ").append(getOutSchema())
+        .append("\n\"in schema: " + getInSchema());
+    sb.append("\n").append(getSubNode().toString());
+
+    return sb.toString();
   }
   
   public String toJSON() {

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/LogicalNode.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/LogicalNode.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/LogicalNode.java
index c206753..d47a84f 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/LogicalNode.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/LogicalNode.java
@@ -25,15 +25,11 @@ import com.google.gson.annotations.Expose;
 import org.apache.tajo.catalog.Schema;
 
 public abstract class LogicalNode implements Cloneable {
-	@Expose
-	private ExprType type;
-	@Expose
-	private Schema inputSchema;
-	@Expose
-	private Schema outputSchema;
+  @Expose private ExprType type;
+	@Expose private Schema inputSchema;
+	@Expose	private Schema outputSchema;
 
-	@Expose
-	private double cost = 0;
+	@Expose	private double cost = 0;
 	
 	public LogicalNode() {
 		

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/Projectable.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/Projectable.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/Projectable.java
new file mode 100644
index 0000000..c2807fe
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/Projectable.java
@@ -0,0 +1,27 @@
+/**
+ * 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.engine.planner.logical;
+
+import org.apache.tajo.engine.planner.Target;
+
+public interface Projectable {
+  boolean hasTargets();
+  void setTargets(Target[] targets);
+  Target [] getTargets();
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ProjectionNode.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ProjectionNode.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ProjectionNode.java
index 2c948f3..999b804 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ProjectionNode.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ProjectionNode.java
@@ -20,11 +20,11 @@ package org.apache.tajo.engine.planner.logical;
 
 import com.google.gson.annotations.Expose;
 import org.apache.tajo.engine.json.GsonCreator;
-import org.apache.tajo.engine.parser.QueryBlock.Target;
+import org.apache.tajo.engine.planner.Target;
 
 import java.util.Arrays;
 
-public class ProjectionNode extends UnaryNode {
+public class ProjectionNode extends UnaryNode implements Projectable {
   /**
    * the targets are always filled even if the query is 'select *'
    */
@@ -34,22 +34,32 @@ public class ProjectionNode extends UnaryNode {
   /**
    * This method is for gson.
    */
+  @SuppressWarnings("unused")
 	private ProjectionNode() {
 		super();
 	}
 
+  /**
+   * @param targets they should be all evaluated ones.
+   */
 	public ProjectionNode(Target [] targets) {		
 		super(ExprType.PROJECTION);
 		this.targets = targets;
 	}
-	
-	public Target [] getTargets() {
-	  return this.targets;
-	}
 
-  public void setTargetList(Target [] targets) {
+  public boolean hasTargets() {
+    return this.targets != null;
+  }
+
+  @Override
+  public void setTargets(Target[] targets) {
     this.targets = targets;
   }
+
+  @Override
+  public Target [] getTargets() {
+    return this.targets;
+  }
 	
 	public void setSubNode(LogicalNode subNode) {
 	  super.setSubNode(subNode);

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ScanNode.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ScanNode.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ScanNode.java
index 64fa63a..7c195dd 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ScanNode.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/logical/ScanNode.java
@@ -20,17 +20,17 @@ package org.apache.tajo.engine.planner.logical;
 
 import com.google.common.base.Objects;
 import com.google.gson.annotations.Expose;
+import org.apache.tajo.catalog.Schema;
 import org.apache.tajo.engine.eval.EvalNode;
 import org.apache.tajo.engine.json.GsonCreator;
-import org.apache.tajo.engine.parser.QueryBlock;
-import org.apache.tajo.engine.parser.QueryBlock.FromTable;
-import org.apache.tajo.engine.parser.QueryBlock.Target;
+import org.apache.tajo.engine.planner.FromTable;
+import org.apache.tajo.engine.planner.Target;
 import org.apache.tajo.util.TUtil;
 
-public class ScanNode extends LogicalNode {
+public class ScanNode extends LogicalNode implements Projectable {
 	@Expose private FromTable table;
 	@Expose private EvalNode qual;
-	@Expose private QueryBlock.Target[] targets;
+	@Expose private Target[] targets;
 	@Expose private boolean local;
   @Expose private boolean broadcast;
 	
@@ -54,6 +54,14 @@ public class ScanNode extends LogicalNode {
 	public boolean hasAlias() {
 	  return table.hasAlias();
 	}
+
+  public String getCanonicalName() {
+    return table.hasAlias() ? table.getAlias() : table.getTableName();
+  }
+
+  public Schema getTableSchema() {
+    return table.getSchema();
+  }
 	
 	public boolean hasQual() {
 	  return qual != null;
@@ -74,15 +82,18 @@ public class ScanNode extends LogicalNode {
 	public void setQual(EvalNode evalTree) {
 	  this.qual = evalTree;
 	}
-	
-	public boolean hasTargetList() {
+
+  @Override
+	public boolean hasTargets() {
 	  return this.targets != null;
 	}
-	
+
+  @Override
 	public void setTargets(Target [] targets) {
 	  this.targets = targets;
 	}
-	
+
+  @Override
 	public Target [] getTargets() {
 	  return this.targets;
 	}
@@ -119,7 +130,7 @@ public class ScanNode extends LogicalNode {
 	    sb.append(", \"qual\": \"").append(this.qual).append("\"");
 	  }
 	  
-	  if (hasTargetList()) {
+	  if (hasTargets()) {
 	    sb.append(", \"target list\": ");
       boolean first = true;
       for (Target target : targets) {
@@ -172,7 +183,7 @@ public class ScanNode extends LogicalNode {
 	    scanNode.qual = (EvalNode) this.qual.clone();
 	  }
 	  
-	  if (hasTargetList()) {
+	  if (hasTargets()) {
 	    scanNode.targets = new Target[targets.length];
       for (int i = 0; i < targets.length; i++) {
         scanNode.targets[i] = (Target) targets[i].clone();

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/AggregationExec.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/AggregationExec.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/AggregationExec.java
index 6c90a40..e7ee5bf 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/AggregationExec.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/AggregationExec.java
@@ -25,7 +25,7 @@ import org.apache.tajo.datum.DatumFactory;
 import org.apache.tajo.engine.eval.ConstEval;
 import org.apache.tajo.engine.eval.EvalContext;
 import org.apache.tajo.engine.eval.EvalNode;
-import org.apache.tajo.engine.parser.QueryBlock;
+import org.apache.tajo.engine.planner.Target;
 import org.apache.tajo.engine.planner.logical.GroupbyNode;
 
 import java.io.IOException;
@@ -77,7 +77,7 @@ public abstract class AggregationExec extends UnaryPhysicalExec {
     evals = new EvalNode[plan.getTargets().length];
     evalContexts = new EvalContext[plan.getTargets().length];
     for (int i = 0; i < plan.getTargets().length; i++) {
-      QueryBlock.Target t = plan.getTargets()[i];
+      Target t = plan.getTargets()[i];
       if (t.getEvalTree().getType() == EvalNode.Type.FIELD && !nonNullGroupingFields.contains(t.getColumnSchema())) {
         evals[i] = new ConstEval(DatumFactory.createNullDatum());
         evalContexts[i] = evals[i].newContext();

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/BNLJoinExec.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/BNLJoinExec.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/BNLJoinExec.java
index 7747dcd..666d049 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/BNLJoinExec.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/BNLJoinExec.java
@@ -62,7 +62,9 @@ public class BNLJoinExec extends BinaryPhysicalExec {
     super(context, SchemaUtil.merge(outer.getSchema(), inner.getSchema()),
         SchemaUtil.merge(outer.getSchema(), inner.getSchema()), outer, inner);
     this.joinQual = join.getJoinQual();
-    this.qualCtx = this.joinQual.newContext();
+    if (joinQual != null) { // if join type is not 'cross join'
+      this.qualCtx = this.joinQual.newContext();
+    }
     this.outerTupleSlots = new ArrayList<Tuple>(TUPLE_SLOT_SIZE);
     this.innerTupleSlots = new ArrayList<Tuple>(TUPLE_SLOT_SIZE);
     this.outerIterator = outerTupleSlots.iterator();

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/EvalExprExec.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/EvalExprExec.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/EvalExprExec.java
index 5af4f18..ae87330 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/EvalExprExec.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/EvalExprExec.java
@@ -20,7 +20,7 @@ package org.apache.tajo.engine.planner.physical;
 
 import org.apache.tajo.TaskAttemptContext;
 import org.apache.tajo.engine.eval.EvalContext;
-import org.apache.tajo.engine.parser.QueryBlock.Target;
+import org.apache.tajo.engine.planner.Target;
 import org.apache.tajo.engine.planner.logical.EvalExprNode;
 import org.apache.tajo.storage.Tuple;
 import org.apache.tajo.storage.VTuple;

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/IndexWriteExec.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/IndexWriteExec.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/IndexWriteExec.java
deleted file mode 100644
index 210a90c..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/IndexWriteExec.java
+++ /dev/null
@@ -1,88 +0,0 @@
-/**
- * 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.engine.planner.physical;
-
-import com.google.common.base.Preconditions;
-import org.apache.hadoop.fs.Path;
-import org.apache.tajo.TaskAttemptContext;
-import org.apache.tajo.catalog.Column;
-import org.apache.tajo.catalog.Schema;
-import org.apache.tajo.conf.TajoConf;
-import org.apache.tajo.engine.planner.logical.IndexWriteNode;
-import org.apache.tajo.storage.*;
-import org.apache.tajo.storage.index.bst.BSTIndex;
-import org.apache.tajo.storage.index.bst.BSTIndex.BSTIndexWriter;
-import org.apache.tajo.util.IndexUtil;
-
-import java.io.IOException;
-
-public class IndexWriteExec extends UnaryPhysicalExec {
-  private int [] indexKeys = null;
-  private final BSTIndexWriter indexWriter;
-  private final TupleComparator comp;
-
-  public IndexWriteExec(final TaskAttemptContext context, final StorageManager sm,
-      final IndexWriteNode plan, final Fragment fragment,
-      final PhysicalExec child) throws IOException {
-    super(context, plan.getInSchema(), plan.getOutSchema(), child);
-    Preconditions.checkArgument(inSchema.equals(child.getSchema()));
-
-    indexKeys = new int[plan.getSortSpecs().length];
-    Schema keySchema = new Schema();
-    Column col;
-    for (int i = 0 ; i < plan.getSortSpecs().length; i++) {
-      col = plan.getSortSpecs()[i].getSortKey();
-      indexKeys[i] = inSchema.getColumnId(col.getQualifiedName());
-      keySchema.addColumn(inSchema.getColumn(col.getQualifiedName()));
-    }
-    this.comp = new TupleComparator(keySchema, plan.getSortSpecs());
-    
-    BSTIndex bst = new BSTIndex(new TajoConf());
-    Path dir = new Path(sm.getTablePath(plan.getTableName()) , "index");
-    // TODO - to be improved
-    this.indexWriter = bst.getIndexWriter(new Path(dir,
-        IndexUtil.getIndexNameOfFrag(fragment, plan.getSortSpecs())),
-        BSTIndex.TWO_LEVEL_INDEX, keySchema, comp);
-  }
-
-  @Override
-  public Tuple next() throws IOException {
-    indexWriter.open();
-    Tuple tuple;
-    
-    while ((tuple = child.next()) != null) {
-      Tuple keys = new VTuple(indexKeys.length);
-      for (int idx = 0; idx < indexKeys.length; idx++) {
-        keys.put(idx, tuple.get(indexKeys[idx]));
-      }
-      indexWriter.write(keys, tuple.getOffset());
-    }
-    
-    indexWriter.flush();
-    indexWriter.close();
-    return null;
-  }
-
-  @Override
-  public void rescan() throws IOException {
-  }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/NLJoinExec.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/NLJoinExec.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/NLJoinExec.java
index 82e78f3..3ce25ea 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/NLJoinExec.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/physical/NLJoinExec.java
@@ -67,6 +67,10 @@ public class NLJoinExec extends BinaryPhysicalExec {
     outTuple = new VTuple(outSchema.getColumnNum());
   }
 
+  public JoinNode getPlan() {
+    return this.plan;
+  }
+
   public Tuple next() throws IOException {
     for (;;) {
       if (needNewOuter) {

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/utils/SchemaUtil.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/utils/SchemaUtil.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/utils/SchemaUtil.java
index 53332a6..1f7fb85 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/utils/SchemaUtil.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/utils/SchemaUtil.java
@@ -21,9 +21,7 @@ package org.apache.tajo.engine.utils;
 import org.apache.tajo.catalog.Column;
 import org.apache.tajo.catalog.Schema;
 import org.apache.tajo.common.TajoDataTypes.DataType;
-import org.apache.tajo.engine.parser.QueryBlock;
-
-import java.util.Collection;
+import org.apache.tajo.engine.planner.FromTable;
 
 public class SchemaUtil {
   public static Schema merge(Schema left, Schema right) {
@@ -42,9 +40,9 @@ public class SchemaUtil {
     return merged;
   }
 
-  public static Schema merge(QueryBlock.FromTable [] fromTables) {
+  public static Schema merge(FromTable[] fromTables) {
     Schema merged = new Schema();
-    for (QueryBlock.FromTable table : fromTables) {
+    for (FromTable table : fromTables) {
       merged.addColumns(table.getSchema());
     }
 
@@ -65,31 +63,6 @@ public class SchemaUtil {
     return common;
   }
 
-  public static Schema mergeAllWithNoDup(Collection<Column>...columnList) {
-    Schema merged = new Schema();
-    for (Collection<Column> columns : columnList) {
-      for (Column col : columns) {
-        if (merged.contains(col.getQualifiedName())) {
-          continue;
-        }
-        merged.addColumn(col);
-      }
-    }
-
-    return merged;
-  }
-
-  public static Schema getProjectedSchema(Schema inSchema, Collection<Column> columns) {
-    Schema projected = new Schema();
-    for (Column col : columns) {
-      if (inSchema.contains(col.getQualifiedName())) {
-        projected.addColumn(col);
-      }
-    }
-
-    return projected;
-  }
-
   public static DataType[] newNoNameSchema(DataType... types) {
     DataType [] dataTypes = types.clone();
     return dataTypes;

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/ClientService.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/ClientService.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/ClientService.java
index 7116b13..827641a 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/ClientService.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/ClientService.java
@@ -20,6 +20,7 @@ package org.apache.tajo.master;
 
 import com.google.protobuf.RpcController;
 import com.google.protobuf.ServiceException;
+import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.fs.FileStatus;
@@ -132,7 +133,7 @@ public class ClientService extends AbstractService {
         SubmitQueryRespose.Builder build = SubmitQueryRespose.newBuilder();
         build.setResultCode(ResultCode.ERROR);
         if (e.getMessage() != null) {
-          build.setErrorMessage(e.getMessage());
+          build.setErrorMessage(ExceptionUtils.getStackTrace(e));
         } else {
           build.setErrorMessage("Internal Error");
         }
@@ -159,7 +160,9 @@ public class ClientService extends AbstractService {
         return builder.build();
       } catch (Exception e) {
         builder.setResultCode(ResultCode.ERROR);
-        builder.setErrorMessage(e.getMessage());
+        if (e.getMessage() == null) {
+          builder.setErrorMessage(ExceptionUtils.getStackTrace(e));
+        }
         return builder.build();
       }
     }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/ExecutionBlock.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/ExecutionBlock.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/ExecutionBlock.java
index 676fec3..69d0804 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/ExecutionBlock.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/ExecutionBlock.java
@@ -76,11 +76,7 @@ public class ExecutionBlock {
         || plan.getType() == ExprType.CREATE_INDEX);
 
     this.plan = plan;
-    if (plan instanceof StoreTableNode) {
-      store = (StoreTableNode) plan;
-    } else {
-      store = (StoreTableNode) ((IndexWriteNode)plan).getSubNode();
-    }
+    store = (StoreTableNode) plan;
 
     LogicalNode node = plan;
     ArrayList<LogicalNode> s = new ArrayList<LogicalNode>();

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalEngine.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalEngine.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalEngine.java
index 978862e..3710f02 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalEngine.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalEngine.java
@@ -33,6 +33,7 @@ import org.apache.hadoop.yarn.service.AbstractService;
 import org.apache.hadoop.yarn.util.Records;
 import org.apache.tajo.QueryConf;
 import org.apache.tajo.QueryId;
+import org.apache.tajo.algebra.Expr;
 import org.apache.tajo.catalog.CatalogService;
 import org.apache.tajo.catalog.CatalogUtil;
 import org.apache.tajo.catalog.TableDesc;
@@ -44,24 +45,23 @@ import org.apache.tajo.engine.exception.EmptyClusterException;
 import org.apache.tajo.engine.exception.IllegalQueryStatusException;
 import org.apache.tajo.engine.exception.NoSuchQueryIdException;
 import org.apache.tajo.engine.exception.UnknownWorkerException;
-import org.apache.tajo.engine.parser.DropTableStmt;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
-import org.apache.tajo.engine.parser.StatementType;
-import org.apache.tajo.engine.planner.LogicalOptimizer;
-import org.apache.tajo.engine.planner.LogicalPlanner;
-import org.apache.tajo.engine.planner.PlanningContext;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
+import org.apache.tajo.engine.planner.*;
 import org.apache.tajo.engine.planner.global.GlobalOptimizer;
 import org.apache.tajo.engine.planner.global.MasterPlan;
+import org.apache.tajo.engine.planner.logical.*;
+import org.apache.tajo.engine.planner.LogicalOptimizer;
+import org.apache.tajo.engine.planner.LogicalPlanner;
 import org.apache.tajo.engine.planner.logical.CreateTableNode;
 import org.apache.tajo.engine.planner.logical.LogicalNode;
 import org.apache.tajo.engine.planner.logical.LogicalRootNode;
-import org.apache.tajo.engine.query.exception.TQLSyntaxError;
 import org.apache.tajo.master.TajoMaster.MasterContext;
 import org.apache.tajo.storage.StorageManager;
 import org.apache.tajo.storage.StorageUtil;
 import org.apache.tajo.util.TajoIdUtils;
 
 import java.io.IOException;
+import java.sql.SQLException;
 import java.util.EnumSet;
 import java.util.Set;
 
@@ -73,8 +73,8 @@ public class GlobalEngine extends AbstractService {
   private final MasterContext context;
   private final StorageManager sm;
 
+  private SQLAnalyzer analyzer;
   private CatalogService catalog;
-  private QueryAnalyzer analyzer;
   private LogicalPlanner planner;
   private GlobalPlanner globalPlanner;
   private GlobalOptimizer globalOptimizer;
@@ -93,7 +93,7 @@ public class GlobalEngine extends AbstractService {
   public void start() {
     try  {
       connectYarnClient();
-      analyzer = new QueryAnalyzer(context.getCatalog());
+      analyzer = new SQLAnalyzer();
       planner = new LogicalPlanner(context.getCatalog());
 
       globalPlanner = new GlobalPlanner(context.getConf(), context.getCatalog(),
@@ -115,19 +115,17 @@ public class GlobalEngine extends AbstractService {
       throws InterruptedException, IOException,
       NoSuchQueryIdException, IllegalQueryStatusException,
       UnknownWorkerException, EmptyClusterException {
+
     long querySubmittionTime = context.getClock().getTime();
     LOG.info("SQL: " + tql);
     // parse the query
-    PlanningContext planningContext = analyzer.parse(tql);
+    Expr planningContext = analyzer.parse(tql);
+    LogicalRootNode plan = (LogicalRootNode) createLogicalPlan(planningContext);
 
-    StatementType cmdType = planningContext.getParseTree().getStatementType();
-
-    if (cmdType == StatementType.CREATE_TABLE || cmdType == StatementType.DROP_TABLE) {
-      updateQuery(planningContext);
+    if (PlannerUtil.checkIfDDLPlan(plan)) {
+      updateQuery(plan.getSubNode());
       return TajoIdUtils.NullQueryId;
     } else {
-      LogicalRootNode plan = (LogicalRootNode) createLogicalPlan(planningContext);
-
       ApplicationAttemptId appAttemptId = submitQuery();
       QueryId queryId = TajoIdUtils.createQueryId(appAttemptId);
       MasterPlan masterPlan = createGlobalPlan(queryId, plan);
@@ -135,8 +133,9 @@ public class GlobalEngine extends AbstractService {
       queryConf.setUser(UserGroupInformation.getCurrentUser().getShortUserName());
 
       // the output table is given by user
-      if (planningContext.hasExplicitOutputTable()) {
-        queryConf.setOutputTable(planningContext.getExplicitOutputTable());
+      if (plan.getSubNode().getType() == ExprType.CREATE_TABLE) {
+        CreateTableNode createTableNode = (CreateTableNode) plan.getSubNode();
+        queryConf.setOutputTable(createTableNode.getTableName());
       }
 
       QueryMaster query = new QueryMaster(context, appAttemptId,
@@ -192,38 +191,48 @@ public class GlobalEngine extends AbstractService {
     return attemptId;
   }
 
-  public boolean updateQuery(String sql) throws IOException {
+  public QueryId updateQuery(String sql) throws IOException, SQLException {
     LOG.info("SQL: " + sql);
-    PlanningContext planningContext = analyzer.parse(sql);
-    return updateQuery(planningContext);
+    // parse the query
+    Expr planningContext = analyzer.parse(sql);
+    LogicalRootNode plan = (LogicalRootNode) createLogicalPlan(planningContext);
+
+    if (!PlannerUtil.checkIfDDLPlan(plan)) {
+      throw new SQLException("This is not update query:\n" + sql);
+    } else {
+      updateQuery(plan.getSubNode());
+      return TajoIdUtils.NullQueryId;
+    }
   }
 
-  public boolean updateQuery(PlanningContext planningContext) throws IOException {
-    StatementType type = planningContext.getParseTree().getStatementType();
+  private boolean updateQuery(LogicalNode root) throws IOException {
 
-    switch (type) {
+    switch (root.getType()) {
       case CREATE_TABLE:
-        LogicalRootNode plan = (LogicalRootNode) createLogicalPlan(planningContext);
-        createTable(plan);
+        CreateTableNode createTable = (CreateTableNode) root;
+        createTable(createTable);
         return true;
 
       case DROP_TABLE:
-        DropTableStmt stmt = (DropTableStmt) planningContext.getParseTree();
+        DropTableNode stmt = (DropTableNode) root;
         dropTable(stmt.getTableName());
         return true;
 
       default:
-        throw new TQLSyntaxError(planningContext.getRawQuery(), "updateQuery cannot handle such query");
+        throw new InternalError("updateQuery cannot handle such query: \n" + root.toJSON());
     }
   }
 
-  private LogicalNode createLogicalPlan(PlanningContext planningContext)
-      throws IOException {
+  private LogicalNode createLogicalPlan(Expr expression) throws IOException {
 
-    LogicalNode plan = planner.createPlan(planningContext);
-    plan = LogicalOptimizer.optimize(planningContext, plan);
-    LogicalNode optimizedPlan = LogicalOptimizer.pushIndex(plan, sm);
-    LOG.info("LogicalPlan:\n" + plan);
+    LogicalPlan plan = planner.createPlan(expression);
+    LogicalNode optimizedPlan = null;
+    try {
+      optimizedPlan = LogicalOptimizer.optimize(plan);
+    } catch (CloneNotSupportedException e) {
+      e.printStackTrace();
+    }
+    LOG.info("LogicalPlan:\n" + plan.getRootBlock().getRoot());
 
     return optimizedPlan;
   }
@@ -241,18 +250,15 @@ public class GlobalEngine extends AbstractService {
     query.start();
   }
 
-  private TableDesc createTable(LogicalRootNode root) throws IOException {
-    CreateTableNode createTable = (CreateTableNode) root.getSubNode();
+  private TableDesc createTable(CreateTableNode createTable) throws IOException {
     TableMeta meta;
 
     if (createTable.hasOptions()) {
       meta = CatalogUtil.newTableMeta(createTable.getSchema(),
           createTable.getStorageType(), createTable.getOptions());
-
     } else {
       meta = CatalogUtil.newTableMeta(createTable.getSchema(),
           createTable.getStorageType());
-
     }
 
     if(!createTable.isExternal()){

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalPlanner.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalPlanner.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalPlanner.java
index d447d07..9907377 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalPlanner.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/GlobalPlanner.java
@@ -27,7 +27,7 @@ import org.apache.tajo.SubQueryId;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
 import org.apache.tajo.conf.TajoConf;
-import org.apache.tajo.engine.parser.QueryBlock.FromTable;
+import org.apache.tajo.engine.planner.FromTable;
 import org.apache.tajo.engine.planner.PlannerUtil;
 import org.apache.tajo.engine.planner.global.MasterPlan;
 import org.apache.tajo.engine.planner.logical.*;
@@ -44,20 +44,14 @@ import java.util.Map;
 public class GlobalPlanner {
   private static Log LOG = LogFactory.getLog(GlobalPlanner.class);
 
-  private TajoConf conf;
   private StorageManager sm;
-  private CatalogService catalog;
   private QueryId queryId;
-  private EventHandler eventHandler;
 
   public GlobalPlanner(final TajoConf conf, final CatalogService catalog,
                        final StorageManager sm,
                        final EventHandler eventHandler)
       throws IOException {
-    this.conf = conf;
     this.sm = sm;
-    this.catalog = catalog;
-    this.eventHandler = eventHandler;
   }
 
   /**
@@ -80,28 +74,17 @@ public class GlobalPlanner {
 
     // insert store at the subnode of the root
     UnaryNode root = rootNode;
-    IndexWriteNode indexNode = null;
-    // TODO: check whether the type of the subnode is CREATE_INDEX
-    if (root.getSubNode().getType() == ExprType.CREATE_INDEX) {
-      indexNode = (IndexWriteNode) root.getSubNode();
-      root = (UnaryNode)root.getSubNode();
-      
-      StoreIndexNode store = new StoreIndexNode(
-          QueryIdFactory.newSubQueryId(this.queryId).toString());
-      store.setLocal(false);
-      PlannerUtil.insertNode(root, store);
-      
-    } else if (root.getSubNode().getType() != ExprType.STORE) {
+    if (root.getSubNode().getType() != ExprType.STORE) {
       SubQueryId subQueryId = QueryIdFactory.newSubQueryId(this.queryId);
       outputTableName = subQueryId.toString();
       insertStore(subQueryId.toString(),root).setLocal(false);
     }
     
     // convert 2-phase plan
-    LogicalNode tp = convertTo2Phase(rootNode);
+    LogicalNode twoPhased = convertTo2Phase(rootNode);
 
     // make query graph
-    MasterPlan globalPlan = convertToGlobalPlan(indexNode, tp);
+    MasterPlan globalPlan = convertToGlobalPlan(twoPhased);
     globalPlan.setOutputTableName(outputTableName);
 
     return globalPlan;
@@ -702,20 +685,13 @@ public class GlobalPlanner {
     return parent;
   }
   
-  private MasterPlan convertToGlobalPlan(IndexWriteNode index,
-                                         LogicalNode logicalPlan) throws IOException {
+  private MasterPlan convertToGlobalPlan(LogicalNode logicalPlan) throws IOException {
     recursiveBuildSubQuery(logicalPlan);
     ExecutionBlock root;
-    
-    if (index != null) {
-      SubQueryId id = QueryIdFactory.newSubQueryId(queryId);
-      ExecutionBlock unit = new ExecutionBlock(id);
-      root = makeScanSubQuery(unit);
-      root.setPlan(index);
-    } else {
-      root = convertMap.get(((LogicalRootNode)logicalPlan).getSubNode());
-      root.getStoreTableNode().setLocal(false);
-    }
+
+    root = convertMap.get(((LogicalRootNode)logicalPlan).getSubNode());
+    root.getStoreTableNode().setLocal(false);
+
     return new MasterPlan(root);
   }
 }


[12/13] TAJO-87: Integration of tajo algebra module and SQL parser

Posted by hy...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/test/java/org/apache/tajo/algebra/TestExpr.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/test/java/org/apache/tajo/algebra/TestExpr.java b/tajo-algebra/src/test/java/org/apache/tajo/algebra/TestExpr.java
index 984c30f..24b535b 100644
--- a/tajo-algebra/src/test/java/org/apache/tajo/algebra/TestExpr.java
+++ b/tajo-algebra/src/test/java/org/apache/tajo/algebra/TestExpr.java
@@ -22,83 +22,84 @@ import org.junit.Test;
 
 import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertFalse;
-import static org.apache.tajo.algebra.LiteralExpr.LiteralType;
+import static org.apache.tajo.algebra.LiteralValue.LiteralType;
 
 public class TestExpr {
 
   @Test
   public void testBinaryEquals() {
-    Expr expr1 = new BinaryOperator(ExprType.Plus,
-        new LiteralExpr("1", LiteralType.Unsigned_Integer),
-        new LiteralExpr("2", LiteralType.Unsigned_Integer));
+    Expr expr1 = new BinaryOperator(OpType.Plus,
+        new LiteralValue("1", LiteralType.Unsigned_Integer),
+        new LiteralValue("2", LiteralType.Unsigned_Integer));
 
-    Expr expr2 = new BinaryOperator(ExprType.Plus,
-        new LiteralExpr("1", LiteralType.Unsigned_Integer),
-        new LiteralExpr("2", LiteralType.Unsigned_Integer));
+    Expr expr2 = new BinaryOperator(OpType.Plus,
+        new LiteralValue("1", LiteralType.Unsigned_Integer),
+        new LiteralValue("2", LiteralType.Unsigned_Integer));
 
     assertEquals(expr1, expr2);
 
-    Expr expr3 = new BinaryOperator(ExprType.Minus,
-        new LiteralExpr("1", LiteralType.Unsigned_Integer),
-        new LiteralExpr("2", LiteralType.Unsigned_Integer));
+    Expr expr3 = new BinaryOperator(OpType.Minus,
+        new LiteralValue("1", LiteralType.Unsigned_Integer),
+        new LiteralValue("2", LiteralType.Unsigned_Integer));
 
     assertFalse(expr1.equals(expr3));
 
 
-    Expr expr4 = new BinaryOperator(ExprType.Plus,
-        new LiteralExpr("2", LiteralType.Unsigned_Integer),
-        new LiteralExpr("2", LiteralType.Unsigned_Integer));
+    Expr expr4 = new BinaryOperator(OpType.Plus,
+        new LiteralValue("2", LiteralType.Unsigned_Integer),
+        new LiteralValue("2", LiteralType.Unsigned_Integer));
 
     assertFalse(expr1.equals(expr4));
 
-    Expr expr5 = new BinaryOperator(ExprType.Plus,
-        new LiteralExpr("1", LiteralType.Unsigned_Integer),
-        new LiteralExpr("3", LiteralType.Unsigned_Integer));
+    Expr expr5 = new BinaryOperator(OpType.Plus,
+        new LiteralValue("1", LiteralType.Unsigned_Integer),
+        new LiteralValue("3", LiteralType.Unsigned_Integer));
 
     assertFalse(expr1.equals(expr5));
   }
 
   @Test
   public void testBinaryHierarchyEquals() {
-    Expr left1 = new BinaryOperator(ExprType.Plus,
-        new LiteralExpr("1", LiteralType.Unsigned_Integer),
-        new LiteralExpr("2", LiteralType.Unsigned_Integer));
+    Expr left1 = new BinaryOperator(OpType.Plus,
+        new LiteralValue("1", LiteralType.Unsigned_Integer),
+        new LiteralValue("2", LiteralType.Unsigned_Integer));
 
-    Expr right1 = new BinaryOperator(ExprType.Multiply,
-        new LiteralExpr("2", LiteralType.Unsigned_Integer),
-        new LiteralExpr("3", LiteralType.Unsigned_Integer));
+    Expr right1 = new BinaryOperator(OpType.Multiply,
+        new LiteralValue("2", LiteralType.Unsigned_Integer),
+        new LiteralValue("3", LiteralType.Unsigned_Integer));
 
-    Expr one = new BinaryOperator(ExprType.Plus, left1, right1);
+    Expr one = new BinaryOperator(OpType.Plus, left1, right1);
 
-    Expr left2 = new BinaryOperator(ExprType.Plus,
-        new LiteralExpr("1", LiteralType.Unsigned_Integer),
-        new LiteralExpr("2", LiteralType.Unsigned_Integer));
+    Expr left2 = new BinaryOperator(OpType.Plus,
+        new LiteralValue("1", LiteralType.Unsigned_Integer),
+        new LiteralValue("2", LiteralType.Unsigned_Integer));
 
-    Expr right2 = new BinaryOperator(ExprType.Multiply,
-        new LiteralExpr("2", LiteralType.Unsigned_Integer),
-        new LiteralExpr("3", LiteralType.Unsigned_Integer));
+    Expr right2 = new BinaryOperator(OpType.Multiply,
+        new LiteralValue("2", LiteralType.Unsigned_Integer),
+        new LiteralValue("3", LiteralType.Unsigned_Integer));
 
-    Expr two = new BinaryOperator(ExprType.Plus, left2, right2);
+    Expr two = new BinaryOperator(OpType.Plus, left2, right2);
 
     assertEquals(one, two);
 
-    Expr left3 = new BinaryOperator(ExprType.Minus,
-        new LiteralExpr("1", LiteralType.Unsigned_Integer),
-        new LiteralExpr("2", LiteralType.Unsigned_Integer));
+    Expr left3 = new BinaryOperator(OpType.Minus,
+        new LiteralValue("1", LiteralType.Unsigned_Integer),
+        new LiteralValue("2", LiteralType.Unsigned_Integer));
 
-    Expr wrong1 = new BinaryOperator(ExprType.Plus, left3, right1);
+    Expr wrong1 = new BinaryOperator(OpType.Plus, left3, right1);
 
     assertFalse(one.equals(wrong1));
   }
 
   @Test
   public void testEquals() throws Exception {
-    Expr expr = new BinaryOperator(ExprType.LessThan,
-        new LiteralExpr("1", LiteralType.Unsigned_Integer),
-        new LiteralExpr("2", LiteralType.Unsigned_Integer));
+    Expr expr = new BinaryOperator(OpType.LessThan,
+        new LiteralValue("1", LiteralType.Unsigned_Integer),
+        new LiteralValue("2", LiteralType.Unsigned_Integer));
 
     Relation relation = new Relation("employee");
-    Selection selection = new Selection(relation, expr);
+    Selection selection = new Selection(expr);
+    selection.setChild(relation);
 
     Aggregation aggregation = new Aggregation();
     aggregation.setTargets(new Target[]{
@@ -119,12 +120,13 @@ public class TestExpr {
   }
 
   private Expr generateOneExpr() {
-    Expr expr = new BinaryOperator(ExprType.LessThan,
-        new LiteralExpr("1", LiteralType.Unsigned_Integer),
-        new LiteralExpr("2", LiteralType.Unsigned_Integer));
+    Expr expr = new BinaryOperator(OpType.LessThan,
+        new LiteralValue("1", LiteralType.Unsigned_Integer),
+        new LiteralValue("2", LiteralType.Unsigned_Integer));
 
     Relation relation = new Relation("employee");
-    Selection selection = new Selection(relation, expr);
+    Selection selection = new Selection(expr);
+    selection.setChild(relation);
 
     Aggregation aggregation = new Aggregation();
     aggregation.setTargets(new Target[]{

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/Options.java
----------------------------------------------------------------------
diff --git a/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/Options.java b/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/Options.java
index 6fbfa61..afd27b2 100644
--- a/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/Options.java
+++ b/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/Options.java
@@ -69,6 +69,12 @@ public class Options implements ProtoObject<KeyValueSetProto>, Cloneable {
 		setModified();
 		this.keyVals.put(key, val);
 	}
+
+  public void putAll(Map<String, String> keyValues) {
+    initOptions();
+    setModified();
+    this.keyVals.putAll(keyValues);
+  }
 	
 	public void putAll(Options options) {
 	  initOptions();

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/Schema.java
----------------------------------------------------------------------
diff --git a/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/Schema.java b/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/Schema.java
index 5a4be0d..7a0168c 100644
--- a/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/Schema.java
+++ b/tajo-catalog/tajo-catalog-common/src/main/java/org/apache/tajo/catalog/Schema.java
@@ -57,7 +57,7 @@ public class Schema implements ProtoObject<SchemaProto>, Cloneable {
 	public Schema(Schema schema) {
 	  this();
 		this.fields = new ArrayList<Column>(schema.fields);
-		this.fieldsByName = new HashMap<String, Integer>(schema.fieldsByName);
+		this.fieldsByName = new TreeMap<String, Integer>(schema.fieldsByName);
 	}
 	
 	public Schema(Column [] columns) {

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-catalog/tajo-catalog-server/src/main/java/org/apache/tajo/catalog/CatalogServer.java
----------------------------------------------------------------------
diff --git a/tajo-catalog/tajo-catalog-server/src/main/java/org/apache/tajo/catalog/CatalogServer.java b/tajo-catalog/tajo-catalog-server/src/main/java/org/apache/tajo/catalog/CatalogServer.java
index 57754f3..c3d0d4a 100644
--- a/tajo-catalog/tajo-catalog-server/src/main/java/org/apache/tajo/catalog/CatalogServer.java
+++ b/tajo-catalog/tajo-catalog-server/src/main/java/org/apache/tajo/catalog/CatalogServer.java
@@ -443,8 +443,14 @@ public class CatalogServer extends AbstractService {
       for (int i = 0; i < size; i++) {
         paramTypes.add(request.getParameterTypes(i));
       }
-      return functions.get(CatalogUtil.getCanonicalName(
-          request.getSignature().toLowerCase(), paramTypes));
+
+      String key = CatalogUtil.getCanonicalName(
+          request.getSignature().toLowerCase(), paramTypes);
+      if (!functions.containsKey(key)) {
+        return null;
+      } else {
+        return functions.get(key);
+      }
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-common/src/main/java/org/apache/tajo/annotation/NotThreadSafe.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/annotation/NotThreadSafe.java b/tajo-common/src/main/java/org/apache/tajo/annotation/NotThreadSafe.java
new file mode 100644
index 0000000..caeb654
--- /dev/null
+++ b/tajo-common/src/main/java/org/apache/tajo/annotation/NotThreadSafe.java
@@ -0,0 +1,30 @@
+/**
+ * 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.annotation;
+
+import java.lang.annotation.*;
+
+/**
+ * The type to which this annotation is applied is not thread-safe.
+ */
+@Documented
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.CLASS)
+public @interface NotThreadSafe {
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-common/src/main/java/org/apache/tajo/annotation/ThreadSafe.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/annotation/ThreadSafe.java b/tajo-common/src/main/java/org/apache/tajo/annotation/ThreadSafe.java
new file mode 100644
index 0000000..84eae4b
--- /dev/null
+++ b/tajo-common/src/main/java/org/apache/tajo/annotation/ThreadSafe.java
@@ -0,0 +1,30 @@
+/**
+ * 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.annotation;
+
+import java.lang.annotation.*;
+
+/**
+ * The type to which this annotation is applied is not thread-safe.
+ */
+@Documented
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.CLASS)
+public @interface ThreadSafe {
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-common/src/main/java/org/apache/tajo/util/TUtil.java
----------------------------------------------------------------------
diff --git a/tajo-common/src/main/java/org/apache/tajo/util/TUtil.java b/tajo-common/src/main/java/org/apache/tajo/util/TUtil.java
index 33021fe..f44062d 100644
--- a/tajo-common/src/main/java/org/apache/tajo/util/TUtil.java
+++ b/tajo-common/src/main/java/org/apache/tajo/util/TUtil.java
@@ -30,9 +30,10 @@ public class TUtil {
   /**
    * check two objects as equals. 
    * It will return true even if they are all null.
-   * 
-   * @param s1
-   * @param s2
+   *
+   * @param s1 the first object to be compared.
+   * @param s2 the second object to be compared
+   *
    * @return true if they are equal or all null
    */
   public static boolean checkEquals(Object s1, Object s2) {
@@ -46,11 +47,11 @@ public class TUtil {
   }
 
   /**
-   * check two arrays as equals. 
+   * check two arrays as equals. It also check the equivalence of null.
    * It will return true even if they are all null.
-   * 
-   * @param s1
-   * @param s2
+   *
+   * @param s1 the first array to be compared.
+   * @param s2 the second array to be compared
    * @return true if they are equal or all null
    */
   public static boolean checkEquals(Object [] s1, Object [] s2) {
@@ -73,6 +74,10 @@ public class TUtil {
     return new HashSet<T>();
   }
 
+  public static <T> Set<T> newHashSet(T ...items) {
+    return new HashSet<T>(Arrays.asList(items));
+  }
+
   public static <T> List<T> newList() {
     return new ArrayList<T>();
   }
@@ -101,4 +106,18 @@ public class TUtil {
                 QueryIdFactory.newSubQueryId(
                     QueryIdFactory.newQueryId())), 0);
   }
+
+  /**
+   * It check if T is null or not.
+   *
+   * @param reference the object reference to be checked
+   * @param <T> The object type
+   * @return The reference
+   */
+  public static <T> T checkNotNull(T reference) {
+    if (reference == null) {
+      throw new NullPointerException();
+    }
+    return reference;
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/pom.xml
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/pom.xml b/tajo-core/tajo-core-backend/pom.xml
index 8cd2128..99bb666 100644
--- a/tajo-core/tajo-core-backend/pom.xml
+++ b/tajo-core/tajo-core-backend/pom.xml
@@ -33,6 +33,8 @@
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+    <antlr4.visitor>true</antlr4.visitor>
+    <antlr4.listener>true</antlr4.listener>
   </properties>
 
   <repositories>
@@ -130,11 +132,11 @@
       </plugin>
       <plugin>
         <groupId>org.antlr</groupId>
-        <artifactId>antlr3-maven-plugin</artifactId>
+        <artifactId>antlr4-maven-plugin</artifactId>
         <executions>
           <execution>
             <goals>
-              <goal>antlr</goal>
+              <goal>antlr4</goal>
             </goals>
           </execution>
         </executions>
@@ -238,6 +240,10 @@
     </dependency>
     <dependency>
       <groupId>org.apache.tajo</groupId>
+      <artifactId>tajo-algebra</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.tajo</groupId>
       <artifactId>tajo-catalog-common</artifactId>
     </dependency>
     <dependency>
@@ -314,16 +320,16 @@
       <artifactId>protobuf-java</artifactId>
     </dependency>
     <dependency>
-      <groupId>org.antlr</groupId>
-      <artifactId>antlr</artifactId>
-    </dependency>
-    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
     </dependency>
     <dependency>
       <groupId>org.antlr</groupId>
-      <artifactId>antlr-runtime</artifactId>
+      <artifactId>antlr4</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.antlr</groupId>
+      <artifactId>antlr4-runtime</artifactId>
     </dependency>
     <dependency>
       <groupId>commons-logging</groupId>

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/antlr3/org/apache/tajo/engine/parser/SQLLexer.g
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/antlr3/org/apache/tajo/engine/parser/SQLLexer.g b/tajo-core/tajo-core-backend/src/main/antlr3/org/apache/tajo/engine/parser/SQLLexer.g
deleted file mode 100644
index 48aa8de..0000000
--- a/tajo-core/tajo-core-backend/src/main/antlr3/org/apache/tajo/engine/parser/SQLLexer.g
+++ /dev/null
@@ -1,391 +0,0 @@
-/*
- 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.
- */
-
-lexer grammar SQLLexer;
-
-@header {
-package org.apache.tajo.engine.parser;
-
-import org.apache.tajo.engine.query.exception.TQLParseError;
-}
-
-@members {
-   @Override
-   public String getErrorMessage(RecognitionException e, String[] tokenNames) {
-    String msg = null;
-
-    if (e instanceof NoViableAltException) {
-      msg = "character " + getCharErrorDisplay(e.c) + " not supported here";
-    } else {
-      msg = super.getErrorMessage(e, tokenNames);
-    }
-    return msg;
-   }
-
-   @Override
-   public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
-
-     String hdr = getErrorHeader(e);
-     String msg = getErrorMessage(e, tokenNames);
-     throw new TQLParseError(hdr + ":" + msg);
-   }
-}
-
-
-/*
-===============================================================================
-  Tokens for Case Insensitive Keywords
-===============================================================================
-*/
-fragment A
-	:	'A' | 'a';
-
-fragment B
-	:	'B' | 'b';
-
-fragment C
-	:	'C' | 'c';
-
-fragment D
-	:	'D' | 'd';
-
-fragment E
-	:	'E' | 'e';
-
-fragment F
-	:	'F' | 'f';
-
-fragment G
-	:	'G' | 'g';
-
-fragment H
-	:	'H' | 'h';
-
-fragment I
-	:	'I' | 'i';
-
-fragment J
-	:	'J' | 'j';
-
-fragment K
-	:	'K' | 'k';
-
-fragment L
-	:	'L' | 'l';
-
-fragment M
-	:	'M' | 'm';
-
-fragment N
-	:	'N' | 'n';
-
-fragment O
-	:	'O' | 'o';
-
-fragment P
-	:	'P' | 'p';
-
-fragment Q
-	:	'Q' | 'q';
-
-fragment R
-	:	'R' | 'r';
-
-fragment S
-	:	'S' | 's';
-
-fragment T
-	:	'T' | 't';
-
-fragment U
-	:	'U' | 'u';
-
-fragment V
-	:	'V' | 'v';
-
-fragment W
-	:	'W' | 'w';
-
-fragment X
-	:	'X' | 'x';
-
-fragment Y
-	:	'Y' | 'y';
-
-fragment Z
-	:	'Z' | 'z';
-
-/*
-===============================================================================
-  Reserved Keywords
-===============================================================================
-*/
-
-AS : A S;
-ALL : A L L;
-AND : A N D;
-ASC : A S C;
-
-BY : B Y;
-
-CASE : C A S E;
-CHARACTER : C H A R A C T E R;
-COALESCE : C O A L E S C E;
-COUNT : C O U N T;
-CREATE : C R E A T E;
-CROSS : C R O S S;
-CUBE : C U B E;
-
-DESC : D E S C;
-DISTINCT : D I S T I N C T;
-DROP : D R O P;
-
-END : E N D;
-ELSE : E L S E;
-EXCEPT : E X C E P T;
-EXTERNAL : E X T E R N A L;
-FALSE : F A L S E;
-FIRST : F I R S T;
-FORMAT : F O R M A T;
-FULL : F U L L;
-FROM : F R O M;
-
-GROUP : G R O U P;
-
-HAVING : H A V I N G;
-
-IN : I N;
-INDEX : I N D E X;
-INNER : I N N E R;
-INSERT : I N S E R T;
-INTERSECT : I N T E R S E C T;
-INTO : I N T O;
-IS : I S;
-
-JOIN : J O I N;
-
-LAST : L A S T;
-LEFT : L E F T;
-LIKE : L I K E;
-LIMIT : L I M I T;
-LOCATION : L O C A T I O N;
-
-NATIONAL : N A T I O N A L;
-NATURAL : N A T U R A L;
-NOT : N O T;
-NULL : N U L L;
-NULLIF : N U L L I F;
-
-ON : O N;
-OUTER : O U T E R;
-OR : O R;
-ORDER : O R D E R;
-
-PRECISION : P R E C I S I ON;
-
-RIGHT : R I G H T;
-ROLLUP : R O L L U P;
-
-SET : S E T;
-SELECT : S E L E C T;
-
-TABLE : T A B L E;
-THEN : T H E N;
-TRUE : T R U E;
-
-UNION : U N I O N;
-UNIQUE : U N I Q U E;
-UNKNOWN : U N K N O W N;
-USING : U S I N G;
-
-VALUES : V A L U E S;
-VARYING : V A R Y I N G;
-
-WHEN : W H E N;
-WHERE : W H E R E;
-WITH : W I T H;
-
-ZONE : Z O N E;
-
-/*
-===============================================================================
-  Data Type Tokens
-===============================================================================
-*/
-BOOLEAN : B O O L E A N;
-BOOL : B O O L;
-BIT : B I T;
-VARBIT : V A R B I T;
-
-INT1 : I N T '1';
-INT2 : I N T '2';
-INT4 : I N T '4';
-INT8 : I N T '8';
-
-TINYINT : T I N Y I N T; // alias for INT1
-SMALLINT : S M A L L I N T; // alias for INT2
-INT : I N T; // alias for INT4
-INTEGER : I N T E G E R; // alias - INT4
-BIGINT : B I G I N T; // alias for INT8
-
-FLOAT4 : F L O A T '4';
-FLOAT8 : F L O A T '8';
-
-REAL : R E A L; // alias for FLOAT4
-FLOAT : F L O A T; // alias for FLOAT8
-DOUBLE : D O U B L E; // alias for FLOAT8
-
-NUMERIC : N U M E R I C;
-DECIMAL : D E C I M A L; // alias for number
-
-CHAR : C H A R;
-VARCHAR : V A R C H A R;
-NCHAR : N C H A R;
-NVARCHAR : N V A R C H A R;
-
-DATE : D A T E;
-TIME : T I M E;
-TIMETZ : T I M E T Z;
-TIMESTAMP : T I M E S T A M P;
-TIMESTAMPTZ : T I M E S T A M P T Z;
-
-TEXT : T E X T;
-
-BINARY : B I N A R Y;
-VARBINARY : V A R B I N A R Y;
-BLOB : B L O B;
-BYTEA : B Y T E A; // alias for BLOB
-
-INET4 : I N E T '4';
-
-// Operators
-ASSIGN  : ':=';
-EQUAL  : '=';
-SEMI_COLON :  ';';
-COMMA : ',';
-NOT_EQUAL  : '<>' | '!=' | '~='| '^=' ;
-LTH : '<' ;
-LEQ : '<=';
-GTH   : '>';
-GEQ   : '>=';
-LEFT_PAREN :  '(';
-RIGHT_PAREN : ')';
-PLUS  : '+';
-MINUS : '-';
-MULTIPLY: '*';
-DIVIDE  : '/';
-MODULAR : '%';
-DOT : '.';
-
-NUMBER : Digit+;
-
-fragment
-Digit : '0'..'9';
-
-REAL_NUMBER
-    :   ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
-    |   '.' ('0'..'9')+ EXPONENT?
-    |   ('0'..'9')+ EXPONENT
-    ;
-
-COMMENT
-    :   '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
-    |   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
-    ;
-
-/*
-===============================================================================
- Identifiers
-===============================================================================
-*/
-
-Identifier
-  : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|Digit|'_'|':')*
-  ;
-
-/*
-===============================================================================
- Literal
-===============================================================================
-*/
-
-// Some Unicode Character Ranges
-fragment
-Control_Characters                  :   '\u0001' .. '\u001F';
-fragment
-Extended_Control_Characters         :   '\u0080' .. '\u009F';
-
-Character_String_Literal
-    : Quote ( ESC_SEQ | ~('\\'|Quote) )* Quote
-      { setText(getText().substring(1, getText().length()-1)); }
-    | Double_Quote ( ESC_SEQ | ~('\\'|Double_Quote) )* Double_Quote
-      { setText(getText().substring(1, getText().length()-1)); }
-    ;
-
-Quote
-  : '\'';
-
-Double_Quote
-  : '"';
-
-fragment
-EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
-
-fragment
-HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;
-
-fragment
-ESC_SEQ
-    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
-    |   UNICODE_ESC
-    |   OCTAL_ESC
-    ;
-
-fragment
-OCTAL_ESC
-    :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
-    |   '\\' ('0'..'7') ('0'..'7')
-    |   '\\' ('0'..'7')
-    ;
-
-fragment
-UNICODE_ESC
-    :   '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
-    ;
-
-
-/*
-===============================================================================
- Whitespace Tokens
-===============================================================================
-*/
-
-Space : ' '
-{
-	$channel = HIDDEN;
-};
-
-White_Space :	( Control_Characters  | Extended_Control_Characters )+
-{
-	$channel = HIDDEN;
-};
-
-
-BAD : . {
-  skip();
-} ;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/antlr3/org/apache/tajo/engine/parser/SQLParser.g
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/antlr3/org/apache/tajo/engine/parser/SQLParser.g b/tajo-core/tajo-core-backend/src/main/antlr3/org/apache/tajo/engine/parser/SQLParser.g
deleted file mode 100644
index ea4d633..0000000
--- a/tajo-core/tajo-core-backend/src/main/antlr3/org/apache/tajo/engine/parser/SQLParser.g
+++ /dev/null
@@ -1,789 +0,0 @@
-/**
- * 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.
- */
-
-parser grammar SQLParser;
-
-options {
-	language=Java;
-	backtrack=true;
-	memoize=true;
-	output=AST;
-	ASTLabelType=CommonTree;
-	tokenVocab=SQLLexer;
-}
-
-tokens {
-  ALL;
-  COLUMN;
-  COUNT_VAL;
-  COUNT_ROWS;
-  CREATE_INDEX;
-  CREATE_TABLE;
-  DROP_TABLE;
-  DESC_TABLE;
-  EMPTY_GROUPING_SET;
-  FIELD_NAME;
-  FIELD_DEF;
-  FUNCTION;
-  FUNC_ARGS;
-  GROUP_BY;
-  NULL_ORDER;
-  ORDER;
-  ORDER_BY;
-  PARAM;
-  PARAMS;
-  SEL_LIST;
-  SET_QUALIFIER;
-  SHOW_TABLE;
-  SHOW_FUNCTION;
-  SORT_KEY;
-  SORT_SPECIFIERS;
-  STORE;
-  STORE_TYPE;
-  TABLE_DEF;
-  TARGET_FIELDS;
-  VALUES;
-}
-
-@header {
-package org.apache.tajo.engine.parser;
-
-import java.util.List;
-import java.util.ArrayList;
-import org.apache.tajo.engine.query.exception.TQLParseError;
-}
-
-@members {
-   @Override
-   public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
-     int pos = 0;
-     int line = 0;
-     if (e.charPositionInLine < 0 && input.LT(-1) != null) {
-        Token t = input.LT(-1);
-        line = t.getLine();
-        pos = t.getCharPositionInLine();
-     } else {
-        line = e.line;
-        pos = e.charPositionInLine;
-     }
-     String msg = getErrorMessage(e, tokenNames);
-     throw new TQLParseError(msg, line, pos);
-   }
-
-   @Override
-   public String getErrorMessage(RecognitionException e, String[] tokenNames) {
-     String msg = null;
-
-     if (e instanceof NoViableAltException) {
-       msg = "syntax error at or near " + getTokenErrorDisplay(e.token) + (input.LT(2) != null ? " " + getTokenErrorDisplay(input.LT(2)) : "")
-       + (input.LT(3) != null ? " " + getTokenErrorDisplay(input.LT(3)) : "");
-      } else if (e instanceof MismatchedTokenException) {
-       MismatchedTokenException mte = (MismatchedTokenException) e;
-       msg = super.getErrorMessage(e, tokenNames) + (input.LT(-1) == null ? "" : " near '" + input.LT(-1).getText()) + "'";
-      } else if (e instanceof FailedPredicateException) {
-       FailedPredicateException fpe = (FailedPredicateException) e;
-       msg = "Failed to recognize predicate '" + fpe.token.getText() + "'. Failed rule: '" + fpe.ruleName + "'";
-      }else {
-       msg = super.getErrorMessage(e, tokenNames);
-      }
-
-      return msg;
-   }
-}
-
-/*
-===============================================================================
-  <data types>
-===============================================================================
-*/
-
-data_type
-  : boolean_type
-  | bit_type
-  | varbit_type
-  | binary_type
-  | varbinary_type
-  | blob_type
-  | INET4
-  | character_string_type
-  | datetime_type
-  | numeric_type
-  ;
-
-character_string_type
-  : char_type
-  | varchar_type
-  | nchar_type
-  | nvarchar_type
-  | TEXT
-  ;
-
-numeric_type
-  : int1_type
-  | int2_type
-  | int4_type
-  | int8_type
-  | float4_type
-  | float_type
-  | float8_type
-  | number_type
-  ;
-
-datetime_type
-  : DATE
-  | TIME
-  | timetz_type
-  | TIMESTAMP
-  | timestamptz_type
-  ;
-
-precision_param
-  : LEFT_PAREN! NUMBER RIGHT_PAREN!
-  | LEFT_PAREN! NUMBER COMMA! NUMBER RIGHT_PAREN!
-  ;
-type_length
-  : LEFT_PAREN! NUMBER RIGHT_PAREN!
-  ;
-
-boolean_type
-  : BOOLEAN
-  | BOOL -> BOOLEAN
-  ;
-bit_type
-  : BIT type_length? -> BIT
-  ;
-varbit_type
-  : VARBIT type_length? -> VARBIT
-  | BIT VARYING type_length? -> VARBIT
-  ;
-int1_type
-  : INT1
-  | TINYINT -> INT1
-  ;
-int2_type
-  : INT2
-  | SMALLINT -> INT2
-  ;
-int4_type
-  : INT4
-  | INT -> INT4
-  | INTEGER -> INT4
-  ;
-int8_type
-  : INT8
-  | BIGINT -> INT8
-  ;
-float4_type
-  : FLOAT4
-  | REAL -> FLOAT4
-  ;
-float_type : FLOAT type_length? -> ^(FLOAT type_length?);
-float8_type
-  : FLOAT8
-  | DOUBLE -> FLOAT8
-  | DOUBLE PRECISION -> FLOAT8
-  ;
-number_type
-  : NUMERIC (precision_param)? -> NUMERIC
-  | DECIMAL (precision_param)? -> NUMERIC
-  ;
-char_type
-  : CHAR type_length? -> CHAR
-  | CHARACTER type_length? -> CHAR
-  ;
-varchar_type
-  : VARCHAR type_length? -> VARCHAR
-  | CHARACTER VARYING type_length? -> VARCHAR
-  ;
-nchar_type
-  : NCHAR type_length? -> NCHAR
-  | NATIONAL CHARACTER type_length? -> NCHAR
-  ;
-nvarchar_type
-  : NVARCHAR type_length? -> NVARCHAR
-  | NATIONAL CHARACTER VARYING type_length? -> NVARCHAR
-  ;
-timetz_type
-  : TIMETZ
-  | TIME WITH TIME ZONE -> TIMETZ
-  ;
-timestamptz_type
-  : TIMESTAMPTZ
-  | TIMESTAMP WITH TIME ZONE -> TIMESTAMPTZ
-  ;
-binary_type
-  : BINARY type_length?
-  ;
-varbinary_type
-  : VARBINARY type_length?
-  | BINARY VARYING type_length?
-  ;
-blob_type
-  : BLOB
-  | BYTEA -> BLOB
-  ;
-
-/*
-===============================================================================
-  SQL statement (Start Symbol)
-===============================================================================
-*/
-sql
-  : statement EOF
-  ;
-
-statement
-  : data_statement
-  | data_change_statement
-  | schema_statement
-  | index_statement
-  ;
-
-data_statement
-  : query_expression
-  | set_stmt
-  ;
-
-data_change_statement
-  : insert_statement
-  ;
-
-schema_statement
-  : create_table_statement
-  | DROP TABLE table -> ^(DROP_TABLE table)
-  ;
-
-index_statement
-  : CREATE (u=UNIQUE)? INDEX n=Identifier ON t=table (m=method_specifier)?
-    LEFT_PAREN s=sort_specifier_list RIGHT_PAREN p=param_clause?
-    -> ^(CREATE_INDEX $u? $m? $p? $n $t $s)
-  ;
-
-create_table_statement
-  : CREATE EXTERNAL TABLE t=table def=table_elements USING f=Identifier
-    p=param_clause? (LOCATION path=Character_String_Literal)
-      -> ^(CREATE_TABLE $t EXTERNAL ^(TABLE_DEF $def) ^(USING $f) $p?
-         ^(LOCATION $path))
-  | CREATE TABLE t=table (def=table_elements)? (USING s=Identifier)?
-    (p=param_clause)? (AS q=query_expression)?
-      -> ^(CREATE_TABLE $t ^(TABLE_DEF $def)? ^(USING $s)? $p? ^(AS $q)?)
-  ;
-
-table_elements
-  : LEFT_PAREN field_element (COMMA field_element)* RIGHT_PAREN
-    -> field_element+
-  ;
-
-field_element
-  : Identifier field_type -> ^(FIELD_DEF Identifier field_type)
-  ;
-
-field_type
-  : data_type
-  ;
-
-/*
-===============================================================================
-  <insert stmt>
-===============================================================================
-*/
-
-insert_statement
-  : INSERT INTO table (LEFT_PAREN column_reference_list RIGHT_PAREN)? VALUES array
-  -> ^(INSERT ^(TABLE table) ^(VALUES array) ^(TARGET_FIELDS column_reference_list)?)
-  ;
-
-/*
-===============================================================================
-  <query_expression>
-===============================================================================
-*/
-query_expression
-  : query_expression_body
-  ;
-
-query_expression_body
-  : non_join_query_expression
-  | joined_table
-  ;
-
-non_join_query_expression
-  : (non_join_query_term | joined_table (UNION | EXCEPT)^ (ALL|DISTINCT)? query_term)
-    ((UNION | EXCEPT)^ (ALL|DISTINCT)? query_term)*
-  ;
-
-query_term
-  : non_join_query_term
-  | joined_table
-  ;
-
-non_join_query_term
-  : ( non_join_query_primary | joined_table INTERSECT^ (ALL|DISTINCT)? query_primary)
-    (INTERSECT^ (ALL|DISTINCT)? query_primary)*
-  ;
-
-query_primary
-  : non_join_query_primary
-  | joined_table
-  ;
-
-non_join_query_primary
-  : simple_table
-  | LEFT_PAREN non_join_query_expression RIGHT_PAREN
-  ;
-
-simple_table
-  options {k=1;}
-  : query_specification
-  ;
-
-query_specification
-  : SELECT set_qualifier? select_list from_clause? where_clause? groupby_clause? having_clause?
-    orderby_clause? limit_clause?
-    -> ^(SELECT from_clause? set_qualifier? select_list where_clause? groupby_clause?
-    having_clause? orderby_clause? limit_clause?)
-  ;
-
-select_list
-  : MULTIPLY -> ^(SEL_LIST ALL)
-  | derived_column (COMMA derived_column)* -> ^(SEL_LIST derived_column+)
-  ;
-
-set_qualifier
-  : DISTINCT -> ^(SET_QUALIFIER DISTINCT)
-  | ALL -> ^(SET_QUALIFIER ALL)
-  ;
-
-derived_column
-  : boolean_value_expression as_clause? -> ^(COLUMN boolean_value_expression as_clause?)
-  ;
-
-column_reference
-	:	(t=Identifier DOT)? b=Identifier -> ^(FIELD_NAME $b $t?)
-	;
-
-as_clause
-  : (AS)? column_reference
-  ;
-
-column_reference_list
-	:	column_reference (COMMA column_reference)* -> column_reference+
-	;
-
-table
-  : Identifier
-  ;
-
-from_clause
-  : FROM^ table_reference_list
-  ;
-
-table_reference_list
-  :table_reference (COMMA table_reference)* -> table_reference+
-  ;
-
-table_reference
-  : table_primary
-  | joined_table
-  ;
-
-joined_table
-  : table_primary (cross_join | qualified_join | natural_join | union_join)+
-  ;
-
-joined_table_prim
-  : cross_join
-  | qualified_join
-  | natural_join
-  | union_join
-  ;
-
-cross_join
-  : CROSS JOIN r=table_primary -> ^(JOIN CROSS $r)
-  ;
-
-qualified_join
-  : (t=join_type)? JOIN r=table_primary s=join_specification -> ^(JOIN $t? $r $s)
-  ;
-
-natural_join
-  : NATURAL (t=join_type)? JOIN r=table_primary -> ^(JOIN NATURAL $t? $r)
-  ;
-
-union_join
-  : UNION JOIN r=table_primary -> ^(JOIN UNION $r)
-  ;
-
-join_type
-  : INNER
-  | t=outer_join_type OUTER -> ^(OUTER $t)
-  | t=outer_join_type -> ^(OUTER $t)
-  ;
-
-outer_join_type
-  : LEFT
-  | RIGHT
-  | FULL
-  ;
-
-join_specification
-  : join_condition
-  | named_columns_join
-  ;
-
-join_condition
-  : ON^ search_condition
-  ;
-
-named_columns_join
-  : USING LEFT_PAREN f=column_reference_list RIGHT_PAREN -> ^(USING $f)
-  ;
-
-table_primary
-  : table ((AS)? a=Identifier)? -> ^(TABLE table ($a)?)
-  ;
-
-where_clause
-  : WHERE^ search_condition
-  ;
-
-/*
-===============================================================================
-  <routine invocation>
-
-  Invoke an SQL-invoked routine.
-===============================================================================
-*/
-
-routine_invocation
-	: Identifier LEFT_PAREN funcArgs? RIGHT_PAREN -> ^(FUNCTION[$Identifier.text] funcArgs?)
-	| COUNT LEFT_PAREN funcArgs RIGHT_PAREN -> ^(COUNT_VAL funcArgs)
-	| COUNT LEFT_PAREN MULTIPLY RIGHT_PAREN -> ^(COUNT_ROWS)
-	;
-
-funcArgs
-  : boolean_value_expression (COMMA boolean_value_expression)* -> boolean_value_expression+
-  ;
-
-/*
-===============================================================================
-  <groupby clause>
-===============================================================================
-*/
-groupby_clause
-  : GROUP BY g=grouping_element_list -> ^(GROUP_BY $g)
-  ;
-
-grouping_element_list
-  : grouping_element (COMMA grouping_element)* -> grouping_element+
-  ;
-
-grouping_element
-  : ordinary_grouping_set
-  | rollup_list
-  | cube_list
-  | empty_grouping_set
-  ;
-
-ordinary_grouping_set
-  : column_reference_list
-  | LEFT_PAREN! column_reference_list RIGHT_PAREN!
-  ;
-
-rollup_list
-  : ROLLUP LEFT_PAREN c=ordinary_grouping_set RIGHT_PAREN -> ^(ROLLUP $c)
-  ;
-
-cube_list
-  : CUBE LEFT_PAREN c=ordinary_grouping_set RIGHT_PAREN -> ^(CUBE $c)
-  ;
-
-empty_grouping_set
-  : LEFT_PAREN RIGHT_PAREN -> ^(EMPTY_GROUPING_SET)
-  ;
-
-having_clause
-  : HAVING^ boolean_value_expression
-  ;
-
-/*
-===============================================================================
-  <orderby clause>
-
-  Specify a comparison of two row values.
-===============================================================================
-*/
-
-orderby_clause
-  : ORDER BY sort_specifier_list -> ^(ORDER_BY sort_specifier_list)
-  ;
-
-sort_specifier_list
-  : sort_specifier (COMMA sort_specifier)* -> ^(SORT_SPECIFIERS sort_specifier+)
-  ;
-
-sort_specifier
-  : fn=column_reference a=order_specification? o=null_ordering? -> ^(SORT_KEY $fn $a? $o?)
-  ;
-
-order_specification
-  : ASC -> ^(ORDER ASC)
-  | DESC -> ^(ORDER DESC)
-  ;
-
-limit_clause
-  : LIMIT e=numeric_value_expression -> ^(LIMIT $e)
-  ;
-
-null_ordering
-  : NULL FIRST -> ^(NULL_ORDER FIRST)
-  | NULL LAST -> ^(NULL_ORDER LAST)
-  ;
-
-/*
-===============================================================================
-  <set stmt>
-
-  Specify a comparison of two row values.
-===============================================================================
-*/
-
-set_stmt
-	:	SET (UNION|INTERSECT|EXCEPT) table
-	;
-
-search_condition
-	:	boolean_value_expression
-	;
-
-param_clause
-  : WITH LEFT_PAREN param (COMMA param)* RIGHT_PAREN -> ^(PARAMS param+)
-  ;
-
-param
-  : k=Character_String_Literal EQUAL v=numeric_value_expression -> ^(PARAM $k $v)
-  ;
-
-method_specifier
-  : USING m=Identifier -> ^(USING[$m.text])
-  ;
-
-/*
-===============================================================================
-  <boolean value expression>
-===============================================================================
-*/
-
-boolean_value_expression
-	:	and_predicate (OR^ and_predicate)*
-	;
-
-and_predicate
-  :	boolean_factor (AND^ boolean_factor)*
-	;
-
-boolean_factor
-  : boolean_test
-  | NOT boolean_test -> ^(NOT boolean_test)
-  ;
-
-boolean_test
-  : boolean_primary is_clause?
-  ;
-
-is_clause
-  : IS NOT? t=truth_value -> ^(IS NOT? $t)
-  ;
-
-truth_value
-  : TRUE | FALSE | UNKNOWN
-  ;
-
-boolean_primary
-  : predicate
-  | numeric_value_expression
-  | LEFT_PAREN! boolean_value_expression RIGHT_PAREN!
-  | case_expression
-  ;
-
-/*
-===============================================================================
-  <predicate>
-===============================================================================
-*/
-
-predicate
-  : comparison_predicate
-  | in_predicate
-  | like_predicate
-  | null_predicate
-  ;
-
-/*
-===============================================================================
-  <comparison_predicate>
-
-  Specify a comparison of two row values.
-===============================================================================
-*/
-comparison_predicate
-  options{k=1;}
-	:	l=numeric_value_expression c=comp_op r=numeric_value_expression -> ^($c $l $r)
-	;
-
-comp_op
-  : EQUAL
-  | NOT_EQUAL
-  | LTH
-  | LEQ
-  | GTH
-  | GEQ
-  ;
-
-/*
-===============================================================================
-  <in_predicate>
-
-  Specify a quantified comparison.
-===============================================================================
-*/
-
-in_predicate : v=numeric_value_expression  NOT? IN a=in_predicate_value -> ^(IN $v $a NOT?);
-
-in_predicate_value
-  : LEFT_PAREN! in_value_list  RIGHT_PAREN!
-	;
-
-in_value_list
-  : numeric_value_expression  ( COMMA numeric_value_expression  )* -> numeric_value_expression+;
-
-/*
-===============================================================================
-  <like_predicate>
-
-  Specify a pattern-match comparison.
-===============================================================================
-*/
-
-like_predicate
-  : f=column_reference NOT? LIKE s=Character_String_Literal -> ^(LIKE NOT? $f $s)
-  ;
-
-/*
-===============================================================================
-  <null_predicate>
-
-  Specify a test for a null value.
-===============================================================================
-*/
-
-null_predicate
-  : f=numeric_value_expression IS (n=NOT)? NULL -> ^(IS $f NULL $n?)
-  ;
-
-/*
-===============================================================================
-  <numeric_value_expression>
-
-  Specify a comparison of two row values.
-===============================================================================
-*/
-
-numeric_value_expression
-	:	term ((PLUS|MINUS)^ term)*
-	;
-
-term
-  :	numeric_primary ((MULTIPLY|DIVIDE|MODULAR)^ numeric_primary)*
-	;
-
-array
-  : LEFT_PAREN literal (COMMA literal )* RIGHT_PAREN -> literal+
-  ;
-
-numeric_primary
-  :	literal
-	| column_reference
-	|	LEFT_PAREN! numeric_value_expression RIGHT_PAREN!
-	| routine_invocation
-	;
-
-literal
-  : string_value_expr
-  | signed_numerical_literal
-  | NULL
-  ;
-
-string_value_expr
-  : Character_String_Literal
-  ;
-
-signed_numerical_literal
-  : sign? unsigned_numerical_literal
-  ;
-
-unsigned_numerical_literal
-  : NUMBER
-  | REAL_NUMBER
-  ;
-
-sign
-  : PLUS | MINUS
-  ;
-
-/*
-===============================================================================
-  case_expression
-===============================================================================
-*/
-
-case_expression
-  : case_specification
-  ;
-
-case_abbreviation
-  : NULLIF LEFT_PAREN numeric_value_expression COMMA boolean_value_expression  RIGHT_PAREN
-  | COALESCE LEFT_PAREN numeric_value_expression ( COMMA boolean_value_expression  )+ RIGHT_PAREN
-  ;
-
-case_specification
-  : simple_case
-  | searched_case
-  ;
-
-simple_case
-  : CASE numeric_value_expression ( simple_when_clause )+ ( else_clause  )? END
-  ;
-
-searched_case
-  : CASE^ (searched_when_clause)+ (else_clause)? END!
-  ;
-
-simple_when_clause : WHEN numeric_value_expression THEN result ;
-
-searched_when_clause
-  : WHEN c=search_condition THEN r=result -> ^(WHEN $c $r)
-  ;
-
-else_clause
-  : ELSE r=result -> ^(ELSE $r)
-  ;
-
-result
-  : numeric_value_expression | NULL
-  ;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4 b/tajo-core/tajo-core-backend/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4
new file mode 100644
index 0000000..3ae6ad2
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/antlr4/org/apache/tajo/engine/parser/SQLLexer.g4
@@ -0,0 +1,367 @@
+/*
+ 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.
+ */
+
+lexer grammar SQLLexer;
+
+@header {
+}
+
+@members {
+}
+
+
+/*
+===============================================================================
+  Tokens for Case Insensitive Keywords
+===============================================================================
+*/
+fragment A
+	:	'A' | 'a';
+
+fragment B
+	:	'B' | 'b';
+
+fragment C
+	:	'C' | 'c';
+
+fragment D
+	:	'D' | 'd';
+
+fragment E
+	:	'E' | 'e';
+
+fragment F
+	:	'F' | 'f';
+
+fragment G
+	:	'G' | 'g';
+
+fragment H
+	:	'H' | 'h';
+
+fragment I
+	:	'I' | 'i';
+
+fragment J
+	:	'J' | 'j';
+
+fragment K
+	:	'K' | 'k';
+
+fragment L
+	:	'L' | 'l';
+
+fragment M
+	:	'M' | 'm';
+
+fragment N
+	:	'N' | 'n';
+
+fragment O
+	:	'O' | 'o';
+
+fragment P
+	:	'P' | 'p';
+
+fragment Q
+	:	'Q' | 'q';
+
+fragment R
+	:	'R' | 'r';
+
+fragment S
+	:	'S' | 's';
+
+fragment T
+	:	'T' | 't';
+
+fragment U
+	:	'U' | 'u';
+
+fragment V
+	:	'V' | 'v';
+
+fragment W
+	:	'W' | 'w';
+
+fragment X
+	:	'X' | 'x';
+
+fragment Y
+	:	'Y' | 'y';
+
+fragment Z
+	:	'Z' | 'z';
+
+/*
+===============================================================================
+  Reserved Keywords
+===============================================================================
+*/
+
+AS : A S;
+ALL : A L L;
+AND : A N D;
+ANY : A N Y;
+ASC : A S C;
+
+BY : B Y;
+
+CASE : C A S E;
+CHARACTER : C H A R A C T E R;
+COALESCE : C O A L E S C E;
+COUNT : C O U N T;
+CREATE : C R E A T E;
+CROSS : C R O S S;
+CUBE : C U B E;
+
+DEC : D E C;
+DESC : D E S C;
+DISTINCT : D I S T I N C T;
+DROP : D R O P;
+
+END : E N D;
+ELSE : E L S E;
+EXCEPT : E X C E P T;
+EXISTS : E X I S T S;
+EXTERNAL : E X T E R N A L;
+
+FALSE : F A L S E;
+FIRST : F I R S T;
+FORMAT : F O R M A T;
+FULL : F U L L;
+FROM : F R O M;
+
+GROUP : G R O U P;
+
+HAVING : H A V I N G;
+
+IN : I N;
+INDEX : I N D E X;
+INNER : I N N E R;
+INSERT : I N S E R T;
+INTERSECT : I N T E R S E C T;
+INTO : I N T O;
+IS : I S;
+
+JOIN : J O I N;
+
+LAST : L A S T;
+LEFT : L E F T;
+LIKE : L I K E;
+LIMIT : L I M I T;
+LOCATION : L O C A T I O N;
+
+NATIONAL : N A T I O N A L;
+NATURAL : N A T U R A L;
+NOT : N O T;
+NULL : N U L L;
+NULLIF : N U L L I F;
+
+ON : O N;
+OUTER : O U T E R;
+OR : O R;
+ORDER : O R D E R;
+
+PRECISION : P R E C I S I ON;
+
+RIGHT : R I G H T;
+ROLLUP : R O L L U P;
+
+SET : S E T;
+SELECT : S E L E C T;
+SOME : S O M E;
+
+TABLE : T A B L E;
+THEN : T H E N;
+TRUE : T R U E;
+
+UNION : U N I O N;
+UNIQUE : U N I Q U E;
+UNKNOWN : U N K N O W N;
+USING : U S I N G;
+
+VALUES : V A L U E S;
+VARYING : V A R Y I N G;
+
+WHEN : W H E N;
+WHERE : W H E R E;
+WITH : W I T H;
+
+ZONE : Z O N E;
+
+/*
+===============================================================================
+  Data Type Tokens
+===============================================================================
+*/
+BOOLEAN : B O O L E A N;
+BOOL : B O O L;
+BIT : B I T;
+VARBIT : V A R B I T;
+
+INT1 : I N T '1';
+INT2 : I N T '2';
+INT4 : I N T '4';
+INT8 : I N T '8';
+
+TINYINT : T I N Y I N T; // alias for INT1
+SMALLINT : S M A L L I N T; // alias for INT2
+INT : I N T; // alias for INT4
+INTEGER : I N T E G E R; // alias - INT4
+BIGINT : B I G I N T; // alias for INT8
+
+FLOAT4 : F L O A T '4';
+FLOAT8 : F L O A T '8';
+
+REAL : R E A L; // alias for FLOAT4
+FLOAT : F L O A T; // alias for FLOAT8
+DOUBLE : D O U B L E; // alias for FLOAT8
+
+NUMERIC : N U M E R I C;
+DECIMAL : D E C I M A L; // alias for number
+
+CHAR : C H A R;
+VARCHAR : V A R C H A R;
+NCHAR : N C H A R;
+NVARCHAR : N V A R C H A R;
+
+DATE : D A T E;
+TIME : T I M E;
+TIMETZ : T I M E T Z;
+TIMESTAMP : T I M E S T A M P;
+TIMESTAMPTZ : T I M E S T A M P T Z;
+
+TEXT : T E X T;
+
+BINARY : B I N A R Y;
+VARBINARY : V A R B I N A R Y;
+BLOB : B L O B;
+BYTEA : B Y T E A; // alias for BLOB
+
+INET4 : I N E T '4';
+
+// Operators
+ASSIGN  : ':=';
+EQUAL  : '=';
+SEMI_COLON :  ';';
+COMMA : ',';
+NOT_EQUAL  : '<>' | '!=' | '~='| '^=' ;
+LTH : '<' ;
+LEQ : '<=';
+GTH   : '>';
+GEQ   : '>=';
+LEFT_PAREN :  '(';
+RIGHT_PAREN : ')';
+PLUS  : '+';
+MINUS : '-';
+MULTIPLY: '*';
+DIVIDE  : '/';
+MODULAR : '%';
+DOT : '.';
+
+NUMBER : Digit+;
+
+fragment
+Digit : '0'..'9';
+
+REAL_NUMBER
+    :   ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
+    |   '.' ('0'..'9')+ EXPONENT?
+    |   ('0'..'9')+ EXPONENT
+    ;
+
+BlockComment
+    :   '/*' .*? '*/' -> skip
+    ;
+
+LineComment
+    :   '//' ~[\r\n]* -> skip
+    ;
+
+/*
+===============================================================================
+ Identifiers
+===============================================================================
+*/
+
+Identifier
+  : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|Digit|'_'|':')*
+  ;
+
+/*
+===============================================================================
+ Literal
+===============================================================================
+*/
+
+// Some Unicode Character Ranges
+fragment
+Control_Characters                  :   '\u0001' .. '\u001F';
+fragment
+Extended_Control_Characters         :   '\u0080' .. '\u009F';
+
+Character_String_Literal
+  : '\'' ( ESC_SEQ | ~('\\'|'\'') )* '\''
+  | '"' ( ESC_SEQ | ~('\\'|'"') )* '"'
+  ;
+
+fragment
+EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
+
+fragment
+HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;
+
+fragment
+ESC_SEQ
+    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
+    |   UNICODE_ESC
+    |   OCTAL_ESC
+    ;
+
+fragment
+OCTAL_ESC
+    :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
+    |   '\\' ('0'..'7') ('0'..'7')
+    |   '\\' ('0'..'7')
+    ;
+
+fragment
+UNICODE_ESC
+    :   '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
+    ;
+
+
+/*
+===============================================================================
+ Whitespace Tokens
+===============================================================================
+*/
+
+Space
+  : ' ' -> skip
+  ;
+
+White_Space
+  :	( Control_Characters  | Extended_Control_Characters )+ -> skip
+  ;
+
+
+BAD
+  : . -> skip
+  ;

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4 b/tajo-core/tajo-core-backend/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4
new file mode 100644
index 0000000..2354a32
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/antlr4/org/apache/tajo/engine/parser/SQLParser.g4
@@ -0,0 +1,788 @@
+/**
+ * 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.
+ */
+
+parser grammar SQLParser;
+
+options {
+	language=Java;
+	tokenVocab=SQLLexer;
+}
+
+@header {
+}
+
+@members {
+}
+
+/*
+===============================================================================
+  <data types>
+===============================================================================
+*/
+
+data_type
+  : predefined_type
+  ;
+
+predefined_type
+  : character_string_type
+  | national_character_string_type
+  | binary_large_object_string_type
+  | numeric_type
+  | boolean_type
+  | datetime_type
+  | bit_type
+  | binary_type
+  ;
+
+character_string_type
+  : CHARACTER type_length?
+  | CHAR type_length?  |
+  | CHARACTER VARYING type_length?
+  | CHAR VARYING type_length?
+  | VARCHAR type_length?
+  | TEXT
+  ;
+
+type_length
+  : LEFT_PAREN! NUMBER RIGHT_PAREN!
+  ;
+
+national_character_string_type
+  : NATIONAL CHARACTER type_length?
+  | NATIONAL CHAR type_length?
+  | NCHAR type_length?
+  | NATIONAL CHARACTER VARYING type_length?
+  | NATIONAL CHAR VARYING type_length?
+  | NCHAR VARYING type_length?
+  | NVARCHAR type_length?
+  ;
+
+binary_large_object_string_type
+  : BLOB type_length?
+  | BYTEA type_length?
+  ;
+
+numeric_type
+  : exact_numeric_type | approximate_numeric_type
+  ;
+
+exact_numeric_type
+  : NUMERIC (precision_param)?
+  | DECIMAL (precision_param)?
+  | DEC (precision_param)?
+  | INT1
+  | TINYINT
+  | INT2
+  | SMALLINT
+  | INT4
+  | INT
+  | INTEGER
+  | INT8
+  | BIGINT
+  ;
+
+approximate_numeric_type
+  : FLOAT (precision_param)?
+  | FLOAT4
+  | REAL
+  | FLOAT8
+  | DOUBLE
+  | DOUBLE PRECISION
+  ;
+
+precision_param
+  : LEFT_PAREN! precision=NUMBER RIGHT_PAREN!
+  | LEFT_PAREN! precision=NUMBER COMMA! scale=NUMBER RIGHT_PAREN!
+  ;
+
+boolean_type
+  : BOOLEAN
+  | BOOL
+  ;
+
+datetime_type
+  : DATE
+  | TIME
+  | TIME WITH TIME ZONE
+  | TIMETZ
+  | TIMESTAMP
+  | TIMESTAMP WITH TIME ZONE
+  | TIMESTAMPTZ
+  ;
+
+bit_type
+  : BIT type_length?
+  | VARBIT type_length?
+  | BIT VARYING type_length?
+  ;
+
+binary_type
+  : BINARY type_length?
+  | BINARY VARYING type_length?
+  | VARBINARY type_length?
+  ;
+
+/*
+===============================================================================
+  SQL statement (Start Symbol)
+===============================================================================
+*/
+sql
+  : statement EOF
+  ;
+
+statement
+  : data_statement
+  | data_change_statement
+  | schema_statement
+  | index_statement
+  ;
+
+data_statement
+  : query_expression
+  ;
+
+data_change_statement
+  : insert_statement
+  ;
+
+schema_statement
+  : create_table_statement
+  | drop_table_statement
+  ;
+
+index_statement
+  : CREATE (u=UNIQUE)? INDEX n=Identifier ON t=table_name (m=method_specifier)?
+    LEFT_PAREN s=sort_specifier_list RIGHT_PAREN p=param_clause?
+  ;
+
+create_table_statement
+  : CREATE EXTERNAL TABLE table_name table_elements USING file_type=Identifier
+    (param_clause)? (LOCATION path=Character_String_Literal)
+  | CREATE TABLE table_name (table_elements)? (USING file_type=Identifier)?
+    (param_clause)? (AS query_expression)?
+  ;
+
+table_elements
+  : LEFT_PAREN field_element (COMMA field_element)* RIGHT_PAREN
+  ;
+
+field_element
+  : name=Identifier field_type
+  ;
+
+field_type
+  : data_type
+  ;
+
+param_clause
+  : WITH LEFT_PAREN param (COMMA param)* RIGHT_PAREN
+  ;
+
+param
+  : key=Character_String_Literal EQUAL value=numeric_value_expression
+  ;
+
+method_specifier
+  : USING m=Identifier
+  ;
+
+drop_table_statement
+  : DROP TABLE table_name
+  ;
+
+/*
+===============================================================================
+  <insert stmt>
+===============================================================================
+*/
+
+insert_statement
+  : INSERT INTO table_name (LEFT_PAREN column_name_list RIGHT_PAREN)? VALUES array
+  ;
+
+/*
+===============================================================================
+  <query_expression>
+===============================================================================
+*/
+query_expression
+  : query_expression_body
+  ;
+
+query_expression_body
+  : non_join_query_expression
+  | joined_table
+  ;
+
+non_join_query_expression
+  : (non_join_query_term
+  | joined_table (UNION | EXCEPT) (ALL|DISTINCT)? query_term)
+    ((UNION | EXCEPT) (ALL|DISTINCT)? query_term)*
+  ;
+
+query_term
+  : non_join_query_term
+  | joined_table
+  ;
+
+non_join_query_term
+  : ( non_join_query_primary
+  | joined_table INTERSECT (ALL|DISTINCT)? query_primary)
+    (INTERSECT (ALL|DISTINCT)? query_primary)*
+  ;
+
+query_primary
+  : non_join_query_primary
+  | joined_table
+  ;
+
+non_join_query_primary
+  : simple_table
+  | LEFT_PAREN non_join_query_expression RIGHT_PAREN
+  ;
+
+simple_table
+  : query_specification
+  | explicit_table
+  ;
+
+explicit_table
+  : TABLE table_or_query_name 
+  ;
+
+table_or_query_name
+  : table_name
+  | Identifier
+  ;
+
+table_name
+  : Identifier  ( DOT  Identifier (  DOT Identifier )? )?
+  ;
+
+query_specification
+  : SELECT set_qualifier? select_list table_expression?
+  ;
+
+select_list
+  : MULTIPLY
+  | derived_column (COMMA derived_column)*
+  ;
+
+set_qualifier
+  : DISTINCT
+  | ALL
+  ;
+
+derived_column
+  : boolean_value_expression as_clause?
+  ;
+
+column_reference
+  : (tb_name=Identifier DOT)? name=Identifier
+  ;
+
+as_clause
+  : (AS)? Identifier
+  ;
+
+column_reference_list
+  : column_reference (COMMA column_reference)*
+  ;
+
+/*
+===============================================================================
+  <table expression>
+===============================================================================
+*/
+
+table_expression
+  : from_clause
+    where_clause?
+    groupby_clause?
+    having_clause?
+    orderby_clause?
+    limit_clause?
+  ;
+
+/*
+===============================================================================
+  <from clause>
+===============================================================================
+*/
+
+from_clause
+  : FROM table_reference_list
+  ;
+
+table_reference_list
+  :table_reference (COMMA table_reference)*
+  ;
+
+table_reference
+  : joined_table
+  | table_primary
+  ;
+
+joined_table
+//  : table_primary (cross_join | qualified_join | natural_join | union_join)+
+  : table_primary joined_table_primary+
+  ;
+
+joined_table_primary
+  : CROSS JOIN right=table_primary
+  | (t=join_type)? JOIN right=table_primary s=join_specification
+  | NATURAL (t=join_type)? JOIN right=table_primary
+  | UNION JOIN right=table_primary
+  ;
+
+cross_join
+  : CROSS JOIN r=table_primary
+  ;
+
+qualified_join
+  : (t=join_type)? JOIN r=table_primary s=join_specification
+  ;
+
+natural_join
+  : NATURAL (t=join_type)? JOIN r=table_primary
+  ;
+
+union_join
+  : UNION JOIN r=table_primary
+  ;
+
+join_type
+  : INNER
+  | t=outer_join_type
+  ;
+
+outer_join_type
+  : outer_join_type_part2 OUTER?
+  ;
+
+outer_join_type_part2
+  : LEFT
+  | RIGHT
+  | FULL
+  ;
+
+join_specification
+  : join_condition
+  | named_columns_join
+  ;
+
+join_condition
+  : ON^ search_condition
+  ;
+
+named_columns_join
+  : USING LEFT_PAREN f=column_reference_list RIGHT_PAREN
+  ;
+
+table_primary
+  : table_or_query_name ((AS)? alias=Identifier)? (LEFT_PAREN column_name_list RIGHT_PAREN)?
+  | derived_table (AS)? name=Identifier (LEFT_PAREN column_name_list RIGHT_PAREN)?
+  ;
+
+column_name_list
+  :  Identifier  ( COMMA Identifier  )*
+  ;
+
+derived_table
+  : table_subquery
+  ;
+
+where_clause
+  : WHERE^ search_condition
+  ;
+
+search_condition
+  : boolean_value_expression
+  ;
+
+/*
+==============================================================================================
+  <subquery>
+
+  Specify a scalar value, a row, or a table derived from a query_expression .
+==============================================================================================
+*/
+
+scalar_subquery
+  :  subquery
+  ;
+
+row_subquery
+  :  subquery
+  ;
+
+table_subquery
+  : subquery
+  ;
+
+subquery
+	:  LEFT_PAREN! query_expression RIGHT_PAREN!
+	;
+
+/*
+===============================================================================
+  <routine invocation>
+
+  Invoke an SQL-invoked routine.
+===============================================================================
+*/
+
+routine_invocation
+  : COUNT LEFT_PAREN sql_argument_list RIGHT_PAREN
+  | COUNT LEFT_PAREN MULTIPLY RIGHT_PAREN
+  | Identifier LEFT_PAREN sql_argument_list? RIGHT_PAREN
+  ;
+
+sql_argument_list
+  : boolean_value_expression (COMMA boolean_value_expression)*
+  ;
+
+/*
+===============================================================================
+  <groupby clause>
+===============================================================================
+*/
+groupby_clause
+  : GROUP BY g=grouping_element_list
+  ;
+
+grouping_element_list
+  : grouping_element (COMMA grouping_element)*
+  ;
+
+grouping_element
+  : ordinary_grouping_set
+  | rollup_list
+  | cube_list
+  | empty_grouping_set
+  ;
+
+ordinary_grouping_set
+  : column_reference_list
+  | LEFT_PAREN column_reference_list RIGHT_PAREN
+  ;
+
+rollup_list
+  : ROLLUP LEFT_PAREN c=ordinary_grouping_set RIGHT_PAREN
+  ;
+
+cube_list
+  : CUBE LEFT_PAREN c=ordinary_grouping_set RIGHT_PAREN
+  ;
+
+empty_grouping_set
+  : LEFT_PAREN RIGHT_PAREN
+  ;
+
+having_clause
+  : HAVING^ boolean_value_expression
+  ;
+
+/*
+===============================================================================
+  <orderby clause>
+
+  Specify a comparison of two row values.
+===============================================================================
+*/
+
+orderby_clause
+  : ORDER BY sort_specifier_list
+  ;
+
+sort_specifier_list
+  : sort_specifier (COMMA sort_specifier)*
+  ;
+
+sort_specifier
+  : column=column_reference order=order_specification? null_order=null_ordering?
+  ;
+
+order_specification
+  : ASC
+  | DESC
+  ;
+
+limit_clause
+  : LIMIT e=numeric_value_expression
+  ;
+
+null_ordering
+  : NULL FIRST
+  | NULL LAST
+  ;
+
+/*
+===============================================================================
+  <boolean value expression>
+===============================================================================
+*/
+
+boolean_value_expression
+  : and_predicate (OR^ and_predicate)*
+  ;
+
+and_predicate
+  : boolean_factor (AND^ boolean_factor)*
+  ;
+
+boolean_factor
+  : boolean_test
+  | NOT boolean_test
+  ;
+
+boolean_test
+  : boolean_primary is_clause?
+  ;
+
+is_clause
+  : IS NOT? t=truth_value
+  ;
+
+truth_value
+  : TRUE | FALSE | UNKNOWN
+  ;
+
+boolean_primary
+  : predicate
+  | numeric_value_expression  
+  | case_expression
+  | LEFT_PAREN! boolean_value_expression RIGHT_PAREN!
+  ;
+
+/*
+===============================================================================
+  <predicate>
+===============================================================================
+*/
+
+predicate
+  : comparison_predicate
+  | in_predicate
+  | like_predicate
+  | null_predicate
+  ;
+
+/*
+===============================================================================
+  <comparison_predicate>
+
+  Specify a comparison of two row values.
+===============================================================================
+*/
+comparison_predicate
+  : left=numeric_value_expression c=comp_op right=numeric_value_expression
+  ;
+
+comp_op
+  : EQUAL
+  | NOT_EQUAL
+  | LTH
+  | LEQ
+  | GTH
+  | GEQ
+  ;
+
+/*
+===============================================================================
+  <in_predicate>
+
+  Specify a quantified comparison.
+===============================================================================
+*/
+
+in_predicate
+  : predicand=numeric_value_expression  NOT? IN in_predicate_value
+  ;
+
+
+in_predicate_value
+  : table_subquery
+  | LEFT_PAREN! in_value_list  RIGHT_PAREN!
+  ;
+
+in_value_list
+  : numeric_value_expression  ( COMMA numeric_value_expression  )*
+  ;
+
+/*
+===============================================================================
+  <like_predicate>
+
+  Specify a pattern-match comparison.
+===============================================================================
+*/
+
+like_predicate
+  : f=column_reference NOT? LIKE s=Character_String_Literal
+  ;
+
+/*
+===============================================================================
+  <null_predicate>
+
+  Specify a test for a null value.
+===============================================================================
+*/
+
+null_predicate
+  : predicand=numeric_value_expression IS (n=NOT)? NULL
+  ;
+
+/*
+==============================================================================================
+  <quantified_comparison_predicate>
+
+  Specify a quantified comparison.
+==============================================================================================
+*/
+
+quantified_comparison_predicate
+  : l=numeric_value_expression  c=comp_op q=quantifier s=table_subquery
+  ;
+
+quantifier : all  | some ;
+
+all : ALL;
+
+some : SOME | ANY;
+
+/*
+==============================================================================================
+  <exists_predicate>
+
+  Specify a test for a non_empty set.
+==============================================================================================
+*/
+
+exists_predicate
+  : EXISTS s=table_subquery
+  ;
+
+
+/*
+==============================================================================================
+  unique_predicate
+
+  Specify a test for the absence of duplicate rows
+==============================================================================================
+*/
+
+unique_predicate
+  : UNIQUE s=table_subquery
+  ;
+
+/*
+===============================================================================
+  <numeric_value_expression>
+
+  Specify a comparison of two row values.
+===============================================================================
+*/
+
+numeric_value_expression
+  : left=term ((PLUS|MINUS) right=term)*
+  ;
+
+term
+  : left=numeric_primary ((MULTIPLY|DIVIDE|MODULAR) right=numeric_primary)*
+  ;
+
+array
+  : LEFT_PAREN numeric_value_expression (COMMA numeric_value_expression )* RIGHT_PAREN
+  ;
+
+numeric_primary
+  : literal
+  | column_reference  
+  | routine_invocation
+  | scalar_subquery
+  | LEFT_PAREN! numeric_value_expression RIGHT_PAREN!
+  ;
+
+literal
+  : string_value_expr
+  | signed_numerical_literal
+  | NULL
+  ;
+
+string_value_expr
+  : Character_String_Literal
+  ;
+
+signed_numerical_literal
+  : sign? unsigned_numerical_literal
+  ;
+
+unsigned_numerical_literal
+  : NUMBER
+  | REAL_NUMBER
+  ;
+
+sign
+  : PLUS | MINUS
+  ;
+
+/*
+===============================================================================
+  case_expression
+===============================================================================
+*/
+
+case_expression
+  : case_specification
+  ;
+
+case_abbreviation
+  : NULLIF LEFT_PAREN numeric_value_expression COMMA boolean_value_expression  RIGHT_PAREN
+  | COALESCE LEFT_PAREN numeric_value_expression ( COMMA boolean_value_expression  )+ RIGHT_PAREN
+  ;
+
+case_specification
+  : simple_case
+  | searched_case
+  ;
+
+simple_case
+  : CASE boolean_value_expression ( simple_when_clause )+ ( else_clause  )? END
+  ;
+
+searched_case
+  : CASE (searched_when_clause)+ (else_clause)? END
+  ;
+
+simple_when_clause : WHEN numeric_value_expression THEN result ;
+
+searched_when_clause
+  : WHEN c=search_condition THEN r=result
+  ;
+
+else_clause
+  : ELSE r=result
+  ;
+
+result
+  : numeric_value_expression | NULL
+  ;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/benchmark/TPCH.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/benchmark/TPCH.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/benchmark/TPCH.java
index 811cf95..79915bb 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/benchmark/TPCH.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/benchmark/TPCH.java
@@ -104,7 +104,7 @@ public class TPCH extends BenchmarkSet {
     schemas.put(REGION, region);
 
     Schema orders = new Schema()
-        .addColumn("o_orderkey", Type.INT4) // 0
+        .addColumn("o_orderkey", Type.INT8) // 0
         .addColumn("o_custkey", Type.INT4) // 1
         .addColumn("o_orderstatus", Type.TEXT) // 2
         .addColumn("o_totalprice", Type.FLOAT4) // 3

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/client/TajoClient.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/client/TajoClient.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/client/TajoClient.java
index 6b5832c..8317065 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/client/TajoClient.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/client/TajoClient.java
@@ -106,17 +106,20 @@ public class TajoClient {
    *
    * @return If failed, return null.
    */
-  public ResultSet executeQueryAndGetResult(String tql)
+  public ResultSet executeQueryAndGetResult(String sql)
       throws ServiceException, IOException {
     QueryRequest.Builder builder = QueryRequest.newBuilder();
-    builder.setQuery(tql);
+    builder.setQuery(sql);
     SubmitQueryRespose response = service.submitQuery(null, builder.build());
-    QueryId queryId = new QueryId(response.getQueryId());
-    if (queryId.equals(TajoIdUtils.NullQueryId)) {
-      return null;
+    if (response.getResultCode() == ResultCode.OK) {
+      QueryId queryId = new QueryId(response.getQueryId());
+      if (queryId.equals(TajoIdUtils.NullQueryId)) {
+        return null;
+      }
+      return getQueryResultAndWait(queryId);
+    } else {
+      throw new ServiceException(response.getErrorMessage());
     }
-
-    return getQueryResultAndWait(queryId);
   }
 
   public QueryStatus getQueryStatus(QueryId queryId) throws ServiceException {
@@ -241,10 +244,6 @@ public class TajoClient {
     return service.dropTable(null, builder.build()).getValue();
   }
 
-  public List<String> getClusterInfo() {
-    return null;
-  }
-
   /**
    * Get a list of table names. All table and column names are
    * represented as lower-case letters.

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/eval/EvalTreeUtil.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/eval/EvalTreeUtil.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/eval/EvalTreeUtil.java
index 4686dc1..bde2df5 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/eval/EvalTreeUtil.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/eval/EvalTreeUtil.java
@@ -25,7 +25,7 @@ import org.apache.tajo.catalog.Column;
 import org.apache.tajo.catalog.Schema;
 import org.apache.tajo.common.TajoDataTypes.DataType;
 import org.apache.tajo.engine.eval.EvalNode.Type;
-import org.apache.tajo.engine.parser.QueryBlock.Target;
+import org.apache.tajo.engine.planner.Target;
 import org.apache.tajo.engine.utils.SchemaUtil;
 import org.apache.tajo.exception.InternalException;
 
@@ -58,15 +58,15 @@ public class EvalTreeUtil {
   /**
    * Convert a list of conjunctive normal forms into a singleton expression.
    *  
-   * @param evalNode
-   * @return
+   * @param cnfExprs
+   * @return The EvalNode object that merges all CNF-formed expressions.
    */
-  public static EvalNode transformCNF2Singleton(EvalNode...evalNode) {    
-    if (evalNode.length == 1) {
-      return evalNode[0];
+  public static EvalNode transformCNF2Singleton(EvalNode...cnfExprs) {
+    if (cnfExprs.length == 1) {
+      return cnfExprs[0];
     }
     
-    return transformCNF2Singleton_(evalNode, 0);
+    return transformCNF2Singleton_(cnfExprs, 0);
   }
   
   private static EvalNode transformCNF2Singleton_(EvalNode [] evalNode, int idx) {
@@ -81,12 +81,12 @@ public class EvalTreeUtil {
   /**
    * Get a list of exprs similar to CNF
    * 
-   * @param node
-   * @return
+   * @param expr The expression to be transformed to an array of CNF-formed expressions.
+   * @return An array of CNF-formed expressions
    */
-  public static EvalNode [] getConjNormalForm(EvalNode node) {
+  public static EvalNode [] getConjNormalForm(EvalNode expr) {
     List<EvalNode> list = new ArrayList<EvalNode>();    
-    getConjNormalForm(node, list);    
+    getConjNormalForm(expr, list);
     return list.toArray(new EvalNode[list.size()]);
   }
   
@@ -99,26 +99,6 @@ public class EvalTreeUtil {
     }
   }
   
-  /**
-   * Compute a schema from a list of exprs.
-   * 
-   * @param inputSchema
-   * @param evalNodes
-   * @return
-   * @throws InternalException
-   */
-  public static Schema getSchemaByExprs(Schema inputSchema, EvalNode [] evalNodes) 
-      throws InternalException {
-    Schema schema = new Schema();
-    for (EvalNode expr : evalNodes) {
-      schema.addColumn(
-          expr.getName(),
-          getDomainByExpr(inputSchema, expr)[0]);
-    }
-    
-    return schema;
-  }
-  
   public static Schema getSchemaByTargets(Schema inputSchema, Target [] targets) 
       throws InternalException {
     Schema schema = new Schema();
@@ -203,10 +183,6 @@ public class EvalTreeUtil {
   /**
    * Examine if the expr contains the column reference corresponding 
    * to the target column
-   * 
-   * @param expr
-   * @param target
-   * @return
    */
   public static boolean containColumnRef(EvalNode expr, Column target) {
     Set<EvalNode> exprSet = Sets.newHashSet();

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/exception/NTAQueryException.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/exception/NTAQueryException.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/exception/NTAQueryException.java
deleted file mode 100644
index f280e50..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/exception/NTAQueryException.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/**
- * 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.engine.exception;
-
-import java.io.IOException;
-
-public class NTAQueryException extends IOException {
-	private static final long serialVersionUID = -5012296598261064705L;
-
-	public NTAQueryException() {
-	}
-	
-	public NTAQueryException(Exception e) {
-		super(e);
-	}
-
-	/**
-	 * @param query
-	 */
-	public NTAQueryException(String query) {
-		super(query);
-	}
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/builtin/CountValue.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/builtin/CountValue.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/builtin/CountValue.java
index 46ccc3b..2b5fb80 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/builtin/CountValue.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/builtin/CountValue.java
@@ -24,6 +24,9 @@ import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.datum.NullDatum;
 import org.apache.tajo.storage.Tuple;
 
+/**
+ * Count(column) function
+ */
 public final class CountValue extends CountRows {
 
   public CountValue() {
@@ -33,7 +36,7 @@ public final class CountValue extends CountRows {
   }
   @Override
   public void eval(FunctionContext ctx, Tuple params) {
-    if (params.get(0) instanceof NullDatum) {
+    if (!(params.get(0) instanceof NullDatum)) {
       ((CountRowContext) ctx).count++;
     }
   }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/json/FromTableDeserializer.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/json/FromTableDeserializer.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/json/FromTableDeserializer.java
index aa4bbbf..4a5b2ac 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/json/FromTableDeserializer.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/json/FromTableDeserializer.java
@@ -24,7 +24,7 @@ package org.apache.tajo.engine.json;
 import com.google.gson.*;
 import org.apache.tajo.catalog.TableDesc;
 import org.apache.tajo.catalog.TableDescImpl;
-import org.apache.tajo.engine.parser.QueryBlock.FromTable;
+import org.apache.tajo.engine.planner.FromTable;
 import org.apache.tajo.storage.Fragment;
 
 import java.lang.reflect.Type;

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CommandType.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CommandType.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CommandType.java
deleted file mode 100644
index 911da97..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CommandType.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/**
- * 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.engine.parser;
-
-public enum CommandType {
-	// Select
-	SELECT,
-	
-	// Update
-	INSERT,
-	UPDATE,
-	DELETE,
-	
-	// Schema
-	CREATE_TABLE,
-	DROP_TABLE,
-	
-	// Control
-	SHOW_TABLES,
-	DESC_TABLE,
-	SHOW_FUNCTION;
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CopyStmt.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CopyStmt.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CopyStmt.java
deleted file mode 100644
index cef63cf..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CopyStmt.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/**
- * 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.engine.parser;
-
-import org.apache.hadoop.fs.Path;
-import org.apache.tajo.catalog.Options;
-import org.apache.tajo.engine.planner.PlanningContext;
-
-import static org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
-
-public class CopyStmt extends ParseTree {
-  private String tableName;
-  private Path path;
-  private StoreType storeType;
-  private Options params = null;
-
-  public CopyStmt(PlanningContext context) {
-    super(context, StatementType.COPY);
-  }
-
-  public void setTableName(String tableName) {
-    this.tableName = tableName;
-  }
-
-  public String getTableName() {
-    return this.tableName;
-  }
-
-  public void setPath(Path path) {
-    this.path = path;
-  }
-
-  public Path getPath() {
-    return this.path;
-  }
-
-  public void setStoreType(StoreType storeType) {
-    this.storeType = storeType;
-  }
-
-  public StoreType getStoreType() {
-    return this.storeType;
-  }
-
-  public boolean hasParams() {
-    return this.params != null;
-  }
-
-  public void setParams(Options params) {
-    this.params = params;
-  }
-
-  public Options getParams() {
-    return this.params;
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CreateIndexStmt.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CreateIndexStmt.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CreateIndexStmt.java
deleted file mode 100644
index 447aeb0..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CreateIndexStmt.java
+++ /dev/null
@@ -1,107 +0,0 @@
-/**
- * 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.engine.parser;
-
-import com.google.common.base.Preconditions;
-import org.apache.tajo.catalog.Options;
-import org.apache.tajo.catalog.SortSpec;
-import org.apache.tajo.catalog.proto.CatalogProtos.IndexMethod;
-import org.apache.tajo.engine.planner.PlanningContext;
-
-public class CreateIndexStmt extends ParseTree {
-  private String idxName;
-  private boolean unique = false;
-  private String tableName;
-  private IndexMethod method = IndexMethod.TWO_LEVEL_BIN_TREE;
-  private SortSpec[] sortSpecs;
-  private Options params = null;
-
-  public CreateIndexStmt(final PlanningContext context) {
-    super(context, StatementType.CREATE_INDEX);
-  }
-
-  public CreateIndexStmt(final PlanningContext context, String idxName,
-                         boolean unique, String tableName,
-                         SortSpec [] sortSpecs) {
-    this(context);
-    this.idxName = idxName;
-    this.unique = unique;
-    this.tableName = tableName;
-    this.sortSpecs = sortSpecs;
-  }
-  
-  public void setIndexName(String name) {
-    this.idxName = name;
-  }
-  
-  public String getIndexName() {
-    return this.idxName;
-  }
-  
-  public boolean isUnique() {
-    return this.unique;
-  }
-  
-  public void setUnique() {
-    this.unique = true;
-  }
-  
-  public void setTableName(String tableName) {
-    this.tableName = tableName;
-    addTableRef(tableName, tableName);
-  }
-  
-  public String getTableName() {
-    return this.tableName;
-  }
-  
-  public void setMethod(IndexMethod method) {
-    this.method = method;
-  }
-  
-  public IndexMethod getMethod() {
-    return this.method;
-  }
-  
-  public void setSortSpecs(SortSpec[] sortSpecs) {
-    Preconditions.checkNotNull(sortSpecs);
-    Preconditions.checkArgument(sortSpecs.length > 0,
-        "Sort specifiers must be at least one");
-    this.sortSpecs = sortSpecs;
-  }
-  
-  public SortSpec[] getSortSpecs() {
-    return this.sortSpecs;
-  }
-  
-  public boolean hasParams() {
-    return this.params != null;
-  }
-  
-  public void setParams(Options params) {
-    this.params = params;
-  }
-  
-  public Options getParams() {
-    return this.params;
-  }
-}
\ No newline at end of file


[06/13] TAJO-87: Integration of tajo algebra module and SQL parser

Posted by hy...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/parser/TestSQLParser.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/parser/TestSQLParser.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/parser/TestSQLParser.java
deleted file mode 100644
index 3ba0004..0000000
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/parser/TestSQLParser.java
+++ /dev/null
@@ -1,993 +0,0 @@
-/**
- * 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.engine.parser;
-
-import org.antlr.runtime.ANTLRStringStream;
-import org.antlr.runtime.CommonTokenStream;
-import org.antlr.runtime.RecognitionException;
-import org.antlr.runtime.tree.CommonTree;
-import org.antlr.runtime.tree.Tree;
-import org.junit.Test;
-import org.apache.tajo.engine.query.exception.TQLSyntaxError;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-
-public class TestSQLParser {
-  static final String[] selQueries = {
-      "select id, name, age, gender from people", // 0
-      "select title, ISBN from Books", // 1
-      "select studentId from students", // 2
-      "select id, name, age, gender from people as p, students as s", // 4
-      "select name, addr, sum(score) from students group by name, addr", // 5
-      "select name, addr, age from people where age > 30", // 6
-      "select name, addr, age from people where age = 30", // 7
-      "select name as n, sum(score, 3+4, 3>4) as total, 3+4 as id from people where age = 30", // 8
-      "select ipv4:src_ip from test", // 9
-      "select distinct id, name, age, gender from people", // 10
-      "select all id, name, age, gender from people", // 11
-  };
-
-  static final String[] insQueries = {
-      "insert into people values (1, 'hyunsik', 32)",
-      "insert into people (id, name, age) values (1, 'hyunsik', 32)" };
-
-  public static Tree parseQuery(String query) throws TQLSyntaxError {
-    ANTLRStringStream input = new ANTLRStringStream(query);
-    SQLLexer lexer = new SQLLexer(input);
-    CommonTokenStream tokens = new CommonTokenStream(lexer);
-    SQLParser parser = new SQLParser(tokens);
-
-    Tree tree;
-    try {
-      tree = ((Tree) parser.statement().getTree());
-    } catch (RecognitionException e) {
-      throw new TQLSyntaxError(query, e.getMessage());
-    }
-
-    return tree;
-  }
-
-  @Test
-  public void testSelectClause() throws RecognitionException,
-      TQLSyntaxError {
-    Tree tree = parseQuery(selQueries[0]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-  }
-  
-  private final String groupingClause [] = {
-      "select col0, col1, col2, col3, sum(col4) as total, avg(col5) from base group by col0, cube (col1, col2), rollup(col3) having total > 100"
-  };
-  
-  @Test
-  public void testCubeByClause() throws RecognitionException {
-    Tree ast = parseQuery(groupingClause[0]);
-    assertEquals(SQLParser.SELECT, ast.getType());
-    int idx = 0;    
-    assertEquals(SQLParser.FROM, ast.getChild(idx++).getType());
-    assertEquals(SQLParser.SEL_LIST, ast.getChild(idx++).getType());
-    assertEquals(SQLParser.GROUP_BY, ast.getChild(idx).getType());
-    
-    Tree groupby = ast.getChild(idx);
-    int grpIdx = 0;
-    assertEquals(SQLParser.FIELD_NAME, groupby.getChild(grpIdx++).getType());
-    assertEquals(SQLParser.CUBE, groupby.getChild(grpIdx).getType());
-    
-    int cubeIdx = 0;
-    Tree cube = groupby.getChild(grpIdx);
-    assertEquals(SQLParser.FIELD_NAME, cube.getChild(cubeIdx++).getType());
-    assertEquals(SQLParser.FIELD_NAME, cube.getChild(cubeIdx).getType());
-    grpIdx++;
-    assertEquals(SQLParser.ROLLUP, groupby.getChild(grpIdx).getType());
-    
-    int rollupIdx = 0;
-    Tree rollup = groupby.getChild(grpIdx);
-    assertEquals(SQLParser.FIELD_NAME, rollup.getChild(rollupIdx).getType());
-    
-    idx++;
-    assertEquals(SQLParser.HAVING, ast.getChild(idx).getType());
-  }
-
-  @Test
-  public void testColumnFamily() throws TQLSyntaxError {
-    Tree ast = parseQuery(selQueries[8]);
-    assertEquals(SQLParser.SELECT, ast.getType());
-    assertEquals(SQLParser.SEL_LIST, ast.getChild(1).getType());
-    assertEquals(SQLParser.COLUMN, ast.getChild(1).getChild(0).getType());
-    assertEquals(SQLParser.FIELD_NAME, ast.getChild(1).getChild(0).getChild(0)
-        .getType());
-    FieldName fieldName = new FieldName(ast.getChild(1).getChild(0).getChild(0));
-    assertEquals("ipv4", fieldName.getFamilyName());
-    assertEquals("src_ip", fieldName.getSimpleName());
-    assertEquals("ipv4:src_ip", fieldName.getName());
-  }
-
-  @Test
-  public void testSetQualifier() throws TQLSyntaxError {
-    Tree ast = parseQuery(selQueries[9]);
-    assertEquals(SQLParser.SELECT, ast.getType());
-    assertEquals(SQLParser.SET_QUALIFIER, ast.getChild(1).getType());
-    assertEquals(SQLParser.DISTINCT, ast.getChild(1).getChild(0).getType());
-    assertSetListofSetQualifierTest(ast);
-
-    ast = parseQuery(selQueries[10]);
-    assertEquals(SQLParser.SELECT, ast.getType());
-    assertEquals(SQLParser.SET_QUALIFIER, ast.getChild(1).getType());
-    assertEquals(SQLParser.ALL, ast.getChild(1).getChild(0).getType());
-    assertSetListofSetQualifierTest(ast);
-  }
-
-  private void assertSetListofSetQualifierTest(Tree ast) {
-    assertEquals(SQLParser.SEL_LIST, ast.getChild(2).getType());
-    assertEquals(SQLParser.COLUMN, ast.getChild(2).getChild(0).getType());
-    assertEquals(SQLParser.FIELD_NAME, ast.getChild(2).getChild(0).getChild(0)
-        .getType());
-  }
-
-  @Test
-  public void testWhereClause() throws RecognitionException, TQLSyntaxError {
-    Tree tree = parseQuery(selQueries[5]);
-
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    tree = tree.getChild(2).getChild(0);
-
-    assertEquals(tree.getType(), SQLParser.GTH);
-    assertEquals(tree.getChild(0).getType(), SQLParser.FIELD_NAME);
-    FieldName fieldName = new FieldName(tree.getChild(0));
-    assertEquals(fieldName.getName(), "age");
-    assertEquals(tree.getChild(1).getType(), SQLParser.NUMBER);
-    assertEquals(tree.getChild(1).getText(), "30");
-  }
-
-  @Test
-  public void testInsertClause() throws RecognitionException,
-      TQLSyntaxError {
-    Tree tree = parseQuery(insQueries[0]);
-    assertEquals(tree.getType(), SQLParser.INSERT);
-  }
-  
-  static String [] JOINS = {
-    "select name, addr from people natural join student natural join professor", // 0
-    "select name, addr from people inner join student on people.name = student.name", // 1
-    "select name, addr from people inner join student using (id, name)", // 2
-    "select name, addr from people join student using (id, name)", // 3
-    "select name, addr from people cross join student", // 4
-    "select name, addr from people left outer join student on people.name = student.name", // 5
-    "select name, addr from people right outer join student on people.name = student.name", // 6
-    "select * from table1 " +
-        "cross join table2 " +
-        "join table3 on table1.id = table3.id " +
-        "inner join table4 on table1.id = table4.id " +
-        "left outer join table5 on table1.id = table5.id " +
-        "right outer join table6 on table1.id = table6.id " +
-        "full outer join table7 on table1.id = table7.id " +
-        "natural join table8 " +
-        "natural inner join table9 " +
-        "natural left outer join table10 " +
-        "natural right outer join table11 " +
-        "natural full outer join table12 ",
-         // 7 - all possible join clauses*/
-    "select s_acctbal, s_name, n_name, p_partkey, p_mfgr, s_address, s_phone, s_comment, ps_supplycost " + // 8
-      "from region join nation on n_regionkey = r_regionkey and r_name = 'EUROPE' " +
-      "join supplier on s_nationekey = n_nationkey " +
-      "join partsupp on s_suppkey = ps_ps_suppkey " +
-      "join part on p_partkey = ps_partkey and p_type like '%BRASS' and p_size = 15"
-  };
-  
-  @Test
-  /**
-   *                 from
-   *        ---------------------
-   *       /     |               \
-   *      /      \                \
-   *   TABLE     join              join
-   *   /       /     \            /     \
-   *  people  NATURAL TABLE     NATURAL TABLE
-   *                    /                 /
-   *                  student          professor
-   */
-  public void testNaturalJoinClause() {
-    Tree tree = parseQuery(JOINS[0]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.FROM, tree.getChild(0).getType());
-    CommonTree fromAST = (CommonTree) tree.getChild(0);
-    assertEquals(SQLParser.TABLE, fromAST.getChild(0).getType());
-    assertEquals("people", fromAST.getChild(0).getChild(0).getText());
-    assertEquals(SQLParser.JOIN, fromAST.getChild(1).getType());
-    assertEquals(SQLParser.NATURAL, fromAST.getChild(1).getChild(0).getType());
-    assertEquals("student", fromAST.getChild(1).getChild(1).getChild(0).getText());
-    assertEquals(SQLParser.JOIN, fromAST.getChild(2).getType());
-    assertEquals(SQLParser.NATURAL, fromAST.getChild(2).getChild(0).getType());
-    assertEquals("professor", fromAST.getChild(2).getChild(1).getChild(0).getText());
-  }
-  
-  @Test
-  public void testInnerJoinClause() {
-    Tree tree = parseQuery(JOINS[1]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.FROM, tree.getChild(0).getType());
-    CommonTree fromAST = (CommonTree) tree.getChild(0);
-    assert2WayJoinAST(fromAST, SQLParser.INNER);
-    assertEquals(SQLParser.ON, fromAST.getChild(1).getChild(2).getType());
-    
-    tree = parseQuery(JOINS[2]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    fromAST = (CommonTree) tree.getChild(0);
-    assert2WayJoinAST(fromAST, SQLParser.INNER);
-    assertEquals(SQLParser.USING, fromAST.getChild(1).getChild(2).getType());
-    assertEquals(2, fromAST.getChild(1).getChild(2).getChildCount());
-
-    /**
-     *       from
-     *       /     \
-     *     TABLE          join
-     *      /       /             \
-     *    people   TABLE        using
-     *              /       /              \
-     *            student  FIELD_NAME  FIELD_NAME
-     *                      /              \
-     *                     id              name
-     */
-    tree = parseQuery(JOINS[3]);
-    fromAST = (CommonTree) tree.getChild(0);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.TABLE, fromAST.getChild(0).getType());
-    assertEquals("people", fromAST.getChild(0).getChild(0).getText());
-    assertEquals(SQLParser.JOIN, fromAST.getChild(1).getType());
-    assertEquals(SQLParser.TABLE, fromAST.getChild(1).getChild(0).getType());
-    assertEquals("student", fromAST.getChild(1).getChild(0).getChild(0).getText());
-    assertEquals(SQLParser.USING, fromAST.getChild(1).getChild(1).getType());
-    assertEquals(2, fromAST.getChild(1).getChild(1).getChildCount());
-  }
-
-  private void assert2WayJoinAST(Tree fromAST, int joinType) {
-    assertEquals(SQLParser.TABLE, fromAST.getChild(0).getType());
-    assertEquals("people", fromAST.getChild(0).getChild(0).getText());
-    assertEquals(SQLParser.JOIN, fromAST.getChild(1).getType());
-    assertEquals(joinType, fromAST.getChild(1).getChild(0).getType());
-    assertEquals(SQLParser.TABLE, fromAST.getChild(1).getChild(1).getType());
-    assertEquals("student", fromAST.getChild(1).getChild(1).getChild(0).getText());
-  }
-  
-  @Test
-  public void testCrossJoinClause() {
-    Tree tree = parseQuery(JOINS[4]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.FROM, tree.getChild(0).getType());
-    CommonTree fromAST = (CommonTree) tree.getChild(0);
-    assert2WayJoinAST(fromAST, SQLParser.CROSS);
-  }
-  
-  @Test
-  public void testLeftOuterJoinClause() {
-    Tree tree = parseQuery(JOINS[5]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.FROM, tree.getChild(0).getType());
-    CommonTree fromAST = (CommonTree) tree.getChild(0);
-    assert2WayJoinAST(fromAST, SQLParser.OUTER);
-    assertEquals(SQLParser.LEFT, fromAST.getChild(1).getChild(0).getChild(0).getType());
-    assertEquals(SQLParser.ON, fromAST.getChild(1).getChild(2).getType());
-    assertEquals(1, fromAST.getChild(1).getChild(2).getChildCount());
-  }
-  
-  @Test
-  public void testRightOuterJoinClause() {
-    Tree tree = parseQuery(JOINS[6]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.FROM, tree.getChild(0).getType());
-    CommonTree fromAST = (CommonTree) tree.getChild(0);
-    assert2WayJoinAST(fromAST, SQLParser.OUTER);
-    assertEquals(SQLParser.RIGHT, fromAST.getChild(1).getChild(0).getChild(0).getType());
-    assertEquals(SQLParser.ON, fromAST.getChild(1).getChild(2).getType());
-    assertEquals(1, fromAST.getChild(1).getChild(2).getChildCount());
-  }
-
-  @Test
-  public void testAllJoinTypes() {
-    Tree tree = parseQuery(JOINS[7]);
-    assertEquals(tree.getType(), SQLParser.SELECT);
-    assertEquals(SQLParser.FROM, tree.getChild(0).getType());
-    CommonTree fromAST = (CommonTree) tree.getChild(0);
-
-    assertEquals(12,fromAST.getChildCount());
-    assertEquals(SQLParser.TABLE, fromAST.getChild(0).getType());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(1).getType());
-    Tree join = fromAST.getChild(1);
-    assertEquals(SQLParser.CROSS, join.getChild(0).getType());
-    assertEquals("table2", join.getChild(1).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(2).getType());
-    join = fromAST.getChild(2);
-    assertEquals(SQLParser.TABLE, join.getChild(0).getType());
-    assertEquals("table3", join.getChild(0).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(3).getType());
-    join = fromAST.getChild(3);
-    assertEquals(SQLParser.INNER, join.getChild(0).getType());
-    assertEquals("table4", join.getChild(1).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(4).getType());
-    join = fromAST.getChild(4);
-    assertEquals(SQLParser.OUTER, join.getChild(0).getType());
-    assertEquals(SQLParser.LEFT, join.getChild(0).getChild(0).getType());
-    assertEquals("table5", join.getChild(1).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(5).getType());
-    join = fromAST.getChild(5);
-    assertEquals(SQLParser.OUTER, join.getChild(0).getType());
-    assertEquals(SQLParser.RIGHT, join.getChild(0).getChild(0).getType());
-    assertEquals("table6", join.getChild(1).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(6).getType());
-    join = fromAST.getChild(6);
-    assertEquals(SQLParser.OUTER, join.getChild(0).getType());
-    assertEquals(SQLParser.FULL, join.getChild(0).getChild(0).getType());
-    assertEquals("table7", join.getChild(1).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(7).getType());
-    join = fromAST.getChild(7);
-    assertEquals(SQLParser.NATURAL, join.getChild(0).getType());
-    assertEquals("table8", join.getChild(1).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(8).getType());
-    join = fromAST.getChild(8);
-    assertEquals(SQLParser.NATURAL, join.getChild(0).getType());
-    assertEquals(SQLParser.INNER, join.getChild(1).getType());
-    assertEquals("table9", join.getChild(2).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(9).getType());
-    join = fromAST.getChild(9);
-    assertEquals(SQLParser.NATURAL, join.getChild(0).getType());
-    assertEquals(SQLParser.OUTER, join.getChild(1).getType());
-    assertEquals(SQLParser.LEFT, join.getChild(1).getChild(0).getType());
-    assertEquals("table10", join.getChild(2).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(10).getType());
-    join = fromAST.getChild(10);
-    assertEquals(SQLParser.NATURAL, join.getChild(0).getType());
-    assertEquals(SQLParser.OUTER, join.getChild(1).getType());
-    assertEquals(SQLParser.RIGHT, join.getChild(1).getChild(0).getType());
-    assertEquals("table11", join.getChild(2).getChild(0).getText());
-
-    assertEquals(SQLParser.JOIN, fromAST.getChild(11).getType());
-    join = fromAST.getChild(11);
-    assertEquals(SQLParser.NATURAL, join.getChild(0).getType());
-    assertEquals(SQLParser.OUTER, join.getChild(1).getType());
-    assertEquals(SQLParser.FULL, join.getChild(1).getChild(0).getType());
-    assertEquals("table12", join.getChild(2).getChild(0).getText());
-  }
-  
-  private final static String setClauses [] = {
-    "select a,b,c from table1 union select a,b,c from table1", // 0
-    "select a,b,c from table1 union all select a,b,c from table1", // 1
-    "select a,b,c from table1 union distinct select a,b,c from table1", // 2
-    "select a,b,c from table1 except select a,b,c from table1", // 3
-    "select a,b,c from table1 except all select a,b,c from table1", // 4
-    "select a,b,c from table1 except distinct select a,b,c from table1", // 5
-    "select a,b,c from table1 union select a,b,c from table1 union select a,b,c from table2", // 6
-    "select a,b,c from table1 union select a,b,c from table1 intersect select a,b,c from table2" // 7
-  };
-  
-  @Test
-  public void testUnionClause() throws RecognitionException {
-    Tree tree = parseQuery(setClauses[0]);
-    assertEquals(SQLParser.UNION, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(1).getType());
-    
-    tree = parseQuery(setClauses[1]);
-    assertEquals(SQLParser.UNION, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.ALL, tree.getChild(1).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(2).getType());
-    
-    tree = parseQuery(setClauses[2]);
-    assertEquals(SQLParser.UNION, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.DISTINCT, tree.getChild(1).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(2).getType());
-    
-    tree = parseQuery(setClauses[3]);
-    assertEquals(SQLParser.EXCEPT, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(1).getType());
-    
-    tree = parseQuery(setClauses[4]);
-    assertEquals(SQLParser.EXCEPT, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.ALL, tree.getChild(1).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(2).getType());
-    
-    tree = parseQuery(setClauses[5]);
-    assertEquals(SQLParser.EXCEPT, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.DISTINCT, tree.getChild(1).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(2).getType());
-    
-    tree = parseQuery(setClauses[6]);
-    assertEquals(SQLParser.UNION, tree.getType());
-    assertEquals(SQLParser.UNION, tree.getChild(0).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(1).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getChild(0).getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getChild(1).getType());
-    
-    tree = parseQuery(setClauses[7]);
-    assertEquals(SQLParser.UNION, tree.getType());
-    assertEquals(SQLParser.SELECT, tree.getChild(0).getType());
-    assertEquals(SQLParser.INTERSECT, tree.getChild(1).getType());
-  }
-
-  static String[] schemaStmts = { 
-    "drop table abc", // 0
-    "create table name (name text, age int)", // 1
-    "create table name (name text, age int) using rcfile", // 2
-    "create table name (name text, age int) using rcfile with ('rcfile.buffer'=4096)", // 3
-    "create table name as select * from test", // 4
-    "create table name (name text, age int) as select * from test", // 5
-    "create table name (name text, age int) using rcfile as select * from test", // 6
-    "create table name (name text, age int) using rcfile with ('rcfile.buffer'= 4096) as select * from test", // 7
-    "create table widetable (" +
-        "col0 bit,\n" +
-        "col0 BIT(10),\n" +
-        "col0 bit varying,\n" +
-        "col0 bit VARYING(10),\n" +
-        "col1 tinyint,\n" +
-        "col2 smallInt,\n" +
-        "col3 integer,\n" +
-        "col4 biginT,\n" +
-        "col5 real,\n" +
-        "col5 float,\n" +
-        "col5 float(53),\n" +
-        "col6 double,\n" +
-        "col6 doublE precision,\n" +
-        "col7 numeric,\n" +
-        "col7 numeric(10),\n" +
-        "col7 numeric(10,2),\n" +
-        "col8 decimal,\n" +
-        "col8 decimal(10),\n" +
-        "col8 decimal(10,2),\n" +
-        "col9 char,\n" +
-        "col9 character,\n" +
-        "col10 chaR(10),\n" +
-        "col10 character(10),\n" +
-        "col11 varchar,\n" +
-        "col11 character varying,\n" +
-        "col12 varchar(255),\n" +
-        "col11 character varying (255),\n" +
-        "col11 nchar,\n" +
-        "col11 nchar(255),\n" +
-        "col11 national character,\n" +
-        "col11 national character(255),\n" +
-        "col11 nvarchar,\n" +
-        "col11 nvarchar(255),\n" +
-        "col11 natIonal character varying,\n" +
-        "col11 national character varying (255),\n" +
-        "col11 date,\n" +
-        "col11 time,\n" +
-        "col11 timetz,\n" +
-        "col11 time With time zone,\n" +
-        "col11 timesTamptz,\n" +
-        "col11 timestamp with time zone,\n" +
-        "col11 binary,\n" +
-        "col11 binary(10),\n" +
-        "col11 varbinary(10),\n" +
-        "col11 binary Varying(10),\n" +
-        "col11 blOb\n" +
-        ") as select * from test", // 8
-    "create table widetable (col1 float(10), col2 float) as select * from test", // 9
-    "create external table table1 (name text, age int, earn bigint, score float) using csv location '/tmp/data'", // 10
-  };
-
-  @Test
-  public void testCreateTableAsSelect1() throws RecognitionException, TQLSyntaxError {
-    Tree ast = parseQuery(schemaStmts[1]);
-    assertEquals(ast.getType(), SQLParser.CREATE_TABLE);
-    assertEquals(SQLParser.Identifier, ast.getChild(0).getType());
-    assertEquals(SQLParser.TABLE_DEF, ast.getChild(1).getType());
-  }
-
-  @Test
-  public void testCreateTableAsSelect2() throws RecognitionException, TQLSyntaxError {
-    Tree ast = parseQuery(schemaStmts[2]);
-    assertEquals(ast.getType(), SQLParser.CREATE_TABLE);
-    assertEquals(SQLParser.Identifier, ast.getChild(0).getType());
-    assertEquals(SQLParser.TABLE_DEF, ast.getChild(1).getType());
-    assertEquals(SQLParser.USING, ast.getChild(2).getType());
-    assertEquals("rcfile", ast.getChild(2).getChild(0).getText());
-  }
-
-  @Test
-  public void testCreateTableAsSelect3() throws RecognitionException, TQLSyntaxError {
-    Tree ast = parseQuery(schemaStmts[3]);
-    assertEquals(ast.getType(), SQLParser.CREATE_TABLE);
-    assertEquals(SQLParser.Identifier, ast.getChild(0).getType());
-    assertEquals(SQLParser.TABLE_DEF, ast.getChild(1).getType());
-    assertEquals(SQLParser.USING, ast.getChild(2).getType());
-    assertEquals("rcfile", ast.getChild(2).getChild(0).getText());
-    assertEquals(SQLParser.PARAMS, ast.getChild(3).getType());
-  }
-
-  @Test
-  public void testCreateTableAsSelect4() throws RecognitionException, TQLSyntaxError {
-    Tree ast = parseQuery(schemaStmts[4]);
-    assertEquals(ast.getType(), SQLParser.CREATE_TABLE);
-    assertEquals(SQLParser.Identifier, ast.getChild(0).getType());
-    assertEquals(SQLParser.AS, ast.getChild(1).getType());
-    assertEquals(SQLParser.SELECT, ast.getChild(1).getChild(0).getType());
-  }
-
-  @Test
-  public void testCreateTableAsSelect5() throws RecognitionException, TQLSyntaxError {
-    Tree ast = parseQuery(schemaStmts[5]);
-    assertEquals(ast.getType(), SQLParser.CREATE_TABLE);
-    assertEquals(SQLParser.Identifier, ast.getChild(0).getType());
-    assertEquals(SQLParser.TABLE_DEF, ast.getChild(1).getType());
-    assertEquals(SQLParser.AS, ast.getChild(2).getType());
-    assertEquals(SQLParser.SELECT, ast.getChild(2).getChild(0).getType());
-  }
-
-  @Test
-  public void testCreateTableAsSelect6() throws RecognitionException, TQLSyntaxError {
-    Tree ast = parseQuery(schemaStmts[6]);
-    assertEquals(ast.getType(), SQLParser.CREATE_TABLE);
-    assertEquals(SQLParser.Identifier, ast.getChild(0).getType());
-    assertEquals(SQLParser.TABLE_DEF, ast.getChild(1).getType());
-    assertEquals(SQLParser.USING, ast.getChild(2).getType());
-    assertEquals(SQLParser.AS, ast.getChild(3).getType());
-    assertEquals(SQLParser.SELECT, ast.getChild(3).getChild(0).getType());
-  }
-
-  @Test
-  public void testCreateTableAsSelect7() throws RecognitionException, TQLSyntaxError {
-    Tree ast = parseQuery(schemaStmts[7]);
-    assertEquals(ast.getType(), SQLParser.CREATE_TABLE);
-    assertEquals(SQLParser.Identifier, ast.getChild(0).getType());
-    assertEquals(SQLParser.TABLE_DEF, ast.getChild(1).getType());
-    assertEquals(SQLParser.USING, ast.getChild(2).getType());
-    assertEquals(SQLParser.PARAMS, ast.getChild(3).getType());
-    assertEquals(SQLParser.AS, ast.getChild(4).getType());
-    assertEquals(SQLParser.SELECT, ast.getChild(4).getChild(0).getType());
-  }
-
-  @Test
-  public void testCreateTableWithVariousDataType1() throws RecognitionException, TQLSyntaxError {
-    Tree ast = parseQuery(schemaStmts[8]);
-    assertEquals(ast.getType(), SQLParser.CREATE_TABLE);
-    assertEquals(SQLParser.Identifier, ast.getChild(0).getType());
-    assertEquals(SQLParser.TABLE_DEF, ast.getChild(1).getType());
-    assertEquals(SQLParser.AS, ast.getChild(2).getType());
-    assertEquals(SQLParser.SELECT, ast.getChild(2).getChild(0).getType());
-  }
-
-  @Test
-  public void testCreateTableWithVariousDataType2() throws RecognitionException, TQLSyntaxError {
-    Tree ast = parseQuery(schemaStmts[9]);
-    assertEquals(ast.getType(), SQLParser.CREATE_TABLE);
-    assertEquals(SQLParser.Identifier, ast.getChild(0).getType());
-    assertEquals(SQLParser.TABLE_DEF, ast.getChild(1).getType());
-    assertEquals("10", ast.getChild(1).getChild(0).getChild(1).getChild(0).getText());
-    assertEquals(SQLParser.AS, ast.getChild(2).getType());
-    assertEquals(SQLParser.SELECT, ast.getChild(2).getChild(0).getType());
-  }
-  
-  @Test
-  public void testCreateTableLocation1() throws RecognitionException, TQLSyntaxError {
-    Tree ast = parseQuery(schemaStmts[10]);
-    assertEquals(ast.getType(), SQLParser.CREATE_TABLE);
-    assertEquals(SQLParser.Identifier, ast.getChild(0).getType());
-    assertEquals(SQLParser.EXTERNAL, ast.getChild(1).getType());
-    assertEquals(SQLParser.TABLE_DEF, ast.getChild(2).getType());
-    assertEquals(SQLParser.USING, ast.getChild(3).getType());
-    assertEquals(SQLParser.LOCATION, ast.getChild(4).getType());
-    assertEquals("/tmp/data", ast.getChild(4).getChild(0).getText());
-  }
-
-  @Test
-  public void testDropTable() throws RecognitionException, TQLSyntaxError {
-    Tree ast = parseQuery(schemaStmts[0]);
-    assertEquals(ast.getType(), SQLParser.DROP_TABLE);
-    assertEquals(ast.getChild(0).getText(), "abc");
-  }
-
-  static String[] exprs = { 
-      "1 + 2", // 0
-      "3 - 4", // 1
-      "5 * 6", // 2
-      "7 / 8", // 3
-      "10 % 2", // 4
-      "1 * 2 > 3 / 4", // 5
-      "1 * 2 < 3 / 4", // 6
-      "1 * 2 = 3 / 4", // 7
-      "1 * 2 != 3 / 4", // 8
-      "1 * 2 <> 3 / 4", // 9
-      "(3, 4)", // 10
-      "('male', 'female')", // 11
-      "gender in ('male', 'female')", // 12
-      "gender not in ('male', 'female')", // 13
-      "score > 90 and age < 20", // 14
-      "score > 90 and age < 20 and name != 'hyunsik'", // 15
-      "score > 90 or age < 20", // 16
-      "score > 90 or age < 20 and name != 'hyunsik'", // 17
-      "((a+3 > 1) or 1=1) and (3 != (abc + 4) and type in (3,4))", // 18
-      "3", // 19
-      "1.2", // 20
-      "sum(age)", // 21
-      "now()", // 22
-      "not (90 > 100)", // 23
-      "type like '%top'", // 24
-      "type not like 'top%'", // 25
-      "col = 'value'", // 26
-      "col is null", // 27
-      "col is not null", // 28
-      "col = null", // 29
-      "col != null" // 30
-
-  };
-
-  public static SQLParser parseExpr(String expr) {
-    ANTLRStringStream input = new ANTLRStringStream(expr);
-    SQLLexer lexer = new SQLLexer(input);
-    CommonTokenStream tokens = new CommonTokenStream(lexer);
-    return new SQLParser(tokens);
-  }
-
-  @Test
-  public void testArithEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[0]);
-    CommonTree node = (CommonTree) p.numeric_value_expression().getTree();
-
-    assertEquals(node.getText(), "+");
-    assertEquals(node.getChild(0).getText(), "1");
-    assertEquals(node.getChild(1).getText(), "2");
-
-    p = parseExpr(exprs[1]);
-    node = (CommonTree) p.numeric_value_expression().getTree();
-    assertEquals(node.getText(), "-");
-    assertEquals(node.getChild(0).getText(), "3");
-    assertEquals(node.getChild(1).getText(), "4");
-
-    p = parseExpr(exprs[2]);
-    node = (CommonTree) p.numeric_value_expression().getTree();
-    assertEquals(node.getText(), "*");
-    assertEquals(node.getChild(0).getText(), "5");
-    assertEquals(node.getChild(1).getText(), "6");
-
-    p = parseExpr(exprs[3]);
-    node = (CommonTree) p.numeric_value_expression().getTree();
-    assertEquals(node.getText(), "/");
-    assertEquals(node.getChild(0).getText(), "7");
-    assertEquals(node.getChild(1).getText(), "8");
-
-    p = parseExpr(exprs[4]);
-    node = (CommonTree) p.numeric_value_expression().getTree();
-    assertEquals(node.getText(), "%");
-    assertEquals(node.getChild(0).getText(), "10");
-    assertEquals(node.getChild(1).getText(), "2");
-  }
-
-  @Test
-  public void testCompEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[5]);
-    CommonTree node = (CommonTree) p.comparison_predicate().getTree();
-    assertEquals(node.getText(), ">");
-    assertEquals("*", node.getChild(0).getText());
-    assertEquals("/", node.getChild(1).getText());
-
-    p = parseExpr(exprs[6]);
-    node = (CommonTree) p.comparison_predicate().getTree();
-    assertEquals(node.getText(), "<");
-    assertEquals(node.getChild(0).getText(), "*");
-    assertEquals(node.getChild(1).getText(), "/");
-
-    p = parseExpr(exprs[7]);
-    node = (CommonTree) p.comparison_predicate().getTree();
-    assertEquals(node.getText(), "=");
-    assertEquals(node.getChild(0).getText(), "*");
-    assertEquals(node.getChild(1).getText(), "/");
-
-    p = parseExpr(exprs[8]);
-    node = (CommonTree) p.comparison_predicate().getTree();
-    assertEquals(node.getText(), "!=");
-    assertEquals(node.getChild(0).getText(), "*");
-    assertEquals(node.getChild(1).getText(), "/");
-
-    p = parseExpr(exprs[9]);
-    node = (CommonTree) p.comparison_predicate().getTree();
-    assertEquals(node.getText(), "<>");
-    assertEquals(node.getChild(0).getText(), "*");
-    assertEquals(node.getChild(1).getText(), "/");
-  }
-
-  @Test
-  public void testAtomArray() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[10]);
-    CommonTree node = (CommonTree) p.array().getTree();
-    assertEquals(node.getChild(0).getText(), "3");
-    assertEquals(node.getChild(1).getText(), "4");
-
-    p = parseExpr(exprs[11]);
-    node = (CommonTree) p.array().getTree();
-    assertEquals(node.getChild(0).getText(), "male");
-    assertEquals(node.getChild(1).getText(), "female");
-  }
-
-  @Test
-  public void testInEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[12]);
-    CommonTree node = (CommonTree) p.in_predicate().getTree();
-    assertEquals(node.getType(), SQLParser.IN);
-    assertEquals(node.getChild(1).getText(), "male");
-    assertEquals(node.getChild(2).getText(), "female");
-
-    p = parseExpr(exprs[13]);
-    node = (CommonTree) p.in_predicate().getTree();
-    assertEquals(node.getType(), SQLParser.IN);
-    assertEquals(node.getChild(0).getType(), SQLParser.FIELD_NAME);
-    FieldName fieldName = new FieldName(node.getChild(0));
-    assertEquals(fieldName.getName(), "gender");
-    assertEquals(node.getChild(1).getText(), "male");
-    assertEquals(node.getChild(2).getText(), "female");
-    assertEquals(node.getChild(3).getType(), SQLParser.NOT);
-  }
-
-  @Test
-  public void testAndEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[14]);
-    CommonTree node = (CommonTree) p.and_predicate().getTree();
-    assertEquals(node.getText(), "and");
-    assertEquals(node.getChild(0).getText(), ">");
-    assertEquals(node.getChild(1).getText(), "<");
-
-    p = parseExpr(exprs[15]);
-    node = (CommonTree) p.and_predicate().getTree();
-    assertEquals(node.getText(), "and");
-    assertEquals(node.getChild(0).getText(), "and");
-    assertEquals(node.getChild(1).getText(), "!=");
-  }
-
-  @Test
-  public void testOrEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[16]);
-    CommonTree node = (CommonTree) p.boolean_value_expression().getTree();
-    assertEquals(node.getText(), "or");
-    assertEquals(node.getChild(0).getText(), ">");
-    assertEquals(node.getChild(1).getText(), "<");
-
-    p = parseExpr(exprs[17]);
-    node = (CommonTree) p.boolean_value_expression().getTree();
-    assertEquals(node.getText(), "or");
-    assertEquals(node.getChild(0).getText(), ">");
-    assertEquals(node.getChild(1).getText(), "and");
-  }
-
-  @Test
-  public void testComplexEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[18]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(node.getText(), "and");
-    assertEquals(node.getChild(0).getText(), "or");
-    assertEquals(node.getChild(1).getText(), "and");
-  }
-  
-  @Test
-  public void testNotEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[23]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(node.getType(), SQLParser.NOT);
-    assertEquals(node.getChild(0).getType(), SQLParser.GTH);
-    CommonTree gth = (CommonTree) node.getChild(0);
-    assertEquals(gth.getChild(0).getType(), SQLParser.NUMBER);
-    assertEquals(gth.getChild(1).getType(), SQLParser.NUMBER);
-  }
-
-  @Test
-  public void testFuncCallEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[21]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(node.getType(), SQLParser.FUNCTION);
-    assertEquals(node.getText(), "sum");
-    assertEquals(node.getChild(0).getType(), SQLParser.FIELD_NAME);
-    FieldName fieldName = new FieldName(node.getChild(0));
-    assertEquals(fieldName.getName(), "age");
-
-    p = parseExpr(exprs[22]);
-    node = (CommonTree) p.search_condition().getTree();
-    assertEquals(node.getType(), SQLParser.FUNCTION);
-    assertEquals(node.getText(), "now");
-    assertNull(node.getChild(1));
-  }
-  
-  @Test
-  public void testLikeEvalTree() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[24]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(SQLParser.LIKE, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(0).getType());
-    FieldName fieldName = new FieldName(node.getChild(0));
-    assertEquals(fieldName.getName(), "type");
-    assertEquals(SQLParser.Character_String_Literal, node.getChild(1).getType());
-    
-    p = parseExpr(exprs[25]);
-    node = (CommonTree) p.search_condition().getTree();    
-    assertEquals(SQLParser.NOT, node.getChild(0).getType());
-    assertEquals(SQLParser.LIKE, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(1).getType());
-    fieldName = new FieldName(node.getChild(1));
-    assertEquals(fieldName.getName(), "type");
-    assertEquals(SQLParser.Character_String_Literal, node.getChild(2).getType());
-  }
-
-  @Test
-  /**
-   * TODO - needs more tests
-   */
-  public void testConstEval() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[26]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(SQLParser.EQUAL, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(0).getType());
-    assertEquals(SQLParser.Character_String_Literal, node.getChild(1).getType());
-  }
-
-  @Test
-  public void testIsNull() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[27]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(SQLParser.IS, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(0).getType());
-    assertEquals(SQLParser.NULL, node.getChild(1).getType());
-  }
-
-  @Test
-  public void testIsNotNull() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[28]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(SQLParser.IS, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(0).getType());
-    assertEquals(SQLParser.NULL, node.getChild(1).getType());
-    assertEquals(SQLParser.NOT, node.getChild(2).getType());
-  }
-
-  @Test
-  public void testEqualNull() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[29]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(SQLParser.EQUAL, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(0).getType());
-    assertEquals(SQLParser.NULL, node.getChild(1).getType());
-  }
-
-  @Test
-  public void testNotEqualNull() throws RecognitionException {
-    SQLParser p = parseExpr(exprs[30]);
-    CommonTree node = (CommonTree) p.search_condition().getTree();
-    assertEquals(SQLParser.NOT_EQUAL, node.getType());
-    assertEquals(SQLParser.FIELD_NAME, node.getChild(0).getType());
-    assertEquals(SQLParser.NULL, node.getChild(1).getType());
-  }
-
-  public static String [] caseStatements = {
-      "select " +
-          "case " +
-          "when p_type like 'PROMO%' then l_extendedprice * (1 - l_discount) " +
-          "when p_type = 'MOCC' then l_extendedprice - 100 " +
-          "else 0 end " +
-          "as cond " +
-          "from lineitem"
-  };
-
-  @Test
-  public void testCaseWhen() throws RecognitionException {
-    SQLParser p = parseExpr(caseStatements[0]);
-    CommonTree node = (CommonTree) p.statement().getTree();
-
-    assertEquals(SQLParser.SEL_LIST, node.getChild(1).getType());
-    CommonTree selList = (CommonTree) node.getChild(1);
-    assertEquals(1, selList.getChildCount());
-    assertEquals(SQLParser.COLUMN, selList.getChild(0).getType());
-
-    CommonTree column = (CommonTree) selList.getChild(0);
-    assertEquals(SQLParser.CASE, column.getChild(0).getType());
-    CommonTree caseStatement = (CommonTree) column.getChild(0);
-
-    assertEquals(3, caseStatement.getChildCount());
-    assertEquals(SQLParser.WHEN, caseStatement.getChild(0).getType());
-    assertEquals(SQLParser.WHEN, caseStatement.getChild(1).getType());
-    assertEquals(SQLParser.ELSE, caseStatement.getChild(2).getType());
-
-    CommonTree cond1 = (CommonTree) caseStatement.getChild(0);
-    CommonTree cond2 = (CommonTree) caseStatement.getChild(1);
-    CommonTree elseStmt = (CommonTree) caseStatement.getChild(2);
-
-    assertEquals(SQLParser.LIKE, cond1.getChild(0).getType());
-    assertEquals(SQLParser.EQUAL, cond2.getChild(0).getType());
-    assertEquals(SQLParser.NUMBER, elseStmt.getChild(0).getType());
-  }
-
-  public class FieldName {
-    private final String tableName;
-    private final String familyName;
-    private final String fieldName;
-
-    public FieldName(Tree tree) {
-      if(tree.getChild(1) == null) {
-        this.tableName = null;
-      } else {
-        this.tableName = tree.getChild(1).getText();
-      }
-
-      String name = tree.getChild(0).getText();
-      if(name.contains(":")) {
-        String [] splits = name.split(":");
-        this.familyName = splits[0];
-        this.fieldName = splits[1];
-      } else {
-        this.familyName = null;
-        this.fieldName = name;
-      }
-    }
-
-    public boolean hasTableName() {
-      return this.tableName != null;
-    }
-
-    public String getTableName(){
-      return this.tableName;
-    }
-
-    public boolean hasFamilyName() {
-      return this.familyName != null;
-    }
-
-    public String getFamilyName() {
-      return this.familyName;
-    }
-
-    public String getFullName() {
-      StringBuilder sb = new StringBuilder();
-      if(tableName != null) {
-        sb.append(tableName)
-            .append(".");
-      }
-      if(familyName != null) {
-        sb.append(familyName).
-            append(":");
-      }
-      sb.append(fieldName);
-
-      return sb.toString();
-    }
-
-    public String getName() {
-      if(familyName == null) {
-        return fieldName;
-      } else
-        return familyName+":"+fieldName;
-    }
-
-    public String getSimpleName() {
-      return this.fieldName;
-    }
-
-    public String toString() {
-      return getName();
-    }
-  }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/plan/global/TestGlobalQueryPlanner.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/plan/global/TestGlobalQueryPlanner.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/plan/global/TestGlobalQueryPlanner.java
index ec86eaf..c3c9e04 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/plan/global/TestGlobalQueryPlanner.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/plan/global/TestGlobalQueryPlanner.java
@@ -21,13 +21,10 @@ package org.apache.tajo.engine.plan.global;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.yarn.event.AsyncDispatcher;
-import org.apache.zookeeper.KeeperException;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
 import org.apache.tajo.QueryId;
 import org.apache.tajo.QueryIdFactory;
 import org.apache.tajo.TajoTestingCluster;
+import org.apache.tajo.algebra.Expr;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.FunctionType;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
@@ -36,10 +33,8 @@ import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.DatumFactory;
 import org.apache.tajo.engine.eval.TestEvalTree.TestSum;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
-import org.apache.tajo.engine.planner.LogicalOptimizer;
-import org.apache.tajo.engine.planner.LogicalPlanner;
-import org.apache.tajo.engine.planner.PlanningContext;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
+import org.apache.tajo.engine.planner.*;
 import org.apache.tajo.engine.planner.global.MasterPlan;
 import org.apache.tajo.engine.planner.logical.*;
 import org.apache.tajo.master.ExecutionBlock;
@@ -47,6 +42,10 @@ import org.apache.tajo.master.ExecutionBlock.PartitionType;
 import org.apache.tajo.master.GlobalPlanner;
 import org.apache.tajo.master.TajoMaster;
 import org.apache.tajo.storage.*;
+import org.apache.zookeeper.KeeperException;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
 import java.io.IOException;
 import java.util.Iterator;
@@ -60,7 +59,7 @@ public class TestGlobalQueryPlanner {
   private static CatalogService catalog;
   private static GlobalPlanner planner;
   private static Schema schema;
-  private static QueryAnalyzer analyzer;
+  private static SQLAnalyzer analyzer;
   private static LogicalPlanner logicalPlanner;
   private static QueryId queryId;
   private static StorageManager sm;
@@ -99,7 +98,7 @@ public class TestGlobalQueryPlanner {
 
     planner = new GlobalPlanner(conf, catalog, new StorageManager(conf),
         dispatcher.getEventHandler());
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     logicalPlanner = new LogicalPlanner(catalog);
 
     int tbNum = 2;
@@ -143,14 +142,16 @@ public class TestGlobalQueryPlanner {
   }
   
   @Test
-  public void testScan() throws IOException {
-    PlanningContext context = analyzer.parse(
+  public void testScan() throws IOException, CloneNotSupportedException {
+    Expr context = analyzer.parse(
         "select age, sumtest(salary) from table0");
-    LogicalNode plan1 = logicalPlanner.createPlan(context);
-    plan1 = LogicalOptimizer.optimize(context, plan1);
+
+    LogicalPlan plan = logicalPlanner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
+
 
     MasterPlan globalPlan = planner.build(queryId,
-        (LogicalRootNode) plan1);
+        (LogicalRootNode) rootNode);
 
     ExecutionBlock unit = globalPlan.getRoot();
     assertFalse(unit.hasChildBlock());
@@ -163,13 +164,13 @@ public class TestGlobalQueryPlanner {
 
   @Test
   public void testGroupby() throws IOException, KeeperException,
-      InterruptedException {
-    PlanningContext context = analyzer.parse(
+      InterruptedException, CloneNotSupportedException {
+    Expr context = analyzer.parse(
         "create table store1 as select age, sumtest(salary) from table0 group by age");
-    LogicalNode plan = logicalPlanner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    LogicalPlan plan = logicalPlanner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
-    MasterPlan globalPlan = planner.build(queryId, (LogicalRootNode) plan);
+    MasterPlan globalPlan = planner.build(queryId, (LogicalRootNode) rootNode);
 
     ExecutionBlock next, prev;
     
@@ -201,13 +202,13 @@ public class TestGlobalQueryPlanner {
   }
   
   @Test
-  public void testSort() throws IOException {
-    PlanningContext context = analyzer.parse(
+  public void testSort() throws IOException, CloneNotSupportedException {
+    Expr context = analyzer.parse(
         "create table store1 as select age from table0 order by age");
-    LogicalNode plan = logicalPlanner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    LogicalPlan plan = logicalPlanner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
-    MasterPlan globalPlan = planner.build(queryId, (LogicalRootNode) plan);
+    MasterPlan globalPlan = planner.build(queryId, (LogicalRootNode) rootNode);
 
     ExecutionBlock next, prev;
     
@@ -243,14 +244,15 @@ public class TestGlobalQueryPlanner {
   }
   
   @Test
-  public void testJoin() throws IOException {
-    PlanningContext context = analyzer.parse(
+  public void testJoin() throws IOException, CloneNotSupportedException {
+    Expr expr = analyzer.parse(
         "select table0.age,table0.salary,table1.salary from table0,table1 " +
             "where table0.salary = table1.salary order by table0.age");
-    LogicalNode plan = logicalPlanner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    LogicalPlan plan = logicalPlanner.createPlan(expr);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
+
 
-    MasterPlan globalPlan = planner.build(queryId, (LogicalRootNode) plan);
+    MasterPlan globalPlan = planner.build(queryId, (LogicalRootNode) rootNode);
 
     ExecutionBlock next, prev;
     
@@ -310,13 +312,13 @@ public class TestGlobalQueryPlanner {
   }
   
   @Test
-  public void testSelectAfterJoin() throws IOException {
+  public void testSelectAfterJoin() throws IOException, CloneNotSupportedException {
     String query = "select table0.name, table1.salary from table0,table1 where table0.name = table1.name and table1.salary > 10";
-    PlanningContext context = analyzer.parse(query);
-    LogicalNode plan = logicalPlanner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr context = analyzer.parse(query);
+    LogicalPlan plan = logicalPlanner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
     
-    MasterPlan globalPlan = planner.build(queryId, (LogicalRootNode) plan);
+    MasterPlan globalPlan = planner.build(queryId, (LogicalRootNode) rootNode);
 
     ExecutionBlock unit = globalPlan.getRoot();
     StoreTableNode store = unit.getStoreTableNode();
@@ -332,14 +334,14 @@ public class TestGlobalQueryPlanner {
     }
   }
   
-  @Test
-  public void testCubeby() throws IOException {
-    PlanningContext context = analyzer.parse(
+  //@Test
+  public void testCubeby() throws IOException, CloneNotSupportedException {
+    Expr expr = analyzer.parse(
         "select age, sum(salary) from table0 group by cube (age, id)");
-    LogicalNode plan = logicalPlanner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    LogicalPlan plan = logicalPlanner.createPlan(expr);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
-    MasterPlan globalPlan = planner.build(queryId, (LogicalRootNode) plan);
+    MasterPlan globalPlan = planner.build(queryId, (LogicalRootNode) rootNode);
 
     ExecutionBlock unit = globalPlan.getRoot();
     StoreTableNode store = unit.getStoreTableNode();

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestLogicalOptimizer.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestLogicalOptimizer.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestLogicalOptimizer.java
index a7a1ed7..fe2b114 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestLogicalOptimizer.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestLogicalOptimizer.java
@@ -19,18 +19,19 @@
 package org.apache.tajo.engine.planner;
 
 import org.apache.hadoop.fs.Path;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
 import org.apache.tajo.TajoTestingCluster;
+import org.apache.tajo.algebra.Expr;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.FunctionType;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
 import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.engine.function.builtin.SumInt;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
 import org.apache.tajo.engine.planner.logical.*;
 import org.apache.tajo.master.TajoMaster;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
 import static org.junit.Assert.*;
 
@@ -38,7 +39,7 @@ public class TestLogicalOptimizer {
 
   private static TajoTestingCluster util;
   private static CatalogService catalog;
-  private static QueryAnalyzer analyzer;
+  private static SQLAnalyzer sqlAnalyzer;
   private static LogicalPlanner planner;
 
   @BeforeClass
@@ -84,7 +85,7 @@ public class TestLogicalOptimizer {
         CatalogUtil.newDataTypesWithoutLen(Type.INT4));
 
     catalog.registerFunction(funcDesc);
-    analyzer = new QueryAnalyzer(catalog);
+    sqlAnalyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
   }
 
@@ -105,8 +106,9 @@ public class TestLogicalOptimizer {
   @Test
   public final void testProjectionPushWithNaturalJoin() throws CloneNotSupportedException {
     // two relations
-    PlanningContext context = analyzer.parse(QUERIES[4]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = sqlAnalyzer.parse(QUERIES[4]);
+    LogicalPlan newPlan = planner.createPlan(expr);
+    LogicalNode plan = newPlan.getRootBlock().getRoot();
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;
     TestLogicalNode.testCloneLogicalNode(root);
@@ -117,7 +119,8 @@ public class TestLogicalOptimizer {
     assertEquals(ExprType.SCAN, joinNode.getOuterNode().getType());
     assertEquals(ExprType.SCAN, joinNode.getInnerNode().getType());
     
-    LogicalNode optimized = LogicalOptimizer.optimize(context, plan);
+    LogicalNode optimized = LogicalOptimizer.optimize(newPlan);
+
     assertEquals(ExprType.ROOT, optimized.getType());
     root = (LogicalRootNode) optimized;
     TestLogicalNode.testCloneLogicalNode(root);
@@ -130,19 +133,17 @@ public class TestLogicalOptimizer {
   @Test
   public final void testProjectionPushWithInnerJoin() throws CloneNotSupportedException {
     // two relations
-    PlanningContext context = analyzer.parse(QUERIES[5]);
-    LogicalNode plan = planner.createPlan(context);
-    System.out.println(plan);
-    System.out.println("--------------");
-    LogicalNode optimized = LogicalOptimizer.optimize(context, plan);
-    System.out.println(optimized);
+    Expr expr = sqlAnalyzer.parse(QUERIES[5]);
+    LogicalPlan newPlan = planner.createPlan(expr);
+    LogicalOptimizer.optimize(newPlan);
   }
   
   @Test
   public final void testProjectionPush() throws CloneNotSupportedException {
     // two relations
-    PlanningContext context = analyzer.parse(QUERIES[2]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = sqlAnalyzer.parse(QUERIES[2]);
+    LogicalPlan newPlan = planner.createPlan(expr);
+    LogicalNode plan = newPlan.getRootBlock().getRoot();
     
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;
@@ -153,7 +154,7 @@ public class TestLogicalOptimizer {
     SelectionNode selNode = (SelectionNode) projNode.getSubNode();    
     assertEquals(ExprType.SCAN, selNode.getSubNode().getType());        
     
-    LogicalNode optimized = LogicalOptimizer.optimize(context, plan);
+    LogicalNode optimized = LogicalOptimizer.optimize(newPlan);
     assertEquals(ExprType.ROOT, optimized.getType());
     root = (LogicalRootNode) optimized;
     TestLogicalNode.testCloneLogicalNode(root);
@@ -162,8 +163,9 @@ public class TestLogicalOptimizer {
   
   @Test
   public final void testOptimizeWithGroupBy() throws CloneNotSupportedException {
-    PlanningContext context = analyzer.parse(QUERIES[3]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = sqlAnalyzer.parse(QUERIES[3]);
+    LogicalPlan newPlan = planner.createPlan(expr);
+    LogicalNode plan = newPlan.getRootBlock().getRoot();
         
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;
@@ -176,7 +178,7 @@ public class TestLogicalOptimizer {
     SelectionNode selNode = (SelectionNode) groupbyNode.getSubNode();
     assertEquals(ExprType.SCAN, selNode.getSubNode().getType());
     
-    LogicalNode optimized = LogicalOptimizer.optimize(context, plan);
+    LogicalNode optimized = LogicalOptimizer.optimize(newPlan);
     assertEquals(ExprType.ROOT, optimized.getType());
     root = (LogicalRootNode) optimized;
     TestLogicalNode.testCloneLogicalNode(root);
@@ -188,8 +190,9 @@ public class TestLogicalOptimizer {
   @Test
   public final void testPushable() throws CloneNotSupportedException {
     // two relations
-    PlanningContext context = analyzer.parse(QUERIES[0]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = sqlAnalyzer.parse(QUERIES[0]);
+    LogicalPlan newPlan = planner.createPlan(expr);
+    LogicalNode plan = newPlan.getRootBlock().getRoot();
     
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;
@@ -206,10 +209,10 @@ public class TestLogicalOptimizer {
     assertFalse(joinNode.hasJoinQual());
     
     // Test for Pushable
-    assertTrue(LogicalOptimizer.canBeEvaluated(selNode.getQual(), joinNode));
+    assertTrue(PlannerUtil.canBeEvaluated(selNode.getQual(), joinNode));
     
     // Optimized plan
-    LogicalNode optimized = LogicalOptimizer.optimize(context, plan);
+    LogicalNode optimized = LogicalOptimizer.optimize(newPlan);
     assertEquals(ExprType.ROOT, optimized.getType());
     root = (LogicalRootNode) optimized;
     
@@ -218,8 +221,9 @@ public class TestLogicalOptimizer {
     assertTrue(joinNode.hasJoinQual());
     
     // Scan Pushable Test
-    context = analyzer.parse(QUERIES[1]);
-    plan = planner.createPlan(context);
+    expr = sqlAnalyzer.parse(QUERIES[1]);
+    newPlan = planner.createPlan(expr);
+    plan = newPlan.getRootBlock().getRoot();
     
     assertEquals(ExprType.ROOT, plan.getType());
     root = (LogicalRootNode) plan;
@@ -234,6 +238,6 @@ public class TestLogicalOptimizer {
     assertEquals(ExprType.SCAN, selNode.getSubNode().getType());
     ScanNode scanNode = (ScanNode) selNode.getSubNode();
     // Test for Join Node
-    assertTrue(LogicalOptimizer.canBeEvaluated(selNode.getQual(), scanNode));
+    assertTrue(PlannerUtil.canBeEvaluated(selNode.getQual(), scanNode));
   }
 }


[09/13] TAJO-87: Integration of tajo algebra module and SQL parser

Posted by hy...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalPlan.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalPlan.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalPlan.java
new file mode 100644
index 0000000..5a9f101
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalPlan.java
@@ -0,0 +1,564 @@
+/*
+ * 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.engine.planner;
+
+import org.apache.tajo.algebra.ColumnReferenceExpr;
+import org.apache.tajo.algebra.Projection;
+import org.apache.tajo.annotation.NotThreadSafe;
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.catalog.Schema;
+import org.apache.tajo.engine.eval.EvalNode;
+import org.apache.tajo.engine.eval.EvalTreeUtil;
+import org.apache.tajo.engine.planner.logical.*;
+
+import java.util.*;
+
+/**
+ * This represents and keeps every information about a query plan for a query.
+ */
+@NotThreadSafe
+public class LogicalPlan {
+  private final LogicalPlanner planner;
+
+  /** the prefix character for virtual tables */
+  public static final char VIRTUAL_TABLE_PREFIX='@';
+  /** it indicates the root block */
+  public static final String ROOT_BLOCK = VIRTUAL_TABLE_PREFIX + "ROOT";
+  /** it indicates a table itself */
+  public static final String TABLE_SELF = VIRTUAL_TABLE_PREFIX + "SELF";
+
+  public static final String ANONYMOUS_TABLE_PREFIX = VIRTUAL_TABLE_PREFIX + "NONAME_";
+  public static Integer anonymousBlockId = 0;
+  public static Integer anonymousColumnId = 0;
+
+  /** a map from between a block name to a block plan */
+  private Map<String, QueryBlock> queryBlocks = new LinkedHashMap<String, QueryBlock>();
+  private Map<LogicalNode, QueryBlock> queryBlockByNode = new HashMap<LogicalNode, QueryBlock>();
+  private Set<LogicalNode> visited = new HashSet<LogicalNode>();
+
+  public LogicalPlan(LogicalPlanner planner) {
+    this.planner = planner;
+  }
+
+  /**
+   * Create a new {@link QueryBlock} and Get
+   *
+   * @param blockName the query block name
+   * @return a created query block
+   */
+  public QueryBlock newAndGetBlock(String blockName) {
+    QueryBlock block = new QueryBlock(blockName);
+    queryBlocks.put(blockName, block);
+    return block;
+  }
+
+  public QueryBlock newAnonymousBlock() {
+    return newAndGetBlock(ANONYMOUS_TABLE_PREFIX + (anonymousBlockId++));
+  }
+
+  public String newAnonymousColumnName() {
+    return "column_" + (anonymousColumnId ++);
+  }
+
+  /**
+   * Check if a query block exists
+   * @param blockName the query block name to be checked
+   * @return true if exists. Otherwise, false
+   */
+  public boolean existBlock(String blockName) {
+    return queryBlocks.containsKey(blockName);
+  }
+
+  public QueryBlock getRootBlock() {
+    return queryBlocks.get(ROOT_BLOCK);
+  }
+
+  public QueryBlock getBlock(String blockName) {
+    return queryBlocks.get(blockName);
+  }
+
+  public QueryBlock getBlock(LogicalNode node) {
+    return queryBlockByNode.get(node);
+  }
+
+  public Collection<QueryBlock> getQueryBlocks() {
+    return queryBlocks.values();
+  }
+
+  public boolean postVisit(String blockName, LogicalNode node, Stack<ExprType> path) {
+    if (visited.contains(node)) {
+      return false;
+    }
+
+    QueryBlock block = queryBlocks.get(blockName);
+
+    // if an added operator is a relation, add it to relation set.
+    switch (node.getType()) {
+      case SCAN:
+        ScanNode relationOp = (ScanNode) node;
+        block.addRelation(relationOp);
+        break;
+
+      case GROUP_BY:
+        block.resolveGrouping();
+        break;
+    }
+
+
+    // if this node is the topmost
+    if (path.size() == 0) {
+      block.setRoot(node);
+    }
+
+    return true;
+  }
+
+  /**
+   * It tries to find a column with a qualified column name.
+   *
+   * @throws VerifyException this exception occurs if there is no column matched to the given name.
+   */
+  public Column findColumnFromRelation(String blockName, String relName, String name)
+      throws VerifyException {
+
+    QueryBlock block = queryBlocks.get(blockName);
+    ScanNode relationOp = block.getRelation(relName);
+
+    // if a column name is outside of this query block
+    if (relationOp == null) {
+      // TODO - nested query can only refer outer query block? or not?
+      for (QueryBlock eachBlock : queryBlocks.values()) {
+        if (eachBlock.containRelation(relName)) {
+          relationOp = eachBlock.getRelation(relName);
+        }
+      }
+    }
+
+    if (relationOp == null) {
+      throw new NoSuchColumnException(relName + "." + name);
+    }
+
+    Schema schema = relationOp.getTableSchema();
+
+    Column column = schema.getColumnByName(name);
+    if (column == null) {
+      throw new VerifyException("ERROR: no such a column "+ name);
+    }
+
+    try {
+      column = (Column) column.clone();
+    } catch (CloneNotSupportedException e) {
+      e.printStackTrace();
+    }
+    String tableName = relationOp.getTableId();
+    column.setName(tableName + "." + column.getColumnName());
+
+    return column;
+  }
+
+  /**
+   * Try to find column from the output of child plans.
+   *
+   * @throws VerifyException
+   */
+  public Column findColumnFromChildNode(ColumnReferenceExpr columnRef, String blockName,
+                                        LogicalNode node)
+      throws VerifyException{
+    List<Column> candidates = new ArrayList<Column>();
+
+    Column candidate;
+    if (columnRef.hasTableName()) {
+      candidate = node.getOutSchema().getColumn(columnRef.getCanonicalName());
+
+      if (candidate == null) { // If not found, try to find the column with alias name
+        String tableName = getBlock(blockName).getRelation(columnRef.getTableName()).getTableId();
+        candidate = node.getOutSchema().getColumn(tableName + "." + columnRef.getName());
+      }
+      candidates.add(candidate);
+
+    } else {
+      candidate = node.getOutSchema().getColumnByName(columnRef.getName());
+      candidates.add(candidate);
+    }
+
+    if (candidates.isEmpty()) {
+      throw new VerifyException("ERROR: no such a column name "+ columnRef.getCanonicalName());
+    } else  if (candidates.size() > 1) {
+      throw new VerifyException("ERROR: column name "+ columnRef.getCanonicalName()
+          + " is ambiguous");
+    }
+
+    return candidates.get(0);
+  }
+
+
+  public Column findColumn(String blockName, ColumnReferenceExpr columnRef) throws VerifyException {
+    if (columnRef.hasTableName()) {
+      return findColumnFromRelation(blockName, columnRef.getTableName(), columnRef.getName());
+    } else {
+      return suspectColumn(blockName, columnRef.getName());
+    }
+  }
+
+  /**
+   * This method tries to find one column with only column name.
+   * If it do not find any column corresponding to the given name, it tries to find the column from other blocks.
+   * If it finds two or more columns corresponding to the given name, it incurs @{link VerifyException}.
+   *
+   * @param blockName The block name is the first priority block used for searching a column corresponding to the name.
+   * @param name The column name to be found
+   * @return the found column
+   *
+   * @throws VerifyException If there are two or more found columns, the exception will be caused.
+   */
+  public Column suspectColumn(String blockName, String name) throws VerifyException {
+    List<Column> candidates = new ArrayList<Column>();
+    Column candidate;
+
+    // Try to find a column from the current query block
+    for (ScanNode rel : queryBlocks.get(blockName).getRelations()) {
+      candidate = findColumnFromRelationOp(rel, name);
+
+      if (candidate != null) {
+        if (!blockName.equals(LogicalPlan.ROOT_BLOCK)) {
+          try {
+            candidate = (Column) candidate.clone();
+          } catch (CloneNotSupportedException e) {
+            e.printStackTrace();
+          }
+          candidate.setName(rel.getTableId() + "." + candidate.getColumnName());
+        }
+        candidates.add(candidate);
+        if (candidates.size() > 0) {
+          break;
+        }
+      }
+    }
+
+    // if a column is not found, try to find the column from outer blocks.
+    if (candidates.isEmpty()) {
+      // for each block
+      Outer:
+      for (QueryBlock block : queryBlocks.values()) {
+        for (ScanNode rel : block.getRelations()) {
+          candidate = findColumnFromRelationOp(rel, name);
+
+          if (candidate != null) {
+            if (!blockName.equals(LogicalPlan.ROOT_BLOCK)) {
+              try {
+                candidate = (Column) candidate.clone();
+              } catch (CloneNotSupportedException e) {
+                e.printStackTrace();
+              }
+              candidate.setName(rel.getTableId() + "." + candidate.getColumnName());
+            }
+            candidates.add(candidate);
+            if (candidates.size() > 0)
+              break Outer;
+          }
+        }
+      }
+    }
+
+    if (candidates.isEmpty()) {
+      throw new VerifyException("ERROR: no such a column name "+ name);
+    } else  if (candidates.size() > 1) {
+      throw new VerifyException("ERROR: column name "+ name + " is ambiguous");
+    }
+
+    return candidates.get(0);
+  }
+
+  private Column findColumnFromRelationOp(ScanNode relation, String name) throws VerifyException {
+    Column candidate = relation.getTableSchema().getColumnByName(name);
+    if (candidate != null) {
+      try {
+        candidate = (Column) candidate.clone();
+      } catch (CloneNotSupportedException e) {
+        throw new RuntimeException(e);
+      }
+      if (!isVirtualRelation(relation.getCanonicalName())) {
+        candidate.setName(relation.getTableId() + "." + name);
+      }
+
+      return candidate;
+    } else {
+      return null;
+    }
+  }
+
+  public static boolean isVirtualRelation(String relationName) {
+    return relationName.charAt(0) == VIRTUAL_TABLE_PREFIX;
+  }
+
+  public class QueryBlock {
+    private String blockName;
+    private LogicalNode blockRoot;
+    private Map<String, ScanNode> relations = new HashMap<String, ScanNode>();
+    private Projection projection;
+
+    private boolean resolvedGrouping = true;
+    private boolean hasGrouping;
+    private Projectable projectionNode;
+    private SelectionNode selectionNode;
+    private GroupbyNode groupingNode;
+    private Schema schema;
+
+    TargetListManager targetListManager;
+
+    public QueryBlock(String blockName) {
+      this.blockName = blockName;
+    }
+
+    public String getName() {
+      return blockName;
+    }
+
+    public boolean hasRoot() {
+      return blockRoot != null;
+    }
+
+    public void setRoot(LogicalNode blockRoot) {
+      this.blockRoot = blockRoot;
+      queryBlockByNode.put(blockRoot, this);
+    }
+
+    public LogicalNode getRoot() {
+      return blockRoot;
+    }
+
+    public boolean containRelation(String name) {
+      return relations.containsKey(name);
+    }
+
+    public void addRelation(ScanNode relation) {
+      relations.put(relation.getCanonicalName(), relation);
+    }
+
+    public ScanNode getRelation(String name) {
+      return relations.get(name);
+    }
+
+    public Collection<ScanNode> getRelations() {
+      return this.relations.values();
+    }
+
+    public void setSchema(Schema schema) {
+      this.schema = schema;
+    }
+
+    public Schema getSchema() {
+      return schema;
+    }
+
+    public boolean hasTableExpression() {
+      return this.relations.size() > 0;
+    }
+
+    public void setProjection(Projection projection) {
+      this.projection = projection;
+    }
+
+    public Projection getProjection() {
+      return this.projection;
+    }
+
+    public void setProjectionNode(Projectable node) {
+      this.projectionNode = node;
+    }
+
+    public Projectable getProjectionNode() {
+      return this.projectionNode;
+    }
+
+    public boolean isGroupingResolved() {
+      return this.resolvedGrouping;
+    }
+
+    public void resolveGrouping() {
+      this.resolvedGrouping = true;
+    }
+
+    public void setHasGrouping() {
+      hasGrouping = true;
+      resolvedGrouping = false;
+    }
+
+    public boolean hasGrouping() {
+      return hasGrouping || hasGroupingNode();
+    }
+
+    public boolean hasGroupingNode() {
+      return this.groupingNode != null;
+    }
+
+    public void setGroupingNode(GroupbyNode groupingNode) {
+      this.groupingNode = groupingNode;
+    }
+
+    public GroupbyNode getGroupingNode() {
+      return this.groupingNode;
+    }
+
+    public boolean hasSelectionNode() {
+      return this.selectionNode != null;
+    }
+
+    public void setSelectionNode(SelectionNode selectionNode) {
+      this.selectionNode = selectionNode;
+    }
+
+    public SelectionNode getSelectionNode() {
+      return selectionNode;
+    }
+
+    public String toString() {
+      return blockName;
+    }
+
+    public boolean isTargetEvaluated(int targetId) {
+      return targetListManager.isEvaluated(targetId);
+    }
+
+    public boolean canTargetEvaluated(int targetId, LogicalNode node) {
+      return (targetListManager.getTarget(targetId) != null) &&
+          PlannerUtil.canBeEvaluated(targetListManager.getTarget(targetId).getEvalTree(), node);
+    }
+
+    /**
+     * It requires node's default output schemas.
+     * @param node
+     */
+    public void checkAndSetEvaluatedTargets(LogicalNode node) {
+      if (!(node instanceof Projectable)) {
+        return;
+      }
+
+      // If all columns are projected and do not include any expression
+      if (projection.isAllProjected() && node instanceof ScanNode) {
+        targetListManager = new TargetListManager(LogicalPlan.this, PlannerUtil.schemaToTargets(node.getInSchema()));
+        targetListManager.setEvaluatedAll();
+
+      } else {
+
+        // fill a target if an annotated target can be created.
+        // Some targets which are based on multiple relations can be created only in a certain
+        // join node.
+        for (int i = 0; i < targetListManager.size(); i++) {
+          if (targetListManager.getTarget(i) == null) {
+            try {
+              targetListManager.updateTarget(i,planner.createTarget(LogicalPlan.this, blockName,
+                  projection.getTargets()[i]));
+            } catch (VerifyException e) {
+            }
+          }
+        }
+
+        // add target to list if a target can be evaluated at this node
+        List<Integer> newEvaluatedTargetIds = new ArrayList<Integer>();
+        for (int i = 0; i < targetListManager.size(); i++) {
+
+          if (targetListManager.getTarget(i) != null && !isTargetEvaluated(i)) {
+            EvalNode expr = targetListManager.getTarget(i).getEvalTree();
+
+            if (canTargetEvaluated(i, node)) {
+
+              if (node instanceof ScanNode) { // for scan node
+                if (expr.getType() == EvalNode.Type.FIELD) {
+                  targetListManager.setEvaluated(i);
+                  if (targetListManager.getTarget(i).hasAlias()) {
+                    newEvaluatedTargetIds.add(i);
+                  }
+                } else if (EvalTreeUtil.findDistinctAggFunction(expr).size() == 0) {
+                  // if this expression does no contain any aggregation function
+                  targetListManager.setEvaluated(i);
+                  newEvaluatedTargetIds.add(i);
+                }
+
+              } else if (node instanceof GroupbyNode) { // for grouping
+                if (EvalTreeUtil.findDistinctAggFunction(expr).size() > 0) {
+                  targetListManager.setEvaluated(i);
+                  newEvaluatedTargetIds.add(i);
+                }
+
+              } else if (node instanceof JoinNode) { // for join
+                if (EvalTreeUtil.findDistinctAggFunction(expr).size() == 0) {
+                  // if this expression does no contain any aggregation function,
+                  targetListManager.setEvaluated(i);
+                  newEvaluatedTargetIds.add(i);
+                }
+              }
+            }
+          }
+        }
+
+        if (node instanceof ScanNode || node instanceof JoinNode) {
+          Schema baseSchema = null;
+          if (node instanceof ScanNode) {
+            baseSchema = ((ScanNode)node).getTableSchema();
+          } else if (node instanceof JoinNode) {
+            baseSchema = node.getInSchema();
+          }
+
+          if (newEvaluatedTargetIds.size() > 0) {
+            // fill addedTargets with output columns and new expression columns (e.g., aliased column or expressions)
+            Target [] addedTargets = new Target[baseSchema.getColumnNum() + newEvaluatedTargetIds.size()];
+            PlannerUtil.schemaToTargets(baseSchema, addedTargets);
+            int baseIdx = baseSchema.getColumnNum();
+            for (int i = 0; i < newEvaluatedTargetIds.size(); i++) {
+              addedTargets[baseIdx + i] = targetListManager.getTarget(newEvaluatedTargetIds.get(i));
+            }
+
+            // set targets to ScanNode because it needs to evaluate expressions
+            ((Projectable)node).setTargets(addedTargets);
+            // the output schema of ScanNode has to have the combination of the original output and newly-added targets.
+            node.setOutSchema(LogicalPlanner.getProjectedSchema(LogicalPlan.this, addedTargets));
+          } else {
+            node.setOutSchema(node.getInSchema());
+          }
+
+          // if newEvaluatedTargetIds == 0, the original output schema will be used.
+        } else if (node instanceof GroupbyNode) {
+          // Set the current targets to the GroupByNode because the GroupByNode is the last projection operator.
+          ((Projectable)node).setTargets(targetListManager.getTargets());
+          node.setOutSchema(targetListManager.getUpdatedSchema());
+        }
+      }
+
+      // replace the evaluated targets for upper operators
+      try {
+        targetListManager.getUpdatedTarget();
+      } catch (CloneNotSupportedException e) {
+        throw new InternalError(e.getMessage());
+      }
+
+      if (targetListManager.isAllEvaluated()) {
+        schema = targetListManager.getUpdatedSchema();
+      }
+    }
+
+    public TargetListManager getTargetListManager() {
+      return targetListManager;
+    }
+
+    public Target [] getCurrentTargets() {
+      return targetListManager.getTargets();
+    }
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalPlanner.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalPlanner.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalPlanner.java
index 8f05593..d8bb157 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalPlanner.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalPlanner.java
@@ -18,36 +18,39 @@
 
 package org.apache.tajo.engine.planner;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.tajo.catalog.CatalogService;
-import org.apache.tajo.catalog.Column;
-import org.apache.tajo.catalog.Schema;
+import org.apache.hadoop.fs.Path;
+import org.apache.tajo.algebra.*;
+import org.apache.tajo.catalog.*;
+import org.apache.tajo.catalog.function.AggFunction;
+import org.apache.tajo.catalog.function.GeneralFunction;
 import org.apache.tajo.catalog.proto.CatalogProtos;
+import org.apache.tajo.common.TajoDataTypes;
 import org.apache.tajo.common.TajoDataTypes.DataType;
+import org.apache.tajo.datum.DatumFactory;
 import org.apache.tajo.engine.eval.*;
-import org.apache.tajo.engine.parser.*;
-import org.apache.tajo.engine.parser.QueryBlock.*;
+import org.apache.tajo.engine.eval.EvalNode.Type;
+import org.apache.tajo.engine.planner.LogicalPlan.QueryBlock;
 import org.apache.tajo.engine.planner.logical.*;
-import org.apache.tajo.engine.planner.logical.join.Edge;
-import org.apache.tajo.engine.planner.logical.join.JoinTree;
 import org.apache.tajo.engine.query.exception.InvalidQueryException;
 import org.apache.tajo.engine.query.exception.NotSupportQueryException;
+import org.apache.tajo.engine.query.exception.UndefinedFunctionException;
 import org.apache.tajo.engine.utils.SchemaUtil;
+import org.apache.tajo.exception.InternalException;
 
 import java.util.List;
-import java.util.Map;
-import java.util.Set;
 import java.util.Stack;
 
+import static org.apache.tajo.algebra.Aggregation.GroupType;
+
 /**
- * This class creates a logical plan from a parse tree ({@link org.apache.tajo.engine.parser.QueryBlock})
- * generated by {@link org.apache.tajo.engine.parser.QueryAnalyzer}.
+ * This class creates a logical plan from a parse tree ({@link org.apache.tajo.engine.parser.SQLAnalyzer})
+ * generated by {@link org.apache.tajo.engine.parser.SQLAnalyzer}.
  *
- * @see org.apache.tajo.engine.parser.QueryBlock
+ * @see org.apache.tajo.engine.parser
  */
 public class LogicalPlanner {
   private static Log LOG = LogFactory.getLog(LogicalPlanner.class);
@@ -60,358 +63,461 @@ public class LogicalPlanner {
   /**
    * This generates a logical plan.
    *
-   * @param context
-   * @return a initial logical plan
+   * @param expr A relational algebraic expression for a query.
+   * @return A logical plan
    */
-  public LogicalNode createPlan(PlanningContext context) {
-    LogicalNode plan;
+  public LogicalPlan createPlan(Expr expr) {
+
+    LogicalPlan plan = new LogicalPlan(this);
+    LogicalNode subroot = null;
 
+    Stack<ExprType> stack =
+        new Stack<ExprType>();
+
+    QueryBlock rootBlock = plan.newAndGetBlock(LogicalPlan.ROOT_BLOCK);
     try {
-      plan = createPlanInternal(context, context.getParseTree());
+      subroot = createPlanInternal(plan, rootBlock, expr, stack);
     } catch (CloneNotSupportedException e) {
       throw new InvalidQueryException(e);
+    } catch (VerifyException e) {
+      e.printStackTrace();
     }
 
     LogicalRootNode root = new LogicalRootNode();
-    root.setInSchema(plan.getOutSchema());
-    root.setOutSchema(plan.getOutSchema());
-    root.setSubNode(plan);
-
-    return root;
-  }
-  
-  private LogicalNode createPlanInternal(PlanningContext ctx,
-      ParseTree query) throws CloneNotSupportedException {
-    LogicalNode plan;
-    
-    switch(query.getType()) {
-    case SELECT:
-      LOG.info("Planning select statement");
-      QueryBlock select = (QueryBlock) query;
-      plan = buildSelectPlan(ctx, select);
-      break;
-      
-    case UNION:
-    case EXCEPT:
-    case INTERSECT:
-      SetStmt set = (SetStmt) query;
-      plan = buildSetPlan(ctx, set);
-      break;
-      
-    case CREATE_INDEX:
-      LOG.info("Planning create index statement");
-      CreateIndexStmt createIndex = (CreateIndexStmt) query;
-      plan = buildCreateIndexPlan(createIndex);
-      break;
-
-    case CREATE_TABLE:
-    case CREATE_TABLE_AS:
-      LOG.info("Planning store statement");
-      CreateTableStmt createTable = (CreateTableStmt) query;
-      plan = buildCreateTablePlan(ctx, createTable);
-      break;
-
-    default:
-      throw new NotSupportQueryException(query.toString());
-    }
-    
+    root.setInSchema(subroot.getOutSchema());
+    root.setOutSchema(subroot.getOutSchema());
+    root.setSubNode(subroot);
+    plan.getRootBlock().setRoot(root);
+
     return plan;
   }
 
-  private LogicalNode buildSetPlan(PlanningContext ctx, SetStmt stmt)
-      throws CloneNotSupportedException {
-    BinaryNode bin;
-    switch (stmt.getType()) {
-    case UNION:
-      bin = new UnionNode();
-      break;
-    case EXCEPT:
-      bin = new ExceptNode();
-      break;
-    case INTERSECT:
-      bin = new IntersectNode();
-      break;
-    default:
-      throw new IllegalStateException("the statement cannot be matched to any set operation type");
-    }
-    
-    bin.setOuter(createPlanInternal(ctx, stmt.getLeftTree()));
-    bin.setInner(createPlanInternal(ctx, stmt.getRightTree()));
-    bin.setInSchema(bin.getOuterNode().getOutSchema());
-    bin.setOutSchema(bin.getOuterNode().getOutSchema());
-    return bin;
-  }
-
-  private LogicalNode buildCreateIndexPlan(CreateIndexStmt stmt) {
-    FromTable table = new FromTable(catalog.getTableDesc(stmt.getTableName()));
-    ScanNode scan = new ScanNode(table);
-    scan.setInSchema(table.getSchema());
-    scan.setOutSchema(table.getSchema());
-    IndexWriteNode indexWrite = new IndexWriteNode(stmt);
-    indexWrite.setSubNode(scan);
-    indexWrite.setInSchema(scan.getOutSchema());
-    indexWrite.setOutSchema(scan.getOutSchema());
-    
-    return indexWrite;
-  }
-
-  private static LogicalNode buildCreateTablePlan(final PlanningContext ctx,
-                                                  final CreateTableStmt query)
-      throws CloneNotSupportedException {
-    LogicalNode node;
+  /**
+   * Relational operators can be divided into two categories as follows:
+   * <oi>
+   *  <li>General operator: this type operators do not affect tuple schema and do not evaluate
+   *  expressions. Selection, Sort, and Limit belong to this operator.</li>
+   *  <li>Projectable operator: this type operators affects tuple schema and evaluate expressions.
+   *  Scan, Groupby, and Join belong to this operators.
+   *  </li>
+   * </oi>
+   */
+  private LogicalNode createPlanInternal(LogicalPlan plan, QueryBlock block, Expr expr, Stack<ExprType> stack)
+      throws CloneNotSupportedException, VerifyException {
+    LogicalNode currentNode;
+    QueryBlock currentBlock = checkNewBlockAndGet(plan, block.getName());
 
-    if (query.hasQueryBlock()) {
-      LogicalNode selectPlan = buildSelectPlan(ctx, query.getSelectStmt());
-      StoreTableNode storeNode = new StoreTableNode(query.getTableName());
+    switch(expr.getType()) {
+      case Projection:
+        Projection projection = (Projection) expr;
+        currentNode = buildProjectionNode(plan, currentBlock, projection, stack);
+        break;
 
-      storeNode.setSubNode(selectPlan);
+      case Filter:
+        Selection selection = (Selection) expr;
+        currentNode = buildSelectionNode(plan, currentBlock, selection, stack);
+        break;
 
-      if (query.hasDefinition()) {
-        storeNode.setOutSchema(query.getTableDef());
-      } else {
-        // TODO - strip qualified name
-        storeNode.setOutSchema(selectPlan.getOutSchema());
-      }
-      storeNode.setInSchema(selectPlan.getOutSchema());
+      case Aggregation:
+        Aggregation aggregation = (Aggregation) expr;
+        currentNode = buildGroupingPlan(plan, currentBlock, aggregation, stack);
+        break;
 
-      if (query.hasStoreType()) {
-        storeNode.setStorageType(query.getStoreType());
-      } else {
-        // default type
-        // TODO - it should be configurable.
-        storeNode.setStorageType(CatalogProtos.StoreType.CSV);
-      }
-      if (query.hasOptions()) {
-        storeNode.setOptions(query.getOptions());
-      }
+      case Join:
+        Join join = (Join) expr;
+        currentNode = buildExplicitJoinPlan(plan, currentBlock, join, stack);
+        break;
 
-      node = storeNode;
-    } else {
-      CreateTableNode createTable =
-          new CreateTableNode(query.getTableName(), query.getTableDef());
+      case Sort:
+        Sort sort = (Sort) expr;
+        currentNode = buildSortPlan(plan, currentBlock, sort, stack);
+        break;
 
-      if (query.hasStoreType()) {
-        createTable.setStorageType(query.getStoreType());
-      } else {
-        // default type
-        // TODO - it should be configurable.
-        createTable.setStorageType(CatalogProtos.StoreType.CSV);
-      }
-      if (query.hasOptions()) {
-        createTable.setOptions(query.getOptions());
-      }
+      case Limit:
+        Limit limit = (Limit) expr;
+        currentNode = buildLimitPlan(plan, currentBlock, limit, stack);
+        break;
 
-      if (query.hasPath()) {
-        createTable.setPath(query.getPath());
-      }
-      createTable.setExternal(query.isExternal());
-      node = createTable;
+      case Union:
+      case Except:
+      case Intersect:
+        SetOperation setOp = (SetOperation) expr;
+        currentNode = buildSetPlan(plan, currentBlock, setOp, stack);
+        break;
+
+
+      case RelationList:
+        currentNode = buildRelationListPlan(plan, currentBlock, (RelationList) expr, stack);
+        if (((RelationList) expr).size() == 1) { // skip visitPost because it is already visited
+          return currentNode;
+        }
+        break;
+
+      case Relation:
+        currentNode = buildScanPlan(currentBlock, expr);
+        break;
+
+      case CreateTable:
+        CreateTable createTable = (CreateTable) expr;
+        currentNode = buildCreateTable(plan, currentBlock, createTable, stack);
+        break;
+
+      case DropTable:
+        DropTable dropTable = (DropTable) expr;
+        currentNode = buildDropTable(dropTable);
+        break;
+
+      default:
+        throw new NotSupportQueryException(expr.getType().name());
     }
-    
-    return node;
+
+    // mark the node as the visited node and do post work for each operator
+    plan.postVisit(block.getName(), currentNode, stack);
+    // check and set evaluated targets and update in/out schemas
+    currentBlock.checkAndSetEvaluatedTargets(currentNode);
+    return currentNode;
   }
-  
+
   /**
-   * ^(SELECT from_clause? where_clause? groupby_clause? selectList)
-   * 
-   * @param query
-   * @return the planed logical plan
+   * It checks if the first node in this query block. If not, it creates and adds a new query block.
+   * In addition, it always returns the query block corresponding to the block name.
    */
-  private static LogicalNode buildSelectPlan(PlanningContext ctx,
-                                             QueryBlock query)
-      throws CloneNotSupportedException {
-    LogicalNode subroot;
-    EvalNode whereCondition = null;
-    EvalNode [] cnf = null;
-    if(query.hasWhereClause()) {
-      whereCondition = query.getWhereCondition();
-      whereCondition = AlgebraicUtil.simplify(whereCondition);
-      cnf = EvalTreeUtil.getConjNormalForm(whereCondition);
-    }
-
-    if(query.hasFromClause()) {
-      if (query.hasExplicitJoinClause()) {
-        subroot = createExplicitJoinTree(query);
-      } else {
-        subroot = createImplicitJoinTree(query.getFromTables(), cnf);
-      }
+  private QueryBlock checkNewBlockAndGet(LogicalPlan plan, String blockName) {
+    QueryBlock block = plan.getBlock(blockName);
+    if (block == null) {
+      return plan.newAndGetBlock(blockName);
     } else {
-      subroot = new EvalExprNode(query.getTargetList());
-      subroot.setOutSchema(getProjectedSchema(ctx, query.getTargetList()));
-      return subroot;
-    }
-    
-    if(whereCondition != null) {
-      SelectionNode selNode = 
-          new SelectionNode(query.getWhereCondition());
-      selNode.setSubNode(subroot);
-      selNode.setInSchema(subroot.getOutSchema());
-      selNode.setOutSchema(selNode.getInSchema());
-      subroot = selNode;
-    }
-    
-    if(query.hasAggregation()) {
-      if (query.isDistinct()) {
-        throw new InvalidQueryException("Cannot support GROUP BY queries with distinct keyword");
+      return block;
+    }
+  }
+
+  private ScanNode buildScanPlan(QueryBlock block, Expr expr)
+      throws VerifyException {
+    // 1. init phase
+
+    // 2. build child plans
+    // 3. build scan plan
+    Relation relation = (Relation) expr;
+    TableDesc desc = catalog.getTableDesc(relation.getName());
+    FromTable fromTable = new FromTable(desc);
+
+    if (relation.hasAlias()) {
+      fromTable.setAlias(relation.getAlias());
+    }
+
+    ScanNode scanNode = new ScanNode(fromTable);
+
+    return scanNode;
+  }
+
+  /*===============================================================================================
+    JOIN SECTION
+   ===============================================================================================*/
+  private LogicalNode buildRelationListPlan(LogicalPlan plan, QueryBlock block,
+                                            RelationList relations, Stack<ExprType> stack)
+      throws VerifyException, CloneNotSupportedException {
+
+    LogicalNode current = createPlanInternal(plan, block, relations.getRelations()[0], stack);
+
+    LogicalNode left;
+    LogicalNode right;
+    if (relations.size() > 1) {
+
+      for (int i = 1; i < relations.size(); i++) {
+        left = current;
+        right = createPlanInternal(plan, block, relations.getRelations()[i], stack);
+        current = createCatasianProduct(left, right);
       }
+    }
 
-      GroupbyNode groupbyNode = null;
-      if (query.hasGroupbyClause()) {
-        if (query.getGroupByClause().getGroupSet().get(0).getType() == GroupType.GROUPBY) {          
-          groupbyNode = new GroupbyNode(query.getGroupByClause().getGroupSet().get(0).getColumns());
-          groupbyNode.setTargetList(query.getTargetList());
-          groupbyNode.setSubNode(subroot);
-          groupbyNode.setInSchema(subroot.getOutSchema());
-          Schema outSchema = getProjectedSchema(ctx, query.getTargetList());
-          groupbyNode.setOutSchema(outSchema);
-          subroot = groupbyNode;
-        } else if (query.getGroupByClause().getGroupSet().get(0).getType() == GroupType.CUBE) {
-          LogicalNode union = createGroupByUnionByCube(ctx, query,
-              subroot, query.getGroupByClause());
-          Schema outSchema = getProjectedSchema(ctx, query.getTargetList());
-          union.setOutSchema(outSchema);
-          subroot = union;
-        }
-        if(query.hasHavingCond())
-          groupbyNode.setHavingCondition(query.getHavingCond());
+    return current;
+  }
+
+  private LogicalNode buildExplicitJoinPlan(LogicalPlan plan, QueryBlock block, Join join, Stack<ExprType> stack)
+      throws VerifyException, CloneNotSupportedException {
+    // Phase 1: Init
+    // Phase 2: build child plans
+    stack.push(ExprType.JOIN);
+    LogicalNode left = createPlanInternal(plan, block, join.getLeft(), stack);
+    LogicalNode right = createPlanInternal(plan, block, join.getRight(), stack);
+    stack.pop();
+
+    // Phase 3: build this plan
+    JoinNode joinNode = new JoinNode(join.getJoinType(), left, right);
+
+    // Set A merged input schema
+    Schema merged;
+    if (join.isNatural()) {
+      merged = getNaturalJoin(left, right);
+    } else {
+      merged = SchemaUtil.merge(left.getOutSchema(), right.getOutSchema());
+    }
+    joinNode.setInSchema(merged);
+    joinNode.setOutSchema(merged);
+
+    // Determine join conditions
+    if (join.isNatural()) { // if natural join, it should have the equi-join conditions by common column names
+      Schema leftSchema = joinNode.getOuterNode().getInSchema();
+      Schema rightSchema = joinNode.getInnerNode().getInSchema();
+      Schema commons = SchemaUtil.getCommons(leftSchema, rightSchema);
+      EvalNode njCond = getNaturalJoinCondition(leftSchema, rightSchema, commons);
+      joinNode.setJoinQual(njCond);
+    } else if (join.hasQual()) { // otherwise, the given join conditions are set
+      joinNode.setJoinQual(createEvalTree(plan, block.getName(), join.getQual()));
+    }
+
+    return joinNode;
+  }
+
+  private static EvalNode getNaturalJoinCondition(Schema outer, Schema inner, Schema commons) {
+    EvalNode njQual = null;
+    EvalNode equiQual;
+
+    Column leftJoinKey;
+    Column rightJoinKey;
+    for (Column common : commons.getColumns()) {
+      leftJoinKey = outer.getColumnByName(common.getColumnName());
+      rightJoinKey = inner.getColumnByName(common.getColumnName());
+      equiQual = new BinaryEval(EvalNode.Type.EQUAL,
+          new FieldEval(leftJoinKey), new FieldEval(rightJoinKey));
+      if (njQual == null) {
+        njQual = equiQual;
       } else {
-        // when aggregation functions are used without grouping fields
-        groupbyNode = new GroupbyNode(new Column[] {});
-        groupbyNode.setTargetList(query.getTargetList());
-        groupbyNode.setSubNode(subroot);
-        groupbyNode.setInSchema(subroot.getOutSchema());
-        Schema outSchema = getProjectedSchema(ctx, query.getTargetList());
-        groupbyNode.setOutSchema(outSchema);
-        subroot = groupbyNode;
+        njQual = new BinaryEval(EvalNode.Type.AND,
+            njQual, equiQual);
       }
     }
-    
-    if(query.hasOrderByClause()) {
-      SortNode sortNode = new SortNode(query.getSortKeys());
-      sortNode.setSubNode(subroot);
-      sortNode.setInSchema(subroot.getOutSchema());
-      sortNode.setOutSchema(sortNode.getInSchema());
-      subroot = sortNode;
-    }
-
-    ProjectionNode prjNode;
-    if (query.getProjectAll()) {
-      Schema merged = SchemaUtil.merge(query.getFromTables());
-      Target [] allTargets = PlannerUtil.schemaToTargets(merged);
-      prjNode = new ProjectionNode(allTargets);
-      prjNode.setSubNode(subroot);
-      prjNode.setInSchema(merged);
-      prjNode.setOutSchema(merged);
-      subroot = prjNode;
-      query.setTargetList(allTargets);
-    } else {
-      prjNode = new ProjectionNode(query.getTargetList());
-      if (subroot != null) { // false if 'no from' statement
-        prjNode.setSubNode(subroot);
+
+    return njQual;
+  }
+
+  private static LogicalNode createCatasianProduct(LogicalNode left, LogicalNode right) {
+    JoinNode join = new JoinNode(JoinType.CROSS_JOIN, left, right);
+    Schema joinSchema = SchemaUtil.merge(
+        join.getOuterNode().getOutSchema(),
+        join.getInnerNode().getOutSchema());
+    join.setInSchema(joinSchema);
+    join.setOutSchema(joinSchema);
+
+    return join;
+  }
+
+  private static Schema getNaturalJoin(LogicalNode outer, LogicalNode inner) {
+    Schema joinSchema = new Schema();
+    Schema commons = SchemaUtil.getCommons(outer.getOutSchema(),
+        inner.getOutSchema());
+    joinSchema.addColumns(commons);
+    for (Column c : outer.getOutSchema().getColumns()) {
+      for (Column common : commons.getColumns()) {
+        if (!common.getColumnName().equals(c.getColumnName())) {
+          joinSchema.addColumn(c);
+        }
       }
-      prjNode.setInSchema(subroot.getOutSchema());
-
-      // All aggregate functions are evaluated before the projection.
-      // So, the targets for aggregate functions should be updated.
-      LogicalOptimizer.TargetListManager tlm = new LogicalOptimizer.
-          TargetListManager(ctx, query.getTargetList());
-      for (int i = 0; i < tlm.getTargets().length; i++) {
-        if (EvalTreeUtil.findDistinctAggFunction(tlm.getTarget(i).getEvalTree()).size() > 0) {
-          tlm.setEvaluated(i);
+    }
+
+    for (Column c : inner.getOutSchema().getColumns()) {
+      for (Column common : commons.getColumns()) {
+        if (!common.getColumnName().equals(c.getColumnName())) {
+          joinSchema.addColumn(c);
         }
       }
-      prjNode.setTargetList(tlm.getUpdatedTarget());
-      Schema projected = getProjectedSchema(ctx, tlm.getUpdatedTarget());
-      prjNode.setOutSchema(projected);
-      subroot = prjNode;
     }
+    return joinSchema;
+  }
+
+  /*===============================================================================================
+    SET OPERATION SECTION
+   ===============================================================================================*/
+  private LogicalNode buildSetPlan(LogicalPlan plan, QueryBlock block, SetOperation setOperation,
+                                   Stack<ExprType> stack) throws VerifyException, CloneNotSupportedException {
+    LogicalNode left;
+    LogicalNode right;
 
-    GroupbyNode dupRemoval;
-    if (query.isDistinct()) {
-      dupRemoval = new GroupbyNode(subroot.getOutSchema().toArray());
-      dupRemoval.setTargetList(query.getTargetList());
-      dupRemoval.setSubNode(subroot);
-      dupRemoval.setInSchema(subroot.getOutSchema());
-      Schema outSchema = getProjectedSchema(ctx, query.getTargetList());
-      dupRemoval.setOutSchema(outSchema);
-      subroot = dupRemoval;
+    QueryBlock leftBlock = plan.newAnonymousBlock();
+    Stack<ExprType> leftStack = new Stack<ExprType>();
+    left = createPlanInternal(plan, leftBlock, setOperation.getLeft(), leftStack);
+    Stack<ExprType> rightStack = new Stack<ExprType>();
+    QueryBlock rightBlock = plan.newAnonymousBlock();
+    right = createPlanInternal(plan, rightBlock, setOperation.getRight(), rightStack);
+
+    verifySetStatement(setOperation.getType(), leftBlock, rightBlock);
+
+    BinaryNode setOp;
+    if (setOperation.getType() == OpType.Union) {
+      setOp = new UnionNode(left, right);
+    } else if (setOperation.getType() == OpType.Except) {
+      setOp = new ExceptNode(left, right);
+    } else if (setOperation.getType() == OpType.Intersect) {
+      setOp = new IntersectNode(left, right);
+    } else {
+      throw new VerifyException(setOperation.toJson());
     }
 
-    if (query.hasLimitClause()) {
-      LimitNode limitNode = new LimitNode(query.getLimitClause());
-      limitNode.setSubNode(subroot);
-      limitNode.setInSchema(subroot.getOutSchema());
-      limitNode.setOutSchema(limitNode.getInSchema());
-      subroot = limitNode;
+    // Strip the table names from the targets of the both blocks
+    // in order to check the equivalence the schemas of both blocks.
+    Target [] leftStrippedTargets = PlannerUtil.stripTarget(leftBlock.getCurrentTargets());
+
+    Schema outSchema = PlannerUtil.targetToSchema(leftStrippedTargets);
+    setOp.setInSchema(left.getOutSchema());
+    setOp.setOutSchema(outSchema);
+    setOp.setOuter(left);
+    setOp.setInner(right);
+
+    if (isNoUpperProjection(stack)) {
+//      ProjectionNode projectionNode = new ProjectionNode(leftStrippedTargets);
+//      projectionNode.setSubNode(setOp);
+
+//      localBlock.setProjectionNode(projectionNode);
+      block.targetListManager = new TargetListManager(plan, leftStrippedTargets);
+      block.targetListManager.setEvaluatedAll();
+      block.targetListManager.getUpdatedTarget();
+      block.setSchema(block.targetListManager.getUpdatedSchema());
     }
-    
-    return subroot;
+
+    return setOp;
   }
 
-  public static LogicalNode createGroupByUnionByCube(
-      final PlanningContext context,
-      final QueryBlock queryBlock,
-      final LogicalNode subNode,
-      final GroupByClause clause) {
+  private boolean verifySetStatement(OpType type, QueryBlock left, QueryBlock right)
+      throws VerifyException {
 
-    GroupElement element = clause.getGroupSet().get(0);
+    if (left.getCurrentTargets().length != right.getCurrentTargets().length) {
+      throw new VerifyException("ERROR: each " + type.name() + " query must have the same number of columns");
+    }
 
-    List<Column []> cuboids  = generateCuboids(element.getColumns());
+    Target [] targets1 = left.getCurrentTargets();
+    Target [] targets2 = right.getCurrentTargets();
 
-    return createGroupByUnion(context, queryBlock, subNode, cuboids, 0);
+    for (int i = 0; i < targets1.length; i++) {
+      if (!targets1[i].getDataType().equals(targets2[i].getDataType())) {
+        throw new VerifyException("UNION types " + targets1[i].getDataType().getType() + " and "
+            + targets2[i].getDataType().getType() + " cannot be matched");
+      }
+    }
+
+    return true;
   }
 
-  private static Target [] cloneTargets(Target [] srcs)
-      throws CloneNotSupportedException {
-    Target [] clone = new Target[srcs.length];
-    for (int i = 0; i < srcs.length; i++) {
-      clone[i] = (Target) srcs[i].clone();
-    }
+  private SelectionNode buildSelectionNode(LogicalPlan plan, QueryBlock block, Selection selection,
+                                           Stack<ExprType> stack)
+      throws VerifyException, CloneNotSupportedException {
+    // 1. init phase:
 
-    return clone;
+    // 2. build child plans:
+    stack.push(ExprType.SELECTION);
+    LogicalNode child = createPlanInternal(plan, block, selection.getChild(), stack);
+    stack.pop();
+
+    // 3. build this plan:
+    EvalNode searchCondition = createEvalTree(plan, block.getName(), selection.getQual());
+    SelectionNode selectionNode = new SelectionNode(searchCondition);
+
+    // 4. set child plan, update input/output schemas:
+    selectionNode.setSubNode(child);
+    selectionNode.setInSchema(child.getOutSchema());
+    selectionNode.setOutSchema(child.getOutSchema());
+
+    // 5. update block information:
+    block.setSelectionNode(selectionNode);
+
+    return selectionNode;
+  }
+
+  /*===============================================================================================
+    GROUP BY SECTION
+   ===============================================================================================*/
+
+  private LogicalNode buildGroupingPlan(LogicalPlan plan, QueryBlock block, Aggregation aggregation,
+                                        Stack<ExprType> stack)
+      throws VerifyException, CloneNotSupportedException {
+
+    // 1. Initialization Phase:
+
+    // 2. Build Child Plan Phase:
+    stack.push(ExprType.GROUP_BY);
+    LogicalNode child = createPlanInternal(plan, block, aggregation.getChild(), stack);
+    stack.pop();
+
+    // 3. Build This Plan:
+    Aggregation.GroupElement [] groupElements = aggregation.getGroupSet();
+
+    if (groupElements[0].getType() == GroupType.OrdinaryGroup) { // for group-by
+      GroupElement annotatedElements [] = new GroupElement[groupElements.length];
+      for (int i = 0; i < groupElements.length; i++) {
+        annotatedElements[i] = new GroupElement(
+            groupElements[i].getType(),
+            annotateGroupingColumn(plan, block.getName(), groupElements[i].getColumns(), child));
+      }
+      GroupbyNode groupingNode = new GroupbyNode(annotatedElements[0].getColumns());
+      if (aggregation.hasHavingCondition()) {
+        groupingNode.setHavingCondition(
+            createEvalTree(plan, block.getName(), aggregation.getHavingCondition()));
+      }
+
+      // 4. Set Child Plan and Update Input Schemes Phase
+      groupingNode.setSubNode(child);
+      block.setGroupingNode(groupingNode);
+      groupingNode.setInSchema(child.getInSchema());
+
+      // 5. Update Output Schema and Targets for Upper Plan
+
+      return groupingNode;
+
+    } else if (groupElements[0].getType() == GroupType.Cube) { // for cube by
+      List<Column []> cuboids  = generateCuboids(annotateGroupingColumn(plan, block.getName(),
+          groupElements[0].getColumns(), child));
+      UnionNode topUnion = createGroupByUnion(plan, block, child, cuboids, 0);
+      block.resolveGrouping();
+      block.getTargetListManager().setEvaluatedAll();
+
+      return topUnion;
+    } else {
+      throw new InvalidQueryException("Not support grouping");
+    }
   }
 
-  private static UnionNode createGroupByUnion(final PlanningContext context,
-                                              final QueryBlock queryBlock,
-                                              final LogicalNode subNode,
-                                              final List<Column []> cuboids,
-                                              final int idx) {
+  private UnionNode createGroupByUnion(final LogicalPlan plan,
+                                       final QueryBlock block,
+                                       final LogicalNode subNode,
+                                       final List<Column []> cuboids,
+                                       final int idx) {
     UnionNode union;
     try {
       if ((cuboids.size() - idx) > 2) {
         GroupbyNode g1 = new GroupbyNode(cuboids.get(idx));
-        Target [] clone = cloneTargets(queryBlock.getTargetList());
+        Target [] clone = cloneTargets(block.getCurrentTargets());
 
-        g1.setTargetList(clone);
+        g1.setTargets(clone);
         g1.setSubNode((LogicalNode) subNode.clone());
         g1.setInSchema(g1.getSubNode().getOutSchema());
-        Schema outSchema = getProjectedSchema(context, queryBlock.getTargetList());
+        Schema outSchema = getProjectedSchema(plan, block.getCurrentTargets());
         g1.setOutSchema(outSchema);
 
-        union = new UnionNode(g1, createGroupByUnion(context, queryBlock,
-            subNode, cuboids, idx+1));
+        LogicalNode right = createGroupByUnion(plan, block, subNode, cuboids, idx+1);
+        union = new UnionNode(g1, right);
         union.setInSchema(g1.getOutSchema());
         union.setOutSchema(g1.getOutSchema());
+
         return union;
       } else {
         GroupbyNode g1 = new GroupbyNode(cuboids.get(idx));
-        Target [] clone = cloneTargets(queryBlock.getTargetList());
-        g1.setTargetList(clone);
+        Target [] clone = cloneTargets(block.getCurrentTargets());
+        g1.setTargets(clone);
         g1.setSubNode((LogicalNode) subNode.clone());
         g1.setInSchema(g1.getSubNode().getOutSchema());
-        Schema outSchema = getProjectedSchema(context, queryBlock.getTargetList());
+        Schema outSchema = getProjectedSchema(plan, clone);
         g1.setOutSchema(outSchema);
 
         GroupbyNode g2 = new GroupbyNode(cuboids.get(idx+1));
-        clone = cloneTargets(queryBlock.getTargetList());
-        g2.setTargetList(clone);
+        clone = cloneTargets(block.getCurrentTargets());
+        g2.setTargets(clone);
         g2.setSubNode((LogicalNode) subNode.clone());
         g2.setInSchema(g1.getSubNode().getOutSchema());
-        outSchema = getProjectedSchema(context, queryBlock.getTargetList());
+        outSchema = getProjectedSchema(plan, clone);
         g2.setOutSchema(outSchema);
         union = new UnionNode(g1, g2);
         union.setInSchema(g1.getOutSchema());
         union.setOutSchema(g1.getOutSchema());
+
         return union;
       }
     } catch (CloneNotSupportedException cnse) {
@@ -419,17 +525,40 @@ public class LogicalPlanner {
       throw new InvalidQueryException(cnse);
     }
   }
-  
-  public static final Column [] ALL 
-    = Lists.newArrayList().toArray(new Column[0]);
-  
+
+  /**
+   * It transforms a list of column references into a list of annotated columns with considering aliased expressions.
+   */
+  private Column [] annotateGroupingColumn(LogicalPlan plan, String blockName,
+                                           ColumnReferenceExpr[] columnRefs, LogicalNode child)
+      throws VerifyException {
+    Column [] columns = new Column[columnRefs.length];
+    for (int i = 0; i < columnRefs.length; i++) {
+      columns[i] = plan.findColumnFromChildNode(columnRefs[i], blockName, child);
+    }
+
+    return columns;
+  }
+
+  private static Target [] cloneTargets(Target [] sourceTargets)
+      throws CloneNotSupportedException {
+    Target [] clone = new Target[sourceTargets.length];
+    for (int i = 0; i < sourceTargets.length; i++) {
+      clone[i] = (Target) sourceTargets[i].clone();
+    }
+
+    return clone;
+  }
+
+  public static final Column [] ALL= Lists.newArrayList().toArray(new Column[0]);
+
   public static List<Column []> generateCuboids(Column [] columns) {
     int numCuboids = (int) Math.pow(2, columns.length);
-    int maxBits = columns.length;    
-    
+    int maxBits = columns.length;
+
     List<Column []> cube = Lists.newArrayList();
     List<Column> cuboidCols;
-    
+
     cube.add(ALL);
     for (int cuboidId = 1; cuboidId < numCuboids; cuboidId++) {
       cuboidCols = Lists.newArrayList();
@@ -444,207 +573,448 @@ public class LogicalPlanner {
     return cube;
   }
 
-  private static LogicalNode createExplicitJoinTree(QueryBlock block) {
-    return createExplicitJoinTree_(block.getJoinClause());
+  /*===============================================================================================
+    SORT SECTION
+   ===============================================================================================*/
+
+  private SortNode buildSortPlan(LogicalPlan plan, QueryBlock block, Sort sort, Stack<ExprType> stack)
+      throws VerifyException, CloneNotSupportedException {
+
+    // 1. Initialization Phase:
+    // 2. Build Child Plans:
+    stack.push(ExprType.SORT);
+    LogicalNode child = createPlanInternal(plan, block, sort.getChild(), stack);
+    child = insertGroupingIfUnresolved(plan, block.getName(), child, stack);
+    stack.pop();
+
+    // 3. Build this plan:
+    SortSpec [] annotatedSortSpecs = new SortSpec[sort.getSortSpecs().length];
+    Column column;
+    Sort.SortSpec[] sortSpecs = sort.getSortSpecs();
+    for (int i = 0; i < sort.getSortSpecs().length; i++) {
+      column = plan.findColumnFromChildNode(sortSpecs[i].getKey(), block.getName(), child);
+      annotatedSortSpecs[i] = new SortSpec(column, sortSpecs[i].isAscending(),
+          sortSpecs[i].isNullFirst());
+    }
+    SortNode sortNode = new SortNode(annotatedSortSpecs);
+
+    // 4. Set Child Plan, Update Input/Output Schemas:
+    sortNode.setSubNode(child);
+    sortNode.setInSchema(child.getOutSchema());
+    sortNode.setOutSchema(child.getOutSchema());
+
+    return sortNode;
+  }
+
+  private LimitNode buildLimitPlan(LogicalPlan plan, QueryBlock block, Limit limit, Stack<ExprType> stack)
+      throws VerifyException, CloneNotSupportedException {
+    // build child plans
+    stack.push(ExprType.LIMIT);
+    LogicalNode child = createPlanInternal(plan, block, limit.getChild(), stack);
+    stack.pop();
+
+    // build limit plan
+    EvalNode firstFetchNum = createEvalTree(plan, block.getName(), limit.getFetchFirstNum());
+    firstFetchNum.eval(null, null, null);
+    LimitNode limitNode = new LimitNode(firstFetchNum.terminate(null).asInt8());
+
+    // set child plan and update input/output schemas.
+    limitNode.setSubNode(child);
+    limitNode.setInSchema(child.getOutSchema());
+    limitNode.setOutSchema(child.getOutSchema());
+    return limitNode;
   }
-  
-  private static LogicalNode createExplicitJoinTree_(JoinClause joinClause) {
 
-    JoinNode join;
-    if (joinClause.hasLeftJoin()) {
-      LogicalNode outer = createExplicitJoinTree_(joinClause.getLeftJoin());
-      join = new JoinNode(joinClause.getJoinType(), outer);
-      join.setInner(new ScanNode(joinClause.getRight()));
-    } else {
-      join = new JoinNode(joinClause.getJoinType(), new ScanNode(joinClause.getLeft()),
-          new ScanNode(joinClause.getRight()));
+  /*===============================================================================================
+    PROJECTION SECTION
+   ===============================================================================================*/
+
+  private LogicalNode buildProjectionNode(LogicalPlan plan, QueryBlock block,
+                                          Projection projection, Stack<ExprType> stack)
+      throws VerifyException, CloneNotSupportedException {
+
+    //1: init Phase
+    block.setProjection(projection);
+    if (!projection.isAllProjected()) {
+      block.targetListManager = new TargetListManager(plan, projection.size());
     }
-    if (joinClause.hasJoinQual()) {
-      join.setJoinQual(joinClause.getJoinQual());
-    } else if (joinClause.hasJoinColumns()) { 
-      // for using clause of explicit join
-      // TODO - to be implemented. Now, tajo only support 'ON' join clause.
+
+    if (!projection.hasChild()) {
+      EvalExprNode evalOnly =
+          new EvalExprNode(annotateTargets(plan, block.getName(), projection.getTargets()));
+      evalOnly.setOutSchema(getProjectedSchema(plan, evalOnly.getExprs()));
+      block.setProjectionNode(evalOnly);
+      for (int i = 0; i < evalOnly.getTargets().length; i++) {
+        block.targetListManager.updateTarget(i, evalOnly.getTargets()[i]);
+      }
+      return evalOnly;
     }
-    
-    // Determine Join Schemas
-    Schema merged;
-    if (joinClause.isNatural()) {
-      merged = getNaturalJoin(join.getOuterNode(), join.getInnerNode());
+
+    // 2: Build Child Plans
+    stack.push(ExprType.PROJECTION);
+    LogicalNode child = createPlanInternal(plan, block, projection.getChild(), stack);
+    child = insertGroupingIfUnresolved(plan, block.getName(), child, stack);
+    stack.pop();
+
+    // All targets must be evaluable before the projection.
+    Preconditions.checkState(block.getTargetListManager().isAllEvaluated(),
+        "Some targets cannot be evaluated in the query block \"%s\"", block.getName());
+
+    ProjectionNode projectionNode;
+    if (projection.isAllProjected()) {
+      projectionNode = new ProjectionNode(PlannerUtil.schemaToTargets(child.getOutSchema()));
     } else {
-      merged = SchemaUtil.merge(join.getOuterNode().getOutSchema(),
-          join.getInnerNode().getOutSchema());
-    }
-    
-    join.setInSchema(merged);
-    join.setOutSchema(merged);
-    
-    // Determine join quals
-    // if natural join, should have the equi join conditions on common columns
-    if (joinClause.isNatural()) {
-      Schema leftSchema = join.getOuterNode().getOutSchema();
-      Schema rightSchema = join.getInnerNode().getOutSchema();
-      Schema commons = SchemaUtil.getCommons(
-          leftSchema, rightSchema);
-      EvalNode njCond = getNaturalJoinCondition(leftSchema, rightSchema, commons);
-      join.setJoinQual(njCond);
-    } else if (joinClause.hasJoinQual()) { 
-      // otherwise, the given join conditions are set
-      join.setJoinQual(joinClause.getJoinQual());
+      projectionNode = new ProjectionNode(block.getCurrentTargets());
     }
-    
-    return join;
-  }
-  
-  private static EvalNode getNaturalJoinCondition(Schema outer, Schema inner, Schema commons) {
-    EvalNode njQual = null;
-    EvalNode equiQual;
-    
-    Column leftJoinKey;
-    Column rightJoinKey;
-    for (Column common : commons.getColumns()) {
-      leftJoinKey = outer.getColumnByName(common.getColumnName());
-      rightJoinKey = inner.getColumnByName(common.getColumnName());
-      equiQual = new BinaryEval(EvalNode.Type.EQUAL,
-          new FieldEval(leftJoinKey), new FieldEval(rightJoinKey));
-      if (njQual == null) {
-        njQual = equiQual;
-      } else {
-        njQual = new BinaryEval(EvalNode.Type.AND,
-            njQual, equiQual);
+
+    block.setProjectionNode(projectionNode);
+    projectionNode.setOutSchema(getProjectedSchema(plan, projectionNode.getTargets()));
+    projectionNode.setInSchema(child.getOutSchema());
+    projectionNode.setSubNode(child);
+
+    if (projection.isDistinct() && block.hasGrouping()) {
+      throw new VerifyException("Cannot support grouping and distinct at the same time");
+    } else {
+      if (projection.isDistinct()) {
+        Schema outSchema = projectionNode.getOutSchema();
+        GroupbyNode dupRemoval = new GroupbyNode(outSchema.toArray());
+        dupRemoval.setTargets(block.getTargetListManager().getTargets());
+        dupRemoval.setInSchema(child.getOutSchema());
+        dupRemoval.setOutSchema(outSchema);
+        dupRemoval.setSubNode(child);
+        projectionNode.setSubNode(dupRemoval);
       }
     }
-    
-    return njQual;
+
+    return projectionNode;
   }
-  
-  private static LogicalNode createImplicitJoinTree(FromTable [] tables,
-                                                    EvalNode [] cnf) {
-    if (cnf == null) {
-      return createCatasianProduct(tables);
+
+  /**
+   * Insert a group-by operator before a sort or a projection operator.
+   * It is used only when a group-by clause is not given.
+   */
+  private LogicalNode insertGroupingIfUnresolved(LogicalPlan plan, String blockName,
+                                                 LogicalNode child, Stack<ExprType> stack) {
+    QueryBlock block = plan.getBlock(blockName);
+    if (!block.isGroupingResolved()) {
+      GroupbyNode groupbyNode = new GroupbyNode(new Column[] {});
+      groupbyNode.setTargets(block.getCurrentTargets());
+      groupbyNode.setSubNode(child);
+      groupbyNode.setInSchema(child.getOutSchema());
+
+      plan.postVisit(blockName, groupbyNode, stack);
+      block.checkAndSetEvaluatedTargets(groupbyNode);
+      return groupbyNode;
     } else {
-      return createCrossJoinFromJoinCondition(tables, cnf);
+      return child;
     }
   }
 
-  private static LogicalNode createCrossJoinFromJoinCondition(
-      FromTable [] tables, EvalNode [] cnf) {
-    Map<String, FromTable> fromTableMap = Maps.newHashMap();
-    for (FromTable f : tables) {
-      // TODO - to consider alias and self-join
-      fromTableMap.put(f.getTableName(), f);
+  private boolean isNoUpperProjection(Stack<ExprType> stack) {
+    for (ExprType node : stack) {
+      if (!( (node == ExprType.PROJECTION) || (node == ExprType.GROUP_BY) || (node == ExprType.JOIN) )) {
+        return false;
+      }
     }
 
-    JoinTree joinTree = new JoinTree(); // to infer join order
-    for (EvalNode expr : cnf) {
-      if (PlannerUtil.isJoinQual(expr)) {
-        joinTree.addJoin(expr);
+    return true;
+  }
+
+  /*===============================================================================================
+    Data Definition Language (DDL) SECTION
+   ===============================================================================================*/
+
+  private LogicalNode buildCreateTable(LogicalPlan plan, QueryBlock block, CreateTable expr, Stack<ExprType> stack)
+      throws VerifyException, CloneNotSupportedException {
+
+     String tableName = expr.getTableName();
+
+    if (expr.hasSubQuery()) {
+      stack.add(ExprType.CREATE_TABLE);
+      LogicalNode subQuery = createPlanInternal(plan, block, expr.getSubQuery(), stack);
+      stack.pop();
+      StoreTableNode storeNode = new StoreTableNode(tableName);
+      storeNode.setSubNode(subQuery);
+
+      if (expr.hasTableElements()) {
+        Schema schema = convertTableElementsSchema(expr.getTableElements());
+        storeNode.setOutSchema(schema);
+      } else {
+        storeNode.setOutSchema(subQuery.getOutSchema());
+      }
+      storeNode.setInSchema(subQuery.getOutSchema());
+
+      if (expr.hasStorageType()) {
+        storeNode.setStorageType(CatalogUtil.getStoreType(expr.getStorageType()));
+      } else {
+        // default type
+        // TODO - it should be configurable.
+        storeNode.setStorageType(CatalogProtos.StoreType.CSV);
+      }
+
+      if (expr.hasParams()) {
+        Options options = new Options();
+        options.putAll(expr.getParams());
+        storeNode.setOptions(options);
+      }
+
+      return storeNode;
+    } else {
+      CreateTableNode createTableNode = new CreateTableNode(expr.getTableName(),
+          convertTableElementsSchema(expr.getTableElements()));
+
+      if (expr.isExternal()) {
+        createTableNode.setExternal(true);
+      }
+
+      if (expr.hasStorageType()) {
+        createTableNode.setStorageType(CatalogUtil.getStoreType(expr.getStorageType()));
+      } else {
+        // default type
+        // TODO - it should be configurable.
+        createTableNode.setStorageType(CatalogProtos.StoreType.CSV);
+      }
+      if (expr.hasParams()) {
+        Options options = new Options();
+        options.putAll(expr.getParams());
+        createTableNode.setOptions(options);
+      }
+
+      if (expr.hasLocation()) {
+        createTableNode.setPath(new Path(expr.getLocation()));
       }
+
+      return createTableNode;
     }
+  }
 
-    List<String> remain = Lists.newArrayList(fromTableMap.keySet());
-    remain.removeAll(joinTree.getTables()); // only remain joins not matched to any join condition
-    List<Edge> joinOrder = null;
-    LogicalNode subroot = null;
-    JoinNode join;
-    Schema joinSchema;
+  /**
+   * It transforms table definition elements to schema.
+   *
+   * @param elements to be transformed
+   * @return schema transformed from table definition elements
+   */
+  private Schema convertTableElementsSchema(CreateTable.ColumnDefinition [] elements) {
+    Schema schema = new Schema();
+
+    Column column;
+    for (CreateTable.ColumnDefinition columnDefinition: elements) {
+      column = new Column(columnDefinition.getColumnName(),
+          TajoDataTypes.Type.valueOf(columnDefinition.getDataType()));
+      schema.addColumn(column);
+    }
 
-    // if there are at least one join matched to the one of join conditions,
-    // we try to traverse the join tree in the depth-first manner and
-    // determine the initial join order. Here, we do not consider the join cost.
-    // The optimized join order will be considered in the optimizer.
-    if (joinTree.getJoinNum() > 0) {
-      Stack<String> stack = new Stack<String>();
-      Set<String> visited = Sets.newHashSet();
+    return schema;
+  }
 
+  private LogicalNode buildDropTable(DropTable dropTable) {
+    DropTableNode dropTableNode = new DropTableNode(dropTable.getTableName());
+    return dropTableNode;
+  }
 
-      // initially, one table is pushed into the stack
-      String seed = joinTree.getTables().iterator().next();
-      stack.add(seed);
+  /*===============================================================================================
+    Expression SECTION
+   ===============================================================================================*/
 
-      joinOrder = Lists.newArrayList();
+  public EvalNode createEvalTree(LogicalPlan plan, String blockName, final Expr expr)
+      throws VerifyException {
+    switch(expr.getType()) {
 
-      while (!stack.empty()) {
-        String table = stack.pop();
-        if (visited.contains(table)) {
-          continue;
+      // constants
+      case Literal:
+        LiteralValue literal = (LiteralValue) expr;
+        switch (literal.getValueType()) {
+          case String:
+            return new ConstEval(DatumFactory.createText(literal.getValue()));
+          case Unsigned_Integer:
+            return new ConstEval(DatumFactory.createInt4(literal.getValue()));
+          case Unsigned_Large_Integer:
+            return new ConstEval(DatumFactory.createInt8(literal.getValue()));
+          case Unsigned_Float:
+            return new ConstEval(DatumFactory.createFloat4(literal.getValue()));
+          default:
+            throw new RuntimeException("Unsupported type: " + literal.getValueType());
         }
-        visited.add(table);
 
-        // 'joinOrder' will contain all tables corresponding to the given join conditions.
-        for (Edge edge : joinTree.getEdges(table)) {
-          if (!visited.contains(edge.getTarget()) && !edge.getTarget().equals(table)) {
-            stack.add(edge.getTarget());
-            joinOrder.add(edge);
+        // unary expression
+      case Not:
+        NotExpr notExpr = (NotExpr) expr;
+        return new NotEval(createEvalTree(plan, blockName, notExpr.getChild()));
+
+      // binary expressions
+      case LikePredicate:
+        LikePredicate like = (LikePredicate) expr;
+        FieldEval field = (FieldEval) createEvalTree(plan, blockName, like.getColumnRef());
+        ConstEval pattern = (ConstEval) createEvalTree(plan, blockName, like.getPattern());
+        return new LikeEval(like.isNot(), field, pattern);
+
+      case Is:
+        break;
+
+      case And:
+      case Or:
+      case Equals:
+      case NotEquals:
+      case LessThan:
+      case LessThanOrEquals:
+      case GreaterThan:
+      case GreaterThanOrEquals:
+      case Plus:
+      case Minus:
+      case Multiply:
+      case Divide:
+      case Modular:
+        BinaryOperator bin = (BinaryOperator) expr;
+        return new BinaryEval(exprTypeToEvalType(expr.getType()),
+            createEvalTree(plan, blockName, bin.getLeft()),
+            createEvalTree(plan, blockName, bin.getRight()));
+
+      // others
+      case Column:
+        return createFieldEval(plan, blockName, (ColumnReferenceExpr) expr);
+
+      case CountRowsFunction:
+        FunctionDesc countRows = catalog.getFunction("count", new DataType[] {});
+
+        try {
+          plan.getBlock(blockName).setHasGrouping();
+
+          return new AggFuncCallEval(countRows, (AggFunction) countRows.newInstance(),
+              new EvalNode [] {});
+        } catch (InternalException e) {
+          throw new UndefinedFunctionException(CatalogUtil.
+              getCanonicalName(countRows.getSignature(), new DataType[] {}));
+        }
+
+      case CountValueFunction:
+      case Function:
+        FunctionExpr function = (FunctionExpr) expr;
+        // Given parameters
+        Expr [] params = function.getParams();
+        EvalNode [] givenArgs = new EvalNode[params.length];
+        DataType[] paramTypes = new DataType[params.length];
+
+        if (expr.getType() == OpType.CountValueFunction) {
+          givenArgs[0] = createEvalTree(plan, blockName, params[0]);
+          paramTypes[0] = CatalogUtil.newDataTypeWithoutLen(TajoDataTypes.Type.ANY);
+        } else {
+          for (int i = 0; i < params.length; i++) {
+            givenArgs[i] = createEvalTree(plan, blockName, params[i]);
+            paramTypes[i] = givenArgs[i].getValueType()[0];
           }
         }
-      }
 
-      subroot = new ScanNode(fromTableMap.get(joinOrder.get(0).getSrc()));
-      LogicalNode inner;
-      for (Edge edge : joinOrder) {
-        inner = new ScanNode(fromTableMap.get(edge.getTarget()));
-        join = new JoinNode(JoinType.CROSS_JOIN, subroot, inner);
-        subroot = join;
-
-        joinSchema = SchemaUtil.merge(
-            join.getOuterNode().getOutSchema(),
-            join.getInnerNode().getOutSchema());
-        join.setInSchema(joinSchema);
-        join.setOutSchema(joinSchema);
-      }
+        if (!catalog.containFunction(function.getSignature(), paramTypes)) {
+            throw new UndefinedFunctionException(CatalogUtil.
+                getCanonicalName(function.getSignature(), paramTypes));
+        }
+
+        FunctionDesc funcDesc = catalog.getFunction(function.getSignature(), paramTypes);
+
+        try {
+          if (funcDesc.getFuncType() == CatalogProtos.FunctionType.GENERAL)
+
+            return new FuncCallEval(funcDesc,
+                (GeneralFunction) funcDesc.newInstance(), givenArgs);
+          else {
+            plan.getBlock(blockName).setHasGrouping();
+            return new AggFuncCallEval(funcDesc,
+                (AggFunction) funcDesc.newInstance(), givenArgs);
+          }
+        } catch (InternalException e) {
+          e.printStackTrace();
+        }
+
+      case CaseWhen:
+        CaseWhenPredicate caseWhenExpr = (CaseWhenPredicate) expr;
+        return createCaseWhenEval(plan, blockName, caseWhenExpr);
+
+      case IsNullPredicate:
+        IsNullPredicate nullPredicate = (IsNullPredicate) expr;
+        return new IsNullEval(nullPredicate.isNot(),
+            createFieldEval(plan, blockName, nullPredicate.getColumnRef()));
+
+      default:
     }
+    return null;
+  }
 
-    // Here, there are two cases:
-    // 1) there already exists the join plan.
-    // 2) there are no join plan.
-    if (joinOrder != null) { // case 1)
-      // if there are join tables corresponding to any join condition,
-      // the join plan is placed as the outer plan of the product.
-      remain.remove(joinOrder.get(0).getSrc());
-      remain.remove(joinOrder.get(0).getTarget());
-    } else { // case 2)
-      // if there are no inferred joins, the one of the remain join tables is placed as the left table
-      subroot = new ScanNode(fromTableMap.get(remain.get(0)));
-      remain.remove(remain.get(0));
-    }
-
-    // Here, the variable 'remain' contains join tables which are not matched to any join conditions.
-    // Thus, they will be joined by catasian product
-    for (String table : remain) {
-      join = new JoinNode(JoinType.CROSS_JOIN,
-          subroot, new ScanNode(fromTableMap.get(table)));
-      joinSchema = SchemaUtil.merge(
-          join.getOuterNode().getOutSchema(),
-          join.getInnerNode().getOutSchema());
-      join.setInSchema(joinSchema);
-      join.setOutSchema(joinSchema);
-      subroot = join;
-    }
-
-    return subroot;
-  }
-
-  // TODO - this method is somewhat duplicated to createCrossJoinFromJoinCondition. Later, it should be removed.
-  private static LogicalNode createCatasianProduct(FromTable [] tables) {
-    LogicalNode subroot = new ScanNode(tables[0]);
-    Schema joinSchema;
-    if(tables.length > 1) {
-      for(int i=1; i < tables.length; i++) {
-        JoinNode join = new JoinNode(JoinType.CROSS_JOIN,
-            subroot, new ScanNode(tables[i]));
-        joinSchema = SchemaUtil.merge(
-            join.getOuterNode().getOutSchema(),
-            join.getInnerNode().getOutSchema());
-        join.setInSchema(joinSchema);
-        join.setOutSchema(joinSchema);
-        subroot = join;
-      }
+  private FieldEval createFieldEval(LogicalPlan plan, String blockName,
+                                    ColumnReferenceExpr columnRef) throws VerifyException {
+    Column column;
+    if (columnRef.hasTableName()) {
+      column = plan.findColumnFromRelation(blockName, columnRef.getTableName(), columnRef.getName());
+    } else {
+      column = plan.suspectColumn(blockName, columnRef.getName());
     }
+    return new FieldEval(column);
+  }
 
-    return subroot;
+  private static Type exprTypeToEvalType(OpType type) {
+    switch (type) {
+      case And: return Type.AND;
+      case Or: return Type.OR;
+      case Equals: return Type.EQUAL;
+      case NotEquals: return Type.NOT_EQUAL;
+      case LessThan: return Type.LTH;
+      case LessThanOrEquals: return Type.LEQ;
+      case GreaterThan: return Type.GTH;
+      case GreaterThanOrEquals: return Type.GEQ;
+      case Plus: return Type.PLUS;
+      case Minus: return Type.MINUS;
+      case Multiply: return Type.MULTIPLY;
+      case Divide: return Type.DIVIDE;
+      case Modular: return Type.MODULAR;
+      case Column: return Type.FIELD;
+      case Function: return Type.FUNCTION;
+      default: throw new RuntimeException("Unsupported type: " + type);
+    }
   }
 
-  public static Schema getProjectedSchema(PlanningContext context, Target [] targets) {
+  public CaseWhenEval createCaseWhenEval(LogicalPlan plan, String blockName,
+                                              CaseWhenPredicate caseWhen) throws VerifyException {
+    CaseWhenEval caseEval = new CaseWhenEval();
+    EvalNode condition;
+    EvalNode result;
+
+    for (CaseWhenPredicate.WhenExpr when : caseWhen.getWhens()) {
+      condition = createEvalTree(plan, blockName, when.getCondition());
+      result = createEvalTree(plan, blockName, when.getResult());
+      caseEval.addWhen(condition, result);
+    }
+
+    if (caseWhen.hasElseResult()) {
+      caseEval.setElseResult(createEvalTree(plan, blockName, caseWhen.getElseResult()));
+    }
+
+    return caseEval;
+  }
+
+  Target [] annotateTargets(LogicalPlan plan, String blockName,
+                                       org.apache.tajo.algebra.Target [] targets)
+      throws VerifyException {
+    Target annotatedTargets [] = new Target[targets.length];
+
+    for (int i = 0; i < targets.length; i++) {
+      annotatedTargets[i] = createTarget(plan, blockName, targets[i]);
+    }
+    return annotatedTargets;
+  }
+
+  Target createTarget(LogicalPlan plan, String blockId,
+                             org.apache.tajo.algebra.Target target) throws VerifyException {
+    if (target.hasAlias()) {
+      return new Target(createEvalTree(plan, blockId, target.getExpr()),
+          target.getAlias());
+    } else {
+      return new Target(createEvalTree(plan, blockId, target.getExpr()));
+    }
+  }
+
+  /**
+   * It transforms a list of targets to schema. If it contains anonymous targets, it names them.
+   */
+  static Schema getProjectedSchema(LogicalPlan plan, Target [] targets) {
     Schema projected = new Schema();
     for(Target t : targets) {
       DataType type = t.getEvalTree().getValueType()[0];
@@ -652,7 +1022,7 @@ public class LogicalPlanner {
       if (t.hasAlias()) {
         name = t.getAlias();
       } else if (t.getEvalTree().getName().equals("?")) {
-        name = context.getGeneratedColumnName();
+        name = plan.newAnonymousColumnName();
       } else {
         name = t.getEvalTree().getName();
       }
@@ -661,27 +1031,4 @@ public class LogicalPlanner {
 
     return projected;
   }
-  
-  private static Schema getNaturalJoin(LogicalNode outer, LogicalNode inner) {
-    Schema joinSchema = new Schema();
-    Schema commons = SchemaUtil.getCommons(outer.getOutSchema(),
-        inner.getOutSchema());
-    joinSchema.addColumns(commons);
-    for (Column c : outer.getOutSchema().getColumns()) {
-      for (Column common : commons.getColumns()) {
-        if (!common.getColumnName().equals(c.getColumnName())) {
-          joinSchema.addColumn(c);
-        }
-      }
-    }
-
-    for (Column c : inner.getOutSchema().getColumns()) {
-      for (Column common : commons.getColumns()) {
-        if (!common.getColumnName().equals(c.getColumnName())) {
-          joinSchema.addColumn(c);
-        }
-      }
-    }
-    return joinSchema;
-  }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/NoSuchColumnException.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/NoSuchColumnException.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/NoSuchColumnException.java
new file mode 100644
index 0000000..56ae230
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/NoSuchColumnException.java
@@ -0,0 +1,25 @@
+/*
+ * 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.engine.planner;
+
+public class NoSuchColumnException extends VerifyException {
+  public NoSuchColumnException(String columnName) {
+    super("ERROR: no such column '" + columnName + "'");
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PhysicalPlannerImpl.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PhysicalPlannerImpl.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PhysicalPlannerImpl.java
index 0796cfd..9671066 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PhysicalPlannerImpl.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/PhysicalPlannerImpl.java
@@ -125,11 +125,6 @@ public class PhysicalPlannerImpl implements PhysicalPlanner {
         return new LimitExec(ctx, limitNode.getInSchema(),
             limitNode.getOutSchema(), outer, limitNode);
 
-      case CREATE_INDEX:
-        IndexWriteNode createIndexNode = (IndexWriteNode) logicalNode;
-        outer = createPlanRecursive(ctx, createIndexNode.getSubNode());
-        return createIndexWritePlan(sm, ctx, createIndexNode, outer);
-
       case BST_INDEX_SCAN:
         IndexScanNode indexScanNode = (IndexScanNode) logicalNode;
         outer = createIndexScanExec(ctx, indexScanNode);
@@ -290,14 +285,6 @@ public class PhysicalPlannerImpl implements PhysicalPlanner {
     return new ExternalSortExec(ctx, sm, sortNode, subOp);
   }
 
-  public PhysicalExec createIndexWritePlan(StorageManager sm,
-                                           TaskAttemptContext ctx, IndexWriteNode indexWriteNode, PhysicalExec subOp)
-      throws IOException {
-
-    return new IndexWriteExec(ctx, sm, indexWriteNode, ctx.getTable(indexWriteNode
-        .getTableName()), subOp);
-  }
-
   public PhysicalExec createIndexScanExec(TaskAttemptContext ctx,
                                           IndexScanNode annotation)
       throws IOException {


[04/13] TAJO-87: Integration of tajo algebra module and SQL parser

Posted by hy...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestPhysicalPlanner.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestPhysicalPlanner.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestPhysicalPlanner.java
index df9c853..77b43f1 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestPhysicalPlanner.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestPhysicalPlanner.java
@@ -25,14 +25,11 @@ import org.apache.commons.codec.binary.Base64;
 import org.apache.hadoop.fs.FileStatus;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
-import org.apache.hadoop.fs.PathFilter;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
 import org.apache.tajo.QueryIdFactory;
 import org.apache.tajo.QueryUnitAttemptId;
 import org.apache.tajo.TajoTestingCluster;
 import org.apache.tajo.TaskAttemptContext;
+import org.apache.tajo.algebra.Expr;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
 import org.apache.tajo.common.TajoDataTypes.Type;
@@ -40,12 +37,12 @@ import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.DatumFactory;
 import org.apache.tajo.datum.NullDatum;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
+import org.apache.tajo.engine.eval.AggFuncCallEval;
+import org.apache.tajo.engine.eval.EvalNode;
+import org.apache.tajo.engine.eval.EvalTreeUtil;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
 import org.apache.tajo.engine.planner.*;
-import org.apache.tajo.engine.planner.logical.LogicalNode;
-import org.apache.tajo.engine.planner.logical.LogicalRootNode;
-import org.apache.tajo.engine.planner.logical.StoreTableNode;
-import org.apache.tajo.engine.planner.logical.UnionNode;
+import org.apache.tajo.engine.planner.logical.*;
 import org.apache.tajo.master.ExecutionBlock.PartitionType;
 import org.apache.tajo.master.TajoMaster;
 import org.apache.tajo.storage.*;
@@ -54,6 +51,9 @@ import org.apache.tajo.util.CommonTestingUtil;
 import org.apache.tajo.util.TUtil;
 import org.apache.tajo.worker.RangeRetrieverHandler;
 import org.apache.tajo.worker.dataserver.retriever.FileChunk;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
 import java.io.File;
 import java.io.IOException;
@@ -67,7 +67,7 @@ public class TestPhysicalPlanner {
   private static TajoTestingCluster util;
   private static TajoConf conf;
   private static CatalogService catalog;
-  private static QueryAnalyzer analyzer;
+  private static SQLAnalyzer analyzer;
   private static LogicalPlanner planner;
   private static StorageManager sm;
   private static Path testDir;
@@ -148,7 +148,7 @@ public class TestPhysicalPlanner {
     appender.flush();
     appender.close();
     catalog.addTable(score);
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
   }
 
@@ -171,27 +171,27 @@ public class TestPhysicalPlanner {
       "select managerId, empId, deptName from employee order by managerId, empId desc", // 10
       "select deptName, nullable from score group by deptName, nullable", // 11
       "select 3 < 4 as ineq, 3.5 * 2 as score", // 12
-//      "select (3 > 2) = (1 > 0) and 3 > 1", // 12
       "select (1 > 0) and 3 > 1", // 13
       "select deptName, class, sum(score), max(score), min(score) from score", // 14
       "select deptname, class, sum(score), max(score), min(score) from score group by deptname" // 15
   };
 
   @Test
-  public final void testCreateScanPlan() throws IOException {
+  public final void testCreateScanPlan() throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "employee", employee.getMeta(),
         employee.getPath(), Integer.MAX_VALUE);
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testCreateScanPlan");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil
         .newQueryUnitAttemptId(),
         new Fragment[] { frags[0] }, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[0]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = analyzer.parse(QUERIES[0]);
+    LogicalPlan plan = planner.createPlan(expr);
+    LogicalNode rootNode =plan.getRootBlock().getRoot();
+    LogicalOptimizer.optimize(plan);
 
-    LogicalOptimizer.optimize(context, plan);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
 
     Tuple tuple;
     int i = 0;
@@ -207,18 +207,22 @@ public class TestPhysicalPlanner {
   }
 
   @Test
-  public final void testGroupByPlan() throws IOException {
+  public final void testGroupByPlan() throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "score", score.getMeta(), score.getPath(),
         Integer.MAX_VALUE);
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testGroupByPlan");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
         new Fragment[] { frags[0] }, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[7]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr context = analyzer.parse(QUERIES[7]);
+    LogicalPlan plan = planner.createPlan(context);
+    System.out.println(plan.getRootBlock().getRoot());
+    System.out.println("-------------------------------");
+    LogicalOptimizer.optimize(plan);
+    LogicalNode rootNode = plan.getRootBlock().getRoot();
+    System.out.println(rootNode);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
 
     int i = 0;
     Tuple tuple;
@@ -234,7 +238,8 @@ public class TestPhysicalPlanner {
   }
 
   @Test
-  public final void testHashGroupByPlanWithALLField() throws IOException {
+  public final void testHashGroupByPlanWithALLField() throws IOException,
+      CloneNotSupportedException {
     // TODO - currently, this query does not use hash-based group operator.
     Fragment[] frags = StorageManager.splitNG(conf, "score", score.getMeta(), score.getPath(),
         Integer.MAX_VALUE);
@@ -242,12 +247,12 @@ public class TestPhysicalPlanner {
         "target/test-data/testHashGroupByPlanWithALLField");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
         new Fragment[] { frags[0] }, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[15]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr expr = analyzer.parse(QUERIES[15]);
+    LogicalPlan plan = planner.createPlan(expr);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
 
     int i = 0;
     Tuple tuple;
@@ -264,18 +269,18 @@ public class TestPhysicalPlanner {
   }
 
   @Test
-  public final void testSortGroupByPlan() throws IOException {
+  public final void testSortGroupByPlan() throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "score", score.getMeta(), score.getPath(),
         Integer.MAX_VALUE);
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testSortGroupByPlan");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
         new Fragment[]{frags[0]}, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[7]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr context = analyzer.parse(QUERIES[7]);
+    LogicalPlan plan = planner.createPlan(context);
+    LogicalOptimizer.optimize(plan);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, plan.getRootBlock().getRoot());
 
     /*HashAggregateExec hashAgg = (HashAggregateExec) exec;
 
@@ -321,7 +326,7 @@ public class TestPhysicalPlanner {
   };
 
   @Test
-  public final void testStorePlan() throws IOException {
+  public final void testStorePlan() throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "score", score.getMeta(), score.getPath(),
         Integer.MAX_VALUE);
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testStorePlan");
@@ -330,15 +335,16 @@ public class TestPhysicalPlanner {
         workDir);
     ctx.setOutputPath(new Path(workDir, "grouped1"));
 
-    PlanningContext context = analyzer.parse(CreateTableAsStmts[0]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr context = analyzer.parse(CreateTableAsStmts[0]);
+    LogicalPlan plan = planner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
+
 
-    TableMeta outputMeta = CatalogUtil.newTableMeta(plan.getOutSchema(),
+    TableMeta outputMeta = CatalogUtil.newTableMeta(rootNode.getOutSchema(),
         StoreType.CSV);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
     exec.init();
     exec.next();
     exec.close();
@@ -361,7 +367,7 @@ public class TestPhysicalPlanner {
   }
 
   @Test
-  public final void testStorePlanWithRCFile() throws IOException {
+  public final void testStorePlanWithRCFile() throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "score", score.getMeta(), score.getPath(),
         Integer.MAX_VALUE);
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testStorePlanWithRCFile");
@@ -370,15 +376,15 @@ public class TestPhysicalPlanner {
         workDir);
     ctx.setOutputPath(new Path(workDir, "grouped2"));
 
-    PlanningContext context = analyzer.parse(CreateTableAsStmts[1]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr context = analyzer.parse(CreateTableAsStmts[1]);
+    LogicalPlan plan = planner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
-    TableMeta outputMeta = CatalogUtil.newTableMeta(plan.getOutSchema(),
+    TableMeta outputMeta = CatalogUtil.newTableMeta(rootNode.getOutSchema(),
         StoreType.RCFILE);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
     exec.init();
     exec.next();
     exec.close();
@@ -400,41 +406,33 @@ public class TestPhysicalPlanner {
     assertEquals(10, ctx.getResultStats().getNumRows().longValue());
   }
 
-  class PathFilterWithoutMeta implements PathFilter {
-
-    @Override
-    public boolean accept(Path path) {
-      String name = path.getName();
-      return name.startsWith(".");
-    }
-  }
-
   @Test
-  public final void testPartitionedStorePlan() throws IOException {
+  public final void testPartitionedStorePlan() throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "score", score.getMeta(), score.getPath(),
         Integer.MAX_VALUE);
     QueryUnitAttemptId id = TUtil.newQueryUnitAttemptId();
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testPartitionedStorePlan");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, id, new Fragment[] { frags[0] },
         workDir);
-    PlanningContext context = analyzer.parse(QUERIES[7]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr context = analyzer.parse(QUERIES[7]);
+    LogicalPlan plan = planner.createPlan(context);
+    LogicalNode rootNode = plan.getRootBlock().getRoot();
 
     int numPartitions = 3;
     Column key1 = new Column("score.deptName", Type.TEXT);
     Column key2 = new Column("score.class", Type.TEXT);
     StoreTableNode storeNode = new StoreTableNode("partition");
     storeNode.setPartitions(PartitionType.HASH, new Column[]{key1, key2}, numPartitions);
-    PlannerUtil.insertNode(plan, storeNode);
-    plan = LogicalOptimizer.optimize(context, plan);
+    PlannerUtil.insertNode(rootNode, storeNode);
+    rootNode = LogicalOptimizer.optimize(plan);
 
-    TableMeta outputMeta = CatalogUtil.newTableMeta(plan.getOutSchema(),
+    TableMeta outputMeta = CatalogUtil.newTableMeta(rootNode.getOutSchema(),
         StoreType.CSV);
 
     FileSystem fs = sm.getFileSystem();
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
     exec.init();
     exec.next();
     exec.close();
@@ -468,7 +466,7 @@ public class TestPhysicalPlanner {
 
   @Test
   public final void testPartitionedStorePlanWithEmptyGroupingSet()
-      throws IOException {
+      throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "score", score.getMeta(), score.getPath(),
         Integer.MAX_VALUE);
     QueryUnitAttemptId id = TUtil.newQueryUnitAttemptId();
@@ -477,20 +475,20 @@ public class TestPhysicalPlanner {
         "target/test-data/testPartitionedStorePlanWithEmptyGroupingSet");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, id, new Fragment[] { frags[0] },
         workDir);
-    PlanningContext context = analyzer.parse(QUERIES[14]);
-    LogicalNode plan = planner.createPlan(context);
-
+    Expr expr = analyzer.parse(QUERIES[14]);
+    LogicalPlan plan = planner.createPlan(expr);
+    LogicalNode rootNode = plan.getRootBlock().getRoot();
     int numPartitions = 1;
     StoreTableNode storeNode = new StoreTableNode("emptyset");
     storeNode.setPartitions(PartitionType.HASH, new Column[] {}, numPartitions);
-    PlannerUtil.insertNode(plan, storeNode);
-    plan = LogicalOptimizer.optimize(context, plan);
+    PlannerUtil.insertNode(rootNode, storeNode);
+    LogicalOptimizer.optimize(plan);
 
-    TableMeta outputMeta = CatalogUtil.newTableMeta(plan.getOutSchema(),
+    TableMeta outputMeta = CatalogUtil.newTableMeta(rootNode.getOutSchema(),
         StoreType.CSV);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
     exec.init();
     exec.next();
     exec.close();
@@ -523,19 +521,29 @@ public class TestPhysicalPlanner {
     assertEquals(1, ctx.getResultStats().getNumRows().longValue());
   }
 
-  //@Test
-  public final void testAggregationFunction() throws IOException {
+  @Test
+  public final void testAggregationFunction() throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "score", score.getMeta(), score.getPath(),
         Integer.MAX_VALUE);
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testAggregationFunction");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
         new Fragment[] { frags[0] }, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[8]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr context = analyzer.parse(QUERIES[8]);
+    LogicalPlan plan = planner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
+
+    // Set all aggregation functions to the first phase mode
+    GroupbyNode groupbyNode = (GroupbyNode) PlannerUtil.findTopNode(rootNode, ExprType.GROUP_BY);
+    for (Target target : groupbyNode.getTargets()) {
+      for (EvalNode eval : EvalTreeUtil.findDistinctAggFunction(target.getEvalTree())) {
+        if (eval instanceof AggFuncCallEval) {
+          ((AggFuncCallEval) eval).setFirstPhase();
+        }
+      }
+    }
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
 
     exec.init();
     Tuple tuple = exec.next();
@@ -546,38 +554,50 @@ public class TestPhysicalPlanner {
     exec.close();
   }
 
-  //@Test
-  public final void testCountFunction() throws IOException {
+  @Test
+  public final void testCountFunction() throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "score", score.getMeta(), score.getPath(),
         Integer.MAX_VALUE);
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testCountFunction");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
         new Fragment[] { frags[0] }, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[10]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr context = analyzer.parse(QUERIES[9]);
+    LogicalPlan plan = planner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
+    System.out.println(rootNode.toString());
+
+    // Set all aggregation functions to the first phase mode
+    GroupbyNode groupbyNode = (GroupbyNode) PlannerUtil.findTopNode(rootNode, ExprType.GROUP_BY);
+    for (Target target : groupbyNode.getTargets()) {
+      for (EvalNode eval : EvalTreeUtil.findDistinctAggFunction(target.getEvalTree())) {
+        if (eval instanceof AggFuncCallEval) {
+          ((AggFuncCallEval) eval).setFirstPhase();
+        }
+      }
+    }
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
-
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
+    exec.init();
     Tuple tuple = exec.next();
     assertEquals(30, tuple.get(0).asInt8());
     assertNull(exec.next());
+    exec.close();
   }
 
   @Test
-  public final void testGroupByWithNullValue() throws IOException {
+  public final void testGroupByWithNullValue() throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "score", score.getMeta(), score.getPath(),
         Integer.MAX_VALUE);
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testGroupByWithNullValue");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
         new Fragment[] { frags[0] }, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[11]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr context = analyzer.parse(QUERIES[11]);
+    LogicalPlan plan = planner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
 
     int count = 0;
     exec.init();
@@ -589,16 +609,16 @@ public class TestPhysicalPlanner {
   }
 
   @Test
-  public final void testUnionPlan() throws IOException {
+  public final void testUnionPlan() throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "employee", employee.getMeta(), employee.getPath(),
         Integer.MAX_VALUE);
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testUnionPlan");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
         new Fragment[] { frags[0] }, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[0]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
-    LogicalRootNode root = (LogicalRootNode) plan;
+    Expr  context = analyzer.parse(QUERIES[0]);
+    LogicalPlan plan = planner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
+    LogicalRootNode root = (LogicalRootNode) rootNode;
     UnionNode union = new UnionNode(root.getSubNode(), root.getSubNode());
     root.setSubNode(union);
 
@@ -615,16 +635,16 @@ public class TestPhysicalPlanner {
   }
 
   @Test
-  public final void testEvalExpr() throws IOException {
+  public final void testEvalExpr() throws IOException, CloneNotSupportedException {
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testEvalExpr");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
         new Fragment[] { }, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[12]);
-    LogicalNode plan = planner.createPlan(context);
-    LogicalOptimizer.optimize(context, plan);
+    Expr expr = analyzer.parse(QUERIES[12]);
+    LogicalPlan plan = planner.createPlan(expr);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf, sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
     Tuple tuple;
     exec.init();
     tuple = exec.next();
@@ -632,12 +652,12 @@ public class TestPhysicalPlanner {
     assertEquals(true, tuple.get(0).asBool());
     assertTrue(7.0d == tuple.get(1).asFloat8());
 
-    context = analyzer.parse(QUERIES[13]);
-    plan = planner.createPlan(context);
-    LogicalOptimizer.optimize(context, plan);
+    expr = analyzer.parse(QUERIES[13]);
+    plan = planner.createPlan(expr);
+    rootNode = LogicalOptimizer.optimize(plan);
 
     phyPlanner = new PhysicalPlannerImpl(conf, sm);
-    exec = phyPlanner.createPlan(ctx, plan);
+    exec = phyPlanner.createPlan(ctx, rootNode);
     exec.init();
     tuple = exec.next();
     exec.close();
@@ -649,18 +669,18 @@ public class TestPhysicalPlanner {
   };
 
   //@Test
-  public final void testCreateIndex() throws IOException {
+  public final void testCreateIndex() throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "employee", employee.getMeta(), employee.getPath(),
         Integer.MAX_VALUE);
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testCreateIndex");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
         new Fragment[] {frags[0]}, workDir);
-    PlanningContext context = analyzer.parse(createIndexStmt[0]);
-    LogicalNode plan = planner.createPlan(context);
-    LogicalOptimizer.optimize(context, plan);
+    Expr context = analyzer.parse(createIndexStmt[0]);
+    LogicalPlan plan = planner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf, sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
     exec.init();
     while (exec.next() != null) {
     }
@@ -675,19 +695,19 @@ public class TestPhysicalPlanner {
   };
 
   @Test
-  public final void testDuplicateEliminate() throws IOException {
+  public final void testDuplicateEliminate() throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "score", score.getMeta(), score.getPath(),
         Integer.MAX_VALUE);
 
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testDuplicateEliminate");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
         new Fragment[] {frags[0]}, workDir);
-    PlanningContext context = analyzer.parse(duplicateElimination[0]);
-    LogicalNode plan = planner.createPlan(context);
-    LogicalOptimizer.optimize(context, plan);
+    Expr expr = analyzer.parse(duplicateElimination[0]);
+    LogicalPlan plan = planner.createPlan(expr);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
     Tuple tuple;
 
     int cnt = 0;
@@ -706,63 +726,20 @@ public class TestPhysicalPlanner {
       "select name, empId from employee order by empId"
   };
 
-  /*
-  @Test
-  public final void testBug() throws IOException {
-    Schema s1 = new Schema();
-    s1.addColumn("o_orderdate", Type.TEXT);
-    s1.addColumn("o_shippriority", Type.INT4);
-    s1.addColumn("o_orderkey", DataType.LONG);
-
-    Options opt = new Options();
-    opt.put(CSVFile.DELIMITER, "|");
-    TableMeta meta1 = new TableMetaImpl(s1, StoreType.CSV, opt);
-    TableDesc desc1 = new TableDescImpl("s1", meta1, new Path("file:/home/hyunsik/error/sample/sq_1358404721340_0001_000001_03"));
-
-    Schema s2 = new Schema();
-    s2.addColumn("l_orderkey", DataType.LONG);
-    s2.addColumn("l_extendedprice", DataType.DOUBLE);
-    s2.addColumn("l_discount", DataType.DOUBLE);
-    TableMeta meta2 = new TableMetaImpl(s2, StoreType.CSV, opt);
-    TableDesc desc2 = new TableDescImpl("s2", meta1, new Path("file:/home/hyunsik/error/sample/sq_1358404721340_0001_000001_04"));
-
-    Path workDir = CommonTestingUtil.getTestDir("target/test-data/testBug");
-
-
-    Fragment [] frag1 = sm.splitNG("sq_1358404721340_0001_000001_03", meta1, new Path("file:/home/hyunsik/error/sample/sq_1358404721340_0001_000001_03"), util.getDefaultFileSystem().getDefaultBlockSize());
-    Fragment [] frag2 = sm.splitNG("sq_1358404721340_0001_000001_04", meta2, new Path("file:/home/hyunsik/error/sample/sq_1358404721340_0001_000001_04"), util.getDefaultFileSystem().getDefaultBlockSize());
-
-    TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(), null, workDir);
-    PlanningContext context = analyzer.parse("select l_orderkey,  sum(l_extendedprice*(1-l_discount)) as revenue, o_orderdate, o_shippriority from s1, s2 where l_orderkey (LONG) = o_orderkey group by l_orderkey, o_orderdate, o_shippriority order by o_orderdate");
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
-    JoinNode joinNode = (JoinNode) PlannerUtil.findTopNode(plan, ExprType.JOIN);
-
-    ScanNode [] scanNodes = (ScanNode[]) PlannerUtil.findAllNodes(plan, ExprType.SCAN);
-    System.out.println(scanNodes[0].getTableId());
-    System.out.println(scanNodes[1].getTableId());
-//    SeqScanExec seqScanExec = new SeqScanExec(ctx, sm, plan, frag1);
-//
-//    HashJoinExec join = new HashJoinExec(ctx, joinNode, scan1, scan2);
-//
-//    System.out.println(scan1.next());
-//    System.out.println(scan2.next());
-  }*/
-
   @Test
-  public final void testIndexedStoreExec() throws IOException {
+  public final void testIndexedStoreExec() throws IOException, CloneNotSupportedException {
     Fragment[] frags = StorageManager.splitNG(conf, "employee", employee.getMeta(),
         employee.getPath(), Integer.MAX_VALUE);
 
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testIndexedStoreExec");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
         new Fragment[] {frags[0]}, workDir);
-    PlanningContext context = analyzer.parse(SORT_QUERY[0]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr context = analyzer.parse(SORT_QUERY[0]);
+    LogicalPlan plan = planner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
 
     ProjectionExec proj = (ProjectionExec) exec;
     ExternalSortExec sort = (ExternalSortExec) proj.getChild();
@@ -786,7 +763,7 @@ public class TestPhysicalPlanner {
         keySchema, comp);
     reader.open();
     Path outputPath = StorageUtil.concatPath(workDir, "output", "output");
-    TableMeta meta = CatalogUtil.newTableMeta(plan.getOutSchema(), StoreType.CSV, new Options());
+    TableMeta meta = CatalogUtil.newTableMeta(rootNode.getOutSchema(), StoreType.CSV, new Options());
     SeekableScanner scanner = (SeekableScanner)
         StorageManager.getScanner(conf, meta, outputPath);
     scanner.init();

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestSortExec.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestSortExec.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestSortExec.java
index 89e8197..472a371 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestSortExec.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestSortExec.java
@@ -19,23 +19,24 @@
 package org.apache.tajo.engine.planner.physical;
 
 import org.apache.hadoop.fs.Path;
-import org.junit.After;
-import org.junit.BeforeClass;
-import org.junit.Test;
 import org.apache.tajo.TajoTestingCluster;
 import org.apache.tajo.TaskAttemptContext;
+import org.apache.tajo.algebra.Expr;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
 import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.DatumFactory;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
 import org.apache.tajo.engine.planner.*;
 import org.apache.tajo.engine.planner.logical.LogicalNode;
 import org.apache.tajo.storage.*;
 import org.apache.tajo.util.CommonTestingUtil;
 import org.apache.tajo.util.TUtil;
+import org.junit.After;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
 import java.io.IOException;
 import java.util.Random;
@@ -46,7 +47,7 @@ public class TestSortExec {
   private static TajoConf conf;
   private static final String TEST_PATH = "target/test-data/TestPhysicalPlanner";
   private static CatalogService catalog;
-  private static QueryAnalyzer analyzer;
+  private static SQLAnalyzer analyzer;
   private static LogicalPlanner planner;
   private static StorageManager sm;
   private static TajoTestingCluster util;
@@ -90,7 +91,7 @@ public class TestSortExec {
     TableDesc desc = new TableDescImpl("employee", employeeMeta, tablePath);
     catalog.addTable(desc);
 
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
   }
 
@@ -103,18 +104,18 @@ public class TestSortExec {
       "select managerId, empId, deptName from employee order by managerId, empId desc" };
 
   @Test
-  public final void testNext() throws IOException {
+  public final void testNext() throws IOException, CloneNotSupportedException {
     Fragment [] frags = sm.splitNG(conf, "employee", employeeMeta, tablePath, Integer.MAX_VALUE);
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/TestSortExec");
     TaskAttemptContext ctx = new TaskAttemptContext(conf, TUtil
         .newQueryUnitAttemptId(),
         new Fragment[] { frags[0] }, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[0]);
-    LogicalNode plan = planner.createPlan(context);
-    LogicalOptimizer.optimize(context, plan);
+    Expr context = analyzer.parse(QUERIES[0]);
+    LogicalPlan plan = planner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf, sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
 
     Tuple tuple;
     Datum preVal = null;

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestJoinQuery.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestJoinQuery.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestJoinQuery.java
index 8838302..e11666c 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestJoinQuery.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestJoinQuery.java
@@ -118,7 +118,7 @@ public class TestJoinQuery {
 
   @Test
   public void testJoinRefEval() throws Exception {
-    ResultSet res = tpch.execute("select r_regionkey, n_regionkey, (r_regionkey + n_regionkey) as plus from region, nation where r_regionkey = n_regionkey");
+    ResultSet res = tpch.execute("select r_regionkey, n_regionkey, (r_regionkey + n_regionkey) as plus from region join nation on r_regionkey = n_regionkey");
     int r, n;
     while(res.next()) {
       r = res.getInt(1);
@@ -130,10 +130,10 @@ public class TestJoinQuery {
   @Test
   public void testJoinAndCaseWhen() throws Exception {
     ResultSet res = tpch.execute("select r_regionkey, n_regionkey, " +
-        "case when (((r_regionkey + n_regionkey) % 2) = 0 and r_regionkey = 1) then 'one' " +
-        "when (((r_regionkey + n_regionkey) % 2) = 0 and r_regionkey = 2) then 'two' " +
-        "when (((r_regionkey + n_regionkey) % 2) = 0 and r_regionkey = 3) then 'three' " +
-        "when (((r_regionkey + n_regionkey) % 2) = 0 and r_regionkey = 4) then 'four' " +
+        "case when r_regionkey = 1 then 'one' " +
+        "when r_regionkey = 2 then 'two' " +
+        "when r_regionkey = 3 then 'three' " +
+        "when r_regionkey = 4 then 'four' " +
         "else 'zero' " +
         "end as cond from region, nation where r_regionkey = n_regionkey");
 

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java
index 1f1db84..a47f5bd 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java
@@ -20,11 +20,11 @@ package org.apache.tajo.engine.query;
 
 import com.google.common.collect.Maps;
 import com.google.common.collect.Sets;
+import org.apache.tajo.IntegrationTest;
+import org.apache.tajo.TpchTestBase;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
-import org.apache.tajo.IntegrationTest;
-import org.apache.tajo.TpchTestBase;
 
 import java.io.IOException;
 import java.sql.ResultSet;
@@ -134,7 +134,7 @@ public class TestSelectQuery {
     assertEquals("ly final dependencies: slyly bold",res.getString(16));
   }
 
-  //@Test
+  @Test
   public final void testSelectDistinct() throws Exception {
     Set<String> result1 = Sets.newHashSet();
     result1.add("1,1");
@@ -266,18 +266,18 @@ public class TestSelectQuery {
   @Test
   public final void testUnion1() throws Exception {
     ResultSet res = tpch.execute(
-        "select l_orderkey as num from lineitem union select c_custkey as num from customer");
+        "select o_custkey as num from orders union select c_custkey as num from customer");
     int count = 0;
     for (;res.next();) {
       count++;
     }
-    assertEquals(8, count);
+    assertEquals(6, count);
   }
 
   @Test
   public final void testUnion2() throws Exception {
     ResultSet res = tpch.execute(
-        "select l_orderkey from lineitem as l1 union select l_orderkey from lineitem as l2");
+        "select l_orderkey from lineitem l1 union select l_orderkey from lineitem l2");
     int count = 0;
     for (;res.next();) {
       count++;
@@ -289,7 +289,11 @@ public class TestSelectQuery {
   public final void testCreateAfterSelect() throws Exception {
     ResultSet res = tpch.execute(
         "create table orderkeys as select l_orderkey from lineitem");
-    assertNull(res);
+    int count = 0;
+    for (;res.next();) {
+      count++;
+    }
+    assertEquals(count, 5);
   }
 
   //@Test

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java
index 2e43bda..14344fe 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/query/TestSortQuery.java
@@ -66,6 +66,25 @@ public class TestSortQuery {
   }
 
   @Test
+  public final void testSortWithAliasKey() throws Exception {
+    ResultSet res = tpch.execute(
+        "select l_linenumber, l_orderkey as sortkey from lineitem order by sortkey");
+    int cnt = 0;
+    Long prev = null;
+    while(res.next()) {
+      if (prev == null) {
+        prev = res.getLong(2);
+      } else {
+        assertTrue(prev <= res.getLong(2));
+        prev = res.getLong(2);
+      }
+      cnt++;
+    }
+
+    assertEquals(5, cnt);
+  }
+
+  @Test
   public final void testSortDesc() throws Exception {
     ResultSet res = tpch.execute(
         "select l_linenumber, l_orderkey from lineitem order by l_orderkey desc");

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/master/TestExecutionBlockCursor.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/master/TestExecutionBlockCursor.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/master/TestExecutionBlockCursor.java
index e7ede9d..bce7e6c 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/master/TestExecutionBlockCursor.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/master/TestExecutionBlockCursor.java
@@ -16,6 +16,9 @@ package org.apache.tajo.master;
 
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.yarn.event.AsyncDispatcher;
+import org.apache.tajo.algebra.Expr;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
+import org.apache.tajo.engine.planner.*;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.apache.tajo.QueryIdFactory;
@@ -24,9 +27,6 @@ import org.apache.tajo.benchmark.TPCH;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos;
 import org.apache.tajo.conf.TajoConf;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
-import org.apache.tajo.engine.planner.LogicalPlanner;
-import org.apache.tajo.engine.planner.PlanningContext;
 import org.apache.tajo.engine.planner.global.MasterPlan;
 import org.apache.tajo.engine.planner.logical.LogicalNode;
 import org.apache.tajo.engine.planner.logical.LogicalRootNode;
@@ -39,7 +39,7 @@ public class TestExecutionBlockCursor {
   private static TajoConf conf;
   private static CatalogService catalog;
   private static GlobalPlanner planner;
-  private static QueryAnalyzer analyzer;
+  private static SQLAnalyzer analyzer;
   private static LogicalPlanner logicalPlanner;
 
   @BeforeClass
@@ -58,7 +58,7 @@ public class TestExecutionBlockCursor {
       catalog.addTable(d);
     }
 
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     logicalPlanner = new LogicalPlanner(catalog);
 
     StorageManager sm  = new StorageManager(conf);
@@ -74,15 +74,16 @@ public class TestExecutionBlockCursor {
 
   @Test
   public void testNextBlock() throws Exception {
-    PlanningContext context = analyzer.parse(
+    Expr context = analyzer.parse(
         "select s_acctbal, s_name, n_name, p_partkey, p_mfgr, s_address, s_phone, s_comment, ps_supplycost, " +
             "r_name, p_type, p_size " +
             "from region join nation on n_regionkey = r_regionkey and r_name = 'AMERICA' " +
             "join supplier on s_nationkey = n_nationkey " +
             "join partsupp on s_suppkey = ps_suppkey " +
             "join part on p_partkey = ps_partkey and p_type like '%BRASS' and p_size = 15");
-    LogicalNode logicalPlan = logicalPlanner.createPlan(context);
-    MasterPlan plan = planner.build(QueryIdFactory.newQueryId(), (LogicalRootNode) logicalPlan);
+    LogicalPlan logicalPlan = logicalPlanner.createPlan(context);
+    LogicalNode rootNode = LogicalOptimizer.optimize(logicalPlan);
+    MasterPlan plan = planner.build(QueryIdFactory.newQueryId(), (LogicalRootNode) rootNode);
 
     ExecutionBlockCursor cursor = new ExecutionBlockCursor(plan);
 
@@ -92,7 +93,7 @@ public class TestExecutionBlockCursor {
       count++;
     }
 
-    // 4 input relations, 4 join, and 1 projection = 10 execution blocks
-    assertEquals(10, count);
+    // 4 input relations, 4 join, and 1 projection = 9 execution blocks
+    assertEquals(9, count);
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/worker/TestRangeRetrieverHandler.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/worker/TestRangeRetrieverHandler.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/worker/TestRangeRetrieverHandler.java
index faba6a3..711d63d 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/worker/TestRangeRetrieverHandler.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/worker/TestRangeRetrieverHandler.java
@@ -23,6 +23,8 @@ import com.google.common.collect.Maps;
 import org.apache.commons.codec.binary.Base64;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
+import org.apache.tajo.algebra.Expr;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -35,7 +37,6 @@ import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.DatumFactory;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
 import org.apache.tajo.engine.planner.*;
 import org.apache.tajo.engine.planner.logical.LogicalNode;
 import org.apache.tajo.engine.planner.physical.ExternalSortExec;
@@ -61,7 +62,7 @@ public class TestRangeRetrieverHandler {
   private TajoTestingCluster util;
   private TajoConf conf;
   private CatalogService catalog;
-  private QueryAnalyzer analyzer;
+  private SQLAnalyzer analyzer;
   private LogicalPlanner planner;
   private StorageManager sm;
   private Schema schema;
@@ -80,7 +81,7 @@ public class TestRangeRetrieverHandler {
     catalog = util.getMiniCatalogCluster().getCatalog();
     sm = StorageManager.get(conf, testDir);
 
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
 
     schema = new Schema();
@@ -135,12 +136,12 @@ public class TestRangeRetrieverHandler {
     TaskAttemptContext
         ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
         new Fragment[] {frags[0]}, testDir);
-    PlanningContext context = analyzer.parse(SORT_QUERY[0]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr expr = analyzer.parse(SORT_QUERY[0]);
+    LogicalPlan plan = planner.createPlan(expr);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
 
     ProjectionExec proj = (ProjectionExec) exec;
     ExternalSortExec sort = (ExternalSortExec) proj.getChild();
@@ -246,12 +247,12 @@ public class TestRangeRetrieverHandler {
     TaskAttemptContext
         ctx = new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
         new Fragment[] {frags[0]}, testDir);
-    PlanningContext context = analyzer.parse(SORT_QUERY[1]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr expr = analyzer.parse(SORT_QUERY[1]);
+    LogicalPlan plan = planner.createPlan(expr);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
 
     ProjectionExec proj = (ProjectionExec) exec;
     ExternalSortExec sort = (ExternalSortExec) proj.getChild();

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/create_table_1.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/create_table_1.sql b/tajo-core/tajo-core-backend/src/test/queries/create_table_1.sql
new file mode 100644
index 0000000..8875b0e
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/create_table_1.sql
@@ -0,0 +1 @@
+create table name (name text, age int)
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/create_table_10.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/create_table_10.sql b/tajo-core/tajo-core-backend/src/test/queries/create_table_10.sql
new file mode 100644
index 0000000..43314fa
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/create_table_10.sql
@@ -0,0 +1 @@
+create external table table1 (name text, age int, earn bigint, score float) using csv location '/tmp/data'
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/create_table_2.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/create_table_2.sql b/tajo-core/tajo-core-backend/src/test/queries/create_table_2.sql
new file mode 100644
index 0000000..4580e82
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/create_table_2.sql
@@ -0,0 +1 @@
+create table name (name text, age int) using rcfile
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/create_table_3.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/create_table_3.sql b/tajo-core/tajo-core-backend/src/test/queries/create_table_3.sql
new file mode 100644
index 0000000..5ab2af2
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/create_table_3.sql
@@ -0,0 +1 @@
+create table name (name text, age int) using rcfile with ('rcfile.buffer'=4096)
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/create_table_4.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/create_table_4.sql b/tajo-core/tajo-core-backend/src/test/queries/create_table_4.sql
new file mode 100644
index 0000000..fe4d580
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/create_table_4.sql
@@ -0,0 +1 @@
+create table name as select * from test
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/create_table_5.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/create_table_5.sql b/tajo-core/tajo-core-backend/src/test/queries/create_table_5.sql
new file mode 100644
index 0000000..b22b841
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/create_table_5.sql
@@ -0,0 +1 @@
+create table name (name text, age int) as select * from test
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/create_table_6.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/create_table_6.sql b/tajo-core/tajo-core-backend/src/test/queries/create_table_6.sql
new file mode 100644
index 0000000..27e06f2
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/create_table_6.sql
@@ -0,0 +1 @@
+create table name (name text, age int) using rcfile as select * from test
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/create_table_7.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/create_table_7.sql b/tajo-core/tajo-core-backend/src/test/queries/create_table_7.sql
new file mode 100644
index 0000000..5c9bf35
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/create_table_7.sql
@@ -0,0 +1 @@
+create table name (name text, age int) using rcfile with ('rcfile.buffer'= 4096) as select * from test
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/create_table_8.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/create_table_8.sql b/tajo-core/tajo-core-backend/src/test/queries/create_table_8.sql
new file mode 100644
index 0000000..d6c2ae6
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/create_table_8.sql
@@ -0,0 +1,48 @@
+create table widetable (
+  col0 bit,
+  col1 BIT(10),
+  col2 bit varying,
+  col3 bit VARYING(10),
+  col4 tinyint,
+  col5 smallInt,
+  col6 integer,
+  col7 biginT,
+  col8 real,
+  col9 float,
+  col10 float(53),
+  col11 double,
+  col12 doublE precision,
+  col13 numeric,
+  col14 numeric(10),
+  col15 numeric(10,2),
+  col16 decimal,
+  col17 decimal(10),
+  col18 decimal(10,2),
+  col19 char,
+  col20 character,
+  col21 chaR(10),
+  col22 character(10),
+  col23 varchar,
+  col24 character varying,
+  col25 varchar(255),
+  col26 character varying (255),
+  col27 nchar,
+  col28 nchar(255),
+  col29 national character,
+  col30 national character(255),
+  col31 nvarchar,
+  col32 nvarchar(255),
+  col33 natIonal character varying,
+  col34 national character varying (255),
+  col35 date,
+  col36 time,
+  col37 timetz,
+  col38 time With time zone,
+  col39 timesTamptz,
+  col40 timestamp with time zone,
+  col41 binary,
+  col42 binary(10),
+  col43 varbinary(10),
+  col44 binary Varying(10),
+  col45 blOb
+) as select * from test
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/create_table_9.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/create_table_9.sql b/tajo-core/tajo-core-backend/src/test/queries/create_table_9.sql
new file mode 100644
index 0000000..49e1e3c
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/create_table_9.sql
@@ -0,0 +1 @@
+create table widetable (col1 float(10), col2 float) as select * from test
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/drop_table.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/drop_table.sql b/tajo-core/tajo-core-backend/src/test/queries/drop_table.sql
new file mode 100644
index 0000000..0e52112
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/drop_table.sql
@@ -0,0 +1 @@
+drop table abc
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/groupby_1.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/groupby_1.sql b/tajo-core/tajo-core-backend/src/test/queries/groupby_1.sql
new file mode 100644
index 0000000..85dbe21
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/groupby_1.sql
@@ -0,0 +1 @@
+select col0, col1, col2, col3, sum(col4) as total, avg(col5) from base group by col0, cube (col1, col2), rollup(col3) having total > 100
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/insert_1.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/insert_1.sql b/tajo-core/tajo-core-backend/src/test/queries/insert_1.sql
new file mode 100644
index 0000000..e9f65c4
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/insert_1.sql
@@ -0,0 +1 @@
+insert into people values (1, 'hyunsik', 32)
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/insert_2.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/insert_2.sql b/tajo-core/tajo-core-backend/src/test/queries/insert_2.sql
new file mode 100644
index 0000000..72d699b
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/insert_2.sql
@@ -0,0 +1 @@
+insert into people (id, name, age) values (1, 'hyunsik', 32)
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/join_1.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/join_1.sql b/tajo-core/tajo-core-backend/src/test/queries/join_1.sql
new file mode 100644
index 0000000..ad27795
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/join_1.sql
@@ -0,0 +1 @@
+select name, addr from people natural join student natural join professor
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/join_10.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/join_10.sql b/tajo-core/tajo-core-backend/src/test/queries/join_10.sql
new file mode 100644
index 0000000..c9056cc
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/join_10.sql
@@ -0,0 +1 @@
+select * from a cross join b, c, d, e
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/join_11.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/join_11.sql b/tajo-core/tajo-core-backend/src/test/queries/join_11.sql
new file mode 100644
index 0000000..25daeb0
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/join_11.sql
@@ -0,0 +1 @@
+select * from x, y, (select * from a, b, c WHERE something) as ss where somethingelse
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/join_2.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/join_2.sql b/tajo-core/tajo-core-backend/src/test/queries/join_2.sql
new file mode 100644
index 0000000..03056ac
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/join_2.sql
@@ -0,0 +1 @@
+select name, addr from people inner join student on people.name = student.name
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/join_3.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/join_3.sql b/tajo-core/tajo-core-backend/src/test/queries/join_3.sql
new file mode 100644
index 0000000..22281fe
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/join_3.sql
@@ -0,0 +1 @@
+select name, addr from people inner join student using (id, name)
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/join_4.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/join_4.sql b/tajo-core/tajo-core-backend/src/test/queries/join_4.sql
new file mode 100644
index 0000000..7e572de
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/join_4.sql
@@ -0,0 +1 @@
+select name, addr from people join student using (id, name)
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/join_5.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/join_5.sql b/tajo-core/tajo-core-backend/src/test/queries/join_5.sql
new file mode 100644
index 0000000..79c69e3
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/join_5.sql
@@ -0,0 +1 @@
+select name, addr from people cross join student
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/join_6.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/join_6.sql b/tajo-core/tajo-core-backend/src/test/queries/join_6.sql
new file mode 100644
index 0000000..10aafa9
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/join_6.sql
@@ -0,0 +1 @@
+select name, addr from people left outer join student on people.name = student.name
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/join_7.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/join_7.sql b/tajo-core/tajo-core-backend/src/test/queries/join_7.sql
new file mode 100644
index 0000000..402b82f
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/join_7.sql
@@ -0,0 +1 @@
+select name, addr from people right outer join student on people.name = student.name
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/join_8.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/join_8.sql b/tajo-core/tajo-core-backend/src/test/queries/join_8.sql
new file mode 100644
index 0000000..f413478
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/join_8.sql
@@ -0,0 +1,13 @@
+select *
+  from table1
+  cross join table2
+  join table3 on table1.id = table3.id
+  inner join table4 on table1.id = table4.id
+  left outer join table5 on table1.id = table5.id
+  right outer join table6 on table1.id = table6.id
+  full outer join table7 on table1.id = table7.id
+  natural join table8
+  natural inner join table9
+  natural left outer join table10
+  natural right outer join table11
+  natural full outer join table12
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/join_9.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/join_9.sql b/tajo-core/tajo-core-backend/src/test/queries/join_9.sql
new file mode 100644
index 0000000..6f0ae0d
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/join_9.sql
@@ -0,0 +1,5 @@
+select s_acctbal, s_name, n_name, p_partkey, p_mfgr, s_address, s_phone, s_comment, ps_supplycost
+  from region join nation on n_regionkey = r_regionkey and r_name = 'EUROPE'
+  join supplier on s_nationekey = n_nationkey
+  join partsupp on s_suppkey = ps_ps_suppkey
+  join part on p_partkey = ps_partkey and p_type like '%BRASS' and p_size = 15
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/select_1.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/select_1.sql b/tajo-core/tajo-core-backend/src/test/queries/select_1.sql
new file mode 100644
index 0000000..7196f0c
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/select_1.sql
@@ -0,0 +1 @@
+select id, name, age, gender from people
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/select_2.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/select_2.sql b/tajo-core/tajo-core-backend/src/test/queries/select_2.sql
new file mode 100644
index 0000000..0727705
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/select_2.sql
@@ -0,0 +1 @@
+select id, name, age, gender from people as p, students as s
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/select_3.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/select_3.sql b/tajo-core/tajo-core-backend/src/test/queries/select_3.sql
new file mode 100644
index 0000000..2e8f43a
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/select_3.sql
@@ -0,0 +1 @@
+select name, addr, sum(score) from students group by name, addr
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/select_4.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/select_4.sql b/tajo-core/tajo-core-backend/src/test/queries/select_4.sql
new file mode 100644
index 0000000..6c19a45
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/select_4.sql
@@ -0,0 +1 @@
+select name, addr, age from people where age > 30
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/select_5.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/select_5.sql b/tajo-core/tajo-core-backend/src/test/queries/select_5.sql
new file mode 100644
index 0000000..336a9be
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/select_5.sql
@@ -0,0 +1 @@
+select name as n, sum(score, 3+4, 3>4) as total, 3+4 as id from people where age = 30
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/select_6.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/select_6.sql b/tajo-core/tajo-core-backend/src/test/queries/select_6.sql
new file mode 100644
index 0000000..965aa08
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/select_6.sql
@@ -0,0 +1 @@
+select ipv4:src_ip from test
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/select_7.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/select_7.sql b/tajo-core/tajo-core-backend/src/test/queries/select_7.sql
new file mode 100644
index 0000000..d54276b
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/select_7.sql
@@ -0,0 +1 @@
+select distinct id, name, age, gender from people
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/select_8.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/select_8.sql b/tajo-core/tajo-core-backend/src/test/queries/select_8.sql
new file mode 100644
index 0000000..13f6842
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/select_8.sql
@@ -0,0 +1 @@
+select all id, name, age, gender from people
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/set_1.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/set_1.sql b/tajo-core/tajo-core-backend/src/test/queries/set_1.sql
new file mode 100644
index 0000000..88600fc
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/set_1.sql
@@ -0,0 +1 @@
+select c1,c2,c3 from table1 union select c4,c5,c6 from table2 union all select c7, c8 from table3
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/set_2.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/set_2.sql b/tajo-core/tajo-core-backend/src/test/queries/set_2.sql
new file mode 100644
index 0000000..0eb5670
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/set_2.sql
@@ -0,0 +1 @@
+select a,b,c from table1 union distinct select a,b,c from table2 except all select a,b,c from table3
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/queries/set_3.sql
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/queries/set_3.sql b/tajo-core/tajo-core-backend/src/test/queries/set_3.sql
new file mode 100644
index 0000000..eebfbcb
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/queries/set_3.sql
@@ -0,0 +1 @@
+select a,b,c from table1 intersect select a,b,c from table2 intersect all select a,b,c from table3
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-storage/pom.xml
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-storage/pom.xml b/tajo-core/tajo-core-storage/pom.xml
index 6c3e61e..0c503d6 100644
--- a/tajo-core/tajo-core-storage/pom.xml
+++ b/tajo-core/tajo-core-storage/pom.xml
@@ -68,17 +68,6 @@
         </executions>
       </plugin>
       <plugin>
-        <groupId>org.antlr</groupId>
-        <artifactId>antlr3-maven-plugin</artifactId>
-        <executions>
-          <execution>
-            <goals>
-              <goal>antlr</goal>
-            </goals>
-          </execution>
-        </executions>
-      </plugin>
-      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-antrun-plugin</artifactId>
         <executions>

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-frontend/pom.xml
----------------------------------------------------------------------
diff --git a/tajo-frontend/pom.xml b/tajo-frontend/pom.xml
deleted file mode 100644
index 2e0f5fd..0000000
--- a/tajo-frontend/pom.xml
+++ /dev/null
@@ -1,149 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-  Copyright 2012 Database Lab., Korea Univ.
-
-  Licensed 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.
-  -->
-
-<project xmlns="http://maven.apache.org/POM/4.0.0"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-  <parent>
-    <artifactId>tajo-project</artifactId>
-    <groupId>org.apache.tajo</groupId>
-    <version>0.2.0-SNAPSHOT</version>
-	  <relativePath>../tajo-project</relativePath>
-  </parent>
-  <modelVersion>4.0.0</modelVersion>
-  <artifactId>tajo-frontend</artifactId>
-  <packaging>pom</packaging>
-  <name>Tajo Frontend</name>
-  <properties>
-    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
-  </properties>
-
-  <modules>
-    <module>tajo-frontend-sql</module>
-  </modules>
-
-  <profiles>
-    <profile>
-      <id>docs</id>
-      <activation>
-        <activeByDefault>false</activeByDefault>
-      </activation>
-      <build>
-        <plugins>
-          <plugin>
-            <groupId>org.apache.maven.plugins</groupId>
-            <artifactId>maven-javadoc-plugin</artifactId>
-            <executions>
-              <execution>
-                <!-- build javadoc jars per jar for publishing to maven -->
-                <id>module-javadocs</id>
-                <phase>package</phase>
-                <goals>
-                  <goal>jar</goal>
-                </goals>
-                <configuration>
-                  <destDir>${project.build.directory}</destDir>
-                </configuration>
-              </execution>
-            </executions>
-          </plugin>
-        </plugins>
-      </build>
-    </profile>
-    <profile>
-      <id>src</id>
-      <activation>
-        <activeByDefault>false</activeByDefault>
-      </activation>
-      <build>
-        <plugins>
-          <plugin>
-            <groupId>org.apache.maven.plugins</groupId>
-            <artifactId>maven-source-plugin</artifactId>
-            <executions>
-              <execution>
-                <!-- builds source jars and attaches them to the project for publishing -->
-                <id>hadoop-java-sources</id>
-                <phase>package</phase>
-                <goals>
-                  <goal>jar-no-fork</goal>
-                </goals>
-              </execution>
-            </executions>
-          </plugin>
-        </plugins>
-      </build>
-    </profile>
-    <profile>
-      <id>dist</id>
-      <activation>
-        <activeByDefault>false</activeByDefault>
-      </activation>
-      <build>
-        <plugins>
-          <plugin>
-            <groupId>org.apache.maven.plugins</groupId>
-            <artifactId>maven-antrun-plugin</artifactId>
-            <executions>
-              <execution>
-                <id>dist</id>
-                <phase>prepare-package</phase>
-                <goals>
-                  <goal>run</goal>
-                </goals>
-                <configuration>
-                  <target>
-                    <echo file="${project.build.directory}/dist-layout-stitching.sh">
-                      run() {
-                      echo "\$ ${@}"
-                      "${@}"
-                      res=$?
-                      if [ $res != 0 ]; then
-                      echo
-                      echo "Failed!"
-                      echo
-                      exit $res
-                      fi
-                      }
-
-                      ROOT=`cd ${basedir}/..;pwd`
-                      echo
-                      echo "Current directory `pwd`"
-                      echo
-                      run rm -rf ${project.artifactId}-${project.version}
-                      run mkdir ${project.artifactId}-${project.version}
-                      run cd ${project.artifactId}-${project.version}
-                      run cp -r ${basedir}/tajo-frontend-sql/target/tajo-frontend-sql-${project.version}*.jar .
-                      echo
-                      echo "Tajo Frontend dist layout available at: ${project.build.directory}/${project.artifactId}-${project.version}"
-                      echo
-                    </echo>
-                    <exec executable="sh" dir="${project.build.directory}" failonerror="true">
-                      <arg line="./dist-layout-stitching.sh"/>
-                    </exec>
-                  </target>
-                </configuration>
-              </execution>
-            </executions>
-          </plugin>
-        </plugins>
-      </build>
-    </profile>
-  </profiles>
-
-</project>

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-frontend/tajo-frontend-sql/pom.xml
----------------------------------------------------------------------
diff --git a/tajo-frontend/tajo-frontend-sql/pom.xml b/tajo-frontend/tajo-frontend-sql/pom.xml
deleted file mode 100644
index 963163e..0000000
--- a/tajo-frontend/tajo-frontend-sql/pom.xml
+++ /dev/null
@@ -1,187 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-  Copyright 2012 Database Lab., Korea Univ.
-
-  Licensed 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.
-  -->
-
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-  <modelVersion>4.0.0</modelVersion>
-  <parent>
-    <artifactId>tajo-project</artifactId>
-    <groupId>org.apache.tajo</groupId>
-    <version>0.2.0-SNAPSHOT</version>
-    <relativePath>../../tajo-project</relativePath>
-  </parent>
-  <artifactId>tajo-frontend-sql</artifactId>
-  <packaging>jar</packaging>
-  <name>Tajo Frontend SQL</name>
-  <version>0.2.0-SNAPSHOT</version>
-  <properties>
-    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
-  </properties>
-
-  <build>
-    <plugins>
-      <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-compiler-plugin</artifactId>
-        <configuration>
-          <source>1.6</source>
-          <target>1.6</target>
-          <encoding>${project.build.sourceEncoding}</encoding>
-        </configuration>
-      </plugin>
-      <plugin>
-        <groupId>org.antlr</groupId>
-        <artifactId>antlr3-maven-plugin</artifactId>
-        <executions>
-          <execution>
-            <goals>
-              <goal>antlr</goal>
-            </goals>
-          </execution>
-        </executions>
-      </plugin>
-    </plugins>
-  </build>
-
-  <dependencies>
-    <dependency>
-      <groupId>org.apache.tajo</groupId>
-      <artifactId>tajo-common</artifactId>
-      <scope>compile</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.tajo</groupId>
-      <artifactId>tajo-algebra</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.tajo</groupId>
-      <artifactId>tajo-algebra</artifactId>
-      <type>test-jar</type>
-      <scope>test</scope>
-    </dependency>
-
-    <dependency>
-      <groupId>com.google.protobuf</groupId>
-      <artifactId>protobuf-java</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.antlr</groupId>
-      <artifactId>antlr</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>junit</groupId>
-      <artifactId>junit</artifactId>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.antlr</groupId>
-      <artifactId>antlr-runtime</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>commons-logging</groupId>
-      <artifactId>commons-logging</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>commons-logging</groupId>
-      <artifactId>commons-logging-api</artifactId>
-    </dependency>
-
-    <dependency>
-      <groupId>org.mockito</groupId>
-      <artifactId>mockito-core</artifactId>
-      <scope>test</scope>
-    </dependency>
-
-    <dependency>
-      <groupId>com.google.code.gson</groupId>
-      <artifactId>gson</artifactId>
-      <type>jar</type>
-      <scope>compile</scope>
-    </dependency>
-  </dependencies>
-
-  <profiles>
-    <profile>
-      <id>docs</id>
-      <activation>
-        <activeByDefault>false</activeByDefault>
-      </activation>
-      <build>
-        <plugins>
-          <plugin>
-            <groupId>org.apache.maven.plugins</groupId>
-            <artifactId>maven-javadoc-plugin</artifactId>
-            <executions>
-              <execution>
-                <!-- build javadoc jars per jar for publishing to maven -->
-                <id>module-javadocs</id>
-                <phase>package</phase>
-                <goals>
-                  <goal>jar</goal>
-                </goals>
-                <configuration>
-                  <destDir>${project.build.directory}</destDir>
-                </configuration>
-              </execution>
-            </executions>
-          </plugin>
-        </plugins>
-      </build>
-    </profile>
-    <profile>
-      <id>src</id>
-      <activation>
-        <activeByDefault>false</activeByDefault>
-      </activation>
-      <build>
-        <plugins>
-          <plugin>
-            <groupId>org.apache.maven.plugins</groupId>
-            <artifactId>maven-source-plugin</artifactId>
-            <executions>
-              <execution>
-                <!-- builds source jars and attaches them to the project for publishing -->
-                <id>tajo-java-sources</id>
-                <phase>package</phase>
-                <goals>
-                  <goal>jar-no-fork</goal>
-                </goals>
-              </execution>
-            </executions>
-          </plugin>
-        </plugins>
-      </build>
-    </profile>
-  </profiles>
-
-  <reporting>
-    <plugins>
-      <plugin>
-        <groupId>org.codehaus.mojo</groupId>
-        <artifactId>findbugs-maven-plugin</artifactId>
-        <version>2.3.2</version>
-        <configuration>
-          <xmlOutput>true</xmlOutput>
-          <xmlOutputDirectory>target/findbugs</xmlOutputDirectory>
-          <excludeFilterFile>src/main/findbugs/findbugs-exclude.xml</excludeFilterFile>
-        </configuration>
-      </plugin>
-    </plugins>
-  </reporting>
-</project>
-


[10/13] TAJO-87: Integration of tajo algebra module and SQL parser

Posted by hy...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java
new file mode 100644
index 0000000..92d0336
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java
@@ -0,0 +1,900 @@
+/**
+ * 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.engine.parser;
+
+import org.antlr.v4.runtime.ANTLRInputStream;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.tree.TerminalNode;
+import org.apache.tajo.algebra.*;
+import org.apache.tajo.algebra.Aggregation.GroupType;
+import org.apache.tajo.algebra.LiteralValue.LiteralType;
+import org.apache.tajo.engine.parser.SQLParser.*;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.apache.tajo.algebra.Aggregation.GroupElement;
+import static org.apache.tajo.common.TajoDataTypes.Type;
+import static org.apache.tajo.engine.parser.SQLParser.*;
+
+public class SQLAnalyzer extends SQLParserBaseVisitor<Expr> {
+  private SQLParser parser;
+
+  public SQLAnalyzer() {
+  }
+
+  public Expr parse(String sql) {
+    ANTLRInputStream input = new ANTLRInputStream(sql);
+    SQLLexer lexer = new SQLLexer(input);
+    CommonTokenStream tokens = new CommonTokenStream(lexer);
+    this.parser = new SQLParser(tokens);
+    parser.setBuildParseTree(true);
+    SqlContext context = parser.sql();
+    return visitSql(context);
+  }
+
+  @Override
+  public Expr visitSql(SqlContext ctx) {
+    return visit(ctx.statement());
+  }
+
+  @Override
+  public Expr visitNon_join_query_expression(SQLParser.Non_join_query_expressionContext ctx) {
+
+    Expr current = visitNon_join_query_term(ctx.non_join_query_term());
+    if (ctx.getChildCount() == 1) {
+      return current;
+    }
+
+    OpType operatorType;
+    Expr left;
+    for (int i = 1; i < ctx.getChildCount();) {
+      int idx = i;
+      boolean distinct = true;
+
+      if (ctx.getChild(idx) instanceof TerminalNode) {
+        if (((TerminalNode) ctx.getChild(idx)).getSymbol().getType() == UNION) {
+          operatorType = OpType.Union;
+        } else {
+          operatorType = OpType.Except;
+        }
+
+        idx++;
+
+        if (ctx.getChild(idx) instanceof TerminalNode) {
+          if (((TerminalNode) ctx.getChild(idx)).getSymbol().getType() == ALL) {
+            distinct = false;
+          }
+
+          idx++;
+        }
+
+        SQLParser.Query_termContext queryTermContext =
+            (SQLParser.Query_termContext) ctx.getChild(idx);
+        Expr right = visitQuery_term(queryTermContext);
+
+        left = current;
+        current = new SetOperation(operatorType, left, right, distinct);
+
+        i+=idx;
+      }
+    }
+
+    return current;
+  }
+
+  @Override
+  public Expr visitNon_join_query_term(Non_join_query_termContext ctx) {
+
+    Expr current = visitNon_join_query_primary(ctx.non_join_query_primary());
+    Expr left;
+
+    for (int i = 1; i < ctx.getChildCount();) {
+      int idx = i;
+      boolean distinct = true;
+
+      if (ctx.getChild(idx) instanceof TerminalNode) {
+        if (((TerminalNode) ctx.getChild(idx)).getSymbol().getType() == INTERSECT) {
+          idx++;
+        }
+
+        if (ctx.getChild(idx) instanceof TerminalNode) {
+          if (((TerminalNode) ctx.getChild(idx)).getSymbol().getType() == ALL) {
+            distinct = false;
+            idx++;
+          }
+        }
+
+        Query_primaryContext queryPrimaryContext = (Query_primaryContext) ctx.getChild(idx);
+        Expr right = visitQuery_primary(queryPrimaryContext);
+
+        left = current;
+        current = new SetOperation(OpType.Intersect, left, right, distinct);
+
+        i+=idx;
+      }
+    }
+
+    return current;
+
+  }
+
+  @Override
+  public Expr visitQuery_specification(SQLParser.Query_specificationContext ctx) {
+    Expr current = null;
+    if (ctx.table_expression() != null) {
+      current = visitFrom_clause(ctx.table_expression().from_clause());
+
+      if (ctx.table_expression().where_clause() != null) {
+        Selection selection = visitWhere_clause(ctx.table_expression().where_clause());
+        selection.setChild(current);
+        current = selection;
+      }
+
+      if (ctx.table_expression().groupby_clause() != null) {
+        Aggregation aggregation = visitGroupby_clause(ctx.table_expression().groupby_clause());
+        aggregation.setChild(current);
+        current = aggregation;
+
+        if (ctx.table_expression().having_clause() != null) {
+          Expr havingCondition = visitBoolean_value_expression(
+              ctx.table_expression().having_clause().boolean_value_expression());
+          aggregation.setHavingCondition(havingCondition);
+        }
+      }
+
+      if (ctx.table_expression().orderby_clause() != null) {
+        Sort sort = visitOrderby_clause(ctx.table_expression().orderby_clause());
+        sort.setChild(current);
+        current = sort;
+      }
+
+      if (ctx.table_expression().limit_clause() != null) {
+        Limit limit = visitLimit_clause(ctx.table_expression().limit_clause());
+        limit.setChild(current);
+        current = limit;
+      }
+    }
+
+    Projection projection = new Projection();
+
+    if (ctx.set_qualifier() != null && ctx.set_qualifier().DISTINCT() != null) {
+      projection.setDistinct();
+    }
+
+    if (ctx.select_list().MULTIPLY() != null) {
+      projection.setAll();
+    } else {
+      Target targets [] = new Target[ctx.select_list().derived_column().size()];
+      for (int i = 0; i < targets.length; i++) {
+        targets[i] = visitDerived_column(ctx.select_list().derived_column(i));
+      }
+      projection.setTargets(targets);
+    }
+
+    if (current != null) {
+      projection.setChild(current);
+    }
+
+    current = projection;
+
+    return current;
+  }
+
+  @Override
+  public RelationList visitFrom_clause(SQLParser.From_clauseContext ctx) {
+    Expr [] relations = new Expr[ctx.table_reference_list().table_reference().size()];
+    for (int i = 0; i < relations.length; i++) {
+      relations[i] = visit(ctx.table_reference_list().table_reference(i));
+    }
+    return new RelationList(relations);
+  }
+
+  @Override
+  public Selection visitWhere_clause(SQLParser.Where_clauseContext ctx) {
+    return new Selection(visitSearch_condition(ctx.search_condition()));
+  }
+
+  @Override
+  public Aggregation visitGroupby_clause(SQLParser.Groupby_clauseContext ctx) {
+    Aggregation clause = new Aggregation();
+
+    // If grouping group is not empty
+    if (ctx.grouping_element_list().grouping_element().get(0).empty_grouping_set() == null) {
+      GroupElement [] groups = new GroupElement[ctx.grouping_element_list().
+          grouping_element().size()];
+      for (int i = 0; i < groups.length; i++) {
+        SQLParser.Grouping_elementContext element =
+            ctx.grouping_element_list().grouping_element().get(i);
+        if (element.ordinary_grouping_set() != null) {
+          groups[i] = new GroupElement(GroupType.OrdinaryGroup,
+              getColumnReferences(element.ordinary_grouping_set().column_reference_list()));
+        } else if (element.rollup_list() != null) {
+          groups[i] = new GroupElement(GroupType.Rollup,
+              getColumnReferences(element.rollup_list().c.column_reference_list()));
+        } else if (element.cube_list() != null) {
+          groups[i] = new GroupElement(GroupType.Cube,
+              getColumnReferences(element.cube_list().c.column_reference_list()));
+        }
+      }
+      clause.setGroups(groups);
+    }
+
+    return clause;
+  }
+
+  @Override
+  public Sort visitOrderby_clause(SQLParser.Orderby_clauseContext ctx) {
+    int size = ctx.sort_specifier_list().sort_specifier().size();
+    Sort.SortSpec specs [] = new Sort.SortSpec[size];
+    for (int i = 0; i < size; i++) {
+      SQLParser.Sort_specifierContext specContext = ctx.sort_specifier_list().sort_specifier(i);
+      ColumnReferenceExpr column = visitColumn_reference(specContext.column);
+      specs[i] = new Sort.SortSpec(column);
+      if (specContext.order_specification() != null) {
+        if (specContext.order.DESC() != null) {
+          specs[i].setDescending();
+        }
+      }
+
+      if (specContext.null_ordering() != null) {
+        if (specContext.null_ordering().FIRST() != null) {
+          specs[i].setNullFirst();
+        }
+      }
+    }
+
+    return new Sort(specs);
+  }
+
+  @Override
+  public Limit visitLimit_clause(SQLParser.Limit_clauseContext ctx) {
+    return new Limit(visitNumeric_value_expression(ctx.numeric_value_expression()));
+  }
+
+  @Override
+  public Expr visitJoined_table(SQLParser.Joined_tableContext ctx) {
+    Expr top = visitTable_primary(ctx.table_primary());
+
+    // The following loop builds a left deep join tree.
+    Join join;
+    for (int i = 0; i < ctx.joined_table_primary().size(); i++) {
+      join = visitJoined_table_primary(ctx.joined_table_primary(i));
+      join.setLeft(top);
+      top = join;
+    }
+
+    return top;
+  }
+
+  @Override
+  public Join visitJoined_table_primary(SQLParser.Joined_table_primaryContext ctx) {
+    Join join;
+    if (ctx.CROSS() != null) {
+      join = new Join(JoinType.CROSS_JOIN);
+    } else if (ctx.UNION() != null) {
+      join = new Join(JoinType.UNION);
+    } else { // qualified join or natural
+      if (ctx.join_type() != null && ctx.join_type().outer_join_type() != null) {
+        Outer_join_type_part2Context outer_join_typeContext = ctx.join_type().outer_join_type()
+            .outer_join_type_part2();
+        if (outer_join_typeContext.FULL() != null) {
+          join = new Join(JoinType.FULL_OUTER);
+        } else if (outer_join_typeContext.LEFT() != null) {
+          join = new Join(JoinType.LEFT_OUTER);
+        } else {
+          join = new Join(JoinType.RIGHT_OUTER);
+        }
+      } else {
+        join = new Join(JoinType.INNER);
+      }
+
+      if (ctx.NATURAL() != null) {
+        join.setNatural();
+      }
+
+      if (ctx.join_specification() != null) { // only for qualified join
+        if (ctx.join_specification().join_condition() != null) {
+          Expr searchCondition = visitSearch_condition(ctx.join_specification().
+              join_condition().search_condition());
+          join.setQual(searchCondition);
+        } else if (ctx.join_specification().named_columns_join() != null) {
+          ColumnReferenceExpr [] columns = getColumnReferences(ctx.join_specification().
+              named_columns_join().column_reference_list());
+          join.setJoinColumns(columns);
+        }
+      }
+    }
+
+    join.setRight(visitTable_primary(ctx.right));
+    return join;
+  }
+
+  private ColumnReferenceExpr [] getColumnReferences(Column_reference_listContext ctx) {
+    ColumnReferenceExpr [] columnRefs = new ColumnReferenceExpr[ctx.column_reference().size()];
+    for (int i = 0; i < columnRefs.length; i++) {
+      columnRefs[i] = visitColumn_reference(ctx.column_reference(i));
+    }
+    return columnRefs;
+  }
+
+  @Override
+  public Expr visitTable_primary(SQLParser.Table_primaryContext ctx) {
+    if (ctx.table_or_query_name() != null) {
+      Relation relation = new Relation(ctx.table_or_query_name().getText());
+      if (ctx.AS() != null) {
+        relation.setAlias(ctx.alias.getText());
+      }
+      return relation;
+    } else if (ctx.derived_table() != null) {
+      return new TableSubQuery(ctx.name.getText(), visit(ctx.derived_table().table_subquery()));
+    } else {
+      return null;
+    }
+  }
+
+
+  @Override
+  public Expr visitSubquery(SQLParser.SubqueryContext ctx) {
+    return visitQuery_expression(ctx.query_expression());
+  }
+
+  @Override
+  public CaseWhenPredicate visitSimple_case(SQLParser.Simple_caseContext ctx) {
+    Expr leftTerm = visitBoolean_value_expression(ctx.boolean_value_expression());
+    CaseWhenPredicate caseWhen = new CaseWhenPredicate();
+
+    for (int i = 0; i < ctx.simple_when_clause().size(); i++) {
+      Simple_when_clauseContext simpleWhenCtx = ctx.simple_when_clause(i);
+      BinaryOperator bin = new BinaryOperator(OpType.Equals, leftTerm,
+          visitNumeric_value_expression(simpleWhenCtx.numeric_value_expression()));
+      caseWhen.addWhen(bin, buildCaseResult(simpleWhenCtx.result()));
+    }
+    if (ctx.else_clause() != null) {
+      caseWhen.setElseResult(buildCaseResult(ctx.else_clause().result()));
+    }
+    return caseWhen;
+  }
+
+  private Expr buildCaseResult(ResultContext result) {
+    if (result.NULL() != null) {
+      return new NullValue();
+    } else {
+      return visitNumeric_value_expression(result.numeric_value_expression());
+    }
+  }
+
+  @Override public CaseWhenPredicate visitSearched_case(SQLParser.Searched_caseContext ctx) {
+    CaseWhenPredicate caseWhen = new CaseWhenPredicate();
+
+    for (int i = 0; i < ctx.searched_when_clause().size(); i++) {
+      Searched_when_clauseContext searchedWhenCtx = ctx.searched_when_clause(i);
+      caseWhen.addWhen(
+          visitSearch_condition(searchedWhenCtx.search_condition()),
+          buildCaseResult(searchedWhenCtx.result()));
+    }
+    if (ctx.else_clause() != null) {
+      caseWhen.setElseResult(buildCaseResult(ctx.else_clause().result()));
+    }
+    return caseWhen;
+  }
+
+  @Override
+  public Expr visitAnd_predicate(SQLParser.And_predicateContext ctx) {
+    Expr current = visitBoolean_factor(ctx.boolean_factor(0));
+
+    Expr left;
+    Expr right;
+    for (int i = 1; i < ctx.boolean_factor().size(); i++) {
+      left = current;
+      right = visitBoolean_factor(ctx.boolean_factor(i));
+      current = new BinaryOperator(OpType.And, left, right);
+    }
+
+    return current;
+  }
+
+  @Override
+  public Expr visitBoolean_value_expression(SQLParser.Boolean_value_expressionContext ctx) {
+
+    Expr current = visitAnd_predicate(ctx.and_predicate(0));
+
+    Expr left;
+    Expr right;
+    for (int i = 1; i < ctx.and_predicate().size(); i++) {
+      left = current;
+      right = visitAnd_predicate(ctx.and_predicate(i));
+      current = new BinaryOperator(OpType.Or, left, right);
+    }
+
+    return current;
+  }
+
+  @Override
+  public Expr visitBoolean_factor(SQLParser.Boolean_factorContext ctx) {
+    if (ctx.NOT() != null) {
+      return new NotExpr(visitBoolean_test(ctx.boolean_test()));
+    } else {
+      return visitBoolean_test(ctx.boolean_test());
+    }
+  }
+
+  @Override
+  public Expr visitBoolean_test(SQLParser.Boolean_testContext ctx) {
+    return visitBoolean_primary(ctx.boolean_primary());
+  }
+
+  @Override
+  public Expr visitBoolean_primary(SQLParser.Boolean_primaryContext ctx) {
+    if (ctx.predicate() != null) {
+      return visitPredicate(ctx.predicate());
+    } else if (ctx.numeric_value_expression() != null) {
+      return visitNumeric_value_expression(ctx.numeric_value_expression());
+    } else if (ctx.case_expression() != null) {
+      return visitCase_expression(ctx.case_expression());
+    } else if (ctx.boolean_value_expression() != null) {
+      return visitBoolean_value_expression(ctx.boolean_value_expression());
+    } else {
+      return visitChildren(ctx);
+    }
+  }
+
+  @Override
+  public BinaryOperator visitComparison_predicate(SQLParser.Comparison_predicateContext ctx) {
+    TerminalNode operator = (TerminalNode) ctx.comp_op().getChild(0);
+    return new BinaryOperator(tokenToExprType(operator.getSymbol().getType()),
+        visitNumeric_value_expression(ctx.left),
+        visitNumeric_value_expression(ctx.right));
+  }
+
+  @Override
+  public Expr visitNumeric_value_expression(SQLParser.Numeric_value_expressionContext ctx) {
+    Expr current = visitTerm(ctx.term(0));
+
+    Expr left;
+    Expr right;
+    for (int i = 1; i < ctx.getChildCount(); i++) {
+      left = current;
+      TerminalNode operator = (TerminalNode) ctx.getChild(i++);
+      right = visitTerm((TermContext) ctx.getChild(i));
+
+      if (operator.getSymbol().getType() == PLUS) {
+        current = new BinaryOperator(OpType.Plus, left, right);
+      } else {
+        current = new BinaryOperator(OpType.Minus, left, right);
+      }
+    }
+
+    return current;
+  }
+
+  @Override
+  public Expr visitTerm(SQLParser.TermContext ctx) {
+    Expr current = visitNumeric_primary(ctx.numeric_primary(0));
+
+    Expr left;
+    Expr right;
+    for (int i = 1; i < ctx.getChildCount(); i++) {
+      left = current;
+      TerminalNode operator = (TerminalNode) ctx.getChild(i++);
+      right = visitNumeric_primary((Numeric_primaryContext) ctx.getChild(i));
+
+      if (operator.getSymbol().getType() == MULTIPLY) {
+        current = new BinaryOperator(OpType.Multiply, left, right);
+      } else if (operator.getSymbol().getType() == DIVIDE) {
+        current = new BinaryOperator(OpType.Divide, left, right);
+      } else {
+        current = new BinaryOperator(OpType.Modular, left, right);
+      }
+    }
+
+    return current;
+  }
+
+  @Override
+  public Expr visitNumeric_primary(SQLParser.Numeric_primaryContext ctx) {
+    if (ctx.numeric_value_expression() != null) {
+      return visitNumeric_value_expression(ctx.numeric_value_expression());
+    } else {
+      return visitChildren(ctx);
+    }
+  }
+
+  public static OpType tokenToExprType(int tokenId) {
+    switch (tokenId) {
+      case SQLParser.UNION: return OpType.Union;
+      case SQLParser.EXCEPT: return OpType.Except;
+      case SQLParser.INTERSECT: return OpType.Intersect;
+
+      case SQLParser.AND: return OpType.And;
+      case SQLParser.OR: return OpType.Or;
+
+      case SQLParser.EQUAL: return OpType.Equals;
+      case SQLParser.NOT_EQUAL: return OpType.NotEquals;
+      case SQLParser.LTH: return OpType.LessThan;
+      case SQLParser.LEQ: return OpType.LessThanOrEquals;
+      case SQLParser.GTH: return OpType.GreaterThan;
+      case SQLParser.GEQ: return OpType.GreaterThanOrEquals;
+
+      case SQLParser.MULTIPLY: return OpType.Multiply;
+      case SQLParser.DIVIDE: return OpType.Divide;
+      case SQLParser.MODULAR: return OpType.Modular;
+      case SQLParser.PLUS: return OpType.Plus;
+      case SQLParser.MINUS: return OpType.Minus;
+
+      default: throw new RuntimeException("Unknown Token Id: " + tokenId);
+    }
+  }
+
+  @Override
+  public InPredicate visitIn_predicate(SQLParser.In_predicateContext ctx) {
+    return new InPredicate(visitChildren(ctx.numeric_value_expression()),
+        visitChildren(ctx.in_predicate_value()));
+  }
+
+  @Override
+  public ValueListExpr visitIn_value_list(SQLParser.In_value_listContext ctx) {
+    int size = ctx.numeric_value_expression().size();
+    Expr [] exprs = new Expr[size];
+    for (int i = 0; i < size; i++) {
+      exprs[i] = visit(ctx.numeric_value_expression(i));
+    }
+    return new ValueListExpr(exprs);
+  }
+
+  @Override
+  public Expr visitArray(SQLParser.ArrayContext ctx) {
+    int size = ctx.numeric_value_expression().size();
+    Expr [] exprs = new Expr[size];
+    for (int i = 0; i < size; i++) {
+      exprs[i] = visit(ctx.numeric_value_expression(i));
+    }
+    return new ValueListExpr(exprs);
+  }
+
+  @Override
+  public LikePredicate visitLike_predicate(SQLParser.Like_predicateContext ctx) {
+    boolean not = ctx.NOT() != null;
+
+    ColumnReferenceExpr predicand = (ColumnReferenceExpr) visit(ctx.column_reference());
+    Expr pattern = new LiteralValue(stripQuote(ctx.Character_String_Literal().getText()),
+        LiteralType.String);
+    return new LikePredicate(not, predicand, pattern);
+  }
+
+  @Override
+  public IsNullPredicate visitNull_predicate(SQLParser.Null_predicateContext ctx) {
+    boolean not = ctx.NOT() != null;
+
+    ColumnReferenceExpr predicand = (ColumnReferenceExpr) visit(ctx.numeric_value_expression());
+    return new IsNullPredicate(not, predicand);
+  }
+
+  @Override
+  public ColumnReferenceExpr visitColumn_reference(SQLParser.Column_referenceContext ctx) {
+    ColumnReferenceExpr column = new ColumnReferenceExpr(ctx.name.getText());
+    if (ctx.tb_name != null) {
+      column.setTableName(ctx.tb_name.getText());
+    }
+
+    return column;
+  }
+
+  @Override
+  public LiteralValue visitUnsigned_numerical_literal(SQLParser.Unsigned_numerical_literalContext ctx) {
+    if (ctx.NUMBER() != null) {
+      return new LiteralValue(ctx.getText(), LiteralType.Unsigned_Integer);
+    } else {
+      return new LiteralValue(ctx.getText(), LiteralType.Unsigned_Float);
+    }
+  }
+
+  @Override
+  public FunctionExpr visitRoutine_invocation(SQLParser.Routine_invocationContext ctx) {
+    if (ctx.COUNT() != null && ctx.MULTIPLY() != null) {
+      return new CountRowsFunctionExpr();
+    }else if (ctx.COUNT() != null && ctx.sql_argument_list().boolean_value_expression().size() == 1) {
+      return new CountValueFunctionExpr(visitBoolean_value_expression(
+          ctx.sql_argument_list().boolean_value_expression().get(0)));
+    } else {
+      String signature = ctx.Identifier().getText();
+      FunctionExpr function = new FunctionExpr(signature);
+      if (ctx.sql_argument_list() != null) {
+        int numArgs = ctx.sql_argument_list().boolean_value_expression().size();
+        Expr [] argument_list = new Expr[numArgs];
+        for (int i = 0; i < numArgs; i++) {
+          argument_list[i] = visitBoolean_value_expression(ctx.sql_argument_list().
+              boolean_value_expression().get(i));
+        }
+
+        function.setParams(argument_list);
+      }
+      return function;
+    }
+  }
+
+  @Override
+  public Target visitDerived_column(SQLParser.Derived_columnContext ctx) {
+    Target target = new Target(visitBoolean_value_expression(ctx.boolean_value_expression()));
+    if (ctx.as_clause() != null) {
+      target.setAlias(ctx.as_clause().Identifier().getText());
+    }
+    return target;
+  }
+
+  @Override
+  public LiteralValue visitCharacter_string_type(SQLParser.Character_string_typeContext ctx) {
+    return new LiteralValue(stripQuote(ctx.getText()), LiteralType.String);
+  }
+
+  @Override
+  public LiteralValue visitString_value_expr(SQLParser.String_value_exprContext ctx) {
+    return new LiteralValue(stripQuote(ctx.getText()), LiteralType.String);
+  }
+
+  @Override
+  public Expr visitCreate_table_statement(SQLParser.Create_table_statementContext ctx) {
+    String tableName = ctx.table_name().getText();
+    CreateTable createTable = new CreateTable(tableName);
+
+    if (ctx.EXTERNAL() != null) {
+      createTable.setExternal();
+
+      CreateTable.ColumnDefinition [] elements = getDefinitions(ctx.table_elements());
+      String fileType = ctx.file_type.getText();
+      String path = stripQuote(ctx.path.getText());
+
+      createTable.setTableElements(elements);
+      createTable.setStorageType(fileType);
+      createTable.setLocation(path);
+    } else {
+      if (ctx.table_elements() != null) {
+        CreateTable.ColumnDefinition [] elements = getDefinitions(ctx.table_elements());
+        createTable.setTableElements(elements);
+      }
+
+      if (ctx.USING() != null) {
+        String fileType = ctx.file_type.getText();
+        createTable.setStorageType(fileType);
+      }
+
+      if (ctx.query_expression() != null) {
+        Expr subquery = visitQuery_expression(ctx.query_expression());
+        createTable.setSubQuery(subquery);
+      }
+    }
+
+    if (ctx.param_clause() != null) {
+      Map<String, String> params = getParams(ctx.param_clause());
+      createTable.setParams(params);
+    }
+    return createTable;
+  }
+
+  private CreateTable.ColumnDefinition [] getDefinitions(SQLParser.Table_elementsContext ctx) {
+    int size = ctx.field_element().size();
+    CreateTable.ColumnDefinition [] elements = new CreateTable.ColumnDefinition[size];
+    for (int i = 0; i < size; i++) {
+      String name = ctx.field_element(i).name.getText();
+      TypeDefinition typeDef = getDataType(ctx.field_element(i).field_type().data_type());
+      String type = typeDef.getType();
+      elements[i] = new CreateTable.ColumnDefinition(name, type);
+      if (typeDef.getLengthOrPrecision() != null) {
+        elements[i].setLengthOrPrecision(typeDef.getLengthOrPrecision());
+
+        if (typeDef.getScale() != null) {
+          elements[i].setScale(typeDef.getScale());
+        }
+      }
+
+    }
+
+    return elements;
+  }
+
+  private TypeDefinition getDataType(SQLParser.Data_typeContext type) {
+    SQLParser.Predefined_typeContext predefined_type = type.predefined_type();
+
+    TypeDefinition typeDefinition = null;
+    if (predefined_type.character_string_type() != null) {
+      SQLParser.Character_string_typeContext character_string_type =
+          predefined_type.character_string_type();
+
+      if ((character_string_type.CHARACTER() != null || character_string_type.CHAR() != null) &&
+          character_string_type.VARYING() == null) {
+
+        typeDefinition = new TypeDefinition(Type.CHAR.name());
+
+        if (character_string_type.type_length() != null) {
+          typeDefinition.setLengthOrPrecision(
+              Integer.parseInt(character_string_type.type_length().NUMBER().getText()));
+        }
+
+      } else if (character_string_type.VARCHAR() != null
+          || character_string_type.VARYING() != null) {
+
+        typeDefinition = new TypeDefinition(Type.VARCHAR.name());
+
+        if (character_string_type.type_length() != null) {
+          typeDefinition.setLengthOrPrecision(
+              Integer.parseInt(character_string_type.type_length().NUMBER().getText()));
+        }
+
+      } else if (character_string_type.TEXT() != null) {
+        typeDefinition =  new TypeDefinition(Type.TEXT.name());
+      }
+
+    } else if (predefined_type.national_character_string_type() != null) {
+      SQLParser.National_character_string_typeContext nchar_type =
+          predefined_type.national_character_string_type();
+      if ((nchar_type.CHAR() != null || nchar_type.CHARACTER() != null
+          || nchar_type.NCHAR() != null) && nchar_type.VARYING() == null) {
+        typeDefinition = new TypeDefinition(Type.NCHAR.name());
+      } else if (nchar_type.NVARCHAR() != null || nchar_type.VARYING() != null) {
+        typeDefinition = new TypeDefinition(Type.NVARCHAR.name());
+      }
+
+      if (nchar_type.type_length() != null) {
+        typeDefinition.setLengthOrPrecision(
+            Integer.parseInt(nchar_type.type_length().NUMBER().getText()));
+      }
+
+    } else if (predefined_type.binary_large_object_string_type() != null) {
+      SQLParser.Binary_large_object_string_typeContext blob_type =
+          predefined_type.binary_large_object_string_type();
+      typeDefinition = new TypeDefinition(Type.BLOB.name());
+      if (blob_type.type_length() != null) {
+        typeDefinition.setLengthOrPrecision(
+            Integer.parseInt(blob_type.type_length().NUMBER().getText()));
+      }
+    } else if (predefined_type.numeric_type() != null) {
+      // exact number
+      if (predefined_type.numeric_type().exact_numeric_type() != null) {
+        SQLParser.Exact_numeric_typeContext exactType =
+            predefined_type.numeric_type().exact_numeric_type();
+        if (exactType.TINYINT() != null || exactType.INT1() != null) {
+          typeDefinition = new TypeDefinition(Type.INT1.name());
+        } else if (exactType.INT2() != null || exactType.SMALLINT() != null) {
+          typeDefinition = new TypeDefinition(Type.INT2.name());
+        } else if (exactType.INT4() != null || exactType.INTEGER() != null ||
+            exactType.INT() != null) {
+          typeDefinition = new TypeDefinition(Type.INT4.name());
+        } else if (exactType.INT8() != null || exactType.BIGINT() != null) {
+          typeDefinition = new TypeDefinition(Type.INT8.name());
+        } else if (exactType.NUMERIC() != null) {
+          typeDefinition = new TypeDefinition(Type.NUMERIC.name());
+        } else if (exactType.DECIMAL() != null || exactType.DEC() != null) {
+          typeDefinition = new TypeDefinition(Type.DECIMAL.name());
+        }
+
+        if (typeDefinition.getType().equals(Type.NUMERIC.name()) ||
+            typeDefinition.getType().equals(Type.DECIMAL.name())) {
+          if (exactType.precision_param() != null) {
+            if (exactType.precision_param().scale != null) {
+              typeDefinition.setScale(
+                  Integer.parseInt(exactType.precision_param().scale.getText()));
+            }
+            typeDefinition.setLengthOrPrecision(
+                Integer.parseInt(exactType.precision_param().precision.getText()));
+          }
+        }
+      } else { // approximate number
+        SQLParser.Approximate_numeric_typeContext approximateType =
+            predefined_type.numeric_type().approximate_numeric_type();
+        if (approximateType.FLOAT() != null || approximateType.FLOAT4() != null
+            || approximateType.REAL() != null) {
+          typeDefinition = new TypeDefinition(Type.FLOAT4.name());
+        } else if (approximateType.FLOAT8() != null || approximateType.DOUBLE() != null) {
+          typeDefinition = new TypeDefinition(Type.FLOAT8.name());
+        }
+      }
+    } else if (predefined_type.boolean_type() != null)  {
+      typeDefinition = new TypeDefinition(Type.BOOLEAN.name());
+    } else if (predefined_type.datetime_type() != null) {
+      SQLParser.Datetime_typeContext dateTimeType = predefined_type.datetime_type();
+      if (dateTimeType.DATE() != null) {
+        typeDefinition = new TypeDefinition(Type.DATE.name());
+      } else if (dateTimeType.TIME(0) != null && dateTimeType.ZONE() == null) {
+        typeDefinition = new TypeDefinition(Type.TIME.name());
+      } else if ((dateTimeType.TIME(0) != null && dateTimeType.ZONE() != null) ||
+          dateTimeType.TIMETZ() != null) {
+        typeDefinition = new TypeDefinition(Type.TIMEZ.name());
+      } else if (dateTimeType.TIMESTAMP() != null && dateTimeType.ZONE() == null) {
+        typeDefinition = new TypeDefinition(Type.TIMESTAMP.name());
+      } else if ((dateTimeType.TIMESTAMP() != null && dateTimeType.ZONE() != null) ||
+          dateTimeType.TIMESTAMPTZ() != null) {
+        typeDefinition = new TypeDefinition(Type.TIMESTAMPZ.name());
+      }
+    } else if (predefined_type.bit_type() != null) {
+      SQLParser.Bit_typeContext bitType = predefined_type.bit_type();
+      if (bitType.VARBIT() != null || bitType.VARYING() != null) {
+        typeDefinition = new TypeDefinition(Type.VARBIT.name());
+      } else {
+        typeDefinition = new TypeDefinition(Type.BIT.name());
+      }
+      if (bitType.type_length() != null) {
+        typeDefinition.setLengthOrPrecision(
+            Integer.parseInt(bitType.type_length().NUMBER().getText()));
+      }
+    } else if (predefined_type.binary_type() != null) {
+      SQLParser.Binary_typeContext binaryType = predefined_type.binary_type();
+      if (binaryType.VARBINARY() != null || binaryType.VARYING() != null) {
+        typeDefinition = new TypeDefinition(Type.VARBINARY.name());
+      } else {
+        typeDefinition = new TypeDefinition(Type.BINARY.name());
+      }
+
+      if (binaryType.type_length() != null) {
+        typeDefinition.setLengthOrPrecision(
+            Integer.parseInt(binaryType.type_length().NUMBER().getText()));
+      }
+    }
+
+    return typeDefinition;
+  }
+
+  public static class TypeDefinition {
+    private String type;
+    private Integer length_or_precision;
+    private Integer scale;
+
+    TypeDefinition(String type) {
+      this.type = type;
+    }
+
+    String getType() {
+      return type;
+    }
+
+    public void setLengthOrPrecision(Integer length_or_precision) {
+      this.length_or_precision = length_or_precision;
+    }
+
+    public Integer getLengthOrPrecision() {
+      return length_or_precision;
+    }
+
+    void setScale(Integer scale) {
+      this.scale = scale;
+    }
+
+    public Integer getScale() {
+      return this.scale;
+    }
+  }
+
+  @Override
+  public Expr visitDrop_table_statement(SQLParser.Drop_table_statementContext ctx) {
+    return new DropTable(ctx.table_name().getText());
+  }
+
+
+  private Map<String, String> getParams(SQLParser.Param_clauseContext ctx) {
+    Map<String, String> params = new HashMap<String, String>();
+    for (int i = 0; i < ctx.param().size(); i++) {
+      params.put(stripQuote(ctx.param(i).key.getText()), stripQuote(ctx.param(i).value.getText()));
+    }
+
+    return params;
+  }
+
+  private static String stripQuote(String str) {
+    return str.substring(1, str.length() - 1);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SetStmt.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SetStmt.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SetStmt.java
deleted file mode 100644
index 126e4bd..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SetStmt.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/**
- * 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.engine.parser;
-
-import org.apache.tajo.engine.planner.PlanningContext;
-
-import java.util.Map.Entry;
-
-public class SetStmt extends ParseTree {
-  private ParseTree leftTree;
-  private ParseTree rightTree;
-  private boolean distinct = true;
-
-  public SetStmt(final PlanningContext context,
-                 final StatementType type,
-                 final ParseTree leftTree,
-                 final ParseTree rightTree,
-                 boolean distinct) {
-    super(context, type);
-    this.leftTree = leftTree;
-    this.rightTree = rightTree;
-    this.distinct = distinct;
-
-    for (Entry<String, String> entry : leftTree.getAliasToNames()) {
-       addTableRef(entry.getValue(), entry.getKey());
-    }
-
-    for (Entry<String, String> entry : rightTree.getAliasToNames()) {
-      addTableRef(entry.getValue(), entry.getKey());
-    }
-  }
-  
-  public boolean isDistinct() {
-    return distinct;
-  }
-  
-  public ParseTree getLeftTree() {
-    return this.leftTree;
-  }
-  
-  public ParseTree getRightTree() {
-    return this.rightTree;
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/StatementType.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/StatementType.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/StatementType.java
deleted file mode 100644
index de9b7e6..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/StatementType.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/**
- * 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.engine.parser;
-
-public enum StatementType {
-	// Store
-  STORE,
-  CREATE_TABLE_AS,
-  COPY,
-
-  // Select
-	SELECT,
-	UNION,
-	EXCEPT,
-	INTERSECT,
-	
-	// Update
-	INSERT,
-	UPDATE,
-	DELETE,
-	
-	// Schema	
-	CREATE_TABLE,
-	DROP_TABLE,
-	
-	// INDEX
-	CREATE_INDEX,
-	DROP_INDEX,
-
-	// Control
-	SHOW_TABLES,
-	DESC_TABLE,
-	SHOW_FUNCTION,
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/TableMap.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/TableMap.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/TableMap.java
deleted file mode 100644
index 2fce526..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/TableMap.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/**
- * 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.engine.parser;
-
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
-import org.apache.tajo.catalog.exception.AlreadyExistsTableException;
-
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Set;
-
-public class TableMap {
-  private Map<String, String> aliasToNameMap = Maps.newHashMap();
-  private Map<String, Set<String>> nameToAliasMap = Maps.newHashMap();
-
-  public boolean exists(String alias) {
-    return aliasToNameMap.containsKey(alias);
-  }
-
-  public void addFromTable(String tableName, String alias) {
-    if (aliasToNameMap.containsKey(alias)) {
-      throw new AlreadyExistsTableException();
-    }
-    aliasToNameMap.put(alias, tableName);
-
-    if (nameToAliasMap.containsKey(tableName)) {
-      Preconditions.checkState(!nameToAliasMap.get(tableName).contains(alias),
-          "There is inconsistency of the map between name and alias");
-      nameToAliasMap.get(tableName).add(alias);
-    } else {
-      nameToAliasMap.put(tableName, Sets.newHashSet(alias));
-    }
-  }
-
-  public void addFromTable(QueryBlock.FromTable fromTable) {
-    addFromTable(fromTable.getTableName(), fromTable.getTableId());
-  }
-
-  public String getTableNameByAlias(String alias) {
-    return aliasToNameMap.get(alias);
-  }
-
-  public Iterable<String> getAllTableNames() {
-    return nameToAliasMap.keySet();
-  }
-
-  public Iterable<Entry<String, String>> getAliasToNames() {
-    return aliasToNameMap.entrySet();
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/FromTable.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/FromTable.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/FromTable.java
new file mode 100644
index 0000000..24161d4
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/FromTable.java
@@ -0,0 +1,105 @@
+/**
+ * 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.engine.planner;
+
+import com.google.gson.Gson;
+import com.google.gson.annotations.Expose;
+import org.apache.tajo.catalog.Schema;
+import org.apache.tajo.catalog.TableDesc;
+import org.apache.tajo.catalog.proto.CatalogProtos;
+import org.apache.tajo.engine.json.GsonCreator;
+import org.apache.tajo.util.TUtil;
+
+public class FromTable implements Cloneable {
+  @Expose
+  private TableDesc desc;
+  @Expose
+  private String alias = null;
+
+  public FromTable() {}
+
+  public FromTable(final TableDesc desc) {
+    this.desc = desc;
+  }
+
+  public FromTable(final TableDesc desc, final String alias) {
+    this(desc);
+    this.alias = alias;
+  }
+
+  public final String getTableName() {
+    return desc.getId();
+  }
+
+  public final String getTableId() {
+    return alias == null ? desc.getId() : alias;
+  }
+
+  public final CatalogProtos.StoreType getStoreType() {
+    return desc.getMeta().getStoreType();
+  }
+
+  public final Schema getSchema() {
+    return desc.getMeta().getSchema();
+  }
+
+  public final void setAlias(String alias) {
+    this.alias = alias;
+  }
+
+  public final String getAlias() {
+    return alias;
+  }
+
+  public final boolean hasAlias() {
+    return alias != null;
+  }
+
+  public final String toString() {
+    if (alias != null)
+      return desc.getId() + " as " + alias;
+    else
+      return desc.getId();
+  }
+
+  public boolean equals(Object obj) {
+    if (obj instanceof FromTable) {
+      FromTable other = (FromTable) obj;
+      return this.desc.equals(other.desc)
+          && TUtil.checkEquals(this.alias, other.alias);
+    } else {
+      return false;
+    }
+  }
+
+  @Override
+  public Object clone() throws CloneNotSupportedException {
+    FromTable table = (FromTable) super.clone();
+    table.desc = (TableDesc) desc.clone();
+    table.alias = alias;
+
+    return table;
+  }
+
+  public String toJSON() {
+    desc.initFromProto();
+    Gson gson = GsonCreator.getInstance();
+    return gson.toJson(this, FromTable.class);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/GroupElement.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/GroupElement.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/GroupElement.java
new file mode 100644
index 0000000..3c8ee5c
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/GroupElement.java
@@ -0,0 +1,65 @@
+/**
+ * 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.engine.planner;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.annotations.Expose;
+import org.apache.tajo.algebra.Aggregation;
+import org.apache.tajo.catalog.Column;
+
+public class GroupElement implements Cloneable {
+  @Expose
+  private Aggregation.GroupType type;
+  @Expose private Column[] columns;
+
+  @SuppressWarnings("unused")
+  public GroupElement() {
+    // for gson
+  }
+
+  public GroupElement(Aggregation.GroupType type, Column[] columns) {
+    this.type = type;
+    this.columns = columns;
+  }
+
+  public Aggregation.GroupType getType() {
+    return this.type;
+  }
+
+  public Column [] getColumns() {
+    return this.columns;
+  }
+
+  public String toString() {
+    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
+        .setPrettyPrinting().create();
+    return gson.toJson(this);
+  }
+
+  public Object clone() throws CloneNotSupportedException {
+    GroupElement groups = (GroupElement) super.clone();
+    groups.type = type;
+    groups.columns = new Column[columns.length];
+    for (int i = 0; i < columns.length; i++) {
+      groups.columns[i++] = (Column) columns[i].clone();
+    }
+    return groups;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/JoinClause.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/JoinClause.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/JoinClause.java
new file mode 100644
index 0000000..218875a
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/JoinClause.java
@@ -0,0 +1,126 @@
+/**
+ * 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.engine.planner;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.annotations.Expose;
+import org.apache.tajo.algebra.JoinType;
+import org.apache.tajo.catalog.Column;
+import org.apache.tajo.engine.eval.EvalNode;
+
+public class JoinClause implements Cloneable {
+  @Expose private JoinType joinType;
+  @Expose private FromTable left;
+  @Expose private FromTable right;
+  @Expose private JoinClause leftJoin;
+  @Expose private EvalNode joinQual;
+  @Expose private Column[] joinColumns;
+  @Expose private boolean natural = false;
+
+  @SuppressWarnings("unused")
+  public JoinClause() {
+    // for gson
+  }
+
+  public JoinClause(final JoinType joinType) {
+    this.joinType = joinType;
+  }
+
+  public JoinClause(final JoinType joinType, final FromTable right) {
+    this.joinType = joinType;
+    this.right = right;
+  }
+
+  public JoinClause(final JoinType joinType, final FromTable left,
+                    final FromTable right) {
+    this(joinType, right);
+    this.left = left;
+  }
+
+  public JoinType getJoinType() {
+    return this.joinType;
+  }
+
+  public void setNatural() {
+    this.natural = true;
+  }
+
+  public boolean isNatural() {
+    return this.natural;
+  }
+
+  public void setRight(FromTable right) {
+    this.right = right;
+  }
+
+  public void setLeft(FromTable left) {
+    this.left = left;
+  }
+
+  public void setLeft(JoinClause left) {
+    this.leftJoin = left;
+  }
+
+  public boolean hasLeftJoin() {
+    return leftJoin != null;
+  }
+
+  public FromTable getLeft() {
+    return this.left;
+  }
+
+  public FromTable getRight() {
+    return this.right;
+  }
+
+  public JoinClause getLeftJoin() {
+    return this.leftJoin;
+  }
+
+  public void setJoinQual(EvalNode qual) {
+    this.joinQual = qual;
+  }
+
+  public boolean hasJoinQual() {
+    return this.joinQual != null;
+  }
+
+  public EvalNode getJoinQual() {
+    return this.joinQual;
+  }
+
+  public void setJoinColumns(Column [] columns) {
+    this.joinColumns = columns;
+  }
+
+  public boolean hasJoinColumns() {
+    return this.joinColumns != null;
+  }
+
+  public Column [] getJoinColumns() {
+    return this.joinColumns;
+  }
+
+
+  public String toString() {
+    Gson gson = new GsonBuilder().setPrettyPrinting().create();
+    return gson.toJson(this);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/JoinType.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/JoinType.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/JoinType.java
deleted file mode 100644
index 2fbdab1..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/JoinType.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/**
- * 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.engine.planner;
-
-public enum JoinType {
-  CROSS_JOIN,
-  INNER,
-	LEFT_OUTER,
-	RIGHT_OUTER,
-	FULL_OUTER,
-  UNION
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalOptimizer.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalOptimizer.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalOptimizer.java
index d62ec69..3cb83ad 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalOptimizer.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/planner/LogicalOptimizer.java
@@ -16,9 +16,6 @@
  * limitations under the License.
  */
 
-/**
- * 
- */
 package org.apache.tajo.engine.planner;
 
 import com.google.common.base.Preconditions;
@@ -26,129 +23,59 @@ import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.hadoop.fs.Path;
+import org.apache.tajo.algebra.JoinType;
 import org.apache.tajo.catalog.Column;
 import org.apache.tajo.catalog.Schema;
 import org.apache.tajo.catalog.SortSpec;
-import org.apache.tajo.catalog.TableMeta;
 import org.apache.tajo.engine.eval.EvalNode;
 import org.apache.tajo.engine.eval.EvalTreeUtil;
-import org.apache.tajo.engine.eval.FieldEval;
-import org.apache.tajo.engine.parser.CreateTableStmt;
-import org.apache.tajo.engine.parser.ParseTree;
-import org.apache.tajo.engine.parser.QueryBlock;
-import org.apache.tajo.engine.parser.QueryBlock.Target;
 import org.apache.tajo.engine.planner.logical.*;
 import org.apache.tajo.engine.query.exception.InvalidQueryException;
 import org.apache.tajo.engine.utils.SchemaUtil;
-import org.apache.tajo.storage.StorageManager;
-import org.apache.tajo.util.IndexUtil;
 
-import java.io.IOException;
 import java.util.*;
 
 /**
- * This class optimizes a logical plan corresponding to one query block.
+ * This class optimizes a logical plan.
  */
 public class LogicalOptimizer {
   private static Log LOG = LogFactory.getLog(LogicalOptimizer.class);
-  
+
   private LogicalOptimizer() {
   }
 
-  public static LogicalNode optimize(PlanningContext context, LogicalNode plan) {
-    LogicalNode toBeOptimized;
+  public static class OptimizationContext {
+    TargetListManager targetListManager;
 
-    try {
-      toBeOptimized = (LogicalNode) plan.clone();
-    } catch (CloneNotSupportedException e) {
-      LOG.error(e);
-      throw new InvalidQueryException("Cannot clone: " + plan);
+    public OptimizationContext(LogicalPlan plan, String blockName) {
+      this.targetListManager = new TargetListManager(plan, blockName);
     }
 
-    switch (context.getParseTree().getType()) {
-      case SELECT:
-        //case UNION: // TODO - to be implemented
-        //case EXCEPT:
-        //case INTERSECT:
-      case CREATE_TABLE_AS:
-        // if there are selection node
-        if(PlannerUtil.findTopNode(plan, ExprType.SELECTION) != null) {
-          pushSelection(context, toBeOptimized);
-        }
-
-        try {
-          pushProjection(context, toBeOptimized);
-        } catch (CloneNotSupportedException e) {
-          throw new InvalidQueryException(e);
-        }
-
-        break;
-      default:
+    public OptimizationContext(LogicalPlan plan, Target [] targets) {
+      this.targetListManager = new TargetListManager(plan, targets);
     }
 
-    return toBeOptimized;
-  }
-  
-  public static LogicalNode pushIndex(LogicalNode plan , StorageManager sm) throws IOException {
-    if(PlannerUtil.findTopNode(plan, ExprType.SCAN) == null) {
-      return plan;
-    }
-    LogicalNode toBeOptimized;
-    try {
-      toBeOptimized = (LogicalNode) plan.clone();
-    } catch (CloneNotSupportedException e) {
-      LOG.error(e);
-      throw new InvalidQueryException("Cannot clone: " + plan);
+    public TargetListManager getTargetListManager() {
+      return this.targetListManager;
     }
-  
-    changeScanToIndexNode(null ,toBeOptimized , sm);
-    return toBeOptimized;
-    
   }
-  private static void changeScanToIndexNode
-    (LogicalNode parent , LogicalNode cur , StorageManager sm ) throws IOException {
-    if( cur instanceof BinaryNode) {
-      changeScanToIndexNode(cur , ((BinaryNode)cur).getOuterNode() , sm);
-      changeScanToIndexNode(cur , ((BinaryNode)cur).getInnerNode() , sm);
+
+  public static LogicalNode optimize(LogicalPlan plan) throws CloneNotSupportedException {
+    LogicalNode toBeOptimized;
+
+    toBeOptimized = plan.getRootBlock().getRoot();
+
+    if (PlannerUtil.checkIfDDLPlan(toBeOptimized) || !plan.getRootBlock().hasTableExpression()) {
+      LOG.info("This query skips the logical optimization step.");
     } else {
-      switch(cur.getType()) {
-      case CREATE_INDEX:
-        return;
-      case SCAN:
-        ScanNode scan = (ScanNode)cur;
-        EvalNode qual = scan.getQual();
-        if(qual == null) { 
-          return;
-        }else {
-          String tableName = scan.getTableId();
-          Path path = new Path(sm.getTablePath(tableName), "index");
-         
-          if(sm.getFileSystem().exists(path)) {
-            
-            TableMeta meta = sm.getTableMeta(path);       
-            IndexScanNode node;
-            if ((node = IndexUtil.indexEval(scan, meta.getOptions())) == null) {
-              return;
-            }
-            if( parent instanceof BinaryNode ) {
-              if (scan.equals(((BinaryNode)parent).getOuterNode())) {
-                ((BinaryNode)parent).setOuter(node);
-              } else {
-                ((BinaryNode)parent).setInner(node);
-               }
-            } else {
-              ((UnaryNode)parent).setSubNode(node);
-            }
-          }
-          return;
-        }
-      default:
-        changeScanToIndexNode(cur , ((UnaryNode)cur).getSubNode() , sm);
-        break;
+      if(PlannerUtil.findTopNode(toBeOptimized, ExprType.SELECTION) != null) {
+            pushSelection(toBeOptimized);
       }
+      pushProjection(plan);
     }
+    return toBeOptimized;
   }
+
   /**
    * This method pushes down the selection into the appropriate sub 
    * logical operators.
@@ -166,11 +93,10 @@ public class LogicalOptimizer {
    * multiple relations are actually join conditions, and other expression 
    * on single relation can be used in a scan operator or an Index Scan 
    * operator.   
-   * 
-   * @param ctx
+   *
    * @param plan
    */
-  private static void pushSelection(PlanningContext ctx, LogicalNode plan) {
+  private static void pushSelection(LogicalNode plan) {
     SelectionNode selNode = (SelectionNode) PlannerUtil.findTopNode(plan,
         ExprType.SELECTION);
     Preconditions.checkNotNull(selNode);
@@ -184,7 +110,12 @@ public class LogicalOptimizer {
                                              List<EvalNode> cnf, Stack<LogicalNode> stack) {
     
     switch(plan.getType()) {
-    
+
+      case ROOT:
+        LogicalRootNode rootNode = (LogicalRootNode) plan;
+        pushSelectionRecursive(rootNode.getSubNode(), cnf, stack);
+        break;
+
     case SELECTION:
       SelectionNode selNode = (SelectionNode) plan;
       stack.push(selNode);
@@ -214,7 +145,7 @@ public class LogicalOptimizer {
 
       List<EvalNode> matched = Lists.newArrayList();
       for (EvalNode eval : cnf) {
-        if (canBeEvaluated(eval, plan)) {
+        if (PlannerUtil.canBeEvaluated(eval, plan)) {
           matched.add(eval);
         }
       }
@@ -249,7 +180,7 @@ public class LogicalOptimizer {
     case SCAN:
       matched = Lists.newArrayList();
       for (EvalNode eval : cnf) {
-        if (canBeEvaluated(eval, plan)) {
+        if (PlannerUtil.canBeEvaluated(eval, plan)) {
           matched.add(eval);
         }
       }
@@ -286,457 +217,336 @@ public class LogicalOptimizer {
       break;
     }
   }
-  
-  public static boolean canBeEvaluated(EvalNode eval, LogicalNode node) {
-    Set<Column> columnRefs = EvalTreeUtil.findDistinctRefColumns(eval);
-
-    if (node.getType() == ExprType.JOIN) {
-      JoinNode joinNode = (JoinNode) node;
-      Set<String> tableIds = Sets.newHashSet();
-      // getting distinct table references
-      for (Column col : columnRefs) {
-        if (!tableIds.contains(col.getTableName())) {
-          tableIds.add(col.getTableName());
-        }
-      }
-      
-      // if the references only indicate two relation, the condition can be 
-      // pushed into a join operator.
-      if (tableIds.size() != 2) {
-        return false;
-      }
-      
-      String [] outer = PlannerUtil.getLineage(joinNode.getOuterNode());
-      String [] inner = PlannerUtil.getLineage(joinNode.getInnerNode());
-
-      Set<String> o = Sets.newHashSet(outer);
-      Set<String> i = Sets.newHashSet(inner);
-      if (outer == null || inner == null) {      
-        throw new InvalidQueryException("ERROR: Unexpected logical plan");
-      }
-      Iterator<String> it = tableIds.iterator();
-      if (o.contains(it.next()) && i.contains(it.next())) {
-        return true;
-      }
-      
-      it = tableIds.iterator();
-      if (i.contains(it.next()) && o.contains(it.next())) {
-        return true;
-      }
-      
-      return false;
-    } else {
-      for (Column col : columnRefs) {
-        if (!node.getInSchema().contains(col.getQualifiedName())) {
-          return false;
-        }
-      }
-
-      return true;
-    }
-  }
 
   /**
    * This method pushes down the projection list into the appropriate and
    * below logical operators.
-   * @param context
    * @param plan
    */
-  private static void pushProjection(PlanningContext context,
-                                     LogicalNode plan)
+  private static void pushProjection(LogicalPlan plan)
       throws CloneNotSupportedException {
     Stack<LogicalNode> stack = new Stack<LogicalNode>();
 
-    ParseTree tree = context.getParseTree();
-    QueryBlock block;
-
-    if (tree instanceof QueryBlock) {
-      block = (QueryBlock) context.getParseTree();
-
-    } else if (tree instanceof CreateTableStmt) {
-
-      CreateTableStmt createTableStmt = (CreateTableStmt) tree;
+    OptimizationContext optCtx;
 
-      if (createTableStmt.hasQueryBlock()) {
-        block = createTableStmt.getSelectStmt();
-      } else {
-        return;
-      }
+    if (plan.getRootBlock().getProjection() != null &&
+        plan.getRootBlock().getProjection().isAllProjected()) {
+      optCtx = new OptimizationContext(plan,
+          plan.getRootBlock().getProjectionNode().getTargets());
     } else {
-
-      return;
+      optCtx = new OptimizationContext(plan, LogicalPlan.ROOT_BLOCK);
     }
-    OptimizationContext optCtx = new OptimizationContext(context,
-        block.getTargetList());
 
-    pushProjectionRecursive(context, optCtx, plan, stack, new HashSet<Column>());
+    Collection<Column> finalSchema = plan.getRootBlock().getSchema().getColumns();
+
+    pushProjectionRecursive(plan, optCtx, plan.getRootBlock().getRoot(), stack,
+        new HashSet<Column>(finalSchema));
+
   }
 
   /**
-   * Groupby, Join, and Scan can project necessary columns.
-   * This method has three roles:
-   * 1) collect column reference necessary for sortkeys, join keys, selection conditions, grouping fields,
-   * and having conditions
+   * This method visits all operators recursively and shrink output columns according to only necessary columns
+   * at each operator.
+   *
+   * This method has three steps:
+   * <ol>
+   *   <li></li> collect column references necessary for each operator. For example, sort requires sortkeys,
+   *   join requires join conditions, selection requires filter conditions, and groupby requires grouping keys and having
+   *
    * 2) shrink the output schema of each operator so that the operator reduces the output columns according to
    * the necessary columns of their parent operators
    * 3) shrink the input schema of each operator according to the shrunk output schemas of the child operators.
-   *
-   *
-   * @param ctx
-   * @param node
-   * //@param necessary - columns necessary for above logical nodes, but it excepts the fields for the target list
-   * //@param targetList
-   * @return
    */
   private static LogicalNode pushProjectionRecursive(
-      final PlanningContext ctx, final OptimizationContext optContext,
+      final LogicalPlan plan, final OptimizationContext optContext,
       final LogicalNode node, final Stack<LogicalNode> stack,
-      final Set<Column> necessary) throws CloneNotSupportedException {
+      final Set<Column> upperRequired) throws CloneNotSupportedException {
+
+    LogicalNode currentNode = null;
 
-    LogicalNode outer;
-    LogicalNode inner;
     switch (node.getType()) {
-      case ROOT: // It does not support the projection
-        LogicalRootNode root = (LogicalRootNode) node;
-        stack.add(root);
-        outer = pushProjectionRecursive(ctx, optContext,
-            root.getSubNode(), stack, necessary);
-        root.setInSchema(outer.getOutSchema());
-        root.setOutSchema(outer.getOutSchema());
+      // They need only simple work
+      case ROOT:
+      case STORE:
+      case LIMIT:
+        currentNode = pushDownCommonPost(plan, optContext, (UnaryNode) node, upperRequired, stack);
         break;
 
-      case STORE:
-        StoreTableNode store = (StoreTableNode) node;
-        stack.add(store);
-        outer = pushProjectionRecursive(ctx, optContext,
-            store.getSubNode(), stack, necessary);
-        store.setInSchema(outer.getOutSchema());
-        store.setOutSchema(outer.getOutSchema());
+      // They need special preworks.
+      case SELECTION:
+        currentNode = pushDownSelection(plan, optContext, (SelectionNode) node, upperRequired, stack);
+        break;
+      case SORT:
+        currentNode = pushDownSort(plan, optContext, (SortNode) node, upperRequired, stack);
+        break;
+      case UNION:
+      case EXCEPT:
+      case INTERSECT:
+        currentNode = pushDownSetNode(plan, optContext, (UnionNode) node, upperRequired, stack);
         break;
 
+      // Projection, GroupBy, Join, and Scan are all projectable.
+      // A projectable operator can shrink or expand output columns through alias name and expressions.
       case PROJECTION:
-        ProjectionNode projNode = (ProjectionNode) node;
-
-        stack.add(projNode);
-        outer = pushProjectionRecursive(ctx, optContext,
-            projNode.getSubNode(), stack, necessary);
-        stack.pop();
-
-        LogicalNode childNode = projNode.getSubNode();
-        if (optContext.getTargetListManager().isAllEvaluated() // if all exprs are evaluated
-            && (childNode.getType() == ExprType.JOIN ||
-               childNode.getType() == ExprType.GROUP_BY ||
-               childNode.getType() == ExprType.SCAN)) { // if the child node is projectable
-            projNode.getSubNode().setOutSchema(
-                optContext.getTargetListManager().getUpdatedSchema());
-            LogicalNode parent = stack.peek();
-            ((UnaryNode)parent).setSubNode(projNode.getSubNode());
-            return projNode.getSubNode();
-        } else {
-          // the output schema is not changed.
-          projNode.setInSchema(outer.getOutSchema());
-          projNode.setTargetList(
-              optContext.getTargetListManager().getUpdatedTarget());
-        }
-        return projNode;
+        currentNode = pushDownProjection(plan, optContext, (ProjectionNode) node, upperRequired, stack);
+        break;
+      case GROUP_BY:
+        currentNode = pushDownGroupBy(plan, optContext, (GroupbyNode) node, upperRequired, stack);
+        break;
+      case JOIN:
+        currentNode = pushDownJoin(plan, optContext, (JoinNode) node, upperRequired, stack);
+        break;
+      case SCAN:
+        currentNode = pushdownScanNode(optContext, (ScanNode) node, upperRequired, stack);
+        break;
 
-      case SELECTION: // It does not support the projection
-        SelectionNode selNode = (SelectionNode) node;
+      default:
+    }
 
-        if (selNode.getQual() != null) {
-          necessary.addAll(EvalTreeUtil.findDistinctRefColumns(selNode.getQual()));
-        }
+    return currentNode;
+  }
 
-        stack.add(selNode);
-        outer = pushProjectionRecursive(ctx, optContext, selNode.getSubNode(),
-            stack, necessary);
-        stack.pop();
-        selNode.setInSchema(outer.getOutSchema());
-        selNode.setOutSchema(outer.getOutSchema());
-        break;
+  private static LogicalNode pushDownCommonPost(LogicalPlan plan, OptimizationContext context, UnaryNode node,
+                                                Set<Column> upperRequired, Stack<LogicalNode> stack) throws CloneNotSupportedException {
+    stack.push(node);
+    LogicalNode child = pushProjectionRecursive(plan, context,
+        node.getSubNode(), stack, upperRequired);
+    stack.pop();
+    node.setInSchema(child.getOutSchema());
+    node.setOutSchema(child.getOutSchema());
+
+    if (node instanceof Projectable) {
+      pushDownProjectablePost(context, node, upperRequired, isTopmostProjectable(stack));
+    }
+    return node;
+  }
 
-      case GROUP_BY: {
-        GroupbyNode groupByNode = (GroupbyNode)node;
+  private static LogicalNode pushDownProjection(LogicalPlan plan, OptimizationContext context,
+                                                   ProjectionNode projNode, Set<Column> upperRequired,
+                                                   Stack<LogicalNode> path) throws CloneNotSupportedException {
 
-        if (groupByNode.hasHavingCondition()) {
-          necessary.addAll(EvalTreeUtil.findDistinctRefColumns(groupByNode.getHavingCondition()));
-        }
+    for (Target target : projNode.getTargets()) {
+      upperRequired.add(target.getColumnSchema());
+    }
 
-        stack.add(groupByNode);
-        outer = pushProjectionRecursive(ctx, optContext,
-            groupByNode.getSubNode(), stack, necessary);
-        stack.pop();
-        groupByNode.setInSchema(outer.getOutSchema());
-        // set all targets
-        groupByNode.setTargetList(optContext.getTargetListManager().getUpdatedTarget());
-
-        TargetListManager targets = optContext.getTargetListManager();
-        List<Target> groupbyPushable = Lists.newArrayList();
-        List<Integer> groupbyPushableId = Lists.newArrayList();
-
-        EvalNode expr;
-        for (int i = 0; i < targets.size(); i++) {
-          expr = targets.getTarget(i).getEvalTree();
-          if (canBeEvaluated(expr, groupByNode) &&
-              EvalTreeUtil.findDistinctAggFunction(expr).size() > 0 && expr.getType() != EvalNode.Type.FIELD) {
-            targets.setEvaluated(i);
-            groupbyPushable.add((Target) targets.getTarget(i).clone());
-            groupbyPushableId.add(i);
-          }
-        }
+    path.push(projNode);
+    LogicalNode child = pushProjectionRecursive(plan, context, projNode.getSubNode(), path, upperRequired);
+    path.pop();
 
-        return groupByNode;
-      }
+    LogicalNode childNode = projNode.getSubNode();
 
-      case SORT: // It does not support the projection
-        SortNode sortNode = (SortNode) node;
+    // If all expressions are evaluated in the child operators and the last operator is projectable,
+    // ProjectionNode will not be necessary. It eliminates ProjectionNode.
+    if (context.getTargetListManager().isAllEvaluated() && (childNode instanceof Projectable)) {
+      LogicalNode parent = path.peek();
+      // update the child node's output schemas
+      child.setOutSchema(context.getTargetListManager().getUpdatedSchema());
+      PlannerUtil.deleteNode(parent, projNode);
+      return child;
+    } else {
+      projNode.setInSchema(child.getOutSchema());
+      projNode.setTargets(context.getTargetListManager().getUpdatedTarget());
+      return projNode;
+    }
+  }
 
-        for (SortSpec spec : sortNode.getSortKeys()) {
-          necessary.add(spec.getSortKey());
-        }
+  private static LogicalNode pushDownSelection(LogicalPlan plan, OptimizationContext context,
+                                                 SelectionNode selectionNode, Set<Column> upperRequired,
+                                                 Stack<LogicalNode> path) throws CloneNotSupportedException {
+    if (selectionNode.getQual() != null) {
+      upperRequired.addAll(EvalTreeUtil.findDistinctRefColumns(selectionNode.getQual()));
+    }
 
-        stack.add(sortNode);
-        outer = pushProjectionRecursive(ctx, optContext,
-            sortNode.getSubNode(), stack, necessary);
-        stack.pop();
-        sortNode.setInSchema(outer.getOutSchema());
-        sortNode.setOutSchema(outer.getOutSchema());
-        break;
+    return pushDownCommonPost(plan, context, selectionNode, upperRequired, path);
+  }
 
+  private static GroupbyNode pushDownGroupBy(LogicalPlan plan, OptimizationContext context, GroupbyNode groupbyNode,
+                                             Set<Column> upperRequired, Stack<LogicalNode> stack)
+      throws CloneNotSupportedException {
 
-      case JOIN: {
-        JoinNode joinNode = (JoinNode) node;
-        Set<Column> parentNecessary = Sets.newHashSet(necessary);
+    Set<Column> currentRequired = new HashSet<Column>(upperRequired);
 
-        if (joinNode.hasJoinQual()) {
-          necessary.addAll(EvalTreeUtil.findDistinctRefColumns(joinNode.getJoinQual()));
-        }
+    if (groupbyNode.hasHavingCondition()) {
+      currentRequired.addAll(EvalTreeUtil.findDistinctRefColumns(groupbyNode.getHavingCondition()));
+    }
 
-        stack.add(joinNode);
-        outer = pushProjectionRecursive(ctx, optContext,
-            joinNode.getOuterNode(), stack, necessary);
-        inner = pushProjectionRecursive(ctx, optContext,
-            joinNode.getInnerNode(), stack, necessary);
-        stack.pop();
-        Schema merged = SchemaUtil
-            .merge(outer.getOutSchema(), inner.getOutSchema());
-        joinNode.setInSchema(merged);
-
-        TargetListManager targets = optContext.getTargetListManager();
-        List<Target> joinPushable = Lists.newArrayList();
-        List<Integer> joinPushableId = Lists.newArrayList();
-        EvalNode expr;
-        for (int i = 0; i < targets.size(); i++) {
-          expr = targets.getTarget(i).getEvalTree();
-          if (canBeEvaluated(expr, joinNode)
-              && EvalTreeUtil.findDistinctAggFunction(expr).size() == 0
-              && expr.getType() != EvalNode.Type.FIELD) {
-            targets.setEvaluated(i);
-            joinPushable.add(targets.getTarget(i));
-            joinPushableId.add(i);
-          }
-        }
-        if (joinPushable.size() > 0) {
-          joinNode.setTargetList(targets.targets);
-        }
+    for (Target target : groupbyNode.getTargets()) {
+      currentRequired.addAll(EvalTreeUtil.findDistinctRefColumns(target.getEvalTree()));
+    }
 
-        Schema outSchema = shrinkOutSchema(joinNode.getInSchema(), targets.getUpdatedSchema().getColumns());
-        for (Integer t : joinPushableId) {
-          outSchema.addColumn(targets.getEvaluatedColumn(t));
-        }
-        outSchema = SchemaUtil.mergeAllWithNoDup(outSchema.getColumns(),
-            SchemaUtil.getProjectedSchema(joinNode.getInSchema(),parentNecessary).getColumns());
-        joinNode.setOutSchema(outSchema);
-        break;
-      }
+    pushDownCommonPost(plan, context, groupbyNode, currentRequired, stack);
+    return groupbyNode;
+  }
 
-      case UNION:  // It does not support the projection
-        UnionNode unionNode = (UnionNode) node;
-        stack.add(unionNode);
+  private static SortNode pushDownSort(LogicalPlan plan, OptimizationContext context, SortNode sortNode,
+                                       Set<Column> upperRequired, Stack<LogicalNode> stack)
+      throws CloneNotSupportedException {
 
-        ParseTree tree =  ctx.getParseTree();
-        if (tree instanceof CreateTableStmt) {
-          tree = ((CreateTableStmt) tree).getSelectStmt();
-        }
-        QueryBlock block = (QueryBlock) tree;
-
-        OptimizationContext outerCtx = new OptimizationContext(ctx,
-            block.getTargetList());
-        OptimizationContext innerCtx = new OptimizationContext(ctx,
-            block.getTargetList());
-        pushProjectionRecursive(ctx, outerCtx, unionNode.getOuterNode(),
-            stack, necessary);
-        pushProjectionRecursive(ctx, innerCtx, unionNode.getInnerNode(),
-            stack, necessary);
-        stack.pop();
-
-        // if this is the final union, we assume that all targets are evalauted
-        // TODO - is it always correct?
-        if (stack.peek().getType() != ExprType.UNION) {
-          optContext.getTargetListManager().setEvaluatedAll();
-        }
-        break;
+    for (SortSpec spec : sortNode.getSortKeys()) {
+      upperRequired.add(spec.getSortKey());
+    }
 
-      case SCAN: {
-        ScanNode scanNode = (ScanNode) node;
-        TargetListManager targets = optContext.getTargetListManager();
-        List<Integer> scanPushableId = Lists.newArrayList();
-        List<Target> scanPushable = Lists.newArrayList();
-        EvalNode expr;
-        for (int i = 0; i < targets.size(); i++) {
-          expr = targets.getTarget(i).getEvalTree();
-          if (!targets.isEvaluated(i) && canBeEvaluated(expr, scanNode)) {
-            if (expr.getType() == EvalNode.Type.FIELD) {
-              targets.setEvaluated(i);
-            } else if (EvalTreeUtil.findDistinctAggFunction(expr).size() == 0) {
-              targets.setEvaluated(i);
-              scanPushable.add(targets.getTarget(i));
-              scanPushableId.add(i);
-            }
-          }
-        }
+    pushDownCommonPost(plan, context, sortNode, upperRequired, stack);
 
-        if (scanPushable.size() > 0) {
-          scanNode.setTargets(scanPushable.toArray(new Target[scanPushable.size()]));
-        }
-        Schema outSchema = shrinkOutSchema(scanNode.getInSchema(), targets.getUpdatedSchema().getColumns());
-        for (Integer t : scanPushableId) {
-          outSchema.addColumn(targets.getEvaluatedColumn(t));
-        }
-        outSchema = SchemaUtil.mergeAllWithNoDup(outSchema.getColumns(), SchemaUtil.getProjectedSchema(scanNode.getInSchema(),necessary).getColumns());
-        scanNode.setOutSchema(outSchema);
+    return sortNode;
+  }
 
-        break;
+  private static JoinNode pushDownJoin(LogicalPlan plan, OptimizationContext context, JoinNode joinNode,
+                                       Set<Column> upperRequired, Stack<LogicalNode> path)
+      throws CloneNotSupportedException {
+    Set<Column> currentRequired = Sets.newHashSet(upperRequired);
+
+    if (joinNode.hasTargets()) {
+      EvalNode expr;
+      for (Target target : joinNode.getTargets()) {
+        expr = target.getEvalTree();
+        if (expr.getType() != EvalNode.Type.FIELD) {
+          currentRequired.addAll(EvalTreeUtil.findDistinctRefColumns(target.getEvalTree()));
+        }
       }
+    }
 
-      default:
+    if (joinNode.hasJoinQual()) {
+      currentRequired.addAll(EvalTreeUtil.findDistinctRefColumns(joinNode.getJoinQual()));
     }
 
-    return node;
+    path.push(joinNode);
+    LogicalNode outer = pushProjectionRecursive(plan, context,
+        joinNode.getOuterNode(), path, currentRequired);
+    LogicalNode inner = pushProjectionRecursive(plan, context,
+        joinNode.getInnerNode(), path, currentRequired);
+    path.pop();
+
+    Schema merged = SchemaUtil.merge(outer.getOutSchema(), inner.getOutSchema());
+    joinNode.setInSchema(merged);
+    pushDownProjectablePost(context, joinNode, upperRequired, isTopmostProjectable(path));
+
+    return joinNode;
   }
 
-  private static Schema shrinkOutSchema(Schema inSchema, Collection<Column> necessary) {
-    Schema projected = new Schema();
-    for(Column col : inSchema.getColumns()) {
-      if(necessary.contains(col)) {
-        projected.addColumn(col);
-      }
+  private static BinaryNode pushDownSetNode(LogicalPlan plan, OptimizationContext context, UnionNode node,
+                                            Set<Column> upperRequired, Stack<LogicalNode> stack)
+      throws CloneNotSupportedException {
+    BinaryNode setNode = node;
+
+    LogicalPlan.QueryBlock leftBlock = plan.getBlock(setNode.getOuterNode());
+    OptimizationContext leftCtx = new OptimizationContext(plan,
+        leftBlock.getTargetListManager().getUnEvaluatedTargets());
+    LogicalPlan.QueryBlock rightBlock = plan.getBlock(setNode.getInnerNode());
+    OptimizationContext rightCtx = new OptimizationContext(plan,
+        rightBlock.getTargetListManager().getUnEvaluatedTargets());
+
+    stack.push(setNode);
+    pushProjectionRecursive(plan, leftCtx, setNode.getOuterNode(), stack, upperRequired);
+    pushProjectionRecursive(plan, rightCtx, setNode.getInnerNode(), stack, upperRequired);
+    stack.pop();
+
+    // if this is the final union, we assume that all targets are evalauted.
+    // TODO - is it always correct?
+    if (stack.peek().getType() != ExprType.UNION) {
+      context.getTargetListManager().setEvaluatedAll();
     }
-    return projected;
+
+    return setNode;
   }
 
-  public static class OptimizationContext {
-    PlanningContext context;
-    TargetListManager targetListManager;
+  private static ScanNode pushdownScanNode(OptimizationContext optContext, ScanNode scanNode,
+                                           Set<Column> upperRequired, Stack<LogicalNode> stack)
+      throws CloneNotSupportedException {
+    return (ScanNode) pushDownProjectablePost(optContext, scanNode, upperRequired, isTopmostProjectable(stack));
+  }
 
-    public OptimizationContext(PlanningContext context, Target [] targets) {
-      this.context = context;
-      this.targetListManager = new TargetListManager(context, targets);
+  private static boolean isTopmostProjectable(Stack<LogicalNode> stack) {
+    for (LogicalNode node : stack) {
+      if (node.getType() == ExprType.JOIN || node.getType() == ExprType.GROUP_BY) {
+        return false;
+      }
     }
 
-    public TargetListManager getTargetListManager() {
-      return this.targetListManager;
-    }
+    return true;
   }
 
-  public static class TargetListManager {
-    private PlanningContext context;
-    private boolean [] evaluated;
-    private Target [] targets;
-
-    public TargetListManager(PlanningContext context, Target [] targets) {
-      this.context = context;
-      if (targets == null) {
-        evaluated = new boolean[0];
-      } else {
-        evaluated = new boolean[targets.length];
-      }
-      this.targets = targets;
-    }
+  private static LogicalNode pushDownProjectablePost(OptimizationContext context, LogicalNode node,
+                                                     Set<Column> upperRequired, boolean last)
+      throws CloneNotSupportedException {
+    TargetListManager targetListManager = context.getTargetListManager();
+    EvalNode expr;
 
-    public Target getTarget(int id) {
-      return targets[id];
-    }
+    List<Integer> newEvaluatedTargetIds = new ArrayList<Integer>();
 
-    public Target [] getTargets() {
-      return this.targets;
-    }
+    for (int i = 0; i < targetListManager.size(); i++) {
+      expr = targetListManager.getTarget(i).getEvalTree();
 
-    public int size() {
-      return targets.length;
-    }
+      if (!targetListManager.isEvaluated(i) && PlannerUtil.canBeEvaluated(expr, node)) {
 
-    public void setEvaluated(int id) {
-      evaluated[id] = true;
-    }
+        if (node instanceof ScanNode) { // For ScanNode
 
-    public void setEvaluatedAll() {
-      for (int i = 0; i < evaluated.length; i++) {
-        evaluated[i] = true;
-      }
-    }
+          if (expr.getType() == EvalNode.Type.FIELD && !targetListManager.getTarget(i).hasAlias()) {
+            targetListManager.setEvaluated(i);
+          } else if (EvalTreeUtil.findDistinctAggFunction(expr).size() == 0) {
+            targetListManager.setEvaluated(i);
+            newEvaluatedTargetIds.add(i);
+          }
 
-    private boolean isEvaluated(int id) {
-      return evaluated[id];
-    }
+        } else if (node instanceof GroupbyNode) { // For GroupBy
+          if (EvalTreeUtil.findDistinctAggFunction(expr).size() > 0 && expr.getType() != EvalNode.Type.FIELD) {
+            targetListManager.setEvaluated(i);
+            newEvaluatedTargetIds.add(i);
+          }
 
-    public Target [] getUpdatedTarget() throws CloneNotSupportedException {
-      Target [] updated = new Target[evaluated.length];
-      for (int i = 0; i < evaluated.length; i++) {
-        if (evaluated[i]) {
-          Column col = getEvaluatedColumn(i);
-          updated[i] = new Target(new FieldEval(col), i);
-        } else {
-          updated[i] = (Target) targets[i].clone();
+        } else if (node instanceof JoinNode) {
+          if (expr.getType() != EvalNode.Type.FIELD && EvalTreeUtil.findDistinctAggFunction(expr).size() == 0) {
+            targetListManager.setEvaluated(i);
+            newEvaluatedTargetIds.add(i);
+          }
         }
       }
-      return updated;
     }
 
-    public Schema getUpdatedSchema() {
-      Schema schema = new Schema();
-      for (int i = 0; i < evaluated.length; i++) {
-        if (evaluated[i]) {
-          Column col = getEvaluatedColumn(i);
-          schema.addColumn(col);
-        } else {
-          Collection<Column> cols = getColumnRefs(i);
-          for (Column col : cols) {
-            if (!schema.contains(col.getQualifiedName())) {
-              schema.addColumn(col);
-            }
-          }
-        }
+    Projectable projectable = (Projectable) node;
+    if (last) {
+      Preconditions.checkState(targetListManager.isAllEvaluated(), "Not all targets are evaluated.");
+      projectable.setTargets(targetListManager.getTargets());
+      targetListManager.getUpdatedTarget();
+      node.setOutSchema(targetListManager.getUpdatedSchema());
+    } else {
+    // Preparing targets regardless of that the node has targets or not.
+    // This part is required because some node does not have any targets,
+    // if the node has the same input and output schemas.
+
+    Target [] checkingTargets;
+    if (!projectable.hasTargets()) {
+      Schema outSchema = node.getOutSchema();
+      checkingTargets = new Target[outSchema.getColumnNum() + newEvaluatedTargetIds.size()];
+      PlannerUtil.schemaToTargets(outSchema, checkingTargets);
+      int baseIdx = outSchema.getColumnNum();
+      for (int i = 0; i < newEvaluatedTargetIds.size(); i++) {
+        checkingTargets[baseIdx + i] = targetListManager.getTarget(newEvaluatedTargetIds.get(i));
       }
-
-      return schema;
+    } else {
+      checkingTargets = projectable.getTargets();
     }
 
-    public Collection<Column> getColumnRefs(int id) {
-      return EvalTreeUtil.findDistinctRefColumns(targets[id].getEvalTree());
-    }
+    List<Target> projectedTargets = new ArrayList<Target>();
+    for (Column column : upperRequired) {
+      for (Target target : checkingTargets) {
 
-    public Column getEvaluatedColumn(int id) {
-      Target t = targets[id];
-      String name;
-      if (t.hasAlias()) {
-        name = t.getAlias();
-      } else if (t.getEvalTree().getName().equals("?")) {
-        name = context.getGeneratedColumnName();
-      } else {
-        name = t.getEvalTree().getName();
-      }
-      return new Column(name, t.getEvalTree().getValueType()[0]);
-    }
+        if (target.hasAlias() && target.getAlias().equalsIgnoreCase(column.getQualifiedName())) {
+            projectedTargets.add(target);
+        } else {
 
-    public boolean isAllEvaluated() {
-      for (boolean isEval : evaluated) {
-        if (!isEval) {
-          return false;
+          if (target.getColumnSchema().equals(column)) {
+            projectedTargets.add(target);
+          }
         }
       }
+    }
 
-      return true;
+    projectable.setTargets(projectedTargets.toArray(new Target[projectedTargets.size()]));
+    targetListManager.getUpdatedTarget();
+    node.setOutSchema(PlannerUtil.targetToSchema(projectable.getTargets()));
     }
+
+    return node;
   }
 }
\ No newline at end of file


[05/13] TAJO-87: Integration of tajo algebra module and SQL parser

Posted by hy...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestLogicalPlanner.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestLogicalPlanner.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestLogicalPlanner.java
index 45253ad..f66eabb 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestLogicalPlanner.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestLogicalPlanner.java
@@ -22,23 +22,24 @@ import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import com.google.gson.Gson;
 import org.apache.hadoop.fs.Path;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
 import org.apache.tajo.TajoTestingCluster;
+import org.apache.tajo.algebra.Expr;
+import org.apache.tajo.algebra.JoinType;
 import org.apache.tajo.benchmark.TPCH;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.FunctionType;
-import org.apache.tajo.catalog.proto.CatalogProtos.IndexMethod;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
 import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.engine.eval.EvalNode;
 import org.apache.tajo.engine.function.builtin.SumInt;
 import org.apache.tajo.engine.json.GsonCreator;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
 import org.apache.tajo.engine.planner.logical.*;
 import org.apache.tajo.master.TajoMaster;
 import org.apache.tajo.util.FileUtil;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
 import java.io.File;
 import java.io.IOException;
@@ -50,7 +51,7 @@ import static org.junit.Assert.assertTrue;
 public class TestLogicalPlanner {
   private static TajoTestingCluster util;
   private static CatalogService catalog;
-  private static QueryAnalyzer analyzer;
+  private static SQLAnalyzer sqlAnalyzer;
   private static LogicalPlanner planner;
   private static TPCH tpch;
 
@@ -65,15 +66,15 @@ public class TestLogicalPlanner {
     
     Schema schema = new Schema();
     schema.addColumn("name", Type.TEXT);
-    schema.addColumn("empId", Type.INT4);
-    schema.addColumn("deptName", Type.TEXT);
+    schema.addColumn("empid", Type.INT4);
+    schema.addColumn("deptname", Type.TEXT);
 
     Schema schema2 = new Schema();
-    schema2.addColumn("deptName", Type.TEXT);
+    schema2.addColumn("deptname", Type.TEXT);
     schema2.addColumn("manager", Type.TEXT);
 
     Schema schema3 = new Schema();
-    schema3.addColumn("deptName", Type.TEXT);
+    schema3.addColumn("deptname", Type.TEXT);
     schema3.addColumn("score", Type.INT4);
 
     TableMeta meta = CatalogUtil.newTableMeta(schema, StoreType.CSV);
@@ -110,7 +111,7 @@ public class TestLogicalPlanner {
     }
 
     catalog.registerFunction(funcDesc);
-    analyzer = new QueryAnalyzer(catalog);
+    sqlAnalyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
   }
 
@@ -120,25 +121,26 @@ public class TestLogicalPlanner {
   }
 
   static String[] QUERIES = {
-      "select name, empId, deptName from employee where empId > 500", // 0
-      "select name, empId, e.deptName, manager from employee as e, dept as dp", // 1
-      "select name, empId, e.deptName, manager, score from employee as e, dept, score", // 2
-      "select p.deptName, sumtest(score) from dept as p, score group by p.deptName having sumtest(score) > 30", // 3
-      "select p.deptName, score from dept as p, score order by score asc", // 4
+      "select name, empid, deptname from employee where empId > 500", // 0
+      "select name, empid, e.deptname, manager from employee as e, dept as dp", // 1
+      "select name, empid, e.deptname, manager, score from employee as e, dept, score", // 2
+      "select p.deptname, sumtest(score) from dept as p, score group by p.deptName having sumtest(score) > 30", // 3
+      "select p.deptname, score from dept as p, score order by score asc", // 4
       "select name from employee where empId = 100", // 5
       "select name, score from employee, score", // 6
       "select p.deptName, sumtest(score) from dept as p, score group by p.deptName", // 7
       "create table store1 as select p.deptName, sumtest(score) from dept as p, score group by p.deptName", // 8
       "select deptName, sumtest(score) from score group by deptName having sumtest(score) > 30", // 9
-      "select 7 + 8, 8 * 9, 10 * 10 as mul", // 10
+      "select 7 + 8 as res1, 8 * 9 as res2, 10 * 10 as res3", // 10
       "create index idx_employee on employee using bitmap (name null first, empId desc) with ('fillfactor' = 70)", // 11
       "select name, score from employee, score order by score limit 3" // 12
   };
 
   @Test
   public final void testSingleRelation() throws CloneNotSupportedException {
-    PlanningContext context = analyzer.parse(QUERIES[0]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = sqlAnalyzer.parse(QUERIES[0]);
+    LogicalPlan planNode = planner.createPlan(expr);
+    LogicalNode plan = planNode.getRootBlock().getRoot();
     assertEquals(ExprType.ROOT, plan.getType());
     TestLogicalNode.testCloneLogicalNode(plan);
     LogicalRootNode root = (LogicalRootNode) plan;
@@ -155,12 +157,11 @@ public class TestLogicalPlanner {
   }
 
   public static void assertSchema(Schema expected, Schema schema) {
-    assertEquals(expected.getColumnNum(), schema.getColumnNum());
     Column expectedColumn;
     Column column;
     for (int i = 0; i < expected.getColumnNum(); i++) {
       expectedColumn = expected.getColumn(i);
-      column = schema.getColumn(i);
+      column = schema.getColumnByName(expectedColumn.getColumnName());
       assertEquals(expectedColumn.getColumnName(), column.getColumnName());
       assertEquals(expectedColumn.getDataType(), column.getDataType());
     }
@@ -169,8 +170,9 @@ public class TestLogicalPlanner {
   @Test
   public final void testImplicityJoinPlan() throws CloneNotSupportedException {
     // two relations
-    PlanningContext context = analyzer.parse(QUERIES[1]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = sqlAnalyzer.parse(QUERIES[1]);
+    LogicalPlan planNode = planner.createPlan(expr);
+    LogicalNode plan = planNode.getRootBlock().getRoot();
 
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;
@@ -178,10 +180,14 @@ public class TestLogicalPlanner {
 
     Schema expectedSchema = new Schema();
     expectedSchema.addColumn("name", Type.TEXT);
-    expectedSchema.addColumn("empId", Type.INT4);
-    expectedSchema.addColumn("deptName", Type.TEXT);
+    expectedSchema.addColumn("empid", Type.INT4);
+    expectedSchema.addColumn("deptname", Type.TEXT);
     expectedSchema.addColumn("manager", Type.TEXT);
-    assertSchema(expectedSchema, root.getOutSchema());
+    for (int i = 0; i < expectedSchema.getColumnNum(); i++) {
+      Column found = root.getOutSchema().getColumnByName(expectedSchema.getColumn(i).
+          getColumnName());
+      assertEquals(expectedSchema.getColumn(i).getDataType(), found.getDataType());
+    }
 
     assertEquals(ExprType.PROJECTION, root.getSubNode().getType());
     ProjectionNode projNode = (ProjectionNode) root.getSubNode();
@@ -195,13 +201,15 @@ public class TestLogicalPlanner {
     assertEquals(ExprType.SCAN, joinNode.getInnerNode().getType());
     ScanNode rightNode = (ScanNode) joinNode.getInnerNode();
     assertEquals("dept", rightNode.getTableId());
-
-    LogicalNode optimized = LogicalOptimizer.optimize(context, plan);
+    /*
+    LogicalNode optimized = LogicalOptimizer.optimize(expr, plan);
     assertSchema(expectedSchema, optimized.getOutSchema());
+    */
+
 
     // three relations
-    context = analyzer.parse(QUERIES[2]);
-    plan = planner.createPlan(context);
+    expr = sqlAnalyzer.parse(QUERIES[2]);
+    plan = planner.createPlan(expr).getRootBlock().getRoot();
     TestLogicalNode.testCloneLogicalNode(plan);
 
     expectedSchema.addColumn("score", Type.INT4);
@@ -221,10 +229,10 @@ public class TestLogicalPlanner {
     assertEquals(ExprType.SCAN, joinNode.getInnerNode().getType());
     ScanNode scan1 = (ScanNode) joinNode.getInnerNode();
     assertEquals("score", scan1.getTableId());
-    
+
     JoinNode leftNode2 = (JoinNode) joinNode.getOuterNode();
     assertEquals(ExprType.JOIN, leftNode2.getType());
-        
+
     assertEquals(ExprType.SCAN, leftNode2.getOuterNode().getType());
     ScanNode leftScan = (ScanNode) leftNode2.getOuterNode();
     assertEquals("employee", leftScan.getTableId());
@@ -232,12 +240,14 @@ public class TestLogicalPlanner {
     assertEquals(ExprType.SCAN, leftNode2.getInnerNode().getType());
     ScanNode rightScan = (ScanNode) leftNode2.getInnerNode();
     assertEquals("dept", rightScan.getTableId());
-
-    optimized = LogicalOptimizer.optimize(context, plan);
-    assertSchema(expectedSchema, optimized.getOutSchema());
+    /*
+    optimized = LogicalOptimizer.optimize(expr, plan);
+    assertSchema(expectedSchema, optimized.getOutSchema());*/
   }
-  
-  String [] JOINS = { 
+
+
+
+  String [] JOINS = {
       "select name, dept.deptName, score from employee natural join dept natural join score", // 0
       "select name, dept.deptName, score from employee inner join dept on employee.deptName = dept.deptName inner join score on dept.deptName = score.deptName", // 1
       "select name, dept.deptName, score from employee left outer join dept on employee.deptName = dept.deptName right outer join score on dept.deptName = score.deptName" // 2
@@ -250,16 +260,16 @@ public class TestLogicalPlanner {
     expectedJoinSchema.addColumn("deptName", Type.TEXT);
     expectedJoinSchema.addColumn("score", Type.INT4);
   }
-  
+
   @Test
   public final void testNaturalJoinPlan() {
     // two relations
-    PlanningContext context = analyzer.parse(JOINS[0]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr context = sqlAnalyzer.parse(JOINS[0]);
+    LogicalNode plan = planner.createPlan(context).getRootBlock().getRoot();
     assertSchema(expectedJoinSchema, plan.getOutSchema());
 
     assertEquals(ExprType.ROOT, plan.getType());
-    LogicalRootNode root = (LogicalRootNode) plan;    
+    LogicalRootNode root = (LogicalRootNode) plan;
     assertEquals(ExprType.PROJECTION, root.getSubNode().getType());
     ProjectionNode proj = (ProjectionNode) root.getSubNode();
     assertEquals(ExprType.JOIN, proj.getSubNode().getType());
@@ -269,7 +279,7 @@ public class TestLogicalPlanner {
     assertTrue(join.hasJoinQual());
     ScanNode scan = (ScanNode) join.getInnerNode();
     assertEquals("score", scan.getTableId());
-    
+
     assertEquals(ExprType.JOIN, join.getOuterNode().getType());
     join = (JoinNode) join.getOuterNode();
     assertEquals(JoinType.INNER, join.getJoinType());
@@ -280,20 +290,21 @@ public class TestLogicalPlanner {
     ScanNode inner = (ScanNode) join.getInnerNode();
     assertEquals("dept", inner.getTableId());
 
+    /*
     LogicalNode optimized = LogicalOptimizer.optimize(context, plan);
-    System.out.println(optimized);
     assertSchema(expectedJoinSchema, optimized.getOutSchema());
+    */
   }
-  
+
   @Test
   public final void testInnerJoinPlan() {
     // two relations
-    PlanningContext context = analyzer.parse(JOINS[1]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = sqlAnalyzer.parse(JOINS[1]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
     assertSchema(expectedJoinSchema, plan.getOutSchema());
 
     assertEquals(ExprType.ROOT, plan.getType());
-    LogicalRootNode root = (LogicalRootNode) plan;    
+    LogicalRootNode root = (LogicalRootNode) plan;
     assertEquals(ExprType.PROJECTION, root.getSubNode().getType());
     ProjectionNode proj = (ProjectionNode) root.getSubNode();
     assertEquals(ExprType.JOIN, proj.getSubNode().getType());
@@ -302,7 +313,7 @@ public class TestLogicalPlanner {
     assertEquals(ExprType.SCAN, join.getInnerNode().getType());
     ScanNode scan = (ScanNode) join.getInnerNode();
     assertEquals("score", scan.getTableId());
-    
+
     assertEquals(ExprType.JOIN, join.getOuterNode().getType());
     join = (JoinNode) join.getOuterNode();
     assertEquals(JoinType.INNER, join.getJoinType());
@@ -314,20 +325,21 @@ public class TestLogicalPlanner {
     assertEquals("dept", inner.getTableId());
     assertTrue(join.hasJoinQual());
     assertEquals(EvalNode.Type.EQUAL, join.getJoinQual().getType());
-
-    LogicalNode optimized = LogicalOptimizer.optimize(context, plan);
+    /*
+    LogicalNode optimized = LogicalOptimizer.optimize(expr, plan);
     assertSchema(expectedJoinSchema, optimized.getOutSchema());
+    */
   }
-  
+
   @Test
   public final void testOuterJoinPlan() {
     // two relations
-    PlanningContext context = analyzer.parse(JOINS[2]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = sqlAnalyzer.parse(JOINS[2]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
     assertSchema(expectedJoinSchema, plan.getOutSchema());
 
     assertEquals(ExprType.ROOT, plan.getType());
-    LogicalRootNode root = (LogicalRootNode) plan;    
+    LogicalRootNode root = (LogicalRootNode) plan;
     assertEquals(ExprType.PROJECTION, root.getSubNode().getType());
     ProjectionNode proj = (ProjectionNode) root.getSubNode();
     assertEquals(ExprType.JOIN, proj.getSubNode().getType());
@@ -336,7 +348,7 @@ public class TestLogicalPlanner {
     assertEquals(ExprType.SCAN, join.getInnerNode().getType());
     ScanNode scan = (ScanNode) join.getInnerNode();
     assertEquals("score", scan.getTableId());
-    
+
     assertEquals(ExprType.JOIN, join.getOuterNode().getType());
     join = (JoinNode) join.getOuterNode();
     assertEquals(JoinType.LEFT_OUTER, join.getJoinType());
@@ -349,23 +361,26 @@ public class TestLogicalPlanner {
     assertTrue(join.hasJoinQual());
     assertEquals(EvalNode.Type.EQUAL, join.getJoinQual().getType());
 
+    /*
     LogicalNode optimized = LogicalOptimizer.optimize(context, plan);
     assertSchema(expectedJoinSchema, optimized.getOutSchema());
+    */
   }
 
+
   @Test
   public final void testGroupby() throws CloneNotSupportedException {
     // without 'having clause'
-    PlanningContext context = analyzer.parse(QUERIES[7]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr context = sqlAnalyzer.parse(QUERIES[7]);
+    LogicalNode plan = planner.createPlan(context).getRootBlock().getRoot();
 
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;
     testQuery7(root.getSubNode());
-    
+
     // with having clause
-    context = analyzer.parse(QUERIES[3]);
-    plan = planner.createPlan(context);
+    context = sqlAnalyzer.parse(QUERIES[3]);
+    plan = planner.createPlan(context).getRootBlock().getRoot();
     TestLogicalNode.testCloneLogicalNode(plan);
 
     assertEquals(ExprType.ROOT, plan.getType());
@@ -385,22 +400,24 @@ public class TestLogicalPlanner {
     assertEquals(ExprType.SCAN, joinNode.getInnerNode().getType());
     ScanNode rightNode = (ScanNode) joinNode.getInnerNode();
     assertEquals("score", rightNode.getTableId());
-    
-    LogicalOptimizer.optimize(context, plan);
+
+    //LogicalOptimizer.optimize(context, plan);
   }
 
+
   @Test
   public final void testMultipleJoin() throws IOException {
-    PlanningContext context = analyzer.parse(
+    Expr expr = sqlAnalyzer.parse(
         FileUtil.readTextFile(new File("src/test/queries/tpch_q2_simplified.tql")));
-    LogicalNode plan = planner.createPlan(context);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
     Schema expected = tpch.getOutSchema("q2");
     assertSchema(expected, plan.getOutSchema());
-    LogicalNode optimized = LogicalOptimizer.optimize(context, plan);
-    System.out.println(optimized);
-    assertSchema(expected, optimized.getOutSchema());
+//    LogicalNode optimized = LogicalOptimizer.optimize(context, plan);
+//    System.out.println(optimized);
+//    assertSchema(expected, optimized.getOutSchema());
   }
-  
+
+
   static void testQuery7(LogicalNode plan) {
     assertEquals(ExprType.PROJECTION, plan.getType());
     ProjectionNode projNode = (ProjectionNode) plan;
@@ -417,26 +434,27 @@ public class TestLogicalPlanner {
     ScanNode rightNode = (ScanNode) joinNode.getInnerNode();
     assertEquals("score", rightNode.getTableId());
   }
-  
+
+
   @Test
   public final void testStoreTable() throws CloneNotSupportedException {
-    PlanningContext context = analyzer.parse(QUERIES[8]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr context = sqlAnalyzer.parse(QUERIES[8]);
+    LogicalNode plan = planner.createPlan(context).getRootBlock().getRoot();
     TestLogicalNode.testCloneLogicalNode(plan);
-    
+
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;
-    
+
     assertEquals(ExprType.STORE, root.getSubNode().getType());
     StoreTableNode storeNode = (StoreTableNode) root.getSubNode();
     testQuery7(storeNode.getSubNode());
-    LogicalOptimizer.optimize(context, plan);
+    //LogicalOptimizer.optimize(context, plan);
   }
 
   @Test
   public final void testOrderBy() throws CloneNotSupportedException {
-    PlanningContext context = analyzer.parse(QUERIES[4]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = sqlAnalyzer.parse(QUERIES[4]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
     TestLogicalNode.testCloneLogicalNode(plan);
 
     assertEquals(ExprType.ROOT, plan.getType());
@@ -461,60 +479,63 @@ public class TestLogicalPlanner {
 
   @Test
   public final void testLimit() throws CloneNotSupportedException {
-    PlanningContext context = analyzer.parse(QUERIES[12]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = sqlAnalyzer.parse(QUERIES[12]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
     TestLogicalNode.testCloneLogicalNode(plan);
 
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;
 
-    assertEquals(ExprType.LIMIT, root.getSubNode().getType());
-    LimitNode limitNode = (LimitNode) root.getSubNode();
+    assertEquals(ExprType.PROJECTION, root.getSubNode().getType());
+    ProjectionNode projNode = (ProjectionNode) root.getSubNode();
 
-    assertEquals(ExprType.PROJECTION, limitNode.getSubNode().getType());
-    ProjectionNode projNode = (ProjectionNode) limitNode.getSubNode();
+    assertEquals(ExprType.LIMIT, projNode.getSubNode().getType());
+    LimitNode limitNode = (LimitNode) projNode.getSubNode();
 
-    assertEquals(ExprType.SORT, projNode.getSubNode().getType());
+    assertEquals(ExprType.SORT, limitNode.getSubNode().getType());
   }
 
   @Test
   public final void testSPJPush() throws CloneNotSupportedException {
-    PlanningContext context = analyzer.parse(QUERIES[5]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = sqlAnalyzer.parse(QUERIES[5]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
     TestLogicalNode.testCloneLogicalNode(plan);
-    
+
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;
     assertEquals(ExprType.PROJECTION, root.getSubNode().getType());
     ProjectionNode projNode = (ProjectionNode) root.getSubNode();
     assertEquals(ExprType.SELECTION, projNode.getSubNode().getType());
-    SelectionNode selNode = (SelectionNode) projNode.getSubNode();    
+    SelectionNode selNode = (SelectionNode) projNode.getSubNode();
     assertEquals(ExprType.SCAN, selNode.getSubNode().getType());
     ScanNode scanNode = (ScanNode) selNode.getSubNode();
     assertEquals(scanNode.getTableId(), "employee");
-    
-    LogicalNode optimized = LogicalOptimizer.optimize(context, plan);
+
+    /*
+    LogicalNode optimized = LogicalOptimizer.optimize(expr, plan);
     assertEquals(ExprType.ROOT, optimized.getType());
     root = (LogicalRootNode) optimized;
-    
+
     assertEquals(ExprType.SCAN, root.getSubNode().getType());
     scanNode = (ScanNode) root.getSubNode();
-    assertEquals("employee", scanNode.getTableId());
+    assertEquals("employee", scanNode.getTableId());*/
   }
-  
+
+
+
   @Test
   public final void testSPJ() throws CloneNotSupportedException {
-    PlanningContext context = analyzer.parse(QUERIES[6]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = sqlAnalyzer.parse(QUERIES[6]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
     TestLogicalNode.testCloneLogicalNode(plan);
   }
-  
+
   @Test
   public final void testJson() {
-	  PlanningContext context = analyzer.parse(QUERIES[9]);
-	  LogicalNode plan = planner.createPlan(context);
-	  LogicalOptimizer.optimize(context, plan);
-	    
+	  Expr expr = sqlAnalyzer.parse(QUERIES[9]);
+	  LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
+	  //LogicalOptimizer.optimize(context, plan);
+
 	  String json = plan.toJSON();
 	  Gson gson = GsonCreator.getInstance();
 	  LogicalNode fromJson = gson.fromJson(json, LogicalNode.class);
@@ -526,23 +547,23 @@ public class TestLogicalPlanner {
 	  LogicalNode scan = ((GroupbyNode)projNode).getSubNode();
 	  assertEquals(ExprType.SCAN, scan.getType());
   }
-  
+
   @Test
   public final void testVisitor() {
     // two relations
-    PlanningContext context = analyzer.parse(QUERIES[1]);
-    LogicalNode plan = planner.createPlan(context);
-    
+    Expr expr = sqlAnalyzer.parse(QUERIES[1]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
+
     TestVisitor vis = new TestVisitor();
     plan.postOrder(vis);
-    
+
     assertEquals(ExprType.ROOT, vis.stack.pop().getType());
     assertEquals(ExprType.PROJECTION, vis.stack.pop().getType());
     assertEquals(ExprType.JOIN, vis.stack.pop().getType());
     assertEquals(ExprType.SCAN, vis.stack.pop().getType());
     assertEquals(ExprType.SCAN, vis.stack.pop().getType());
   }
-  
+
   private static class TestVisitor implements LogicalNodeVisitor {
     Stack<LogicalNode> stack = new Stack<LogicalNode>();
     @Override
@@ -550,13 +571,14 @@ public class TestLogicalPlanner {
       stack.push(node);
     }
   }
-  
+
+
   @Test
   public final void testExprNode() {
-    PlanningContext context = analyzer.parse(QUERIES[10]);
-    LogicalPlanner planner = new LogicalPlanner(catalog);
-    LogicalNode plan = planner.createPlan(context);
-    LogicalOptimizer.optimize(context, plan);
+    Expr expr = sqlAnalyzer.parse(QUERIES[10]);
+    LogicalPlan planNode = planner.createPlan(expr);
+//    LogicalOptimizer.optimize(context, plan);
+    LogicalNode plan = planNode.getRootBlock().getRoot();
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;
     assertEquals(ExprType.EXPRS, root.getSubNode().getType());
@@ -564,49 +586,26 @@ public class TestLogicalPlanner {
 
     Iterator<Column> it = out.getColumns().iterator();
     Column col = it.next();
-    assertEquals("column_1", col.getColumnName());
+    assertEquals("res1", col.getColumnName());
     col = it.next();
-    assertEquals("column_2", col.getColumnName());
+    assertEquals("res2", col.getColumnName());
     col = it.next();
-    assertEquals("mul", col.getColumnName());
+    assertEquals("res3", col.getColumnName());
   }
-  
-  @Test
-  public final void testCreateIndex() {
-    PlanningContext context = analyzer.parse(QUERIES[11]);
-    LogicalPlanner planner = new LogicalPlanner(catalog);
-    LogicalNode plan = planner.createPlan(context);
-    LogicalOptimizer.optimize(context, plan);
-    LogicalRootNode root = (LogicalRootNode) plan;
 
-    assertEquals(ExprType.CREATE_INDEX, root.getSubNode().getType());
-    IndexWriteNode indexNode = (IndexWriteNode) root.getSubNode();
-    assertEquals("idx_employee", indexNode.getIndexName());
-    assertEquals("employee", indexNode.getTableName());
-    assertEquals(false, indexNode.isUnique());
-    assertEquals(2, indexNode.getSortSpecs().length);
-    assertEquals("name", indexNode.getSortSpecs()[0].getSortKey().getColumnName());
-    assertEquals(Type.TEXT, indexNode.getSortSpecs()[0].getSortKey().getDataType().getType());
-    assertEquals(true, indexNode.getSortSpecs()[0].isNullFirst());
-    assertEquals("empid", indexNode.getSortSpecs()[1].getSortKey().getColumnName());
-    assertEquals(Type.INT4, indexNode.getSortSpecs()[1].getSortKey().getDataType().getType());
-    assertEquals(false, indexNode.getSortSpecs()[1].isAscending());
-    assertEquals(false, indexNode.getSortSpecs()[1].isNullFirst());
-    assertEquals(IndexMethod.BITMAP, indexNode.getMethod());
-  }
-  
   static final String ALIAS [] = {
     "select deptName, sum(score) as total from score group by deptName",
     "select em.empId as id, sum(score) as total from employee as em inner join score using (em.deptName)"
   };
-  
+
+
   @Test
-  public final void testAlias() {
-    PlanningContext context = analyzer.parse(ALIAS[0]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+  public final void testAlias1() {
+    Expr expr = sqlAnalyzer.parse(ALIAS[0]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
+//    plan = LogicalOptimizer.optimize(expr, plan);
     LogicalRootNode root = (LogicalRootNode) plan;
-        
+
     Schema finalSchema = root.getOutSchema();
     Iterator<Column> it = finalSchema.getColumns().iterator();
     Column col = it.next();
@@ -614,11 +613,11 @@ public class TestLogicalPlanner {
     col = it.next();
     assertEquals("total", col.getColumnName());
 
-    context = analyzer.parse(ALIAS[1]);
-    plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    expr = sqlAnalyzer.parse(ALIAS[1]);
+    plan = planner.createPlan(expr).getRootBlock().getRoot();
+//    plan = LogicalOptimizer.optimize(expr, plan);
     root = (LogicalRootNode) plan;
-    
+
     finalSchema = root.getOutSchema();
     it = finalSchema.getColumns().iterator();
     col = it.next();
@@ -626,20 +625,35 @@ public class TestLogicalPlanner {
     col = it.next();
     assertEquals("total", col.getColumnName());
   }
-  
+
+  @Test
+  public final void testAlias2() {
+    Expr expr = sqlAnalyzer.parse(ALIAS[1]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
+//    plan = LogicalOptimizer.optimize(expr, plan);
+    LogicalRootNode root = (LogicalRootNode) plan;
+
+    Schema finalSchema = root.getOutSchema();
+    Iterator<Column> it = finalSchema.getColumns().iterator();
+    Column col = it.next();
+    assertEquals("id", col.getColumnName());
+    col = it.next();
+    assertEquals("total", col.getColumnName());
+  }
+
   static final String CREATE_TABLE [] = {
     "create external table table1 (name text, age int, earn bigint, score real) using csv with ('csv.delimiter'='|') location '/tmp/data'"
   };
-  
+
   @Test
   public final void testCreateTableDef() {
-    PlanningContext context = analyzer.parse(CREATE_TABLE[0]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr expr = sqlAnalyzer.parse(CREATE_TABLE[0]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
+//    plan = LogicalOptimizer.optimize(context, plan);
     LogicalRootNode root = (LogicalRootNode) plan;
     assertEquals(ExprType.CREATE_TABLE, root.getSubNode().getType());
     CreateTableNode createTable = (CreateTableNode) root.getSubNode();
-    
+
     Schema def = createTable.getSchema();
     assertEquals("name", def.getColumn(0).getColumnName());
     assertEquals(Type.TEXT, def.getColumn(0).getDataType().getType());
@@ -654,75 +668,75 @@ public class TestLogicalPlanner {
     assertTrue(createTable.hasOptions());
     assertEquals("|", createTable.getOptions().get("csv.delimiter"));
   }
-  
-  private static final List<Set<Column>> testGenerateCuboidsResult 
+
+  private static final List<Set<Column>> testGenerateCuboidsResult
     = Lists.newArrayList();
   private static final int numCubeColumns = 3;
   private static final Column [] testGenerateCuboids = new Column[numCubeColumns];
-  
-  private static final List<Set<Column>> testCubeByResult 
+
+  private static final List<Set<Column>> testCubeByResult
     = Lists.newArrayList();
   private static final Column [] testCubeByCuboids = new Column[2];
   static {
     testGenerateCuboids[0] = new Column("col1", Type.INT4);
     testGenerateCuboids[1] = new Column("col2", Type.INT8);
     testGenerateCuboids[2] = new Column("col3", Type.FLOAT4);
-    
+
     testGenerateCuboidsResult.add(new HashSet<Column>());
     testGenerateCuboidsResult.add(Sets.newHashSet(testGenerateCuboids[0]));
     testGenerateCuboidsResult.add(Sets.newHashSet(testGenerateCuboids[1]));
     testGenerateCuboidsResult.add(Sets.newHashSet(testGenerateCuboids[2]));
-    testGenerateCuboidsResult.add(Sets.newHashSet(testGenerateCuboids[0], 
+    testGenerateCuboidsResult.add(Sets.newHashSet(testGenerateCuboids[0],
         testGenerateCuboids[1]));
-    testGenerateCuboidsResult.add(Sets.newHashSet(testGenerateCuboids[0], 
+    testGenerateCuboidsResult.add(Sets.newHashSet(testGenerateCuboids[0],
         testGenerateCuboids[2]));
-    testGenerateCuboidsResult.add(Sets.newHashSet(testGenerateCuboids[1], 
+    testGenerateCuboidsResult.add(Sets.newHashSet(testGenerateCuboids[1],
         testGenerateCuboids[2]));
-    testGenerateCuboidsResult.add(Sets.newHashSet(testGenerateCuboids[0], 
+    testGenerateCuboidsResult.add(Sets.newHashSet(testGenerateCuboids[0],
         testGenerateCuboids[1], testGenerateCuboids[2]));
-    
+
     testCubeByCuboids[0] = new Column("employee.name", Type.TEXT);
     testCubeByCuboids[1] = new Column("employee.empid", Type.INT4);
     testCubeByResult.add(new HashSet<Column>());
     testCubeByResult.add(Sets.newHashSet(testCubeByCuboids[0]));
     testCubeByResult.add(Sets.newHashSet(testCubeByCuboids[1]));
-    testCubeByResult.add(Sets.newHashSet(testCubeByCuboids[0], 
+    testCubeByResult.add(Sets.newHashSet(testCubeByCuboids[0],
         testCubeByCuboids[1]));
   }
-  
+
   @Test
   public final void testGenerateCuboids() {
     Column [] columns = new Column[3];
-    
+
     columns[0] = new Column("col1", Type.INT4);
     columns[1] = new Column("col2", Type.INT8);
     columns[2] = new Column("col3", Type.FLOAT4);
-    
+
     List<Column[]> cube = LogicalPlanner.generateCuboids(columns);
-    assertEquals(((int)Math.pow(2, numCubeColumns)), cube.size());    
-    
+    assertEquals(((int)Math.pow(2, numCubeColumns)), cube.size());
+
     Set<Set<Column>> cuboids = Sets.newHashSet();
     for (Column [] cols : cube) {
       cuboids.add(Sets.newHashSet(cols));
     }
-    
+
     for (Set<Column> result : testGenerateCuboidsResult) {
       assertTrue(cuboids.contains(result));
     }
   }
-  
+
   static final String CUBE_ROLLUP [] = {
     "select name, empid, sum(score) from employee natural join score group by cube(name, empid)"
   };
-  
+
   @Test
   public final void testCubeBy() {
-    PlanningContext context = analyzer.parse(CUBE_ROLLUP[0]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
-    
+    Expr expr = sqlAnalyzer.parse(CUBE_ROLLUP[0]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
+//    plan = LogicalOptimizer.optimize(expr, plan);
+
     Set<Set<Column>> cuboids = Sets.newHashSet();
-    
+
     LogicalRootNode root = (LogicalRootNode) plan;
     assertEquals(ExprType.PROJECTION, root.getSubNode().getType());
     ProjectionNode projNode = (ProjectionNode) root.getSubNode();
@@ -732,39 +746,38 @@ public class TestLogicalPlanner {
     assertEquals(ExprType.UNION, u0.getInnerNode().getType());
     GroupbyNode grp = (GroupbyNode) u0.getOuterNode();
     cuboids.add(Sets.newHashSet(grp.getGroupingColumns()));
-    
+
     UnionNode u1 = (UnionNode) u0.getInnerNode();
     assertEquals(ExprType.GROUP_BY, u1.getOuterNode().getType());
     assertEquals(ExprType.UNION, u1.getInnerNode().getType());
     grp = (GroupbyNode) u1.getOuterNode();
     cuboids.add(Sets.newHashSet(grp.getGroupingColumns()));
-    
+
     UnionNode u2 = (UnionNode) u1.getInnerNode();
     assertEquals(ExprType.GROUP_BY, u2.getOuterNode().getType());
     grp = (GroupbyNode) u2.getInnerNode();
     cuboids.add(Sets.newHashSet(grp.getGroupingColumns()));
-    assertEquals(ExprType.GROUP_BY, u2.getInnerNode().getType());    
+    assertEquals(ExprType.GROUP_BY, u2.getInnerNode().getType());
     grp = (GroupbyNode) u2.getOuterNode();
     cuboids.add(Sets.newHashSet(grp.getGroupingColumns()));
-    
+
     assertEquals((int)Math.pow(2, 2), cuboids.size());
     for (Set<Column> result : testCubeByResult) {
       assertTrue(cuboids.contains(result));
     }
   }
-  
+
+
   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 union select deptName from score as s1 intersect select deptName from score as s2",
     "select deptName from employee union select deptName from score as s1 except select deptName from score as s2 intersect select deptName from score as s3"
   };
-  
+
   @Test
   public final void testSetPlan() {
-    PlanningContext context = analyzer.parse(setStatements[0]);
-    LogicalPlanner planner = new LogicalPlanner(catalog);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr expr = sqlAnalyzer.parse(setStatements[0]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;
     assertEquals(ExprType.UNION, root.getSubNode().getType());
@@ -775,39 +788,47 @@ public class TestLogicalPlanner {
     assertEquals(ExprType.PROJECTION, union.getInnerNode().getType());
     ProjectionNode projR = (ProjectionNode) union.getInnerNode();
     assertEquals(ExprType.SELECTION, projR.getSubNode().getType());
+  }
 
+  @Test
+  public final void testSetPlan2() {
     // for testing multiple set statements
-    context = analyzer.parse(setStatements[1]);
-    plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr expr = sqlAnalyzer.parse(setStatements[1]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
+//    plan = LogicalOptimizer.optimize(expr, plan);
     assertEquals(ExprType.ROOT, plan.getType());
-    root = (LogicalRootNode) plan;
+    LogicalRootNode root = (LogicalRootNode) plan;
     assertEquals(ExprType.UNION, root.getSubNode().getType());
-    union = (UnionNode) root.getSubNode();
+    UnionNode union = (UnionNode) root.getSubNode();
     assertEquals(ExprType.PROJECTION, union.getOuterNode().getType());
     assertEquals(ExprType.INTERSECT, union.getInnerNode().getType());
     IntersectNode intersect = (IntersectNode) union.getInnerNode();
     assertEquals(ExprType.PROJECTION, intersect.getOuterNode().getType());
     assertEquals(ExprType.PROJECTION, intersect.getInnerNode().getType());
+  }
 
+  @Test
+  public final void testSetPlan3() {
     // for testing multiple set statements
-    context = analyzer.parse(setStatements[2]);
-    plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr expr = sqlAnalyzer.parse(setStatements[2]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
+//    plan = LogicalOptimizer.optimize(expr, plan);
     assertEquals(ExprType.ROOT, plan.getType());
-    root = (LogicalRootNode) plan;
+    LogicalRootNode root = (LogicalRootNode) plan;
     assertEquals(ExprType.EXCEPT, root.getSubNode().getType());
     ExceptNode except = (ExceptNode) root.getSubNode();
     assertEquals(ExprType.UNION, except.getOuterNode().getType());
     assertEquals(ExprType.INTERSECT, except.getInnerNode().getType());
-    union = (UnionNode) except.getOuterNode();
+    UnionNode union = (UnionNode) except.getOuterNode();
     assertEquals(ExprType.PROJECTION, union.getOuterNode().getType());
     assertEquals(ExprType.PROJECTION, union.getInnerNode().getType());
-    intersect = (IntersectNode) except.getInnerNode();
+    IntersectNode intersect = (IntersectNode) except.getInnerNode();
     assertEquals(ExprType.PROJECTION, intersect.getOuterNode().getType());
     assertEquals(ExprType.PROJECTION, intersect.getInnerNode().getType());
   }
 
+
+
   static final String [] setQualifiers = {
     "select name, empid from employee",
     "select distinct name, empid from employee",
@@ -816,25 +837,30 @@ public class TestLogicalPlanner {
 
   @Test
   public void testSetQualifier() {
-    PlanningContext context = analyzer.parse(setQualifiers[0]);
-    LogicalNode plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    Expr context = sqlAnalyzer.parse(setQualifiers[0]);
+    LogicalNode plan = planner.createPlan(context).getRootBlock().getRoot();
+//    plan = LogicalOptimizer.optimize(context, plan);
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;
-    assertEquals(ExprType.SCAN, root.getSubNode().getType());
+    assertEquals(ExprType.PROJECTION, root.getSubNode().getType());
+    ProjectionNode projectionNode = (ProjectionNode) root.getSubNode();
+    assertEquals(ExprType.SCAN, projectionNode.getSubNode().getType());
 
-    context = analyzer.parse(setQualifiers[1]);
-    plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    context = sqlAnalyzer.parse(setQualifiers[1]);
+    plan = planner.createPlan(context).getRootBlock().getRoot();
+//    plan = LogicalOptimizer.optimize(context, plan);
     assertEquals(ExprType.ROOT, plan.getType());
     root = (LogicalRootNode) plan;
-    assertEquals(ExprType.GROUP_BY, root.getSubNode().getType());
+    assertEquals(ExprType.PROJECTION, root.getSubNode().getType());
+    projectionNode = (ProjectionNode) root.getSubNode();
+    assertEquals(ExprType.GROUP_BY, projectionNode.getSubNode().getType());
 
-    context = analyzer.parse(setQualifiers[2]);
-    plan = planner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
-    assertEquals(ExprType.ROOT, plan.getType());
+    context = sqlAnalyzer.parse(setQualifiers[2]);
+    plan = planner.createPlan(context).getRootBlock().getRoot();
+//    plan = LogicalOptimizer.optimize(context, plan);
     root = (LogicalRootNode) plan;
-    assertEquals(ExprType.SCAN, root.getSubNode().getType());
+    assertEquals(ExprType.PROJECTION, root.getSubNode().getType());
+    projectionNode = (ProjectionNode) root.getSubNode();
+    assertEquals(ExprType.SCAN, projectionNode.getSubNode().getType());
   }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestPlannerUtil.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestPlannerUtil.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestPlannerUtil.java
index 3b6793f..43d8f29 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestPlannerUtil.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/TestPlannerUtil.java
@@ -19,10 +19,8 @@
 package org.apache.tajo.engine.planner;
 
 import org.apache.hadoop.fs.Path;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
 import org.apache.tajo.TajoTestingCluster;
+import org.apache.tajo.algebra.Expr;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.FunctionType;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
@@ -33,11 +31,14 @@ import org.apache.tajo.engine.eval.ConstEval;
 import org.apache.tajo.engine.eval.EvalNode;
 import org.apache.tajo.engine.eval.FieldEval;
 import org.apache.tajo.engine.function.builtin.SumInt;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
 import org.apache.tajo.engine.planner.logical.*;
 import org.apache.tajo.storage.Tuple;
 import org.apache.tajo.storage.TupleComparator;
 import org.apache.tajo.storage.VTuple;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
 import java.util.List;
 
@@ -46,7 +47,7 @@ import static org.junit.Assert.*;
 public class TestPlannerUtil {
   private static TajoTestingCluster util;
   private static CatalogService catalog;
-  private static QueryAnalyzer analyzer;
+  private static SQLAnalyzer analyzer;
   private static LogicalPlanner planner;
 
   @BeforeClass
@@ -88,7 +89,7 @@ public class TestPlannerUtil {
         CatalogUtil.newDataTypesWithoutLen(Type.INT4));
 
     catalog.registerFunction(funcDesc);
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
   }
 
@@ -100,8 +101,8 @@ public class TestPlannerUtil {
   @Test
   public final void testTransformTwoPhase() {
     // without 'having clause'
-    PlanningContext context = analyzer.parse(TestLogicalPlanner.QUERIES[7]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = analyzer.parse(TestLogicalPlanner.QUERIES[7]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
 
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;
@@ -114,8 +115,8 @@ public class TestPlannerUtil {
   
   @Test
   public final void testTrasformTwoPhaseWithStore() {
-    PlanningContext context = analyzer.parse(TestLogicalPlanner.QUERIES[9]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = analyzer.parse(TestLogicalPlanner.QUERIES[9]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
     
     assertEquals(ExprType.ROOT, plan.getType());
     UnaryNode unary = (UnaryNode) plan;
@@ -145,8 +146,8 @@ public class TestPlannerUtil {
   @Test
   public final void testFindTopNode() throws CloneNotSupportedException {
     // two relations
-    PlanningContext context = analyzer.parse(TestLogicalPlanner.QUERIES[1]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = analyzer.parse(TestLogicalPlanner.QUERIES[1]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
 
     assertEquals(ExprType.ROOT, plan.getType());
     LogicalRootNode root = (LogicalRootNode) plan;

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/global/TestGlobalQueryOptimizer.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/global/TestGlobalQueryOptimizer.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/global/TestGlobalQueryOptimizer.java
index 0418221..0c6b1e7 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/global/TestGlobalQueryOptimizer.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/global/TestGlobalQueryOptimizer.java
@@ -24,12 +24,10 @@ package org.apache.tajo.engine.planner.global;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.yarn.event.AsyncDispatcher;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
 import org.apache.tajo.QueryId;
 import org.apache.tajo.QueryIdFactory;
 import org.apache.tajo.TajoTestingCluster;
+import org.apache.tajo.algebra.Expr;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.FunctionType;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
@@ -38,14 +36,17 @@ import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.DatumFactory;
 import org.apache.tajo.engine.eval.TestEvalTree.TestSum;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
 import org.apache.tajo.engine.planner.LogicalOptimizer;
+import org.apache.tajo.engine.planner.LogicalPlan;
 import org.apache.tajo.engine.planner.LogicalPlanner;
-import org.apache.tajo.engine.planner.PlanningContext;
 import org.apache.tajo.engine.planner.logical.*;
 import org.apache.tajo.master.ExecutionBlock;
 import org.apache.tajo.master.GlobalPlanner;
 import org.apache.tajo.storage.*;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
 import java.io.IOException;
 
@@ -58,7 +59,7 @@ public class TestGlobalQueryOptimizer {
   private static CatalogService catalog;
   private static GlobalPlanner planner;
   private static Schema schema;
-  private static QueryAnalyzer analyzer;
+  private static SQLAnalyzer analyzer;
   private static LogicalPlanner logicalPlanner;
   private static QueryId queryId;
   private static GlobalOptimizer optimizer;
@@ -90,7 +91,7 @@ public class TestGlobalQueryOptimizer {
 
     planner = new GlobalPlanner(conf, catalog, new StorageManager(conf),
         dispatcher.getEventHandler());
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     logicalPlanner = new LogicalPlanner(catalog);
 
     int tbNum = 2;
@@ -135,14 +136,13 @@ public class TestGlobalQueryOptimizer {
   }
 
   @Test
-  public void testReduceLogicalQueryUnitSteps() throws IOException {
-    PlanningContext context = analyzer.parse(
+  public void testReduceLogicalQueryUnitSteps() throws IOException, CloneNotSupportedException {
+    Expr expr = analyzer.parse(
         "select table0.age,table0.salary,table1.salary from table0,table1 where table0.salary = table1.salary order by table0.age");
-    LogicalNode plan = logicalPlanner.createPlan(context);
-    plan = LogicalOptimizer.optimize(context, plan);
+    LogicalPlan plan = logicalPlanner.createPlan(expr);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
-    MasterPlan globalPlan = planner.build(queryId,
-        (LogicalRootNode) plan);
+    MasterPlan globalPlan = planner.build(queryId, (LogicalRootNode) rootNode);
     globalPlan = optimizer.optimize(globalPlan);
     
     ExecutionBlock unit = globalPlan.getRoot();

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestBNLJoinExec.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestBNLJoinExec.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestBNLJoinExec.java
index c04f4ba..f144ad3 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestBNLJoinExec.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestBNLJoinExec.java
@@ -19,6 +19,9 @@
 package org.apache.tajo.engine.planner.physical;
 
 import org.apache.hadoop.fs.Path;
+import org.apache.tajo.algebra.Expr;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
+import org.apache.tajo.engine.planner.*;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -30,11 +33,6 @@ import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.DatumFactory;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
-import org.apache.tajo.engine.planner.LogicalPlanner;
-import org.apache.tajo.engine.planner.PhysicalPlanner;
-import org.apache.tajo.engine.planner.PhysicalPlannerImpl;
-import org.apache.tajo.engine.planner.PlanningContext;
 import org.apache.tajo.engine.planner.logical.JoinNode;
 import org.apache.tajo.engine.planner.logical.LogicalNode;
 import org.apache.tajo.storage.*;
@@ -51,7 +49,7 @@ public class TestBNLJoinExec {
   private final String TEST_PATH = "target/test-data/TestNLJoinExec";
   private TajoTestingCluster util;
   private CatalogService catalog;
-  private QueryAnalyzer analyzer;
+  private SQLAnalyzer analyzer;
   private LogicalPlanner planner;
   private StorageManager sm;
   private Path testDir;
@@ -114,7 +112,7 @@ public class TestBNLJoinExec {
 
     people = CatalogUtil.newTableDesc("people", peopleMeta, peoplePath);
     catalog.addTable(people);
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
   }
 
@@ -140,21 +138,19 @@ public class TestBNLJoinExec {
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testCrossJoin");
     TaskAttemptContext ctx = new TaskAttemptContext(conf,
         TUtil.newQueryUnitAttemptId(), merged, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[0]);
-    LogicalNode plan = planner.createPlan(context);
-    //LogicalOptimizer.optimize(ctx, plan);
+    Expr expr = analyzer.parse(QUERIES[0]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
     PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
 
-    /*ProjectionExec proj = (ProjectionExec) exec;
+    ProjectionExec proj = (ProjectionExec) exec;
     NLJoinExec nlJoin = (NLJoinExec) proj.getChild();
-    SeqScanExec scanOuter = (SeqScanExec) nlJoin.getOuter();
-    SeqScanExec scanInner = (SeqScanExec) nlJoin.getInner();
+    SeqScanExec scanOuter = (SeqScanExec) nlJoin.getOuterChild();
+    SeqScanExec scanInner = (SeqScanExec) nlJoin.getInnerChild();
 
-    BNLJoinExec bnl = new BNLJoinExec(ctx, nlJoin.getJoinNode(), scanOuter,
-        scanInner);
-    proj.setsubOp(bnl);*/
+    BNLJoinExec bnl = new BNLJoinExec(ctx, nlJoin.getPlan(), scanOuter, scanInner);
+    proj.setChild(bnl);
 
     int i = 0;
     exec.init();
@@ -178,8 +174,8 @@ public class TestBNLJoinExec {
     TaskAttemptContext ctx =
         new TaskAttemptContext(conf, TUtil.newQueryUnitAttemptId(),
             merged, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[1]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr context = analyzer.parse(QUERIES[1]);
+    LogicalNode plan = planner.createPlan(context).getRootBlock().getRoot();
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
     PhysicalExec exec = phyPlanner.createPlan(ctx, plan);

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestBSTIndexExec.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestBSTIndexExec.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestBSTIndexExec.java
index 6b9f16c..0160c11 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestBSTIndexExec.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestBSTIndexExec.java
@@ -21,6 +21,9 @@ package org.apache.tajo.engine.planner.physical;
 import com.google.common.base.Preconditions;
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
+import org.apache.tajo.algebra.Expr;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
+import org.apache.tajo.engine.planner.*;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -32,11 +35,6 @@ import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.DatumFactory;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
-import org.apache.tajo.engine.planner.LogicalOptimizer;
-import org.apache.tajo.engine.planner.LogicalPlanner;
-import org.apache.tajo.engine.planner.PhysicalPlannerImpl;
-import org.apache.tajo.engine.planner.PlanningContext;
 import org.apache.tajo.engine.planner.logical.LogicalNode;
 import org.apache.tajo.engine.planner.logical.ScanNode;
 import org.apache.tajo.storage.*;
@@ -55,7 +53,7 @@ public class TestBSTIndexExec {
   private TajoConf conf;
   private Path idxPath;
   private CatalogService catalog;
-  private QueryAnalyzer analyzer;
+  private SQLAnalyzer analyzer;
   private LogicalPlanner planner;
   private StorageManager sm;
   private Schema idxSchema;
@@ -136,7 +134,7 @@ public class TestBSTIndexExec {
         sm.getTablePath("employee"));
     catalog.addTable(desc);
 
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
   }
 
@@ -155,13 +153,12 @@ public class TestBSTIndexExec {
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testEqual");
     TaskAttemptContext ctx = new TaskAttemptContext(conf,
         TUtil.newQueryUnitAttemptId(), new Fragment[] { frags[0] }, workDir);
-    PlanningContext context = analyzer.parse(QUERY);
-    LogicalNode plan = planner.createPlan(context);
-
-    plan =  LogicalOptimizer.optimize(context, plan);
+    Expr expr = analyzer.parse(QUERY);
+    LogicalPlan plan = planner.createPlan(expr);
+    LogicalNode rootNode = LogicalOptimizer.optimize(plan);
 
     TmpPlanner phyPlanner = new TmpPlanner(conf, sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
 
     int tupleCount = this.randomValues.get(rndKey);
     int counter = 0;

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestExternalSortExec.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestExternalSortExec.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestExternalSortExec.java
index 85d9380..ada850b 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestExternalSortExec.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestExternalSortExec.java
@@ -19,26 +19,24 @@
 package org.apache.tajo.engine.planner.physical;
 
 import org.apache.hadoop.fs.Path;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
 import org.apache.tajo.TajoTestingCluster;
 import org.apache.tajo.TaskAttemptContext;
+import org.apache.tajo.algebra.Expr;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
 import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.DatumFactory;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
-import org.apache.tajo.engine.planner.LogicalPlanner;
-import org.apache.tajo.engine.planner.PhysicalPlanner;
-import org.apache.tajo.engine.planner.PhysicalPlannerImpl;
-import org.apache.tajo.engine.planner.PlanningContext;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
+import org.apache.tajo.engine.planner.*;
 import org.apache.tajo.engine.planner.logical.LogicalNode;
 import org.apache.tajo.storage.*;
 import org.apache.tajo.util.CommonTestingUtil;
 import org.apache.tajo.util.TUtil;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
 
 import java.io.IOException;
 import java.util.Random;
@@ -51,7 +49,7 @@ public class TestExternalSortExec {
   private TajoTestingCluster util;
   private final String TEST_PATH = "target/test-data/TestExternalSortExec";
   private CatalogService catalog;
-  private QueryAnalyzer analyzer;
+  private SQLAnalyzer analyzer;
   private LogicalPlanner planner;
   private StorageManager sm;
   private Path testDir;
@@ -95,7 +93,7 @@ public class TestExternalSortExec {
 
     employee = new TableDescImpl("employee", employeeMeta, employeePath);
     catalog.addTable(employee);
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
   }
 
@@ -115,11 +113,12 @@ public class TestExternalSortExec {
     Path workDir = new Path(testDir, TestExternalSortExec.class.getName());
     TaskAttemptContext ctx = new TaskAttemptContext(conf,
         TUtil.newQueryUnitAttemptId(), new Fragment[] { frags[0] }, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[0]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = analyzer.parse(QUERIES[0]);
+    LogicalPlan plan = planner.createPlan(expr);
+    LogicalNode rootNode = plan.getRootBlock().getRoot();
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
-    PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
+    PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
     
     ProjectionExec proj = (ProjectionExec) exec;
 

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestHashJoinExec.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestHashJoinExec.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestHashJoinExec.java
index 65af200..0df67fc 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestHashJoinExec.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestHashJoinExec.java
@@ -19,26 +19,24 @@
 package org.apache.tajo.engine.planner.physical;
 
 import org.apache.hadoop.fs.Path;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
 import org.apache.tajo.TajoTestingCluster;
 import org.apache.tajo.TaskAttemptContext;
+import org.apache.tajo.algebra.Expr;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
 import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.DatumFactory;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
-import org.apache.tajo.engine.planner.LogicalPlanner;
-import org.apache.tajo.engine.planner.PhysicalPlanner;
-import org.apache.tajo.engine.planner.PhysicalPlannerImpl;
-import org.apache.tajo.engine.planner.PlanningContext;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
+import org.apache.tajo.engine.planner.*;
 import org.apache.tajo.engine.planner.logical.LogicalNode;
 import org.apache.tajo.storage.*;
 import org.apache.tajo.util.CommonTestingUtil;
 import org.apache.tajo.util.TUtil;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
 
 import java.io.IOException;
 
@@ -50,7 +48,7 @@ public class TestHashJoinExec {
   private final String TEST_PATH = "target/test-data/TestHashJoinExec";
   private TajoTestingCluster util;
   private CatalogService catalog;
-  private QueryAnalyzer analyzer;
+  private SQLAnalyzer analyzer;
   private LogicalPlanner planner;
   private StorageManager sm;
   private Path testDir;
@@ -114,7 +112,7 @@ public class TestHashJoinExec {
 
     people = CatalogUtil.newTableDesc("people", peopleMeta, peoplePath);
     catalog.addTable(people);
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
   }
 
@@ -140,8 +138,8 @@ public class TestHashJoinExec {
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testInnerJoin");
     TaskAttemptContext ctx = new TaskAttemptContext(conf,
         TUtil.newQueryUnitAttemptId(), merged, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[0]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = analyzer.parse(QUERIES[0]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf, sm);
     PhysicalExec exec = phyPlanner.createPlan(ctx, plan);

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestMergeJoinExec.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestMergeJoinExec.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestMergeJoinExec.java
index 9201838..b85d1fa 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestMergeJoinExec.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestMergeJoinExec.java
@@ -20,27 +20,27 @@ package org.apache.tajo.engine.planner.physical;
 
 import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
 import org.apache.tajo.TajoTestingCluster;
 import org.apache.tajo.TaskAttemptContext;
+import org.apache.tajo.algebra.Expr;
 import org.apache.tajo.catalog.*;
 import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
 import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.DatumFactory;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
 import org.apache.tajo.engine.planner.LogicalPlanner;
 import org.apache.tajo.engine.planner.PhysicalPlanner;
 import org.apache.tajo.engine.planner.PhysicalPlannerImpl;
-import org.apache.tajo.engine.planner.PlanningContext;
 import org.apache.tajo.engine.planner.logical.LogicalNode;
 import org.apache.tajo.engine.planner.logical.SortNode;
 import org.apache.tajo.storage.*;
 import org.apache.tajo.util.CommonTestingUtil;
 import org.apache.tajo.util.TUtil;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
 
 import java.io.IOException;
 
@@ -52,7 +52,7 @@ public class TestMergeJoinExec {
   private final String TEST_PATH = "target/test-data/TestMergeJoinExec";
   private TajoTestingCluster util;
   private CatalogService catalog;
-  private QueryAnalyzer analyzer;
+  private SQLAnalyzer analyzer;
   private LogicalPlanner planner;
   private StorageManager sm;
 
@@ -130,7 +130,7 @@ public class TestMergeJoinExec {
 
     people = CatalogUtil.newTableDesc("people", peopleMeta, peoplePath);
     catalog.addTable(people);
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
   }
 
@@ -156,8 +156,8 @@ public class TestMergeJoinExec {
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testInnerJoin");
     TaskAttemptContext ctx = new TaskAttemptContext(conf,
         TUtil.newQueryUnitAttemptId(), merged, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[0]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr expr = analyzer.parse(QUERIES[0]);
+    LogicalNode plan = planner.createPlan(expr).getRootBlock().getRoot();
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
     PhysicalExec exec = phyPlanner.createPlan(ctx, plan);

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestNLJoinExec.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestNLJoinExec.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestNLJoinExec.java
index dde6aa5..c66b0ca 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestNLJoinExec.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/planner/physical/TestNLJoinExec.java
@@ -19,6 +19,9 @@
 package org.apache.tajo.engine.planner.physical;
 
 import org.apache.hadoop.fs.Path;
+import org.apache.tajo.algebra.Expr;
+import org.apache.tajo.engine.parser.SQLAnalyzer;
+import org.apache.tajo.engine.planner.*;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -30,11 +33,6 @@ import org.apache.tajo.common.TajoDataTypes.Type;
 import org.apache.tajo.conf.TajoConf;
 import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.DatumFactory;
-import org.apache.tajo.engine.parser.QueryAnalyzer;
-import org.apache.tajo.engine.planner.LogicalPlanner;
-import org.apache.tajo.engine.planner.PhysicalPlanner;
-import org.apache.tajo.engine.planner.PhysicalPlannerImpl;
-import org.apache.tajo.engine.planner.PlanningContext;
 import org.apache.tajo.engine.planner.logical.LogicalNode;
 import org.apache.tajo.storage.*;
 import org.apache.tajo.util.CommonTestingUtil;
@@ -50,7 +48,7 @@ public class TestNLJoinExec {
   private final String TEST_PATH = "target/test-data/TestNLJoinExec";
   private TajoTestingCluster util;
   private CatalogService catalog;
-  private QueryAnalyzer analyzer;
+  private SQLAnalyzer analyzer;
   private LogicalPlanner planner;
   private StorageManager sm;
   private Path testDir;
@@ -115,7 +113,7 @@ public class TestNLJoinExec {
     people = CatalogUtil.newTableDesc("people", peopleMeta,
         peoplePath);
     catalog.addTable(people);
-    analyzer = new QueryAnalyzer(catalog);
+    analyzer = new SQLAnalyzer();
     planner = new LogicalPlanner(catalog);
   }
 
@@ -142,9 +140,8 @@ public class TestNLJoinExec {
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testCrossJoin");
     TaskAttemptContext ctx = new TaskAttemptContext(conf,
         TUtil.newQueryUnitAttemptId(), merged, workDir);
-    PlanningContext context = analyzer.parse(QUERIES[0]);
-    LogicalNode plan = planner.createPlan(context);
-    //LogicalOptimizer.optimize(ctx, plan);
+    Expr context = analyzer.parse(QUERIES[0]);
+    LogicalNode plan = planner.createPlan(context).getRootBlock().getRoot();
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf, sm);
     PhysicalExec exec = phyPlanner.createPlan(ctx, plan);
@@ -170,8 +167,8 @@ public class TestNLJoinExec {
     Path workDir = CommonTestingUtil.getTestDir("target/test-data/testInnerJoin");
     TaskAttemptContext ctx = new TaskAttemptContext(conf,
         TUtil.newQueryUnitAttemptId(), merged, workDir);
-    PlanningContext context =  analyzer.parse(QUERIES[1]);
-    LogicalNode plan = planner.createPlan(context);
+    Expr context =  analyzer.parse(QUERIES[1]);
+    LogicalNode plan = planner.createPlan(context).getRootBlock().getRoot();
     //LogicalOptimizer.optimize(ctx, plan);
 
     PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf, sm);


[11/13] TAJO-87: Integration of tajo algebra module and SQL parser

Posted by hy...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CreateTableStmt.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CreateTableStmt.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CreateTableStmt.java
deleted file mode 100644
index e05f22a..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/CreateTableStmt.java
+++ /dev/null
@@ -1,134 +0,0 @@
-/**
- * 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.engine.parser;
-
-import org.apache.hadoop.fs.Path;
-import org.apache.tajo.catalog.Options;
-import org.apache.tajo.catalog.Schema;
-import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
-import org.apache.tajo.engine.planner.PlanningContext;
-import org.apache.tajo.engine.planner.PlanningContextImpl;
-
-public class CreateTableStmt extends ParseTree {
-  private final String tableName;
-  private Schema schema;  
-  private StoreType storeType;
-  private Path path;
-  private QueryBlock selectStmt;
-  private Options options;
-  private boolean external;
-
-  public CreateTableStmt(final PlanningContext context,
-                         final String tableName) {
-    super(context, StatementType.CREATE_TABLE);
-    this.tableName = tableName;
-  }
-
-  public CreateTableStmt(final PlanningContext context,
-                         final String tableName, final Schema schema,
-                         StoreType storeType) {
-    super(context, StatementType.CREATE_TABLE);
-    addTableRef(tableName, tableName);
-    this.tableName = tableName;
-    this.schema = schema;
-    this.storeType = storeType;
-  }
-
-  public CreateTableStmt(final PlanningContextImpl context,
-                         final String tableName, final QueryBlock selectStmt) {
-    super(context, StatementType.CREATE_TABLE_AS);
-    context.setOutputTableName(tableName);
-    addTableRef(tableName, tableName);
-    this.tableName = tableName;
-    this.selectStmt = selectStmt;
-  }
-  
-  public final String getTableName() {
-    return this.tableName;
-  }
-  
-  public final boolean hasQueryBlock() {
-    return this.selectStmt != null;
-  }
-  
-  public final QueryBlock getSelectStmt() {
-    return this.selectStmt;
-  }
-  
-  public final boolean hasDefinition() {
-    return this.schema != null;
-  }
-
-  public boolean hasTableDef() {
-    return this.schema != null;
-  }
-
-  public void setTableDef(Schema schema) {
-    this.schema = schema;
-  }
-  
-  public final Schema getTableDef() {
-    return this.schema;
-  }
-
-  public boolean hasStoreType() {
-    return this.storeType != null;
-  }
-
-  public void setStoreType(StoreType type) {
-    this.storeType = type;
-  }
-  
-  public final StoreType getStoreType() {
-    return this.storeType;
-  }
-  
-  public boolean hasOptions() {
-    return this.options != null;
-  }
-  
-  public void setOptions(Options opt) {
-    this.options = opt;
-  }
-  
-  public Options getOptions() {
-    return this.options;
-  }
-
-
-  public boolean hasPath() {
-    return this.path != null;
-  }
-
-  public void setPath(Path path) {
-    this.path = path;
-  }
-
-  public final Path getPath() {
-    return this.path;
-  }
-
-  public boolean isExternal() {
-    return external;
-  }
-
-  public void setExternal(boolean external) {
-    this.external = external;
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/DropTableStmt.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/DropTableStmt.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/DropTableStmt.java
deleted file mode 100644
index 046c37c..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/DropTableStmt.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/**
- * 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.engine.parser;
-
-import org.apache.tajo.engine.planner.PlanningContext;
-
-public class DropTableStmt extends ParseTree {
-  private final String tableName;
-
-  public DropTableStmt(PlanningContext context, final String tableName) {
-    super(context, StatementType.DROP_TABLE);
-    this.tableName = tableName;
-  }
-
-  public String getTableName() {
-    return tableName;
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/ParseTree.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/ParseTree.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/ParseTree.java
deleted file mode 100644
index 76f4c3a..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/ParseTree.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/**
- * 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.engine.parser;
-
-import org.apache.tajo.engine.parser.QueryBlock.FromTable;
-import org.apache.tajo.engine.planner.PlanningContext;
-
-import java.util.Map.Entry;
-
-public abstract class ParseTree {
-  protected final PlanningContext context;
-  protected final StatementType type;
-  private final TableMap tableMap = new TableMap();
-
-  public ParseTree(final PlanningContext context, final StatementType type) {
-    this.context = context;
-    this.type = type;
-  }
-
-  public StatementType getStatementType() {
-    return this.type;
-  }
-
-  protected void addTableRef(FromTable table) {
-    tableMap.addFromTable(table);
-  }
-
-  protected void addTableRef(String tableName, String alias) {
-    tableMap.addFromTable(tableName, alias);
-  }
-
-  public StatementType getType() {
-    return this.type;
-  }
-
-  public String getTableNameByAlias(String alias) {
-    return tableMap.getTableNameByAlias(alias);
-  }
-
-  public Iterable<String> getAllTableNames() {
-    return tableMap.getAllTableNames();
-  }
-
-  public Iterable<Entry<String, String>> getAliasToNames() {
-    return tableMap.getAliasToNames();
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/ParseUtil.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/ParseUtil.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/ParseUtil.java
deleted file mode 100644
index 5786b15..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/ParseUtil.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/**
- * 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.engine.parser;
-
-import org.antlr.runtime.tree.Tree;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.tajo.engine.eval.EvalNode;
-import org.apache.tajo.engine.eval.InvalidEvalException;
-
-public class ParseUtil {
-  private static final Log LOG = LogFactory.getLog(ParseUtil.class);
-
-  public static boolean isConstant(final Tree tree) {
-    switch (tree.getType()) {
-      case SQLParser.NUMBER:
-      case SQLParser.REAL:
-      case SQLParser.Character_String_Literal:
-        return true;
-      default:
-        return false;
-    }
-  }
-
-  public static EvalNode.Type getTypeByParseCode(int parseCode) {
-    switch(parseCode) {
-      case SQLParser.AND:
-        return EvalNode.Type.AND;
-      case SQLParser.OR:
-        return EvalNode.Type.OR;
-      case SQLParser.LIKE:
-        return EvalNode.Type.LIKE;
-      case SQLParser.EQUAL:
-        return EvalNode.Type.EQUAL;
-      case SQLParser.NOT_EQUAL:
-        return EvalNode.Type.NOT_EQUAL;
-      case SQLParser.LTH:
-        return EvalNode.Type.LTH;
-      case SQLParser.LEQ:
-        return EvalNode.Type.LEQ;
-      case SQLParser.GTH:
-        return EvalNode.Type.GTH;
-      case SQLParser.GEQ:
-        return EvalNode.Type.GEQ;
-      case SQLParser.NOT:
-        return EvalNode.Type.NOT;
-      case SQLParser.PLUS:
-        return EvalNode.Type.PLUS;
-      case SQLParser.MINUS:
-        return EvalNode.Type.MINUS;
-      case SQLParser.MULTIPLY:
-        return EvalNode.Type.MULTIPLY;
-      case SQLParser.DIVIDE:
-        return EvalNode.Type.DIVIDE;
-      case SQLParser.MODULAR:
-        return EvalNode.Type.MODULAR;
-      default: throw new InvalidEvalException("We does not support " + parseCode + " type AST yet");
-    }
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/ParserException.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/ParserException.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/ParserException.java
deleted file mode 100644
index b6890f9..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/ParserException.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/**
- * 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.engine.parser;
-
-public class ParserException extends RuntimeException {
-  private static final long serialVersionUID = -4392842405539056555L;
-
-  /**
-   * 
-   */
-  public ParserException() {
-  }
-
-  /**
-   * @param message
-   */
-  public ParserException(String message) {
-    super(message);
-  }
-
-  /**
-   * @param cause
-   */
-  public ParserException(Throwable cause) {
-    super(cause);
-  }
-
-  /**
-   * @param message
-   * @param cause
-   */
-  public ParserException(String message, Throwable cause) {
-    super(message, cause);
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/QueryAnalyzer.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/QueryAnalyzer.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/QueryAnalyzer.java
deleted file mode 100644
index 3873eb9..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/QueryAnalyzer.java
+++ /dev/null
@@ -1,1347 +0,0 @@
-/**
- * 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.engine.parser;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
-import org.antlr.runtime.ANTLRStringStream;
-import org.antlr.runtime.CommonTokenStream;
-import org.antlr.runtime.RecognitionException;
-import org.antlr.runtime.tree.CommonTree;
-import org.antlr.runtime.tree.Tree;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.hadoop.fs.Path;
-import org.apache.tajo.catalog.*;
-import org.apache.tajo.catalog.exception.NoSuchTableException;
-import org.apache.tajo.catalog.function.AggFunction;
-import org.apache.tajo.catalog.function.GeneralFunction;
-import org.apache.tajo.catalog.proto.CatalogProtos.FunctionType;
-import org.apache.tajo.catalog.proto.CatalogProtos.IndexMethod;
-import org.apache.tajo.catalog.proto.CatalogProtos.StoreType;
-import org.apache.tajo.common.TajoDataTypes.DataType;
-import org.apache.tajo.common.TajoDataTypes.Type;
-import org.apache.tajo.common.exception.NotImplementedException;
-import org.apache.tajo.datum.DatumFactory;
-import org.apache.tajo.engine.eval.*;
-import org.apache.tajo.engine.parser.QueryBlock.*;
-import org.apache.tajo.engine.planner.JoinType;
-import org.apache.tajo.engine.planner.PlanningContext;
-import org.apache.tajo.engine.planner.PlanningContextImpl;
-import org.apache.tajo.engine.query.exception.*;
-import org.apache.tajo.exception.InternalException;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * This class transforms a query statement into a QueryBlock.
- *
- * @see org.apache.tajo.engine.parser.QueryBlock
- */
-public final class QueryAnalyzer {
-  private static final Log LOG = LogFactory.getLog(QueryAnalyzer.class);
-  private final CatalogService catalog;
-
-  public QueryAnalyzer(CatalogService catalog) {
-    this.catalog = catalog;
-  }
-
-  public PlanningContext parse(final String query) {
-
-    CommonTree ast;
-    PlanningContextImpl context;
-    try {
-      ast = parseTree(query);
-      context = new PlanningContextImpl(query);
-      ParseTree parseTree = parseQueryTree(context,ast);
-      context.setParseTree(parseTree);
-    } catch (TQLParseError e) {
-      throw new TQLSyntaxError(query, e);
-    }
-
-    if (LOG.isDebugEnabled()) {
-      LOG.debug("Analyzer: " + ast.toStringTree());
-    }
-
-    return context;
-  }
-
-  private ParseTree parseQueryTree(PlanningContextImpl context,
-                                   final CommonTree ast) {
-    ParseTree parseTree = null;
-
-    switch (getCmdType(ast)) {
-      case SELECT:
-        parseTree = parseSelectStatement(context, ast);
-        break;
-
-      case UNION:
-      case EXCEPT:
-      case INTERSECT:
-        parseTree = parseSetStatement(context, ast);
-        break;
-
-      case CREATE_INDEX:
-        parseTree = parseIndexStatement(context, ast);
-        break;
-
-      case CREATE_TABLE:
-        parseTree = parseCreateStatement(context, ast);
-        break;
-
-      case COPY:
-        parseTree = parseCopyStatement(context, ast);
-        break;
-
-      case DROP_TABLE:
-        parseTree = parseDropTableStatement(context, ast);
-
-      default:
-        break;
-    }
-
-    return parseTree;
-  }
-
-  private CopyStmt parseCopyStatement(final PlanningContextImpl context,
-                                      final CommonTree ast) {
-    CopyStmt stmt = new CopyStmt(context);
-    int idx = 0;
-    stmt.setTableName(ast.getChild(idx++).getText());
-    Path path = new Path(ast.getChild(idx++).getText());
-    stmt.setPath(path);
-    StoreType storeType = CatalogUtil.getStoreType(ast.getChild(idx).getText());
-    stmt.setStoreType(storeType);
-
-    if ((ast.getChildCount() - idx) > 1) {
-      idx++;
-      if (ast.getChild(idx).getType() == SQLParser.PARAMS) {
-        Options options = parseParams((CommonTree) ast.getChild(idx));
-        stmt.setParams(options);
-      }
-    }
-
-    return stmt;
-  }
-
-  private DropTableStmt parseDropTableStatement(final PlanningContext context, final CommonTree ast) {
-    int idx = 0;
-    String tableName = ast.getChild(idx).getText();
-    return new DropTableStmt(context, tableName);
-  }
-
-  /**
-   * t=table ASSIGN select_stmt -> ^(CREATE_TABLE $t select_stmt)
-   * | CREATE TABLE t=table AS select_stmt -> ^(CREATE_TABLE $t select_stmt)
-   *
-   * @param ast
-   * @return
-   */
-  private CreateTableStmt parseCreateStatement(final PlanningContextImpl context,
-                                               final CommonTree ast) {
-    CreateTableStmt stmt;
-
-    int idx = 0;
-    CommonTree node;
-    String tableName = ast.getChild(idx).getText();
-    idx++;
-
-    boolean external = false;
-    Schema tableDef = null;
-    StoreType storeType = null;
-    Options options = null;
-    QueryBlock selectStmt = null;
-    Path location = null;
-    while(idx < ast.getChildCount()) {
-      node = (CommonTree) ast.getChild(idx);
-      switch (node.getType()) {
-        case SQLParser.EXTERNAL:
-          external = true;
-          break;
-
-        case SQLParser.TABLE_DEF:
-          tableDef = parseCreateTableDef(node);
-          break;
-
-        case SQLParser.FORMAT:
-        case SQLParser.USING:
-          storeType = CatalogUtil.getStoreType(node.getChild(0).getText());
-          break;
-
-        case SQLParser.PARAMS:
-          options = parseParams(node);
-          break;
-
-        case SQLParser.AS:
-          selectStmt = parseSelectStatement(context, (CommonTree) node.getChild(0));
-          break;
-
-        case SQLParser.LOCATION:
-          location = new Path(node.getChild(0).getText());
-          break;
-
-        default:
-          throw new NotSupportQueryException("ERROR: not yet supported query");
-      }
-      idx++;
-    }
-
-    if (selectStmt != null) {
-      stmt = new CreateTableStmt(context, tableName, selectStmt);
-    } else {
-      stmt = new CreateTableStmt(context, tableName);
-      if (external) {
-        stmt.setExternal(external);
-        if (location != null) {
-          stmt.setPath(location);
-        }
-      }
-    }
-
-    if (tableDef != null) {
-      stmt.setTableDef(tableDef);
-    }
-
-    if (storeType != null) {
-      stmt.setStoreType(storeType);
-      if (options != null) {
-        stmt.setOptions(options);
-      }
-    }
-
-    // constraints
-    if (external && location == null) {
-      throw new TQLSyntaxError(context.getRawQuery(), "CREATE EXTERNAL TABLE statement requires LOCATION clause.");
-    }
-    if (!external && location != null) {
-      throw new TQLSyntaxError(context.getRawQuery(), "LOCATION clause can be only used in CREATE EXTERNAL TABLE statement.");
-    }
-    if (tableDef == null && location != null) {
-      throw new TQLSyntaxError(context.getRawQuery(), "LOCATION clause requires a schema definition.");
-    }
-
-    return stmt;
-  }
-
-  private Schema parseCreateTableDef(final CommonTree ast) {
-    Schema tableDef = new Schema();
-    Type type;
-    for (int i = 0; i < ast.getChildCount(); i++) {
-      Tree child = ast.getChild(i).getChild(1);
-      switch(child.getType()) {
-        case SQLParser.BOOLEAN: type = Type.BOOLEAN; break;
-        case SQLParser.BIT: type = Type.BIT; break;
-
-        case SQLParser.INT1:
-        case SQLParser.INT2:
-          type = Type.INT2;
-          break;
-
-        case SQLParser.INT4: type = Type.INT4; break;
-        case SQLParser.INT8: type = Type.INT8; break;
-        case SQLParser.FLOAT4: type = Type.FLOAT4; break;
-        case SQLParser.FLOAT:
-          if (child.getChildCount() > 0) {
-            int length = Integer.valueOf(child.getChild(0).getText());
-            if (length < 1 || length > 53) {
-              throw new InvalidQueryException("ERROR: floating point precision " + length + " is out of range");
-            }
-            if (length > 25) {
-              type = Type.FLOAT8;
-            } else {
-              type = Type.FLOAT4;
-            }
-          } else { // no given length
-            type = Type.FLOAT8;
-          }
-          break;
-        case SQLParser.FLOAT8:
-          type = Type.FLOAT8; break;
-        case SQLParser.TEXT: type = Type.TEXT; break;
-        case SQLParser.BLOB: type = Type.BLOB; break;
-        case SQLParser.INET4: type = Type.INET4;
-          break;
-
-        case SQLParser.CHAR:
-        case SQLParser.NCHAR:
-        case SQLParser.NUMERIC:
-        case SQLParser.VARCHAR:
-        case SQLParser.NVARCHAR:
-        case SQLParser.BINARY:
-        case SQLParser.VARBINARY:
-          throw new NotImplementedException("ERROR: " + child.toString() +
-              " type is not supported yet");
-
-        default: throw new InvalidQueryException(ast.toStringTree());
-      }
-
-      tableDef.addColumn(ast.getChild(i).getChild(0).getText(), type);
-    }
-
-    return tableDef;
-  }
-
-  private SetStmt parseSetStatement(final PlanningContextImpl context,
-                                    final CommonTree ast) {
-    StatementType type;
-    boolean distinct = true;
-    ParseTree left;
-    ParseTree right;
-
-    switch (ast.getType()) {
-      case SQLParser.UNION:
-        type = StatementType.UNION;
-        break;
-      case SQLParser.EXCEPT:
-        type = StatementType.EXCEPT;
-        break;
-      case SQLParser.INTERSECT:
-        type = StatementType.INTERSECT;
-        break;
-      default:
-        throw new InvalidQueryException("Illegal AST:\n" + ast.toStringTree());
-    }
-
-    int idx = 0;
-    left = parseQueryTree(context, (CommonTree) ast.getChild(idx));
-    idx++;
-    int nodeType = ast.getChild(idx).getType();
-    if (nodeType == SQLParser.ALL) {
-      distinct = true;
-      idx++;
-    } else if (nodeType == SQLParser.DISTINCT) {
-      distinct = false;
-      idx++;
-    }
-    right = parseQueryTree(context, (CommonTree) ast.getChild(idx));
-    return new SetStmt(context, type, left, right, distinct);
-  }
-
-  private QueryBlock parseSelectStatement(PlanningContext context,
-                                          final CommonTree ast) {
-
-    QueryBlock block = new QueryBlock(context);
-
-    CommonTree node;
-    for (int cur = 0; cur < ast.getChildCount(); cur++) {
-      node = (CommonTree) ast.getChild(cur);
-
-      switch (node.getType()) {
-        case SQLParser.FROM:
-          parseFromClause(context, block, node);
-          break;
-
-        case SQLParser.SET_QUALIFIER:
-          parseSetQualifier(block, node);
-          break;
-
-        case SQLParser.SEL_LIST:
-          parseSelectList(context, block, node);
-          break;
-
-        case SQLParser.WHERE:
-          parseWhereClause(context, block, node);
-          break;
-
-        case SQLParser.GROUP_BY:
-          parseGroupByClause(context, block, node);
-          break;
-
-        case SQLParser.HAVING:
-          parseHavingClause(context, block, node);
-          break;
-
-        case SQLParser.ORDER_BY:
-          SortSpec[] sortKeys = parseSortSpecifiers(context, block,
-              (CommonTree) node.getChild(0));
-          block.setSortKeys(sortKeys);
-          break;
-
-        case SQLParser.LIMIT:
-          block.setLimit(parseLimitClause(context, block, node));
-          break;
-
-        default:
-
-      }
-    }
-
-    return block;
-  }
-
-  private void parseSetQualifier(final QueryBlock block, final CommonTree ast) {
-    int idx = 0;
-
-    if (ast.getChild(idx).getType() == SQLParser.DISTINCT) {
-      block.setDistinct();
-    }
-  }
-
-  /**
-   * EBNF: CREATE (UNIQUE?) INDEX n=ID ON t=ID LEFT_PAREN s=sort_specifier_list 
-   * RIGHT_PAREN p=param_clause? <br />
-   * AST:  ^(CREATE_INDEX $n $t $s $p)
-   *
-   * @param ast
-   */
-  private CreateIndexStmt parseIndexStatement(PlanningContext context,
-                                              final CommonTree ast) {
-    CreateIndexStmt indexStmt = new CreateIndexStmt(context);
-
-    int idx = 0;
-    boolean unique = false;
-    // the below things are optional
-    if (ast.getChild(idx).getType() == SQLParser.UNIQUE) {
-      unique = true;
-      idx++;
-    }
-
-    IndexMethod method = null;
-    if (ast.getChild(idx).getType() == SQLParser.USING) {
-      method = getIndexMethod(ast.getChild(idx).getText());
-      idx++;
-    }
-
-    // It's optional, so it can be null if there is no params clause.
-    Options params = null;
-    if (ast.getChild(idx).getType() == SQLParser.PARAMS) {
-      params = parseParams((CommonTree) ast.getChild(idx));
-      idx++;
-    }
-
-    // They are required, so they are always filled.
-    String idxName = ast.getChild(idx++).getText();
-    String tbName = ast.getChild(idx++).getText();
-    indexStmt.setIndexName(idxName);
-    indexStmt.setTableName(tbName);
-
-    SortSpec [] sortSpecs = parseSortSpecifiers(context, indexStmt,
-        (CommonTree) ast.getChild(idx++));
-
-    if (unique) {
-      indexStmt.setUnique();
-    }
-    indexStmt.setSortSpecs(sortSpecs);
-
-    if (method != null) {
-      indexStmt.setMethod(method);
-    }
-
-    if (params != null) {
-      indexStmt.setParams(params);
-    }
-
-    return indexStmt;
-  }
-
-  /**
-   * EBNF: table_list -> tableRef (COMMA tableRef)
-   * @param block
-   * @param ast
-   */
-  private void parseFromClause(PlanningContext context,
-                               final QueryBlock block, final CommonTree ast) {
-    // implicit join or the from clause on single relation
-    boolean isPrevJoin = false;
-    FromTable table = null;
-    JoinClause joinClause = null;
-    CommonTree node;
-    for (int i = 0; i < ast.getChildCount(); i++) {
-      node = (CommonTree) ast.getChild(i);
-
-      switch (node.getType()) {
-
-        case SQLParser.TABLE:
-          // table (AS ID)?
-          // 0 - a table name, 1 - table alias
-          table = parseTable(node);
-          block.addFromTable(table, true);
-          break;
-        case SQLParser.JOIN:
-          QueryBlock.JoinClause newJoin = parseExplicitJoinClause(context, block, node);
-          if (isPrevJoin) {
-            newJoin.setLeft(joinClause);
-            joinClause = newJoin;
-          } else {
-            newJoin.setLeft(table);
-            isPrevJoin = true;
-            joinClause = newJoin;
-          }
-          break;
-        default:
-          throw new TQLSyntaxError(context.getRawQuery(), "Wrong From Clause");
-      } // switch
-    } // for each derievedTable
-
-    if (joinClause != null) {
-      block.setJoinClause(joinClause);
-    }
-  }
-
-
-  private JoinClause parseExplicitJoinClause(final PlanningContext context,
-                                             final QueryBlock block,
-                                             final CommonTree ast) {
-
-    int idx = 0;
-    int parsedJoinType = ast.getChild(idx).getType();
-
-    JoinClause joinClause;
-
-    switch (parsedJoinType) {
-      case SQLParser.CROSS:
-      case SQLParser.UNION:
-        joinClause = parseCrossAndUnionJoin(context, block, ast);
-        break;
-
-      case SQLParser.NATURAL:
-        joinClause = parseNaturalJoinClause(context, block, ast);
-        break;
-
-      case SQLParser.INNER:
-      case SQLParser.OUTER:
-        joinClause = parseQualifiedJoinClause(context, block, ast, 0);
-        break;
-
-      default: // default join (without join type) is inner join
-        joinClause = parseQualifiedJoinClause(context, block, ast, 0);
-    }
-
-    return joinClause;
-  }
-
-  private JoinClause parseNaturalJoinClause(final PlanningContext context,
-                                            final QueryBlock block, Tree ast) {
-    JoinClause join = parseQualifiedJoinClause(context, block, ast, 1);
-    join.setNatural();
-    return join;
-  }
-
-  private JoinClause parseQualifiedJoinClause(PlanningContext context,
-                                              QueryBlock block,
-                                              Tree ast, final int idx) {
-    int childIdx = idx;
-    JoinClause join = null;
-
-    if (ast.getChild(childIdx).getType() == SQLParser.TABLE) { // default join
-      join = new JoinClause(JoinType.INNER);
-      join.setRight(parseTable((CommonTree) ast.getChild(childIdx)));
-      block.addFromTable(join.getRight(), true);
-
-    } else {
-
-      if (ast.getChild(childIdx).getType() == SQLParser.INNER) {
-        join = new JoinClause(JoinType.INNER);
-
-      } else if (ast.getChild(childIdx).getType() == SQLParser.OUTER) {
-
-        switch (ast.getChild(childIdx).getChild(0).getType()) {
-          case SQLParser.LEFT:
-            join = new JoinClause(JoinType.LEFT_OUTER);
-            break;
-          case SQLParser.RIGHT:
-            join = new JoinClause(JoinType.RIGHT_OUTER);
-            break;
-          case SQLParser.FULL:
-            join = new JoinClause(JoinType.FULL_OUTER);
-            break;
-          default:
-            throw new TQLSyntaxError(context.getRawQuery(), "Unknown Join Type");
-        }
-      }
-
-      childIdx++;
-      join.setRight(parseTable((CommonTree) ast.getChild(childIdx)));
-      block.addFromTable(join.getRight(), true);
-    }
-
-    childIdx++;
-
-    if (ast.getChildCount() > childIdx) {
-      CommonTree joinQual = (CommonTree) ast.getChild(childIdx);
-
-      if (joinQual.getType() == SQLParser.ON) {
-        EvalNode joinCond = parseJoinCondition(context, block, joinQual);
-        join.setJoinQual(joinCond);
-
-      } else if (joinQual.getType() == SQLParser.USING) {
-        Column[] joinColumns = parseJoinColumns(context, block, joinQual);
-        join.setJoinColumns(joinColumns);
-      }
-    }
-
-    return join;
-  }
-
-  private JoinClause parseCrossAndUnionJoin(final PlanningContext context,
-                                            final QueryBlock block, Tree ast) {
-    JoinType joinType;
-
-    if (ast.getChild(0).getType() == SQLParser.CROSS) {
-      joinType = JoinType.CROSS_JOIN;
-    } else if (ast.getChild(0).getType() == SQLParser.UNION) {
-      joinType = JoinType.UNION;
-    } else {
-      throw new IllegalStateException("Neither the AST has cross join or union join:\n" + ast.toStringTree());
-    }
-
-    JoinClause join = new JoinClause(joinType);
-    Preconditions.checkState(ast.getChild(1).getType() == SQLParser.TABLE);
-    join.setRight(parseTable((CommonTree) ast.getChild(1)));
-    block.addFromTable(join.getRight(), true);
-
-    return join;
-  }
-
-  private Column [] parseJoinColumns(final PlanningContext context,
-                                     final QueryBlock block,
-                                     final CommonTree ast) {
-    Column [] joinColumns = new Column[ast.getChildCount()];
-
-    for (int i = 0; i < ast.getChildCount(); i++) {
-      joinColumns[i] = checkAndGetColumnByAST(context,
-          block, (CommonTree) ast.getChild(i));
-    }
-    return joinColumns;
-  }
-
-  private EvalNode parseJoinCondition(final PlanningContext context,
-                                      QueryBlock tree, CommonTree ast) {
-    return createEvalTree(context, tree, ast.getChild(0));
-  }
-
-  private FromTable parseTable(final CommonTree tableAST) {
-    String tableName = tableAST.getChild(0).getText();
-    TableDesc desc = checkAndGetTableByName(tableName);
-    FromTable table;
-
-    if (tableAST.getChildCount() > 1) {
-      table = new FromTable(desc,
-          tableAST.getChild(1).getText());
-    } else {
-      table = new FromTable(desc);
-    }
-
-    return table;
-  }
-
-  /**
-   * This method parses the select list of a query statement.
-   * <pre>
-   * EBNF: 
-   *
-   * selectList
-   * : MULTIPLY -> ^(SEL_LIST ALL)
-   * | derivedColumn (COMMA derivedColumn)* -> ^(SEL_LIST derivedColumn+)
-   * ;
-   *
-   * derivedColumn
-   * : bool_expr asClause? -> ^(COLUMN bool_expr asClause?)
-   * ;
-   *
-   * @param block
-   * @param ast
-   */
-  private void parseSelectList(final PlanningContext context,
-                               final QueryBlock block, final CommonTree ast) {
-
-    if (ast.getChild(0).getType() == SQLParser.ALL) {
-      block.setProjectAll();
-    } else {
-      CommonTree node;
-      int numTargets = ast.getChildCount();
-      Target [] targets = new Target[numTargets];
-      EvalNode evalTree;
-      String alias;
-
-      // the final one for each target is the alias
-      // EBNF: bool_expr AS? fieldName
-      for (int i = 0; i < ast.getChildCount(); i++) {
-        node = (CommonTree) ast.getChild(i);
-        evalTree = createEvalTree(context, block, node);
-        targets[i] = new Target(evalTree, i);
-        if (node.getChildCount() > 1) {
-          alias = node.getChild(node.getChildCount() - 1).getChild(0).getText();
-          targets[i].setAlias(alias);
-        }
-      }
-
-      block.setTargetList(targets);
-    }
-  }
-
-  private void parseWhereClause(final PlanningContext context,
-                                final QueryBlock block, final CommonTree ast) {
-    EvalNode whereCond = createEvalTree(context, block, ast.getChild(0));
-    block.setWhereCondition(whereCond);
-  }
-
-  /**
-   * See 'groupby_clause' rule in NQL.g
-   *
-   * @param block
-   * @param ast
-   */
-  private void parseGroupByClause(final PlanningContext context,
-                                  final QueryBlock block,
-                                  final CommonTree ast) {
-    GroupByClause clause = new GroupByClause();
-
-    int idx = 0;
-
-    if (ast.getChild(idx).getType() == SQLParser.EMPTY_GROUPING_SET) {
-      clause.setEmptyGroupSet();
-    } else {
-      // the remain ones are grouping fields.
-      Tree group;
-      List<Column> columnRefs = new ArrayList<Column>();
-      Column [] columns;
-      Column column;
-      for (; idx < ast.getChildCount(); idx++) {
-        group = ast.getChild(idx);
-        switch (group.getType()) {
-          case SQLParser.CUBE:
-            columns = parseColumnReferences(context, block, (CommonTree) group);
-            GroupElement cube = new GroupElement(GroupType.CUBE, columns);
-            clause.addGroupSet(cube);
-            break;
-
-          case SQLParser.ROLLUP:
-            columns = parseColumnReferences(context, block, (CommonTree) group);
-            GroupElement rollup = new GroupElement(GroupType.ROLLUP, columns);
-            clause.addGroupSet(rollup);
-            break;
-
-          case SQLParser.FIELD_NAME:
-            column = checkAndGetColumnByAST(context, block, (CommonTree) group);
-            columnRefs.add(column);
-            break;
-        }
-      }
-
-      if (columnRefs.size() > 0) {
-        Column [] groupingFields = columnRefs.toArray(new Column[columnRefs.size()]);
-        GroupElement g = new GroupElement(GroupType.GROUPBY, groupingFields);
-        clause.addGroupSet(g);
-      }
-    }
-
-    block.setGroupByClause(clause);
-  }
-
-  private void parseHavingClause(final PlanningContext context,
-                                 final QueryBlock block, final CommonTree ast) {
-    EvalNode evalTree =
-        createEvalTree(context, block, ast.getChild(0));
-    block.setHavingCond(evalTree);
-  }
-
-  /**
-   * Should be given Params Node
-   *
-   * EBNF: WITH LEFT_PAREN param (COMMA param)* RIGHT_PAREN 
-   * AST: ^(PARAMS param+)
-   *
-   * @param ast
-   * @return
-   */
-  private static Options parseParams(final CommonTree ast) {
-    Options params = new Options();
-
-    Tree child;
-    for (int i = 0; i < ast.getChildCount(); i++) {
-      child = ast.getChild(i);
-      params.put(child.getChild(0).getText(), child.getChild(1).getText());
-    }
-    return params;
-  }
-
-
-  /**
-   * Should be given SortSpecifiers Node
-   *
-   * EBNF: sort_specifier (COMMA sort_specifier)* -> sort_specifier+
-   *
-   * @param tree
-   * @param ast
-   */
-  private SortSpec[] parseSortSpecifiers(final PlanningContext context,
-                                          final ParseTree tree,
-                                          final CommonTree ast) {
-    int numSortKeys = ast.getChildCount();
-    SortSpec[] sortKeys = new SortSpec[numSortKeys];
-    CommonTree node;
-    Column column;
-
-    // Each child has the following EBNF and AST:
-    // EBNF: fn=fieldName a=order_specification? o=null_ordering? 
-    // AST: ^(SORT_KEY $fn $a? $o?)
-    for (int i = 0; i < numSortKeys; i++) {
-      node = (CommonTree) ast.getChild(i);
-      column = checkAndGetColumnByAST(context, tree, (CommonTree) node.getChild(0));
-      sortKeys[i] = new SortSpec(column);
-
-      if (node.getChildCount() > 1) {
-        Tree child;
-        for (int j = 1; j < node.getChildCount(); j++) {
-          child = node.getChild(j);
-
-          // AST: ^(ORDER ASC) | ^(ORDER DESC)
-          if (child.getType() == SQLParser.ORDER) {
-            if (child.getChild(0).getType() == SQLParser.DESC) {
-              sortKeys[i].setDescOrder();
-            }
-          } else if (child.getType() == SQLParser.NULL_ORDER) {
-            // AST: ^(NULL_ORDER FIRST) | ^(NULL_ORDER LAST)
-            if (child.getChild(0).getType() == SQLParser.FIRST) {
-              sortKeys[i].setNullFirst();
-            }
-          }
-        }
-      }
-    }
-
-    return sortKeys;
-  }
-
-  private LimitClause parseLimitClause(final PlanningContext context,
-                                       final ParseTree tree,
-                                       final CommonTree ast) {
-    EvalNode evalNode = createEvalTree(context, (QueryBlock) tree, ast.getChild(0));
-
-
-    if (evalNode instanceof ConstEval) {
-      ConstEval fetchFirst = (ConstEval) evalNode;
-      LimitClause limitClause = new LimitClause(fetchFirst.getValue().asInt8());
-      return limitClause;
-    }
-
-    throw new TQLSyntaxError(context.getRawQuery(), "LIMIT clause cannot have the parameter "
-        + evalNode);
-  }
-
-  private Column checkAndGetColumnByAST(final PlanningContext context,
-                                        final ParseTree tree,
-                                        final CommonTree fieldNode) {
-    Preconditions.checkArgument(SQLParser.FIELD_NAME == fieldNode.getType());
-
-    String columnName = fieldNode.getChild(0).getText();
-    String tableName = null;
-    if (fieldNode.getChildCount() > 1) {
-      tableName = fieldNode.getChild(1).getText();
-    }
-
-    Column column;
-    if(tableName != null) {
-      TableDesc desc;
-      desc = checkAndGetTableByMappedName(context, tree, tableName);
-      column = checkAndGetFieldByName(desc, columnName);
-    } else {
-      column = expectTableByField(context, tree, columnName);
-    }
-
-    return column;
-  }
-
-  private TableDesc checkAndGetTableByMappedName(PlanningContext context,
-                                                 ParseTree tree,
-                                                 final String tableName) {
-    String realName = tree.getTableNameByAlias(tableName);
-    return checkAndGetTableByName(realName);
-  }
-
-  private TableDesc checkAndGetTableByName(final String tableName) {
-    TableDesc desc;
-
-    try {
-      desc = catalog.getTableDesc(tableName);
-    } catch (NoSuchTableException nst) {
-      throw new InvalidQueryException("ERROR: table \"" + tableName
-          + "\" does not exist");
-    }
-
-    return desc;
-  }
-
-  private static Column checkAndGetFieldByName(final TableDesc desc,
-                                               final String columnName) {
-    Column column;
-
-    column = desc.getMeta().getSchema().getColumn(desc.getId()+"."+columnName);
-    if(column == null) {
-      throw new InvalidQueryException("ERROR: column \"" + columnName
-          + "\" does not exist");
-    }
-
-    return column;
-  }
-
-  /**
-   * determine a column by finding tables which are given by 'from clause'.
-   *
-   * @param tree
-   * @param columnName field name to be find
-   * @return a found column
-   */
-  private Column expectTableByField(final PlanningContext context,
-                                    final ParseTree tree, String columnName) {
-    TableDesc desc;
-    Schema schema;
-    Column column = null;
-    int count = 0;
-
-    // find a column corresponding to the given column name from tables of the catalog
-    for(String table : tree.getAllTableNames()) {
-      desc =
-          catalog.getTableDesc(table);
-      schema = desc.getMeta().getSchema();
-
-      if(schema.contains(table+"."+columnName)) {
-        column = schema.getColumn(table+"."+columnName);
-        count++;
-      }
-    }
-
-    // find a column corresponding to the given column name from the target list
-    if (tree instanceof QueryBlock) {
-      QueryBlock block = ((QueryBlock)tree);
-      if (block.getTargetList() != null) {
-        for (Target target : block.getTargetList()) {
-          if (target.hasAlias() && target.getAlias().equals(columnName)) {
-            try {
-              column = (Column) target.getColumnSchema().clone();
-              column.setName(target.getAlias());
-            } catch (CloneNotSupportedException e) {
-              e.printStackTrace();
-            }
-            count++;
-          }
-        }
-      }
-    }
-
-    // if there are more than one column, we cannot exactly expect
-    // that this column belongs to which table.
-    if(count > 1)
-      throw new AmbiguousFieldException(columnName);
-
-
-    if(column == null) { // if there are no matched column
-      throw new InvalidQueryException("ERROR: column \"" + columnName
-          + "\" does not exist");
-    }
-
-    return column;
-  }
-
-  private static CommonTree parseTree(final String query) {
-    ANTLRStringStream input = new ANTLRStringStream(query);
-    SQLLexer lexer = new SQLLexer(input);
-
-    CommonTokenStream tokens = new CommonTokenStream(lexer);
-    SQLParser parser = new SQLParser(tokens);
-
-    CommonTree ast;
-    try {
-      ast = ((CommonTree) parser.statement().getTree());
-    } catch (RecognitionException e) {
-      throw new TQLParseError(e.getMessage());
-    }
-
-    return ast;
-  }
-
-  private static StatementType getCmdType(final CommonTree ast) {
-    switch (ast.getType()) {
-      case SQLParser.STORE:
-        return StatementType.STORE;
-      case SQLParser.SELECT:
-        return StatementType.SELECT;
-      case SQLParser.UNION:
-        return StatementType.UNION;
-      case SQLParser.EXCEPT:
-        return StatementType.EXCEPT;
-      case SQLParser.INTERSECT:
-        return StatementType.INTERSECT;
-      case SQLParser.INSERT:
-        return StatementType.INSERT;
-      case SQLParser.CREATE_INDEX:
-        return StatementType.CREATE_INDEX;
-      case SQLParser.CREATE_TABLE:
-        return StatementType.CREATE_TABLE;
-      case SQLParser.DROP_TABLE:
-        return StatementType.DROP_TABLE;
-      case SQLParser.SHOW_TABLE:
-        return StatementType.SHOW_TABLES;
-      case SQLParser.DESC_TABLE:
-        return StatementType.DESC_TABLE;
-      case SQLParser.SHOW_FUNCTION:
-        return StatementType.SHOW_FUNCTION;
-      default:
-        return null;
-    }
-  }
-
-  private static IndexMethod getIndexMethod(String method) {
-    Preconditions.checkNotNull(method);
-    if (method.equals("bst")) {
-      return IndexMethod.TWO_LEVEL_BIN_TREE;
-    } else if (method.equals("btree")) {
-      return IndexMethod.BTREE;
-    } else if (method.equals("hash")) {
-      return IndexMethod.HASH;
-    } else if (method.equals("bitmap")) {
-      return IndexMethod.BITMAP;
-    } else {
-      throw new TQLParseError("ERROR: unknown index: " + method);
-    }
-  }
-
-  public EvalNode createEvalTree(final PlanningContext context,
-                                 final QueryBlock tree, final Tree ast) {
-    switch(ast.getType()) {
-
-      // constants
-      case SQLParser.NUMBER:
-        return new ConstEval(DatumFactory.createInt4(
-            Integer.valueOf(ast.getText())));
-
-      case SQLParser.REAL_NUMBER:
-        return new ConstEval(DatumFactory.createFloat4(
-            Float.valueOf(ast.getText())));
-
-      case SQLParser.Character_String_Literal:
-        return new ConstEval(DatumFactory.createText(ast.getText()));
-
-      // unary expression
-      case SQLParser.NOT:
-        return new NotEval(createEvalTree(context, tree, ast.getChild(0)));
-
-      // binary expressions
-      case SQLParser.LIKE:
-        return parseLike(context, tree, ast);
-
-      case SQLParser.IS:
-        return parseIsNullPredicate(context, tree, ast);
-
-      case SQLParser.AND:
-      case SQLParser.OR:
-      case SQLParser.EQUAL:
-      case SQLParser.NOT_EQUAL:
-      case SQLParser.LTH:
-      case SQLParser.LEQ:
-      case SQLParser.GTH:
-      case SQLParser.GEQ:
-      case SQLParser.PLUS:
-      case SQLParser.MINUS:
-      case SQLParser.MULTIPLY:
-      case SQLParser.DIVIDE:
-      case SQLParser.MODULAR:
-        return parseBinaryExpr(context, tree, ast);
-
-      // others
-      case SQLParser.COLUMN:
-        return createEvalTree(context, tree, ast.getChild(0));
-
-      case SQLParser.FIELD_NAME:
-        Column column = checkAndGetColumnByAST(context, tree, (CommonTree) ast);
-        return new FieldEval(column);
-
-      case SQLParser.FUNCTION:
-        String signature = ast.getText();
-
-        EvalNode [] givenArgs = new EvalNode[ast.getChildCount()];
-        DataType [] paramTypes = new DataType[ast.getChildCount()];
-
-        for (int i = 0; i < ast.getChildCount(); i++) {
-          givenArgs[i] = createEvalTree(context, tree, ast.getChild(i));
-          paramTypes[i] = givenArgs[i].getValueType()[0];
-        }
-        if (!catalog.containFunction(signature, paramTypes)) {
-          throw new UndefinedFunctionException(
-              CatalogUtil.getCanonicalName(signature, paramTypes));
-        }
-        FunctionDesc funcDesc = catalog.getFunction(signature, paramTypes);
-
-        try {
-          if (funcDesc.getFuncType() == FunctionType.GENERAL)
-
-            return new FuncCallEval(funcDesc,
-                (GeneralFunction) funcDesc.newInstance(), givenArgs);
-          else {
-            tree.setAggregation();
-
-            return new AggFuncCallEval(funcDesc,
-                (AggFunction) funcDesc.newInstance(), givenArgs);
-          }
-        } catch (InternalException e) {
-          e.printStackTrace();
-        }
-
-        break;
-      case SQLParser.COUNT_VAL:
-        // Getting the first argument
-        EvalNode colRef = createEvalTree(context, tree, ast.getChild(0));
-
-        FunctionDesc countVals = catalog.getFunction("count",
-            CatalogUtil.newDataTypeWithoutLen(Type.ANY));
-        tree.setAggregation();
-        try {
-          return new AggFuncCallEval(countVals, (AggFunction) countVals.newInstance(),
-              new EvalNode [] {colRef});
-        } catch (InternalException e1) {
-          e1.printStackTrace();
-        }
-        break;
-
-      case SQLParser.COUNT_ROWS:
-        FunctionDesc countRows = catalog.getFunction("count", new DataType [] {});
-        tree.setAggregation();
-        try {
-          return new AggFuncCallEval(countRows, (AggFunction) countRows.newInstance(),
-              new EvalNode [] {});
-        } catch (InternalException e) {
-          e.printStackTrace();
-        }
-        break;
-
-      case SQLParser.CASE:
-        return parseCaseWhen(context, tree, ast);
-
-      default:
-    }
-    return null;
-  }
-
-  public IsNullEval parseIsNullPredicate(final PlanningContext context,
-                                         QueryBlock block, Tree tree) {
-    boolean not;
-
-    Preconditions.checkArgument(tree.getType() == SQLParser.IS, "The AST is not IS (NOT) NULL clause");
-    int idx = 0;
-
-    FieldEval field = (FieldEval) createEvalTree(context, block,
-        tree.getChild(idx++));
-    Preconditions.checkArgument(
-        tree.getChild(idx++).getType() == SQLParser.NULL,
-        "We does not support another kind of IS clause yet");
-    not = tree.getChildCount() == (idx + 1)
-        && tree.getChild(idx).getType() == SQLParser.NOT;
-
-    return new IsNullEval(not, field);
-  }
-
-  /**
-   * The EBNF of case statement
-   * <pre>
-   * searched_case
-   * : CASE s=searched_when_clauses e=else_clause END -> ^(CASE $s $e)
-   * ;
-   *
-   * searched_when_clauses
-   * : searched_when_clause searched_when_clause* -> searched_when_clause+
-   * ;
-   *
-   * searched_when_clause
-   * : WHEN c=search_condition THEN r=result -> ^(WHEN $c $r)
-   * ;
-   *
-   * else_clause
-   * : ELSE r=result -> ^(ELSE $r)
-   * ;
-   * </pre>
-   * @param block
-   * @param tree
-   * @return
-   */
-  public CaseWhenEval parseCaseWhen(final PlanningContext context,
-                                    final QueryBlock block, final Tree tree) {
-    int idx = 0;
-
-    CaseWhenEval caseEval = new CaseWhenEval();
-    EvalNode cond;
-    EvalNode thenResult;
-    Tree when;
-
-    for (; idx < tree.getChildCount() &&
-        tree.getChild(idx).getType() == SQLParser.WHEN; idx++) {
-
-      when = tree.getChild(idx);
-      cond = createEvalTree(context, block, when.getChild(0));
-      thenResult = createEvalTree(context, block, when.getChild(1));
-      caseEval.addWhen(cond, thenResult);
-    }
-
-    if (tree.getChild(idx) != null &&
-        tree.getChild(idx).getType() == SQLParser.ELSE) {
-      EvalNode elseResult = createEvalTree(context, block,
-          tree.getChild(idx).getChild(0));
-      caseEval.setElseResult(elseResult);
-    }
-
-    return caseEval;
-  }
-
-  public EvalNode parseDigitByTypeInfer(final PlanningContext context,
-                                        final QueryBlock block, final Tree tree,
-                                        DataType type) {
-    switch (type.getType()) {
-      case INT2:
-        return new ConstEval(DatumFactory.createInt2(tree.getText()));
-      case INT4:
-        return new ConstEval(DatumFactory.createInt4(tree.getText()));
-      case INT8 :
-        return new ConstEval(DatumFactory.createInt8(tree.getText()));
-      default: return createEvalTree(context, block, tree);
-    }
-  }
-
-  private EvalNode parseRealByTypeInfer(final PlanningContext context,
-                                        final QueryBlock block, final Tree tree,
-                                        DataType type) {
-    switch (type.getType()) {
-      case FLOAT4:
-        return new ConstEval(DatumFactory.createFloat4(tree.getText()));
-      case FLOAT8:
-        return new ConstEval(DatumFactory.createFloat8(tree.getText()));
-      default: return createEvalTree(context, block, tree);
-    }
-  }
-
-  private EvalNode parseStringByTypeInfer(final PlanningContext context,
-                                          final QueryBlock block,
-                                          final Tree tree,
-                                          DataType type) {
-    switch (type.getType()) {
-      case CHAR:
-        return new ConstEval(DatumFactory.createChar(tree.getText().charAt(0)));
-      case TEXT:
-        return new ConstEval(DatumFactory.createText(tree.getText()));
-      default: return createEvalTree(context, block, tree);
-    }
-  }
-
-  @VisibleForTesting
-  EvalNode parseBinaryExpr(final PlanningContext context,
-                           final QueryBlock block, final Tree tree) {
-    int constId = -1;
-    int fieldId = -1;
-
-    for (int i = 0; i < 2; i++) {
-      if (ParseUtil.isConstant(tree.getChild(i))) {
-        constId = i;
-      } else if (tree.getChild(i).getType() == SQLParser.FIELD_NAME) {
-        fieldId = i;
-      }
-    }
-
-    if (constId != -1 && fieldId != -1) {
-      EvalNode [] exprs = new EvalNode[2];
-      exprs[fieldId] = createEvalTree(context, block,
-          tree.getChild(fieldId));
-
-      Tree constAst = tree.getChild(constId);
-
-      switch (tree.getChild(constId).getType()) {
-        case SQLParser.NUMBER:
-          exprs[constId] = parseDigitByTypeInfer(context, block, constAst,
-              exprs[fieldId].getValueType()[0]);
-          break;
-
-        case SQLParser.REAL_NUMBER:
-          exprs[constId] = parseRealByTypeInfer(context, block, constAst,
-              exprs[fieldId].getValueType()[0]);
-          break;
-
-        case SQLParser.Character_String_Literal:
-          exprs[constId] = parseStringByTypeInfer(context, block, constAst,
-              exprs[fieldId].getValueType()[0]);
-          break;
-
-        default: throw new org.apache.tajo.engine.eval.InvalidEvalException();
-      }
-
-      if (constId == 0) {
-        return new BinaryEval(ParseUtil.getTypeByParseCode(tree.getType()),
-            exprs[constId], exprs[fieldId]);
-      } else {
-        return new BinaryEval(ParseUtil.getTypeByParseCode(tree.getType()),
-            exprs[fieldId], exprs[constId]);
-      }
-
-    } else {
-      return new BinaryEval(ParseUtil.getTypeByParseCode(tree.getType()),
-          createEvalTree(context, block, tree.getChild(0)),
-          createEvalTree(context, block, tree.getChild(1)));
-    }
-  }
-
-  /**
-   * <pre>
-   * like_predicate : fieldName NOT? LIKE string_value_expr 
-   * -> ^(LIKE NOT? fieldName string_value_expr)
-   * </pre>
-   * @param block
-   * @param tree
-   * @return
-   */
-  private LikeEval parseLike(final PlanningContext context,
-                             final QueryBlock block, final Tree tree) {
-    int idx = 0;
-
-    boolean not = false;
-    if (tree.getChild(idx).getType() == SQLParser.NOT) {
-      not = true;
-      idx++;
-    }
-
-    FieldEval field = (FieldEval) createEvalTree(context, block,
-        tree.getChild(idx));
-    idx++;
-    ConstEval pattern = (ConstEval) createEvalTree(context, block,
-        tree.getChild(idx));
-
-    return new LikeEval(not, field, pattern);
-  }
-
-  /**
-   * It parses the below EBNF.
-   * <pre>
-   * column_reference  
-   * : fieldName (COMMA fieldName)* -> fieldName+
-   * ;
-   * </pre>
-   * @param tree
-   * @param parent
-   * @return
-   */
-  private Column [] parseColumnReferences(final PlanningContext context,
-                                          final ParseTree tree,
-                                          final CommonTree parent) {
-    Column [] columns = new Column[parent.getChildCount()];
-
-    for (int i = 0; i < columns.length; i++) {
-      columns[i] = checkAndGetColumnByAST(context, tree,
-          (CommonTree) parent.getChild(i));
-    }
-
-    return columns;
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/QueryBlock.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/QueryBlock.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/QueryBlock.java
deleted file mode 100644
index 46fdb4b..0000000
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/QueryBlock.java
+++ /dev/null
@@ -1,586 +0,0 @@
-/**
- * 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.engine.parser;
-
-import com.google.common.collect.Lists;
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-import com.google.gson.annotations.Expose;
-import org.apache.tajo.catalog.Column;
-import org.apache.tajo.catalog.Schema;
-import org.apache.tajo.catalog.SortSpec;
-import org.apache.tajo.catalog.TableDesc;
-import org.apache.tajo.catalog.proto.CatalogProtos;
-import org.apache.tajo.common.TajoDataTypes.Type;
-import org.apache.tajo.engine.eval.EvalNode;
-import org.apache.tajo.engine.json.GsonCreator;
-import org.apache.tajo.engine.planner.JoinType;
-import org.apache.tajo.engine.planner.PlanningContext;
-import org.apache.tajo.util.TUtil;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-/**
- * This class contains a set of meta data about a query statement
- */
-public class QueryBlock extends ParseTree {
-  private String storeTable = null;
-  private boolean projectAll = false;
-  private boolean distinct = false;
-  /* select target list */
-  private Target [] targetList = null;
-  /* from clause */
-  private List<FromTable> fromTables = Lists.newArrayList();
-  /* from clause with join */
-  private JoinClause joinClause = null;
-  /* where clause */
-  private EvalNode whereCond = null;
-  /* if true, there is at least one aggregation function. */
-  private boolean aggregation = false;
-  /* if true, there is at least grouping field. */
-  private GroupByClause groupbyClause = null;
-  /* having condition */
-  private EvalNode havingCond = null;
-  /* keys for ordering */
-  private SortSpec [] sortKeys = null;
-  /* limit clause */
-  private LimitClause limitClause = null;
-
-  public QueryBlock(PlanningContext context) {
-    super(context, StatementType.SELECT);
-  }
-
-  public StatementType getStatementType() {
-    return this.type;
-  }
-
-  public void setStoreTable(String storeTable) {
-    this.storeTable = storeTable;
-  }
-
-  public String getStoreTable() {
-    return this.storeTable;
-  }
-
-  public final void setProjectAll() {
-    this.projectAll = true;
-  }
-
-  public final boolean getProjectAll() {
-    return this.projectAll;
-  }
-
-  public final void setDistinct() {
-    this.distinct = true;
-  }
-
-  public final boolean isDistinct() {
-    return this.distinct;
-  }
-
-  public final void setTargetList(Target [] targets) {
-    this.targetList = targets;
-  }
-
-  public final Target [] getTargetList() {
-    return this.targetList;
-  }
-
-  public final boolean hasAggregation() {
-    return this.aggregation || hasGroupbyClause();
-  }
-
-  public final void setAggregation() {
-    this.aggregation = true;
-  }
-
-  public final boolean hasGroupbyClause() {
-    return this.groupbyClause != null;
-  }
-
-  public final void setGroupByClause(final GroupByClause groupbyClause) {
-    this.groupbyClause = groupbyClause;
-  }
-
-  public final GroupByClause getGroupByClause() {
-    return this.groupbyClause;
-  }
-
-  public final boolean hasHavingCond() {
-    return this.havingCond != null;
-  }
-
-  public final void setHavingCond(final EvalNode havingCond) {
-    this.havingCond = havingCond;
-  }
-
-  public final EvalNode getHavingCond() {
-    return this.havingCond;
-  }
-
-  public final boolean hasOrderByClause() {
-    return this.sortKeys != null;
-  }
-
-  public final boolean hasLimitClause() {
-    return this.limitClause != null;
-  }
-
-  public final SortSpec[] getSortKeys() {
-    return this.sortKeys;
-  }
-
-  public void setSortKeys(final SortSpec [] keys) {
-    this.sortKeys = keys;
-  }
-
-  public void setLimit(final LimitClause limit) {
-    this.limitClause = limit;
-  }
-
-  public LimitClause getLimitClause() {
-    return this.limitClause;
-  }
-
-  // From Clause
-  public final boolean hasFromClause() {
-    return fromTables.size() > 0 || joinClause != null;
-  }
-
-  public final void addFromTable(final FromTable table, boolean global) {
-    super.addTableRef(table);
-    this.fromTables.add(table);
-  }
-
-  public final boolean hasExplicitJoinClause() {
-    return joinClause != null;
-  }
-
-  public final void setJoinClause(final JoinClause joinClause) {
-    this.joinClause = joinClause;
-  }
-
-  public final JoinClause getJoinClause() {
-    return this.joinClause;
-  }
-
-  public final int getNumFromTables() {
-    return fromTables.size();
-  }
-
-  public final FromTable [] getFromTables() {
-    return this.fromTables.toArray(new FromTable[fromTables.size()]);
-  }
-
-  public final boolean hasWhereClause() {
-    return this.whereCond != null;
-  }
-
-  public final void setWhereCondition(final EvalNode whereCond) {
-    this.whereCond = whereCond;
-  }
-
-  public final EvalNode getWhereCondition() {
-    return this.whereCond;
-  }
-
-  public static class GroupByClause implements Cloneable {
-    @Expose private boolean isEmptyGroupSet = false;
-    @Expose private List<GroupElement> groups
-        = new ArrayList<QueryBlock.GroupElement>();
-
-    public GroupByClause() {
-    }
-
-    public void setEmptyGroupSet() {
-      isEmptyGroupSet = true;
-    }
-
-    public void addGroupSet(GroupElement group) {
-      groups.add(group);
-      if (isEmptyGroupSet) {
-        isEmptyGroupSet = false;
-      }
-    }
-
-    public boolean isEmptyGroupSet() {
-      return this.isEmptyGroupSet;
-    }
-
-    public List<GroupElement> getGroupSet() {
-      return Collections.unmodifiableList(groups);
-    }
-
-    public String toString() {
-      Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
-          .setPrettyPrinting().create();
-      return gson.toJson(this);
-    }
-
-    public Object clone() throws CloneNotSupportedException {
-      GroupByClause g = (GroupByClause) super.clone();
-      g.isEmptyGroupSet = isEmptyGroupSet;
-      g.groups = new ArrayList<QueryBlock.GroupElement>(groups);
-
-      return g;
-    }
-  }
-
-  public static class GroupElement implements Cloneable {
-    @Expose private GroupType type;
-    @Expose private Column [] columns;
-
-    @SuppressWarnings("unused")
-    private GroupElement() {
-      // for gson
-    }
-
-    public GroupElement(GroupType type, Column [] columns) {
-      this.type = type;
-      this.columns = columns;
-    }
-
-    public GroupType getType() {
-      return this.type;
-    }
-
-    public Column [] getColumns() {
-      return this.columns;
-    }
-
-    public String toString() {
-      Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation()
-          .setPrettyPrinting().create();
-      return gson.toJson(this);
-    }
-
-    public Object clone() throws CloneNotSupportedException {
-      GroupElement groups = (GroupElement) super.clone();
-      groups.type = type;
-      groups.columns = new Column[columns.length];
-      for (int i = 0; i < columns.length; i++) {
-        groups.columns[i++] = (Column) columns[i].clone();
-      }
-      return groups;
-    }
-  }
-
-  public static enum GroupType {
-    GROUPBY,
-    CUBE,
-    ROLLUP,
-    EMPTYSET
-  }
-
-  public static class JoinClause implements Cloneable {
-    @Expose private JoinType joinType;
-    @Expose private FromTable left;
-    @Expose private FromTable right;
-    @Expose private JoinClause leftJoin;
-    @Expose private EvalNode joinQual;
-    @Expose private Column [] joinColumns;
-    @Expose private boolean natural = false;
-
-    @SuppressWarnings("unused")
-    private JoinClause() {
-      // for gson
-    }
-
-    public JoinClause(final JoinType joinType) {
-      this.joinType = joinType;
-    }
-
-    public JoinClause(final JoinType joinType, final FromTable right) {
-      this.joinType = joinType;
-      this.right = right;
-    }
-
-    public JoinClause(final JoinType joinType, final FromTable left,
-                      final FromTable right) {
-      this(joinType, right);
-      this.left = left;
-    }
-
-    public JoinType getJoinType() {
-      return this.joinType;
-    }
-
-    public void setNatural() {
-      this.natural = true;
-    }
-
-    public boolean isNatural() {
-      return this.natural;
-    }
-
-    public void setRight(FromTable right) {
-      this.right = right;
-    }
-
-    public void setLeft(FromTable left) {
-      this.left = left;
-    }
-
-    public void setLeft(JoinClause left) {
-      this.leftJoin = left;
-    }
-
-    public boolean hasLeftJoin() {
-      return leftJoin != null;
-    }
-
-    public FromTable getLeft() {
-      return this.left;
-    }
-
-    public FromTable getRight() {
-      return this.right;
-    }
-
-    public JoinClause getLeftJoin() {
-      return this.leftJoin;
-    }
-
-    public void setJoinQual(EvalNode qual) {
-      this.joinQual = qual;
-    }
-
-    public boolean hasJoinQual() {
-      return this.joinQual != null;
-    }
-
-    public EvalNode getJoinQual() {
-      return this.joinQual;
-    }
-
-    public void setJoinColumns(Column [] columns) {
-      this.joinColumns = columns;
-    }
-
-    public boolean hasJoinColumns() {
-      return this.joinColumns != null;
-    }
-
-    public Column [] getJoinColumns() {
-      return this.joinColumns;
-    }
-
-
-    public String toString() {
-      Gson gson = new GsonBuilder().setPrettyPrinting().create();
-      return gson.toJson(this);
-    }
-  }
-
-  public static class Target implements Cloneable {
-    @Expose private EvalNode eval;
-    @Expose private Column column;
-    @Expose private String alias = null;
-    @Expose private int targetId;
-
-    public Target(EvalNode eval, int targetId) {
-      this.eval = eval;
-      if (eval.getType() == EvalNode.Type.AGG_FUNCTION &&
-          eval.getValueType().length > 1) { // hack for partial result
-        this.column = new Column(eval.getName(), Type.ARRAY);
-      } else {
-        this.column = new Column(eval.getName(), eval.getValueType()[0]);
-      }
-      this.targetId = targetId;
-    }
-
-    public Target(final EvalNode eval, final String alias, int targetId) {
-      this(eval, targetId);
-      this.alias = alias;
-    }
-
-    public final void setAlias(String alias) {
-      this.alias = alias;
-    }
-
-    public final String getAlias() {
-      return alias;
-    }
-
-    public final boolean hasAlias() {
-      return alias != null;
-    }
-
-    public EvalNode getEvalTree() {
-      return this.eval;
-    }
-
-    public Column getColumnSchema() {
-      return this.column;
-    }
-
-    public int getTargetId() {
-      return this.targetId;
-    }
-
-    public String toString() {
-      StringBuilder sb = new StringBuilder(eval.toString());
-      if(hasAlias()) {
-        sb.append(", alias=").append(alias);
-      }
-      return sb.toString();
-    }
-
-    public boolean equals(Object obj) {
-      if(obj instanceof Target) {
-        Target other = (Target) obj;
-
-        boolean b1 = eval.equals(other.eval);
-        boolean b2 = column.equals(other.column);
-        boolean b3 = TUtil.checkEquals(alias, other.alias);
-
-        return b1 && b2 && b3;
-      } else {
-        return false;
-      }
-    }
-
-    public int hashCode() {
-      return this.eval.getName().hashCode();
-    }
-
-    @Override
-    public Object clone() throws CloneNotSupportedException {
-      Target target = (Target) super.clone();
-      target.eval = (EvalNode) eval.clone();
-      target.column = (Column) column.clone();
-      target.alias = alias != null ? alias : null;
-
-      return target;
-    }
-
-    public String toJSON() {
-      return GsonCreator.getInstance().toJson(this, Target.class);
-    }
-  }
-
-  public static class FromTable implements Cloneable {
-    @Expose
-    private TableDesc desc;
-    @Expose
-    private String alias = null;
-
-    public FromTable() {}
-
-    public FromTable(final TableDesc desc) {
-      this.desc = desc;
-    }
-
-    public FromTable(final TableDesc desc, final String alias) {
-      this(desc);
-      this.alias = alias;
-    }
-
-    public final String getTableName() {
-      return desc.getId();
-    }
-
-    public final String getTableId() {
-      return alias == null ? desc.getId() : alias;
-    }
-
-    public final CatalogProtos.StoreType getStoreType() {
-      return desc.getMeta().getStoreType();
-    }
-
-    public final Schema getSchema() {
-      return desc.getMeta().getSchema();
-    }
-
-    public final void setAlias(String alias) {
-      this.alias = alias;
-    }
-
-    public final String getAlias() {
-      return alias;
-    }
-
-    public final boolean hasAlias() {
-      return alias != null;
-    }
-
-    public final String toString() {
-      if (alias != null)
-        return desc.getId() + " as " + alias;
-      else
-        return desc.getId();
-    }
-
-    public boolean equals(Object obj) {
-      if (obj instanceof FromTable) {
-        FromTable other = (FromTable) obj;
-        return this.desc.equals(other.desc)
-            && TUtil.checkEquals(this.alias, other.alias);
-      } else {
-        return false;
-      }
-    }
-
-    @Override
-    public Object clone() throws CloneNotSupportedException {
-      FromTable table = (FromTable) super.clone();
-      table.desc = (TableDesc) desc.clone();
-      table.alias = alias;
-
-      return table;
-    }
-
-    public String toJSON() {
-      desc.initFromProto();
-      Gson gson = GsonCreator.getInstance();
-      return gson.toJson(this, FromTable.class);
-    }
-  }
-
-  public static class LimitClause implements Cloneable {
-    @Expose private long fetchFirstNum;
-
-    public LimitClause(long fetchFirstNum) {
-      this.fetchFirstNum = fetchFirstNum;
-    }
-
-    public long getLimitRow() {
-      return this.fetchFirstNum;
-    }
-
-    @Override
-    public String toString() {
-      return "LIMIT " + fetchFirstNum;
-    }
-
-    public boolean equals(Object obj) {
-      return obj instanceof LimitClause &&
-          fetchFirstNum == ((LimitClause)obj).fetchFirstNum;
-    }
-
-    @Override
-    public Object clone() throws CloneNotSupportedException {
-      LimitClause newLimit = (LimitClause) super.clone();
-      newLimit.fetchFirstNum = fetchFirstNum;
-      return newLimit;
-    }
-  }
-}
\ No newline at end of file


[13/13] git commit: TAJO-87: Integration of tajo algebra module and SQL parser

Posted by hy...@apache.org.
TAJO-87: Integration of tajo algebra module and SQL parser


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

Branch: refs/heads/master
Commit: 00c3ee2bdb9dd047485ab6ec52cd2284f0b1980a
Parents: ed25aa8
Author: Hyunsik Choi <hy...@apache.org>
Authored: Fri Jul 26 18:33:54 2013 +0900
Committer: Hyunsik Choi <hy...@apache.org>
Committed: Mon Jul 29 12:01:11 2013 +0900

----------------------------------------------------------------------
 CHANGES.txt                                     |    2 +
 pom.xml                                         |    7 +-
 .../org/apache/tajo/algebra/Aggregation.java    |   24 +-
 .../org/apache/tajo/algebra/BinaryOperator.java |    8 +-
 .../org/apache/tajo/algebra/CaseWhenExpr.java   |   88 --
 .../apache/tajo/algebra/CaseWhenPredicate.java  |   89 ++
 .../tajo/algebra/ColumnReferenceExpr.java       |   34 +-
 .../tajo/algebra/CountRowsFunctionExpr.java     |   21 +
 .../tajo/algebra/CountValueFunctionExpr.java    |   21 +
 .../org/apache/tajo/algebra/CreateTable.java    |   71 +-
 .../java/org/apache/tajo/algebra/DropTable.java |   41 +
 .../main/java/org/apache/tajo/algebra/Expr.java |   34 +-
 .../java/org/apache/tajo/algebra/ExprType.java  |   99 --
 .../org/apache/tajo/algebra/ExprVisitor.java    |   19 +
 .../org/apache/tajo/algebra/FunctionExpr.java   |   32 +-
 .../java/org/apache/tajo/algebra/Having.java    |   56 +
 .../org/apache/tajo/algebra/InPredicate.java    |   34 +
 .../apache/tajo/algebra/IsNullPredicate.java    |   44 +
 .../main/java/org/apache/tajo/algebra/Join.java |   44 +-
 .../org/apache/tajo/algebra/JsonHelper.java     |    2 +-
 .../java/org/apache/tajo/algebra/LikeExpr.java  |   51 -
 .../org/apache/tajo/algebra/LikePredicate.java  |   51 +
 .../java/org/apache/tajo/algebra/Limit.java     |   12 +-
 .../org/apache/tajo/algebra/LiteralExpr.java    |   53 -
 .../org/apache/tajo/algebra/LiteralValue.java   |   53 +
 .../java/org/apache/tajo/algebra/NotExpr.java   |   31 +
 .../java/org/apache/tajo/algebra/NullValue.java |   31 +
 .../java/org/apache/tajo/algebra/OpType.java    |  117 ++
 .../org/apache/tajo/algebra/Projection.java     |   20 +-
 .../java/org/apache/tajo/algebra/Relation.java  |   18 +-
 .../org/apache/tajo/algebra/RelationList.java   |   62 +
 .../org/apache/tajo/algebra/ScalarSubQuery.java |    3 +-
 .../java/org/apache/tajo/algebra/Selection.java |   17 +-
 .../org/apache/tajo/algebra/SetOperation.java   |    9 +-
 .../main/java/org/apache/tajo/algebra/Sort.java |   26 +-
 .../org/apache/tajo/algebra/TableSubQuery.java  |   15 +-
 .../java/org/apache/tajo/algebra/Target.java    |   15 +-
 .../org/apache/tajo/algebra/UnaryOperator.java  |    6 +-
 .../org/apache/tajo/algebra/ValueListExpr.java  |   40 +
 .../java/org/apache/tajo/algebra/TestExpr.java  |   86 +-
 .../java/org/apache/tajo/catalog/Options.java   |    6 +
 .../java/org/apache/tajo/catalog/Schema.java    |    2 +-
 .../org/apache/tajo/catalog/CatalogServer.java  |   10 +-
 .../apache/tajo/annotation/NotThreadSafe.java   |   30 +
 .../org/apache/tajo/annotation/ThreadSafe.java  |   30 +
 .../main/java/org/apache/tajo/util/TUtil.java   |   33 +-
 tajo-core/tajo-core-backend/pom.xml             |   20 +-
 .../org/apache/tajo/engine/parser/SQLLexer.g    |  391 -----
 .../org/apache/tajo/engine/parser/SQLParser.g   |  789 ----------
 .../org/apache/tajo/engine/parser/SQLLexer.g4   |  367 +++++
 .../org/apache/tajo/engine/parser/SQLParser.g4  |  788 ++++++++++
 .../java/org/apache/tajo/benchmark/TPCH.java    |    2 +-
 .../java/org/apache/tajo/client/TajoClient.java |   21 +-
 .../apache/tajo/engine/eval/EvalTreeUtil.java   |   46 +-
 .../engine/exception/NTAQueryException.java     |   42 -
 .../engine/function/builtin/CountValue.java     |    5 +-
 .../tajo/engine/json/FromTableDeserializer.java |    2 +-
 .../apache/tajo/engine/parser/CommandType.java  |   41 -
 .../org/apache/tajo/engine/parser/CopyStmt.java |   72 -
 .../tajo/engine/parser/CreateIndexStmt.java     |  107 --
 .../tajo/engine/parser/CreateTableStmt.java     |  134 --
 .../tajo/engine/parser/DropTableStmt.java       |   34 -
 .../apache/tajo/engine/parser/ParseTree.java    |   63 -
 .../apache/tajo/engine/parser/ParseUtil.java    |   76 -
 .../tajo/engine/parser/ParserException.java     |   54 -
 .../tajo/engine/parser/QueryAnalyzer.java       | 1347 ------------------
 .../apache/tajo/engine/parser/QueryBlock.java   |  586 --------
 .../apache/tajo/engine/parser/SQLAnalyzer.java  |  900 ++++++++++++
 .../org/apache/tajo/engine/parser/SetStmt.java  |   60 -
 .../tajo/engine/parser/StatementType.java       |   53 -
 .../org/apache/tajo/engine/parser/TableMap.java |   68 -
 .../apache/tajo/engine/planner/FromTable.java   |  105 ++
 .../tajo/engine/planner/GroupElement.java       |   65 +
 .../apache/tajo/engine/planner/JoinClause.java  |  126 ++
 .../apache/tajo/engine/planner/JoinType.java    |   28 -
 .../tajo/engine/planner/LogicalOptimizer.java   |  754 ++++------
 .../apache/tajo/engine/planner/LogicalPlan.java |  564 ++++++++
 .../tajo/engine/planner/LogicalPlanner.java     | 1333 ++++++++++-------
 .../engine/planner/NoSuchColumnException.java   |   25 +
 .../engine/planner/PhysicalPlannerImpl.java     |   13 -
 .../apache/tajo/engine/planner/PlannerUtil.java |  206 ++-
 .../tajo/engine/planner/PlanningContext.java    |   40 -
 .../engine/planner/PlanningContextImpl.java     |   94 --
 .../apache/tajo/engine/planner/Projector.java   |    3 -
 .../org/apache/tajo/engine/planner/Target.java  |  115 ++
 .../tajo/engine/planner/TargetListManager.java  |  168 +++
 .../tajo/engine/planner/VerifyException.java    |   25 +
 .../engine/planner/logical/CreateTableNode.java |    2 +-
 .../engine/planner/logical/DropTableNode.java   |   71 +
 .../engine/planner/logical/EvalExprNode.java    |   27 +-
 .../tajo/engine/planner/logical/ExprType.java   |    3 +-
 .../engine/planner/logical/GroupbyNode.java     |   29 +-
 .../engine/planner/logical/IndexWriteNode.java  |   95 --
 .../tajo/engine/planner/logical/JoinNode.java   |   43 +-
 .../tajo/engine/planner/logical/LimitNode.java  |   25 +-
 .../engine/planner/logical/LogicalNode.java     |   12 +-
 .../engine/planner/logical/Projectable.java     |   27 +
 .../engine/planner/logical/ProjectionNode.java  |   24 +-
 .../tajo/engine/planner/logical/ScanNode.java   |   33 +-
 .../planner/physical/AggregationExec.java       |    4 +-
 .../engine/planner/physical/BNLJoinExec.java    |    4 +-
 .../engine/planner/physical/EvalExprExec.java   |    2 +-
 .../engine/planner/physical/IndexWriteExec.java |   88 --
 .../engine/planner/physical/NLJoinExec.java     |    4 +
 .../apache/tajo/engine/utils/SchemaUtil.java    |   33 +-
 .../org/apache/tajo/master/ClientService.java   |    7 +-
 .../org/apache/tajo/master/ExecutionBlock.java  |    6 +-
 .../org/apache/tajo/master/GlobalEngine.java    |   82 +-
 .../org/apache/tajo/master/GlobalPlanner.java   |   42 +-
 .../apache/tajo/master/GlobalPlannerUtils.java  |  164 +--
 .../main/java/org/apache/tajo/master/Query.java |   29 +-
 .../java/org/apache/tajo/master/QueryUnit.java  |    7 +-
 .../org/apache/tajo/BackendTestingUtil.java     |   17 +-
 .../org/apache/tajo/benchmark/TestTPCH.java     |    8 +-
 .../apache/tajo/engine/eval/TestEvalTree.java   |   75 +-
 .../tajo/engine/eval/TestEvalTreeUtil.java      |   99 +-
 .../tajo/engine/parser/TestQueryAnalyzer.java   |  653 ---------
 .../tajo/engine/parser/TestSQLAnalyzer.java     |  297 ++++
 .../tajo/engine/parser/TestSQLParser.java       |  993 -------------
 .../plan/global/TestGlobalQueryPlanner.java     |   84 +-
 .../engine/planner/TestLogicalOptimizer.java    |   60 +-
 .../tajo/engine/planner/TestLogicalPlanner.java |  448 +++---
 .../tajo/engine/planner/TestPlannerUtil.java    |   25 +-
 .../global/TestGlobalQueryOptimizer.java        |   26 +-
 .../planner/physical/TestBNLJoinExec.java       |   32 +-
 .../planner/physical/TestBSTIndexExec.java      |   21 +-
 .../planner/physical/TestExternalSortExec.java  |   25 +-
 .../planner/physical/TestHashJoinExec.java      |   22 +-
 .../planner/physical/TestMergeJoinExec.java     |   18 +-
 .../engine/planner/physical/TestNLJoinExec.java |   21 +-
 .../planner/physical/TestPhysicalPlanner.java   |  295 ++--
 .../engine/planner/physical/TestSortExec.java   |   23 +-
 .../apache/tajo/engine/query/TestJoinQuery.java |   10 +-
 .../tajo/engine/query/TestSelectQuery.java      |   18 +-
 .../apache/tajo/engine/query/TestSortQuery.java |   19 +
 .../tajo/master/TestExecutionBlockCursor.java   |   21 +-
 .../tajo/worker/TestRangeRetrieverHandler.java  |   23 +-
 .../src/test/queries/create_table_1.sql         |    1 +
 .../src/test/queries/create_table_10.sql        |    1 +
 .../src/test/queries/create_table_2.sql         |    1 +
 .../src/test/queries/create_table_3.sql         |    1 +
 .../src/test/queries/create_table_4.sql         |    1 +
 .../src/test/queries/create_table_5.sql         |    1 +
 .../src/test/queries/create_table_6.sql         |    1 +
 .../src/test/queries/create_table_7.sql         |    1 +
 .../src/test/queries/create_table_8.sql         |   48 +
 .../src/test/queries/create_table_9.sql         |    1 +
 .../src/test/queries/drop_table.sql             |    1 +
 .../src/test/queries/groupby_1.sql              |    1 +
 .../src/test/queries/insert_1.sql               |    1 +
 .../src/test/queries/insert_2.sql               |    1 +
 .../src/test/queries/join_1.sql                 |    1 +
 .../src/test/queries/join_10.sql                |    1 +
 .../src/test/queries/join_11.sql                |    1 +
 .../src/test/queries/join_2.sql                 |    1 +
 .../src/test/queries/join_3.sql                 |    1 +
 .../src/test/queries/join_4.sql                 |    1 +
 .../src/test/queries/join_5.sql                 |    1 +
 .../src/test/queries/join_6.sql                 |    1 +
 .../src/test/queries/join_7.sql                 |    1 +
 .../src/test/queries/join_8.sql                 |   13 +
 .../src/test/queries/join_9.sql                 |    5 +
 .../src/test/queries/select_1.sql               |    1 +
 .../src/test/queries/select_2.sql               |    1 +
 .../src/test/queries/select_3.sql               |    1 +
 .../src/test/queries/select_4.sql               |    1 +
 .../src/test/queries/select_5.sql               |    1 +
 .../src/test/queries/select_6.sql               |    1 +
 .../src/test/queries/select_7.sql               |    1 +
 .../src/test/queries/select_8.sql               |    1 +
 .../src/test/queries/set_1.sql                  |    1 +
 .../src/test/queries/set_2.sql                  |    1 +
 .../src/test/queries/set_3.sql                  |    1 +
 tajo-core/tajo-core-storage/pom.xml             |   11 -
 tajo-frontend/pom.xml                           |  149 --
 tajo-frontend/tajo-frontend-sql/pom.xml         |  187 ---
 .../main/antlr3/tajo/frontend/sql/SQLLexer.g    | 1152 ---------------
 .../main/antlr3/tajo/frontend/sql/SQLParser.g   |  956 -------------
 .../apache/tajo/frontend/sql/QueryBlock.java    |  133 --
 .../apache/tajo/frontend/sql/SQLAnalyzer.java   |  932 ------------
 .../apache/tajo/frontend/sql/SQLParseError.java |   27 -
 .../tajo/frontend/sql/SQLSyntaxError.java       |   45 -
 .../tajo/frontend/sql/TestSQLAnalyzer.java      |  572 --------
 .../apache/tajo/frontend/sql/TestSQLParser.java |  914 ------------
 tajo-project/pom.xml                            |   12 +-
 185 files changed, 7106 insertions(+), 13608 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 988324e..2c984ea 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -12,6 +12,8 @@ Release 0.2.0 - unreleased
 
   IMPROVEMENTS
 
+    TAJO-87: Integration of tajo algebra module and SQL parser. (hyunsik)
+
     TAJO-70: Refactor GlobalEngine to handle DDL statements. (hyunsik)
 
     TAJO-86: Improved error messages in query analyzer. (jinho)

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 604ff83..e1e7b7d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -80,7 +80,6 @@
     <module>tajo-project</module>
     <module>tajo-common</module>
     <module>tajo-algebra</module>
-    <module>tajo-frontend</module>
     <module>tajo-core</module>
     <module>tajo-rpc</module>
     <module>tajo-catalog</module>
@@ -249,11 +248,6 @@
             </lifecycleMappingMetadata>
           </configuration>
         </plugin>
-        <plugin>
-          <groupId>org.antlr</groupId>
-          <artifactId>antlr3-maven-plugin</artifactId>
-          <version>3.4</version>
-        </plugin>
       </plugins>
     </pluginManagement>
 
@@ -304,6 +298,7 @@
           <excludes>
             <exclude>CHANGES.txt</exclude>
             <exclude>**/*.tql</exclude>
+            <exclude>**/*.sql</exclude>
             <exclude>**/*.schema</exclude>
             <exclude>**/*.tbl</exclude>
             <!-- generated content -->

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Aggregation.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Aggregation.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Aggregation.java
index a70956b..eccb873 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Aggregation.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Aggregation.java
@@ -21,19 +21,19 @@ package org.apache.tajo.algebra;
 import org.apache.tajo.util.TUtil;
 
 public class Aggregation extends UnaryOperator {
-  private Target [] targets;
+  private Target[] targets;
   private GroupElement [] groups;
   private Expr havingCondition;
 
   public Aggregation() {
-    super(ExprType.Aggregation);
+    super(OpType.Aggregation);
   }
 
-  public Target [] getTargets() {
+  public Target[] getTargets() {
     return this.targets;
   }
 
-  public void setTargets(Target [] targets) {
+  public void setTargets(Target[] targets) {
     this.targets = targets;
   }
 
@@ -109,8 +109,18 @@ public class Aggregation extends UnaryOperator {
   }
 
   public static enum GroupType {
-    GROUPBY,
-    CUBE,
-    ROLLUP
+    OrdinaryGroup(""),
+    Cube("Cube"),
+    Rollup("Rollup"),
+    EmptySet("()");
+
+    String displayName;
+    GroupType(String displayName) {
+      this.displayName = displayName;
+    }
+
+    public String toString() {
+      return displayName;
+    }
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/BinaryOperator.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/BinaryOperator.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/BinaryOperator.java
index 1883df2..3e00e5e 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/BinaryOperator.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/BinaryOperator.java
@@ -19,14 +19,14 @@
 package org.apache.tajo.algebra;
 
 public class BinaryOperator extends Expr {
-  Expr left;
-  Expr right;
+  protected Expr left;
+  protected Expr right;
 
-  BinaryOperator(ExprType opType) {
+  BinaryOperator(OpType opType) {
     super(opType);
   }
 
-  public BinaryOperator(ExprType type, Expr left, Expr right) {
+  public BinaryOperator(OpType type, Expr left, Expr right) {
     super(type);
     this.left = left;
     this.right = right;

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CaseWhenExpr.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/CaseWhenExpr.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CaseWhenExpr.java
deleted file mode 100644
index 5e95b8a..0000000
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/CaseWhenExpr.java
+++ /dev/null
@@ -1,88 +0,0 @@
-/**
- * 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.algebra;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-
-public class CaseWhenExpr extends Expr {
-  private List<WhenExpr> whens = new ArrayList<WhenExpr>();
-  private Expr else_result;
-
-  public CaseWhenExpr() {
-    super(ExprType.CaseWhen);
-  }
-
-  public void addWhen(Expr condition, Expr result) {
-    whens.add(new WhenExpr(condition, result));
-  }
-
-  public Collection<WhenExpr> getWhens() {
-    return this.whens;
-  }
-
-  public void setElseResult(Expr else_result) {
-    this.else_result = else_result;
-  }
-
-  public Object getElseResult() {
-    return this.else_result;
-  }
-
-  public boolean hasElseResult() {
-    return else_result != null;
-  }
-
-  @Override
-  boolean equalsTo(Expr expr) {
-    return false;
-  }
-
-  @Override
-  public String toJson() {
-    return JsonHelper.toJson(this);
-  }
-
-  public static class WhenExpr {
-    Expr condition;
-    Expr result;
-
-    public WhenExpr(Expr condition, Expr result) {
-      this.condition = condition;
-      this.result = result;
-    }
-
-    public Expr getCondition() {
-      return this.condition;
-    }
-
-    public Expr getResult() {
-      return this.result;
-    }
-
-    public boolean equals(Object obj) {
-      if (obj instanceof WhenExpr) {
-        WhenExpr another = (WhenExpr) obj;
-      }
-
-      return false;
-    }
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CaseWhenPredicate.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/CaseWhenPredicate.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CaseWhenPredicate.java
new file mode 100644
index 0000000..398aa55
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CaseWhenPredicate.java
@@ -0,0 +1,89 @@
+/**
+ * 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.algebra;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+public class CaseWhenPredicate extends Expr {
+  private List<WhenExpr> whens = new ArrayList<WhenExpr>();
+  private Expr elseResult;
+
+  public CaseWhenPredicate() {
+    super(OpType.CaseWhen);
+  }
+
+  public void addWhen(Expr condition, Expr result) {
+    whens.add(new WhenExpr(condition, result));
+  }
+
+  public Collection<WhenExpr> getWhens() {
+    return this.whens;
+  }
+
+  public void setElseResult(Expr elseResult) {
+    this.elseResult = elseResult;
+  }
+
+  public Expr getElseResult() {
+    return this.elseResult;
+  }
+
+  public boolean hasElseResult() {
+    return elseResult != null;
+  }
+
+  @Override
+  boolean equalsTo(Expr expr) {
+    return false;
+  }
+
+  @Override
+  public String toJson() {
+    return JsonHelper.toJson(this);
+  }
+
+  public static class WhenExpr {
+    Expr condition;
+    Expr result;
+
+    public WhenExpr(Expr condition, Expr result) {
+      this.condition = condition;
+      this.result = result;
+    }
+
+    public Expr getCondition() {
+      return this.condition;
+    }
+
+    public Expr getResult() {
+      return this.result;
+    }
+
+    public boolean equals(Object obj) {
+      if (obj instanceof WhenExpr) {
+        WhenExpr another = (WhenExpr) obj;
+        return condition.equals(another.condition) && result.equals(result);
+      }
+
+      return false;
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ColumnReferenceExpr.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/ColumnReferenceExpr.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ColumnReferenceExpr.java
index 888404d..dfc2584 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/ColumnReferenceExpr.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ColumnReferenceExpr.java
@@ -21,29 +21,41 @@ package org.apache.tajo.algebra;
 import org.apache.tajo.util.TUtil;
 
 public class ColumnReferenceExpr extends Expr {
-  private String rel_name;
-  private String column_name;
+  private String tableName;
+  private String name;
 
   public ColumnReferenceExpr(String columnName) {
-    super(ExprType.Column);
-    this.column_name = columnName;
+    super(OpType.Column);
+    this.name = columnName;
   }
 
-  public void setRelationName(String tableName) {
-    this.rel_name = tableName;
+  public void setTableName(String tableName) {
+    this.tableName = tableName;
   }
 
   public String getName() {
-    return this.column_name;
+    return this.name;
   }
 
-  public String getRelationName() {
-    return this.rel_name;
+  public boolean hasTableName() {
+    return this.tableName != null;
+  }
+
+  public String getTableName() {
+    return this.tableName;
+  }
+
+  public String getCanonicalName() {
+    if (tableName != null) {
+      return tableName + "." + name;
+    } else {
+      return name;
+    }
   }
 
   public boolean equalsTo(Expr expr) {
     ColumnReferenceExpr another = (ColumnReferenceExpr) expr;
-    return column_name.equals(another.column_name) &&
-        TUtil.checkEquals(rel_name, another.rel_name);
+    return name.equals(another.name) &&
+        TUtil.checkEquals(tableName, another.tableName);
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CountRowsFunctionExpr.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/CountRowsFunctionExpr.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CountRowsFunctionExpr.java
new file mode 100644
index 0000000..77468fb
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CountRowsFunctionExpr.java
@@ -0,0 +1,21 @@
+/*
+ * Licensed 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.algebra;
+
+public class CountRowsFunctionExpr extends FunctionExpr {
+  public CountRowsFunctionExpr() {
+    super(OpType.CountRowsFunction, "count");
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CountValueFunctionExpr.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/CountValueFunctionExpr.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CountValueFunctionExpr.java
new file mode 100644
index 0000000..80c527b
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CountValueFunctionExpr.java
@@ -0,0 +1,21 @@
+/*
+ * Licensed 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.algebra;
+
+public class CountValueFunctionExpr extends FunctionExpr {
+  public CountValueFunctionExpr(Expr param) {
+    super(OpType.CountValueFunction, "count", new Expr[] {param});
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CreateTable.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/CreateTable.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CreateTable.java
index 92404ee..1552467 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/CreateTable.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/CreateTable.java
@@ -23,25 +23,34 @@ import org.apache.tajo.util.TUtil;
 import java.util.Map;
 
 public class CreateTable extends Expr {
-  private String rel_name;
-  private ColumnDefinition [] table_elements;
-  private String storage_type;
+  private boolean external = false;
+  private String tableName;
+  private ColumnDefinition [] tableElements;
+  private String storageType;
   private String location;
   private Expr subquery;
   private Map<String, String> params;
 
   public CreateTable(final String tableName) {
-    super(ExprType.CreateTable);
-    this.rel_name = tableName;
+    super(OpType.CreateTable);
+    this.tableName = tableName;
   }
 
-  public CreateTable(final String relationName, final Expr subQuery) {
-    this(relationName);
+  public CreateTable(final String tableName, final Expr subQuery) {
+    this(tableName);
     this.subquery = subQuery;
   }
 
-  public String getRelationName() {
-    return this.rel_name;
+  public void setExternal() {
+    external = true;
+  }
+
+  public boolean isExternal() {
+    return external;
+  }
+
+  public String getTableName() {
+    return this.tableName;
   }
 
   public boolean hasLocation() {
@@ -57,27 +66,27 @@ public class CreateTable extends Expr {
   }
 
   public boolean hasTableElements() {
-    return this.table_elements != null;
+    return this.tableElements != null;
   }
 
   public ColumnDefinition [] getTableElements() {
-    return table_elements;
+    return tableElements;
   }
 
   public void setTableElements(ColumnDefinition [] tableElements) {
-    this.table_elements = tableElements;
+    this.tableElements = tableElements;
   }
 
   public boolean hasStorageType() {
-    return storage_type != null;
+    return storageType != null;
   }
 
   public void setStorageType(String storageType) {
-    this.storage_type = storageType;
+    this.storageType = storageType;
   }
 
   public String getStorageType() {
-    return storage_type;
+    return storageType;
   }
 
   public boolean hasParams() {
@@ -96,6 +105,10 @@ public class CreateTable extends Expr {
     return subquery != null;
   }
 
+  public void setSubQuery(Expr subquery) {
+    this.subquery = subquery;
+  }
+
   public Expr getSubQuery() {
     return subquery;
   }
@@ -103,9 +116,9 @@ public class CreateTable extends Expr {
   @Override
   boolean equalsTo(Expr expr) {
     CreateTable another = (CreateTable) expr;
-    return rel_name.equals(another.rel_name) &&
-        TUtil.checkEquals(table_elements, another.table_elements) &&
-        TUtil.checkEquals(storage_type, another.storage_type) &&
+    return tableName.equals(another.tableName) &&
+        TUtil.checkEquals(tableElements, another.tableElements) &&
+        TUtil.checkEquals(storageType, another.storageType) &&
         TUtil.checkEquals(location, another.location) &&
         TUtil.checkEquals(subquery, another.subquery) &&
         TUtil.checkEquals(params, another.params);
@@ -114,6 +127,8 @@ public class CreateTable extends Expr {
   public static class ColumnDefinition {
     String col_name;
     String data_type;
+    Integer length_or_precision;
+    Integer scale;
 
     public ColumnDefinition(String columnName, String dataType) {
       this.col_name = columnName;
@@ -128,10 +143,28 @@ public class CreateTable extends Expr {
       return this.data_type;
     }
 
+    public void setLengthOrPrecision(int lengthOrPrecision) {
+      this.length_or_precision = lengthOrPrecision;
+    }
+
+    public Integer getLengthOrPrecision() {
+      return this.length_or_precision;
+    }
+
+    public void setScale(int scale) {
+      this.scale = scale;
+    }
+
+    public Integer getScale() {
+      return this.scale;
+    }
+
     public boolean equals(Object obj) {
       if (obj instanceof ColumnDefinition) {
         ColumnDefinition another = (ColumnDefinition) obj;
-        return col_name.equals(another.col_name) && data_type.equals(another.data_type);
+        return col_name.equals(another.col_name) && data_type.equals(another.data_type) &&
+            TUtil.checkEquals(length_or_precision, another.length_or_precision) &&
+            TUtil.checkEquals(scale, another.scale);
       }
 
       return false;

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/DropTable.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/DropTable.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/DropTable.java
new file mode 100644
index 0000000..9544cf3
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/DropTable.java
@@ -0,0 +1,41 @@
+/**
+ * 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.algebra;
+
+public class DropTable extends Expr {
+  private final String tableName;
+
+  public DropTable(String tableName) {
+    super(OpType.DropTable);
+    this.tableName = tableName;
+  }
+
+  public String getTableName() {
+    return this.tableName;
+  }
+
+  @Override
+  boolean equalsTo(Expr expr) {
+    if (expr instanceof DropTable) {
+      DropTable another = (DropTable) expr;
+      return tableName.equals(another.tableName);
+    }
+    return false;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Expr.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Expr.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Expr.java
index 041ce1d..54c230b 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Expr.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Expr.java
@@ -23,14 +23,14 @@ import com.google.gson.*;
 import java.lang.reflect.Type;
 
 public abstract class Expr implements JsonSerializable {
-  protected ExprType op_type;
+  protected OpType opType;
 
-	public Expr(ExprType op_type) {
-		this.op_type = op_type;
+	public Expr(OpType opType) {
+		this.opType = opType;
 	}
 	
-	public ExprType getType() {
-		return this.op_type;
+	public OpType getType() {
+		return this.opType;
 	}
 
   abstract boolean equalsTo(Expr expr);
@@ -40,7 +40,7 @@ public abstract class Expr implements JsonSerializable {
 	  if (obj instanceof Expr) {
 	    Expr casted = (Expr) obj;
 
-      if (this.op_type == casted.op_type && equalsTo(casted)) {
+      if (this.opType == casted.opType && equalsTo(casted)) {
         if (this instanceof UnaryOperator) {
           UnaryOperator one = (UnaryOperator) this;
           UnaryOperator another = (UnaryOperator) casted;
@@ -76,6 +76,24 @@ public abstract class Expr implements JsonSerializable {
     return JsonHelper.toJson(this);
   }
 
+  /**
+   * This method provides a visiting way in the post order.
+   *
+   * @param visitor
+   */
+  public void accept(ExprVisitor visitor) {
+    if (this instanceof UnaryOperator) {
+      UnaryOperator unary = (UnaryOperator) this;
+      unary.getChild().accept(visitor);
+    } else if (this instanceof BinaryOperator) {
+      BinaryOperator bin = (BinaryOperator) this;
+      bin.getLeft().accept(visitor);
+      bin.getRight().accept(visitor);
+    }
+
+    visitor.visit(this);
+  }
+
   static class JsonSerDer
       implements JsonSerializer<Expr>, JsonDeserializer<Expr> {
 
@@ -85,14 +103,14 @@ public abstract class Expr implements JsonSerializable {
         throws JsonParseException {
       JsonObject jsonObject = json.getAsJsonObject();
       String operator = jsonObject.get("type").getAsString();
-      return context.deserialize(json, ExprType.valueOf(operator).getBaseClass());
+      return context.deserialize(json, OpType.valueOf(operator).getBaseClass());
     }
 
 
     @Override
     public JsonElement serialize(Expr src, Type typeOfSrc,
                                  JsonSerializationContext context) {
-      return context.serialize(src, src.op_type.getBaseClass());
+      return context.serialize(src, src.opType.getBaseClass());
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ExprType.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/ExprType.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ExprType.java
deleted file mode 100644
index f94e567..0000000
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/ExprType.java
+++ /dev/null
@@ -1,99 +0,0 @@
-/**
- * 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.algebra;
-
-import com.google.gson.*;
-
-import java.lang.reflect.Type;
-
-public enum ExprType {
-  // relational operators
-  Relation(Relation.class),
-  TableSubQuery(TableSubQuery.class),
-  Except(SetOperation.class),
-	Aggregation(org.apache.tajo.algebra.Aggregation.class),
-  Intersect(SetOperation.class),
-  Join(Join.class),
-  Projection(Projection.class),
-  Rename,
-  Selection(Selection.class),
-  Sort(Sort.class),
-  Union(SetOperation.class),
-  Limit(Limit.class),
-
-  // extended relational operators
-  CreateTable(CreateTable.class),
-
-  // logical operators
-  And(BinaryOperator.class),
-  Or(BinaryOperator.class),
-
-  // comparison operators
-  Equals(BinaryOperator.class),
-  NotEquals(BinaryOperator.class),
-  LessThan(BinaryOperator.class),
-  LessThanOrEquals(BinaryOperator.class),
-  GreaterThan(BinaryOperator.class),
-  GreaterThanOrEquals(BinaryOperator.class),
-
-  // arithmetic operators
-  Plus(BinaryOperator.class),
-  Minus(BinaryOperator.class),
-  Multiply(BinaryOperator.class),
-  Divide(BinaryOperator.class),
-  Mod(BinaryOperator.class),
-
-  // other expressions
-  Column(ColumnReferenceExpr.class),
-  Function(FunctionExpr.class),
-  CaseWhen(CaseWhenExpr.class),
-  Like(LikeExpr.class),
-  Literal(LiteralExpr.class),
-  ScalarSubQuery(ScalarSubQuery.class);
-
-  private Class baseClass;
-
-  ExprType() {
-    this.baseClass = Expr.class;
-  }
-  ExprType(Class clazz) {
-    this.baseClass = clazz;
-  }
-
-  public Class getBaseClass() {
-    return this.baseClass;
-  }
-
-  public static class JsonSerDer implements JsonSerializer<ExprType>,
-                                            JsonDeserializer<ExprType> {
-
-    @Override
-    public JsonElement serialize(ExprType src, Type typeOfSrc,
-                                 JsonSerializationContext context) {
-      return new JsonPrimitive(src.name());
-    }
-
-    @Override
-    public ExprType deserialize(JsonElement json, Type typeOfT,
-                                      JsonDeserializationContext context)
-        throws JsonParseException {
-      return ExprType.valueOf(json.getAsString());
-    }
-  }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ExprVisitor.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/ExprVisitor.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ExprVisitor.java
new file mode 100644
index 0000000..d05c1db
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ExprVisitor.java
@@ -0,0 +1,19 @@
+/*
+ * Licensed 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.algebra;
+
+public interface ExprVisitor {
+  void visit(Expr expr);
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/FunctionExpr.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/FunctionExpr.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/FunctionExpr.java
index 3bab189..29188bf 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/FunctionExpr.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/FunctionExpr.java
@@ -25,7 +25,37 @@ public class FunctionExpr extends Expr {
   private Expr[] params;
 
   public FunctionExpr(String signature) {
-    super(ExprType.Function);
+    this(OpType.Function, signature);
+  }
+
+  public FunctionExpr(String signature, Expr [] params) {
+    this(OpType.Function, signature);
+    setParams(params);
+  }
+
+  protected FunctionExpr(OpType type, String signature) {
+    super(type);
+    if (type != OpType.Function && type != OpType.CountRowsFunction) {
+      throw new IllegalArgumentException("FunctionExpr cannot accept " + type + "type");
+    }
+    this.signature = signature;
+  }
+
+  protected FunctionExpr(OpType type, String signature, Expr [] params) {
+    super(type);
+    if (type != OpType.Function && type != OpType.CountValueFunction) {
+      throw new IllegalArgumentException("FunctionExpr cannot accept " + type + "type");
+    }
+    this.signature = signature;
+    setParams(params);
+  }
+
+  public String getSignature() {
+    return this.signature;
+  }
+
+  public Expr[] getParams() {
+    return params;
   }
 
   public void setParams(Expr[] params) {

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Having.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Having.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Having.java
new file mode 100644
index 0000000..d48537a
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Having.java
@@ -0,0 +1,56 @@
+/**
+ * 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.algebra;
+
+import org.apache.tajo.util.TUtil;
+
+public class Having extends UnaryOperator implements JsonSerializable {
+  private Expr qual;
+
+  public Having(Expr qual) {
+    super(OpType.Having);
+    this.qual = qual;
+  }
+
+  public boolean hasQual() {
+    return qual != null;
+  }
+
+  public void setQual(Expr expr) {
+    this.qual = expr;
+  }
+
+	public Expr getQual() {
+		return this.qual;
+	}
+
+  @Override
+  public String toJson() {
+    return JsonHelper.toJson(this);
+  }
+
+  @Override
+  public boolean equalsTo(Expr expr) {
+    if (expr instanceof Having) {
+      Having other = (Having) expr;
+      return TUtil.checkEquals(qual, other.qual);
+    }
+    return false;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/InPredicate.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/InPredicate.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/InPredicate.java
new file mode 100644
index 0000000..abdcc86
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/InPredicate.java
@@ -0,0 +1,34 @@
+/**
+ * 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.algebra;
+
+public class InPredicate extends BinaryOperator {
+
+  public InPredicate(Expr predicand, Expr in_values) {
+    super(OpType.InPredicate, predicand, in_values);
+  }
+
+  public Expr getPredicand() {
+    return left;
+  }
+
+  public Expr getInValue() {
+    return right;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/IsNullPredicate.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/IsNullPredicate.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/IsNullPredicate.java
new file mode 100644
index 0000000..610a81e
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/IsNullPredicate.java
@@ -0,0 +1,44 @@
+/**
+ * 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.algebra;
+
+public class IsNullPredicate extends Expr {
+  private final boolean not;
+  private final ColumnReferenceExpr columnRef;
+
+  public IsNullPredicate(boolean not, ColumnReferenceExpr column) {
+    super(OpType.IsNullPredicate);
+    this.not = not;
+    this.columnRef = column;
+  }
+
+  public boolean isNot() {
+    return not;
+  }
+
+  public ColumnReferenceExpr getColumnRef() {
+    return columnRef;
+  }
+
+  @Override
+  boolean equalsTo(Expr expr) {
+    IsNullPredicate nullPredicate = (IsNullPredicate) expr;
+    return not == nullPredicate.not && columnRef.equals(nullPredicate.columnRef);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Join.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Join.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Join.java
index c2316fd..51c84c2 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Join.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Join.java
@@ -20,43 +20,45 @@ package org.apache.tajo.algebra;
 
 import org.apache.tajo.util.TUtil;
 
+import java.util.Arrays;
+
 public class Join extends BinaryOperator {
-  private JoinType join_type;
-  private Expr join_qual;
-  private ColumnReferenceExpr [] join_columns;
+  private JoinType joinType;
+  private Expr joinQual;
+  private ColumnReferenceExpr[] joinColumns;
   private boolean natural = false;
 
   public Join(JoinType joinType) {
-    super(ExprType.Join);
-    this.join_type = joinType;
+    super(OpType.Join);
+    this.joinType = joinType;
   }
 
   public JoinType getJoinType() {
-    return  this.join_type;
+    return  this.joinType;
   }
 
   public boolean hasQual() {
-    return this.join_qual != null;
+    return this.joinQual != null;
   }
 
   public Expr getQual() {
-    return this.join_qual;
+    return this.joinQual;
   }
 
   public void setQual(Expr expr) {
-    this.join_qual = expr;
+    this.joinQual = expr;
   }
 
   public boolean hasJoinColumns() {
-    return join_columns != null;
+    return joinColumns != null;
   }
 
-  public ColumnReferenceExpr [] getJoinColumns() {
-    return join_columns;
+  public ColumnReferenceExpr[] getJoinColumns() {
+    return joinColumns;
   }
 
   public void setJoinColumns(ColumnReferenceExpr[] columns) {
-    join_columns = columns;
+    joinColumns = columns;
   }
 
   public void setNatural() {
@@ -69,9 +71,9 @@ public class Join extends BinaryOperator {
 
   boolean equalsTo(Expr expr) {
     Join another = (Join) expr;
-    return join_type.equals(another.join_type) &&
-        TUtil.checkEquals(join_qual, another.join_qual) &&
-        TUtil.checkEquals(join_columns, another.join_columns) &&
+    return joinType.equals(another.joinType) &&
+        TUtil.checkEquals(joinQual, another.joinQual) &&
+        TUtil.checkEquals(joinColumns, another.joinColumns) &&
         natural == another.natural;
   }
 
@@ -79,4 +81,14 @@ public class Join extends BinaryOperator {
   public String toJson() {
     return JsonHelper.toJson(this);
   }
+
+  @Override
+  public int hashCode() {
+    int result = getType().hashCode();
+    result = 31 * result + joinType.hashCode();
+    result = 31 * result + (joinQual != null ? joinQual.hashCode() : 0);
+    result = 31 * result + (joinColumns != null ? Arrays.hashCode(joinColumns) : 0);
+    result = 31 * result + (natural ? 1 : 0);
+    return result;
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/JsonHelper.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/JsonHelper.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/JsonHelper.java
index 5cf0376..f05a4ea 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/JsonHelper.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/JsonHelper.java
@@ -38,7 +38,7 @@ public class JsonHelper {
 
   private void initBuilder() {
     builder = new GsonBuilder().setPrettyPrinting();
-    builder.registerTypeAdapter(ExprType.class, new ExprType.JsonSerDer());
+    builder.registerTypeAdapter(OpType.class, new OpType.JsonSerDer());
     builder.registerTypeAdapter(Expr.class, new Expr.JsonSerDer());
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/LikeExpr.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/LikeExpr.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/LikeExpr.java
deleted file mode 100644
index ac73d74..0000000
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/LikeExpr.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/**
- * 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.algebra;
-
-public class LikeExpr extends BinaryOperator {
-  private boolean not;
-  private ColumnReferenceExpr column_ref;
-  private Expr pattern;
-
-  public LikeExpr(boolean not, ColumnReferenceExpr columnReferenceExpr, Expr pattern) {
-    super(ExprType.Like);
-    this.not = not;
-    this.column_ref = columnReferenceExpr;
-    this.pattern = pattern;
-  }
-
-  public boolean isNot() {
-    return not;
-  }
-
-  public ColumnReferenceExpr getColumnRef() {
-    return this.column_ref;
-  }
-
-  public Expr getPattern() {
-    return this.pattern;
-  }
-
-  boolean equalsTo(Expr expr) {
-    LikeExpr another = (LikeExpr) expr;
-    return not == another.not &&
-        column_ref.equals(another.column_ref) &&
-        pattern.equals(another.pattern);
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/LikePredicate.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/LikePredicate.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/LikePredicate.java
new file mode 100644
index 0000000..e091df1
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/LikePredicate.java
@@ -0,0 +1,51 @@
+/**
+ * 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.algebra;
+
+public class LikePredicate extends Expr {
+  private boolean not;
+  private ColumnReferenceExpr columnRef;
+  private Expr pattern;
+
+  public LikePredicate(boolean not, ColumnReferenceExpr columnReferenceExpr, Expr pattern) {
+    super(OpType.LikePredicate);
+    this.not = not;
+    this.columnRef = columnReferenceExpr;
+    this.pattern = pattern;
+  }
+
+  public boolean isNot() {
+    return not;
+  }
+
+  public ColumnReferenceExpr getColumnRef() {
+    return this.columnRef;
+  }
+
+  public Expr getPattern() {
+    return this.pattern;
+  }
+
+  boolean equalsTo(Expr expr) {
+    LikePredicate another = (LikePredicate) expr;
+    return not == another.not &&
+        columnRef.equals(another.columnRef) &&
+        pattern.equals(another.pattern);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Limit.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Limit.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Limit.java
index e3ded02..dd3aee9 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Limit.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Limit.java
@@ -20,15 +20,15 @@ package org.apache.tajo.algebra;
 
 
 public class Limit extends UnaryOperator {
-  private Expr fetch_first_num;
+  private Expr fetchFirstNum;
 
-  public Limit(Expr fetch_first_num) {
-    super(ExprType.Limit);
-    this.fetch_first_num = fetch_first_num;
+  public Limit(Expr fetchFirstNum) {
+    super(OpType.Limit);
+    this.fetchFirstNum = fetchFirstNum;
   }
 
   public Expr getFetchFirstNum() {
-    return this.fetch_first_num;
+    return this.fetchFirstNum;
   }
 
   @Override
@@ -44,6 +44,6 @@ public class Limit extends UnaryOperator {
   @Override
   boolean equalsTo(Expr expr) {
     Limit another = (Limit) expr;
-    return fetch_first_num.equals(another.fetch_first_num);
+    return fetchFirstNum.equals(another.fetchFirstNum);
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/LiteralExpr.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/LiteralExpr.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/LiteralExpr.java
deleted file mode 100644
index df8f4dd..0000000
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/LiteralExpr.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/**
- * 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.algebra;
-
-public class LiteralExpr extends Expr {
-  private String value;
-  private LiteralType value_type;
-
-  public static enum LiteralType {
-    String,
-    Unsigned_Integer,
-    Unsigned_Float,
-    Unsigned_Large_Integer,
-  }
-
-  public LiteralExpr(String value, LiteralType value_type) {
-    super(ExprType.Literal);
-    this.value = value;
-    this.value_type = value_type;
-  }
-
-  public String getValue() {
-    return this.value;
-  }
-
-  public LiteralType getValueType() {
-    return this.value_type;
-  }
-
-  public boolean equalsTo(Expr expr) {
-    LiteralExpr another = (LiteralExpr) expr;
-    boolean a = value_type.equals(another.value_type);
-    boolean b =  value.equals(another.value);
-
-    return a && b;
-  }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/LiteralValue.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/LiteralValue.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/LiteralValue.java
new file mode 100644
index 0000000..02445f0
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/LiteralValue.java
@@ -0,0 +1,53 @@
+/**
+ * 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.algebra;
+
+public class LiteralValue extends Expr {
+  private String value;
+  private LiteralType valueType;
+
+  public static enum LiteralType {
+    String,
+    Unsigned_Integer,
+    Unsigned_Float,
+    Unsigned_Large_Integer,
+  }
+
+  public LiteralValue(String value, LiteralType valueType) {
+    super(OpType.Literal);
+    this.value = value;
+    this.valueType = valueType;
+  }
+
+  public String getValue() {
+    return this.value;
+  }
+
+  public LiteralType getValueType() {
+    return this.valueType;
+  }
+
+  public boolean equalsTo(Expr expr) {
+    LiteralValue another = (LiteralValue) expr;
+    boolean a = valueType.equals(another.valueType);
+    boolean b =  value.equals(another.value);
+
+    return a && b;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/NotExpr.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/NotExpr.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/NotExpr.java
new file mode 100644
index 0000000..aba2f0e
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/NotExpr.java
@@ -0,0 +1,31 @@
+/**
+ * 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.algebra;
+
+public class NotExpr extends UnaryOperator {
+  public NotExpr(Expr child) {
+    super(OpType.Not);
+    setChild(child);
+  }
+
+  @Override
+  boolean equalsTo(Expr expr) {
+    return expr instanceof NotExpr;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/NullValue.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/NullValue.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/NullValue.java
new file mode 100644
index 0000000..9be1a2f
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/NullValue.java
@@ -0,0 +1,31 @@
+/*
+ * 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.algebra;
+
+public class NullValue extends Expr {
+
+  public NullValue() {
+    super(OpType.Null);
+  }
+
+  @Override
+  boolean equalsTo(Expr expr) {
+    return expr instanceof NullValue;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/OpType.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/OpType.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/OpType.java
new file mode 100644
index 0000000..7be2ea9
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/OpType.java
@@ -0,0 +1,117 @@
+/**
+ * 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.algebra;
+
+import com.google.gson.*;
+
+import java.lang.reflect.Type;
+
+public enum OpType {
+
+  // relational operators
+  Relation(Relation.class),
+  RelationList(RelationList.class),
+  TableSubQuery(TableSubQuery.class),
+  Except(SetOperation.class),
+  Having(Having.class),
+	Aggregation(Aggregation.class),
+  Intersect(SetOperation.class),
+  Join(Join.class),
+  Projection(Projection.class),
+  Rename,
+  Filter(Selection.class),
+  Sort(Sort.class),
+  Union(SetOperation.class),
+  Limit(Limit.class),
+
+  // Data definition language
+  CreateTable(CreateTable.class),
+  DropTable(DropTable.class),
+
+  // logical operators
+  And(BinaryOperator.class),
+  Or(BinaryOperator.class),
+  Not(NotExpr.class),
+
+  // comparison predicates
+  Equals(BinaryOperator.class),
+  NotEquals(BinaryOperator.class),
+  LessThan(BinaryOperator.class),
+  LessThanOrEquals(BinaryOperator.class),
+  GreaterThan(BinaryOperator.class),
+  GreaterThanOrEquals(BinaryOperator.class),
+
+  // other predicates
+  CaseWhen(CaseWhenPredicate.class),
+  IsNullPredicate(IsNullPredicate.class),
+  InPredicate(InPredicate.class),
+  LikePredicate(LikePredicate.class),
+  ValueList(ValueListExpr.class),
+  Is,
+
+  // arithmetic operators
+  Plus(BinaryOperator.class),
+  Minus(BinaryOperator.class),
+  Multiply(BinaryOperator.class),
+  Divide(BinaryOperator.class),
+  Modular(BinaryOperator.class),
+
+  // other expressions
+  Column(ColumnReferenceExpr.class),
+  Target(Target.class),
+  Function(FunctionExpr.class),
+
+  // set functions
+  CountRowsFunction(CountRowsFunctionExpr.class),
+  CountValueFunction(CountValueFunctionExpr.class),
+
+  Literal(LiteralValue.class),
+  Null(NullValue.class),
+  ScalarSubQuery(ScalarSubQuery.class);
+
+  private Class baseClass;
+
+  OpType() {
+    this.baseClass = Expr.class;
+  }
+  OpType(Class clazz) {
+    this.baseClass = clazz;
+  }
+
+  public Class getBaseClass() {
+    return this.baseClass;
+  }
+
+  public static class JsonSerDer implements JsonSerializer<OpType>,
+                                            JsonDeserializer<OpType> {
+
+    @Override
+    public JsonElement serialize(OpType src, Type typeOfSrc,
+                                 JsonSerializationContext context) {
+      return new JsonPrimitive(src.name());
+    }
+
+    @Override
+    public OpType deserialize(JsonElement json, Type typeOfT,
+                                      JsonDeserializationContext context)
+        throws JsonParseException {
+      return OpType.valueOf(json.getAsString());
+    }
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Projection.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Projection.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Projection.java
index bc66bc0..38520ae 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Projection.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Projection.java
@@ -21,13 +21,17 @@ package org.apache.tajo.algebra;
 import org.apache.tajo.util.TUtil;
 
 public class Projection extends UnaryOperator implements Cloneable {
-  private boolean project_all;
+  private boolean all;
   private boolean distinct = false;
 
-  private Target [] targets;
+  private Target[] targets;
 
   public Projection() {
-    super(ExprType.Projection);
+    super(OpType.Projection);
+  }
+
+  public int size() {
+    return targets.length;
   }
 
   public boolean isDistinct() {
@@ -39,25 +43,25 @@ public class Projection extends UnaryOperator implements Cloneable {
   }
 
   public void setAll() {
-    project_all = true;
+    all = true;
   }
 
   public boolean isAllProjected() {
-    return project_all;
+    return all;
   }
 	
-	public Target [] getTargets() {
+	public Target[] getTargets() {
 	  return this.targets;
 	}
 
-  public void setTargets(Target [] targets) {
+  public void setTargets(Target[] targets) {
     this.targets = targets;
   }
 
   @Override
   boolean equalsTo(Expr expr) {
     Projection another = (Projection) expr;
-    return TUtil.checkEquals(project_all, another.project_all) &&
+    return TUtil.checkEquals(all, another.all) &&
         TUtil.checkEquals(targets, another.targets);
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Relation.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Relation.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Relation.java
index df44b12..0cb0bbe 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Relation.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Relation.java
@@ -21,20 +21,24 @@ package org.apache.tajo.algebra;
 import org.apache.tajo.util.TUtil;
 
 public class Relation extends Expr {
-  private String rel_name;
+  private String tableName;
   private String alias;
 
-  protected Relation(ExprType type, String relationName) {
+  protected Relation(OpType type, String relationName) {
     super(type);
-    this.rel_name = relationName;
+    this.tableName = relationName;
   }
 
   public Relation(String relationName) {
-    this(ExprType.Relation, relationName);
+    this(OpType.Relation, relationName);
+  }
+
+  public String getCanonicalName() {
+    return alias == null ? tableName : alias;
   }
 
   public String getName() {
-    return rel_name;
+    return tableName;
   }
 
   public boolean hasAlias() {
@@ -57,13 +61,13 @@ public class Relation extends Expr {
   @Override
   boolean equalsTo(Expr expr) {
     Relation other = (Relation) expr;
-    return TUtil.checkEquals(rel_name, other.rel_name) &&
+    return TUtil.checkEquals(tableName, other.tableName) &&
         TUtil.checkEquals(alias, other.alias);
   }
 
   @Override
   public int hashCode() {
-    int result = rel_name.hashCode();
+    int result = tableName.hashCode();
     result = 31 * result + (alias != null ? alias.hashCode() : 0);
     return result;
   }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/RelationList.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/RelationList.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/RelationList.java
new file mode 100644
index 0000000..8e9f878
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/RelationList.java
@@ -0,0 +1,62 @@
+/**
+ * Licensed 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.algebra;
+
+import com.google.common.base.Preconditions;
+import org.apache.tajo.util.TUtil;
+
+import java.util.Set;
+
+public class RelationList extends Expr {
+  private Expr[] relations;
+
+  public RelationList(Expr[] relations) {
+    super(OpType.RelationList);
+    checkRelations(relations);
+    this.relations = relations;
+  }
+
+  private void checkRelations(Expr[] relations) {
+    for (Expr rel : relations) {
+      Preconditions.checkArgument(
+          rel.getType() == OpType.Relation ||
+          rel.getType() == OpType.Join ||
+          rel.getType() == OpType.TableSubQuery,
+          "Only Relation, Join, or TableSubQuery can be given to RelationList, but this expr "
+              + " is " + rel.getType());
+    }
+  }
+
+  public Expr[] getRelations() {
+    return this.relations;
+  }
+
+  public int size() {
+    return this.relations.length;
+  }
+
+  @Override
+  public String toString() {
+    return toJson();
+  }
+
+  @Override
+  boolean equalsTo(Expr expr) {
+    Set<Expr> thisSet = TUtil.newHashSet(relations);
+    RelationList another = (RelationList) expr;
+    Set<Expr> anotherSet = TUtil.newHashSet(another.relations);
+    return thisSet.equals(anotherSet);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ScalarSubQuery.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/ScalarSubQuery.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ScalarSubQuery.java
index f07cb31..2273a48 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/ScalarSubQuery.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ScalarSubQuery.java
@@ -20,8 +20,9 @@ package org.apache.tajo.algebra;
 
 public class ScalarSubQuery extends Expr {
   private Expr subquery;
+
   public ScalarSubQuery(Expr subQuery) {
-    super(ExprType.ScalarSubQuery);
+    super(OpType.ScalarSubQuery);
     this.subquery = subQuery;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Selection.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Selection.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Selection.java
index d9f68bc..f43c252 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Selection.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Selection.java
@@ -21,24 +21,23 @@ package org.apache.tajo.algebra;
 import org.apache.tajo.util.TUtil;
 
 public class Selection extends UnaryOperator implements JsonSerializable {
-  private Expr search_condition;
+  private Expr qual;
 
-  public Selection(Expr relation, Expr qual) {
-    super(ExprType.Selection);
-    setChild(relation);
-    search_condition = qual;
+  public Selection(Expr qual) {
+    super(OpType.Filter);
+    this.qual = qual;
   }
 
   public boolean hasQual() {
-    return search_condition != null;
+    return qual != null;
   }
 
   public void setQual(Expr expr) {
-    this.search_condition = expr;
+    this.qual = expr;
   }
 
 	public Expr getQual() {
-		return this.search_condition;
+		return this.qual;
 	}
 
   @Override
@@ -50,7 +49,7 @@ public class Selection extends UnaryOperator implements JsonSerializable {
   public boolean equalsTo(Expr expr) {
     if (expr instanceof Selection) {
       Selection other = (Selection) expr;
-      return TUtil.checkEquals(search_condition, other.search_condition);
+      return TUtil.checkEquals(qual, other.qual);
     }
     return false;
   }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/SetOperation.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/SetOperation.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/SetOperation.java
index 8a5ff9e..47ab44a 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/SetOperation.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/SetOperation.java
@@ -24,11 +24,12 @@ import com.google.common.base.Preconditions;
 public class SetOperation extends BinaryOperator {
   private boolean distinct = true;
 
-  public SetOperation(ExprType type, Expr left, Expr right, boolean distinct) {
+  public SetOperation(OpType type, Expr left, Expr right, boolean distinct) {
     super(type, left, right);
-    Preconditions.checkArgument(type == ExprType.Union ||
-        type == ExprType.Intersect ||
-        type == ExprType.Except);
+    Preconditions.checkArgument(type == OpType.Union ||
+        type == OpType.Intersect ||
+        type == OpType.Except);
+    this.distinct = distinct;
   }
 
   public boolean isDistinct() {

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Sort.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Sort.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Sort.java
index 7260e97..f57cd89 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Sort.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Sort.java
@@ -18,28 +18,29 @@
 
 package org.apache.tajo.algebra;
 
+import com.google.gson.annotations.SerializedName;
 import org.apache.tajo.util.TUtil;
 
 public class Sort extends UnaryOperator {
-  private SortSpec [] sort_specs;
+  private SortSpec [] sortSpecs;
 
   public Sort(final SortSpec [] sortSpecs) {
-    super(ExprType.Sort);
-    this.sort_specs = sortSpecs;
+    super(OpType.Sort);
+    this.sortSpecs = sortSpecs;
   }
 
-  public void setSortSpecs(SortSpec[] sort_specs) {
-    this.sort_specs = sort_specs;
+  public void setSortSpecs(SortSpec[] sortSpecs) {
+    this.sortSpecs = sortSpecs;
   }
 
   public SortSpec [] getSortSpecs() {
-    return this.sort_specs;
+    return this.sortSpecs;
   }
 
   @Override
   public boolean equalsTo(Expr expr) {
     Sort another = (Sort) expr;
-    return TUtil.checkEquals(sort_specs, another.sort_specs);
+    return TUtil.checkEquals(sortSpecs, another.sortSpecs);
   }
 
   @Override
@@ -50,7 +51,8 @@ public class Sort extends UnaryOperator {
   public static class SortSpec {
     private ColumnReferenceExpr key;
     private boolean asc = true;
-    private boolean null_first = false;
+    @SerializedName("null_first")
+    private boolean nullFirst = false;
 
     public SortSpec(final ColumnReferenceExpr key) {
       this.key = key;
@@ -67,7 +69,7 @@ public class Sort extends UnaryOperator {
                     final boolean nullFirst) {
       this(sortKey);
       this.asc = asc;
-      this.null_first = nullFirst;
+      this.nullFirst = nullFirst;
     }
 
     public final boolean isAscending() {
@@ -79,11 +81,11 @@ public class Sort extends UnaryOperator {
     }
 
     public final boolean isNullFirst() {
-      return this.null_first;
+      return this.nullFirst;
     }
 
     public final void setNullFirst() {
-      this.null_first = true;
+      this.nullFirst = true;
     }
 
     public final ColumnReferenceExpr getKey() {
@@ -95,7 +97,7 @@ public class Sort extends UnaryOperator {
         SortSpec other = (SortSpec) obj;
         return TUtil.checkEquals(key, other.key) &&
             TUtil.checkEquals(asc, other.asc) &&
-            TUtil.checkEquals(null_first, other.null_first);
+            TUtil.checkEquals(nullFirst, other.nullFirst);
       }
       return false;
     }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/TableSubQuery.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/TableSubQuery.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/TableSubQuery.java
index d46dc4a..55c44ba 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/TableSubQuery.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/TableSubQuery.java
@@ -20,12 +20,25 @@ package org.apache.tajo.algebra;
 
 public class TableSubQuery extends Relation {
   private Expr subquery;
+  private String [] columnNames;
 
   public TableSubQuery(String relName, Expr subquery) {
-    super(ExprType.TableSubQuery, relName);
+    super(OpType.TableSubQuery, relName);
     this.subquery = subquery;
   }
 
+  public boolean hasColumnNames() {
+    return this.columnNames != null;
+  }
+
+  public void setColumnNames(String[] aliasList) {
+    this.columnNames = aliasList;
+  }
+
+  public String [] getColumnNames() {
+    return columnNames;
+  }
+
   public Expr getSubQuery() {
     return subquery;
   }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Target.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Target.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Target.java
index f7019ea..2de5057 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/Target.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/Target.java
@@ -20,11 +20,12 @@ package org.apache.tajo.algebra;
 
 import org.apache.tajo.util.TUtil;
 
-public class Target implements JsonSerializable {
+public class Target extends Expr {
   private Expr expr;
   private String alias;
 
   public Target(Expr expr) {
+    super(OpType.Target);
    this.expr = expr;
   }
 
@@ -45,12 +46,7 @@ public class Target implements JsonSerializable {
   }
 
   @Override
-  public String toJson() {
-    return JsonHelper.toJson(this);
-  }
-
-  @Override
-  public boolean equals(Object obj) {
+  public boolean equalsTo(Expr obj) {
     if (obj instanceof Target) {
       Target another = (Target) obj;
       return expr.equals(another.expr) && TUtil.checkEquals(alias, another.alias);
@@ -58,4 +54,9 @@ public class Target implements JsonSerializable {
 
     return false;
   }
+
+  @Override
+  public String toJson() {
+    return JsonHelper.toJson(this);
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/UnaryOperator.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/UnaryOperator.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/UnaryOperator.java
index 59db158..fec87d0 100644
--- a/tajo-algebra/src/main/java/org/apache/tajo/algebra/UnaryOperator.java
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/UnaryOperator.java
@@ -21,7 +21,7 @@ package org.apache.tajo.algebra;
 public abstract class UnaryOperator extends Expr {
   private Expr child;
 
-  public UnaryOperator(ExprType type) {
+  public UnaryOperator(OpType type) {
     super(type);
   }
 
@@ -29,6 +29,10 @@ public abstract class UnaryOperator extends Expr {
     return this.child;
   }
 
+  public boolean hasChild() {
+    return child != null;
+  }
+
   public void setChild(Expr op) {
     this.child = op;
   }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ValueListExpr.java
----------------------------------------------------------------------
diff --git a/tajo-algebra/src/main/java/org/apache/tajo/algebra/ValueListExpr.java b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ValueListExpr.java
new file mode 100644
index 0000000..bbcbd21
--- /dev/null
+++ b/tajo-algebra/src/main/java/org/apache/tajo/algebra/ValueListExpr.java
@@ -0,0 +1,40 @@
+/**
+ * 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.algebra;
+
+import org.apache.tajo.util.TUtil;
+
+public class ValueListExpr extends Expr {
+  private Expr[] values;
+
+  public ValueListExpr(Expr[] values) {
+    super(OpType.ValueList);
+    this.values = values;
+  }
+
+  public Expr[] getValues() {
+    return values;
+  }
+
+  @Override
+  boolean equalsTo(Expr expr) {
+    ValueListExpr valueListExpr = (ValueListExpr) expr;
+    return TUtil.checkEquals(values, valueListExpr.values);
+  }
+}


[03/13] TAJO-87: Integration of tajo algebra module and SQL parser

Posted by hy...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-frontend/tajo-frontend-sql/src/main/antlr3/tajo/frontend/sql/SQLLexer.g
----------------------------------------------------------------------
diff --git a/tajo-frontend/tajo-frontend-sql/src/main/antlr3/tajo/frontend/sql/SQLLexer.g b/tajo-frontend/tajo-frontend-sql/src/main/antlr3/tajo/frontend/sql/SQLLexer.g
deleted file mode 100644
index 40a0a4d..0000000
--- a/tajo-frontend/tajo-frontend-sql/src/main/antlr3/tajo/frontend/sql/SQLLexer.g
+++ /dev/null
@@ -1,1152 +0,0 @@
-/*
- This software is made available under the BSD License:
-
- Copyright (c) 2011, Mage Systems
- Portions Copyright (c)  Jonathan Leffler 2004-2009'
- Portions Copyright (c) 2003, ISO - International Organization for Standardization
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
-
-     * Redistributions of source code must retain the above copyright notice, this
-       list of conditions and the following disclaimer.
-     * Redistributions in binary form must reproduce the above copyright notice,
-       this list of conditions and the following disclaimer in the documentation
-       and/or other materials provided with the distribution.
-     * Neither the name of the "Mage Systems" nor the names of its contributors
-       may be used to endorse or promote products derived from this software
-       without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/*
- 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.
- */
-
-/*
-==============================================================================================
- Based on BNF Grammer for ISO/IEC 9075-2:2003 Database Language SQL (SQL-2003) SQL/Foundation
- Refer to http://savage.net.au/SQL/sql-2003-2.bnf.html
-==============================================================================================
-*/
-
-lexer grammar SQLLexer;
-
-options{
-  language=Java;
-}
-
-@header {
-package org.apache.tajo.frontend.sql;
-
-import org.apache.tajo.frontend.sql.SQLParseError;
-}
-
-@members {
-   @Override
-   public void reportError(RecognitionException e) {
-    throw new SQLParseError(getErrorHeader(e));
-   }
-}
-
-/*
-==============================================================================================
-  Comment   Tokens
-==============================================================================================
-*/
-fragment
-Start_Comment   : '/*';
-
-fragment
-End_Comment     : '*/';
-
-fragment
-Line_Comment    : '//';
-
-COMMENT
-    :   (   Start_Comment ( options {greedy=false;} : . )* End_Comment )+
-    {
-      $channel=HIDDEN;
-    }
-    ;
-
-LINE_COMMENT
-    :   (   ( Line_Comment | '--' ) ~('\n'|'\r')* '\r'? '\n')+
-    {
-      $channel=HIDDEN;
-    }
-    ;
-
-fragment
-SQL_Terminal_Character  :  SQL_Language_Character;
-
-fragment
-SQL_Language_Character  :  Simple_Latin_Letter | Digit | SQL_Special_Character;
-
-fragment
-Simple_Latin_Letter  :  Simple_Latin_Upper_Case_Letter | Simple_Latin_Lower_Case_Letter;
-
-fragment A
-	:	'A' | 'a';
-
-fragment B
-	:	'B' | 'b';
-
-fragment C
-	:	'C' | 'c';
-
-fragment D
-	:	'D' | 'd';
-
-fragment E
-	:	'E' | 'e';
-
-fragment F
-	:	'F' | 'f';
-
-fragment G
-	:	'G' | 'g';
-
-fragment H
-	:	'H' | 'h';
-
-fragment I
-	:	'I' | 'i';
-
-fragment J
-	:	'J' | 'j';
-
-fragment K
-	:	'K' | 'k';
-
-fragment L
-	:	'L' | 'l';
-
-fragment M
-	:	'M' | 'm';
-
-fragment N
-	:	'N' | 'n';
-
-fragment O
-	:	'O' | 'o';
-
-fragment P
-	:	'P' | 'p';
-
-fragment Q
-	:	'Q' | 'q';
-
-fragment R
-	:	'R' | 'r';
-
-fragment S
-	:	'S' | 's';
-
-fragment T
-	:	'T' | 't';
-
-fragment U
-	:	'U' | 'u';
-
-fragment V
-	:	'V' | 'v';
-
-fragment W
-	:	'W' | 'w';
-
-fragment X
-	:	'X' | 'x';
-
-fragment Y
-	:	'Y' | 'y';
-
-fragment Z
-	:	'Z' | 'z';
-
-fragment
-Simple_Latin_Upper_Case_Letter :
-		'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' |
-		'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
-
-fragment
-Simple_Latin_Lower_Case_Letter :
-		'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k' | 'l' | 'm' |
-		'n' | 'o' | 'p' | 'q' | 'r' | 's' | 't' | 'u' | 'v' | 'w' | 'x' | 'y' | 'z';
-
-fragment
-Digit  :  '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
-
-fragment
-OctalDigit  :  '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7';
-
-fragment
-Hexit   :  Digit | A | B | C | D | E | F;
-
-fragment
-SQL_Special_Character :
-		Space
-	|	Double_Quote
-	|	Percent
-	|	Ampersand
-	|	Quote
-	|	Left_Paren
-	|	Right_Paren
-	|	Asterisk
-	|	Plus_Sign
-	|	Comma
-	|	Minus_Sign
-	|	Period
-	|	Slash
-	|	Colon
-	|	Semicolon
-	|	Less_Than_Operator
-	|	Equals_Operator
-	|	Greater_Than_Operator
-	|	Question_Mark
-	|	Left_Bracket
-	|	Right_Bracket
-	|	Circumflex
-	|	Underscore
-	|	Vertical_Bar
-	|	Left_Brace
-	|	Right_Brace
-	;
-
-fragment
-Unsigned_Large_Integer
-	:	;
-fragment
-Signed_Large_Integer
-	:	;
-fragment
-Unsigned_Float
-	:	;
-fragment
-Signed_Float
-	:	;
-
-/*
-==============================================================================================
- Reserved Keyword Tokens
-==============================================================================================
-*/
-ALL                         :   A L L;
-AS                          :   A S;
-AND                         :   A N D;
-
-BEGIN                       :   B E G I N;
-BETWEEN                     :   B E T W E E N;
-BIGINT                      :   B I G I N T;
-BINARY                      :   B I N A R Y;
-BLOB                        :   B L O B;
-BOOLEAN                     :   B O O L E A N;
-BOTH                        :   B O T H;
-BY                          :   B Y;
-// non-standard
-BYTE                        :   B Y T E;
-BYTES                       :   B Y T E S;
-
-CASE                        :   C A S E;
-CHAR                        :   C H A R;
-CREATE                      :   C R E A T E;
-CROSS                       :   C R O S S;
-CUBE                        :   C U B E;
-
-DATE                        :   D A T E;
-DAY                         :   D A Y;
-DECIMAL                     :   D E C I M A L;
-DECLARE                     :   D E C L A R E;
-DESCRIBE                    :   D E S C R I B E;
-DISCONNECT                  :   D I S C O N N E C T;
-DISTINCT                    :   D I S T I N C T;
-DOUBLE                      :   D O U B L E;
-DROP                        :   D R O P;
-
-ELSE                        :   E L S E;
-END_EXEC                    :   E N D Hyphen E X E C;
-END                         :   E N D;
-ESCAPE                      :   E S C A P E;
-EXCEPT                      :   E X C E P T;
-// non-standard
-EXTERNAL                    :   E X T E R N A L;
-
-FALSE                       :   F A L S E;
-FETCH                       :   F E T C H;
-FILTER                      :   F I L T E R;
-FLOAT                       :   F L O A T;
-FOREIGN                     :   F O R E I G N;
-FOR                         :   F O R;
-FREE                        :   F R E E;
-FROM                        :   F R O M;
-FULL                        :   F U L L;
-FUNCTION                    :   F U N C T I O N;
-
-IDENTITY                    :   I D E N T I T Y;
-IMMEDIATE                   :   I M M E D I A T E;
-INDICATOR                   :   I N D I C A T O R;
-INNER                       :   I N N E R;
-INOUT                       :   I N O U T;
-INPUT                       :   I N P U T;
-INSENSITIVE                 :   I N S E N S I T I V E;
-INSERT                      :   I N S E R T;
-INTEGER                     :   I N T E G E R;
-INTERSECT                   :   I N T E R S E C T;
-INTERVAL                    :   I N T E R V A L;
-INTO                        :   I N T O;
-INT                         :   I N T;
-IN                          :   I N;
-ISOLATION                   :   I S O L A T I O N;
-IS                          :   I S;
-// non standards
-IPV4                        :   I P V '4';
-INDEX                       :   I N D E X;
-
-JOIN                        :   J O I N;
-
-NATIONAL                    :   N A T I O N A L;
-NATURAL                     :   N A T U R A L;
-NCHAR_VARYING               :   N C H A R Underscore V A R Y I N G;
-NCHAR                       :   N C H A R;
-NCLOB                       :   N C L O B;
-NEW                         :   N E W;
-NONE                        :   N O N E;
-NOTFOUND                    :   N O T F O U N D;
-NOT                         :   N O T;
-NO                          :   N O;
-NULL                        :   N U L L;
-NUMERIC                     :   N U M E R I C;
-
-LANGUAGE                    :   L A N G U A G E;
-LEFT                        :   L E F T;
-LIKE                        :   L I K E;
-LOCALTIMESTAMP              :   L O C A L T I M E S T A M P;
-LOCALTIME                   :   L O C A L T I M E;
-LOCAL                       :   L O C A L;
-LIMIT                       :   L I M I T;
-// non standards
-LOCATION                    :   L O C A T I O N;
-
-GET                         :   G E T;
-GLOBAL                      :   G L O B A L;
-GRANT                       :   G R A N T;
-GROUPING                    :   G R O U P I N G;
-GROUP                       :   G R O U P;
-
-HAVING                      :   H A V I N G;
-HOUR                        :   H O U R;
-
-OF                          :   O F;
-ON                          :   O N;
-ORDER                       :   O R D E R;
-OR                          :   O R;
-OUTER                       :   O U T E R;
-OUTPUT                      :   O U T P U T;
-
-RIGHT                       :   R I G H T;
-ROLLBACK                    :   R O L L B A C K;
-ROLLUP                      :   R O L L U P;
-ROWS                        :   R O W S;
-ROW                         :   R O W;
-
-SELECT                      :   S E L E C T;
-SET                         :   S E T;
-SESSION                     :   S E S S I O N;
-SIMILAR                     :   S I M I L A R;
-SMALLINT                    :   S M A L L I N T;
-SQL                         :   S Q L;
-START                       :   S T A R T;
-SYSTEM_USER                 :   S Y S T E M Underscore U S E R;
-SYSTEM                      :   S Y S T E M;
-// non standards
-STRING                      :   S T R I N G;
-
-TABLE                       :   T A B L E;
-THEN                        :   T H E N;
-TIMESTAMP                   :   T I M E S T A M P;
-TIMEZONE_HOUR               :   T I M E Z O N E Underscore H O U R;
-TIMEZONE_MINUTE             :   T I M E Z O N E Underscore M I N U T E;
-TIME                        :   T I M E;
-TO                          :   T O;
-TRUE                        :   T R U E;
-
-UNION                       :   U N I O N;
-UNIQUE                      :   U N I Q U E;
-UNKNOWN                     :   U N K N O W N;
-USER                        :   U S E R;
-USING                       :   U S I N G;
-
-VALUES                      :   V A L U E S;
-VALUE                       :   V A L U E;
-VARCHAR                     :   V A R C H A R;
-VARYING                     :   V A R Y I N G;
-
-WHEN                        :   W H E N;
-WHERE                       :   W H E R E;
-WINDOW                      :   W I N D O W;
-WITH                        :   W I T H;
-WITHIN                      :   W I T H I N;
-WITHOUT                     :   W I T H O U T;
-
-YEAR                        :   Y E A R;
-
-/*
-==============================================================================================
- Non-Reserved Keyword Tokens
-==============================================================================================
-*/
-ASC                         :   A S C;
-
-COUNT                       :   C O U N T;
-
-DESC                        :   D E S C;
-
-FINAL                       :   F I N A L;
-FIRST                       :   F I R S T;
-
-LAST                        :   L A S T;
-LONG                        :   L O N G;
-
-// Non Standard Keyword Tokens
-CLEAR : 'clear';
-DIFF : 'diff';
-
-
-/*
-==============================================================================================
- Punctuation and Arithmetic/Logical Operators
-==============================================================================================
-*/
-Not_Equals_Operator
-  : '<>' | '!=' | '~='| '^=' ;
-Greater_Or_Equals_Operator
-  : '>=';
-Less_Or_Equals_Operator
-  : '<=';
-Concatenation_Operator
-  :	 '||';
-Right_Arrow
-  :	 '->';
-Double_Colon
-  :	 '::';
-Double_Period
-  :	 '..';
-Back_Quote
-  :	 '`';
-Tilde
-  :	 '~';
-Exclamation
-  :	 '!';
-AT_Sign
-  :	 '@';
-Percent
-  :	 '\%';
-
-Circumflex
-  :	 '^';
-Ampersand
-  :	 '&';
-Asterisk
-  : '*';
-Left_Paren
-  :  '(';
-Right_Paren
-  : ')';
-Plus_Sign
-  : '+';
-Minus_Sign
-  : '-';
-fragment
-Hyphen
-	:	 '-';
-Equals_Operator
-	:	 '=';
-Left_Brace
-	:	 '{';
-Right_Brace
-	:	 '}';
-
-/*
-==============================================================================================
- The trigraphs are new in SQL-2003.
-==============================================================================================
-*/
-
-Left_Bracket
-	:	 '[';
-Left_Bracket_Trigraph
-	:	 '??(';
-Right_Bracket
-	:	 ']';
-Right_Bracket_Trigraph
-	:	 '??)';
-Vertical_Bar
-	:	 '|';
-Colon
-	:	 ':';
-Semicolon
-	:	 ';';
-Double_Quote
-	:	 '"';
-Quote
-	:	 '\'';
-Less_Than_Operator
-	:	 '<';
-Greater_Than_Operator
-	:	 '>';
-Comma
-  : ',';
-Period
-  : '.';
-Question_Mark
-	:	 '?';
-Slash
-	:	 '/';
-
-fragment
-Underscore	: '_';
-fragment
-Back_Slash  : '\\';
-fragment
-Hash_Sign   : '#';
-fragment
-Dollar_Sign : '$';
-
-
-// Tajo Ones
-ASSIGN  : ':=';
-
-/*
-==============================================================================================
- Character Set rules
-==============================================================================================
-*/
-
-fragment
-Unicode_Permitted_Identifier_Character  :
-        Basic_Latin_Without_Quotes
-    |   Latin1_Supplement
-    |   Latin_ExtendedA
-    |   Latin_ExtendedB
-    |   IPA_Extensions
-    |   Combining_Diacritical_Marks
-    |   Greek_and_Coptic
-    |   Cyrillic
-    |   Cyrillic_Supplementary
-    |   Armenian
-    |   Hebrew
-    |   Arabic
-    |   Syriac
-    |   Thaana
-    |   Devanagari
-    |   Bengali
-    |   Gurmukhi
-    |   Gujarati
-    |   Oriya
-    |   Tamil
-    |   Telugu
-    |   Kannada
-    |   Malayalam
-    |   Sinhala
-    |   Thai
-    |   Lao
-    |   Tibetan
-    |   Myanmar
-    |   Georgian
-    |   Hangul_Jamo
-    |   Ethiopic
-    |   Cherokee
-    |   Unified_Canadian_Aboriginal
-    |   Ogham
-    |   Runic
-    |   Tagalog
-    |   Hanunoo
-    |   Buhid
-    |   Tagbanwa
-    |   Khmer
-    |   Mongolian
-    |   Limbu
-    |   Tai_Le
-    |   Khmer_Symbols
-    |   Phonetic_Extensions
-    |   Latin_Extended_Additional
-    |   Greek_Extended
-    |   Superscripts_and_Subscripts
-    |   Currency_Symbols
-    |   Combining_Diacritical_Symbol_Marks
-    |   Letterlike_Symbols
-    |   Number_Forms
-    |   Enclosed_Alphanumerics
-    |   CJK_Radicals_Supplement
-    |   Kangxi_Radicals
-    |   Ideographic_Description_Characters
-    |   CJK_Symbols_and_Punctuation
-    |   Hiragana
-    |   Katakana
-    |   Bopomofo
-    |   Hangul_Compatibility_Jamo
-    |   Kanbun
-    |   Bopomofo_Extended
-    |   Katakana_Phonetic_Extensions
-    |   Enclosed_CJK_Letters_and_Months
-    |   CJK_Compatibility
-    |   CJK_Unified_Ideographs_ExtensionA
-    |   CJK_Unified_Ideographs
-    |   Yi_Syllables
-    |   Yi_Radicals
-    |   Hangul_Syllables
-    |   High_Surrogates
-    |   High_Private_Use_Surrogates
-    |   Low_Surrogates
-    |   Private_Use_Area
-    |   CJK_Compatibility_Ideographs
-    |   Alphabetic_Presentation_Forms
-    |   Arabic_Presentation_FormsA
-    |   Variation_Selectors
-    |   Combining_Half_Marks
-    |   CJK_Compatibility_Forms
-    |   Small_Form_Variants
-    |   Arabic_Presentation_FormsB
-    |   Halfwidth_and_Fullwidth_Forms
-    ;
-
-//  Generate an Unexpected Token Error if any forbidden characters are used in a Unicode Identifier
-Unicode_Forbidden_Identifier_Characters  :  ( Unicode_Forbidden_Identifier_Character )+;
-
-fragment
-Unicode_Forbidden_Identifier_Character  :
-        Control_Characters
-    |   Spacing_Modifier_Letters
-    |   General_Punctuation
-    |   Arrows
-    |   Mathematical_Operators
-    |   Miscellaneous_Technical
-    |   Control_Pictures
-    |   Optical_Character_Recognition
-    |   Box_Drawing
-    |   Block_Elements
-    |   Geometric_Shapes
-    |   Miscellaneous_Symbols
-    |   Dingbats
-    |   Miscellaneous_Mathematical_SymbolsA
-    |   Supplemental_ArrowsA
-    |   Braille_Patterns
-    |   Supplemental_ArrowsB
-    |   Miscellaneous_Mathematical_SymbolsB
-    |   Supplemental_Mathematical_Operators
-    |   Miscellaneous_Symbols_and_Arrows
-    |   Yijing_Hexagram_Symbols
-    |   Specials
-    ;
-
-// Unicode Character Ranges
-fragment
-Unicode_Character_Without_Quotes    :   Basic_Latin_Without_Quotes
-                                    |   '\u00A0' .. '\uFFFF';
-fragment
-Extended_Latin_Without_Quotes       :   '\u0001' .. '!' | '#' .. '&' | '(' .. '\u00FF';
-fragment
-Control_Characters                  :   '\u0001' .. '\u001F';
-fragment
-Basic_Latin                         :   '\u0020' .. '\u007F';
-fragment
-Basic_Latin_Without_Quotes          :   ' ' .. '!' | '#' .. '&' | '(' .. '~';
-fragment
-Regex_Non_Escaped_Unicode           :   ~( '|' | '*' | '+' | '-' | '?' | '\%' | '_' | '^' | ':' | '{' | '}' | '(' | ')' | '[' | '\\' ) ;
-fragment
-Regex_Escaped_Unicode               :   ' ' .. '[' | ']' .. '~' | '\u00A0' .. '\uFFFF';
-fragment
-Unicode_Allowed_Escape_Chracter		:	'!' | '#' .. '&' | '(' .. '/' | ':' .. '@' | '[' .. '`' | '{' .. '~' | '\u0080' .. '\u00BF';
-fragment
-Full_Unicode						:	'\u0001' .. '\uFFFF';
-fragment
-Extended_Control_Characters         :   '\u0080' .. '\u009F';
-fragment
-Latin1_Supplement                   :   '\u00A0' .. '\u00FF';
-fragment
-Latin_ExtendedA                     :   '\u0100' .. '\u017F';
-fragment
-Latin_ExtendedB                     :   '\u0180' .. '\u024F';
-fragment
-IPA_Extensions                      :   '\u0250' .. '\u02AF';
-fragment
-Spacing_Modifier_Letters            :   '\u02B0' .. '\u02FF';
-fragment
-Combining_Diacritical_Marks         :   '\u0300' .. '\u036F';
-fragment
-Greek_and_Coptic                    :   '\u0370' .. '\u03FF';
-fragment
-Cyrillic                            :   '\u0400' .. '\u04FF';
-fragment
-Cyrillic_Supplementary              :   '\u0500' .. '\u052F';
-fragment
-Armenian                            :   '\u0530' .. '\u058F';
-fragment
-Hebrew                              :   '\u0590' .. '\u05FF';
-fragment
-Arabic                              :   '\u0600' .. '\u06FF';
-fragment
-Syriac                              :   '\u0700' .. '\u074F';
-fragment
-Thaana                              :   '\u0780' .. '\u07BF';
-fragment
-Devanagari                          :   '\u0900' .. '\u097F';
-fragment
-Bengali                             :   '\u0980' .. '\u09FF';
-fragment
-Gurmukhi                            :   '\u0A00' .. '\u0A7F';
-fragment
-Gujarati                            :   '\u0A80' .. '\u0AFF';
-fragment
-Oriya                               :   '\u0B00' .. '\u0B7F';
-fragment
-Tamil                               :   '\u0B80' .. '\u0BFF';
-fragment
-Telugu                              :   '\u0C00' .. '\u0C7F';
-fragment
-Kannada                             :   '\u0C80' .. '\u0CFF';
-fragment
-Malayalam                           :   '\u0D00' .. '\u0D7F';
-fragment
-Sinhala                             :   '\u0D80' .. '\u0DFF';
-fragment
-Thai                                :   '\u0E00' .. '\u0E7F';
-fragment
-Lao                                 :   '\u0E80' .. '\u0EFF';
-fragment
-Tibetan                             :   '\u0F00' .. '\u0FFF';
-fragment
-Myanmar                             :   '\u1000' .. '\u109F';
-fragment
-Georgian                            :   '\u10A0' .. '\u10FF';
-fragment
-Hangul_Jamo                         :   '\u1100' .. '\u11FF';
-fragment
-Ethiopic                            :   '\u1200' .. '\u137F';
-fragment
-Cherokee                            :   '\u13A0' .. '\u13FF';
-fragment
-Unified_Canadian_Aboriginal         :   '\u1400' .. '\u167F';
-fragment
-Ogham                               :   '\u1680' .. '\u169F';
-fragment
-Runic                               :   '\u16A0' .. '\u16FF';
-fragment
-Tagalog                             :   '\u1700' .. '\u171F';
-fragment
-Hanunoo                             :   '\u1720' .. '\u173F';
-fragment
-Buhid                               :   '\u1740' .. '\u175F';
-fragment
-Tagbanwa                            :   '\u1760' .. '\u177F';
-fragment
-Khmer                               :   '\u1780' .. '\u17FF';
-fragment
-Mongolian                           :   '\u1800' .. '\u18AF';
-fragment
-Limbu                               :   '\u1900' .. '\u194F';
-fragment
-Tai_Le                              :   '\u1950' .. '\u197F';
-fragment
-Khmer_Symbols                       :   '\u19E0' .. '\u19FF';
-fragment
-Phonetic_Extensions                 :   '\u1D00' .. '\u1D7F';
-fragment
-Latin_Extended_Additional           :   '\u1E00' .. '\u1EFF';
-fragment
-Greek_Extended                      :   '\u1F00' .. '\u1FFF';
-fragment
-General_Punctuation                 :   '\u2000' .. '\u206F';
-fragment
-Superscripts_and_Subscripts         :   '\u2070' .. '\u209F';
-fragment
-Currency_Symbols                    :   '\u20A0' .. '\u20CF';
-fragment
-Combining_Diacritical_Symbol_Marks  :   '\u20D0' .. '\u20FF';
-fragment
-Letterlike_Symbols                  :   '\u2100' .. '\u214F';
-fragment
-Number_Forms                        :   '\u2150' .. '\u218F';
-fragment
-Arrows                              :   '\u2190' .. '\u21FF';
-fragment
-Mathematical_Operators              :   '\u2200' .. '\u22FF';
-fragment
-Miscellaneous_Technical             :   '\u2300' .. '\u23FF';
-fragment
-Control_Pictures                    :   '\u2400' .. '\u243F';
-fragment
-Optical_Character_Recognition       :   '\u2440' .. '\u245F';
-fragment
-Enclosed_Alphanumerics              :   '\u2460' .. '\u24FF';
-fragment
-Box_Drawing                         :   '\u2500' .. '\u257F';
-fragment
-Block_Elements                      :   '\u2580' .. '\u259F';
-fragment
-Geometric_Shapes                    :   '\u25A0' .. '\u25FF';
-fragment
-Miscellaneous_Symbols               :   '\u2600' .. '\u26FF';
-fragment
-Dingbats                            :   '\u2700' .. '\u27BF';
-fragment
-Miscellaneous_Mathematical_SymbolsA  :    '\u27C0' .. '\u27EF';
-fragment
-Supplemental_ArrowsA                :   '\u27F0' .. '\u27FF';
-fragment
-Braille_Patterns                    :   '\u2800' .. '\u28FF';
-fragment
-Supplemental_ArrowsB                :   '\u2900' .. '\u297F';
-fragment
-Miscellaneous_Mathematical_SymbolsB  :    '\u2980' .. '\u29FF';
-fragment
-Supplemental_Mathematical_Operators  :    '\u2A00' .. '\u2AFF';
-fragment
-Miscellaneous_Symbols_and_Arrows    :   '\u2B00' .. '\u2BFF';
-fragment
-CJK_Radicals_Supplement             :   '\u2E80' .. '\u2EFF';
-fragment
-Kangxi_Radicals                     :   '\u2F00' .. '\u2FDF';
-fragment
-Ideographic_Description_Characters  :   '\u2FF0' .. '\u2FFF';
-fragment
-CJK_Symbols_and_Punctuation         :   '\u3000' .. '\u303F';
-fragment
-Hiragana                            :   '\u3040' .. '\u309F';
-fragment
-Katakana                            :   '\u30A0' .. '\u30FF';
-fragment
-Bopomofo                            :   '\u3100' .. '\u312F';
-fragment
-Hangul_Compatibility_Jamo           :   '\u3130' .. '\u318F';
-fragment
-Kanbun                              :   '\u3190' .. '\u319F';
-fragment
-Bopomofo_Extended                   :   '\u31A0' .. '\u31BF';
-fragment
-Katakana_Phonetic_Extensions        :   '\u31F0' .. '\u31FF';
-fragment
-Enclosed_CJK_Letters_and_Months     :   '\u3200' .. '\u32FF';
-fragment
-CJK_Compatibility                   :   '\u3300' .. '\u33FF';
-fragment
-CJK_Unified_Ideographs_ExtensionA   :   '\u3400' .. '\u4DBF';
-fragment
-CJK_Unified_Ideographs              :   '\u4E00' .. '\u9FFF';
-fragment
-Yijing_Hexagram_Symbols             :   '\u4DC0' .. '\u4DFF';
-fragment
-Yi_Syllables                        :   '\uA000' .. '\uA48F';
-fragment
-Yi_Radicals                         :   '\uA490' .. '\uA4CF';
-fragment
-Hangul_Syllables                    :   '\uAC00' .. '\uD7AF';
-fragment
-High_Surrogates                     :   '\uD800' .. '\uDB7F';
-fragment
-High_Private_Use_Surrogates         :   '\uDB80' .. '\uDBFF';
-fragment
-Low_Surrogates                      :   '\uDC00' .. '\uDFFF';
-fragment
-Private_Use_Area                    :   '\uE000' .. '\uF8FF';
-fragment
-CJK_Compatibility_Ideographs        :   '\uF900' .. '\uFAFF';
-fragment
-Alphabetic_Presentation_Forms       :   '\uFB00' .. '\uFB4F';
-fragment
-Arabic_Presentation_FormsA          :   '\uFB50' .. '\uFDFF';
-fragment
-Variation_Selectors                 :   '\uFE00' .. '\uFE0F';
-fragment
-Combining_Half_Marks                :   '\uFE20' .. '\uFE2F';
-fragment
-CJK_Compatibility_Forms             :   '\uFE30' .. '\uFE4F';
-fragment
-Small_Form_Variants                 :   '\uFE50' .. '\uFE6F';
-fragment
-Arabic_Presentation_FormsB          :   '\uFE70' .. '\uFEFF';
-fragment
-Halfwidth_and_Fullwidth_Forms       :   '\uFF00' .. '\uFFEF';
-fragment
-Specials                            :   '\uFFF0' .. '\uFFFF';
-
-
-
-
-fragment
-Unicode_Identifier_Part          :  Unicode_Escape_Value | Unicode_Permitted_Identifier_Character;
-
-fragment
-Unicode_Escape_Value             :  Unicode_4_Digit_Escape_Value  | Unicode_6_Digit_Escape_Value ;
-
-fragment
-Unicode_4_Digit_Escape_Value     :  Escape_Character  Hexit  Hexit  Hexit  Hexit ;
-
-fragment
-Unicode_6_Digit_Escape_Value     :  Escape_Character  Plus_Sign Hexit  Hexit  Hexit  Hexit  Hexit  Hexit ;
-
-Escape_Character                 :  '\\' /* Unicode_Allowed_Escape_Chracter */ /*!! See the Syntax Rules*/;
-
-
-/*
-==============================================================================================
- 5.3 <literal> (p143)
-==============================================================================================
-*/
-
-fragment
-HexPair     :   Hexit Hexit;
-
-fragment
-HexQuad     :   Hexit Hexit Hexit Hexit;
-
-fragment
-Unsigned_Integer	:
-	(	'0'
-			(	( 'x' | 'X' )
-				HexPair ( HexPair ( HexQuad (HexQuad HexQuad)? )? )?
-			|	OctalDigit ( OctalDigit )*
-			|	{true}?
-				( '0' )*
-			)
-	|	( '1'..'9' ) ( Digit )*
-	);
-
-fragment
-Signed_Integer	:
-	( Plus_Sign | Minus_Sign ) ( Digit )+
-	;
-
-Number	:
-	(	'0'
-			(	( 'x' | 'X' )
-				HexPair ( HexPair ( HexQuad (HexQuad HexQuad)? )? )?
-				(	('K' | 'M' |'G')
-					{
-						_type  =  Unsigned_Large_Integer;
-					}
-				|	{true}?
-					{
-						_type  =  Unsigned_Integer;
-					}
-				)
-
-			|	( OctalDigit )*
-				(	('K' | 'M' |'G')
-					{
-						_type  =  Unsigned_Large_Integer;
-					}
-				|	{true}?
-					{
-						_type  =  Unsigned_Integer;
-					}
-				)
-
-			|	Period
-				( Digit )+ ( ( 'f' | 'F' | 'd' | 'D' | 'e' | 'E' ) ( Plus_Sign | Minus_Sign )? Digit ( Digit ( Digit )? )? )?
-				{
-					_type  =  Unsigned_Float;
-				}
-
-			|	'8'..'9' ( Digit )*
-				(	('K' | 'M' |'G')
-					{
-						_type  =  Unsigned_Large_Integer;
-					}
-				|	{true}?
-					{
-						_type  =  Unsigned_Integer;
-					}
-				)
-			)
-	|	( Plus_Sign | Minus_Sign ) ( Digit )+
-			(	Period ( Digit )+ ( ( 'f' | 'F' | 'd' | 'D' | 'e' | 'E' ) ( Plus_Sign | Minus_Sign )? Digit ( Digit ( Digit )? )? )?
-				{
-					_type  =  Signed_Float;
-				}
-
-			|	(	('K' | 'M' |'G')
-					{
-						_type  =  Signed_Large_Integer;
-					}
-				|	{true}?
-					{
-						_type  =  Signed_Integer;
-					}
-				)
-			)
-	|	( '1'..'9' ) ( Digit )*
-			(	Period ( Digit )+ ( ( 'f' | 'F' | 'd' | 'D' | 'e' | 'E' ) ( Plus_Sign | Minus_Sign )? Digit ( Digit ( Digit )? )? )?
-				{
-					_type  =  Unsigned_Float;
-				}
-
-			|	(	('K' | 'M' |'G')
-					{
-						_type  =  Unsigned_Large_Integer;
-					}
-				|	{true}?
-					{
-						_type  =  Unsigned_Integer;
-					}
-				)
-			)
-
-	|	Period
-			( Digit )+ ( ( 'f' | 'F' | 'd' | 'D' | 'e' | 'E' ) ( Plus_Sign | Minus_Sign )? Digit ( Digit ( Digit )? )? )?
-				{
-					_type  =  Unsigned_Float;
-				}
-	);
-
-fragment
-Character_Set_Name  : ( ( ( Regular_Identifier )  Period )?
-                          ( Regular_Identifier )  Period )?
-                            Regular_Identifier ;
-
-fragment
-Character_Set		:	Underscore  Character_Set_Name;
-
-fragment
-Character_String_Literal :
-		  Quote ( Extended_Latin_Without_Quotes  )* Quote ( Quote ( Extended_Latin_Without_Quotes  )* Quote )* {setText(getText().substring(1, getText().length()-1));}
-		;
-
-fragment
-National_Character_String_Literal :
-		'N' Quote ( Extended_Latin_Without_Quotes  )* Quote ( Quote ( Extended_Latin_Without_Quotes  )* Quote )*
-		;
-
-fragment
-Unicode_Character_String_Literal  :
-		'U' Ampersand Quote ( Unicode_Character_Without_Quotes  )* Quote ( Quote ( Unicode_Character_Without_Quotes  )* Quote )*
-		;
-
-fragment
-Bit :   ('0' | '1');
-
-fragment
-Bit_String_Literal  :
-		'B' Quote ( Bit Bit Bit Bit  Bit Bit Bit Bit  )* Quote ( Quote ( Bit Bit Bit Bit  Bit Bit Bit Bit  )* Quote )*
-		;
-
-fragment
-Hex_String_Literal  :
-		'X' Quote ( Hexit  Hexit  )* Quote ( Quote ( Hexit  Hexit  )* Quote )*
-		;
-
-String_Literal	:
-	(	Character_Set
-		(
-			Unicode_Character_String_Literal
-				{
-					_type  =  Unicode_Character_String_Literal;
-				}
-		|	Character_String_Literal
-				{
-					_type  =  Character_String_Literal;
-				}
-		)
-	|	Bit_String_Literal
-			{
-				_type  =  Bit_String_Literal;
-			}
-	|	Hex_String_Literal
-			{
-				_type  =  Hex_String_Literal;
-			}
-	|	National_Character_String_Literal
-			{
-				_type  =  National_Character_String_Literal;
-			}
-	|	Unicode_Character_String_Literal
-			{
-				_type  =  Unicode_Character_String_Literal;
-			}
-	|	Character_String_Literal
-			{
-				_type  =  Character_String_Literal;
-			}
-	);
-
-/*
-==============================================================================================
- 5.4 Names and identifiers (p151)
-
- Refer to the comparison table among various commercial DBMSs.
-
- Database        1st Character           Sunsequent Characters           Case Sensitive  Max Length
- ============    ====================    ============================    ==============  ==========
- Sybase ASA      Latin,'_','@','#','$'   Latin,Digit,'_','@','#','$'     no              128
- Sybase ASE      Latin,'_','@','#'       Latin,Digit,'_','@','#','$'     no              255
- SQL Server      Latin,'_','@','#'       Latin,Digit,'_','@','#','$'     no              128
- Teradata        Latin,'_','#','$'       Latin,Digit,'_','#','$'         no              30
- MySQL           Latin,Digit             Latin,Digit,'_','$'             yes             64
- Informix        Latin,'_'               Latin,Digit,'_','$'             no              128
- PostgreSQL      Latin,'_'               Latin,Digit,'_','$'             no              63
- Oracle          Latin                   Latin,Digit,'_','#','$'         no              30
- Interbase       Latin                   Latin,Digit,'_','$'             no              67
- ANSI SQL92      Latin                   Latin,Digit,'_'                 yes             128
- IBM DB2         same as ANSI SQL92                                      no              128
- HP Neoview      same as ANSI SQL92                                      no              128
- ------------    --------------------    ----------------------------    --------------  ----------
- Tajo SQL        Latin                   Latin,Digit,'_',':'             no              128
-==============================================================================================
-*/
-
-Regular_Identifier     :  Tajo_Identifier;
-
-fragment
-Tajo_Identifier        :  Tajo_Identifier_Start ( Tajo_Identifier_Part )*;
-fragment
-Tajo_Identifier_Start  :  Simple_Latin_Letter;
-fragment
-Tajo_Identifier_Part   :  Simple_Latin_Letter | Digit | Underscore | Colon;
-
-/*
-==============================================================================================
- Whitespace Tokens
- ignore all control characters and non-Ascii characters that are not enclosed in quotes
-==============================================================================================
-*/
-
-Space    :  ' '
-{
-	$channel=HIDDEN;
-};
-
-White_Space  :	( Control_Characters  | Extended_Control_Characters )+
-{
-	$channel=HIDDEN;
-};
-
-
-BAD : . {
-  //error(UNKNOWN_CHARACTER, $text);
-  skip();
-} ;
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-frontend/tajo-frontend-sql/src/main/antlr3/tajo/frontend/sql/SQLParser.g
----------------------------------------------------------------------
diff --git a/tajo-frontend/tajo-frontend-sql/src/main/antlr3/tajo/frontend/sql/SQLParser.g b/tajo-frontend/tajo-frontend-sql/src/main/antlr3/tajo/frontend/sql/SQLParser.g
deleted file mode 100644
index 19b078b..0000000
--- a/tajo-frontend/tajo-frontend-sql/src/main/antlr3/tajo/frontend/sql/SQLParser.g
+++ /dev/null
@@ -1,956 +0,0 @@
-/*
- This software is made available under the BSD License:
-
- Copyright (c) 2011, Mage Systems
- Portions Copyright (c)  Jonathan Leffler 2004-2009'
- Portions Copyright (c) 2003, ISO - International Organization for Standardization
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
-
-     * Redistributions of source code must retain the above copyright notice, this
-       list of conditions and the following disclaimer.
-     * Redistributions in binary form must reproduce the above copyright notice,
-       this list of conditions and the following disclaimer in the documentation
-       and/or other materials provided with the distribution.
-     * Neither the name of the "Mage Systems" nor the names of its contributors
-       may be used to endorse or promote products derived from this software
-       without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
- OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/*
- 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.
- */
-
-/*
-==============================================================================================
- Based on BNF Grammer for ISO/IEC 9075-2:2003 Database Language SQL (SQL-2003) SQL/Foundation
- Refer to http://savage.net.au/SQL/sql-2003-2.bnf.html
-==============================================================================================
-*/
-
-parser grammar SQLParser;
-
-options {
-	language=Java;
-	tokenVocab=SQLLexer;
-	backtrack=true;
-	memoize=true;
-	output=AST;
-	ASTLabelType=CommonTree;
-}
-
-tokens {
-  ALL;
-  COLUMN;
-  COUNT_VAL;
-  COUNT_ROWS;
-  CREATE_INDEX;
-  CREATE_TABLE;
-  DROP_TABLE;
-  DESC_TABLE;
-  EMPTY_GROUPING_SET;
-  FIELD_NAME;
-  FIELD_DEF;
-  FUNCTION;
-  FUNC_ARGS;
-  GROUP_BY;
-  NULL_ORDER;
-  ORDER;
-  ORDER_BY;
-  PARAM;
-  PARAMS;
-  SEL_LIST;
-  SESSION_CLEAR;
-  SET_QUALIFIER;
-  SHOW_TABLE;
-  SHOW_FUNCTION;
-  SORT_KEY;
-  SORT_SPECIFIERS;
-  STORE;
-  STORE_TYPE;
-  SUBQUERY;
-  TABLE_DEF;
-  TARGET_FIELDS;
-}
-
-@header {
-package org.apache.tajo.frontend.sql;
-
-import java.util.List;
-import java.util.ArrayList;
-import org.apache.tajo.frontend.sql.SQLParseError;
-}
-
-@members {
-   @Override
-   public void reportError(RecognitionException e) {
-     throw new SQLParseError(getErrorHeader(e));
-   }
-
-   @Override
-   public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
-     String hdr = getErrorHeader(e);
-     String msg = getErrorMessage(e, tokenNames);
-     throw new SQLParseError(hdr + ":" + msg);
-   }
-}
-
-// SQL Main
-sql
-  : statement EOF
-  ;
-
-statement
-  : sessionStatement
-  | dataStatement
-  | dataChangeStatement
-  | schemaStatement
-  | indexStatement
-  ;
-
-sessionStatement
-  : SESSION CLEAR -> ^(SESSION_CLEAR)
-  ;
-  
-dataStatement
-  : query_expression
-  | set_stmt
-  ;
-  
-dataChangeStatement
-  : insertStmt
-  ;
-  
-schemaStatement
-  : createTableStatement
-  | DROP TABLE table_name -> ^(DROP_TABLE table_name)
-  ;
-  
-indexStatement
-  : CREATE (u=UNIQUE)? INDEX n=identifier ON t=table_name (m=method_specifier)?
-    Left_Paren s=sort_specifier_list Right_Paren p=param_clause?
-    -> ^(CREATE_INDEX $u? $m? $p? $n $t $s)
-  ;
-
-createTableStatement
-  : CREATE EXTERNAL TABLE t=table_name def=tableElements USING f=identifier p=param_clause? (LOCATION path=Character_String_Literal)
-      -> ^(CREATE_TABLE $t EXTERNAL ^(TABLE_DEF $def) ^(USING $f) $p? ^(LOCATION $path))
-  | CREATE TABLE t=table_name (def=tableElements)? (USING f=identifier)? (p=param_clause)? (AS q=query_expression)?
-      -> ^(CREATE_TABLE $t ^(TABLE_DEF $def)? ^(USING $f)? $p? ^(AS $q)?)
-  ;
-
-tableElements
-  : Left_Paren fieldElement (Comma fieldElement)* Right_Paren -> fieldElement+
-  ;
-
-fieldElement
-  : identifier fieldType -> ^(FIELD_DEF identifier fieldType)
-  ;
-
-fieldType
-  : BOOL
-  | BYTE
-  | CHAR
-  | INT
-  | LONG
-  | FLOAT
-  | DOUBLE
-  | TEXT
-  | DATE
-  | BYTES
-  | IPv4
-  | VARCHAR
-  | STRING
-  ;
-
-query_expression
-  : query_expression_body
-  ;
-
-query_expression_body
-  : non_join_query_expression
-  | joined_table
-  ;
-
-non_join_query_expression
-  : (non_join_query_term | joined_table (UNION | EXCEPT)^ (ALL|DISTINCT)? query_term)
-    ((UNION | EXCEPT)^ (ALL|DISTINCT)? query_term)*
-  ;
-
-query_term
-  : non_join_query_term
-  | joined_table
-  ;
-
-non_join_query_term
-  : ( non_join_query_primary | joined_table INTERSECT^ (ALL|DISTINCT)? query_primary)
-    (INTERSECT^ (ALL|DISTINCT)? query_primary)*
-  ;
-
-query_primary
-  : non_join_query_primary
-  | joined_table
-  ;
-
-non_join_query_primary
-  : simple_table
-  | Left_Paren non_join_query_expression Right_Paren
-  ;
-
-simple_table
-options {k=1;}
-  : query_specification
-  ;
-
-query_specification
-  : SELECT setQualifier? selectList from_clause? where_clause? groupby_clause? having_clause?
-    orderby_clause? limit_clause?
-
-    -> ^(SELECT from_clause? setQualifier? selectList where_clause? groupby_clause? having_clause?
-    orderby_clause? limit_clause?)
-  ;
-
-insertStmt
-  : INSERT INTO table_name (Left_Paren column_reference Right_Paren)? VALUES array
-    -> ^(INSERT ^(TABLE table_name) ^(VALUES array) ^(TARGET_FIELDS column_reference)?)
-  ;
-
-selectList
-  : Asterisk -> ^(SEL_LIST ALL)
-  | derivedColumn (Comma derivedColumn)* -> ^(SEL_LIST derivedColumn+)
-  ;
-
-setQualifier
-  : DISTINCT -> ^(SET_QUALIFIER DISTINCT)
-  | ALL -> ^(SET_QUALIFIER ALL)
-  ;
-
-derivedColumn
-  : value_expression asClause? -> ^(COLUMN value_expression asClause?)
-  ;
-
-fieldName
-	:	(t=identifier Period)? b=identifier -> ^(FIELD_NAME $b $t?)
-	;
-
-asClause
-  : (AS)? fieldName
-  ;
-
-column_reference
-	:	fieldName (Comma fieldName)* -> fieldName+
-	;
-
-funcCall
-	: identifier Left_Paren Right_Paren -> ^(FUNCTION[$identifier.text])
-	| identifier Left_Paren funcArgs? Right_Paren -> ^(FUNCTION[$identifier.text] funcArgs?)
-	| COUNT Left_Paren funcArgs Right_Paren -> ^(COUNT_VAL funcArgs)
-	| COUNT Left_Paren Asterisk Right_Paren -> ^(COUNT_ROWS)
-	;
-
-funcArgs
-  : value_expression (Comma value_expression)* -> value_expression+
-  ;
-
-from_clause
-  : FROM^ table_reference_list
-  ;
-
-table_reference_list
-  : table_reference (Comma table_reference)* -> table_reference+
-  ;
-
-table_reference
-  : joined_table
-  | table_primary
-  ;
-
-joined_table
-  : table_primary (
-    cross_join
-  | qualified_join
-  | natural_join
-  | union_join )+
-  ;
-
-joined_table_prim
-  : cross_join
-  | qualified_join
-  | natural_join
-  | union_join
-  ;
-
-cross_join
-  : CROSS JOIN r=table_primary -> ^(JOIN CROSS $r)
-  ;
-
-qualified_join
-  : (t=join_type)? JOIN r=table_primary s=join_specification -> ^(JOIN $t? $r $s)
-  ;
-
-natural_join
-  : NATURAL (t=join_type)? JOIN r=table_primary -> ^(JOIN NATURAL $t? $r)
-  ;
-
-union_join
-  : UNION JOIN r=table_primary -> ^(JOIN UNION $r)
-  ;
-
-join_type
-  options {k=1;}
-  : INNER
-  | t=outer_join_type -> ^(OUTER $t)
-  ;
-
-outer_join_type
-  : outer_join_type_part2 (OUTER?)!
-  ;
-
-outer_join_type_part2
-  : LEFT
-  | RIGHT
-  | FULL
-  ;
-
-join_specification
-  : join_condition
-  | named_columns_join
-  ;
-
-join_condition
-  : ON^ search_condition
-  ;
-
-named_columns_join
-  : USING Left_Paren f=column_reference Right_Paren -> ^(USING $f)
-  ;
-
-table_primary
-  : table_name ((AS)? a=identifier)? -> ^(TABLE table_name ($a)?)
-  | t=derived_table (AS)? a=identifier-> ^(SUBQUERY $t $a)
-  ;
-
-derived_table
-  : table_subquery
-  ;
-
-
-
-where_clause
-  : WHERE^ search_condition
-  ;
-
-groupby_clause
-  : GROUP BY g=grouping_element_list -> ^(GROUP_BY $g)
-  ;
-
-grouping_element_list
-  : grouping_element (Comma grouping_element)* -> grouping_element+
-  ;
-
-grouping_element
-  : ordinary_grouping_set
-  | rollup_list
-  | cube_list
-  | empty_grouping_set
-  ;
-
-ordinary_grouping_set
-  : column_reference
-  | Left_Paren! column_reference Right_Paren!
-  ;
-
-rollup_list
-  : ROLLUP Left_Paren c=ordinary_grouping_set Right_Paren -> ^(ROLLUP $c)
-  ;
-
-cube_list
-  : CUBE Left_Paren c=ordinary_grouping_set Right_Paren -> ^(CUBE $c)
-  ;
-
-empty_grouping_set
-  : Left_Paren Right_Paren -> ^(EMPTY_GROUPING_SET)
-  ;
-
-having_clause
-  : HAVING^ boolean_value_expression
-  ;
-
-orderby_clause
-  : ORDER BY sort_specifier_list -> ^(ORDER_BY sort_specifier_list)
-  ;
-
-sort_specifier_list
-  : sort_specifier (Comma sort_specifier)* -> ^(SORT_SPECIFIERS sort_specifier+)
-  ;
-
-sort_specifier
-  : fn=fieldName a=order_specification? o=null_ordering? -> ^(SORT_KEY $fn $a? $o?)
-  ;
-
-order_specification
-  : ASC -> ^(ORDER ASC)
-  | DESC -> ^(ORDER DESC)
-  ;
-
-limit_clause
-  : LIMIT e=numeric_value_expression -> ^(LIMIT $e)
-  ;
-
-null_ordering
-  : NULL FIRST -> ^(NULL_ORDER FIRST)
-  | NULL LAST -> ^(NULL_ORDER LAST)
-  ;
-
-set_stmt
-	:	SET (UNION|INTERSECT|DIFF) table_name
-	;
-
-search_condition
-	:	boolean_value_expression
-	;
-
-param_clause
-  : WITH Left_Paren param (Comma param)* Right_Paren -> ^(PARAMS param+)
-  ;
-
-param
-  : k=Character_String_Literal Equals_Operator v=boolean_value_expression -> ^(PARAM $k $v)
-  ;
-
-method_specifier
-  : USING m=identifier -> ^(USING[$m.text])
-  ;
-
-/*
-==============================================================================================
-  5.3 <literal> (p143)
-
-  Progress: Almost done
-
-  TODO:
-    * literal should be fixed
-    * NULL should be removed
-==============================================================================================
-*/
-
-literal
-  options{k=1;}
-  : general_literal
-  ;
-
-unsigned_literal
-  : unsigned_numerical_literal
-  | general_literal
-  ;
-
-general_literal
-  options{k=1;}
-  : unsigned_numerical_literal
-  | Character_String_Literal
-  |	National_Character_String_Literal
-  | Bit_String_Literal
-  | Hex_String_Literal
-  |	boolean_literal
-  | day_time_literal
-  | interval_literal
-  | NULL
-  ;
-
-boolean_literal
-  : TRUE | FALSE | UNKNOWN
-  ;
-
-signed_numerical_literal
-  : Signed_Integer
-  | Signed_Large_Integer
-  | Signed_Float
-  ;
-
-unsigned_numerical_literal
-  : Unsigned_Integer
-  | Unsigned_Large_Integer
-  | Unsigned_Float
-  ;
-
-
-/*
-==============================================================================================
-  5.4 Names and identifiers (p151)
-==============================================================================================
-*/
-
-identifier                  : Regular_Identifier | Unicode_Identifier;
-sql_language_identifier     : Regular_Identifier;
-user_identifier             : Regular_Identifier;
-schema_name                 : identifier  ( Period  identifier )? ;
-fully_qualified_identifier  : identifier  ( Period  identifier (  Period  identifier )? )?;
-
-
-table_name
-  : identifier  ( Period  identifier (  Period  identifier )? )?
-  ;
-
-identifier_chain            : identifier  ( Period identifier  )*;
-
-/*
-==============================================================================================
-  6.25 value_expression  (p236)
-
-  Specify a value.
-
-  TODO:
-    * collection_value_expression
-    * collection_value_constructor
-==============================================================================================
-*/
-
-
-value_expression
-options{k=2;}
-  : common_value_expression
-  | boolean_value_expression
-  | row_value_expression
-  ;
-
-common_value_expression
-options {k=1;}
-  : numeric_value_expression
-  | string_value_expression
-  ;
-
-user_defined_type_value_expression
-  options {k=1;}
-  : value_expression_primary
-  ;
-
-reference_value_expression
-  options {k=1;}
-  : value_expression_primary
-  ;
-
-/*
-==============================================================================================
-  6.26 numeric_value_expression  (p240)
-
-  Specify a numeric value.
-==============================================================================================
-*/
-
-numeric_value_expression
-  options {k=1;}
-	:	term ((Plus_Sign|Minus_Sign)^ term)*
-	;
-
-term
-  options {k=1;}
-  :	factor ((Asterisk|Slash|Percent)^ factor)*
-	;
-
-factor
-  options {k=1;}
-  : (sign)? numeric_primary
-	;
-
-numeric_primary
-  options{k=1;}
-  : value_expression_primary
-  ;
-
-array
-  : Left_Paren literal (Comma literal )* Right_Paren -> literal+
-  ;
-
-/*
-==============================================================================================
-  6.28 string_value_expression  (p251)
-
-  Specify a character string value or a binary string value.
-  TODO:
-    * collate_clause
-==============================================================================================
-*/
-
-string_value_expression
-  options{k=1;}
-  : character_value_expression; //  | blob_value_expression ;
-
-character_value_expression
-  options{k=1;}
-  : character_factor ( Concatenation_Operator  character_factor )*;
-
-character_factor
-  options{k=1;}
-  :   character_primary
-  ;
-
-character_primary
-  options{k=1;}
-  : value_expression_primary
-  ;
-
-/*
-==============================================================================================
- 6.3 value expression primary
-
-==============================================================================================
-*/
-
-value_expression_primary
-  : parenthesized_value_expression
-  | nonparenthesized_value_expression_primary
-  ;
-
-parenthesized_value_expression
-  options{k=1;}
-  :  Left_Paren! value_expression  Right_Paren!
-  ;
-
-/*
-==============================================================================================
- 6.34 boolean_value_expression  (p277)
- Progress: Completed
-==============================================================================================
-*/
-
-boolean_value_expression
-  options{k=1;}
-	:	boolean_term (OR^ boolean_term)*
-	;
-
-boolean_term
-  options{k=1;}
-  :	boolean_factor (AND^ boolean_factor)*
-	;
-
-boolean_factor
-  : boolean_test
-  | NOT boolean_test -> ^(NOT boolean_test)
-  ;
-
-boolean_test
-  : boolean_primary is_clause?
-  ;
-
-is_clause
-  : IS NOT? t=truth_value -> ^(IS NOT? $t)
-  ;
-
-truth_value
-  : TRUE | FALSE | UNKNOWN
-  ;
-
-boolean_primary
-  options {k=1;}
-  : predicate
-  | boolean_predicand
-  ;
-
-boolean_predicand
-  options {k=1;}
-  : parenthesized_boolean_value_expression
-  | nonparenthesized_value_expression_primary
-  ;
-
-parenthesized_boolean_value_expression
-  options {k=1;}
-  :  Left_Paren! boolean_value_expression  Right_Paren!
-  ;
-
-nonparenthesized_value_expression_primary
-  : fieldName
-  | unsigned_value_specification
-  | q=scalar_subquery -> ^(SUBQUERY $q)
-  | case_expression
-  | funcCall
-  | NULL
-  ;
-
-
-predicate
-  options {k=1;}
-  : comparison_predicate
-  | in_predicate
-  | like_predicate
-  | null_predicate
-  ;
-
-in_predicate
-	:	numeric_value_expression NOT? IN array -> ^(IN numeric_value_expression array NOT?)
-	;
-
-like_predicate
-  : f=fieldName NOT? LIKE s=Character_String_Literal -> ^(LIKE NOT? $f $s)
-  ;
-
-null_predicate
-  : f=numeric_value_expression IS (n=NOT)? NULL -> ^(IS $f NULL $n?)
-  ;
-
-/*
-==============================================================================================
-  6.4 <value specification> and <target specification> (p176)
-==============================================================================================
-*/
-
-value_specification
-    options{k=1;}
-    :   literal
-    |   general_value_specification
-    ;
-
-unsigned_value_specification
-    options{k=1;}
-    :   unsigned_literal
-    |   general_value_specification ;
-
-general_value_specification
-    : identifier_chain
-    ;
-
-/*
-==============================================================================================
-  6.11 case_expression  (p197)
-
-  Progress: Almost done
-  TODO:
-    * when_operand
-    * overlaps_predicate_part_1
-==============================================================================================
-*/
-
-case_expression
-  : case_specification
-  ;
-
-case_abbreviation
-  : NULLIF Left_Paren value_expression  Comma value_expression  Right_Paren
-  | COALESCE Left_Paren value_expression  ( Comma value_expression  )+ Right_Paren
-  ;
-
-case_specification
-  : simple_case
-  | searched_case
-  ;
-
-simple_case
-  : CASE case_operand  ( simple_when_clause )+ ( else_clause  )? END
-  ;
-
-searched_case
-  : CASE^ (searched_when_clause)+ (else_clause)? END!
-  ;
-
-simple_when_clause   :  WHEN when_operand  THEN result ;
-
-searched_when_clause
-  : WHEN c=search_condition THEN r=result -> ^(WHEN $c $r)
-  ;
-
-else_clause
-  : ELSE r=result -> ^(ELSE $r)
-  ;
-
-case_operand
-  options {k=1;}
-  : row_value_predicand
-//| overlaps_predicate_part_1
-  ;
-
-when_operand
-  options {k=1;}
-  : row_value_predicand
-  ;
-
-result
-  : result_expression | NULL
-  ;
-
-result_expression : value_expression;
-
-/*
-==============================================================================================
-  7 Query expressions
-    7.1 <row value constructor> (p293)
-
-  Specify a value or list of values to be constructed into a row or partial row.
-
-  TODO
-    * explicit_row_value_constructor
-    * Others
-==============================================================================================
-*/
-
-row_value_constructor  :
-        common_value_expression
-    |   boolean_value_expression
-//  |   explicit_row_value_constructor
-    ;
-
-row_value_constructor_predicand
-    options {k=1;}
-    :   common_value_expression
-    |   boolean_predicand
-//  |   explicit_row_value_constructor
-    ;
-
-/*
-==============================================================================================
-  7.15 <subquery> (p368)
-
-  Specify a scalar value, a row, or a table derived from a query_expression .
-==============================================================================================
-*/
-
-scalar_subquery
-	options {k=1;}
-	:  subquery
-	;
-
-row_subquery
-	options {k=1;}
-	:  subquery
-	;
-
-table_subquery
-  : subquery
-  ;
-
-subquery
-	options {k=1;}
-	:  Left_Paren! query_expression Right_Paren!
-	;
-
-/*
-==============================================================================================
-  7.2 row_value_expression  (p296)
-
-  Specify a row value.
-
-  TODO
-    * explicit_row_value_constructor
-==============================================================================================
-*/
-
-row_value_expression
-    options {k=1;}
-    :   row_value_special_case
-  //|   explicit_row_value_constructor
-    ;
-
-table_row_value_expression
-    options {k=1;}
-    :   row_value_special_case
-    |   row_value_constructor
-    ;
-
-contextually_typed_row_value_expression
-    options {k=1;}
-    :   row_value_special_case
-//  |   contextually_typed_row_value_constructor
-    ;
-
-row_value_predicand
-    options {k=1;}
-    : row_value_constructor_predicand
-    | row_value_special_case
-    ;
-
-row_value_special_case
-    options {k=1;}
-    :  nonparenthesized_value_expression_primary
-    ;
-
-/*
-==============================================================================================
-  8.2 <comparison predicate> (p373)
-
-  Progress: completed
-==============================================================================================
-*/
-
-comparison_predicate
-  options{k=1;}
-	:	l=row_value_predicand c=comp_op r=row_value_predicand -> ^($c $l $r)
-	;
-
-comp_op
-  :	Equals_Operator
-  |	Not_Equals_Operator
-  |	Less_Than_Operator
-  |	Less_Or_Equals_Operator
-  |	Greater_Than_Operator
-  |	Greater_Or_Equals_Operator
-  ;
-
-/*
-==============================================================================================
-  The Quote symbol  rule consists of two immediately adjacent Quote marks with no spaces.
-  As usual, this would be best handled in the lexical analyzer, not in the grammar.
-==============================================================================================
-*/
-
-quote_symbol : Quote Quote;
-sign : Plus_Sign | Minus_Sign;
-
-datetime_literal
-  : date_literal  | time_literal  | timestamp_literal
-  ;
-date_literal
-  : DATE Quote years_value  Minus_Sign months_value Minus_Sign days_value  Quote
-  ;
-time_literal
-  : TIME Quote hours_value  Colon minutes_value  Colon seconds_value
-    (  ( Plus_Sign | Minus_Sign )  hours_value  Colon minutes_value )?  Quote
-  ;
-timestamp_literal
-  : TIMESTAMP Quote  years_value  Minus_Sign months_value Minus_Sign days_value
-    Space hours_value  Colon minutes_value  Colon seconds_value
-    (  ( Plus_Sign | Minus_Sign )  hours_value  Colon minutes_value )?
-    Quote
-  ;
-interval_literal
-  : INTERVAL
-    Quote ( Plus_Sign | Minus_Sign )? ( year_month_literal | day_time_literal ) Quote
-  ;
-
-year_month_literal : years_value  | years_value  Minus_Sign months_value ;
-day_time_literal   : day_time_interval  | time_interval ;
-day_time_interval
-  : DAYS days_value  Space hours_value  ( Colon minutes_value  ( Colon seconds_value  )? )?
-  ;
-time_interval      : HOURS hours_value  ( Colon minutes_value   ( Colon seconds_value )? )?;
-
-years_value        : Unsigned_Integer ;
-months_value       : Unsigned_Integer ;
-days_value         : Unsigned_Integer ;
-hours_value        : Unsigned_Integer ;
-minutes_value      : Unsigned_Integer ;
-seconds_value      : Unsigned_Integer  ( Period ( Unsigned_Integer  )? )?;

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/00c3ee2b/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/QueryBlock.java
----------------------------------------------------------------------
diff --git a/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/QueryBlock.java b/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/QueryBlock.java
deleted file mode 100644
index 0f2042d..0000000
--- a/tajo-frontend/tajo-frontend-sql/src/main/java/org/apache/tajo/frontend/sql/QueryBlock.java
+++ /dev/null
@@ -1,133 +0,0 @@
-/**
- * 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.frontend.sql;
-
-import org.apache.tajo.algebra.*;
-
-public class QueryBlock {
-  private String relName;
-
-  /* projection */
-  private Projection projection;
-  /* distinct or all? */
-  private boolean distinct = false;
-  /* from clause or join clause */
-  private Expr tableExpression = null;
-  /* where clause */
-  private Expr searchCondition = null;
-  /* if true, there is at least grouping field. */
-  private Aggregation aggregation = null;
-  /* having condition */
-  private Expr havingCondition = null;
-  /* keys for ordering */
-  private Sort sort = null;
-  /* limit clause */
-  private Limit limit = null;
-
-  public QueryBlock() {
-  }
-
-  public void setProjection(Projection projection) {
-    this.projection = projection;
-  }
-
-  public boolean hasTableExpression() {
-    return this.tableExpression != null;
-  }
-
-  public Expr getTableExpression() {
-    return this.tableExpression;
-  }
-
-  public void setTableExpression(Expr fromClause) {
-    this.tableExpression = fromClause;
-  }
-
-  public boolean hasSearchCondition() {
-    return searchCondition != null;
-  }
-
-  public Expr getSearchCondition() {
-    return this.searchCondition;
-  }
-
-  public void setSearchCondition(Expr expr) {
-    this.searchCondition = expr;
-  }
-
-  public boolean hasAggregation() {
-    return this.aggregation != null;
-  }
-
-  public Aggregation getAggregation() {
-    return this.aggregation;
-  }
-
-  public void setAggregation(Aggregation groupby) {
-    this.aggregation = groupby;
-  }
-
-  public boolean hasHavingCondition(Expr expr) {
-    return havingCondition != null;
-  }
-
-  public Expr getHavingCondition() {
-    return havingCondition;
-  }
-
-  public void setHavingCondition(Expr expr) {
-    havingCondition = expr;
-  }
-
-  public boolean hasSort() {
-    return sort != null;
-  }
-
-  public Sort getSort() {
-    return sort;
-  }
-
-  public void setSort(Sort sort) {
-    this.sort = sort;
-  }
-
-  public boolean hasLimit() {
-    return limit != null;
-  }
-
-  public Limit getLimit() {
-    return this.limit;
-  }
-
-  public void setLimit(Limit limit) {
-    this.limit = limit;
-  }
-
-  public Projection getProjection() {
-    return this.projection;
-  }
-
-  public boolean isDistinct() {
-    return this.distinct;
-  }
-
-  public void setDistinct() {
-    this.distinct = true;
-  }
-}