You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by jh...@apache.org on 2019/08/27 06:52:04 UTC

[calcite] branch master updated (77e7808 -> e5ae179)

This is an automated email from the ASF dual-hosted git repository.

jhyde pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git.


    from 77e7808  [CALCITE-3283] RelSubset does not contain its best RelNode (Xiening Dai)
     new be678e3  [CALCITE-3260] Add Expressions.evaluate(Node), a public API for evaluating linq4j expressions (Wang Yanlin)
     new 8a3fde5  In SqlFunctionsTest, replace assertEquals and assertNull with assertThat
     new b039b15  [CALCITE-3280] Add REGEXP_REPLACE function in Oracle, MySQL libraries (Shuming Li)
     new e5ae179  [CALCITE-3122] Convert Pig Latin scripts into Calcite relational algebra and Calcite SQL (Khai Tran)

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../calcite/adapter/enumerable/EnumUtils.java      |   18 +-
 .../calcite/adapter/enumerable/RexImpTable.java    |   19 +
 .../org/apache/calcite/plan/RelOptCluster.java     |    2 +-
 .../apache/calcite/prepare/RelOptTableImpl.java    |   10 +
 .../org/apache/calcite/rel/core/Uncollect.java     |    6 +-
 .../calcite/rel/logical/ToLogicalConverter.java    |  162 ++
 .../calcite/rel/rel2sql/RelToSqlConverter.java     |  117 ++
 .../apache/calcite/rel/rel2sql/SqlImplementor.java |   42 +-
 .../apache/calcite/runtime/CalciteResource.java    |    3 +
 .../org/apache/calcite/runtime/SqlFunctions.java   |   66 +-
 .../calcite/schema/impl/ScalarFunctionImpl.java    |   14 +
 .../apache/calcite/sql/SqlBasicTypeNameSpec.java   |    2 +-
 .../org/apache/calcite/sql/SqlLateralOperator.java |    5 +-
 .../main/java/org/apache/calcite/sql/SqlNode.java  |   21 +-
 .../org/apache/calcite/sql/SqlUnnestOperator.java  |    9 +-
 .../calcite/sql/fun/SqlLibraryOperators.java       |    5 +-
 ...Function.java => SqlRegexpReplaceFunction.java} |   52 +-
 .../calcite/sql/validate/SqlValidatorImpl.java     |    4 +-
 .../org/apache/calcite/tools/PigRelBuilder.java    |   27 +-
 .../java/org/apache/calcite/tools/RelBuilder.java  |   22 +-
 .../org/apache/calcite/util/BuiltInMethod.java     |    8 +
 .../main/java/org/apache/calcite/util/Unsafe.java  |   35 +
 .../calcite/runtime/CalciteResource.properties     |    1 +
 .../rel/logical/ToLogicalConverterTest.java        |  411 +++++
 .../rel/rel2sql/RelToSqlConverterStructsTest.java  |    7 +-
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java |   36 +
 .../apache/calcite/sql/parser/SqlParserTest.java   |   12 +-
 .../calcite/sql/test/SqlOperatorBaseTest.java      |   39 +
 .../java/org/apache/calcite/test/Matchers.java     |   19 +-
 .../org/apache/calcite/test/SqlFunctionsTest.java  |  538 ++++---
 kafka/pom.xml                                      |    4 +-
 .../apache/calcite/linq4j/tree/Expressions.java    |   10 +
 .../apache/calcite/linq4j/test/ExpressionTest.java |    9 +
 piglet/pom.xml                                     |   25 +
 .../calcite/piglet/DynamicTupleRecordType.java     |   76 +
 .../org/apache/calcite/piglet/PigConverter.java    |  288 ++++
 .../org/apache/calcite/piglet/PigRelBuilder.java   |  658 ++++++++
 .../org/apache/calcite/piglet/PigRelExVisitor.java |  452 ++++++
 .../org/apache/calcite/piglet/PigRelExWalker.java  |   84 +
 .../calcite/piglet/PigRelOpInnerVisitor.java       |  251 +++
 .../org/apache/calcite/piglet/PigRelOpVisitor.java |  722 +++++++++
 .../org/apache/calcite/piglet/PigRelOpWalker.java  |   92 ++
 .../org/apache/calcite/piglet/PigRelSqlUdfs.java   |  361 +++++
 .../calcite/piglet/PigRelToSqlConverter.java       |  128 ++
 .../apache/calcite/piglet/PigRelUdfConverter.java  |  195 +++
 .../java/org/apache/calcite/piglet/PigTable.java   |   76 +
 .../calcite/piglet/PigToSqlAggregateRule.java      |  408 +++++
 .../java/org/apache/calcite/piglet/PigTypes.java   |  200 +++
 .../org/apache/calcite/piglet/PigUdfFinder.java    |  112 ++
 .../java/org/apache/calcite/piglet/PigUdfs.java    |   28 +-
 .../calcite/piglet/PigUserDefinedFunction.java     |   71 +-
 .../org/apache/calcite/test/CalciteHandler.java    |   20 +-
 .../java/org/apache/calcite/test/PigRelExTest.java |  250 +++
 .../java/org/apache/calcite/test/PigRelOpTest.java | 1631 ++++++++++++++++++++
 .../org/apache/calcite/test/PigRelTestBase.java    |   23 +-
 .../org/apache/calcite/test/PigScriptTest.java     |   79 +
 piglet/src/test/resources/log4j.properties         |    4 +-
 piglet/src/test/resources/testPig.pig              |   44 +
 .../org/apache/calcite/sql/ddl/SqlCreateTable.java |    4 +-
 site/_docs/history.md                              |    2 +-
 site/_docs/reference.md                            |    3 +-
 61 files changed, 7632 insertions(+), 390 deletions(-)
 create mode 100644 core/src/main/java/org/apache/calcite/rel/logical/ToLogicalConverter.java
 copy core/src/main/java/org/apache/calcite/sql/fun/{SqlJsonRemoveFunction.java => SqlRegexpReplaceFunction.java} (57%)
 create mode 100644 core/src/test/java/org/apache/calcite/rel/logical/ToLogicalConverterTest.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/DynamicTupleRecordType.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigConverter.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigRelBuilder.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigRelExVisitor.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigRelExWalker.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigRelOpInnerVisitor.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigRelOpVisitor.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigRelOpWalker.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigRelSqlUdfs.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigRelToSqlConverter.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigRelUdfConverter.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigTable.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigToSqlAggregateRule.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigTypes.java
 create mode 100644 piglet/src/main/java/org/apache/calcite/piglet/PigUdfFinder.java
 copy linq4j/src/main/java/org/apache/calcite/linq4j/function/BigDecimalFunction1.java => piglet/src/main/java/org/apache/calcite/piglet/PigUdfs.java (52%)
 copy core/src/main/java/org/apache/calcite/sql/validate/SqlUserDefinedFunction.java => piglet/src/main/java/org/apache/calcite/piglet/PigUserDefinedFunction.java (50%)
 create mode 100644 piglet/src/test/java/org/apache/calcite/test/PigRelExTest.java
 create mode 100644 piglet/src/test/java/org/apache/calcite/test/PigRelOpTest.java
 copy core/src/main/java/org/apache/calcite/rel/stream/LogicalChi.java => piglet/src/test/java/org/apache/calcite/test/PigRelTestBase.java (64%)
 create mode 100644 piglet/src/test/java/org/apache/calcite/test/PigScriptTest.java
 create mode 100644 piglet/src/test/resources/testPig.pig


[calcite] 01/04: [CALCITE-3260] Add Expressions.evaluate(Node), a public API for evaluating linq4j expressions (Wang Yanlin)

Posted by jh...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jhyde pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git

commit be678e3dc65f5933f85927e53b2811738d124f11
Author: yanzhi <ya...@antfin.com>
AuthorDate: Sat Aug 17 17:20:40 2019 +0800

    [CALCITE-3260] Add Expressions.evaluate(Node), a public API for evaluating linq4j expressions (Wang Yanlin)
    
    And a couple of cosmetic changes.
    
    Close apache/calcite#1387
---
 .../calcite/rel/rel2sql/RelToSqlConverterStructsTest.java      |  7 ++++---
 .../main/java/org/apache/calcite/linq4j/tree/Expressions.java  | 10 ++++++++++
 .../java/org/apache/calcite/linq4j/test/ExpressionTest.java    |  9 +++++++++
 .../main/java/org/apache/calcite/sql/ddl/SqlCreateTable.java   |  4 ++--
 4 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterStructsTest.java b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterStructsTest.java
index 0bf9ce0..c3ffd19 100644
--- a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterStructsTest.java
+++ b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterStructsTest.java
@@ -115,9 +115,10 @@ public class RelToSqlConverterStructsTest {
       RelDataType bigint = tf.createSqlType(SqlTypeName.BIGINT);
       RelDataType n1Type = tf.createStructType(
           ImmutableList.of(
-              tf.createStructType(ImmutableList.of(bigint), ImmutableList.of("b")),
-              tf.createStructType(ImmutableList.of(bigint), ImmutableList.of("c"))
-          ),
+              tf.createStructType(ImmutableList.of(bigint),
+                  ImmutableList.of("b")),
+              tf.createStructType(ImmutableList.of(bigint),
+                  ImmutableList.of("c"))),
           ImmutableList.of("n11", "n12"));
       RelDataType n2Type = tf.createStructType(
           ImmutableList.of(bigint),
diff --git a/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Expressions.java b/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Expressions.java
index 1c92876..4d32103 100644
--- a/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Expressions.java
+++ b/linq4j/src/main/java/org/apache/calcite/linq4j/tree/Expressions.java
@@ -38,6 +38,7 @@ import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
+import java.util.Objects;
 import java.util.UUID;
 
 /**
@@ -3051,6 +3052,15 @@ public abstract class Expressions {
     return new FluentArrayList<>(toList(ts));
   }
 
+  /**
+   * Evaluates an expression and returns the result.
+   */
+  public static Object evaluate(Node node) {
+    Objects.requireNonNull(node);
+    final Evaluator evaluator = new Evaluator();
+    return ((AbstractNode) node).evaluate(evaluator);
+  }
+
   // ~ Private helper methods ------------------------------------------------
 
   private static boolean shouldLift(Expression left, Expression right,
diff --git a/linq4j/src/test/java/org/apache/calcite/linq4j/test/ExpressionTest.java b/linq4j/src/test/java/org/apache/calcite/linq4j/test/ExpressionTest.java
index 67c529a..8515041 100644
--- a/linq4j/src/test/java/org/apache/calcite/linq4j/test/ExpressionTest.java
+++ b/linq4j/src/test/java/org/apache/calcite/linq4j/test/ExpressionTest.java
@@ -52,6 +52,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.TreeSet;
 
+import static org.apache.calcite.linq4j.test.BlockBuilderBase.ONE;
+import static org.apache.calcite.linq4j.test.BlockBuilderBase.TWO;
+
 import static org.hamcrest.core.Is.is;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertThat;
@@ -1506,6 +1509,12 @@ public class ExpressionTest {
         Expressions.toString(Expressions.constant(map)));
   }
 
+  @Test public void testEvaluate() {
+    Expression x = Expressions.add(ONE, TWO);
+    Object value = Expressions.evaluate(x);
+    assertThat(value, is(3));
+  }
+
   /** An enum. */
   enum MyEnum {
     X,
diff --git a/server/src/main/java/org/apache/calcite/sql/ddl/SqlCreateTable.java b/server/src/main/java/org/apache/calcite/sql/ddl/SqlCreateTable.java
index 9eb5ba2..ef92070 100644
--- a/server/src/main/java/org/apache/calcite/sql/ddl/SqlCreateTable.java
+++ b/server/src/main/java/org/apache/calcite/sql/ddl/SqlCreateTable.java
@@ -164,8 +164,8 @@ public class SqlCreateTable extends SqlCreate
     final ImmutableList.Builder<ColumnDef> b = ImmutableList.builder();
     final RelDataTypeFactory.Builder builder = typeFactory.builder();
     final RelDataTypeFactory.Builder storedBuilder = typeFactory.builder();
-    // REVIEW 2019-08-19 Danny Chan: Should we implement the #validate(SqlValidator)
-    // to get the SqlValidator instance ?
+    // REVIEW 2019-08-19 Danny Chan: Should we implement the
+    // #validate(SqlValidator) to get the SqlValidator instance?
     final SqlValidator validator = SqlDdlNodes.validator(context, true);
     for (Ord<SqlNode> c : Ord.zip(columnList)) {
       if (c.e instanceof SqlColumnDeclaration) {


[calcite] 02/04: In SqlFunctionsTest, replace assertEquals and assertNull with assertThat

Posted by jh...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jhyde pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git

commit 8a3fde56e6931ca8c5a12d22489c6ce0eae4a8bc
Author: Julian Hyde <jh...@apache.org>
AuthorDate: Mon Aug 26 13:28:31 2019 -0700

    In SqlFunctionsTest, replace assertEquals and assertNull with assertThat
---
 .../org/apache/calcite/test/SqlFunctionsTest.java  | 498 +++++++++++----------
 1 file changed, 252 insertions(+), 246 deletions(-)

diff --git a/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java b/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
index 8f788d7..69c3963 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
@@ -48,12 +48,12 @@ import static org.apache.calcite.runtime.SqlFunctions.subtractMonths;
 import static org.apache.calcite.runtime.SqlFunctions.toBase64;
 import static org.apache.calcite.runtime.SqlFunctions.trim;
 import static org.apache.calcite.runtime.SqlFunctions.upper;
+import static org.apache.calcite.test.Matchers.within;
 
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
 import static org.hamcrest.core.AnyOf.anyOf;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;
@@ -63,150 +63,156 @@ import static java.nio.charset.StandardCharsets.UTF_8;
 /**
  * Unit test for the methods in {@link SqlFunctions} that implement SQL
  * functions.
+ *
+ * <p>Developers, please use {@link org.junit.Assert#assertThat assertThat}
+ * rather than {@code assertEquals}.
  */
 public class SqlFunctionsTest {
   @Test public void testCharLength() {
-    assertEquals(3, charLength("xyz"));
+    assertThat(charLength("xyz"), is(3));
   }
 
   @Test public void testConcat() {
-    assertEquals("a bcd", concat("a b", "cd"));
+    assertThat(concat("a b", "cd"), is("a bcd"));
     // The code generator will ensure that nulls are never passed in. If we
     // pass in null, it is treated like the string "null", as the following
     // tests show. Not the desired behavior for SQL.
-    assertEquals("anull", concat("a", null));
-    assertEquals("nullnull", concat((String) null, null));
-    assertEquals("nullb", concat(null, "b"));
+    assertThat(concat("a", null), is("anull"));
+    assertThat(concat((String) null, null), is("nullnull"));
+    assertThat(concat(null, "b"), is("nullb"));
   }
 
   @Test public void testPosixRegex() {
-    assertEquals(true, posixRegex("abc", "abc", true));
-    assertEquals(true, posixRegex("abc", "^a", true));
-    assertEquals(true, posixRegex("abc", "(b|d)", true));
-    assertEquals(false, posixRegex("abc", "^(b|c)", true));
+    assertThat(posixRegex("abc", "abc", true), is(true));
+    assertThat(posixRegex("abc", "^a", true), is(true));
+    assertThat(posixRegex("abc", "(b|d)", true), is(true));
+    assertThat(posixRegex("abc", "^(b|c)", true), is(false));
 
-    assertEquals(true, posixRegex("abc", "ABC", false));
-    assertEquals(true, posixRegex("abc", "^A", false));
-    assertEquals(true, posixRegex("abc", "(B|D)", false));
-    assertEquals(false, posixRegex("abc", "^(B|C)", false));
+    assertThat(posixRegex("abc", "ABC", false), is(true));
+    assertThat(posixRegex("abc", "^A", false), is(true));
+    assertThat(posixRegex("abc", "(B|D)", false), is(true));
+    assertThat(posixRegex("abc", "^(B|C)", false), is(false));
 
-    assertEquals(false, posixRegex("abc", "^[[:xdigit:]]$", false));
-    assertEquals(true, posixRegex("abc", "^[[:xdigit:]]+$", false));
-    assertEquals(false, posixRegex("abcq", "^[[:xdigit:]]+$", false));
+    assertThat(posixRegex("abc", "^[[:xdigit:]]$", false), is(false));
+    assertThat(posixRegex("abc", "^[[:xdigit:]]+$", false), is(true));
+    assertThat(posixRegex("abcq", "^[[:xdigit:]]+$", false), is(false));
 
-    assertEquals(true, posixRegex("abc", "[[:xdigit:]]", false));
-    assertEquals(true, posixRegex("abc", "[[:xdigit:]]+", false));
-    assertEquals(true, posixRegex("abcq", "[[:xdigit:]]", false));
+    assertThat(posixRegex("abc", "[[:xdigit:]]", false), is(true));
+    assertThat(posixRegex("abc", "[[:xdigit:]]+", false), is(true));
+    assertThat(posixRegex("abcq", "[[:xdigit:]]", false), is(true));
   }
 
   @Test public void testLower() {
-    assertEquals("a bcd iijk", lower("A bCd Iijk"));
+    assertThat(lower("A bCd Iijk"), is("a bcd iijk"));
   }
 
   @Test public void testFromBase64() {
     final List<String> expectedList =
-            Arrays.asList("", "\0", "0", "a", " ", "\n", "\r\n", "\u03C0", "hello\tword");
-    for (String expected: expectedList) {
-      assertEquals(new ByteString(expected.getBytes(UTF_8)), fromBase64(toBase64(expected)));
+        Arrays.asList("", "\0", "0", "a", " ", "\n", "\r\n", "\u03C0",
+            "hello\tword");
+    for (String expected : expectedList) {
+      assertThat(fromBase64(toBase64(expected)),
+          is(new ByteString(expected.getBytes(UTF_8))));
     }
     assertThat("546869732069732061207465737420537472696e672e",
-            is(fromBase64("VGhpcyB  pcyBh\rIHRlc3Qg\tU3Ry\naW5nLg==").toString()));
-    assertNull(null, fromBase64("-1"));
+        is(fromBase64("VGhpcyB  pcyBh\rIHRlc3Qg\tU3Ry\naW5nLg==").toString()));
+    assertThat(fromBase64("-1"), nullValue());
   }
 
   @Test public void testToBase64() {
-    assertThat(
-            "VGhpcyBpcyBhIHRlc3QgU3RyaW5nLiBjaGVjayByZXN1bHRlIG91dCBvZiA3NlRoaXMgaXMgYSB0\n"
-                    + "ZXN0IFN0cmluZy5UaGlzIGlzIGEgdGVzdCBTdHJpbmcuVGhpcyBpcyBhIHRlc3QgU3RyaW5nLlRo\n"
-                    + "aXMgaXMgYSB0ZXN0IFN0cmluZy5UaGlzIGlzIGEgdGVzdCBTdHJpbmcuIFRoaXMgaXMgYSB0ZXN0\n"
-                    + "IFN0cmluZy4gY2hlY2sgcmVzdWx0ZSBvdXQgb2YgNzZUaGlzIGlzIGEgdGVzdCBTdHJpbmcuVGhp\n"
-                    + "cyBpcyBhIHRlc3QgU3RyaW5nLlRoaXMgaXMgYSB0ZXN0IFN0cmluZy5UaGlzIGlzIGEgdGVzdCBT\n"
-                    + "dHJpbmcuVGhpcyBpcyBhIHRlc3QgU3RyaW5nLiBUaGlzIGlzIGEgdGVzdCBTdHJpbmcuIGNoZWNr\n"
-                    + "IHJlc3VsdGUgb3V0IG9mIDc2VGhpcyBpcyBhIHRlc3QgU3RyaW5nLlRoaXMgaXMgYSB0ZXN0IFN0\n"
-                    + "cmluZy5UaGlzIGlzIGEgdGVzdCBTdHJpbmcuVGhpcyBpcyBhIHRlc3QgU3RyaW5nLlRoaXMgaXMg\n"
-                    + "YSB0ZXN0IFN0cmluZy4=",
-            is(
-            toBase64("This is a test String. check resulte out of 76This is a test String."
-                    + "This is a test String.This is a test String.This is a test String."
-                    + "This is a test String. This is a test String. check resulte out of 76"
-                    + "This is a test String.This is a test String.This is a test String."
-                    + "This is a test String.This is a test String. This is a test String. "
-                    + "check resulte out of 76This is a test String.This is a test String."
-                    + "This is a test String.This is a test String.This is a test String.")));
-    assertThat("", is(toBase64("")));
+    final String s = ""
+        + "This is a test String. check resulte out of 76This is a test String."
+        + "This is a test String.This is a test String.This is a test String."
+        + "This is a test String. This is a test String. check resulte out of 76"
+        + "This is a test String.This is a test String.This is a test String."
+        + "This is a test String.This is a test String. This is a test String. "
+        + "check resulte out of 76This is a test String.This is a test String."
+        + "This is a test String.This is a test String.This is a test String.";
+    final String actual = ""
+        + "VGhpcyBpcyBhIHRlc3QgU3RyaW5nLiBjaGVjayByZXN1bHRlIG91dCBvZiA3NlRoaXMgaXMgYSB0\n"
+        + "ZXN0IFN0cmluZy5UaGlzIGlzIGEgdGVzdCBTdHJpbmcuVGhpcyBpcyBhIHRlc3QgU3RyaW5nLlRo\n"
+        + "aXMgaXMgYSB0ZXN0IFN0cmluZy5UaGlzIGlzIGEgdGVzdCBTdHJpbmcuIFRoaXMgaXMgYSB0ZXN0\n"
+        + "IFN0cmluZy4gY2hlY2sgcmVzdWx0ZSBvdXQgb2YgNzZUaGlzIGlzIGEgdGVzdCBTdHJpbmcuVGhp\n"
+        + "cyBpcyBhIHRlc3QgU3RyaW5nLlRoaXMgaXMgYSB0ZXN0IFN0cmluZy5UaGlzIGlzIGEgdGVzdCBT\n"
+        + "dHJpbmcuVGhpcyBpcyBhIHRlc3QgU3RyaW5nLiBUaGlzIGlzIGEgdGVzdCBTdHJpbmcuIGNoZWNr\n"
+        + "IHJlc3VsdGUgb3V0IG9mIDc2VGhpcyBpcyBhIHRlc3QgU3RyaW5nLlRoaXMgaXMgYSB0ZXN0IFN0\n"
+        + "cmluZy5UaGlzIGlzIGEgdGVzdCBTdHJpbmcuVGhpcyBpcyBhIHRlc3QgU3RyaW5nLlRoaXMgaXMg\n"
+        + "YSB0ZXN0IFN0cmluZy4=";
+    assertThat(toBase64(s), is(actual));
+    assertThat(toBase64(""), is(""));
   }
 
   @Test public void testUpper() {
-    assertEquals("A BCD IIJK", upper("A bCd iIjk"));
+    assertThat(upper("A bCd iIjk"), is("A BCD IIJK"));
   }
 
   @Test public void testInitcap() {
-    assertEquals("Aa", initcap("aA"));
-    assertEquals("Zz", initcap("zz"));
-    assertEquals("Az", initcap("AZ"));
-    assertEquals("Try A Little  ", initcap("tRy a littlE  "));
-    assertEquals("Won'T It?No", initcap("won't it?no"));
-    assertEquals("1a", initcap("1A"));
-    assertEquals(" B0123b", initcap(" b0123B"));
+    assertThat(initcap("aA"), is("Aa"));
+    assertThat(initcap("zz"), is("Zz"));
+    assertThat(initcap("AZ"), is("Az"));
+    assertThat(initcap("tRy a littlE  "), is("Try A Little  "));
+    assertThat(initcap("won't it?no"), is("Won'T It?No"));
+    assertThat(initcap("1A"), is("1a"));
+    assertThat(initcap(" b0123B"), is(" B0123b"));
   }
 
   @Test public void testLesser() {
-    assertEquals("a", lesser("a", "bc"));
-    assertEquals("ac", lesser("bc", "ac"));
+    assertThat(lesser("a", "bc"), is("a"));
+    assertThat(lesser("bc", "ac"), is("ac"));
     try {
       Object o = lesser("a", null);
       fail("Expected NPE, got " + o);
     } catch (NullPointerException e) {
       // ok
     }
-    assertEquals("a", lesser(null, "a"));
-    assertNull(lesser((String) null, null));
+    assertThat(lesser(null, "a"), is("a"));
+    assertThat(lesser((String) null, null), nullValue());
   }
 
   @Test public void testGreater() {
-    assertEquals("bc", greater("a", "bc"));
-    assertEquals("bc", greater("bc", "ac"));
+    assertThat(greater("a", "bc"), is("bc"));
+    assertThat(greater("bc", "ac"), is("bc"));
     try {
       Object o = greater("a", null);
       fail("Expected NPE, got " + o);
     } catch (NullPointerException e) {
       // ok
     }
-    assertEquals("a", greater(null, "a"));
-    assertNull(greater((String) null, null));
+    assertThat(greater(null, "a"), is("a"));
+    assertThat(greater((String) null, null), nullValue());
   }
 
   /** Test for {@link SqlFunctions#rtrim}. */
   @Test public void testRtrim() {
-    assertEquals("", rtrim(""));
-    assertEquals("", rtrim("    "));
-    assertEquals("   x", rtrim("   x  "));
-    assertEquals("   x", rtrim("   x "));
-    assertEquals("   x y", rtrim("   x y "));
-    assertEquals("   x", rtrim("   x"));
-    assertEquals("x", rtrim("x"));
+    assertThat(rtrim(""), is(""));
+    assertThat(rtrim("    "), is(""));
+    assertThat(rtrim("   x  "), is("   x"));
+    assertThat(rtrim("   x "), is("   x"));
+    assertThat(rtrim("   x y "), is("   x y"));
+    assertThat(rtrim("   x"), is("   x"));
+    assertThat(rtrim("x"), is("x"));
   }
 
   /** Test for {@link SqlFunctions#ltrim}. */
   @Test public void testLtrim() {
-    assertEquals("", ltrim(""));
-    assertEquals("", ltrim("    "));
-    assertEquals("x  ", ltrim("   x  "));
-    assertEquals("x ", ltrim("   x "));
-    assertEquals("x y ", ltrim("x y "));
-    assertEquals("x", ltrim("   x"));
-    assertEquals("x", ltrim("x"));
+    assertThat(ltrim(""), is(""));
+    assertThat(ltrim("    "), is(""));
+    assertThat(ltrim("   x  "), is("x  "));
+    assertThat(ltrim("   x "), is("x "));
+    assertThat(ltrim("x y "), is("x y "));
+    assertThat(ltrim("   x"), is("x"));
+    assertThat(ltrim("x"), is("x"));
   }
 
   /** Test for {@link SqlFunctions#trim}. */
   @Test public void testTrim() {
-    assertEquals("", trimSpacesBoth(""));
-    assertEquals("", trimSpacesBoth("    "));
-    assertEquals("x", trimSpacesBoth("   x  "));
-    assertEquals("x", trimSpacesBoth("   x "));
-    assertEquals("x y", trimSpacesBoth("   x y "));
-    assertEquals("x", trimSpacesBoth("   x"));
-    assertEquals("x", trimSpacesBoth("x"));
+    assertThat(trimSpacesBoth(""), is(""));
+    assertThat(trimSpacesBoth("    "), is(""));
+    assertThat(trimSpacesBoth("   x  "), is("x"));
+    assertThat(trimSpacesBoth("   x "), is("x"));
+    assertThat(trimSpacesBoth("   x y "), is("x y"));
+    assertThat(trimSpacesBoth("   x"), is("x"));
+    assertThat(trimSpacesBoth("x"), is("x"));
   }
 
   static String trimSpacesBoth(String s) {
@@ -296,203 +302,203 @@ public class SqlFunctionsTest {
     final List<String> abc = Arrays.asList("a", "b", "c");
     final List<String> a = Collections.singletonList("a");
     final List<String> empty = Collections.emptyList();
-    assertEquals(0, Utilities.compare(ac, ac));
-    assertEquals(0, Utilities.compare(ac, new ArrayList<>(ac)));
-    assertEquals(-1, Utilities.compare(a, ac));
-    assertEquals(-1, Utilities.compare(empty, ac));
-    assertEquals(1, Utilities.compare(ac, a));
-    assertEquals(1, Utilities.compare(ac, abc));
-    assertEquals(1, Utilities.compare(ac, empty));
-    assertEquals(0, Utilities.compare(empty, empty));
+    assertThat(Utilities.compare(ac, ac), is(0));
+    assertThat(Utilities.compare(ac, new ArrayList<>(ac)), is(0));
+    assertThat(Utilities.compare(a, ac), is(-1));
+    assertThat(Utilities.compare(empty, ac), is(-1));
+    assertThat(Utilities.compare(ac, a), is(1));
+    assertThat(Utilities.compare(ac, abc), is(1));
+    assertThat(Utilities.compare(ac, empty), is(1));
+    assertThat(Utilities.compare(empty, empty), is(0));
   }
 
   @Test public void testTruncateLong() {
-    assertEquals(12000L, SqlFunctions.truncate(12345L, 1000L));
-    assertEquals(12000L, SqlFunctions.truncate(12000L, 1000L));
-    assertEquals(12000L, SqlFunctions.truncate(12001L, 1000L));
-    assertEquals(11000L, SqlFunctions.truncate(11999L, 1000L));
+    assertThat(SqlFunctions.truncate(12345L, 1000L), is(12000L));
+    assertThat(SqlFunctions.truncate(12000L, 1000L), is(12000L));
+    assertThat(SqlFunctions.truncate(12001L, 1000L), is(12000L));
+    assertThat(SqlFunctions.truncate(11999L, 1000L), is(11000L));
 
-    assertEquals(-13000L, SqlFunctions.truncate(-12345L, 1000L));
-    assertEquals(-12000L, SqlFunctions.truncate(-12000L, 1000L));
-    assertEquals(-13000L, SqlFunctions.truncate(-12001L, 1000L));
-    assertEquals(-12000L, SqlFunctions.truncate(-11999L, 1000L));
+    assertThat(SqlFunctions.truncate(-12345L, 1000L), is(-13000L));
+    assertThat(SqlFunctions.truncate(-12000L, 1000L), is(-12000L));
+    assertThat(SqlFunctions.truncate(-12001L, 1000L), is(-13000L));
+    assertThat(SqlFunctions.truncate(-11999L, 1000L), is(-12000L));
   }
 
   @Test public void testTruncateInt() {
-    assertEquals(12000, SqlFunctions.truncate(12345, 1000));
-    assertEquals(12000, SqlFunctions.truncate(12000, 1000));
-    assertEquals(12000, SqlFunctions.truncate(12001, 1000));
-    assertEquals(11000, SqlFunctions.truncate(11999, 1000));
+    assertThat(SqlFunctions.truncate(12345, 1000), is(12000));
+    assertThat(SqlFunctions.truncate(12000, 1000), is(12000));
+    assertThat(SqlFunctions.truncate(12001, 1000), is(12000));
+    assertThat(SqlFunctions.truncate(11999, 1000), is(11000));
 
-    assertEquals(-13000, SqlFunctions.truncate(-12345, 1000));
-    assertEquals(-12000, SqlFunctions.truncate(-12000, 1000));
-    assertEquals(-13000, SqlFunctions.truncate(-12001, 1000));
-    assertEquals(-12000, SqlFunctions.truncate(-11999, 1000));
+    assertThat(SqlFunctions.truncate(-12345, 1000), is(-13000));
+    assertThat(SqlFunctions.truncate(-12000, 1000), is(-12000));
+    assertThat(SqlFunctions.truncate(-12001, 1000), is(-13000));
+    assertThat(SqlFunctions.truncate(-11999, 1000), is(-12000));
 
-    assertEquals(12000, SqlFunctions.round(12345, 1000));
-    assertEquals(13000, SqlFunctions.round(12845, 1000));
-    assertEquals(-12000, SqlFunctions.round(-12345, 1000));
-    assertEquals(-13000, SqlFunctions.round(-12845, 1000));
+    assertThat(SqlFunctions.round(12345, 1000), is(12000));
+    assertThat(SqlFunctions.round(12845, 1000), is(13000));
+    assertThat(SqlFunctions.round(-12345, 1000), is(-12000));
+    assertThat(SqlFunctions.round(-12845, 1000), is(-13000));
   }
 
   @Test public void testSTruncateDouble() {
-    assertEquals(12.345d, SqlFunctions.struncate(12.345d, 3), 0.001);
-    assertEquals(12.340d, SqlFunctions.struncate(12.345d, 2), 0.001);
-    assertEquals(12.300d, SqlFunctions.struncate(12.345d, 1), 0.001);
-    assertEquals(12.000d, SqlFunctions.struncate(12.999d, 0), 0.001);
-
-    assertEquals(-12.345d, SqlFunctions.struncate(-12.345d, 3), 0.001);
-    assertEquals(-12.340d, SqlFunctions.struncate(-12.345d, 2), 0.001);
-    assertEquals(-12.300d, SqlFunctions.struncate(-12.345d, 1), 0.001);
-    assertEquals(-12.000d, SqlFunctions.struncate(-12.999d, 0), 0.001);
-
-    assertEquals(12000d, SqlFunctions.struncate(12345d, -3), 0.001);
-    assertEquals(12000d, SqlFunctions.struncate(12000d, -3), 0.001);
-    assertEquals(12000d, SqlFunctions.struncate(12001d, -3), 0.001);
-    assertEquals(10000d, SqlFunctions.struncate(12000d, -4), 0.001);
-    assertEquals(0d, SqlFunctions.struncate(12000d, -5), 0.001);
-    assertEquals(11000d, SqlFunctions.struncate(11999d, -3), 0.001);
-
-    assertEquals(-12000d, SqlFunctions.struncate(-12345d, -3), 0.001);
-    assertEquals(-12000d, SqlFunctions.struncate(-12000d, -3), 0.001);
-    assertEquals(-11000d, SqlFunctions.struncate(-11999d, -3), 0.001);
-    assertEquals(-10000d, SqlFunctions.struncate(-12000d, -4), 0.001);
-    assertEquals(0d, SqlFunctions.struncate(-12000d, -5), 0.001);
+    assertThat(SqlFunctions.struncate(12.345d, 3), within(12.345d, 0.001));
+    assertThat(SqlFunctions.struncate(12.345d, 2), within(12.340d, 0.001));
+    assertThat(SqlFunctions.struncate(12.345d, 1), within(12.300d, 0.001));
+    assertThat(SqlFunctions.struncate(12.999d, 0), within(12.000d, 0.001));
+
+    assertThat(SqlFunctions.struncate(-12.345d, 3), within(-12.345d, 0.001));
+    assertThat(SqlFunctions.struncate(-12.345d, 2), within(-12.340d, 0.001));
+    assertThat(SqlFunctions.struncate(-12.345d, 1), within(-12.300d, 0.001));
+    assertThat(SqlFunctions.struncate(-12.999d, 0), within(-12.000d, 0.001));
+
+    assertThat(SqlFunctions.struncate(12345d, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.struncate(12000d, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.struncate(12001d, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.struncate(12000d, -4), within(10000d, 0.001));
+    assertThat(SqlFunctions.struncate(12000d, -5), within(0d, 0.001));
+    assertThat(SqlFunctions.struncate(11999d, -3), within(11000d, 0.001));
+
+    assertThat(SqlFunctions.struncate(-12345d, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.struncate(-12000d, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.struncate(-11999d, -3), within(-11000d, 0.001));
+    assertThat(SqlFunctions.struncate(-12000d, -4), within(-10000d, 0.001));
+    assertThat(SqlFunctions.struncate(-12000d, -5), within(0d, 0.001));
   }
 
   @Test public void testSTruncateLong() {
-    assertEquals(12000d, SqlFunctions.struncate(12345L, -3), 0.001);
-    assertEquals(12000d, SqlFunctions.struncate(12000L, -3), 0.001);
-    assertEquals(12000d, SqlFunctions.struncate(12001L, -3), 0.001);
-    assertEquals(10000d, SqlFunctions.struncate(12000L, -4), 0.001);
-    assertEquals(0d, SqlFunctions.struncate(12000L, -5), 0.001);
-    assertEquals(11000d, SqlFunctions.struncate(11999L, -3), 0.001);
+    assertThat(SqlFunctions.struncate(12345L, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.struncate(12000L, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.struncate(12001L, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.struncate(12000L, -4), within(10000d, 0.001));
+    assertThat(SqlFunctions.struncate(12000L, -5), within(0d, 0.001));
+    assertThat(SqlFunctions.struncate(11999L, -3), within(11000d, 0.001));
 
-    assertEquals(-12000d, SqlFunctions.struncate(-12345L, -3), 0.001);
-    assertEquals(-12000d, SqlFunctions.struncate(-12000L, -3), 0.001);
-    assertEquals(-11000d, SqlFunctions.struncate(-11999L, -3), 0.001);
-    assertEquals(-10000d, SqlFunctions.struncate(-12000L, -4), 0.001);
-    assertEquals(0d, SqlFunctions.struncate(-12000L, -5), 0.001);
+    assertThat(SqlFunctions.struncate(-12345L, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.struncate(-12000L, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.struncate(-11999L, -3), within(-11000d, 0.001));
+    assertThat(SqlFunctions.struncate(-12000L, -4), within(-10000d, 0.001));
+    assertThat(SqlFunctions.struncate(-12000L, -5), within(0d, 0.001));
   }
 
   @Test public void testSTruncateInt() {
-    assertEquals(12000d, SqlFunctions.struncate(12345, -3), 0.001);
-    assertEquals(12000d, SqlFunctions.struncate(12000, -3), 0.001);
-    assertEquals(12000d, SqlFunctions.struncate(12001, -3), 0.001);
-    assertEquals(10000d, SqlFunctions.struncate(12000, -4), 0.001);
-    assertEquals(0d, SqlFunctions.struncate(12000, -5), 0.001);
-    assertEquals(11000d, SqlFunctions.struncate(11999, -3), 0.001);
+    assertThat(SqlFunctions.struncate(12345, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.struncate(12000, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.struncate(12001, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.struncate(12000, -4), within(10000d, 0.001));
+    assertThat(SqlFunctions.struncate(12000, -5), within(0d, 0.001));
+    assertThat(SqlFunctions.struncate(11999, -3), within(11000d, 0.001));
 
-    assertEquals(-12000d, SqlFunctions.struncate(-12345, -3), 0.001);
-    assertEquals(-12000d, SqlFunctions.struncate(-12000, -3), 0.001);
-    assertEquals(-11000d, SqlFunctions.struncate(-11999, -3), 0.001);
-    assertEquals(-10000d, SqlFunctions.struncate(-12000, -4), 0.001);
-    assertEquals(0d, SqlFunctions.struncate(-12000, -5), 0.001);
+    assertThat(SqlFunctions.struncate(-12345, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.struncate(-12000, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.struncate(-11999, -3), within(-11000d, 0.001));
+    assertThat(SqlFunctions.struncate(-12000, -4), within(-10000d, 0.001));
+    assertThat(SqlFunctions.struncate(-12000, -5), within(0d, 0.001));
   }
 
   @Test public void testSRoundDouble() {
-    assertEquals(12.345d, SqlFunctions.sround(12.345d, 3), 0.001);
-    assertEquals(12.350d, SqlFunctions.sround(12.345d, 2), 0.001);
-    assertEquals(12.300d, SqlFunctions.sround(12.345d, 1), 0.001);
-    assertEquals(13.000d, SqlFunctions.sround(12.999d, 2), 0.001);
-    assertEquals(13.000d, SqlFunctions.sround(12.999d, 1), 0.001);
-    assertEquals(13.000d, SqlFunctions.sround(12.999d, 0), 0.001);
-
-    assertEquals(-12.345d, SqlFunctions.sround(-12.345d, 3), 0.001);
-    assertEquals(-12.350d, SqlFunctions.sround(-12.345d, 2), 0.001);
-    assertEquals(-12.300d, SqlFunctions.sround(-12.345d, 1), 0.001);
-    assertEquals(-13.000d, SqlFunctions.sround(-12.999d, 2), 0.001);
-    assertEquals(-13.000d, SqlFunctions.sround(-12.999d, 1), 0.001);
-    assertEquals(-13.000d, SqlFunctions.sround(-12.999d, 0), 0.001);
-
-    assertEquals(12350d, SqlFunctions.sround(12345d, -1), 0.001);
-    assertEquals(12300d, SqlFunctions.sround(12345d, -2), 0.001);
-    assertEquals(12000d, SqlFunctions.sround(12345d, -3), 0.001);
-    assertEquals(12000d, SqlFunctions.sround(12000d, -3), 0.001);
-    assertEquals(12000d, SqlFunctions.sround(12001d, -3), 0.001);
-    assertEquals(10000d, SqlFunctions.sround(12000d, -4), 0.001);
-    assertEquals(0d, SqlFunctions.sround(12000d, -5), 0.001);
-    assertEquals(12000d, SqlFunctions.sround(11999d, -3), 0.001);
-
-    assertEquals(-12350d, SqlFunctions.sround(-12345d, -1), 0.001);
-    assertEquals(-12300d, SqlFunctions.sround(-12345d, -2), 0.001);
-    assertEquals(-12000d, SqlFunctions.sround(-12345d, -3), 0.001);
-    assertEquals(-12000d, SqlFunctions.sround(-12000d, -3), 0.001);
-    assertEquals(-12000d, SqlFunctions.sround(-11999d, -3), 0.001);
-    assertEquals(-10000d, SqlFunctions.sround(-12000d, -4), 0.001);
-    assertEquals(0d, SqlFunctions.sround(-12000d, -5), 0.001);
+    assertThat(SqlFunctions.sround(12.345d, 3), within(12.345d, 0.001));
+    assertThat(SqlFunctions.sround(12.345d, 2), within(12.350d, 0.001));
+    assertThat(SqlFunctions.sround(12.345d, 1), within(12.300d, 0.001));
+    assertThat(SqlFunctions.sround(12.999d, 2), within(13.000d, 0.001));
+    assertThat(SqlFunctions.sround(12.999d, 1), within(13.000d, 0.001));
+    assertThat(SqlFunctions.sround(12.999d, 0), within(13.000d, 0.001));
+
+    assertThat(SqlFunctions.sround(-12.345d, 3), within(-12.345d, 0.001));
+    assertThat(SqlFunctions.sround(-12.345d, 2), within(-12.350d, 0.001));
+    assertThat(SqlFunctions.sround(-12.345d, 1), within(-12.300d, 0.001));
+    assertThat(SqlFunctions.sround(-12.999d, 2), within(-13.000d, 0.001));
+    assertThat(SqlFunctions.sround(-12.999d, 1), within(-13.000d, 0.001));
+    assertThat(SqlFunctions.sround(-12.999d, 0), within(-13.000d, 0.001));
+
+    assertThat(SqlFunctions.sround(12345d, -1), within(12350d, 0.001));
+    assertThat(SqlFunctions.sround(12345d, -2), within(12300d, 0.001));
+    assertThat(SqlFunctions.sround(12345d, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.sround(12000d, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.sround(12001d, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.sround(12000d, -4), within(10000d, 0.001));
+    assertThat(SqlFunctions.sround(12000d, -5), within(0d, 0.001));
+    assertThat(SqlFunctions.sround(11999d, -3), within(12000d, 0.001));
+
+    assertThat(SqlFunctions.sround(-12345d, -1), within(-12350d, 0.001));
+    assertThat(SqlFunctions.sround(-12345d, -2), within(-12300d, 0.001));
+    assertThat(SqlFunctions.sround(-12345d, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.sround(-12000d, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.sround(-11999d, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.sround(-12000d, -4), within(-10000d, 0.001));
+    assertThat(SqlFunctions.sround(-12000d, -5), within(0d, 0.001));
   }
 
   @Test public void testSRoundLong() {
-    assertEquals(12350d, SqlFunctions.sround(12345L, -1), 0.001);
-    assertEquals(12300d, SqlFunctions.sround(12345L, -2), 0.001);
-    assertEquals(12000d, SqlFunctions.sround(12345L, -3), 0.001);
-    assertEquals(12000d, SqlFunctions.sround(12000L, -3), 0.001);
-    assertEquals(12000d, SqlFunctions.sround(12001L, -3), 0.001);
-    assertEquals(10000d, SqlFunctions.sround(12000L, -4), 0.001);
-    assertEquals(0d, SqlFunctions.sround(12000L, -5), 0.001);
-    assertEquals(12000d, SqlFunctions.sround(11999L, -3), 0.001);
-
-    assertEquals(-12350d, SqlFunctions.sround(-12345L, -1), 0.001);
-    assertEquals(-12300d, SqlFunctions.sround(-12345L, -2), 0.001);
-    assertEquals(-12000d, SqlFunctions.sround(-12345L, -3), 0.001);
-    assertEquals(-12000d, SqlFunctions.sround(-12000L, -3), 0.001);
-    assertEquals(-12000d, SqlFunctions.sround(-11999L, -3), 0.001);
-    assertEquals(-10000d, SqlFunctions.sround(-12000L, -4), 0.001);
-    assertEquals(0d, SqlFunctions.sround(-12000L, -5), 0.001);
+    assertThat(SqlFunctions.sround(12345L, -1), within(12350d, 0.001));
+    assertThat(SqlFunctions.sround(12345L, -2), within(12300d, 0.001));
+    assertThat(SqlFunctions.sround(12345L, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.sround(12000L, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.sround(12001L, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.sround(12000L, -4), within(10000d, 0.001));
+    assertThat(SqlFunctions.sround(12000L, -5), within(0d, 0.001));
+    assertThat(SqlFunctions.sround(11999L, -3), within(12000d, 0.001));
+
+    assertThat(SqlFunctions.sround(-12345L, -1), within(-12350d, 0.001));
+    assertThat(SqlFunctions.sround(-12345L, -2), within(-12300d, 0.001));
+    assertThat(SqlFunctions.sround(-12345L, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.sround(-12000L, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.sround(-11999L, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.sround(-12000L, -4), within(-10000d, 0.001));
+    assertThat(SqlFunctions.sround(-12000L, -5), within(0d, 0.001));
   }
 
   @Test public void testSRoundInt() {
-    assertEquals(12350d, SqlFunctions.sround(12345, -1), 0.001);
-    assertEquals(12300d, SqlFunctions.sround(12345, -2), 0.001);
-    assertEquals(12000d, SqlFunctions.sround(12345, -3), 0.001);
-    assertEquals(12000d, SqlFunctions.sround(12000, -3), 0.001);
-    assertEquals(12000d, SqlFunctions.sround(12001, -3), 0.001);
-    assertEquals(10000d, SqlFunctions.sround(12000, -4), 0.001);
-    assertEquals(0d, SqlFunctions.sround(12000, -5), 0.001);
-    assertEquals(12000d, SqlFunctions.sround(11999, -3), 0.001);
-
-    assertEquals(-12350d, SqlFunctions.sround(-12345, -1), 0.001);
-    assertEquals(-12300d, SqlFunctions.sround(-12345, -2), 0.001);
-    assertEquals(-12000d, SqlFunctions.sround(-12345, -3), 0.001);
-    assertEquals(-12000d, SqlFunctions.sround(-12000, -3), 0.001);
-    assertEquals(-12000d, SqlFunctions.sround(-11999, -3), 0.001);
-    assertEquals(-10000d, SqlFunctions.sround(-12000, -4), 0.001);
-    assertEquals(0d, SqlFunctions.sround(-12000, -5), 0.001);
+    assertThat(SqlFunctions.sround(12345, -1), within(12350d, 0.001));
+    assertThat(SqlFunctions.sround(12345, -2), within(12300d, 0.001));
+    assertThat(SqlFunctions.sround(12345, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.sround(12000, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.sround(12001, -3), within(12000d, 0.001));
+    assertThat(SqlFunctions.sround(12000, -4), within(10000d, 0.001));
+    assertThat(SqlFunctions.sround(12000, -5), within(0d, 0.001));
+    assertThat(SqlFunctions.sround(11999, -3), within(12000d, 0.001));
+
+    assertThat(SqlFunctions.sround(-12345, -1), within(-12350d, 0.001));
+    assertThat(SqlFunctions.sround(-12345, -2), within(-12300d, 0.001));
+    assertThat(SqlFunctions.sround(-12345, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.sround(-12000, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.sround(-11999, -3), within(-12000d, 0.001));
+    assertThat(SqlFunctions.sround(-12000, -4), within(-10000d, 0.001));
+    assertThat(SqlFunctions.sround(-12000, -5), within(0d, 0.001));
   }
 
   @Test public void testByteString() {
     final byte[] bytes = {(byte) 0xAB, (byte) 0xFF};
     final ByteString byteString = new ByteString(bytes);
-    assertEquals(2, byteString.length());
-    assertEquals("abff", byteString.toString());
-    assertEquals("abff", byteString.toString(16));
-    assertEquals("1010101111111111", byteString.toString(2));
+    assertThat(byteString.length(), is(2));
+    assertThat(byteString.toString(), is("abff"));
+    assertThat(byteString.toString(16), is("abff"));
+    assertThat(byteString.toString(2), is("1010101111111111"));
 
     final ByteString emptyByteString = new ByteString(new byte[0]);
-    assertEquals(0, emptyByteString.length());
-    assertEquals("", emptyByteString.toString());
-    assertEquals("", emptyByteString.toString(16));
-    assertEquals("", emptyByteString.toString(2));
+    assertThat(emptyByteString.length(), is(0));
+    assertThat(emptyByteString.toString(), is(""));
+    assertThat(emptyByteString.toString(16), is(""));
+    assertThat(emptyByteString.toString(2), is(""));
 
-    assertEquals(emptyByteString, ByteString.EMPTY);
+    assertThat(ByteString.EMPTY, is(emptyByteString));
 
-    assertEquals("ff", byteString.substring(1, 2).toString());
-    assertEquals("abff", byteString.substring(0, 2).toString());
-    assertEquals("", byteString.substring(2, 2).toString());
+    assertThat(byteString.substring(1, 2).toString(), is("ff"));
+    assertThat(byteString.substring(0, 2).toString(), is("abff"));
+    assertThat(byteString.substring(2, 2).toString(), is(""));
 
     // Add empty string, get original string back
     assertSame(byteString.concat(emptyByteString), byteString);
     final ByteString byteString1 = new ByteString(new byte[]{(byte) 12});
-    assertEquals("abff0c", byteString.concat(byteString1).toString());
+    assertThat(byteString.concat(byteString1).toString(), is("abff0c"));
 
     final byte[] bytes3 = {(byte) 0xFF};
     final ByteString byteString3 = new ByteString(bytes3);
 
-    assertEquals(0, byteString.indexOf(emptyByteString));
-    assertEquals(-1, byteString.indexOf(byteString1));
-    assertEquals(1, byteString.indexOf(byteString3));
-    assertEquals(-1, byteString3.indexOf(byteString));
+    assertThat(byteString.indexOf(emptyByteString), is(0));
+    assertThat(byteString.indexOf(byteString1), is(-1));
+    assertThat(byteString.indexOf(byteString3), is(1));
+    assertThat(byteString3.indexOf(byteString), is(-1));
 
     thereAndBack(bytes);
     thereAndBack(emptyByteString.getBytes());
@@ -696,9 +702,9 @@ public class SqlFunctionsTest {
 
   @Test public void testPlusAny() {
     // null parameters
-    assertNull(SqlFunctions.plusAny(null, null));
-    assertNull(SqlFunctions.plusAny(null, 1));
-    assertNull(SqlFunctions.plusAny(1, null));
+    assertThat(SqlFunctions.plusAny(null, null), nullValue());
+    assertThat(SqlFunctions.plusAny(null, 1), nullValue());
+    assertThat(SqlFunctions.plusAny(1, null), nullValue());
 
     // Numeric types
     assertThat(SqlFunctions.plusAny(2, 1L), is((Object) new BigDecimal(3)));
@@ -726,9 +732,9 @@ public class SqlFunctionsTest {
 
   @Test public void testMinusAny() {
     // null parameters
-    assertNull(SqlFunctions.minusAny(null, null));
-    assertNull(SqlFunctions.minusAny(null, 1));
-    assertNull(SqlFunctions.minusAny(1, null));
+    assertThat(SqlFunctions.minusAny(null, null), nullValue());
+    assertThat(SqlFunctions.minusAny(null, 1), nullValue());
+    assertThat(SqlFunctions.minusAny(1, null), nullValue());
 
     // Numeric types
     assertThat(SqlFunctions.minusAny(2, 1L), is((Object) new BigDecimal(1)));
@@ -756,9 +762,9 @@ public class SqlFunctionsTest {
 
   @Test public void testMultiplyAny() {
     // null parameters
-    assertNull(SqlFunctions.multiplyAny(null, null));
-    assertNull(SqlFunctions.multiplyAny(null, 1));
-    assertNull(SqlFunctions.multiplyAny(1, null));
+    assertThat(SqlFunctions.multiplyAny(null, null), nullValue());
+    assertThat(SqlFunctions.multiplyAny(null, 1), nullValue());
+    assertThat(SqlFunctions.multiplyAny(1, null), nullValue());
 
     // Numeric types
     assertThat(SqlFunctions.multiplyAny(2, 1L), is((Object) new BigDecimal(2)));
@@ -788,9 +794,9 @@ public class SqlFunctionsTest {
 
   @Test public void testDivideAny() {
     // null parameters
-    assertNull(SqlFunctions.divideAny(null, null));
-    assertNull(SqlFunctions.divideAny(null, 1));
-    assertNull(SqlFunctions.divideAny(1, null));
+    assertThat(SqlFunctions.divideAny(null, null), nullValue());
+    assertThat(SqlFunctions.divideAny(null, 1), nullValue());
+    assertThat(SqlFunctions.divideAny(1, null), nullValue());
 
     // Numeric types
     assertThat(SqlFunctions.divideAny(5, 2L),


[calcite] 03/04: [CALCITE-3280] Add REGEXP_REPLACE function in Oracle, MySQL libraries (Shuming Li)

Posted by jh...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jhyde pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git

commit b039b152d8d2e91ab2ca0232ebddfc998f3bb331
Author: ShuMingLi <mi...@gmail.com>
AuthorDate: Mon Aug 26 12:54:12 2019 +0800

    [CALCITE-3280] Add REGEXP_REPLACE function in Oracle, MySQL libraries (Shuming Li)
    
    Close apache/calcite#1401
---
 .../calcite/adapter/enumerable/RexImpTable.java    | 19 ++++++
 .../apache/calcite/runtime/CalciteResource.java    |  3 +
 .../org/apache/calcite/runtime/SqlFunctions.java   | 57 ++++++++++++++++
 .../calcite/sql/fun/SqlLibraryOperators.java       |  5 +-
 .../calcite/sql/fun/SqlRegexpReplaceFunction.java  | 77 ++++++++++++++++++++++
 .../org/apache/calcite/util/BuiltInMethod.java     |  8 +++
 .../main/java/org/apache/calcite/util/Unsafe.java  | 35 ++++++++++
 .../calcite/runtime/CalciteResource.properties     |  1 +
 .../calcite/sql/test/SqlOperatorBaseTest.java      | 39 +++++++++++
 .../org/apache/calcite/test/SqlFunctionsTest.java  | 38 +++++++++++
 site/_docs/reference.md                            |  3 +-
 11 files changed, 283 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java
index 6e0ad74..181a9f3 100644
--- a/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java
+++ b/core/src/main/java/org/apache/calcite/adapter/enumerable/RexImpTable.java
@@ -106,6 +106,7 @@ import static org.apache.calcite.sql.fun.SqlLibraryOperators.JSON_TYPE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.LEFT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.MD5;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.MONTHNAME;
+import static org.apache.calcite.sql.fun.SqlLibraryOperators.REGEXP_REPLACE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.REPEAT;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.REVERSE;
 import static org.apache.calcite.sql.fun.SqlLibraryOperators.RIGHT;
@@ -450,6 +451,24 @@ public class RexImpTable {
         NotImplementor.of(posixRegexImplementor), false);
     defineImplementor(SqlStdOperatorTable.NEGATED_POSIX_REGEX_CASE_SENSITIVE, NullPolicy.STRICT,
         NotImplementor.of(posixRegexImplementor), false);
+    defineImplementor(REGEXP_REPLACE, NullPolicy.STRICT,
+        new NotNullImplementor() {
+          final NotNullImplementor[] implementors = {
+              new ReflectiveCallNotNullImplementor(
+                  BuiltInMethod.REGEXP_REPLACE3.method),
+              new ReflectiveCallNotNullImplementor(
+                  BuiltInMethod.REGEXP_REPLACE4.method),
+              new ReflectiveCallNotNullImplementor(
+                  BuiltInMethod.REGEXP_REPLACE5.method),
+              new ReflectiveCallNotNullImplementor(
+                  BuiltInMethod.REGEXP_REPLACE6.method)
+          };
+          public Expression implement(RexToLixTranslator translator, RexCall call,
+              List<Expression> translatedOperands) {
+            return implementors[call.getOperands().size() - 3]
+                .implement(translator, call, translatedOperands);
+          }
+        }, false);
 
     // Multisets & arrays
     defineMethod(CARDINALITY, BuiltInMethod.COLLECTION_SIZE.method,
diff --git a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
index c830eb2..3c39422 100644
--- a/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
+++ b/core/src/main/java/org/apache/calcite/runtime/CalciteResource.java
@@ -891,6 +891,9 @@ public interface CalciteResource {
 
   @BaseMessage("Not a valid input for JSON_STORAGE_SIZE: ''{0}''")
   ExInst<CalciteException> invalidInputForJsonStorageSize(String value);
+
+  @BaseMessage("Not a valid input for REGEXP_REPLACE: ''{0}''")
+  ExInst<CalciteException> invalidInputForRegexpReplace(String value);
 }
 
 // End CalciteResource.java
diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index 2e23b66..38cf3b6 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -37,6 +37,7 @@ import org.apache.calcite.util.Bug;
 import org.apache.calcite.util.NumberUtil;
 import org.apache.calcite.util.TimeWithTimeZoneString;
 import org.apache.calcite.util.TimestampWithTimeZoneString;
+import org.apache.calcite.util.Unsafe;
 import org.apache.calcite.util.Util;
 
 import org.apache.commons.codec.digest.DigestUtils;
@@ -193,6 +194,62 @@ public class SqlFunctions {
     return DigestUtils.sha1Hex(string.getBytes());
   }
 
+  /** SQL {@code REGEXP_REPLACE} function with 3 arguments. */
+  public static String regexpReplace(String s, String regex,
+      String replacement) {
+    return regexpReplace(s, regex, replacement, 1, 0, null);
+  }
+
+  /** SQL {@code REGEXP_REPLACE} function with 4 arguments. */
+  public static String regexpReplace(String s, String regex, String replacement,
+      int pos) {
+    return regexpReplace(s, regex, replacement, pos, 0, null);
+  }
+
+  /** SQL {@code REGEXP_REPLACE} function with 5 arguments. */
+  public static String regexpReplace(String s, String regex, String replacement,
+      int pos, int occurrence) {
+    return regexpReplace(s, regex, replacement, pos, occurrence, null);
+  }
+
+  /** SQL {@code REGEXP_REPLACE} function with 6 arguments. */
+  public static String regexpReplace(String s, String regex, String replacement,
+      int pos, int occurrence, String matchType) {
+    if (pos < 1 || pos > s.length()) {
+      throw RESOURCE.invalidInputForRegexpReplace(Integer.toString(pos)).ex();
+    }
+
+    final int flags = makeRegexpFlags(matchType);
+    final Pattern pattern = Pattern.compile(regex, flags);
+
+    return Unsafe.regexpReplace(s, pattern, replacement, pos, occurrence);
+  }
+
+  private static int makeRegexpFlags(String stringFlags) {
+    int flags = 0;
+    if (stringFlags != null) {
+      for (int i = 0; i < stringFlags.length(); ++i) {
+        switch (stringFlags.charAt(i)) {
+        case 'i':
+          flags |= Pattern.CASE_INSENSITIVE;
+          break;
+        case 'c':
+          flags &= ~Pattern.CASE_INSENSITIVE;
+          break;
+        case 'n':
+          flags |= Pattern.DOTALL;
+          break;
+        case 'm':
+          flags |= Pattern.MULTILINE;
+          break;
+        default:
+          throw RESOURCE.invalidInputForRegexpReplace(stringFlags).ex();
+        }
+      }
+    }
+    return flags;
+  }
+
   /** SQL SUBSTRING(string FROM ... FOR ...) function. */
   public static String substring(String c, int s, int l) {
     int lc = c.length();
diff --git a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
index 3487ab9..51047fc 100644
--- a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java
@@ -169,6 +169,9 @@ public abstract class SqlLibraryOperators {
   @LibraryOperator(libraries = {MYSQL})
   public static final SqlFunction JSON_STORAGE_SIZE = new SqlJsonStorageSizeFunction();
 
+  @LibraryOperator(libraries = {MYSQL, ORACLE})
+  public static final SqlFunction REGEXP_REPLACE = new SqlRegexpReplaceFunction();
+
   /** The "MONTHNAME(datetime)" function; returns the name of the month,
    * in the current locale, of a TIMESTAMP or DATE argument. */
   @LibraryOperator(libraries = {MYSQL})
@@ -235,7 +238,7 @@ public abstract class SqlLibraryOperators {
           SqlFunctionCategory.STRING);
 
   /** The "CONCAT(arg, ...)" function that concatenates strings.
-   * For example, "CONCACT('a', 'bc', 'd')" returns "abcd". */
+   * For example, "CONCAT('a', 'bc', 'd')" returns "abcd". */
   @LibraryOperator(libraries = {MYSQL, POSTGRESQL, ORACLE})
   public static final SqlFunction CONCAT_FUNCTION =
       new SqlFunction("CONCAT",
diff --git a/core/src/main/java/org/apache/calcite/sql/fun/SqlRegexpReplaceFunction.java b/core/src/main/java/org/apache/calcite/sql/fun/SqlRegexpReplaceFunction.java
new file mode 100644
index 0000000..61a1e67
--- /dev/null
+++ b/core/src/main/java/org/apache/calcite/sql/fun/SqlRegexpReplaceFunction.java
@@ -0,0 +1,77 @@
+/*
+ * 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.calcite.sql.fun;
+
+import org.apache.calcite.sql.SqlCallBinding;
+import org.apache.calcite.sql.SqlFunction;
+import org.apache.calcite.sql.SqlFunctionCategory;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlOperandCountRange;
+import org.apache.calcite.sql.type.OperandTypes;
+import org.apache.calcite.sql.type.ReturnTypes;
+import org.apache.calcite.sql.type.SqlOperandCountRanges;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeTransforms;
+
+/**
+ * The REGEXP_REPLACE(source_string, pattern, replacement [, pos, occurrence, match_type])
+ * searches for a regular expression pattern and replaces every occurrence of the pattern
+ * with the specified string.
+ * */
+public class SqlRegexpReplaceFunction extends SqlFunction {
+
+  public SqlRegexpReplaceFunction() {
+    super("REGEXP_REPLACE",
+        SqlKind.OTHER_FUNCTION,
+        ReturnTypes.cascade(ReturnTypes.explicit(SqlTypeName.VARCHAR),
+            SqlTypeTransforms.TO_NULLABLE),
+        null,
+        null,
+        SqlFunctionCategory.STRING);
+  }
+
+  @Override public SqlOperandCountRange getOperandCountRange() {
+    return SqlOperandCountRanges.between(3, 6);
+  }
+
+  @Override public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) {
+    final int operandCount = callBinding.getOperandCount();
+    for (int i = 0; i < 3; i++) {
+      if (!OperandTypes.STRING.checkSingleOperandType(
+          callBinding, callBinding.operand(i), 0, throwOnFailure)) {
+        return false;
+      }
+    }
+    for (int i = 3; i < operandCount; i++) {
+      if (i == 3 && !OperandTypes.INTEGER.checkSingleOperandType(
+          callBinding, callBinding.operand(i), 0, throwOnFailure)) {
+        return false;
+      }
+      if (i == 4 && !OperandTypes.INTEGER.checkSingleOperandType(
+          callBinding, callBinding.operand(i), 0, throwOnFailure)) {
+        return false;
+      }
+      if (i == 5 && !OperandTypes.STRING.checkSingleOperandType(
+          callBinding, callBinding.operand(i), 0, throwOnFailure)) {
+        return false;
+      }
+    }
+    return true;
+  }
+}
+
+// End SqlRegexpReplaceFunction.java
diff --git a/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java b/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
index c274abe..1c4fb53 100644
--- a/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
+++ b/core/src/main/java/org/apache/calcite/util/BuiltInMethod.java
@@ -383,6 +383,14 @@ public enum BuiltInMethod {
   LIKE(SqlFunctions.class, "like", String.class, String.class),
   SIMILAR(SqlFunctions.class, "similar", String.class, String.class),
   POSIX_REGEX(SqlFunctions.class, "posixRegex", String.class, String.class, Boolean.class),
+  REGEXP_REPLACE3(SqlFunctions.class, "regexpReplace", String.class,
+      String.class, String.class),
+  REGEXP_REPLACE4(SqlFunctions.class, "regexpReplace", String.class,
+      String.class, String.class, int.class),
+  REGEXP_REPLACE5(SqlFunctions.class, "regexpReplace", String.class,
+      String.class, String.class, int.class, int.class),
+  REGEXP_REPLACE6(SqlFunctions.class, "regexpReplace", String.class,
+      String.class, String.class, int.class, int.class, String.class),
   IS_TRUE(SqlFunctions.class, "isTrue", Boolean.class),
   IS_NOT_FALSE(SqlFunctions.class, "isNotFalse", Boolean.class),
   NOT(SqlFunctions.class, "not", Boolean.class),
diff --git a/core/src/main/java/org/apache/calcite/util/Unsafe.java b/core/src/main/java/org/apache/calcite/util/Unsafe.java
index 61805ab..793db6b 100644
--- a/core/src/main/java/org/apache/calcite/util/Unsafe.java
+++ b/core/src/main/java/org/apache/calcite/util/Unsafe.java
@@ -17,6 +17,8 @@
 package org.apache.calcite.util;
 
 import java.io.StringWriter;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * Contains methods that call JDK methods that the
@@ -49,6 +51,39 @@ public class Unsafe {
     // Included in this class because StringBuffer is banned.
     sw.getBuffer().setLength(0);
   }
+
+  /** Helper for the SQL {@code REGEXP_REPLACE} function.
+   *
+   * <p>It is marked "unsafe" because it uses {@link StringBuffer};
+   * Versions of {@link Matcher#appendReplacement(StringBuffer, String)}
+   * and {@link Matcher#appendTail(StringBuffer)}
+   * that use {@link StringBuilder} are not available until JDK 9. */
+  public static String regexpReplace(String s, Pattern pattern,
+      String replacement, int pos, int occurrence) {
+    Bug.upgrade("when we drop JDK 8, replace StringBuffer with StringBuilder");
+    final StringBuffer sb = new StringBuffer();
+    final String input;
+    if (pos != 1) {
+      sb.append(s, 0, pos - 1);
+      input = s.substring(pos - 1);
+    } else {
+      input = s;
+    }
+
+    int count = 0;
+    Matcher matcher = pattern.matcher(input);
+    while (matcher.find()) {
+      if (occurrence == 0) {
+        matcher.appendReplacement(sb, replacement);
+      } else if (++count == occurrence) {
+        matcher.appendReplacement(sb, replacement);
+        break;
+      }
+    }
+    matcher.appendTail(sb);
+
+    return sb.toString();
+  }
 }
 
 // End Unsafe.java
diff --git a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
index d77353a..1b670b6 100644
--- a/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
+++ b/core/src/main/resources/org/apache/calcite/runtime/CalciteResource.properties
@@ -290,4 +290,5 @@ InvalidInputForJsonLength=Not a valid input for JSON_LENGTH: ''{0}''
 InvalidInputForJsonKeys=Not a valid input for JSON_KEYS: ''{0}''
 InvalidInputForJsonRemove=Invalid input for JSON_REMOVE: document: ''{0}'', jsonpath expressions: ''{1}''
 InvalidInputForJsonStorageSize=Not a valid input for JSON_STORAGE_SIZE: ''{0}''
+InvalidInputForRegexpReplace=Not a valid input for REGEXP_REPLACE: ''{0}''
 # End CalciteResource.properties
diff --git a/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java b/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
index e6cf965..70df001 100644
--- a/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
+++ b/core/src/test/java/org/apache/calcite/sql/test/SqlOperatorBaseTest.java
@@ -4524,6 +4524,45 @@ public abstract class SqlOperatorBaseTest {
         });
   }
 
+  @Test public void testRegexpReplaceFunc() {
+    Stream.of(SqlLibrary.MYSQL, SqlLibrary.ORACLE)
+        .map(this::tester)
+        .forEach(t -> {
+          t.setFor(SqlLibraryOperators.REGEXP_REPLACE);
+          t.checkString("regexp_replace('a b c', 'b', 'X')", "a X c",
+              "VARCHAR NOT NULL");
+          t.checkString("regexp_replace('abc def ghi', '[a-z]+', 'X')", "X X X",
+              "VARCHAR NOT NULL");
+          t.checkString("regexp_replace('100-200', '(\\d+)', 'num')", "num-num",
+              "VARCHAR NOT NULL");
+          t.checkString("regexp_replace('100-200', '(-)', '###')", "100###200",
+              "VARCHAR NOT NULL");
+          t.checkNull("regexp_replace(cast(null as varchar), '(-)', '###')");
+          t.checkNull("regexp_replace('100-200', cast(null as varchar), '###')");
+          t.checkNull("regexp_replace('100-200', '(-)', cast(null as varchar))");
+          t.checkString("regexp_replace('abc def ghi', '[a-z]+', 'X', 2)", "aX X X",
+              "VARCHAR NOT NULL");
+          t.checkString("regexp_replace('abc def ghi', '[a-z]+', 'X', 1, 3)", "abc def X",
+              "VARCHAR NOT NULL");
+          t.checkString("regexp_replace('abc def GHI', '[a-z]+', 'X', 1, 3, 'c')", "abc def GHI",
+              "VARCHAR NOT NULL");
+          t.checkString("regexp_replace('abc def GHI', '[a-z]+', 'X', 1, 3, 'i')", "abc def X",
+              "VARCHAR NOT NULL");
+          t.checkString("regexp_replace('abc def GHI', '[a-z]+', 'X', 1, 3, 'i')", "abc def X",
+              "VARCHAR NOT NULL");
+          t.checkString("regexp_replace('abc\t\ndef\t\nghi', '\t', '+')", "abc+\ndef+\nghi",
+              "VARCHAR NOT NULL");
+          t.checkString("regexp_replace('abc\t\ndef\t\nghi', '\t\n', '+')", "abc+def+ghi",
+              "VARCHAR NOT NULL");
+          t.checkString("regexp_replace('abc\t\ndef\t\nghi', '\\w+', '+')", "+\t\n+\t\n+",
+              "VARCHAR NOT NULL");
+          t.checkQuery("select regexp_replace('a b c', 'b', 'X')");
+          t.checkQuery("select regexp_replace('a b c', 'b', 'X', 1)");
+          t.checkQuery("select regexp_replace('a b c', 'b', 'X', 1, 3)");
+          t.checkQuery("select regexp_replace('a b c', 'b', 'X', 1, 3, 'i')");
+        });
+  }
+
   @Test public void testJsonExists() {
     tester.checkBoolean("json_exists('{\"foo\":\"bar\"}', "
         + "'strict $.foo' false on error)", Boolean.TRUE);
diff --git a/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java b/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
index 69c3963..0ec605d 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlFunctionsTest.java
@@ -42,6 +42,7 @@ import static org.apache.calcite.runtime.SqlFunctions.lower;
 import static org.apache.calcite.runtime.SqlFunctions.ltrim;
 import static org.apache.calcite.runtime.SqlFunctions.md5;
 import static org.apache.calcite.runtime.SqlFunctions.posixRegex;
+import static org.apache.calcite.runtime.SqlFunctions.regexpReplace;
 import static org.apache.calcite.runtime.SqlFunctions.rtrim;
 import static org.apache.calcite.runtime.SqlFunctions.sha1;
 import static org.apache.calcite.runtime.SqlFunctions.subtractMonths;
@@ -102,6 +103,43 @@ public class SqlFunctionsTest {
     assertThat(posixRegex("abcq", "[[:xdigit:]]", false), is(true));
   }
 
+  @Test public void testRegexpReplace() {
+    assertThat(regexpReplace("a b c", "b", "X"), is("a X c"));
+    assertThat(regexpReplace("abc def ghi", "[g-z]+", "X"), is("abc def X"));
+    assertThat(regexpReplace("abc def ghi", "[a-z]+", "X"), is("X X X"));
+    assertThat(regexpReplace("a b c", "a|b", "X"), is("X X c"));
+    assertThat(regexpReplace("a b c", "y", "X"), is("a b c"));
+
+    assertThat(regexpReplace("100-200", "(\\d+)", "num"), is("num-num"));
+    assertThat(regexpReplace("100-200", "(\\d+)", "###"), is("###-###"));
+    assertThat(regexpReplace("100-200", "(-)", "###"), is("100###200"));
+
+    assertThat(regexpReplace("abc def ghi", "[a-z]+", "X", 1), is("X X X"));
+    assertThat(regexpReplace("abc def ghi", "[a-z]+", "X", 2), is("aX X X"));
+    assertThat(regexpReplace("abc def ghi", "[a-z]+", "X", 1, 3),
+        is("abc def X"));
+    assertThat(regexpReplace("abc def GHI", "[a-z]+", "X", 1, 3, "c"),
+        is("abc def GHI"));
+    assertThat(regexpReplace("abc def GHI", "[a-z]+", "X", 1, 3, "i"),
+        is("abc def X"));
+
+    try {
+      regexpReplace("abc def ghi", "[a-z]+", "X", 0);
+      fail("'regexp_replace' on an invalid pos is not possible");
+    } catch (CalciteException e) {
+      assertThat(e.getMessage(),
+          is("Not a valid input for REGEXP_REPLACE: '0'"));
+    }
+
+    try {
+      regexpReplace("abc def ghi", "[a-z]+", "X", 1, 3, "WWW");
+      fail("'regexp_replace' on an invalid matchType is not possible");
+    } catch (CalciteException e) {
+      assertThat(e.getMessage(),
+          is("Not a valid input for REGEXP_REPLACE: 'WWW'"));
+    }
+  }
+
   @Test public void testLower() {
     assertThat(lower("A bCd Iijk"), is("a bcd iijk"));
   }
diff --git a/site/_docs/reference.md b/site/_docs/reference.md
index 0ae90ef..c5efae8 100644
--- a/site/_docs/reference.md
+++ b/site/_docs/reference.md
@@ -2193,6 +2193,7 @@ semantics.
 | m p | MD5(string)                                  | Calculates an MD5 128-bit checksum of *string* and returns it as a hex string
 | m | MONTHNAME(date)                                | Returns the name, in the connection's locale, of the month in *datetime*; for example, it returns '二月' for both DATE '2020-02-10' and TIMESTAMP '2020-02-10 10:10:10'
 | o | NVL(value1, value2)                            | Returns *value1* if *value1* is not null, otherwise *value2*
+| m o | REGEXP_REPLACE(string, regexp, rep, [, pos [, occurrence [, matchType]]]) | Replaces all substrings of *string* that match *regexp* with *rep* at the starting *pos* in expr (if omitted, the default is 1), *occurrence* means which occurrence of a match to search for (if omitted, the default is 1), *matchType* specifies how to perform matching
 | m p | REPEAT(string, integer)                      | Returns a string consisting of *string* repeated of *integer* times; returns an empty string if *integer* is less than 1
 | m | REVERSE(string)                                | Returns *string* with the order of the characters reversed
 | m p | RIGHT(string, length)                        | Returns the rightmost *length* characters from the *string*
@@ -2200,7 +2201,7 @@ semantics.
 | m p | SHA1(string)                                 | Calculates a SHA-1 hash value of *string* and returns it as a hex string
 | m o p | SOUNDEX(string)                            | Returns the phonetic representation of *string*; throws if *string* is encoded with multi-byte encoding such as UTF-8
 | m | SPACE(integer)                                 | Returns a string of *integer* spaces; returns an empty string if *integer* is less than 1
-| o | SUBSTR(string, position [, substring_length ]) | Returns a portion of *string*, beginning at character *position*, *substring_length* characters long. SUBSTR calculates lengths using characters as defined by the input character set
+| o | SUBSTR(string, position [, substringLength ]) | Returns a portion of *string*, beginning at character *position*, *substringLength* characters long. SUBSTR calculates lengths using characters as defined by the input character set
 | o p | TO_DATE(string, format)                      | Converts *string* to a date using the format *format*
 | o p | TO_TIMESTAMP(string, format)                 | Converts *string* to a timestamp using the format *format*
 | o p | TRANSLATE(expr, fromString, toString)        | Returns *expr* with all occurrences of each character in *fromString* replaced by its corresponding character in *toString*. Characters in *expr* that are not in *fromString* are not replaced


[calcite] 04/04: [CALCITE-3122] Convert Pig Latin scripts into Calcite relational algebra and Calcite SQL (Khai Tran)

Posted by jh...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

jhyde pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git

commit e5ae1796e8016b0a5e943f458bf31a7e7e8aa75a
Author: Khai Tran <kh...@linkedin.com>
AuthorDate: Tue Dec 6 14:41:11 2016 -0800

    [CALCITE-3122] Convert Pig Latin scripts into Calcite relational algebra and Calcite SQL (Khai Tran)
    
    Remove SqlNode.toSqlString and replace it with SqlNode.unparse.
    
    Mute pig parser log in piglet tests.
    
    Refactor DynamicTupleRecordType.
    
    Remove dependency on core/PigRelBuilder from LogicalPigRelBuilder and
    rename LogicalPigRelBuilder to PigRelBuilder.
    
    Fix RAT report on Travis on unapproved license files.
    
    Close apache/calcite#1265
---
 .../calcite/adapter/enumerable/EnumUtils.java      |   18 +-
 .../org/apache/calcite/plan/RelOptCluster.java     |    2 +-
 .../apache/calcite/prepare/RelOptTableImpl.java    |   10 +
 .../org/apache/calcite/rel/core/Uncollect.java     |    6 +-
 .../calcite/rel/logical/ToLogicalConverter.java    |  162 ++
 .../calcite/rel/rel2sql/RelToSqlConverter.java     |  117 ++
 .../apache/calcite/rel/rel2sql/SqlImplementor.java |   42 +-
 .../org/apache/calcite/runtime/SqlFunctions.java   |    9 +-
 .../calcite/schema/impl/ScalarFunctionImpl.java    |   14 +
 .../apache/calcite/sql/SqlBasicTypeNameSpec.java   |    2 +-
 .../org/apache/calcite/sql/SqlLateralOperator.java |    5 +-
 .../main/java/org/apache/calcite/sql/SqlNode.java  |   21 +-
 .../org/apache/calcite/sql/SqlUnnestOperator.java  |    9 +-
 .../calcite/sql/validate/SqlValidatorImpl.java     |    4 +-
 .../org/apache/calcite/tools/PigRelBuilder.java    |   27 +-
 .../java/org/apache/calcite/tools/RelBuilder.java  |   22 +-
 .../rel/logical/ToLogicalConverterTest.java        |  411 +++++
 .../calcite/rel/rel2sql/RelToSqlConverterTest.java |   36 +
 .../apache/calcite/sql/parser/SqlParserTest.java   |   12 +-
 .../java/org/apache/calcite/test/Matchers.java     |   19 +-
 kafka/pom.xml                                      |    4 +-
 piglet/pom.xml                                     |   25 +
 .../calcite/piglet/DynamicTupleRecordType.java     |   76 +
 .../org/apache/calcite/piglet/PigConverter.java    |  288 ++++
 .../org/apache/calcite/piglet/PigRelBuilder.java   |  658 ++++++++
 .../org/apache/calcite/piglet/PigRelExVisitor.java |  452 ++++++
 .../org/apache/calcite/piglet/PigRelExWalker.java  |   84 +
 .../calcite/piglet/PigRelOpInnerVisitor.java       |  251 +++
 .../org/apache/calcite/piglet/PigRelOpVisitor.java |  722 +++++++++
 .../org/apache/calcite/piglet/PigRelOpWalker.java  |   92 ++
 .../org/apache/calcite/piglet/PigRelSqlUdfs.java   |  361 +++++
 .../calcite/piglet/PigRelToSqlConverter.java       |  128 ++
 .../apache/calcite/piglet/PigRelUdfConverter.java  |  195 +++
 .../java/org/apache/calcite/piglet/PigTable.java   |   76 +
 .../calcite/piglet/PigToSqlAggregateRule.java      |  408 +++++
 .../java/org/apache/calcite/piglet/PigTypes.java   |  200 +++
 .../org/apache/calcite/piglet/PigUdfFinder.java    |  112 ++
 .../java/org/apache/calcite/piglet/PigUdfs.java    |   47 +
 .../calcite/piglet/PigUserDefinedFunction.java     |   78 +
 .../org/apache/calcite/test/CalciteHandler.java    |   20 +-
 .../java/org/apache/calcite/test/PigRelExTest.java |  250 +++
 .../java/org/apache/calcite/test/PigRelOpTest.java | 1631 ++++++++++++++++++++
 .../org/apache/calcite/test/PigRelTestBase.java    |   38 +
 .../org/apache/calcite/test/PigScriptTest.java     |   79 +
 piglet/src/test/resources/log4j.properties         |    4 +-
 piglet/src/test/resources/testPig.pig              |   44 +
 site/_docs/history.md                              |    2 +-
 47 files changed, 7213 insertions(+), 60 deletions(-)

diff --git a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumUtils.java b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumUtils.java
index b4d2e20..028011d 100644
--- a/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumUtils.java
+++ b/core/src/main/java/org/apache/calcite/adapter/enumerable/EnumUtils.java
@@ -188,8 +188,22 @@ public class EnumUtils {
   static List<Expression> fromInternal(Class<?>[] targetTypes,
       List<Expression> expressions) {
     final List<Expression> list = new ArrayList<>();
-    for (int i = 0; i < expressions.size(); i++) {
-      list.add(fromInternal(expressions.get(i), targetTypes[i]));
+    if (targetTypes.length == expressions.size()) {
+      for (int i = 0; i < expressions.size(); i++) {
+        list.add(fromInternal(expressions.get(i), targetTypes[i]));
+      }
+    } else {
+      int j = 0;
+      for (int i = 0; i < expressions.size(); i++) {
+        Class<?> type;
+        if (!targetTypes[j].isArray()) {
+          type = targetTypes[j];
+          j++;
+        } else {
+          type = targetTypes[j].getComponentType();
+        }
+        list.add(fromInternal(expressions.get(i), type));
+      }
     }
     return list;
   }
diff --git a/core/src/main/java/org/apache/calcite/plan/RelOptCluster.java b/core/src/main/java/org/apache/calcite/plan/RelOptCluster.java
index da6b8eb..addec07 100644
--- a/core/src/main/java/org/apache/calcite/plan/RelOptCluster.java
+++ b/core/src/main/java/org/apache/calcite/plan/RelOptCluster.java
@@ -40,7 +40,7 @@ public class RelOptCluster {
   //~ Instance fields --------------------------------------------------------
 
   private final RelDataTypeFactory typeFactory;
-  private final RelOptPlanner planner;
+  private RelOptPlanner planner;
   private final AtomicInteger nextCorrel;
   private final Map<String, RelNode> mapCorrelToRel;
   private RexNode originalExpression;
diff --git a/core/src/main/java/org/apache/calcite/prepare/RelOptTableImpl.java b/core/src/main/java/org/apache/calcite/prepare/RelOptTableImpl.java
index 46eb2c4..62f4dfe 100644
--- a/core/src/main/java/org/apache/calcite/prepare/RelOptTableImpl.java
+++ b/core/src/main/java/org/apache/calcite/prepare/RelOptTableImpl.java
@@ -114,6 +114,16 @@ public class RelOptTableImpl extends Prepare.AbstractPreparingTable {
         c -> expression, null);
   }
 
+  public static RelOptTableImpl create(
+      RelOptSchema schema,
+      RelDataType rowType,
+      List<String> names,
+      Table table,
+      Expression expression) {
+    return new RelOptTableImpl(schema, rowType, names, table,
+        c -> expression, table.getStatistic().getRowCount());
+  }
+
   public static RelOptTableImpl create(RelOptSchema schema, RelDataType rowType,
       Table table, Path path) {
     final SchemaPlus schemaPlus = MySchemaPlus.create(path);
diff --git a/core/src/main/java/org/apache/calcite/rel/core/Uncollect.java b/core/src/main/java/org/apache/calcite/rel/core/Uncollect.java
index 98fa388..7d49748 100644
--- a/core/src/main/java/org/apache/calcite/rel/core/Uncollect.java
+++ b/core/src/main/java/org/apache/calcite/rel/core/Uncollect.java
@@ -27,7 +27,6 @@ import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rel.type.RelDataTypeFactory;
 import org.apache.calcite.rel.type.RelDataTypeField;
 import org.apache.calcite.sql.SqlUnnestOperator;
-import org.apache.calcite.sql.SqlUtil;
 import org.apache.calcite.sql.type.MapSqlType;
 import org.apache.calcite.sql.type.SqlTypeName;
 
@@ -146,9 +145,8 @@ public class Uncollect extends SingleRel {
         if (ret.isStruct()) {
           builder.addAll(ret.getFieldList());
         } else {
-          // Element type is not a record. It may be a scalar type, say
-          // "INTEGER". Wrap it in a struct type.
-          builder.add(SqlUtil.deriveAliasFromOrdinal(field.getIndex()), ret);
+          // Element type is not a record, use the field name of the element directly
+          builder.add(field.getName(), ret);
         }
       }
     }
diff --git a/core/src/main/java/org/apache/calcite/rel/logical/ToLogicalConverter.java b/core/src/main/java/org/apache/calcite/rel/logical/ToLogicalConverter.java
new file mode 100644
index 0000000..adb247f
--- /dev/null
+++ b/core/src/main/java/org/apache/calcite/rel/logical/ToLogicalConverter.java
@@ -0,0 +1,162 @@
+/*
+ * 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.calcite.rel.logical;
+
+import org.apache.calcite.adapter.enumerable.EnumerableInterpreter;
+import org.apache.calcite.adapter.enumerable.EnumerableLimit;
+import org.apache.calcite.adapter.jdbc.JdbcToEnumerableConverter;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelShuttleImpl;
+import org.apache.calcite.rel.SingleRel;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.Calc;
+import org.apache.calcite.rel.core.Correlate;
+import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rel.core.TableScan;
+import org.apache.calcite.rel.core.Uncollect;
+import org.apache.calcite.rel.core.Union;
+import org.apache.calcite.rel.core.Values;
+import org.apache.calcite.rel.core.Window;
+import org.apache.calcite.tools.RelBuilder;
+
+/**
+ * Shuttle to convert any rel plan to a plan with all logical nodes.
+ */
+public class ToLogicalConverter extends RelShuttleImpl {
+  private final RelBuilder relBuilder;
+
+  public ToLogicalConverter(RelBuilder relBuilder) {
+    this.relBuilder = relBuilder;
+  }
+
+  @Override public RelNode visit(TableScan scan) {
+    return LogicalTableScan.create(scan.getCluster(), scan.getTable());
+  }
+
+  @Override public RelNode visit(RelNode relNode) {
+    if (relNode instanceof Aggregate) {
+      final Aggregate agg = (Aggregate) relNode;
+      return relBuilder.push(visit(agg.getInput()))
+          .aggregate(
+              relBuilder.groupKey(agg.getGroupSet(), agg.groupSets),
+              agg.getAggCallList())
+          .build();
+    }
+
+    if (relNode instanceof TableScan) {
+      return visit((TableScan) relNode);
+    }
+
+    if (relNode instanceof Filter) {
+      final Filter filter = (Filter) relNode;
+      return relBuilder.push(visit(filter.getInput()))
+          .filter(filter.getCondition())
+          .build();
+    }
+
+    if (relNode instanceof Project) {
+      final Project project = (Project) relNode;
+      return relBuilder.push(visit(project.getInput()))
+          .project(project.getProjects(), project.getRowType().getFieldNames())
+          .build();
+    }
+
+    if (relNode instanceof Union) {
+      final Union union = (Union) relNode;
+      for (RelNode rel : union.getInputs()) {
+        relBuilder.push(visit(rel));
+      }
+      return relBuilder.union(union.all, union.getInputs().size())
+          .build();
+    }
+
+    if (relNode instanceof Join) {
+      final Join join = (Join) relNode;
+      return relBuilder.push(visit(join.getLeft()))
+          .push(visit(join.getRight()))
+          .join(join.getJoinType(), join.getCondition())
+          .build();
+    }
+
+    if (relNode instanceof Correlate) {
+      final Correlate corr = (Correlate) relNode;
+      return relBuilder.push(visit(corr.getLeft()))
+          .push(visit(corr.getRight()))
+          .join(corr.getJoinType(), relBuilder.literal(true),
+              corr.getVariablesSet())
+          .build();
+    }
+
+    if (relNode instanceof Values) {
+      final Values values = (Values) relNode;
+      return relBuilder.values(values.tuples, values.getRowType())
+          .build();
+    }
+
+    if (relNode instanceof Sort) {
+      final Sort sort = (Sort) relNode;
+      return LogicalSort.create(visit(sort.getInput()), sort.getCollation(),
+          sort.offset, sort.fetch);
+    }
+
+    if (relNode instanceof Window) {
+      final Window window = (Window) relNode;
+      final RelNode input = visit(window.getInput());
+      return LogicalWindow.create(input.getTraitSet(), input, window.constants,
+          window.getRowType(), window.groups);
+    }
+
+    if (relNode instanceof Calc) {
+      final Calc calc = (Calc) relNode;
+      return LogicalCalc.create(visit(calc.getInput()), calc.getProgram());
+    }
+
+    if (relNode instanceof EnumerableInterpreter
+        || relNode instanceof JdbcToEnumerableConverter) {
+      return visit(((SingleRel) relNode).getInput());
+    }
+
+    if (relNode instanceof EnumerableLimit) {
+      final EnumerableLimit limit = (EnumerableLimit) relNode;
+      RelNode logicalInput = visit(limit.getInput());
+      RelCollation collation = RelCollations.of();
+      if (logicalInput instanceof Sort) {
+        collation = ((Sort) logicalInput).collation;
+        logicalInput = ((Sort) logicalInput).getInput();
+      }
+      return LogicalSort.create(logicalInput, collation, limit.offset,
+          limit.fetch);
+    }
+
+    if (relNode instanceof Uncollect) {
+      final Uncollect uncollect = (Uncollect) relNode;
+      final RelNode input = visit(uncollect.getInput());
+      return new Uncollect(input.getCluster(), input.getTraitSet(), input,
+          uncollect.withOrdinality);
+    }
+
+    throw new AssertionError("Need to implement logical converter for "
+        + relNode.getClass().getName());
+  }
+}
+
+// End ToLogicalConverter.java
diff --git a/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java b/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
index cc1070c..1bcf12c 100644
--- a/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
+++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java
@@ -25,6 +25,7 @@ import org.apache.calcite.rel.RelNode;
 import org.apache.calcite.rel.core.Aggregate;
 import org.apache.calcite.rel.core.AggregateCall;
 import org.apache.calcite.rel.core.Calc;
+import org.apache.calcite.rel.core.Correlate;
 import org.apache.calcite.rel.core.CorrelationId;
 import org.apache.calcite.rel.core.Filter;
 import org.apache.calcite.rel.core.Intersect;
@@ -36,6 +37,7 @@ import org.apache.calcite.rel.core.Project;
 import org.apache.calcite.rel.core.Sort;
 import org.apache.calcite.rel.core.TableModify;
 import org.apache.calcite.rel.core.TableScan;
+import org.apache.calcite.rel.core.Uncollect;
 import org.apache.calcite.rel.core.Union;
 import org.apache.calcite.rel.core.Values;
 import org.apache.calcite.rel.logical.LogicalProject;
@@ -168,6 +170,30 @@ public class RelToSqlConverter extends SqlImplementor
   }
 
   /** @see #dispatch */
+  public Result visit(Correlate e) {
+    final Result leftResult =
+        visitChild(0, e.getLeft())
+            .resetAlias(e.getCorrelVariable(), e.getRowType());
+    parseCorrelTable(e, leftResult);
+    final Result rightResult = visitChild(1, e.getRight());
+    final SqlNode rightLateral =
+        SqlStdOperatorTable.LATERAL.createCall(POS, rightResult.node);
+    final SqlNode rightLateralAs =
+        SqlStdOperatorTable.AS.createCall(POS, rightLateral,
+            new SqlIdentifier(rightResult.neededAlias, POS));
+
+    final SqlNode join =
+        new SqlJoin(POS,
+            leftResult.asFrom(),
+            SqlLiteral.createBoolean(false, POS),
+            JoinType.COMMA.symbol(POS),
+            rightLateralAs,
+            JoinConditionType.NONE.symbol(POS),
+            null);
+    return result(join, leftResult, rightResult);
+  }
+
+  /** @see #dispatch */
   public Result visit(Filter e) {
     final RelNode input = e.getInput();
     Result x = visitChild(0, input);
@@ -191,6 +217,7 @@ public class RelToSqlConverter extends SqlImplementor
 
   /** @see #dispatch */
   public Result visit(Project e) {
+    e.getVariablesSet();
     Result x = visitChild(0, e.getInput());
     parseCorrelTable(e, x);
     if (isStar(e.getChildExps(), e.getInput().getRowType(), e.getRowType())) {
@@ -240,6 +267,57 @@ public class RelToSqlConverter extends SqlImplementor
     final List<SqlNode> selectList = new ArrayList<>();
     final List<SqlNode> groupByList =
         generateGroupList(builder, selectList, e, groupKeyList);
+    return buildAggregate(e, builder, selectList, groupByList);
+  }
+
+  /**
+   * Gets the {@link org.apache.calcite.rel.rel2sql.SqlImplementor.Builder} for
+   * the given {@link Aggregate} node.
+   *
+   * @param e Aggregate node
+   * @param inputResult Result from the input
+   * @param inputIsProject Whether the input is a Project
+   * @return A SQL builder
+   */
+  protected Builder getAggregateBuilder(Aggregate e, Result inputResult,
+      boolean inputIsProject) {
+    if (inputIsProject) {
+      final Builder builder = inputResult.builder(e);
+      builder.clauses.add(Clause.GROUP_BY);
+      return builder;
+    } else {
+      return inputResult.builder(e, Clause.GROUP_BY);
+    }
+  }
+
+  /**
+   * Builds the group list for an Aggregate node.
+   *
+   * @param e The Aggregate node
+   * @param builder The SQL builder
+   * @param groupByList output group list
+   * @param selectList output select list
+   */
+  protected void buildAggGroupList(Aggregate e, Builder builder,
+      List<SqlNode> groupByList, List<SqlNode> selectList) {
+    for (int group : e.getGroupSet()) {
+      final SqlNode field = builder.context.field(group);
+      addSelect(selectList, field, e.getRowType());
+      groupByList.add(field);
+    }
+  }
+
+  /**
+   * Builds an aggregate query.
+   *
+   * @param e The Aggregate node
+   * @param builder The SQL builder
+   * @param selectList The precomputed group list
+   * @param groupByList The precomputed select list
+   * @return The aggregate query result
+   */
+  protected Result buildAggregate(Aggregate e, Builder builder,
+      List<SqlNode> selectList, List<SqlNode> groupByList) {
     for (AggregateCall aggCall : e.getAggCallList()) {
       SqlNode aggCallSqlNode = builder.context.toSql(aggCall);
       if (aggCall.getAggregation() instanceof SqlSingleValueAggFunction) {
@@ -731,6 +809,45 @@ public class RelToSqlConverter extends SqlImplementor
     return result(matchRecognize, Expressions.list(Clause.FROM), e, null);
   }
 
+  private SqlCall as(SqlNode e, String alias) {
+    return SqlStdOperatorTable.AS.createCall(POS, e,
+        new SqlIdentifier(alias, POS));
+  }
+
+  public Result visit(Uncollect e) {
+    final Result x = visitChild(0, e.getInput());
+    final SqlNode unnestNode = SqlStdOperatorTable.UNNEST.createCall(POS, x.asStatement());
+    final List<SqlNode> operands = createAsFullOperands(e.getRowType(), unnestNode, x.neededAlias);
+    final SqlNode asNode = SqlStdOperatorTable.AS.createCall(POS, operands);
+    return result(asNode, ImmutableList.of(Clause.FROM), e, null);
+  }
+
+  /**
+   * Creates operands for a full AS operator. Format SqlNode AS alias(col_1, col_2,... ,col_n).
+   *
+   * @param rowType Row type of the SqlNode
+   * @param leftOperand SqlNode
+   * @param alias alias
+   */
+  public List<SqlNode> createAsFullOperands(RelDataType rowType, SqlNode leftOperand,
+      String alias) {
+    final List<SqlNode> result = new ArrayList<>();
+    result.add(leftOperand);
+    result.add(new SqlIdentifier(alias, POS));
+    for (int i = 0; i < rowType.getFieldCount(); i++) {
+      final String lowerName = rowType.getFieldNames().get(i).toLowerCase(Locale.ROOT);
+      SqlIdentifier sqlColumn;
+      if (lowerName.startsWith("expr$")) {
+        sqlColumn = new SqlIdentifier("col_" + i, POS);
+        ordinalMap.put(lowerName, sqlColumn);
+      } else {
+        sqlColumn = new SqlIdentifier(rowType.getFieldNames().get(i), POS);
+      }
+      result.add(sqlColumn);
+    }
+    return result;
+  }
+
   @Override public void addSelect(List<SqlNode> selectList, SqlNode node,
       RelDataType rowType) {
     String name = rowType.getFieldNames().get(selectList.size());
diff --git a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
index cca13f7..89c4e61 100644
--- a/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
+++ b/core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
@@ -505,6 +505,10 @@ public abstract class SqlImplementor {
           sqlIdentifier = (SqlIdentifier) correlAliasContext
               .field(lastAccess.getField().getIndex());
           break;
+        case ROW:
+          final SqlNode expr = toSql(program, referencedExpr);
+          sqlIdentifier = new SqlIdentifier(expr.toString(), POS);
+          break;
         default:
           sqlIdentifier = (SqlIdentifier) toSql(program, referencedExpr);
         }
@@ -680,6 +684,31 @@ public abstract class SqlImplementor {
       }
     }
 
+    /** Converts an expression from {@link RexWindowBound} to {@link SqlNode}
+     * format.
+     *
+     * @param rexWindowBound Expression to convert
+     */
+    public SqlNode toSql(RexWindowBound rexWindowBound) {
+      final SqlNode offsetLiteral =
+          rexWindowBound.getOffset() == null
+              ? null
+              : SqlLiteral.createCharString(rexWindowBound.getOffset().toString(),
+                  SqlParserPos.ZERO);
+      if (rexWindowBound.isPreceding()) {
+        return offsetLiteral == null
+            ? SqlWindow.createUnboundedPreceding(POS)
+            : SqlWindow.createPreceding(offsetLiteral, POS);
+      } else if (rexWindowBound.isFollowing()) {
+        return offsetLiteral == null
+            ? SqlWindow.createUnboundedFollowing(POS)
+            : SqlWindow.createFollowing(offsetLiteral, POS);
+      } else {
+        assert rexWindowBound.isCurrentRow();
+        return SqlWindow.createCurrentRow(POS);
+      }
+    }
+
     protected Context getAliasContext(RexCorrelVariable variable) {
       throw new UnsupportedOperationException();
     }
@@ -1029,7 +1058,7 @@ public abstract class SqlImplementor {
   /** Result of implementing a node. */
   public class Result {
     final SqlNode node;
-    private final String neededAlias;
+    final String neededAlias;
     private final RelDataType neededType;
     private final Map<String, RelDataType> aliases;
     final Expressions.FluentList<Clause> clauses;
@@ -1240,6 +1269,17 @@ public abstract class SqlImplementor {
             ImmutableMap.of(neededAlias, neededType));
       }
     }
+
+    /**
+     * Sets the alias of the join or correlate just created.
+     *
+     * @param alias New alias
+     * @param type type of the node associated with the alias
+     */
+    public Result resetAlias(String alias, RelDataType type) {
+      return new Result(node, clauses, alias, neededType,
+          ImmutableMap.of(alias, type));
+    }
   }
 
   /** Builder. */
diff --git a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
index 38cf3b6..65b3b9b 100644
--- a/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
+++ b/core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
@@ -2744,8 +2744,13 @@ public class SqlFunctions {
     public FlatLists.ComparableList<E> current() {
       int i = 0;
       for (Object element : (Object[]) elements) {
-        final List list2 = (List) element;
-        Object[] a = list2.toArray();
+        Object[] a;
+        if (element.getClass().isArray()) {
+          a = (Object[]) element;
+        } else {
+          final List list2 = (List) element;
+          a = list2.toArray();
+        }
         System.arraycopy(a, 0, flatElements, i, a.length);
         i += a.length;
       }
diff --git a/core/src/main/java/org/apache/calcite/schema/impl/ScalarFunctionImpl.java b/core/src/main/java/org/apache/calcite/schema/impl/ScalarFunctionImpl.java
index 2429e0c..0736524 100644
--- a/core/src/main/java/org/apache/calcite/schema/impl/ScalarFunctionImpl.java
+++ b/core/src/main/java/org/apache/calcite/schema/impl/ScalarFunctionImpl.java
@@ -106,6 +106,20 @@ public class ScalarFunctionImpl extends ReflectiveFunctionBase
     return new ScalarFunctionImpl(method, implementor);
   }
 
+
+  /**
+   * Creates unsafe version of {@link ScalarFunction} from any method. The method
+   * does not need to be static or belong to a class with default constructor. It is
+   * the responsibility of the underlying engine to initialize the UDF object that
+   * contain the method.
+   *
+   * @param method method that is used to implement the function
+   */
+  public static ScalarFunction createUnsafe(Method method) {
+    CallImplementor implementor = createImplementor(method);
+    return new ScalarFunctionImpl(method, implementor);
+  }
+
   public RelDataType getReturnType(RelDataTypeFactory typeFactory) {
     return typeFactory.createJavaType(method.getReturnType());
   }
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlBasicTypeNameSpec.java b/core/src/main/java/org/apache/calcite/sql/SqlBasicTypeNameSpec.java
index 8958847..8b8f1b3 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlBasicTypeNameSpec.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlBasicTypeNameSpec.java
@@ -180,7 +180,7 @@ public class SqlBasicTypeNameSpec extends SqlTypeNameSpec {
 
     if (charSetName != null) {
       writer.keyword("CHARACTER SET");
-      writer.identifier(charSetName, false);
+      writer.identifier(charSetName, true);
     }
   }
 
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlLateralOperator.java b/core/src/main/java/org/apache/calcite/sql/SqlLateralOperator.java
index 9e27459..6a97e2f 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlLateralOperator.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlLateralOperator.java
@@ -36,7 +36,10 @@ public class SqlLateralOperator extends SqlSpecialOperator {
       int rightPrec) {
     if (call.operandCount() == 1
         && (call.getOperandList().get(0).getKind() == SqlKind.COLLECTION_TABLE
-            || call.getOperandList().get(0).getKind() == SqlKind.SNAPSHOT)) {
+            || call.getOperandList().get(0).getKind() == SqlKind.SNAPSHOT
+            || call.getOperandList().get(0).getKind() == SqlKind.SELECT
+            || call.getOperandList().get(0).getKind() == SqlKind.AS)) {
+
       // do not create ( ) around the following TABLE clause
       writer.keyword(getName());
       call.operand(0).unparse(writer, 0, 0);
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlNode.java b/core/src/main/java/org/apache/calcite/sql/SqlNode.java
index 8005ec0..4b88133 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlNode.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlNode.java
@@ -129,17 +129,18 @@ public abstract class SqlNode implements Cloneable {
    * Returns the SQL text of the tree of which this <code>SqlNode</code> is
    * the root.
    *
-   * @param dialect     Dialect
-   * @param forceParens wraps all expressions in parentheses; good for parse
-   *                    test, but false by default.
+   * <p>Typical return values are:
    *
-   *                    <p>Typical return values are:</p>
-   *                    <ul>
-   *                    <li>'It''s a bird!'</li>
-   *                    <li>NULL</li>
-   *                    <li>12.3</li>
-   *                    <li>DATE '1969-04-29'</li>
-   *                    </ul>
+   * <ul>
+   * <li>'It''s a bird!'
+   * <li>NULL
+   * <li>12.3
+   * <li>DATE '1969-04-29'
+   * </ul>
+   *
+   * @param dialect     Dialect (null for ANSI SQL)
+   * @param forceParens Whether to wrap all expressions in parentheses;
+   *                    useful for parse test, but false by default
    */
   public SqlString toSqlString(SqlDialect dialect, boolean forceParens) {
     if (dialect == null) {
diff --git a/core/src/main/java/org/apache/calcite/sql/SqlUnnestOperator.java b/core/src/main/java/org/apache/calcite/sql/SqlUnnestOperator.java
index d55c07e..5f17842 100644
--- a/core/src/main/java/org/apache/calcite/sql/SqlUnnestOperator.java
+++ b/core/src/main/java/org/apache/calcite/sql/SqlUnnestOperator.java
@@ -99,7 +99,14 @@ public class SqlUnnestOperator extends SqlFunctionalOperator {
 
   @Override public void unparse(SqlWriter writer, SqlCall call, int leftPrec,
       int rightPrec) {
-    super.unparse(writer, call, leftPrec, rightPrec);
+    if (call.operandCount() == 1
+        && call.getOperandList().get(0).getKind() == SqlKind.SELECT) {
+      // avoid double ( ) on unnesting a sub-query
+      writer.keyword(getName());
+      call.operand(0).unparse(writer, 0, 0);
+    } else {
+      super.unparse(writer, call, leftPrec, rightPrec);
+    }
     if (withOrdinality) {
       writer.keyword("WITH ORDINALITY");
     }
diff --git a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
index 65aa98b..3035052 100644
--- a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
+++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
@@ -4163,8 +4163,8 @@ public class SqlValidatorImpl implements SqlValidatorWithHints {
     final RelDataType type = deriveType(scope, selectItem);
     setValidatedNodeType(selectItem, type);
 
-    // we do not want to pass on the RelRecordType returned
-    // by the sub query.  Just the type of the single expression
+    // We do not want to pass on the RelRecordType returned
+    // by the sub-query.  Just the type of the single expression
     // in the sub-query select list.
     assert type instanceof RelRecordType;
     RelRecordType rec = (RelRecordType) type;
diff --git a/core/src/main/java/org/apache/calcite/tools/PigRelBuilder.java b/core/src/main/java/org/apache/calcite/tools/PigRelBuilder.java
index 50738cd..25858d8 100644
--- a/core/src/main/java/org/apache/calcite/tools/PigRelBuilder.java
+++ b/core/src/main/java/org/apache/calcite/tools/PigRelBuilder.java
@@ -38,8 +38,7 @@ import java.util.List;
  */
 public class PigRelBuilder extends RelBuilder {
   private String lastAlias;
-
-  private PigRelBuilder(Context context,
+  protected PigRelBuilder(Context context,
       RelOptCluster cluster,
       RelOptSchema relOptSchema) {
     super(context, cluster, relOptSchema);
@@ -125,15 +124,9 @@ public class PigRelBuilder extends RelBuilder {
       int parallel, Iterable<? extends GroupKey> groupKeys) {
     @SuppressWarnings("unchecked") final List<GroupKeyImpl> groupKeyList =
         ImmutableList.copyOf((Iterable) groupKeys);
-    if (groupKeyList.isEmpty()) {
-      throw new IllegalArgumentException("must have at least one group");
-    }
+    validateGroupList(groupKeyList);
+
     final int groupCount = groupKeyList.get(0).nodes.size();
-    for (GroupKeyImpl groupKey : groupKeyList) {
-      if (groupKey.nodes.size() != groupCount) {
-        throw new IllegalArgumentException("group key size mismatch");
-      }
-    }
     final int n = groupKeyList.size();
     for (Ord<GroupKeyImpl> groupKey : Ord.reverse(groupKeyList)) {
       RelNode r = null;
@@ -159,7 +152,19 @@ public class PigRelBuilder extends RelBuilder {
     return this;
   }
 
-  String getAlias() {
+  protected void validateGroupList(List<GroupKeyImpl> groupKeyList) {
+    if (groupKeyList.isEmpty()) {
+      throw new IllegalArgumentException("must have at least one group");
+    }
+    final int groupCount = groupKeyList.get(0).nodes.size();
+    for (GroupKeyImpl groupKey : groupKeyList) {
+      if (groupKey.nodes.size() != groupCount) {
+        throw new IllegalArgumentException("group key size mismatch");
+      }
+    }
+  }
+
+  public String getAlias() {
     if (lastAlias != null) {
       return lastAlias;
     } else {
diff --git a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
index d86e0ed..0d14e46 100644
--- a/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
+++ b/core/src/main/java/org/apache/calcite/tools/RelBuilder.java
@@ -276,6 +276,18 @@ public class RelBuilder {
     return proto(Contexts.of(factories));
   }
 
+  public RelOptCluster getCluster() {
+    return cluster;
+  }
+
+  public RelOptSchema getRelOptSchema() {
+    return relOptSchema;
+  }
+
+  public RelFactories.TableScanFactory getScanFactory() {
+    return scanFactory;
+  }
+
   // Methods for manipulating the stack
 
   /** Adds a relational expression to be the input to the next relational
@@ -2577,11 +2589,11 @@ public class RelBuilder {
     GroupKey alias(String alias);
   }
 
-  /** Implementation of {@link GroupKey}. */
-  protected static class GroupKeyImpl implements GroupKey {
-    final ImmutableList<RexNode> nodes;
-    final ImmutableList<ImmutableList<RexNode>> nodeLists;
-    final String alias;
+  /** Implementation of {@link RelBuilder.GroupKey}. */
+  public static class GroupKeyImpl implements GroupKey {
+    public final ImmutableList<RexNode> nodes;
+    public final ImmutableList<ImmutableList<RexNode>> nodeLists;
+    public final String alias;
 
     GroupKeyImpl(ImmutableList<RexNode> nodes,
         ImmutableList<ImmutableList<RexNode>> nodeLists, String alias) {
diff --git a/core/src/test/java/org/apache/calcite/rel/logical/ToLogicalConverterTest.java b/core/src/test/java/org/apache/calcite/rel/logical/ToLogicalConverterTest.java
new file mode 100644
index 0000000..70c0b1f
--- /dev/null
+++ b/core/src/test/java/org/apache/calcite/rel/logical/ToLogicalConverterTest.java
@@ -0,0 +1,411 @@
+/*
+ * 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.calcite.rel.logical;
+
+import org.apache.calcite.adapter.enumerable.EnumerableConvention;
+import org.apache.calcite.adapter.enumerable.EnumerableInterpreterRule;
+import org.apache.calcite.adapter.enumerable.EnumerableRules;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.rules.ProjectToWindowRule;
+import org.apache.calcite.rex.RexCorrelVariable;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.parser.SqlParser;
+import org.apache.calcite.sql2rel.SqlToRelConverter;
+import org.apache.calcite.test.CalciteAssert;
+import org.apache.calcite.test.RelBuilderTest;
+import org.apache.calcite.tools.FrameworkConfig;
+import org.apache.calcite.tools.Frameworks;
+import org.apache.calcite.tools.Planner;
+import org.apache.calcite.tools.Program;
+import org.apache.calcite.tools.Programs;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.tools.RuleSets;
+import org.apache.calcite.util.Holder;
+import org.apache.calcite.util.TestUtil;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+
+import org.junit.Test;
+
+import static org.apache.calcite.test.Matchers.hasTree;
+
+import static org.junit.Assert.assertThat;
+
+/**
+ * Tests for {@link ToLogicalConverter}.
+ */
+public class ToLogicalConverterTest {
+  private static final ImmutableSet<RelOptRule> RULE_SET =
+      ImmutableSet.of(
+          ProjectToWindowRule.PROJECT,
+          EnumerableRules.ENUMERABLE_VALUES_RULE,
+          EnumerableRules.ENUMERABLE_JOIN_RULE,
+          EnumerableRules.ENUMERABLE_CORRELATE_RULE,
+          EnumerableRules.ENUMERABLE_PROJECT_RULE,
+          EnumerableRules.ENUMERABLE_FILTER_RULE,
+          EnumerableRules.ENUMERABLE_AGGREGATE_RULE,
+          EnumerableRules.ENUMERABLE_SORT_RULE,
+          EnumerableRules.ENUMERABLE_LIMIT_RULE,
+          EnumerableRules.ENUMERABLE_COLLECT_RULE,
+          EnumerableRules.ENUMERABLE_UNCOLLECT_RULE,
+          EnumerableRules.ENUMERABLE_UNION_RULE,
+          EnumerableRules.ENUMERABLE_WINDOW_RULE,
+          EnumerableRules.ENUMERABLE_TABLE_SCAN_RULE,
+          EnumerableInterpreterRule.INSTANCE);
+
+  private static final SqlToRelConverter.Config DEFAULT_REL_CONFIG =
+      SqlToRelConverter.configBuilder()
+          .withTrimUnusedFields(false)
+          .withConvertTableAccess(false)
+          .build();
+
+  private static FrameworkConfig frameworkConfig() {
+    final SchemaPlus rootSchema = Frameworks.createRootSchema(true);
+    final SchemaPlus schema = CalciteAssert.addSchema(rootSchema,
+        CalciteAssert.SchemaSpec.JDBC_FOODMART);
+    return Frameworks.newConfigBuilder()
+               .parserConfig(SqlParser.Config.DEFAULT)
+               .defaultSchema(schema)
+               .sqlToRelConverterConfig(DEFAULT_REL_CONFIG)
+               .build();
+  }
+
+  private static RelBuilder builder() {
+    return RelBuilder.create(RelBuilderTest.config().build());
+  }
+
+  private static RelNode rel(String sql) {
+    final Planner planner = Frameworks.getPlanner(frameworkConfig());
+    try {
+      SqlNode parse = planner.parse(sql);
+      SqlNode validate = planner.validate(parse);
+      return planner.rel(validate).rel;
+    } catch (Exception e) {
+      throw TestUtil.rethrow(e);
+    }
+  }
+
+  private static RelNode toPhysical(RelNode rel) {
+    final RelOptPlanner planner = rel.getCluster().getPlanner();
+    planner.clear();
+    for (RelOptRule rule : RULE_SET) {
+      planner.addRule(rule);
+    }
+
+    final Program program = Programs.of(RuleSets.ofList(planner.getRules()));
+    return program.run(planner, rel, rel.getTraitSet().replace(EnumerableConvention.INSTANCE),
+        ImmutableList.of(), ImmutableList.of());
+  }
+
+  private static RelNode toLogical(RelNode rel) {
+    return rel.accept(new ToLogicalConverter(builder()));
+  }
+
+  private void verify(RelNode rel, String expectedPhysical, String expectedLogical) {
+    RelNode physical = toPhysical(rel);
+    RelNode logical = toLogical(physical);
+    assertThat(physical, hasTree(expectedPhysical));
+    assertThat(logical, hasTree(expectedLogical));
+  }
+
+  @Test public void testValues() {
+    // Equivalent SQL:
+    //   VALUES (true, 1), (false, -50) AS t(a, b)
+    final RelBuilder builder = builder();
+    final RelNode rel =
+        builder
+            .values(new String[]{"a", "b"}, true, 1, false, -50)
+            .build();
+    verify(rel,
+        "EnumerableValues(tuples=[[{ true, 1 }, { false, -50 }]])\n",
+        "LogicalValues(tuples=[[{ true, 1 }, { false, -50 }]])\n");
+  }
+
+  @Test public void testScan() {
+    // Equivalent SQL:
+    //   SELECT *
+    //   FROM emp
+    final RelNode rel =
+        builder()
+            .scan("EMP")
+            .build();
+    verify(rel,
+        "EnumerableTableScan(table=[[scott, EMP]])\n",
+        "LogicalTableScan(table=[[scott, EMP]])\n");
+  }
+
+  @Test public void testProject() {
+    // Equivalent SQL:
+    //   SELECT deptno
+    //   FROM emp
+    final RelBuilder builder = builder();
+    final RelNode rel =
+        builder.scan("EMP")
+            .project(builder.field("DEPTNO"))
+            .build();
+    String expectedPhysial =
+        ""
+            + "EnumerableProject(DEPTNO=[$7])\n"
+            + "  EnumerableTableScan(table=[[scott, EMP]])\n";
+    String expectedLogical =
+        ""
+            + "LogicalProject(DEPTNO=[$7])\n"
+            + "  LogicalTableScan(table=[[scott, EMP]])\n";
+    verify(rel, expectedPhysial, expectedLogical);
+  }
+
+  @Test public void testFilter() {
+    // Equivalent SQL:
+    //   SELECT *
+    //   FROM emp
+    //   WHERE deptno = 10
+    final RelBuilder builder = builder();
+    final RelNode rel =
+        builder.scan("EMP")
+            .filter(
+                builder.equals(
+                    builder.field("DEPTNO"),
+                    builder.literal(10)))
+            .build();
+    String expectedPhysial =
+        ""
+            + "EnumerableFilter(condition=[=($7, 10)])\n"
+            + "  EnumerableTableScan(table=[[scott, EMP]])\n";
+    String expectedLogical =
+        ""
+            + "LogicalFilter(condition=[=($7, 10)])\n"
+            + "  LogicalTableScan(table=[[scott, EMP]])\n";
+    verify(rel, expectedPhysial, expectedLogical);
+  }
+
+  @Test public void testSort() {
+    // Equivalent SQL:
+    //   SELECT *
+    //   FROM emp
+    //   ORDER BY 3
+    final RelBuilder builder = builder();
+    final RelNode rel =
+        builder.scan("EMP")
+            .sort(builder.field(2))
+            .build();
+    String expectedPhysial =
+        ""
+            + "EnumerableSort(sort0=[$2], dir0=[ASC])\n"
+            + "  EnumerableTableScan(table=[[scott, EMP]])\n";
+    String expectedLogical =
+        ""
+            + "LogicalSort(sort0=[$2], dir0=[ASC])\n"
+            + "  LogicalTableScan(table=[[scott, EMP]])\n";
+    verify(rel, expectedPhysial, expectedLogical);
+  }
+
+  @Test public void testLimit() {
+    // Equivalent SQL:
+    //   SELECT *
+    //   FROM emp
+    //   FETCH 10
+    final RelBuilder builder = builder();
+    final RelNode rel =
+        builder.scan("EMP")
+            .limit(0, 10)
+            .build();
+    String expectedPhysial =
+        ""
+            + "EnumerableLimit(fetch=[10])\n"
+            + "  EnumerableTableScan(table=[[scott, EMP]])\n";
+    String expectedLogical =
+        ""
+            + "LogicalSort(fetch=[10])\n"
+            + "  LogicalTableScan(table=[[scott, EMP]])\n";
+    verify(rel, expectedPhysial, expectedLogical);
+  }
+
+  @Test public void testSortLimit() {
+    // Equivalent SQL:
+    //   SELECT *
+    //   FROM emp
+    //   ORDER BY deptno DESC FETCH 10
+    final RelBuilder builder = builder();
+    final RelNode rel =
+        builder.scan("EMP")
+            .sortLimit(-1, 10, builder.desc(builder.field("DEPTNO")))
+            .build();
+    String expectedPhysial =
+        ""
+            + "EnumerableLimit(fetch=[10])\n"
+            + "  EnumerableSort(sort0=[$7], dir0=[DESC])\n"
+            + "    EnumerableTableScan(table=[[scott, EMP]])\n";
+    String expectedLogical =
+        ""
+            + "LogicalSort(sort0=[$7], dir0=[DESC], fetch=[10])\n"
+            + "  LogicalTableScan(table=[[scott, EMP]])\n";
+    verify(rel, expectedPhysial, expectedLogical);
+  }
+
+  @Test public void testAggregate() {
+    // Equivalent SQL:
+    //   SELECT COUNT(empno) AS c
+    //   FROM emp
+    //   GROUP BY deptno
+    final RelBuilder builder = builder();
+    final RelNode rel =
+        builder.scan("EMP")
+            .aggregate(builder.groupKey(builder.field("DEPTNO")),
+                builder.count(false, "C", builder.field("EMPNO")))
+            .build();
+    String expectedPhysial =
+        ""
+            + "EnumerableAggregate(group=[{7}], C=[COUNT($0)])\n"
+            + "  EnumerableTableScan(table=[[scott, EMP]])\n";
+    String expectedLogical =
+        ""
+            + "LogicalAggregate(group=[{7}], C=[COUNT($0)])\n"
+            + "  LogicalTableScan(table=[[scott, EMP]])\n";
+    verify(rel, expectedPhysial, expectedLogical);
+  }
+
+  @Test public void testJoin() {
+    // Equivalent SQL:
+    //   SELECT *
+    //   FROM emp
+    //   JOIN dept ON emp.deptno = dept.deptno
+    final RelBuilder builder = builder();
+    final RelNode rel =
+        builder.scan("EMP")
+            .scan("DEPT")
+            .join(JoinRelType.INNER,
+                builder.call(SqlStdOperatorTable.EQUALS,
+                    builder.field(2, 0, "DEPTNO"),
+                    builder.field(2, 1, "DEPTNO")))
+            .build();
+    String expectedPhysial =
+        ""
+            + "EnumerableHashJoin(condition=[=($7, $8)], joinType=[inner])\n"
+            + "  EnumerableTableScan(table=[[scott, EMP]])\n"
+            + "  EnumerableTableScan(table=[[scott, DEPT]])\n";
+    String expectedLogical =
+        ""
+            + "LogicalJoin(condition=[=($7, $8)], joinType=[inner])\n"
+            + "  LogicalTableScan(table=[[scott, EMP]])\n"
+            + "  LogicalTableScan(table=[[scott, DEPT]])\n";
+    verify(rel, expectedPhysial, expectedLogical);
+  }
+
+  @Test public void testCorrelation() {
+    final RelBuilder builder = builder();
+    final Holder<RexCorrelVariable> v = Holder.of(null);
+    final RelNode rel = builder.scan("EMP")
+                       .variable(v)
+                       .scan("DEPT")
+                       .join(JoinRelType.LEFT,
+                           builder.equals(builder.field(2, 0, "SAL"),
+                               builder.literal(1000)),
+                           ImmutableSet.of(v.get().id))
+                       .build();
+    String expectedPhysial =
+        ""
+            + "EnumerableCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{}])\n"
+            + "  EnumerableTableScan(table=[[scott, EMP]])\n"
+            + "  EnumerableFilter(condition=[=($cor0.SAL, 1000)])\n"
+            + "    EnumerableTableScan(table=[[scott, DEPT]])\n";
+    String expectedLogical =
+        ""
+            + "LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{5}])\n"
+            + "  LogicalTableScan(table=[[scott, EMP]])\n"
+            + "  LogicalFilter(condition=[=($cor0.SAL, 1000)])\n"
+            + "    LogicalTableScan(table=[[scott, DEPT]])\n";
+    verify(rel, expectedPhysial, expectedLogical);
+  }
+
+  @Test public void testUnion() {
+    // Equivalent SQL:
+    //   SELECT deptno FROM emp
+    //   UNION ALL
+    //   SELECT deptno FROM dept
+    final RelBuilder builder = builder();
+    RelNode rel =
+        builder.scan("DEPT")
+            .project(builder.field("DEPTNO"))
+            .scan("EMP")
+            .project(builder.field("DEPTNO"))
+            .union(true)
+            .build();
+    String expectedPhysial =
+        ""
+            + "EnumerableUnion(all=[true])\n"
+            + "  EnumerableProject(DEPTNO=[$0])\n"
+            + "    EnumerableTableScan(table=[[scott, DEPT]])\n"
+            + "  EnumerableProject(DEPTNO=[$7])\n"
+            + "    EnumerableTableScan(table=[[scott, EMP]])\n";
+    String expectedLogical =
+        ""
+            + "LogicalUnion(all=[true])\n"
+            + "  LogicalProject(DEPTNO=[$0])\n"
+            + "    LogicalTableScan(table=[[scott, DEPT]])\n"
+            + "  LogicalProject(DEPTNO=[$7])\n"
+            + "    LogicalTableScan(table=[[scott, EMP]])\n";
+    verify(rel, expectedPhysial, expectedLogical);
+  }
+
+  @Test public void testUncollect() {
+    final String sql =
+        ""
+            + "select did \n"
+            + "from unnest(select collect(\"department_id\") as deptid"
+            + "            from \"department\") as t(did)";
+    String expectedPhysial =
+        ""
+            + "EnumerableUncollect\n"
+            + "  EnumerableAggregate(group=[{}], DEPTID=[COLLECT($0)])\n"
+            + "    JdbcToEnumerableConverter\n"
+            + "      JdbcProject(department_id=[$0])\n"
+            + "        JdbcTableScan(table=[[foodmart, department]])\n";
+    String expectedLogical =
+        ""
+            + "Uncollect\n"
+            + "  LogicalAggregate(group=[{}], DEPTID=[COLLECT($0)])\n"
+            + "    LogicalProject(department_id=[$0])\n"
+            + "      LogicalTableScan(table=[[foodmart, department]])\n";
+    verify(rel(sql), expectedPhysial, expectedLogical);
+  }
+
+  @Test public void testWindow() {
+    String sql = "SELECT rank() over (order by \"hire_date\") FROM \"employee\"";
+    String expectedPhysial =
+        ""
+            + "EnumerableProject($0=[$17])\n"
+            + "  EnumerableWindow(window#0=[window(partition {} order by [9] range between "
+            + "UNBOUNDED PRECEDING and CURRENT ROW aggs [RANK()])])\n"
+            + "    JdbcToEnumerableConverter\n"
+            + "      JdbcTableScan(table=[[foodmart, employee]])\n";
+    String expectedLogical =
+        ""
+            + "LogicalProject($0=[$17])\n"
+            + "  LogicalWindow(window#0=[window(partition {} order by [9] range between UNBOUNDED"
+            + " PRECEDING and CURRENT ROW aggs [RANK()])])\n"
+            + "    LogicalTableScan(table=[[foodmart, employee]])\n";
+    verify(rel(sql), expectedPhysial, expectedLogical);
+  }
+}
+
+// End ToLogicalConverterTest.java
diff --git a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
index 7454730..7f9fc34 100644
--- a/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
+++ b/core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java
@@ -3363,6 +3363,42 @@ public class RelToSqlConverterTest {
         callsUnparseCallOnSqlSelect[0], is(true));
   }
 
+  @Test public void testCorrelate() {
+    final String sql = "select d.\"department_id\", d_plusOne "
+        + "from \"department\" as d, "
+        + "       lateral (select d.\"department_id\" + 1 as d_plusOne"
+        + "                from (values(true)))";
+
+    final String expected = "SELECT \"$cor0\".\"department_id\", \"$cor0\".\"D_PLUSONE\"\n"
+        + "FROM \"foodmart\".\"department\" AS \"$cor0\",\n"
+        + "LATERAL (SELECT \"$cor0\".\"department_id\" + 1 AS \"D_PLUSONE\"\n"
+        + "FROM (VALUES  (TRUE)) AS \"t\" (\"EXPR$0\")) AS \"t0\"";
+    sql(sql).ok(expected);
+  }
+
+  @Test public void testUncollectExplicitAlias() {
+    final String sql = "select did + 1 \n"
+        + "from unnest(select collect(\"department_id\") as deptid"
+        + "            from \"department\") as t(did)";
+
+    final String expected = "SELECT \"DEPTID\" + 1\n"
+        + "FROM UNNEST (SELECT COLLECT(\"department_id\") AS \"DEPTID\"\n"
+        + "FROM \"foodmart\".\"department\") AS \"t0\" (\"DEPTID\")";
+    sql(sql).ok(expected);
+  }
+
+  @Test public void testUncollectImplicitAlias() {
+    final String sql = "select did + 1 \n"
+        + "from unnest(select collect(\"department_id\") "
+        + "            from \"department\") as t(did)";
+
+    final String expected = "SELECT \"col_0\" + 1\n"
+        + "FROM UNNEST (SELECT COLLECT(\"department_id\")\n"
+        + "FROM \"foodmart\".\"department\") AS \"t0\" (\"col_0\")";
+    sql(sql).ok(expected);
+  }
+
+
   @Test public void testWithinGroup1() {
     final String query = "select \"product_class_id\", collect(\"net_weight\") "
         + "within group (order by \"net_weight\" desc) "
diff --git a/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java b/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
index d056ed4..c9f8cb5 100644
--- a/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
+++ b/core/src/test/java/org/apache/calcite/sql/parser/SqlParserTest.java
@@ -2507,9 +2507,9 @@ public class SqlParserTest {
         + "outer apply (select * from emp where emp.deptno = dept.deptno)";
     final String expected = "SELECT *\n"
         + "FROM `DEPT`\n"
-        + "LEFT JOIN LATERAL((SELECT *\n"
+        + "LEFT JOIN LATERAL (SELECT *\n"
         + "FROM `EMP`\n"
-        + "WHERE (`EMP`.`DEPTNO` = `DEPT`.`DEPTNO`))) ON TRUE";
+        + "WHERE (`EMP`.`DEPTNO` = `DEPT`.`DEPTNO`)) ON TRUE";
     sql(sql).ok(expected);
   }
 
@@ -2519,9 +2519,9 @@ public class SqlParserTest {
         + "outer apply (select * from emp where emp.deptno = dept.deptno)";
     final String expected = "SELECT *\n"
         + "FROM `DEPT`\n"
-        + "LEFT JOIN LATERAL((SELECT *\n"
+        + "LEFT JOIN LATERAL (SELECT *\n"
         + "FROM `EMP`\n"
-        + "WHERE (`EMP`.`DEPTNO` = `DEPT`.`DEPTNO`))) ON TRUE";
+        + "WHERE (`EMP`.`DEPTNO` = `DEPT`.`DEPTNO`)) ON TRUE";
     sql(sql).ok(expected);
   }
 
@@ -3467,8 +3467,8 @@ public class SqlParserTest {
 
     // Good: LATERAL (subQuery)
     final String expected2 = "SELECT *\n"
-        + "FROM LATERAL((SELECT *\n"
-        + "FROM `EMP`))";
+        + "FROM LATERAL (SELECT *\n"
+        + "FROM `EMP`)";
     sql("select * from lateral (select * from emp)").ok(expected2);
     sql("select * from lateral (select * from emp) as t")
         .ok(expected2 + " AS `T`");
diff --git a/core/src/test/java/org/apache/calcite/test/Matchers.java b/core/src/test/java/org/apache/calcite/test/Matchers.java
index 97e6ff9..1d85be6 100644
--- a/core/src/test/java/org/apache/calcite/test/Matchers.java
+++ b/core/src/test/java/org/apache/calcite/test/Matchers.java
@@ -32,6 +32,7 @@ import org.hamcrest.Factory;
 import org.hamcrest.Matcher;
 import org.hamcrest.TypeSafeMatcher;
 import org.hamcrest.core.Is;
+import org.hamcrest.core.StringContains;
 
 import java.sql.ResultSet;
 import java.sql.SQLException;
@@ -180,8 +181,8 @@ public class Matchers {
   }
 
   /**
-   * Creates a Matcher that matches a {@link RelNode} its string representation,
-   * after converting Windows-style line endings ("\r\n")
+   * Creates a Matcher that matches a {@link RelNode} if its string
+   * representation, after converting Windows-style line endings ("\r\n")
    * to Unix-style line endings ("\n"), is equal to the given {@code value}.
    */
   @Factory
@@ -193,6 +194,20 @@ public class Matchers {
   }
 
   /**
+   * Creates a Matcher that matches a {@link RelNode} if its string
+   * representation, after converting Windows-style line endings ("\r\n")
+   * to Unix-style line endings ("\n"), contains the given {@code value}
+   * as a substring.
+   */
+  @Factory
+  public static Matcher<RelNode> inTree(final String value) {
+    return compose(StringContains.containsString(value), input -> {
+      // Convert RelNode to a string with Linux line-endings
+      return Util.toLinux(RelOptUtil.toString(input));
+    });
+  }
+
+  /**
    * Creates a matcher that matches when the examined string is equal to the
    * specified <code>operand</code> when all Windows-style line endings ("\r\n")
    * have been converted to Unix-style line endings ("\n").
diff --git a/kafka/pom.xml b/kafka/pom.xml
index bdd5c99..153e227 100644
--- a/kafka/pom.xml
+++ b/kafka/pom.xml
@@ -26,8 +26,8 @@ limitations under the License.
   <!-- The basics. -->
   <artifactId>calcite-kafka</artifactId>
   <packaging>jar</packaging>
-  <name>calcite kafka</name>
-  <description>Kafka Adapter. Exposes kafka topic(s) as stream table(s).</description>
+  <name>Calcite Kafka</name>
+  <description>Kafka adapter for Calcite; exposes Kafka topics as stream tables</description>
 
   <properties>
     <top.dir>${project.basedir}/..</top.dir>
diff --git a/piglet/pom.xml b/piglet/pom.xml
index 55ba8e0..243d9ae 100644
--- a/piglet/pom.xml
+++ b/piglet/pom.xml
@@ -56,6 +56,11 @@ limitations under the License.
     </dependency>
 
     <dependency>
+      <groupId>com.google.code.findbugs</groupId>
+      <artifactId>jsr305</artifactId>
+      <optional>true</optional>
+    </dependency>
+    <dependency>
       <groupId>com.google.guava</groupId>
       <artifactId>guava</artifactId>
     </dependency>
@@ -88,6 +93,20 @@ limitations under the License.
       <artifactId>slf4j-log4j12</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.pig</groupId>
+      <artifactId>pig</artifactId>
+      <classifier>h2</classifier>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.hadoop</groupId>
+      <artifactId>hadoop-common</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.hadoop</groupId>
+      <artifactId>hadoop-client</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
@@ -116,6 +135,9 @@ limitations under the License.
         <configuration>
           <includes>
             <include>org/apache/calcite/test/PigletTest.java</include>
+            <include>org/apache/calcite/test/PigRelExTest.java</include>
+            <include>org/apache/calcite/test/PigRelOpTest.java</include>
+            <include>org/apache/calcite/test/PigScriptTest.java</include>
           </includes>
         </configuration>
       </plugin>
@@ -156,8 +178,11 @@ limitations under the License.
               <ignoredUnusedDeclaredDependencies>
                 <ignoredUnusedDeclaredDependency>net.hydromatic:scott-data-hsqldb</ignoredUnusedDeclaredDependency>
                 <ignoredUnusedDeclaredDependency>org.hsqldb:hsqldb</ignoredUnusedDeclaredDependency>
+                <ignoredUnusedDeclaredDependency>org.apache.hadoop:hadoop-core</ignoredUnusedDeclaredDependency>
                 <ignoredUnusedDeclaredDependency>org.slf4j:slf4j-api</ignoredUnusedDeclaredDependency>
                 <ignoredUnusedDeclaredDependency>org.slf4j:slf4j-log4j12</ignoredUnusedDeclaredDependency>
+                <ignoredUnusedDeclaredDependency>org.apache.hadoop:hadoop-common</ignoredUnusedDeclaredDependency>
+                <ignoredUnusedDeclaredDependency>org.apache.hadoop:hadoop-client</ignoredUnusedDeclaredDependency>
               </ignoredUnusedDeclaredDependencies>
             </configuration>
           </execution>
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/DynamicTupleRecordType.java b/piglet/src/main/java/org/apache/calcite/piglet/DynamicTupleRecordType.java
new file mode 100644
index 0000000..d455d5f
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/DynamicTupleRecordType.java
@@ -0,0 +1,76 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.calcite.rel.type.DynamicRecordTypeImpl;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeField;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Represents Pig Tuples with unknown fields. The tuple field
+ * can only be accessed via name '$index', like ('$0', '$1').
+ * The tuple is then resized to match the index.
+ */
+public class DynamicTupleRecordType extends DynamicRecordTypeImpl {
+  private static final Pattern INDEX_PATTERN = Pattern.compile("^\\$(\\d+)$");
+
+  DynamicTupleRecordType(RelDataTypeFactory typeFactory) {
+    super(typeFactory);
+  }
+
+  @Override public RelDataTypeField getField(String fieldName,
+      boolean caseSensitive, boolean elideRecord) {
+    final int index = nameToIndex(fieldName);
+    if (index >= 0) {
+      resize(index + 1);
+      return super.getField(fieldName, caseSensitive, elideRecord);
+    }
+    return null;
+  }
+
+  /**
+   * Resizes the record if the new size greater than the current size.
+   *
+   * @param size New size
+   */
+  void resize(int size) {
+    int currentSize = getFieldCount();
+    if (size > currentSize) {
+      for (int i = currentSize; i < size; i++) {
+        super.getField("$" + i, true, true);
+      }
+      computeDigest();
+    }
+  }
+
+  /**
+   * Gets index number from field name.
+   * @param fieldName Field name, format example '$1'
+   */
+  private static int nameToIndex(String fieldName) {
+    Matcher matcher = INDEX_PATTERN.matcher(fieldName);
+    if (matcher.find()) {
+      return Integer.parseInt(matcher.group(1));
+    }
+    return -1;
+  }
+}
+
+// End DynamicTupleRecordType.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigConverter.java b/piglet/src/main/java/org/apache/calcite/piglet/PigConverter.java
new file mode 100644
index 0000000..9f3f5b6
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigConverter.java
@@ -0,0 +1,288 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.calcite.adapter.enumerable.EnumerableConvention;
+import org.apache.calcite.adapter.enumerable.EnumerableInterpreterRule;
+import org.apache.calcite.adapter.enumerable.EnumerableRules;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelCollations;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rel.logical.ToLogicalConverter;
+import org.apache.calcite.rel.rel2sql.RelToSqlConverter;
+import org.apache.calcite.rel.rules.FilterMergeRule;
+import org.apache.calcite.rel.rules.FilterProjectTransposeRule;
+import org.apache.calcite.rel.rules.ProjectMergeRule;
+import org.apache.calcite.rel.rules.ProjectToWindowRule;
+import org.apache.calcite.rel.rules.ProjectWindowTransposeRule;
+import org.apache.calcite.sql.SqlDialect;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.pretty.SqlPrettyWriter;
+import org.apache.calcite.tools.FrameworkConfig;
+import org.apache.calcite.tools.Program;
+import org.apache.calcite.tools.Programs;
+import org.apache.calcite.tools.RuleSets;
+
+import org.apache.pig.ExecType;
+import org.apache.pig.PigServer;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.newplan.logical.relational.LogicalPlan;
+
+import com.google.common.collect.ImmutableList;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Extension from PigServer to convert Pig scripts into logical relational
+ * algebra plans and SQL statements.
+ */
+public class PigConverter extends PigServer {
+  // Basic transformation and implementation rules to optimize for Pig-translated logical plans
+  private static final List<RelOptRule> PIG_RULES =
+      ImmutableList.of(
+          ProjectToWindowRule.PROJECT,
+          PigToSqlAggregateRule.INSTANCE,
+          EnumerableRules.ENUMERABLE_VALUES_RULE,
+          EnumerableRules.ENUMERABLE_JOIN_RULE,
+          EnumerableRules.ENUMERABLE_CORRELATE_RULE,
+          EnumerableRules.ENUMERABLE_PROJECT_RULE,
+          EnumerableRules.ENUMERABLE_FILTER_RULE,
+          EnumerableRules.ENUMERABLE_AGGREGATE_RULE,
+          EnumerableRules.ENUMERABLE_SORT_RULE,
+          EnumerableRules.ENUMERABLE_LIMIT_RULE,
+          EnumerableRules.ENUMERABLE_COLLECT_RULE,
+          EnumerableRules.ENUMERABLE_UNCOLLECT_RULE,
+          EnumerableRules.ENUMERABLE_UNION_RULE,
+          EnumerableRules.ENUMERABLE_WINDOW_RULE,
+          EnumerableRules.ENUMERABLE_TABLE_SCAN_RULE,
+          EnumerableInterpreterRule.INSTANCE);
+
+  private static final List<RelOptRule> TRANSFORM_RULES =
+      ImmutableList.of(
+          ProjectWindowTransposeRule.INSTANCE,
+          FilterMergeRule.INSTANCE,
+          ProjectMergeRule.INSTANCE,
+          FilterProjectTransposeRule.INSTANCE,
+          EnumerableRules.ENUMERABLE_VALUES_RULE,
+          EnumerableRules.ENUMERABLE_JOIN_RULE,
+          EnumerableRules.ENUMERABLE_CORRELATE_RULE,
+          EnumerableRules.ENUMERABLE_PROJECT_RULE,
+          EnumerableRules.ENUMERABLE_FILTER_RULE,
+          EnumerableRules.ENUMERABLE_AGGREGATE_RULE,
+          EnumerableRules.ENUMERABLE_SORT_RULE,
+          EnumerableRules.ENUMERABLE_LIMIT_RULE,
+          EnumerableRules.ENUMERABLE_COLLECT_RULE,
+          EnumerableRules.ENUMERABLE_UNCOLLECT_RULE,
+          EnumerableRules.ENUMERABLE_UNION_RULE,
+          EnumerableRules.ENUMERABLE_WINDOW_RULE,
+          EnumerableRules.ENUMERABLE_TABLE_SCAN_RULE,
+          EnumerableInterpreterRule.INSTANCE);
+
+  private final PigRelBuilder builder;
+
+  private PigConverter(FrameworkConfig config, ExecType execType)
+      throws Exception {
+    super(execType);
+    this.builder = PigRelBuilder.create(config);
+  }
+
+  public static PigConverter create(FrameworkConfig config) throws Exception {
+    return new PigConverter(config, ExecType.LOCAL);
+  }
+
+  public PigRelBuilder getBuilder() {
+    return builder;
+  }
+
+  /**
+   * Parses a Pig script and converts it into relational algebra plans,
+   * optimizing the result.
+   *
+   * <p>Equivalent to {@code pigQuery2Rel(pigQuery, true, true, true)}.
+   *
+   * @param pigQuery Pig script
+   *
+   * @return A list of root nodes of the translated relational plans. Each of
+   * these root corresponds to a sink operator (normally a STORE command) in the
+   * Pig plan
+   *
+   * @throws IOException Exception during parsing or translating Pig
+   */
+  public List<RelNode> pigQuery2Rel(String pigQuery) throws IOException {
+    return pigQuery2Rel(pigQuery, true, true, true);
+  }
+
+  /**
+   * Parses a Pig script and converts it into relational algebra plans.
+   *
+   * @param pigQuery Pig script
+   * @param planRewrite Whether to rewrite the translated plan
+   * @param validate Whether to validate the Pig logical plan before doing
+   *   translation
+   * @param usePigRules Whether to use Pig Rules (see PigRelPlanner} to rewrite
+   *   translated rel plan
+   *
+   * @return A list of root nodes of the translated relational plans. Each of
+   * these root corresponds to a sink operator (normally a STORE command) in the
+   * Pig plan
+   *
+   * @throws IOException Exception during parsing or translating Pig
+   */
+  public List<RelNode> pigQuery2Rel(String pigQuery, boolean planRewrite,
+      boolean validate, boolean usePigRules) throws IOException {
+    setBatchOn();
+    registerQuery(pigQuery);
+    final LogicalPlan pigPlan = getCurrentDAG().getLogicalPlan();
+    if (validate) {
+      pigPlan.validate(getPigContext(), scope, false);
+    }
+    return pigPlan2Rel(pigPlan, planRewrite, usePigRules);
+  }
+
+  /**
+   * Gets a Pig script string from a file after doing param substitution.
+   *
+   * @param in Pig script file
+   * @param params Param sub map
+   */
+  public String getPigScript(InputStream in, Map<String, String> params)
+      throws IOException {
+    return getPigContext().doParamSubstitution(in, paramMapToList(params), null);
+  }
+
+  /**
+   * Parses a Pig script and converts it into relational algebra plans.
+   *
+   * @param fileName File name
+   * @param params Param substitution map
+   * @param planRewrite Whether to rewrite the translated plan
+   *
+   * @return A list of root nodes of the translated relational plans. Each of
+   * these root corresponds to a sink operator (normally a STORE command) in the
+   * Pig plan
+   *
+   * @throws IOException Exception during parsing or translating Pig
+   */
+  public List<RelNode> pigScript2Rel(String fileName, Map<String, String> params,
+      boolean planRewrite) throws IOException {
+    setBatchOn();
+    registerScript(fileName, params);
+    final LogicalPlan pigPlan = getCurrentDAG().getLogicalPlan();
+    pigPlan.validate(getPigContext(), scope, false);
+
+    return pigPlan2Rel(pigPlan, planRewrite, true);
+  }
+
+  private List<RelNode> pigPlan2Rel(LogicalPlan pigPlan, boolean planRewrite,
+      boolean usePigRules) throws FrontendException {
+    final PigRelOpWalker walker = new PigRelOpWalker(pigPlan);
+    List<RelNode> relNodes =
+        new PigRelOpVisitor(pigPlan, walker, builder).translate();
+    final List<RelNode> storeRels = builder.getRelsForStores();
+    relNodes = storeRels != null ? storeRels : relNodes;
+
+    if (usePigRules) {
+      relNodes = optimizePlans(relNodes, PIG_RULES);
+    }
+    if (planRewrite) {
+      relNodes = optimizePlans(relNodes, TRANSFORM_RULES);
+    }
+    return relNodes;
+  }
+
+  /**
+   * Converts a Pig script to a list of SQL statements.
+   *
+   * @param pigQuery Pig script
+   * @param sqlDialect Dialect of SQL language
+   * @throws IOException Exception during parsing or translating Pig
+   */
+  public List<String> pigToSql(String pigQuery, SqlDialect sqlDialect)
+      throws IOException {
+    final SqlPrettyWriter writer = new SqlPrettyWriter(sqlDialect);
+    writer.setQuoteAllIdentifiers(false);
+    writer.setAlwaysUseParentheses(false);
+    writer.setSelectListItemsOnSeparateLines(false);
+    writer.setIndentation(2);
+    return pigToSql(pigQuery, writer);
+  }
+
+  /**
+   * Converts a Pig script to a list of SQL statements.
+   *
+   * @param pigQuery Pig script
+   * @param writer The SQL writer to decide dialect and format of SQL statements
+   * @throws IOException Exception during parsing or translating Pig
+   */
+  private List<String> pigToSql(String pigQuery, SqlWriter writer)
+      throws IOException {
+    final RelToSqlConverter sqlConverter =
+        new PigRelToSqlConverter(writer.getDialect());
+    final List<RelNode> finalRels = pigQuery2Rel(pigQuery);
+    final List<String> sqlStatements = new ArrayList<>();
+    for (RelNode rel : finalRels) {
+      final SqlNode sqlNode = sqlConverter.visitChild(0, rel).asStatement();
+      sqlNode.unparse(writer, 0, 0);
+      sqlStatements.add(writer.toString());
+    }
+    return sqlStatements;
+  }
+
+  private List<RelNode> optimizePlans(List<RelNode> originalRels,
+      List<RelOptRule> rules) {
+    final RelOptPlanner planner = originalRels.get(0).getCluster().getPlanner();
+    // Remember old rule set of the planner before resetting it with new rules
+    final List<RelOptRule> oldRules = planner.getRules();
+    resetPlannerRules(planner, rules);
+    final Program program = Programs.of(RuleSets.ofList(planner.getRules()));
+    final List<RelNode> optimizedPlans = new ArrayList<>();
+    for (RelNode rel : originalRels) {
+      final RelCollation collation = rel instanceof Sort
+          ? ((Sort) rel).collation
+          : RelCollations.EMPTY;
+      // Apply the planner to obtain the physical plan
+      final RelNode physicalPlan = program.run(planner, rel,
+          rel.getTraitSet().replace(EnumerableConvention.INSTANCE)
+              .replace(collation).simplify(),
+          ImmutableList.of(), ImmutableList.of());
+
+      // Then convert the physical plan back to logical plan
+      final RelNode logicalPlan = new ToLogicalConverter(builder).visit(physicalPlan);
+      optimizedPlans.add(logicalPlan);
+    }
+    resetPlannerRules(planner, oldRules);
+    return optimizedPlans;
+  }
+
+  private void resetPlannerRules(RelOptPlanner planner,
+      List<RelOptRule> rulesToSet) {
+    planner.clear();
+    for (RelOptRule rule : rulesToSet) {
+      planner.addRule(rule);
+    }
+  }
+}
+
+// End PigConverter.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigRelBuilder.java b/piglet/src/main/java/org/apache/calcite/piglet/PigRelBuilder.java
new file mode 100644
index 0000000..d23a137
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigRelBuilder.java
@@ -0,0 +1,658 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.calcite.plan.Context;
+import org.apache.calcite.plan.Convention;
+import org.apache.calcite.plan.RelOptCluster;
+import org.apache.calcite.plan.RelOptSchema;
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.SingleRel;
+import org.apache.calcite.rel.core.CorrelationId;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Uncollect;
+import org.apache.calcite.rel.logical.LogicalJoin;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.runtime.Hook;
+import org.apache.calcite.sql.SqlAggFunction;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.type.MultisetSqlType;
+import org.apache.calcite.tools.FrameworkConfig;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.Static;
+import org.apache.calcite.util.Util;
+
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.newplan.Operator;
+import org.apache.pig.newplan.logical.relational.LogicalRelationalOperator;
+import org.apache.pig.scripting.jython.JythonFunction;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Extension to {@link RelBuilder} for Pig logical operators.
+ */
+public class PigRelBuilder extends RelBuilder {
+  private final Map<RelNode, String> reverseAliasMap = new HashMap<>();
+  private final Map<String, RelNode> aliasMap = new HashMap<>();
+  private final Map<Operator, RelNode> pigRelMap = new HashMap<>();
+  private final Map<RelNode, Operator> relPigMap = new HashMap<>();
+  private final Map<String, RelNode> storeMap = new HashMap<>();
+  private int nextCorrelId = 0;
+  private final PigRelTranslationContext pigRelContext =
+      new PigRelTranslationContext();
+
+  private PigRelBuilder(Context context, RelOptCluster cluster,
+      RelOptSchema relOptSchema) {
+    super(context, cluster, relOptSchema);
+  }
+
+  /** Creates a PigRelBuilder. */
+  public static PigRelBuilder create(FrameworkConfig config) {
+    final RelBuilder relBuilder = RelBuilder.create(config);
+    Hook.REL_BUILDER_SIMPLIFY.add(Hook.propertyJ(false));
+    return new PigRelBuilder(config.getContext(), relBuilder.getCluster(),
+        relBuilder.getRelOptSchema());
+  }
+
+  public RelNode getRel(String alias) {
+    return aliasMap.get(alias);
+  }
+
+  public RelNode getRel(Operator pig) {
+    return pigRelMap.get(pig);
+  }
+
+  Operator getPig(RelNode rel)  {
+    return relPigMap.get(rel);
+  }
+
+  String getAlias(RelNode rel)  {
+    return reverseAliasMap.get(rel);
+  }
+
+  /**
+   * Gets the next correlation id.
+   *
+   * @return The correlation id
+   */
+  CorrelationId nextCorrelId() {
+    return new CorrelationId(nextCorrelId++);
+  }
+
+  @Override protected boolean shouldMergeProject() {
+    return false;
+  }
+
+  public String getAlias() {
+    final RelNode input = peek();
+    if (reverseAliasMap.containsKey(input)) {
+      return reverseAliasMap.get(input);
+    }
+    return null;
+  }
+
+  @Override public void clear() {
+    super.clear();
+    reverseAliasMap.clear();
+    aliasMap.clear();
+    pigRelMap.clear();
+    relPigMap.clear();
+    storeMap.clear();
+    nextCorrelId = 0;
+  }
+
+  /**
+   * Checks if a Pig logical operator has been translated before. If it has,
+   * push the corresponding relational algebra operator on top instead of
+   * doing the translation work again.
+   *
+   * @param pigOp The Pig logical operator to check.
+   * @return true iff the pigOp has been processed before.
+   */
+  public boolean checkMap(LogicalRelationalOperator pigOp) {
+    if (pigRelMap.containsKey(pigOp)) {
+      push(pigRelMap.get(pigOp));
+      return true;
+    }
+    return false;
+  }
+
+  /**
+   * Updates the Pig logical operator and its alias with the top
+   * relational algebra node.
+   *
+   * @param pigOp the Pig logical operator
+   * @param alias the alias
+   * @param updatePigRelMap whether to update the PigRelMap
+   */
+  public void updateAlias(Operator pigOp, String alias, boolean updatePigRelMap) {
+    final RelNode rel = peek();
+    if (updatePigRelMap) {
+      pigRelMap.put(pigOp, rel);
+    }
+    relPigMap.put(rel, pigOp);
+    aliasMap.put(alias, rel);
+    reverseAliasMap.put(rel, alias);
+  }
+
+  /**
+   * Registers the Pig logical operator with the top relational algebra node.
+   *
+   * @param pigOp the Pig logical operator
+   */
+  void register(LogicalRelationalOperator pigOp) {
+    updateAlias(pigOp, pigOp.getAlias(), true);
+  }
+
+  void registerPigUDF(String className, FuncSpec pigFunc) {
+    Class udfClass = pigFunc.getClass();
+    String key = className;
+    if (udfClass == JythonFunction.class) {
+      final String[] args = pigFunc.getCtorArgs();
+      assert args != null && args.length == 2;
+      final String fileName = args[0].substring(args[0].lastIndexOf("/") + 1,
+          args[0].lastIndexOf(".py"));
+      // key = [clas name]_[file name]_[function name]
+      key = udfClass.getName() + "_" + fileName + "_" + args[1];
+    }
+    pigRelContext.pigUdfs.put(key, pigFunc);
+  }
+
+  /**
+   * Replaces the relational algebra operator at the top of the stack with
+   * a new one.
+   *
+   * @param newRel the new relational algebra operator to replace
+   */
+  void replaceTop(RelNode newRel) {
+    final RelNode topRel = peek();
+    if (topRel instanceof SingleRel) {
+      String alias = reverseAliasMap.get(topRel);
+      if (alias != null) {
+        reverseAliasMap.remove(topRel);
+        reverseAliasMap.put(newRel, alias);
+        aliasMap.put(alias, newRel);
+      }
+      Operator pig = getPig(topRel);
+      if (pig != null) {
+        relPigMap.remove(topRel);
+        relPigMap.put(newRel, pig);
+        pigRelMap.put(pig, newRel);
+      }
+      build();
+      push(newRel);
+    }
+  }
+
+  /**
+   * Scans a table with its given schema and names.
+   *
+   * @param userSchema The schema of the table to scan
+   * @param tableNames The names of the table to scan
+   * @return This builder
+   */
+  public RelBuilder scan(RelOptTable userSchema, String... tableNames) {
+    // First, look up the database schema to find the table schema with the given names
+    final List<String> names = ImmutableList.copyOf(tableNames);
+    final RelOptTable systemSchema = relOptSchema.getTableForMember(names);
+
+    // Now we may end up with two different schemas.
+    if (systemSchema != null) {
+      if (userSchema != null && !compatibleType(userSchema.getRowType(),
+          systemSchema.getRowType())) {
+        // If both schemas are valid, they must be compatible
+        throw new IllegalArgumentException(
+            "Pig script schema does not match database schema for table " + names + ".\n"
+                + "\t Scrip schema: " + userSchema.getRowType().getFullTypeString() + "\n"
+                + "\t Database schema: " + systemSchema.getRowType().getFullTypeString());
+      }
+      // We choose to use systemSchema if it is valid
+      return scan(systemSchema);
+    } else if (userSchema != null) {
+      // If systemSchema is not valid, use userSchema if it is valid
+      return scan(userSchema);
+    } else {
+      // At least one of them needs to be valid
+      throw Static.RESOURCE.tableNotFound(String.join(".", names)).ex();
+    }
+  }
+
+  /**
+   * Scans a table with a given schema.
+   *
+   * @param tableSchema The table schema
+   * @return This builder
+   */
+  private RelBuilder scan(RelOptTable tableSchema) {
+    final RelNode scan = getScanFactory().createScan(cluster, tableSchema);
+    push(scan);
+    return this;
+  }
+
+  /**
+   * Makes a table scan operator for a given row type and names
+   *
+   * @param rowType Row type
+   * @param tableNames Table names
+   * @return This builder
+   */
+  public RelBuilder scan(RelDataType rowType, String... tableNames) {
+    return scan(rowType, Arrays.asList(tableNames));
+  }
+
+  /**
+   * Makes a table scan operator for a given row type and names
+   *
+   * @param rowType Row type
+   * @param tableNames Table names
+   * @return This builder
+   */
+  public RelBuilder scan(RelDataType rowType, List<String> tableNames) {
+    final RelOptTable relOptTable =
+        PigTable.createRelOptTable(getRelOptSchema(), rowType, tableNames);
+    return scan(relOptTable);
+  }
+
+  /**
+   * Projects a specific row type out of a relation algebra operator.
+   * For any field in output type, if there is no matching input field, we project
+   * null value of the corresponding output field type.
+   *
+   * <p>For example, given:
+   * <ul>
+   * <li>Input rel {@code A} with {@code A_type(X: int, Y: varchar)}
+   * <li>Output type {@code B_type(X: int, Y: varchar, Z: boolean, W: double)}
+   * </ul>
+   *
+   * <p>{@code project(A, B_type)} gives new relation
+   * {@code C(X: int, Y: varchar, null, null)}.
+   *
+   * @param input The relation algebra operator to be projected
+   * @param outputType The data type for the projected relation algebra operator
+   * @return The projected relation algebra operator
+   */
+  public RelNode project(RelNode input, RelDataType outputType) {
+    final RelDataType inputType = input.getRowType();
+    if (compatibleType(inputType, outputType)
+        && inputType.getFieldNames().equals(outputType.getFieldNames())) {
+      // Same data type, simply returns the input rel
+      return input;
+    }
+
+    // Now build the projection expressions on top of the input rel.
+    push(input);
+    project(projects(inputType, outputType), outputType.getFieldNames(), true);
+    return build();
+  }
+
+  /**
+   * Builds the projection expressions for a data type on top of an input data type.
+   * For any field in output type, if there is no matching input field, we build
+   * the literal null expression with the corresponding output field type.
+   *
+   * @param inputType The input data type
+   * @param outputType The output data type that defines the types of projection expressions
+   * @return List of projection expressions
+   */
+  private List<RexNode> projects(RelDataType inputType, RelDataType outputType) {
+    final List<RelDataTypeField> outputFields = outputType.getFieldList();
+    final List<RelDataTypeField> inputFields = inputType.getFieldList();
+    final List<RexNode> projectionExprs = new ArrayList<>();
+
+    for (RelDataTypeField outputField : outputFields) {
+      RelDataTypeField matchInputField = null;
+      // First find the matching input field
+      for (RelDataTypeField inputField : inputFields) {
+        if (inputField.getName().equals(outputField.getName())) {
+          // Matched if same name
+          matchInputField = inputField;
+          break;
+        }
+      }
+      if (matchInputField != null) {
+        RexNode fieldProject = field(matchInputField.getIndex());
+        if (matchInputField.getType().equals(outputField.getType())) {
+          // If found and on same type, just project the field
+          projectionExprs.add(fieldProject);
+        } else {
+          // Different types, CAST is required
+          projectionExprs.add(getRexBuilder().makeCast(outputField.getType(), fieldProject));
+        }
+      } else {
+        final RelDataType columnType = outputField.getType();
+        if (!columnType.isStruct() && columnType.getComponentType() == null) {
+          // If not, project the null Literal with the same basic type
+          projectionExprs.add(getRexBuilder().makeNullLiteral(outputField.getType()));
+        } else {
+          // If Record or Multiset just project a constant null
+          projectionExprs.add(literal(null));
+        }
+      }
+    }
+    return projectionExprs;
+  }
+
+  public AggCall aggregateCall(SqlAggFunction aggFunction, String alias, RexNode... operands) {
+    return aggregateCall(aggFunction, false, false, false, null,
+        ImmutableList.of(), alias, ImmutableList.copyOf(operands));
+  }
+
+  /**
+   * Cogroups relations on top of the stack. The number of relations and the
+   * group key are specified in groupKeys
+   *
+   * @param groupKeys Lists of group keys of relations to be cogrouped.
+   * @return This builder
+   */
+  public RelBuilder cogroup(Iterable<? extends GroupKey> groupKeys) {
+    @SuppressWarnings("unchecked") final List<GroupKeyImpl> groupKeyList =
+        ImmutableList.copyOf((Iterable) groupKeys);
+    final int groupCount = groupKeyList.get(0).nodes.size();
+
+    // Pull out all relations needed for the group
+    final int numRels = groupKeyList.size();
+    List<RelNode> cogroupRels = new ArrayList<>();
+    for (int i = 0; i < numRels; i++) {
+      cogroupRels.add(0, build());
+    }
+
+    // Group and join relations from left to right
+    for (int i = 0; i < numRels; i++) {
+      // 1. Group each rel first by using COLLECT operator
+      push(cogroupRels.get(i));
+      // Create a ROW to pass to COLLECT.
+      final RexNode row = field(groupCount);
+      aggregate(groupKeyList.get(i),
+          aggregateCall(SqlStdOperatorTable.COLLECT, row).as(getAlias()));
+      if (i == 0) {
+        continue;
+      }
+
+      // 2. Then join with the previous group relation
+      List<RexNode> predicates = new ArrayList<>();
+      for (int key : Util.range(groupCount)) {
+        predicates.add(equals(field(2, 0, key), field(2, 1, key)));
+      }
+      join(JoinRelType.FULL, and(predicates));
+
+      // 3. Project group keys from one of these two joined relations, whichever
+      // is not null and the remaining payload columns
+      RexNode[] projectFields = new RexNode[groupCount + i + 1];
+      String[] fieldNames = new String [groupCount + i + 1];
+      LogicalJoin join = (LogicalJoin) peek();
+      for (int j = 0; j < groupCount; j++) {
+        RexNode[] caseOperands = new RexNode[3];
+        // WHEN groupKey[i] of leftRel IS NOT NULL
+        caseOperands[0] = call(SqlStdOperatorTable.IS_NOT_NULL, field(j));
+        // THEN choose groupKey[i] of leftRel
+        caseOperands[1] = field(j);
+        // ELSE choose groupKey[i] of rightRel
+        caseOperands[2] = field(j + groupCount + i);
+        projectFields[j] = call(SqlStdOperatorTable.CASE, caseOperands);
+        String leftName = join.getLeft().getRowType().getFieldNames().get(j);
+        String rightName = join.getRight().getRowType().getFieldNames().get(j);
+        fieldNames[j] = leftName.equals(rightName) ? leftName : rightName;
+      }
+
+      // Project the group fields of the leftRel
+      for (int j = groupCount; j < groupCount + i + 1; j++) {
+        projectFields[j] = field(j);
+        fieldNames[j] = peek().getRowType().getFieldNames().get(j);
+      }
+
+      // Project the group fields of the rightRel
+      projectFields[groupCount + i] = field(2 * groupCount + i);
+      fieldNames[groupCount + i] = peek().getRowType().getFieldNames().get(2 * groupCount + i);
+
+      project(ImmutableList.copyOf(projectFields), ImmutableList.copyOf(fieldNames));
+    }
+    return this;
+  }
+
+  /**
+   * Flattens the top relation on provided columns.
+   *
+   * @param flattenCols Indexes of columns to be flattened. These columns should have multiset type.
+   * @return This builder
+   */
+  public RelBuilder multiSetFlatten(List<Integer> flattenCols, List<String> flattenOutputAliases) {
+    final int colCount = peek().getRowType().getFieldCount();
+    final List<RelDataTypeField> inputFields = peek().getRowType().getFieldList();
+    final CorrelationId correlId = nextCorrelId();
+
+    // First build a correlated expression from the input row
+    final RexNode cor = correl(inputFields, correlId);
+
+    // Then project out flatten columns from the correlated expression
+    List<RexNode> flattenNodes = new ArrayList<>();
+    for (int i : flattenCols) {
+      assert inputFields.get(i).getType().getFamily() instanceof MultisetSqlType;
+      flattenNodes.add(getRexBuilder().makeFieldAccess(cor, i));
+    }
+    push(LogicalValues.createOneRow(getCluster()));
+    project(flattenNodes);
+
+    // Now do flatten on input rel that contains only multiset columns
+    multiSetFlatten();
+
+    // And rejoin the result -> output: original columns + new flattened columns
+    join(JoinRelType.INNER, literal(true), ImmutableSet.of(correlId));
+
+    // Finally project out only required columns. The original multiset columns are replaced
+    // by the new corresponding flattened columns
+    final List<RexNode> finnalCols = new ArrayList<>();
+    final List<String> finnalColNames = new ArrayList<>();
+    int flattenCount = 0;
+    for (int i = 0; i < colCount; i++) {
+      if (flattenCols.indexOf(i) >= 0) {
+        // The original multiset columns to be flattened, select new flattened columns instead
+        RelDataType componentType = inputFields.get(i).getType().getComponentType();
+        final int numSubFields = componentType.isStruct() ? componentType.getFieldCount() : 1;
+        for (int j = 0; j < numSubFields; j++) {
+          finnalCols.add(field(colCount + flattenCount));
+          finnalColNames.add(flattenOutputAliases.get(flattenCount));
+          flattenCount++;
+        }
+      } else {
+        // Otherwise, just copy the original column
+        finnalCols.add(field(i));
+        finnalColNames.add(inputFields.get(i).getName());
+      }
+    }
+    project(finnalCols, finnalColNames);
+    return this;
+  }
+
+  /**
+   * Flattens the top relation will all multiset columns. Call this method only if
+   * the top relation contains multiset columns only.
+   *
+   * @return This builder.
+   */
+  public RelBuilder multiSetFlatten() {
+    // [CALCITE-3193] Add RelBuilder.uncollect method, and interface
+    // UncollectFactory, to instantiate Uncollect
+    Uncollect uncollect = new Uncollect(cluster,
+        cluster.traitSetOf(Convention.NONE), build(), false);
+    push(uncollect);
+    return this;
+  }
+
+  /**
+   * Makes the correlated expression from rel input fields and correlation id.
+   *
+   * @param inputFields Rel input field list
+   * @param correlId Correlation id
+   *
+   * @return This builder
+   */
+  public RexNode correl(List<RelDataTypeField> inputFields,
+      CorrelationId correlId) {
+    final RelDataTypeFactory.Builder fieldBuilder =
+        PigTypes.TYPE_FACTORY.builder();
+    for (RelDataTypeField field : inputFields) {
+      fieldBuilder.add(field.getName(), field.getType());
+    }
+    return getRexBuilder().makeCorrel(fieldBuilder.uniquify().build(), correlId);
+  }
+
+  /**
+   * Collects all rows of the top rel into a single multiset value.
+   *
+   * @return This builder
+   */
+  public RelBuilder collect() {
+    final  RelNode inputRel = peek();
+
+    // First project out a combined column which is a of all other columns
+    final RexNode row = getRexBuilder().makeCall(inputRel.getRowType(),
+        SqlStdOperatorTable.ROW, fields());
+    project(ImmutableList.of(literal("all"), row));
+
+    // Update the alias map for the new projected rel.
+    updateAlias(getPig(inputRel), getAlias(inputRel), false);
+
+    // Build a single group for all rows
+    cogroup(ImmutableList.of(groupKey(ImmutableList.<RexNode>of(field(0)))));
+
+    // Finally project out the final multiset value
+    project(field(1));
+
+    return this;
+  }
+
+  public RexNode dot(RexNode node, Object field) {
+    if (field instanceof Integer) {
+      int fieldIndex = (Integer) field;
+      final RelDataType type = node.getType();
+      if (type instanceof DynamicTupleRecordType) {
+        ((DynamicTupleRecordType) type).resize(fieldIndex + 1);
+      }
+      return super.dot(node, fieldIndex);
+    }
+    return super.dot(node, (String) field);
+  }
+
+  public RexNode literal(Object value, RelDataType type) {
+    if (value instanceof Tuple) {
+      assert type.isStruct();
+      return getRexBuilder().makeLiteral(((Tuple) value).getAll(), type, false);
+    }
+
+    if (value instanceof DataBag) {
+      assert type.getComponentType() != null && type.getComponentType().isStruct();
+      final List<List<Object>> multisetObj = new ArrayList<>();
+      for (Tuple tuple : (DataBag) value) {
+        multisetObj.add(tuple.getAll());
+      }
+      return getRexBuilder().makeLiteral(multisetObj, type, false);
+    }
+    return getRexBuilder().makeLiteral(value, type, false);
+  }
+
+  /**
+   * Save the store alias with the corresponding relational algebra node
+   *
+   * @param storeAlias alias of the Pig store operator
+   * @return This builder
+   */
+  RelBuilder store(String storeAlias) {
+    storeMap.put(storeAlias, build());
+    return this;
+  }
+
+  /**
+   * Gets all relational plans corresponding to Pig Store operators.
+   *
+   */
+  public List<RelNode> getRelsForStores() {
+    if (storeMap.isEmpty()) {
+      return null;
+    }
+    return ImmutableList.copyOf(storeMap.values());
+  }
+
+  public ImmutableList<RexNode> getFields(int inputCount, int inputOrdinal, int fieldOrdinal) {
+    if (fieldOrdinal == -1) {
+      return fields(inputCount, inputOrdinal);
+    }
+    return ImmutableList.of(field(inputCount, inputOrdinal, fieldOrdinal));
+  }
+
+  /**
+   * Checks if two relational data types are compatible.
+   *
+   * @param t1 first type
+   * @param t2 second type
+   * @return true if t1 is compatible with t2
+   */
+  public static boolean compatibleType(RelDataType t1, RelDataType t2) {
+    if (t1.isStruct() || t2.isStruct()) {
+      if (!t1.isStruct() || !t2.isStruct()) {
+        return false;
+      }
+      if (t1.getFieldCount() != t2.getFieldCount()) {
+        return false;
+      }
+      List<RelDataTypeField> fields1 = t1.getFieldList();
+      List<RelDataTypeField> fields2 = t2.getFieldList();
+      for (int i = 0; i < fields1.size(); ++i) {
+        if (!compatibleType(
+            fields1.get(i).getType(),
+            fields2.get(i).getType())) {
+          return false;
+        }
+      }
+      return true;
+    }
+    RelDataType comp1 = t1.getComponentType();
+    RelDataType comp2 = t2.getComponentType();
+    if ((comp1 != null) || (comp2 != null)) {
+      if ((comp1 == null) || (comp2 == null)) {
+        return false;
+      }
+      if (!compatibleType(comp1, comp2)) {
+        return false;
+      }
+    }
+    return t1.getSqlTypeName().getFamily() == t2.getSqlTypeName().getFamily();
+  }
+
+  /**
+   * Context constructed during Pig-to-{@link RelNode} translation process.
+   */
+  public class PigRelTranslationContext {
+    final Map<String, FuncSpec> pigUdfs = new HashMap<>();
+  }
+}
+
+// End PigRelBuilder.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigRelExVisitor.java b/piglet/src/main/java/org/apache/calcite/piglet/PigRelExVisitor.java
new file mode 100644
index 0000000..7c2f488
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigRelExVisitor.java
@@ -0,0 +1,452 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexSubQuery;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.schema.impl.ScalarFunctionImpl;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.type.MultisetSqlType;
+import org.apache.calcite.sql.validate.SqlUserDefinedFunction;
+import org.apache.calcite.util.NlsString;
+
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.newplan.Operator;
+import org.apache.pig.newplan.OperatorPlan;
+import org.apache.pig.newplan.PlanWalker;
+import org.apache.pig.newplan.logical.expression.AddExpression;
+import org.apache.pig.newplan.logical.expression.AndExpression;
+import org.apache.pig.newplan.logical.expression.BinCondExpression;
+import org.apache.pig.newplan.logical.expression.CastExpression;
+import org.apache.pig.newplan.logical.expression.ConstantExpression;
+import org.apache.pig.newplan.logical.expression.DereferenceExpression;
+import org.apache.pig.newplan.logical.expression.DivideExpression;
+import org.apache.pig.newplan.logical.expression.EqualExpression;
+import org.apache.pig.newplan.logical.expression.GreaterThanEqualExpression;
+import org.apache.pig.newplan.logical.expression.GreaterThanExpression;
+import org.apache.pig.newplan.logical.expression.IsNullExpression;
+import org.apache.pig.newplan.logical.expression.LessThanEqualExpression;
+import org.apache.pig.newplan.logical.expression.LessThanExpression;
+import org.apache.pig.newplan.logical.expression.LogicalExpressionPlan;
+import org.apache.pig.newplan.logical.expression.LogicalExpressionVisitor;
+import org.apache.pig.newplan.logical.expression.MapLookupExpression;
+import org.apache.pig.newplan.logical.expression.ModExpression;
+import org.apache.pig.newplan.logical.expression.MultiplyExpression;
+import org.apache.pig.newplan.logical.expression.NegativeExpression;
+import org.apache.pig.newplan.logical.expression.NotEqualExpression;
+import org.apache.pig.newplan.logical.expression.NotExpression;
+import org.apache.pig.newplan.logical.expression.OrExpression;
+import org.apache.pig.newplan.logical.expression.ProjectExpression;
+import org.apache.pig.newplan.logical.expression.RegexExpression;
+import org.apache.pig.newplan.logical.expression.ScalarExpression;
+import org.apache.pig.newplan.logical.expression.SubtractExpression;
+import org.apache.pig.newplan.logical.expression.UserFuncExpression;
+import org.apache.pig.newplan.logical.relational.LOInnerLoad;
+import org.apache.pig.newplan.logical.relational.LogicalRelationalOperator;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+
+import java.math.BigDecimal;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.List;
+
+/**
+ * Visits pig expression plans and converts them into corresponding RexNodes.
+ */
+class PigRelExVisitor extends LogicalExpressionVisitor {
+  /** Stack used during post order walking process when processing a Pig
+   * expression plan */
+  private final Deque<RexNode> stack = new ArrayDeque<>();
+
+  /** The relational algebra builder customized for Pig. */
+  private final PigRelBuilder builder;
+
+  // inputCount and inputOrdinal are used to select which relation in the builder
+  // stack to build the projection
+
+  /** Number of inputs */
+  private final int inputCount;
+
+  /** Input ordinal */
+  private final int inputOrdinal;
+
+  /**
+   * Creates a PigRelExVisitor.
+   *
+   * @param expressionPlan Pig expression plan
+   * @param walker The walker over Pig expression plan.
+   * @param builder Relational algebra builder
+   * @param inputCount Number of inputs
+   * @param inputOrdinal Input ordinal
+   * @throws FrontendException Exception during processing Pig operators
+   */
+  private PigRelExVisitor(OperatorPlan expressionPlan, PlanWalker walker,
+      PigRelBuilder builder, int inputCount, int inputOrdinal)
+      throws FrontendException {
+    super(expressionPlan, walker);
+    this.builder = builder;
+    this.inputCount = inputCount;
+    this.inputOrdinal = inputOrdinal;
+  }
+
+  /**
+   * Translates the given pig expression plan into a list of relational algebra
+   * expressions.
+   *
+   * @return Relational algebra expressions
+   * @throws FrontendException Exception during processing Pig operators
+   */
+  private List<RexNode> translate() throws FrontendException {
+    currentWalker.walk(this);
+    return new ArrayList<>(stack);
+  }
+
+  /**
+   * Translates a Pig expression plans into relational algebra expressions.
+   *
+   * @param builder Relational algebra builder
+   * @param pigEx Pig expression plan
+   * @param inputCount Number of inputs
+   * @param inputOrdinal Input ordinal
+   * @return Relational algebra expressions
+   * @throws FrontendException Exception during processing Pig operators
+   */
+  static RexNode translatePigEx(PigRelBuilder builder, LogicalExpressionPlan pigEx,
+      int inputCount, int inputOrdinal) throws FrontendException {
+    final PigRelExWalker walker = new PigRelExWalker(pigEx);
+    final PigRelExVisitor exVisitor =
+        new PigRelExVisitor(pigEx, walker, builder, inputCount, inputOrdinal);
+    final List<RexNode> result = exVisitor.translate();
+    assert result.size() == 1;
+    return result.get(0);
+  }
+
+  /**
+   * Translates a Pig expression plans into relational algebra expressions.
+   *
+   * @param builder Relational algebra builder
+   * @param pigEx Pig expression plan
+   * @return Relational algebra expressions
+   * @throws FrontendException Exception during processing Pig operators
+   */
+  static RexNode translatePigEx(PigRelBuilder builder, LogicalExpressionPlan pigEx)
+      throws FrontendException {
+    return translatePigEx(builder, pigEx, 1, 0);
+  }
+
+  /**
+   * Builds operands for an operator from expressions on the top of the visitor stack.
+   *
+   * @param numOps number of operands
+   * @return List of operand expressions
+   */
+  private ImmutableList<RexNode> buildOperands(int numOps) {
+    List<RexNode> opList = new ArrayList<>();
+    for (int i = 0; i < numOps; i++) {
+      opList.add(0, stack.pop());
+    }
+    return ImmutableList.copyOf(opList);
+  }
+
+  /**
+   * Builds operands for a binary operator.
+   *
+   * @return List of two operand expressions
+   */
+  private ImmutableList<RexNode> buildBinaryOperands() {
+    return buildOperands(2);
+  }
+
+  @Override public void visit(ConstantExpression op) throws FrontendException {
+    RelDataType constType = PigTypes.convertSchemaField(op.getFieldSchema(), false);
+    stack.push(builder.literal(op.getValue(), constType));
+  }
+
+  @Override public void visit(ProjectExpression op) throws FrontendException {
+    String fullAlias = op.getFieldSchema().alias;
+    if (fullAlias != null) {
+      RexNode inputRef;
+      try {
+        // First try the exact name match for the alias
+        inputRef = builder.field(inputCount, inputOrdinal, fullAlias);
+      } catch (IllegalArgumentException e) {
+        // If not found, look for the field name match only.
+        // Note that the full alias may have the format of 'tableName::fieldName'
+        final List<String> fieldNames =
+            builder.peek(inputCount, inputOrdinal).getRowType().getFieldNames();
+        int index = -1;
+        for (int i = 0; i < fieldNames.size(); i++) {
+          if (fullAlias.endsWith(fieldNames.get(i))) {
+            index = i;
+            break;
+          }
+        }
+        if (index < 0) {
+          String shortAlias = fullAlias;
+          if (fullAlias.contains("::")) {
+            String[] tokens = fullAlias.split("::");
+            shortAlias = tokens[tokens.length - 1];
+          }
+          for (int i = 0; i < fieldNames.size(); i++) {
+            if (fieldNames.get(i).equals(shortAlias)) {
+              index = i;
+              break;
+            }
+          }
+          if (index < 0) {
+            throw new IllegalArgumentException(
+                "field [" + fullAlias + "] not found; input fields are: " + fieldNames);
+          }
+        }
+        inputRef = builder.field(inputCount, inputOrdinal, index);
+      }
+      stack.push(inputRef);
+    } else {
+      // Alias not provided, get data from input of LOGenerate
+      assert op.getInputNum() >= 0;
+      final Operator pigRelOp = op.getAttachedRelationalOp();
+      final LogicalRelationalOperator childOp = (LogicalRelationalOperator)
+          pigRelOp.getPlan().getPredecessors(pigRelOp).get(op.getInputNum());
+      if (builder.checkMap(childOp)) {
+        // Inner plan that has been processed before (nested foreach or flatten)
+        builder.push(builder.getRel(childOp));
+        final List<RexNode> fields = builder.getFields(inputCount, inputOrdinal, op.getColNum());
+
+        for (int i = fields.size() - 1; i >= 0; i--) {
+          stack.push(fields.get(i));
+        }
+
+        builder.build();
+      } else {
+        // Simple inner load
+        assert childOp instanceof LOInnerLoad;
+        visit(((LOInnerLoad) childOp).getProjection());
+      }
+    }
+  }
+
+  @Override public void visit(NegativeExpression op) throws FrontendException {
+    final RexNode operand = stack.pop();
+    if (operand instanceof RexLiteral) {
+      final Comparable value = ((RexLiteral) operand).getValue();
+      assert value instanceof BigDecimal;
+      stack.push(builder.literal(((BigDecimal) value).negate()));
+    } else {
+      stack.push(builder.call(SqlStdOperatorTable.UNARY_MINUS, operand));
+    }
+  }
+
+  @Override public void visit(EqualExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.EQUALS, buildBinaryOperands()));
+  }
+
+  @Override public void visit(NotEqualExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.NOT_EQUALS, buildBinaryOperands()));
+  }
+
+  @Override public void visit(LessThanExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.LESS_THAN, buildBinaryOperands()));
+  }
+
+  @Override public void visit(LessThanEqualExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.LESS_THAN_OR_EQUAL, buildBinaryOperands()));
+  }
+
+  @Override public void visit(GreaterThanExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.GREATER_THAN, buildBinaryOperands()));
+  }
+
+  @Override public void visit(GreaterThanEqualExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.GREATER_THAN_OR_EQUAL, buildBinaryOperands()));
+  }
+
+  @Override public void visit(RegexExpression op) throws FrontendException {
+    RexNode operand1 = replacePatternIfPossible(stack.pop());
+    RexNode operand2 = replacePatternIfPossible(stack.pop());
+    stack.push(builder.call(SqlStdOperatorTable.LIKE, ImmutableList.of(operand2, operand1)));
+  }
+
+  /**
+   * Replaces Pig regular expressions with SQL regular expressions in a string.
+   *
+   * @param rexNode The string literal
+   * @return New string literal with Pig regular expressions replaced by SQL regular expressions
+   */
+  private RexNode replacePatternIfPossible(RexNode rexNode) {
+    // Until
+    //   [CALCITE-3194] Convert Pig string patterns into SQL string patterns
+    // is fixed, return the pattern unchanged.
+    return rexNode;
+  }
+
+  @Override public void visit(IsNullExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.IS_NULL, stack.pop()));
+  }
+
+  @Override public void visit(NotExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.NOT, stack.pop()));
+  }
+
+  @Override public void visit(AndExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.AND, buildBinaryOperands()));
+  }
+
+  @Override public void visit(OrExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.OR, buildBinaryOperands()));
+  }
+
+  @Override public void visit(AddExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.PLUS, buildBinaryOperands()));
+  }
+
+  @Override public void visit(SubtractExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.MINUS, buildBinaryOperands()));
+  }
+
+  @Override public void visit(MultiplyExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.MULTIPLY, buildBinaryOperands()));
+  }
+
+  @Override public void visit(ModExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.MOD, buildBinaryOperands()));
+  }
+
+  @Override public void visit(DivideExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.DIVIDE, buildBinaryOperands()));
+  }
+
+  @Override public void visit(BinCondExpression op) throws FrontendException {
+    stack.push(builder.call(SqlStdOperatorTable.CASE, buildOperands(3)));
+  }
+
+  @Override public void visit(UserFuncExpression op) throws FrontendException {
+    if (op.getFuncSpec().getClassName().equals("org.apache.pig.impl.builtin.IdentityColumn")) {
+      // Skip this Pig dummy function
+      return;
+    }
+    final int numAgrs = optSize(op.getPlan().getSuccessors(op))
+        + optSize(op.getPlan().getSoftLinkSuccessors(op));
+
+    final RelDataType returnType = PigTypes.convertSchemaField(op.getFieldSchema());
+    stack.push(
+        PigRelUdfConverter.convertPigFunction(
+            builder, op.getFuncSpec(), buildOperands(numAgrs), returnType));
+
+    String className = op.getFuncSpec().getClassName();
+    SqlOperator sqlOp = ((RexCall) stack.peek()).getOperator();
+    if (sqlOp instanceof SqlUserDefinedFunction) {
+      ScalarFunctionImpl sqlFunc =
+          (ScalarFunctionImpl) ((SqlUserDefinedFunction) sqlOp).getFunction();
+      // "Exec" method can be implemented from the parent class.
+      className = sqlFunc.method.getDeclaringClass().getName();
+    }
+    builder.registerPigUDF(className, op.getFuncSpec());
+  }
+
+  private int optSize(List<Operator> list) {
+    return list != null ? list.size() : 0;
+  }
+
+  @Override public void visit(DereferenceExpression op) throws FrontendException {
+    final RexNode parentField = stack.pop();
+    List<Integer> cols = op.getBagColumns();
+    assert cols != null && cols.size() > 0;
+
+    if (parentField.getType() instanceof MultisetSqlType) {
+      // Calcite does not support projection on Multiset type. We build
+      // our own multiset projection in @PigRelSqlUDFs and use it here
+      final RexNode[] rexCols = new RexNode[cols.size() + 1];
+      // First parent field
+      rexCols[0] = parentField;
+      // The sub-fields to be projected from parent field
+      for (int i = 0; i < cols.size(); i++) {
+        rexCols[i + 1] = builder.literal(cols.get(i));
+      }
+      stack.push(builder.call(PigRelSqlUdfs.MULTISET_PROJECTION, rexCols));
+    } else {
+      if (cols.size() == 1) {
+        // Single field projection
+        stack.push(builder.dot(parentField, cols.get(0)));
+      } else {
+        // Multiple field projection, build a sub struct from the parent struct
+        List<RexNode> relFields = new ArrayList<>();
+        for (Object col : cols) {
+          relFields.add(builder.dot(parentField, col));
+        }
+
+        final RelDataType newRelType = RexUtil.createStructType(
+            PigTypes.TYPE_FACTORY,
+            relFields);
+        stack.push(
+            builder.getRexBuilder().makeCall(newRelType, SqlStdOperatorTable.ROW, relFields));
+      }
+    }
+  }
+
+  @Override public void visit(CastExpression op) throws FrontendException {
+    final RelDataType relType = PigTypes.convertSchemaField(op.getFieldSchema());
+    final RexNode castOperand = stack.pop();
+    if (castOperand instanceof RexLiteral
+        && ((RexLiteral) castOperand).getValue() == null) {
+      if (!relType.isStruct() && relType.getComponentType() == null) {
+        stack.push(builder.getRexBuilder().makeNullLiteral(relType));
+      } else {
+        stack.push(castOperand);
+      }
+    } else {
+      stack.push(builder.getRexBuilder().makeCast(relType, castOperand));
+    }
+  }
+
+  @Override public void visit(MapLookupExpression op) throws FrontendException {
+    final RexNode relKey = builder.literal(op.getLookupKey());
+    final RexNode relMap = stack.pop();
+    stack.push(builder.call(SqlStdOperatorTable.ITEM, relMap, relKey));
+  }
+
+  @Override public void visit(ScalarExpression op) throws FrontendException {
+    // First operand is the path to the materialized view
+    RexNode operand1 = stack.pop();
+    assert operand1 instanceof RexLiteral
+               && ((RexLiteral) operand1).getValue() instanceof NlsString;
+
+    // Second operand is the projection index
+    RexNode operand2 = stack.pop();
+    assert operand2 instanceof RexLiteral
+               && ((RexLiteral) operand2).getValue() instanceof BigDecimal;
+    final int index = ((BigDecimal) ((RexLiteral) operand2).getValue()).intValue();
+
+    RelNode referencedRel = builder.getRel(
+        ((LogicalRelationalOperator) op.getImplicitReferencedOperator()).getAlias());
+    builder.push(referencedRel);
+    List<RexNode> projectCol = Lists.newArrayList((RexNode) builder.field(index));
+    builder.project(projectCol);
+
+    stack.push(RexSubQuery.scalar(builder.build()));
+  }
+}
+
+// End PigRelExVisitor.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigRelExWalker.java b/piglet/src/main/java/org/apache/calcite/piglet/PigRelExWalker.java
new file mode 100644
index 0000000..d0bb13b
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigRelExWalker.java
@@ -0,0 +1,84 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.util.Utils;
+import org.apache.pig.newplan.Operator;
+import org.apache.pig.newplan.OperatorPlan;
+import org.apache.pig.newplan.PlanVisitor;
+import org.apache.pig.newplan.PlanWalker;
+import org.apache.pig.newplan.logical.expression.LogicalExpressionPlan;
+
+import java.util.Collection;
+
+/**
+ * Post-order walker for Pig expression plans. Walk the plan from
+ * source to sinks.
+ */
+class PigRelExWalker extends PlanWalker {
+  PigRelExWalker(OperatorPlan plan) {
+    super(plan);
+  }
+
+  @Override public void walk(PlanVisitor planVisitor) throws FrontendException {
+    if (!(planVisitor instanceof PigRelExVisitor)) {
+      throw new FrontendException("Expected PigRelOpVisitor", 2223);
+    }
+    if (!(getPlan() instanceof LogicalExpressionPlan)) {
+      throw new FrontendException("Expected LogicalExpressionPlan", 2223);
+    }
+
+    final PigRelExVisitor pigRelVistor = (PigRelExVisitor) planVisitor;
+    final LogicalExpressionPlan plan = (LogicalExpressionPlan) getPlan();
+
+    if (plan.getSources().isEmpty()) {
+      return;
+    }
+
+    if (plan.getSources().size() > 1) {
+      throw new FrontendException(
+          "Found LogicalExpressionPlan with more than one root.  Unexpected.", 2224);
+    }
+
+    postOrderWalk(plan.getSources().get(0), pigRelVistor);
+  }
+
+  /**
+   * Does post-order walk on the Pig expression plan from source to sinks.
+   *
+   * @param root The root expression operator
+   * @param visitor The visitor of each Pig expression node.
+   * @throws FrontendException Exception during processing Pig operator
+   */
+  private void postOrderWalk(Operator root, PlanVisitor visitor) throws FrontendException {
+    final Collection<Operator> nexts =
+        Utils.mergeCollection(plan.getSuccessors(root), plan.getSuccessors(root));
+    if (nexts != null) {
+      for (Operator op : nexts) {
+        postOrderWalk(op, visitor);
+      }
+    }
+    root.accept(visitor);
+  }
+
+  @Override public PlanWalker spawnChildWalker(OperatorPlan operatorPlan)  {
+    return new PigRelExWalker(operatorPlan);
+  }
+}
+
+// End PigRelExWalker.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigRelOpInnerVisitor.java b/piglet/src/main/java/org/apache/calcite/piglet/PigRelOpInnerVisitor.java
new file mode 100644
index 0000000..18cfa5d
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigRelOpInnerVisitor.java
@@ -0,0 +1,251 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.CorrelationId;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.logical.LogicalValues;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.type.MultisetSqlType;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
+import org.apache.calcite.util.Litmus;
+
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.newplan.Operator;
+import org.apache.pig.newplan.OperatorPlan;
+import org.apache.pig.newplan.PlanWalker;
+import org.apache.pig.newplan.logical.expression.LogicalExpressionPlan;
+import org.apache.pig.newplan.logical.relational.LOGenerate;
+import org.apache.pig.newplan.logical.relational.LOInnerLoad;
+import org.apache.pig.newplan.logical.relational.LogicalRelationalOperator;
+import org.apache.pig.newplan.logical.relational.LogicalSchema;
+
+import com.google.common.collect.ImmutableSet;
+
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.List;
+
+/**
+ * Visits Pig logical operators of Pig inner logical plans
+ * (in {@link org.apache.pig.newplan.logical.relational.LOForEach})
+ * and converts them into corresponding relational algebra plans.
+ */
+class PigRelOpInnerVisitor extends PigRelOpVisitor {
+  // The relational algebra operator corresponding to the input of LOForeach operator.
+  private final RelNode inputRel;
+
+  // Stack contains correlation id required for processing inner plan.
+  private final Deque<CorrelationId> corStack = new ArrayDeque<>();
+
+  /**
+   *
+   * @param plan Pig inner logical plan
+   * @param walker The walker over Pig logical plan
+   * @param builder Relational algebra builder
+   * @throws FrontendException Exception during processing Pig operators
+   */
+  PigRelOpInnerVisitor(OperatorPlan plan, PlanWalker walker, PigRelBuilder builder)
+      throws FrontendException {
+    super(plan, walker, builder);
+    this.inputRel = builder.peek();
+  }
+
+  @Override public void visit(LOGenerate gen) throws FrontendException {
+    // @LOGenerate is the root of the inner plan, meaning if we reach here, all operators
+    // except this node have been converted into relational algebra nodes stored in the builder.
+    // Here we do the final step of generating the relational algebra output node for the
+    // @LOForEach operator.
+
+    // First rejoin all results of columns processed in nested block, if any, using correlation ids
+    // we remembered before (in visit(LOForeach)).
+    makeCorrelates();
+
+    // The project all expressions in the generate command, but ignore flattened columns now
+    final List<Integer> multisetFlattens = new ArrayList<>();
+    final List<String> flattenOutputAliases = new ArrayList<>();
+    doGenerateWithoutMultisetFlatten(gen, multisetFlattens, flattenOutputAliases);
+    if (multisetFlattens.size() > 0) {
+      builder.multiSetFlatten(multisetFlattens, flattenOutputAliases);
+    }
+  }
+
+  /**
+   * Rejoins all multiset (bag) columns that have been processed in the nested
+   * foreach block.
+   *
+   * @throws FrontendException Exception during processing Pig operators
+   */
+  private void makeCorrelates() throws FrontendException {
+    List<CorrelationId> corIds = new ArrayList<>();
+    List<RelNode> rightRels =  new ArrayList<>();
+
+    // First pull out all correlation ids we remembered from the InnerLoads
+    while (!corStack.isEmpty()) {
+      final CorrelationId corId = corStack.pop();
+      corIds.add(0, corId);
+
+      final List<RelNode> corRels = new ArrayList<>(); // All output rels from same inner load
+      while (!RelOptUtil.notContainsCorrelation(builder.peek(), corId, Litmus.IGNORE)) {
+        corRels.add(0, builder.build());
+      }
+
+      assert corRels.size() > 0;
+      builder.push(corRels.get(0));
+      builder.collect();
+      // Now collapse these rels to a single multiset row and join them together
+      for (int i = 1; i < corRels.size(); i++) {
+        builder.push(corRels.get(i));
+        builder.collect();
+        builder.join(JoinRelType.INNER, builder.literal(true));
+      }
+
+      rightRels.add(0, builder.build());
+    }
+
+    // The do correlate join
+    for (int i = 0; i < corIds.size(); i++) {
+      builder.push(rightRels.get(i));
+      builder.join(JoinRelType.INNER, builder.literal(true), ImmutableSet.of(corIds.get(i)));
+    }
+  }
+
+  /**
+   * Projects all expressions in LOGenerate output expressions, but not consider flatten
+   * multiset columns yet.
+   *
+   * @param gen Pig logical generate operator
+   * @throws FrontendException Exception during processing Pig operators
+   */
+  private void doGenerateWithoutMultisetFlatten(LOGenerate gen, List<Integer> multisetFlattens,
+      List<String> flattenOutputAliases) throws FrontendException {
+    final List<LogicalExpressionPlan> pigProjections = gen.getOutputPlans();
+    final List<RexNode> innerCols = new ArrayList<>(); // For projection expressions
+    final List<String> fieldAlias = new ArrayList<>(); // For projection names/alias
+
+    if (gen.getOutputPlanSchemas() == null) {
+      throw new IllegalArgumentException(
+          "Generate statement at line " + gen.getLocation().line() + " produces empty schema");
+    }
+
+    for (int i = 0; i < pigProjections.size(); i++) {
+      final LogicalSchema outputFieldSchema = gen.getOutputPlanSchemas().get(i);
+      RexNode rexNode = PigRelExVisitor.translatePigEx(builder, pigProjections.get(i));
+      RelDataType dataType = rexNode.getType();
+      // If project field in null constant, dataType will by NULL type, need to check the original
+      // type of Pig Schema
+      if (dataType.getSqlTypeName() == SqlTypeName.NULL) {
+        dataType = PigTypes.convertSchema(outputFieldSchema, true);
+      }
+
+      if (outputFieldSchema.size() == 1 && !gen.getFlattenFlags()[i]) {
+        final RelDataType scriptType = PigTypes.convertSchemaField(
+            outputFieldSchema.getField(0));
+        if (dataType.getSqlTypeName() == SqlTypeName.ANY
+                || !SqlTypeUtil.isComparable(dataType, scriptType)) {
+          // Script schema is different from project expression schema, need to do type cast
+          rexNode = builder.getRexBuilder().makeCast(scriptType, rexNode);
+        }
+      }
+
+      if (gen.getFlattenFlags()[i] && dataType.isStruct()
+              && (dataType.getFieldCount() > 0 || dataType instanceof DynamicTupleRecordType)) {
+        if (dataType instanceof DynamicTupleRecordType) {
+          ((DynamicTupleRecordType) dataType).resize(outputFieldSchema.size());
+        }
+        for (int j = 0; j < dataType.getFieldCount(); j++) {
+          innerCols.add(builder.dot(rexNode, j));
+          fieldAlias.add(outputFieldSchema.getField(j).alias);
+        }
+      } else {
+        innerCols.add(rexNode);
+        String alias = null;
+        if (outputFieldSchema.size() == 1) {
+          // If simple type, take user alias if available
+          alias = outputFieldSchema.getField(0).alias;
+        }
+        fieldAlias.add(alias);
+        if (gen.getFlattenFlags()[i] && dataType.getFamily() instanceof MultisetSqlType) {
+          multisetFlattens.add(innerCols.size() - 1);
+          for (LogicalSchema.LogicalFieldSchema field : outputFieldSchema.getFields()) {
+            String colAlias = field.alias;
+            if (colAlias.contains("::")) {
+              String[] tokens  = colAlias.split("::");
+              colAlias = tokens[tokens.length - 1];
+            }
+            flattenOutputAliases.add(colAlias);
+          }
+        }
+      }
+    }
+    builder.project(innerCols, fieldAlias, true);
+  }
+
+  @Override public void visit(LOInnerLoad load) throws FrontendException {
+    // Inner loads are the first operator the post order walker (@PigRelOpWalker) visits first
+    // We first look at the plan structure to see if the inner load is for a simple projection,
+    // which will not be processed in the nested block
+    List<Operator> succesors = load.getPlan().getSuccessors(load);
+
+    // An inner load is for a simple projection if it is a direct input of the @LOGenerate.
+    // Nothing need to be done further here.
+    if (succesors.size() == 1 && succesors.get(0) instanceof LOGenerate) {
+      return;
+    }
+
+    // Now get the index of projected column using its alias
+    RelDataType inputType = inputRel.getRowType();
+    final String colAlias = load.getProjection().getColAlias();
+    int index = colAlias != null
+                    ? inputType.getFieldNames().indexOf(colAlias)
+                    : load.getProjection().getColNum();
+    assert index >= 0;
+
+    // The column should have multiset type to serve as input for the inner plan
+    assert inputType.getFieldList().get(index).getType().getFamily() instanceof MultisetSqlType;
+
+    // Build a correlated expression from the input row
+    final CorrelationId correlId = builder.nextCorrelId();
+    final RexNode cor = builder.correl(inputType.getFieldList(), correlId);
+
+    // The project out the column from the correlated expression
+    RexNode fieldAccess = builder.getRexBuilder().makeFieldAccess(cor, index);
+    builder.push(LogicalValues.createOneRow(builder.getCluster()));
+    builder.project(fieldAccess);
+
+    // Flatten the column value so that it can be served as the input relation for the inner plan
+    builder.multiSetFlatten();
+
+    // Remember the correlation id, then the walker will walk up successor Pig operators. These
+    // operators will be processed in @PigRelOpVisitor until it hits the @LOGenerate operator,
+    // which will be processed in this class in visit(LOGenerate)
+    corStack.push(correlId);
+  }
+
+  @Override public boolean preVisit(LogicalRelationalOperator root) {
+    // Do not remember the visited PigOp in the inner plan, otherwise, we have trouble in doing
+    // correlate with shared PigOp
+    return false;
+  }
+}
+
+// End PigRelOpInnerVisitor.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigRelOpVisitor.java b/piglet/src/main/java/org/apache/calcite/piglet/PigRelOpVisitor.java
new file mode 100644
index 0000000..c776aa3
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigRelOpVisitor.java
@@ -0,0 +1,722 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.logical.LogicalCorrelate;
+import org.apache.calcite.rel.logical.LogicalJoin;
+import org.apache.calcite.rel.logical.LogicalProject;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rex.RexFieldCollation;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexWindowBound;
+import org.apache.calcite.sql.SqlAggFunction;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlWindow;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.ImmutableBitSet;
+
+import org.apache.pig.builtin.CubeDimensions;
+import org.apache.pig.builtin.RollupDimensions;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.util.LinkedMultiMap;
+import org.apache.pig.impl.util.MultiMap;
+import org.apache.pig.newplan.Operator;
+import org.apache.pig.newplan.OperatorPlan;
+import org.apache.pig.newplan.PlanWalker;
+import org.apache.pig.newplan.logical.expression.LogicalExpressionPlan;
+import org.apache.pig.newplan.logical.expression.UserFuncExpression;
+import org.apache.pig.newplan.logical.relational.LOCogroup;
+import org.apache.pig.newplan.logical.relational.LOCross;
+import org.apache.pig.newplan.logical.relational.LOCube;
+import org.apache.pig.newplan.logical.relational.LODistinct;
+import org.apache.pig.newplan.logical.relational.LOFilter;
+import org.apache.pig.newplan.logical.relational.LOForEach;
+import org.apache.pig.newplan.logical.relational.LOGenerate;
+import org.apache.pig.newplan.logical.relational.LOInnerLoad;
+import org.apache.pig.newplan.logical.relational.LOJoin;
+import org.apache.pig.newplan.logical.relational.LOLimit;
+import org.apache.pig.newplan.logical.relational.LOLoad;
+import org.apache.pig.newplan.logical.relational.LONative;
+import org.apache.pig.newplan.logical.relational.LORank;
+import org.apache.pig.newplan.logical.relational.LOSort;
+import org.apache.pig.newplan.logical.relational.LOSplit;
+import org.apache.pig.newplan.logical.relational.LOSplitOutput;
+import org.apache.pig.newplan.logical.relational.LOStore;
+import org.apache.pig.newplan.logical.relational.LOStream;
+import org.apache.pig.newplan.logical.relational.LOUnion;
+import org.apache.pig.newplan.logical.relational.LogicalPlan;
+import org.apache.pig.newplan.logical.relational.LogicalRelationalOperator;
+import org.apache.pig.newplan.logical.relational.LogicalSchema;
+
+import com.google.common.collect.ImmutableList;
+
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Visits Pig logical operators and converts them into corresponding relational
+ * algebra plans.
+ */
+class PigRelOpVisitor extends PigRelOpWalker.PlanPreVisitor {
+  private static final String RANK_PREFIX = "rank_";
+
+  // The relational algebra builder customized for Pig
+  protected final PigRelBuilder builder;
+  private Operator currentRoot;
+
+  /**
+   * Type of Pig groups
+   */
+  private enum GroupType {
+    CUBE,
+    ROLLUP,
+    REGULAR
+  }
+
+  /**
+   * Creates a PigRelOpVisitor.
+   *
+   * @param plan    Pig logical plan
+   * @param walker  The walker over Pig logical plan
+   * @param builder Relational algebra builder
+   * @throws FrontendException Exception during processing Pig operators
+   */
+  PigRelOpVisitor(OperatorPlan plan, PlanWalker walker, PigRelBuilder builder)
+      throws FrontendException {
+    super(plan, walker);
+    if (!(walker instanceof PigRelOpWalker)) {
+      throw new FrontendException("Expected PigRelOpWalker", 2223);
+    }
+    this.builder = builder;
+    this.currentRoot = null;
+  }
+
+  Operator getCurrentRoot() {
+    return currentRoot;
+  }
+
+  /**
+   * Translates the given pig logical plan into a list of relational algebra plans.
+   *
+   * @return The list of roots of translated plans, each corresponding to a sink
+   * operator in the Pig plan
+   * @throws FrontendException Exception during processing Pig operators
+   */
+  List<RelNode> translate() throws FrontendException {
+    List<RelNode> relNodes = new ArrayList<>();
+    for (Operator pigOp : plan.getSinks()) {
+      currentRoot = pigOp;
+      currentWalker.walk(this);
+      if (!(pigOp instanceof LOStore)) {
+        relNodes.add(builder.build());
+      }
+    }
+    return relNodes;
+  }
+
+  @Override public void visit(LOLoad load) throws FrontendException {
+    // Two types of tables to load:
+    // 1. LOAD '[schemaName.]tableName': load table from database catalog
+    // 2. LOAD '/path/to/tableName': load from a file
+    String fullName = load.getSchemaFile();
+    if (fullName.contains("file://")) {
+      // load from database catalog. Pig will see it as a file in the working directory
+      fullName = Paths.get(load.getSchemaFile()).getFileName().toString();
+    }
+    String[] tableNames;
+    if (fullName.startsWith("/")) {
+      // load from file
+      tableNames = new String[1];
+      tableNames[0] = fullName;
+    } else {
+      // load from catalog
+      tableNames = fullName.split("\\.");
+    }
+    final LogicalSchema pigSchema = load.getSchema();
+    final RelOptTable pigRelOptTable;
+    if (pigSchema == null) {
+      pigRelOptTable = null;
+    } else {
+      // If Pig schema is provided in the load command, convert it into
+      // relational row type
+      final RelDataType rowType = PigTypes.convertSchema(pigSchema);
+      pigRelOptTable = PigTable.createRelOptTable(builder.getRelOptSchema(),
+          rowType, Arrays.asList(tableNames));
+    }
+    builder.scan(pigRelOptTable, tableNames);
+    builder.register(load);
+  }
+
+  @Override public void visit(LOFilter filter) throws FrontendException {
+    final RexNode relExFilter = PigRelExVisitor.translatePigEx(builder, filter.getFilterPlan());
+    builder.filter(relExFilter);
+    builder.register(filter);
+  }
+
+  @Override public void visit(LOForEach foreach) throws FrontendException {
+    // Use an inner visitor to translate Pig inner plan into a relational plan
+    // See @PigRelOpInnerVisitor for details.
+    PigRelOpWalker innerWalker = new PigRelOpWalker(foreach.getInnerPlan());
+    PigRelOpInnerVisitor innerVisitor =
+        new PigRelOpInnerVisitor(foreach.getInnerPlan(), innerWalker, builder);
+    RelNode root = innerVisitor.translate().get(0);
+    builder.push(root);
+    builder.register(foreach);
+  }
+
+  @Override public void visit(LOCogroup loCogroup) throws FrontendException {
+    // Pig parser already converted CUBE operator into a set of operators, including LOCogroup.
+    // Thus this method handles all GROUP/COGROUP/CUBE commands
+    final GroupType groupType = getGroupType(loCogroup);
+    if (groupType == GroupType.REGULAR) {
+      processRegularGroup(loCogroup);
+    } else { // for CUBE and ROLLUP
+      processCube(groupType, loCogroup);
+    }
+
+    // Finally project the group and aggregate fields. Note that if group consists of multiple
+    // group keys, we do grouping using multiple keys and then convert these group keys into
+    // a single composite group key (with tuple/struct type) in this step.
+    // The other option is to create the composite group key first and do grouping on this
+    // composite key. But this option is less friendly the relational algebra, which flat
+    // types are more common.
+    projectGroup(loCogroup.getExpressionPlans().get(0).size());
+    builder.register(loCogroup);
+  }
+
+  /**
+   * Projects group key with 'group' alias so that upstream operator can refer to, along
+   * with other aggregate columns. If group consists of multiple group keys, construct
+   * a composite tuple/struct type to make it compatible with PIG group semantic.
+   *
+   * @param groupCount Number of group keys.
+   */
+  private void projectGroup(int groupCount) {
+    final List<RelDataTypeField> inputFields = builder.peek().getRowType().getFieldList();
+    RexNode groupRex;
+    // First construct the group field
+    if (groupCount == 1) {
+      // Single group key, just project it out directly
+      groupRex = builder.field(0);
+    } else {
+      // Otherwise, build a struct for all group keys use SQL ROW operator
+      List<String> fieldNames = new ArrayList<>();
+      List<RelDataType> fieldTypes = new ArrayList<>();
+      List<RexNode> fieldRexes = new ArrayList<>();
+      for (int j = 0; j < groupCount; j++) {
+        fieldTypes.add(inputFields.get(j).getType());
+        fieldNames.add(inputFields.get(j).getName());
+        fieldRexes.add(builder.field(j));
+      }
+      RelDataType groupDataType =
+          PigTypes.TYPE_FACTORY.createStructType(fieldTypes, fieldNames);
+      groupRex = builder.getRexBuilder().makeCall(
+          groupDataType, SqlStdOperatorTable.ROW, fieldRexes);
+    }
+    List<RexNode> outputFields = new ArrayList<>();
+    List<String> outputNames = new ArrayList<>();
+    // Project group field first
+    outputFields.add(groupRex);
+    outputNames.add("group");
+    // Then all other aggregate fields
+    for (int i = groupCount; i < inputFields.size(); i++) {
+      outputFields.add(builder.field(i));
+      outputNames.add(inputFields.get(i).getName());
+    }
+    builder.project(outputFields, outputNames, true);
+  }
+
+  /**
+   * Processes regular a group/group.
+   *
+   * @param loCogroup Pig logical group operator
+   * @throws FrontendException Exception during processing Pig operators
+   */
+  private void processRegularGroup(LOCogroup loCogroup) throws FrontendException {
+    final List<RelBuilder.GroupKey> groupKeys = new ArrayList<>();
+    final int numRels = loCogroup.getExpressionPlans().size();
+    // Project out the group keys and the whole row, which will be aggregated with
+    // COLLECT operator later.
+    preprocessCogroup(loCogroup, false);
+
+    // Build the group key
+    for (Integer key : loCogroup.getExpressionPlans().keySet()) {
+      final int groupCount = loCogroup.getExpressionPlans().get(key).size();
+      final List<RexNode> relKeys = new ArrayList<>();
+      for (int i = 0; i < groupCount; i++) {
+        relKeys.add(builder.field(numRels - key, 0, i));
+      }
+      groupKeys.add(builder.groupKey(relKeys));
+    }
+
+    // The do COLLECT aggregate.
+    builder.cogroup(groupKeys);
+  }
+
+  /**
+   * Processes a CUBE/ROLLUP group type.
+   *
+   * @param groupType type of the group, either ROLLUP or CUBE
+   * @param loCogroup Pig logical group operator
+   * @throws FrontendException Exception during processing Pig operator
+   */
+  private void processCube(GroupType groupType, LOCogroup loCogroup)
+      throws FrontendException {
+    assert loCogroup.getExpressionPlans().size() == 1;
+    // First adjust the top rel in the builder, which will be served as input rel for
+    // the CUBE COGROUP operator because Pig already convert LOCube into
+    // a ForEach (to project out the group set using @CubeDimensions or @RollupDimension UDFs)
+    // and a @LOCogroup. We dont need to use these UDFs to generate the groupset.
+    // So we need to undo the effect of translate this ForEach int relational
+    // algebra nodes before.
+    adjustCubeInput();
+
+    // Project out the group keys and the whole row, which will be aggregated with
+    // COLLECT operator later.
+    preprocessCogroup(loCogroup, true);
+
+    // Generate the group set for the corresponding group type.
+    ImmutableList.Builder<ImmutableBitSet> groupsetBuilder =
+        new ImmutableList.Builder<>();
+    List<Integer> keyIndexs = new ArrayList<>();
+    groupsetBuilder.add(ImmutableBitSet.of(keyIndexs));
+    int groupCount = loCogroup.getExpressionPlans().get(0).size();
+    for (int i = groupCount - 1; i >= 0; i--) {
+      keyIndexs.add(i);
+      groupsetBuilder.add(ImmutableBitSet.of(keyIndexs));
+    }
+    final ImmutableBitSet groupSet = ImmutableBitSet.of(keyIndexs);
+    final ImmutableList<ImmutableBitSet> groupSets =
+        (groupType == GroupType.CUBE)
+            ? ImmutableList.copyOf(groupSet.powerSet()) : groupsetBuilder.build();
+    RelBuilder.GroupKey groupKey = builder.groupKey(groupSet, groupSets);
+
+    // Finally, do COLLECT aggregate.
+    builder.cogroup(ImmutableList.of(groupKey));
+  }
+
+  /**
+   * Adjusts the rel input for Pig Cube operator.
+   */
+  private void adjustCubeInput() {
+    RelNode project1 = builder.peek();
+    assert project1 instanceof LogicalProject;
+    RelNode correl = ((LogicalProject) project1).getInput();
+    assert correl instanceof LogicalCorrelate;
+    RelNode project2 = ((LogicalCorrelate) correl).getLeft();
+    assert project2 instanceof LogicalProject;
+    builder.replaceTop(((LogicalProject) project2).getInput());
+  }
+
+  /**
+   * Projects out group key and the row for each relation
+   *
+   * @param loCogroup Pig logical group operator
+   * @throws FrontendException Exception during processing Pig operator
+   */
+  private void preprocessCogroup(LOCogroup loCogroup, boolean isCubeRollup)
+      throws FrontendException {
+    final int numRels = loCogroup.getExpressionPlans().size();
+
+    // Pull out all cogrouped relations from the builder
+    List<RelNode> inputRels = new ArrayList<>();
+    for (int i = 0; i < numRels; i++) {
+      inputRels.add(0, builder.build());
+    }
+
+    // Then adding back with the corresponding projection
+    for (int i = 0; i < numRels; i++) {
+      final RelNode originalRel = inputRels.get(i);
+      builder.push(originalRel);
+      final Collection<LogicalExpressionPlan> pigGroupKeys =
+          loCogroup.getExpressionPlans().get(i);
+      List<RexNode> fieldRels = new ArrayList<>();
+      for (LogicalExpressionPlan pigKey : pigGroupKeys) {
+        fieldRels.add(PigRelExVisitor.translatePigEx(builder, pigKey));
+      }
+      final RexNode row = builder.getRexBuilder().makeCall(getGroupRowType(fieldRels, isCubeRollup),
+          SqlStdOperatorTable.ROW, getGroupRowOperands(fieldRels, isCubeRollup));
+      fieldRels.add(row);
+      builder.project(fieldRels);
+      builder.updateAlias(builder.getPig(originalRel), builder.getAlias(originalRel), false);
+    }
+  }
+
+  // Gets row type for the group column
+  private RelDataType getGroupRowType(List<RexNode> groupFields, boolean isCubeRollup) {
+    if (isCubeRollup) {
+      final List<RelDataTypeField> rowFields = builder.peek().getRowType().getFieldList();
+      final List<String> fieldNames = new ArrayList<>();
+      final List<RelDataType> fieldTypes = new ArrayList<>();
+      final List<Integer> groupColIndexes = new ArrayList<>();
+
+      // First copy fields of grouping columns
+      for (RexNode rex : groupFields) {
+        assert rex instanceof RexInputRef;
+        int colIndex = ((RexInputRef) rex).getIndex();
+        groupColIndexes.add(colIndex);
+        fieldNames.add(rowFields.get(colIndex).getName());
+        fieldTypes.add(rowFields.get(colIndex).getType());
+      }
+
+      // Then copy the remaining fields from the parent rel
+      for (int i = 0; i < rowFields.size(); i++) {
+        if (!groupColIndexes.contains(i)) {
+          fieldNames.add(rowFields.get(i).getName());
+          fieldTypes.add(rowFields.get(i).getType());
+        }
+      }
+      return PigTypes.TYPE_FACTORY.createStructType(fieldTypes, fieldNames);
+    }
+    return builder.peek().getRowType();
+  }
+
+  /** Gets the operands for the ROW operator to construct the group column. */
+  private List<RexNode> getGroupRowOperands(List<RexNode> fieldRels,
+      boolean isCubeRollup) {
+    final List<RexNode> rowFields = builder.fields();
+    if (isCubeRollup) {
+      // Add group by columns first
+      List<RexNode> cubeRowFields = new ArrayList<>(fieldRels);
+
+      // Then and remaining columns
+      for (RexNode field : rowFields) {
+        if (!cubeRowFields.contains(field)) {
+          cubeRowFields.add(field);
+        }
+      }
+      return cubeRowFields;
+    }
+    return rowFields;
+  }
+
+  /**
+   * Checks the group type of a group.
+   *
+   * @param pigGroup Pig logical group operator
+   * @return The group type, either CUBE, ROLLUP, or REGULAR
+   */
+  private static GroupType getGroupType(LOCogroup pigGroup) {
+    if (pigGroup.getInputs((LogicalPlan) pigGroup.getPlan()).size() == 1) {
+      final Operator input = pigGroup.getInputs((LogicalPlan) pigGroup.getPlan()).get(0);
+      if (input instanceof LOForEach) {
+        final LOForEach foreach = (LOForEach) input;
+        if (foreach.getInnerPlan().getSinks().size() == 1) {
+          final LOGenerate generate = (LOGenerate) foreach.getInnerPlan().getSinks().get(0);
+          final List<LogicalExpressionPlan> projectList = generate.getOutputPlans();
+          if (projectList.size() > 1) {
+            final LogicalExpressionPlan exPlan = projectList.get(0);
+            if (exPlan.getSources().size() == 1
+                    && exPlan.getSources().get(0) instanceof UserFuncExpression) {
+              final UserFuncExpression func = (UserFuncExpression) exPlan.getSources().get(0);
+              if (func.getFuncSpec().getClassName().equals(CubeDimensions.class.getName())) {
+                return GroupType.CUBE;
+              }
+              if (func.getFuncSpec().getClassName().equals(RollupDimensions.class.getName())) {
+                return GroupType.ROLLUP;
+              }
+            }
+          }
+        }
+      }
+    }
+    return GroupType.REGULAR;
+  }
+
+  @Override public void visit(LOLimit loLimit) throws FrontendException {
+    builder.limit(0, (int) loLimit.getLimit());
+    builder.register(loLimit);
+  }
+
+  @Override public void visit(LOSort loSort) throws FrontendException {
+    // TODO Hanlde custom sortFunc from Pig???
+    final int limit = (int) loSort.getLimit();
+    List<RexNode> relSortCols = new ArrayList<>();
+    if (loSort.isStar()) {
+      // Sort using all columns
+      RelNode top = builder.peek();
+      for (RelDataTypeField field : top.getRowType().getFieldList()) {
+        relSortCols.add(builder.field(field.getIndex()));
+      }
+    } else {
+      // Sort using specific columns
+      assert loSort.getSortColPlans().size() == loSort.getAscendingCols().size();
+      for (int i = 0; i < loSort.getSortColPlans().size(); i++) {
+        RexNode sortColsNoDirection =
+            PigRelExVisitor.translatePigEx(builder, loSort.getSortColPlans().get(i));
+        // Add sort directions
+        if (!loSort.getAscendingCols().get(i)) {
+          relSortCols.add(builder.desc(sortColsNoDirection));
+        } else {
+          relSortCols.add(sortColsNoDirection);
+        }
+      }
+    }
+    builder.sortLimit(-1, limit, relSortCols);
+    builder.register(loSort);
+  }
+
+  @Override public void visit(LOJoin join) throws FrontendException {
+    joinInternal(join.getExpressionPlans(), join.getInnerFlags());
+    LogicalJoin joinRel = (LogicalJoin) builder.peek();
+    Set<String> duplicateNames = new HashSet<>(joinRel.getLeft().getRowType().getFieldNames());
+    duplicateNames.retainAll(joinRel.getRight().getRowType().getFieldNames());
+    if (!duplicateNames.isEmpty()) {
+      final List<String> fieldNames = new ArrayList<>();
+      final List<RexNode> fields = new ArrayList<>();
+      for (RelDataTypeField leftField : joinRel.getLeft().getRowType().getFieldList()) {
+        fieldNames.add(builder.getAlias(joinRel.getLeft()) + "::" + leftField.getName());
+        fields.add(builder.field(leftField.getIndex()));
+      }
+      int leftCount = joinRel.getLeft().getRowType().getFieldList().size();
+      for (RelDataTypeField rightField : joinRel.getRight().getRowType().getFieldList()) {
+        fieldNames.add(builder.getAlias(joinRel.getRight()) + "::" + rightField.getName());
+        fields.add(builder.field(rightField.getIndex() + leftCount));
+      }
+      builder.project(fields, fieldNames);
+    }
+    builder.register(join);
+  }
+
+  @Override public void visit(LOCross loCross) throws FrontendException {
+    final int numInputs = loCross.getInputs().size();
+    MultiMap<Integer, LogicalExpressionPlan> joinPlans = new LinkedMultiMap<>();
+    boolean[] innerFlags = new boolean[numInputs];
+    for (int i = 0; i < numInputs; i++) {
+      // Adding empty join keys
+      joinPlans.put(i, new LinkedList<>());
+      innerFlags[i] = true;
+    }
+    joinInternal(joinPlans, innerFlags);
+    builder.register(loCross);
+  }
+
+  /**
+   * Joins a list of relations (previously pushed into the builder).
+   *
+   * @param joinPlans  Join keys
+   * @param innerFlags Join type
+   * @throws FrontendException Exception during processing Pig operator
+   */
+  private void joinInternal(MultiMap<Integer, LogicalExpressionPlan> joinPlans,
+                            boolean[] innerFlags) throws FrontendException {
+    final int numRels = joinPlans.size();
+
+    // Pull out all joined relations from the builder
+    List<RelNode> joinRels = new ArrayList<>();
+    for (int i = 0; i < numRels; i++) {
+      joinRels.add(0, builder.build());
+    }
+
+    // Then join each pair from left to right
+    for (int i = 0; i < numRels; i++) {
+      builder.push(joinRels.get(i));
+      if (i == 0) {
+        continue;
+      }
+      List<RexNode> predicates = new ArrayList<>();
+      List<LogicalExpressionPlan> leftJoinExprs = joinPlans.get(i - 1);
+      List<LogicalExpressionPlan> rightJoinExprs = joinPlans.get(i);
+      assert leftJoinExprs.size() == rightJoinExprs.size();
+      for (int j = 0; j < leftJoinExprs.size(); j++) {
+        RexNode leftRelExpr =
+            PigRelExVisitor.translatePigEx(builder, leftJoinExprs.get(j), 2, 0);
+        RexNode rightRelExpr =
+            PigRelExVisitor.translatePigEx(builder, rightJoinExprs.get(j), 2, 1);
+        predicates.add(builder.equals(leftRelExpr, rightRelExpr));
+      }
+      builder.join(getJoinType(innerFlags[i - 1], innerFlags[i]), builder.and(predicates));
+    }
+  }
+
+  /**
+   * Decides the join type from the inner types of both relation.
+   *
+   * @param leftInner  true if the left requires inner
+   * @param rightInner true if the right requires inner
+   * @return The join type, either INNER, LEFT, RIGHT, or FULL
+   */
+  private static JoinRelType getJoinType(boolean leftInner, boolean rightInner) {
+    if (leftInner && rightInner) {
+      return JoinRelType.INNER;
+    } else if (leftInner) {
+      return JoinRelType.LEFT;
+    } else if (rightInner) {
+      return JoinRelType.RIGHT;
+    } else {
+      return JoinRelType.FULL;
+    }
+  }
+
+  @Override public void visit(LOUnion loUnion) throws FrontendException {
+    // The tricky thing to translate union are the input schemas. Relational algebra does not
+    // support UNION of input with different schemas, so we need to make sure to have inputs
+    // with same schema first.
+    LogicalSchema unionSchema = loUnion.getSchema();
+    if (unionSchema == null) {
+      throw new IllegalArgumentException("UNION on incompatible types is not supported. "
+          + "Please consider using ONSCHEMA option");
+    }
+    // First get the shared schema
+    int numInputs = loUnion.getInputs().size();
+    RelDataType unionRelType = PigTypes.convertSchema(unionSchema);
+
+    // Then using projections to adjust input relations with the shared schema
+    List<RelNode> adjustedInputs = new ArrayList<>();
+    for (int i = 0; i < numInputs; i++) {
+      adjustedInputs.add(builder.project(builder.build(), unionRelType));
+    }
+
+    // Push the adjusted input back to the builder to do union
+    for (int i = numInputs - 1; i >= 0; i--) {
+      builder.push(adjustedInputs.get(i));
+    }
+
+    // Finally do union
+    builder.union(true, numInputs);
+    builder.register(loUnion);
+  }
+
+  @Override public void visit(LODistinct loDistinct) throws FrontendException {
+    // Straightforward, just build distinct on the top relation
+    builder.distinct();
+    builder.register(loDistinct);
+  }
+
+  @Override public void visit(LOCube cube) throws FrontendException {
+    // Invalid to get here
+    throw new FrontendException("Cube should be translated into group by Pig parser", 10000);
+  }
+
+  @Override public void visit(LOInnerLoad load) throws FrontendException {
+    // InnerLoad should be handled by @PigRelOpInnerVisitor
+    throw new FrontendException("Not implemented", 10000);
+  }
+
+  @Override public void visit(LOSplit loSplit) throws FrontendException {
+    builder.register(loSplit);
+  }
+
+  @Override public void visit(LOSplitOutput loSplitOutput) throws FrontendException {
+    final RexNode relExFilter =
+        PigRelExVisitor.translatePigEx(builder, loSplitOutput.getFilterPlan());
+    builder.filter(relExFilter);
+    builder.register(loSplitOutput);
+  }
+
+  @Override public void visit(LOStore store) throws FrontendException {
+    builder.store(store.getAlias());
+  }
+
+  @Override public void visit(LOGenerate gen) throws FrontendException {
+    // LOGenerate should be handled by @PigRelOpInnerVisitor
+    throw new FrontendException("Not implemented", 10000);
+  }
+
+  @Override public void visit(LORank loRank) throws FrontendException {
+    // First build the rank field using window function with information from loRank
+    final RexNode rankField = buildRankField(loRank);
+
+    // Then project out the rank field along with all other fields
+    final RelDataType inputRowType = builder.peek().getRowType();
+    List<RexNode> projectedFields = new ArrayList<>();
+    List<String> fieldNames = new ArrayList<>();
+
+    projectedFields.add(rankField);
+    fieldNames.add(RANK_PREFIX + loRank.getAlias()); // alias of the rank field
+    for (int i = 0; i < inputRowType.getFieldCount(); i++) {
+      projectedFields.add(builder.field(i));
+      fieldNames.add(inputRowType.getFieldNames().get(i));
+    }
+
+    // Finally do project
+    builder.project(projectedFields, fieldNames);
+    builder.register(loRank);
+  }
+
+  /**
+   * Builds a window function for {@link LORank}.
+   *
+   * @param loRank Pig logical rank operator
+   * @return The window function
+   * @throws FrontendException Exception during processing Pig operator
+   */
+  private RexNode buildRankField(LORank loRank) throws FrontendException {
+    // Aggregate function is either RANK or DENSE_RANK
+    SqlAggFunction rank =
+        loRank.isDenseRank() ? SqlStdOperatorTable.DENSE_RANK : SqlStdOperatorTable.RANK;
+
+    // Build the order keys
+    List<RexFieldCollation> orderNodes = new ArrayList<>();
+    for (int i = 0; i < loRank.getRankColPlans().size(); i++) {
+      RexNode orderNode =
+          PigRelExVisitor.translatePigEx(builder, loRank.getRankColPlans().get(i));
+      Set<SqlKind> flags = new HashSet<>();
+      if (!loRank.getAscendingCol().get(i)) {
+        flags.add(SqlKind.DESCENDING);
+      }
+      orderNodes.add(new RexFieldCollation(orderNode, flags));
+    }
+
+    return builder.getRexBuilder().makeOver(
+        PigTypes.TYPE_FACTORY.createSqlType(SqlTypeName.BIGINT), // Return type
+        rank, // Aggregate function
+        Collections.emptyList(), // Operands for the aggregate function, empty here
+        Collections.emptyList(), // No partition keys
+        ImmutableList.copyOf(orderNodes), // order keys
+        RexWindowBound.create(
+            SqlWindow.createUnboundedPreceding(SqlParserPos.ZERO),
+            null), // window with unbounded lower
+        RexWindowBound.create(
+            SqlWindow.createCurrentRow(SqlParserPos.ZERO),
+            null), // till current
+        false, // Range-based
+        true, // allow partial
+        false, // not return null when count is zero
+        false, // no distinct
+        false);
+  }
+
+  @Override public void visit(LOStream loStream) throws FrontendException {
+    throw new FrontendException("Not implemented", 10000);
+  }
+
+  @Override public void visit(LONative nativeMR) throws FrontendException {
+    throw new FrontendException("Not implemented", 10000);
+  }
+
+  @Override public boolean preVisit(LogicalRelationalOperator root) {
+    return builder.checkMap(root);
+  }
+}
+
+// End PigRelOpVisitor.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigRelOpWalker.java b/piglet/src/main/java/org/apache/calcite/piglet/PigRelOpWalker.java
new file mode 100644
index 0000000..26f2588
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigRelOpWalker.java
@@ -0,0 +1,92 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.util.Utils;
+import org.apache.pig.newplan.Operator;
+import org.apache.pig.newplan.OperatorPlan;
+import org.apache.pig.newplan.PlanVisitor;
+import org.apache.pig.newplan.PlanWalker;
+import org.apache.pig.newplan.logical.relational.LogicalRelationalNodesVisitor;
+import org.apache.pig.newplan.logical.relational.LogicalRelationalOperator;
+
+import java.util.Collection;
+
+/**
+ * Post-order walker for Pig logical relational plans. Walks the plan
+ * from sinks to sources.
+ */
+class PigRelOpWalker extends PlanWalker {
+  /**
+   * Visitor that allow doing pre-visit.
+   */
+  abstract static class PlanPreVisitor extends LogicalRelationalNodesVisitor {
+    PlanPreVisitor(OperatorPlan plan, PlanWalker walker) throws FrontendException {
+      super(plan, walker);
+    }
+
+    /**
+     * Called before a node.
+     *
+     * @param root Pig logical operator to check
+     * @return Returns whether the node has been visited before
+     */
+    public abstract boolean preVisit(LogicalRelationalOperator root);
+  }
+
+  PigRelOpWalker(OperatorPlan plan) {
+    super(plan);
+  }
+
+  @Override public void walk(PlanVisitor planVisitor) throws FrontendException {
+    if (!(planVisitor instanceof PigRelOpVisitor)) {
+      throw new FrontendException("Expected PigRelOpVisitor", 2223);
+    }
+
+    final PigRelOpVisitor pigRelVistor = (PigRelOpVisitor) planVisitor;
+    postOrderWalk(pigRelVistor.getCurrentRoot(), pigRelVistor);
+  }
+
+  /**
+   * Does post-order walk on the Pig logical relational plans from sinks to sources.
+   *
+   * @param root The root Pig logical relational operator
+   * @param visitor The visitor of each Pig logical operator node
+   * @throws FrontendException Exception during processing Pig operator
+   */
+  private void postOrderWalk(Operator root, PlanPreVisitor visitor) throws FrontendException {
+    if (root == null || visitor.preVisit((LogicalRelationalOperator) root)) {
+      return;
+    }
+
+    Collection<Operator> nexts =
+        Utils.mergeCollection(plan.getPredecessors(root), plan.getSoftLinkPredecessors(root));
+    if (nexts != null) {
+      for (Operator op : nexts) {
+        postOrderWalk(op, visitor);
+      }
+    }
+    root.accept(visitor);
+  }
+
+  @Override public PlanWalker spawnChildWalker(OperatorPlan operatorPlan) {
+    return new PigRelOpWalker(operatorPlan);
+  }
+}
+
+// End PigRelOpWalker.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigRelSqlUdfs.java b/piglet/src/main/java/org/apache/calcite/piglet/PigRelSqlUdfs.java
new file mode 100644
index 0000000..cec7d18
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigRelSqlUdfs.java
@@ -0,0 +1,361 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.calcite.adapter.java.JavaTypeFactory;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactoryImpl;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.schema.ScalarFunction;
+import org.apache.calcite.schema.impl.ScalarFunctionImpl;
+import org.apache.calcite.sql.SqlCallBinding;
+import org.apache.calcite.sql.SqlOperandCountRange;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.type.MultisetSqlType;
+import org.apache.calcite.sql.type.OperandTypes;
+import org.apache.calcite.sql.type.SqlOperandCountRanges;
+import org.apache.calcite.sql.type.SqlOperandTypeChecker;
+import org.apache.calcite.sql.type.SqlReturnTypeInference;
+import org.apache.calcite.sql.type.SqlTypeFamily;
+import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.validate.SqlUserDefinedFunction;
+
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.BagFactory;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.data.TupleFactory;
+
+import com.google.common.collect.ImmutableList;
+
+import java.lang.reflect.Method;
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.apache.calcite.piglet.PigTypes.TYPE_FACTORY;
+
+/**
+ * User-defined functions ({@link SqlUserDefinedFunction UDFs})
+ * needed for Pig-to-{@link RelNode} translation.
+ */
+public class PigRelSqlUdfs {
+  private PigRelSqlUdfs() {
+  }
+
+  // Defines ScalarFunc from their implementations
+  private static final ScalarFunction PIG_TUPLE_FUNC =
+      ScalarFunctionImpl.create(PigRelSqlUdfs.class, "buildTuple");
+  private static final ScalarFunction PIG_BAG_FUNC =
+      ScalarFunctionImpl.create(PigRelSqlUdfs.class, "buildBag");
+  private static final ScalarFunction MULTISET_PROJECTION_FUNC =
+      ScalarFunctionImpl.create(PigRelSqlUdfs.class, "projectMultiset");
+
+  /**
+   * Multiset projection projects a subset of columns from the component type
+   * of a multiset type. The result is still a multiset but the component
+   * type only has a subset of columns of the original component type
+   *
+   * <p>For example, given a multiset type
+   * {@code M = [(A: int, B: double, C: varchar)]},
+   * a projection
+   * {@code MULTISET_PROJECTION(M, A, C)}
+   * gives a new multiset
+   * {@code N = [(A: int, C: varchar)]}.
+   */
+  static final SqlUserDefinedFunction MULTISET_PROJECTION =
+      new PigUserDefinedFunction("MULTISET_PROJECTION",
+          multisetProjectionInfer(), multisetProjectionCheck(), null,
+          MULTISET_PROJECTION_FUNC);
+
+  /**
+   * Creates a Pig Tuple from a list of relational operands.
+   *
+   * @param operands Relational operands
+   * @return Pig Tuple SqlUDF
+   */
+  static SqlUserDefinedFunction createPigTupleUDF(ImmutableList<RexNode> operands) {
+    return new PigUserDefinedFunction("PIG_TUPLE",
+        infer(PigRelSqlUdfs.PIG_TUPLE_FUNC),
+        OperandTypes.family(getFamilitTypes(operands)),
+        getRelDataTypes(operands),
+        PigRelSqlUdfs.PIG_TUPLE_FUNC);
+  }
+
+  /**
+   * Creates a Pig DataBag from a list of relational operands.
+   *
+   * @param operands Relational operands
+   * @return Pig DataBag SqlUDF
+   */
+  static SqlUserDefinedFunction createPigBagUDF(ImmutableList<RexNode> operands) {
+    return new PigUserDefinedFunction(
+        "PIG_BAG",
+        infer(PigRelSqlUdfs.PIG_BAG_FUNC),
+        OperandTypes.family(getFamilitTypes(operands)),
+        getRelDataTypes(operands),
+        PigRelSqlUdfs.PIG_BAG_FUNC);
+  }
+
+  /**
+   * Creates a generic SqlUDF operator from a Pig UDF.
+   *
+   * @param udfName Name of the UDF
+   * @param method Method "exec" for implementing the UDF
+   * @param funcSpec Pig Funcspec
+   * @param inputType Argument type for the input
+   * @param returnType Function return data type
+   */
+  static SqlUserDefinedFunction createGeneralPigUdf(String udfName,
+      Method method, FuncSpec funcSpec, RelDataType inputType,
+      RelDataType returnType) {
+    return new PigUserDefinedFunction(udfName, opBinding -> returnType,
+        OperandTypes.ANY, Collections.singletonList(inputType),
+        ScalarFunctionImpl.createUnsafe(method), funcSpec);
+  }
+
+  /**
+   * Returns a {@link SqlReturnTypeInference} for multiset projection operator.
+   */
+  private static SqlReturnTypeInference multisetProjectionInfer() {
+    return opBinding -> {
+      final MultisetSqlType source = (MultisetSqlType) opBinding.getOperandType(0);
+      final List<RelDataTypeField> fields = source.getComponentType().getFieldList();
+      // Project a multiset of single column
+      if (opBinding.getOperandCount() == 2) {
+        final int fieldNo = opBinding.getOperandLiteralValue(1, Integer.class);
+        if (fields.size() == 1) {
+          // Corner case: source with only single column, nothing to do.
+          assert fieldNo == 0;
+          return source;
+        } else {
+          return TYPE_FACTORY.createMultisetType(fields.get(fieldNo).getType(), -1);
+        }
+      }
+      // Construct a multiset of records of the input argument types
+      final List<String> destNames = new ArrayList<>();
+      final List<RelDataType> destTypes = new ArrayList<>();
+      for (int i = 1; i < opBinding.getOperandCount(); i++) {
+        final int fieldNo = opBinding.getOperandLiteralValue(i, Integer.class);
+        destNames.add(fields.get(fieldNo).getName());
+        destTypes.add(fields.get(fieldNo).getType());
+      }
+      return TYPE_FACTORY.createMultisetType(
+          TYPE_FACTORY.createStructType(destTypes, destNames), -1);
+    };
+  }
+
+  /**
+   * Returns a {@link SqlOperandTypeChecker} for multiset projection operator.
+   */
+  private static SqlOperandTypeChecker multisetProjectionCheck() {
+    return new SqlOperandTypeChecker() {
+      public boolean checkOperandTypes(
+          SqlCallBinding callBinding, boolean throwOnFailure) {
+        // Need at least two arguments
+        if (callBinding.getOperandCount() < 2) {
+          return false;
+        }
+
+        // The first argument should be a multiset
+        if (!(callBinding.getOperandType(0) instanceof MultisetSqlType)) {
+          return false;
+        }
+
+        // All the subsequent arguments should be appropriate integers
+        final MultisetSqlType source = (MultisetSqlType) callBinding.getOperandType(0);
+        final int maxFieldNo = source.getComponentType().getFieldCount() - 1;
+
+        for (int i = 1; i < callBinding.getOperandCount(); i++) {
+          if (!(callBinding.getOperandLiteralValue(i, Comparable.class)
+              instanceof BigDecimal)) {
+            return false;
+          }
+          final int fieldNo =
+              callBinding.getOperandLiteralValue(i, Integer.class);
+          // Field number should between 0 and maxFieldNo
+          if (fieldNo < 0 || fieldNo > maxFieldNo) {
+            return false;
+          }
+        }
+        return true;
+      }
+
+      public SqlOperandCountRange getOperandCountRange() {
+        return SqlOperandCountRanges.from(2);
+      }
+
+      public String getAllowedSignatures(SqlOperator op, String opName) {
+        return opName + "(...)";
+      }
+
+      public boolean isOptional(int i) {
+        return false;
+      }
+
+      public Consistency getConsistency() {
+        return Consistency.NONE;
+      }
+    };
+  }
+
+  /**
+   * Helper method to return a list of SqlTypeFamily for a given list of relational operands
+   *
+   * @param operands List of relational operands
+   * @return List of SqlTypeFamilies
+   */
+  private static List<SqlTypeFamily> getFamilitTypes(ImmutableList<RexNode> operands) {
+    List<SqlTypeFamily> ret = new ArrayList<>();
+    for (RexNode operand : operands) {
+      SqlTypeFamily family = operand.getType().getSqlTypeName().getFamily();
+      ret.add(family != null ? family : SqlTypeFamily.ANY);
+    }
+    return ret;
+  }
+
+  /**
+   * Helper method to return a list of RelDataType for a given list of relational operands
+   *
+   * @param operands List of relational operands
+   * @return List of RelDataTypes
+   */
+  private static List<RelDataType> getRelDataTypes(ImmutableList<RexNode> operands) {
+    List<RelDataType> ret = new ArrayList<>();
+    for (RexNode operand : operands) {
+      ret.add(operand.getType());
+    }
+    return ret;
+  }
+
+  /**
+   * Gets the SqlReturnTypeInference that can infer the return type from a
+   * function.
+   *
+   * @param function ScalarFunction
+   * @return SqlReturnTypeInference
+   */
+  private static SqlReturnTypeInference infer(final ScalarFunction function) {
+    return opBinding -> getRelDataType(function);
+  }
+
+  /**
+   * Gets the return data type for a given function.
+   *
+   * @param function ScalarFunction
+   * @return returned data type
+   */
+  private static RelDataType getRelDataType(ScalarFunction function) {
+    final JavaTypeFactory typeFactory = TYPE_FACTORY;
+    final RelDataType type = function.getReturnType(typeFactory);
+    if (type instanceof RelDataTypeFactoryImpl.JavaType
+        && ((RelDataTypeFactoryImpl.JavaType) type).getJavaClass()
+        == Object.class) {
+      return typeFactory.createTypeWithNullability(
+          typeFactory.createSqlType(SqlTypeName.ANY), true);
+    }
+    return typeFactory.toSql(type);
+  }
+
+  /**
+   * Implementation for PIG_TUPLE functions. Builds a Pig Tuple from
+   * an array of objects
+   *
+   * @param elements Array of element objects
+   * @return Pig Tuple
+   */
+  public static Tuple buildTuple(Object... elements) {
+    return TupleFactory.getInstance().newTuple(Arrays.asList(elements));
+  }
+
+
+  /**
+   * Implementation for PIG_BAG functions. Builds a Pig DataBag from
+   * the corresponding input
+   *
+   * @param elements Input that contains a bag
+   * @return Pig Tuple
+   */
+  public static Tuple buildBag(Object... elements) {
+    final TupleFactory tupleFactory = TupleFactory.getInstance();
+    final BagFactory bagFactory = BagFactory.getInstance();
+    // Convert each row into a Tuple
+    List<Tuple> tupleList = new ArrayList<>();
+    if (elements != null) {
+      // The first input contains a list of rows for the bag
+      final List bag = (elements[0] instanceof List)
+          ? (List) elements[0]
+          : Collections.singletonList(elements[0]);
+      for (Object row : bag) {
+        tupleList.add(tupleFactory.newTuple(Arrays.asList(row)));
+      }
+    }
+
+    // Then build a bag from the tuple list
+    DataBag resultBag = bagFactory.newDefaultBag(tupleList);
+
+    // The returned result is a new Tuple with the newly constructed DataBag
+    // as the first item.
+    List<Object> finalTuple = new ArrayList<>();
+    finalTuple.add(resultBag);
+
+    if (elements != null) {
+      // Add the remaining elements from the input
+      for (int i = 1; i < elements.length; i++) {
+        finalTuple.add(elements[i]);
+      }
+    }
+
+    return tupleFactory.newTuple(finalTuple);
+  }
+
+  /**
+   * Implementation for BAG_PROJECTION functions. Builds a new multiset by
+   * projecting certain columns from another multiset.
+   *
+   * @param objects Input argument, the first one is a multiset, the remaining
+   *                are indexes of column to project.
+   * @return The projected multiset
+   */
+  public static List projectMultiset(Object... objects) {
+    // The first input is a multiset
+    final List<Object[]> inputMultiset = (List) objects[0];
+    final List projectedMultiset = new ArrayList<>();
+
+    for (Object[] row : inputMultiset) {
+      if (objects.length > 2) {
+        // Projecting more than one column, the projected multiset should have
+        // the component type of a row
+        Object[] newRow = new Object[objects.length - 1];
+        for (int j = 1; j < objects.length; j++) {
+          newRow[j - 1] = row[(Integer) objects[j]];
+        }
+        projectedMultiset.add(newRow);
+      } else {
+        // Projecting a single column
+        projectedMultiset.add(row[(Integer) objects[1]]);
+      }
+    }
+    return projectedMultiset;
+  }
+}
+
+// End PigRelSqlUdfs.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigRelToSqlConverter.java b/piglet/src/main/java/org/apache/calcite/piglet/PigRelToSqlConverter.java
new file mode 100644
index 0000000..f630b88
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigRelToSqlConverter.java
@@ -0,0 +1,128 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.calcite.adapter.enumerable.EnumerableInterpreter;
+import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.rel.RelFieldCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Window;
+import org.apache.calcite.rel.rel2sql.RelToSqlConverter;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlDialect;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlWindow;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * An extension of {@link RelToSqlConverter} to convert a relation algebra tree,
+ * translated from a Pig script, into a SQL statement.
+ *
+ * <p>The input relational algebra tree can be optimized by the planner for Pig
+ * to {@link RelNode}.
+ */
+public class PigRelToSqlConverter extends RelToSqlConverter {
+
+  /** Creates a RelToSqlConverter.
+   *
+   * @param dialect SQL dialect
+   */
+  PigRelToSqlConverter(SqlDialect dialect) {
+    super(dialect);
+  }
+
+  @Override public Result visit(Aggregate e) {
+    final Result x = visitChild(0, e.getInput());
+    final boolean isProjectOutput = e.getInput() instanceof Project
+        || (e.getInput() instanceof EnumerableInterpreter
+            && ((EnumerableInterpreter) e.getInput()).getInput()
+                instanceof Project);
+    final Builder builder = getAggregateBuilder(e, x, isProjectOutput);
+
+    final List<SqlNode> groupByList = Expressions.list();
+    final List<SqlNode> selectList = new ArrayList<>();
+    buildAggGroupList(e, builder, groupByList, selectList);
+
+    final int groupSetSize = e.getGroupSets().size();
+    SqlNodeList groupBy = new SqlNodeList(groupByList, POS);
+    if (groupSetSize > 1) {
+      // If there are multiple group sets, this should be a result of converting a
+      // Pig CUBE/cube or Pig CUBE/rollup
+      final List<SqlNode> cubeRollupList = Expressions.list();
+      if (groupSetSize == groupByList.size() + 1) {
+        cubeRollupList.add(SqlStdOperatorTable.ROLLUP.createCall(groupBy));
+      } else {
+        assert groupSetSize == Math.round(Math.pow(2, groupByList.size()));
+        cubeRollupList.add(SqlStdOperatorTable.CUBE.createCall(groupBy));
+      }
+      groupBy = new SqlNodeList(cubeRollupList, POS);
+    }
+
+    return buildAggregate(e, builder, selectList, groupBy.getList());
+  }
+
+  /** @see #dispatch */
+  public Result visit(Window e) {
+    final Result x = visitChild(0, e.getInput());
+    final Builder builder = x.builder(e, Clause.SELECT);
+    final List<SqlNode> selectList =
+        new ArrayList<>(builder.context.fieldList());
+
+    for (Window.Group winGroup : e.groups) {
+      final List<SqlNode> partitionList = Expressions.list();
+      for (int i : winGroup.keys) {
+        partitionList.add(builder.context.field(i));
+      }
+
+      final List<SqlNode> orderList = Expressions.list();
+      for (RelFieldCollation orderKey : winGroup.collation().getFieldCollations()) {
+        orderList.add(builder.context.toSql(orderKey));
+      }
+
+      final SqlNode sqlWindow =  SqlWindow.create(
+          null, // Window declaration name
+          null, // Window reference name
+          new SqlNodeList(partitionList, POS),
+          new SqlNodeList(orderList, POS),
+          SqlLiteral.createBoolean(winGroup.isRows, POS),
+          builder.context.toSql(winGroup.lowerBound),
+          builder.context.toSql(winGroup.upperBound),
+          null, // allowPartial
+          POS);
+
+      for (Window.RexWinAggCall winFunc : winGroup.aggCalls) {
+        final List<SqlNode> winFuncOperands = Expressions.list();
+        for (RexNode operand : winFunc.getOperands()) {
+          winFuncOperands.add(builder.context.toSql(null, operand));
+        }
+        SqlNode aggFunc = winFunc.getOperator().createCall(new SqlNodeList(winFuncOperands, POS));
+        selectList.add(SqlStdOperatorTable.OVER.createCall(POS, aggFunc, sqlWindow));
+      }
+      builder.setSelect(new SqlNodeList(selectList, POS));
+    }
+    return builder.result();
+  }
+}
+
+// End PigRelToSqlConverter.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigRelUdfConverter.java b/piglet/src/main/java/org/apache/calcite/piglet/PigRelUdfConverter.java
new file mode 100644
index 0000000..cc88011
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigRelUdfConverter.java
@@ -0,0 +1,195 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.schema.impl.ScalarFunctionImpl;
+import org.apache.calcite.sql.SqlAggFunction;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.sql.validate.SqlUserDefinedFunction;
+
+import org.apache.pig.Accumulator;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+import java.lang.reflect.Method;
+import java.util.Map;
+
+/**
+ * This class maps a Pig UDF to a corresponding SQL built-in function/operator.
+ * If such mapping is not available, it creates a wrapper to allow SQL engines
+ * call Pig UDFs directly.
+ *
+ */
+class PigRelUdfConverter {
+
+  private PigRelUdfConverter() {}
+
+  private static final PigUdfFinder PIG_UDF_FINDER = new PigUdfFinder();
+
+  private static final Map<String, SqlOperator> BUILTIN_FUNC =
+      ImmutableMap.<String, SqlOperator>builder()
+          .put("org.apache.pig.builtin.ABS", SqlStdOperatorTable.ABS)
+          .put("org.apache.pig.builtin.BigDecimalAbs", SqlStdOperatorTable.ABS)
+          .put("org.apache.pig.builtin.BigIntegerAbs", SqlStdOperatorTable.ABS)
+          .put("org.apache.pig.builtin.DoubleAbs", SqlStdOperatorTable.ABS)
+          .put("org.apache.pig.builtin.FloatAbs", SqlStdOperatorTable.ABS)
+          .put("org.apache.pig.builtin.IntAbs", SqlStdOperatorTable.ABS)
+          .put("org.apache.pig.builtin.LongAbs", SqlStdOperatorTable.ABS)
+          .put("org.apache.pig.builtin.CEIL", SqlStdOperatorTable.CEIL)
+          .put("org.apache.pig.builtin.CONCAT", SqlStdOperatorTable.CONCAT)
+          .put("org.apache.pig.builtin.StringConcat", SqlStdOperatorTable.CONCAT)
+          .put("org.apache.pig.builtin.EXP", SqlStdOperatorTable.EXP)
+          .put("org.apache.pig.builtin.FLOOR", SqlStdOperatorTable.FLOOR)
+          .put("org.apache.pig.builtin.LOG", SqlStdOperatorTable.LN)
+          .put("org.apache.pig.builtin.LOG10", SqlStdOperatorTable.LOG10)
+          .put("org.apache.pig.builtin.LOWER", SqlStdOperatorTable.LOWER)
+          .put("org.apache.pig.builtin.RANDOM", SqlStdOperatorTable.RAND)
+          .put("org.apache.pig.builtin.SQRT", SqlStdOperatorTable.SQRT)
+          .put("org.apache.pig.builtin.StringSize", SqlStdOperatorTable.CHAR_LENGTH)
+          .put("org.apache.pig.builtin.SUBSTRING", SqlStdOperatorTable.SUBSTRING)
+          .put("org.apache.pig.builtin.TOTUPLE", SqlStdOperatorTable.ROW)
+          .put("org.apache.pig.builtin.UPPER", SqlStdOperatorTable.UPPER)
+          .build();
+
+  private static final Map<String, SqlAggFunction> BUILTIN_AGG_FUNC =
+      ImmutableMap.<String, SqlAggFunction>builder()
+          // AVG()
+          .put("org.apache.pig.builtin.AVG", SqlStdOperatorTable.AVG)
+          .put("org.apache.pig.builtin.BigDecimalAvg", SqlStdOperatorTable.AVG)
+          .put("org.apache.pig.builtin.BigIntegerAvg", SqlStdOperatorTable.AVG)
+          .put("org.apache.pig.builtin.DoubleAvg", SqlStdOperatorTable.AVG)
+          .put("org.apache.pig.builtin.FloatAvg", SqlStdOperatorTable.AVG)
+          .put("org.apache.pig.builtin.IntAvg", SqlStdOperatorTable.AVG)
+          .put("org.apache.pig.builtin.LongAvg", SqlStdOperatorTable.AVG)
+          // COUNT()
+          .put("org.apache.pig.builtin.COUNT", SqlStdOperatorTable.COUNT)
+          // MAX()
+          .put("org.apache.pig.builtin.MAX", SqlStdOperatorTable.MAX)
+          .put("org.apache.pig.builtin.BigDecimalMax", SqlStdOperatorTable.MAX)
+          .put("org.apache.pig.builtin.BigIntegerMax", SqlStdOperatorTable.MAX)
+          .put("org.apache.pig.builtin.DateTimeMax", SqlStdOperatorTable.MAX)
+          .put("org.apache.pig.builtin.DoubleMax", SqlStdOperatorTable.MAX)
+          .put("org.apache.pig.builtin.FloatMax", SqlStdOperatorTable.MAX)
+          .put("org.apache.pig.builtin.IntMax", SqlStdOperatorTable.MAX)
+          .put("org.apache.pig.builtin.LongMax", SqlStdOperatorTable.MAX)
+          .put("org.apache.pig.builtin.StringMax", SqlStdOperatorTable.MAX)
+          // MIN()
+          .put("org.apache.pig.builtin.MIN", SqlStdOperatorTable.MIN)
+          .put("org.apache.pig.builtin.BigDecimalMin", SqlStdOperatorTable.MIN)
+          .put("org.apache.pig.builtin.BigIntegerMin", SqlStdOperatorTable.MIN)
+          .put("org.apache.pig.builtin.DateTimeMin", SqlStdOperatorTable.MIN)
+          .put("org.apache.pig.builtin.DoubleMin", SqlStdOperatorTable.MIN)
+          .put("org.apache.pig.builtin.FloatMin", SqlStdOperatorTable.MIN)
+          .put("org.apache.pig.builtin.IntMin", SqlStdOperatorTable.MIN)
+          .put("org.apache.pig.builtin.LongMin", SqlStdOperatorTable.MIN)
+          .put("org.apache.pig.builtin.StringMin", SqlStdOperatorTable.MIN)
+          // SUM()
+          .put("org.apache.pig.builtin.BigDecimalSum", SqlStdOperatorTable.SUM)
+          .put("org.apache.pig.builtin.BigIntegerSum", SqlStdOperatorTable.SUM)
+          .put("org.apache.pig.builtin.DoubleSum", SqlStdOperatorTable.SUM)
+          .put("org.apache.pig.builtin.FloatSum", SqlStdOperatorTable.SUM)
+          .put("org.apache.pig.builtin.IntSum", SqlStdOperatorTable.SUM)
+          .put("org.apache.pig.builtin.LongSum", SqlStdOperatorTable.SUM)
+          .build();
+
+  /**
+   * Converts a Pig UDF, given its {@link FuncSpec} and a list of relational
+   * operands (function arguments). To call this function, the arguments of
+   * Pig functions need to be converted into the relational types before.
+   *
+   * @param builder The relational builder
+   * @param pigFunc Pig function description
+   * @param operands Relational operands for the function
+   * @param returnType Function return data type
+   * @return The SQL calls equivalent to the Pig function
+   */
+  static RexNode convertPigFunction(PigRelBuilder builder, FuncSpec pigFunc,
+      ImmutableList<RexNode> operands, RelDataType returnType) throws FrontendException {
+    // First, check the map for the direct mapping SQL builtin
+    final SqlOperator operator = BUILTIN_FUNC.get(pigFunc.getClassName());
+    if (operator != null) {
+      return builder.call(operator, operands);
+    }
+
+    // If no mapping found, build the argument wrapper to convert the relation operands
+    // into a Pig tuple so that the Pig function can consume it.
+    try {
+      // Find the implementation method for the Pig function from
+      // the class defining the UDF.
+      final Class clazz = Class.forName(pigFunc.getClassName());
+      final Method method =
+          PIG_UDF_FINDER.findPigUdfImplementationMethod(clazz);
+
+      // Now create the argument wrapper. Depend on the type of the UDF, the
+      // relational operands are converted into a Pig Tuple or Pig DataBag
+      // with the appropriate wrapper.
+      final SqlUserDefinedFunction convertOp =
+          Accumulator.class.isAssignableFrom(clazz)
+              ? PigRelSqlUdfs.createPigBagUDF(operands)
+              : PigRelSqlUdfs.createPigTupleUDF(operands);
+      final RexNode rexTuple = builder.call(convertOp, operands);
+
+      // Then convert the Pig function into a @SqlUserDefinedFunction.
+      SqlUserDefinedFunction userFuncOp =
+          PigRelSqlUdfs.createGeneralPigUdf(clazz.getSimpleName(),
+              method, pigFunc, rexTuple.getType(), returnType);
+
+      // Ready to return SqlCall after having SqlUDF and operand
+      return builder.call(userFuncOp, ImmutableList.of(rexTuple));
+    } catch (ClassNotFoundException e) {
+      throw new FrontendException("Cannot find the implementation for Pig UDF class: "
+          + pigFunc.getClassName());
+    }
+  }
+
+  /**
+   * Gets the {@link SqlAggFunction} for the corresponding Pig aggregate
+   * UDF call; returns null for invalid rex call.
+   *
+   * @param call Pig aggregate UDF call
+   */
+  static SqlAggFunction getSqlAggFuncForPigUdf(RexCall call) {
+    if (!(call.getOperator() instanceof PigUserDefinedFunction)) {
+      return null;
+    }
+
+    final PigUserDefinedFunction pigUdf = (PigUserDefinedFunction) call.getOperator();
+    if (pigUdf.funcSpec != null) {
+      final String pigUdfClassName = pigUdf.funcSpec.getClassName();
+      final SqlAggFunction sqlAggFunction = BUILTIN_AGG_FUNC.get(pigUdfClassName);
+      if (sqlAggFunction == null) {
+        final Class udfClass =
+            ((ScalarFunctionImpl) pigUdf.getFunction()).method.getDeclaringClass();
+        if (Accumulator.class.isAssignableFrom(udfClass)) {
+          throw new UnsupportedOperationException(
+              "Cannot find corresponding SqlAgg func for Pig aggegate " + pigUdfClassName);
+        }
+      }
+      return sqlAggFunction;
+    }
+    return null;
+  }
+}
+
+// End PigRelUdfConverter.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigTable.java b/piglet/src/main/java/org/apache/calcite/piglet/PigTable.java
new file mode 100644
index 0000000..ba55ab5
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigTable.java
@@ -0,0 +1,76 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.calcite.DataContext;
+import org.apache.calcite.linq4j.Enumerable;
+import org.apache.calcite.linq4j.tree.Expressions;
+import org.apache.calcite.plan.RelOptSchema;
+import org.apache.calcite.plan.RelOptTable;
+import org.apache.calcite.prepare.RelOptTableImpl;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.ScannableTable;
+import org.apache.calcite.schema.Statistic;
+import org.apache.calcite.schema.Statistics;
+import org.apache.calcite.schema.impl.AbstractTable;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+
+/**
+ * A non-queriable table that contains only row type to represent a Pig Table. This table is used
+ * for constructing Calcite logical plan from Pig DAG.
+ */
+public class PigTable extends AbstractTable implements ScannableTable {
+  // Dummy statistics with 10 rows for any table
+  private static final Statistic DUMMY_STATISTICS = Statistics.of(10.0, ImmutableList.of());
+  private final RelDataType rowType;
+
+  private PigTable(RelDataType rowType) {
+    this.rowType = rowType;
+  }
+
+  /**
+   * Creates a {@link RelOptTable} for a schema only table.
+   *
+   * @param schema Catalog object
+   * @param rowType Relational schema for the table
+   * @param names Names of Pig table
+   */
+  public static RelOptTable createRelOptTable(RelOptSchema schema,
+      RelDataType rowType, List<String> names) {
+    final PigTable pigTable = new PigTable(rowType);
+    return RelOptTableImpl.create(schema, rowType, names, pigTable,
+        Expressions.constant(Boolean.TRUE));
+  }
+
+  @Override public RelDataType getRowType(final RelDataTypeFactory typeFactory) {
+    return rowType;
+  }
+
+  @Override public Statistic getStatistic() {
+    return DUMMY_STATISTICS;
+  }
+
+  @Override public Enumerable<Object[]> scan(final DataContext root) {
+    return null;
+  }
+}
+
+// End PigTable.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigToSqlAggregateRule.java b/piglet/src/main/java/org/apache/calcite/piglet/PigToSqlAggregateRule.java
new file mode 100644
index 0000000..91ba04c
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigToSqlAggregateRule.java
@@ -0,0 +1,408 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.RelFactories;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexFieldAccess;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexLiteral;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexShuttle;
+import org.apache.calcite.rex.RexVisitorImpl;
+import org.apache.calcite.sql.SqlAggFunction;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.tools.RelBuilderFactory;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.calcite.piglet.PigTypes.TYPE_FACTORY;
+
+/**
+ * Planner rule that converts Pig aggregate UDF calls to built-in SQL
+ * aggregates.
+ *
+ * <p>This rule is applied for logical relational algebra plan that is
+ * the result of Pig translation. In Pig, aggregate calls are separate
+ * from grouping where we create a bag of all tuples in each group
+ * first then apply the Pig aggregate UDF later.  It is inefficient to
+ * do that in SQL.
+ */
+public class PigToSqlAggregateRule extends RelOptRule {
+  private static final String MULTISET_PROJECTION = "MULTISET_PROJECTION";
+
+  public static final PigToSqlAggregateRule INSTANCE =
+      new PigToSqlAggregateRule(RelFactories.LOGICAL_BUILDER);
+
+  private PigToSqlAggregateRule(RelBuilderFactory relBuilderFactory) {
+    super(
+        operand(Project.class,
+            operand(Project.class,
+                operand(Aggregate.class,
+                    operand(Project.class, any())))),
+        relBuilderFactory,
+        "PigToSqlAggregateRule");
+  }
+
+  /**
+   * Visitor that finds all Pig aggregate UDFs or multiset
+   * projection called in an expression and also whether a column is
+   * referred in that expression.
+   */
+  private class PigAggUdfFinder extends RexVisitorImpl<Void> {
+    // Index of the column
+    private final int projCol;
+    // List of all Pig aggregate UDFs found in the expression
+    private final List<RexCall> pigAggCalls;
+    // True iff the column is referred in the expression
+    private boolean projColReferred;
+    // True to ignore multiset projection inside a PigUDF
+    private boolean ignoreMultisetProj = false;
+
+    PigAggUdfFinder(int projCol) {
+      super(true);
+      this.projCol = projCol;
+      pigAggCalls = new ArrayList<>();
+      projColReferred = false;
+    }
+
+    @Override public Void visitCall(RexCall call) {
+      if (PigRelUdfConverter.getSqlAggFuncForPigUdf(call) != null) {
+        pigAggCalls.add(call);
+        ignoreMultisetProj = true;
+      } else if (isMultisetProjection(call) && !ignoreMultisetProj) {
+        pigAggCalls.add(call);
+      }
+      for (RexNode operand : call.operands) {
+        operand.accept(this);
+      }
+      return null;
+    }
+
+    @Override public Void visitInputRef(RexInputRef inputRef) {
+      if (inputRef.getIndex() == projCol) {
+        projColReferred = true;
+      }
+      return null;
+    }
+  }
+
+  /**
+   * Helper class to replace each {@link RexCall} by a corresponding
+   * {@link RexNode}, defined in a given map, for an expression.
+   *
+   * <p>It also replaces a projection by a new projection.
+   */
+  private class RexCallReplacer extends RexShuttle {
+    private final Map<RexNode, RexNode> replacementMap;
+    private final RexBuilder builder;
+    private final int oldProjCol;
+    private final RexNode newProjectCol;
+
+    RexCallReplacer(RexBuilder builder, Map<RexNode, RexNode> replacementMap,
+        int oldProjCol, RexNode newProjectCol) {
+      this.replacementMap = replacementMap;
+      this.builder = builder;
+      this.oldProjCol = oldProjCol;
+      this.newProjectCol = newProjectCol;
+    }
+
+    RexCallReplacer(RexBuilder builder, Map<RexNode, RexNode> replacementMap) {
+      this(builder, replacementMap, -1, null);
+    }
+
+    @Override public RexNode visitCall(RexCall call) {
+      if (replacementMap.containsKey(call)) {
+        return replacementMap.get(call);
+      }
+
+      List<RexNode> newOperands = new ArrayList<>();
+      for (RexNode operand : call.operands) {
+        if (replacementMap.containsKey(operand)) {
+          newOperands.add(replacementMap.get(operand));
+        } else {
+          newOperands.add(operand.accept(this));
+        }
+      }
+      return builder.makeCall(call.type, call.op, newOperands);
+    }
+
+    @Override public RexNode visitInputRef(RexInputRef inputRef) {
+      if (inputRef.getIndex() == oldProjCol
+          && newProjectCol != null
+          && inputRef.getType() == newProjectCol.getType()) {
+        return newProjectCol;
+      }
+      return inputRef;
+    }
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final Project oldTopProject = call.rel(0);
+    final Project oldMiddleProject = call.rel(1);
+    final Aggregate oldAgg = call.rel(2);
+    final Project oldBottomProject = call.rel(3);
+    final RelBuilder relBuilder = call.builder();
+
+    if (oldAgg.getAggCallList().size() != 1
+        || oldAgg.getAggCallList().get(0).getAggregation().getKind() != SqlKind.COLLECT) {
+      // Prevent the rule to be re-applied. Nothing to do here
+      return;
+    }
+
+    // Step 0: Find all target Pig aggregate UDFs to rewrite
+    final List<RexCall> pigAggUdfs = new ArrayList<>();
+    // Whether we need to keep the grouping aggregate call in the new aggregate
+    boolean needGoupingCol = false;
+    for (RexNode rex : oldTopProject.getProjects()) {
+      PigAggUdfFinder udfVisitor = new PigAggUdfFinder(1);
+      rex.accept(udfVisitor);
+      if (!udfVisitor.pigAggCalls.isEmpty()) {
+        for (RexCall pigAgg : udfVisitor.pigAggCalls) {
+          if (!pigAggUdfs.contains(pigAgg)) {
+            pigAggUdfs.add(pigAgg);
+          }
+        }
+      } else if (udfVisitor.projColReferred) {
+        needGoupingCol = true;
+      }
+    }
+
+
+    // Step 1 Build new bottom project
+    final List<RexNode> newBottomProjects = new ArrayList<>();
+    relBuilder.push(oldBottomProject.getInput());
+    // First project all group keys, just copy from old one
+    for (int i = 0; i < oldAgg.getGroupCount(); i++) {
+      newBottomProjects.add(oldBottomProject.getChildExps().get(i));
+    }
+    // If grouping aggregate is needed, project the whole ROW
+    if (needGoupingCol) {
+      final RexNode row = relBuilder.getRexBuilder().makeCall(relBuilder.peek().getRowType(),
+          SqlStdOperatorTable.ROW, relBuilder.fields());
+      newBottomProjects.add(row);
+    }
+    final int groupCount = oldAgg.getGroupCount() + (needGoupingCol ? 1 : 0);
+
+    // Now figure out which columns need to be projected for Pig UDF aggregate calls
+    // We need to project these columns for the new aggregate
+
+    // This is a map from old index to new index
+    final Map<Integer, Integer> projectedAggColumns = new HashMap<>();
+    for (int i = 0; i < newBottomProjects.size(); i++) {
+      if (newBottomProjects.get(i) instanceof RexInputRef) {
+        projectedAggColumns.put(((RexInputRef) newBottomProjects.get(i)).getIndex(), i);
+      }
+    }
+    // Build a map of each agg call to a list of columns in the new projection for later use
+    final Map<RexCall, List<Integer>> aggCallColumns = new HashMap<>();
+    for (RexCall rexCall : pigAggUdfs) {
+      // Get columns in old projection required for the agg call
+      final List<Integer> requiredColumns = getAggColumns(rexCall);
+      // And map it to columns of new projection
+      final List<Integer> newColIndexes = new ArrayList<>();
+      for (int col : requiredColumns) {
+        Integer newCol = projectedAggColumns.get(col);
+        if (newCol != null) {
+          // The column has been projected before
+          newColIndexes.add(newCol);
+        } else {
+          // Add it to the projection list if we never project it before
+          // First get the ROW operator call
+          final RexCall rowCall = (RexCall) oldBottomProject.getChildExps()
+                                                .get(oldAgg.getGroupCount());
+          // Get the corresponding column index in parent rel through the call operand list
+          final RexInputRef columRef = (RexInputRef) rowCall.getOperands().get(col);
+          final int newIndex = newBottomProjects.size();
+          newBottomProjects.add(columRef);
+          projectedAggColumns.put(columRef.getIndex(), newIndex);
+          newColIndexes.add(newIndex);
+
+        }
+      }
+      aggCallColumns.put(rexCall, newColIndexes);
+    }
+    // Now do the projection
+    relBuilder.project(newBottomProjects);
+
+    // Step 2 build new Aggregate
+    // Copy the group key
+    final RelBuilder.GroupKey groupKey = relBuilder.groupKey(oldAgg.getGroupSet(),
+        oldAgg.groupSets);
+    // The construct the agg call list
+    final List<RelBuilder.AggCall> aggCalls = new ArrayList<>();
+    if (needGoupingCol) {
+      aggCalls.add(
+          relBuilder.aggregateCall(SqlStdOperatorTable.COLLECT,
+              relBuilder.field(groupCount - 1)));
+    }
+    for (RexCall rexCall : pigAggUdfs) {
+      final List<RexNode> aggOperands = new ArrayList<>();
+      for (int i : aggCallColumns.get(rexCall)) {
+        aggOperands.add(relBuilder.field(i));
+      }
+      if (isMultisetProjection(rexCall)) {
+        if (aggOperands.size() == 1) {
+          // Project single column
+          aggCalls.add(
+              relBuilder.aggregateCall(SqlStdOperatorTable.COLLECT,
+                  aggOperands));
+        } else {
+          // Project more than one column, need to construct a record (ROW)
+          // from them
+          final RelDataType rowType =
+              createRecordType(relBuilder, aggCallColumns.get(rexCall));
+          final RexNode row = relBuilder.getRexBuilder()
+              .makeCall(rowType, SqlStdOperatorTable.ROW, aggOperands);
+          aggCalls.add(
+              relBuilder.aggregateCall(SqlStdOperatorTable.COLLECT, row));
+        }
+      } else {
+        final SqlAggFunction udf =
+            PigRelUdfConverter.getSqlAggFuncForPigUdf(rexCall);
+        aggCalls.add(relBuilder.aggregateCall(udf, aggOperands));
+      }
+    }
+    relBuilder.aggregate(groupKey, aggCalls);
+
+    // Step 3 build new top projection
+    final RelDataType aggType = relBuilder.peek().getRowType();
+    // First construct a map from old Pig agg UDF call to a projection
+    // on new aggregate.
+    final Map<RexNode, RexNode> pigCallToNewProjections = new HashMap<>();
+    for (int i = 0; i < pigAggUdfs.size(); i++) {
+      final RexCall pigAgg = pigAggUdfs.get(i);
+      final int colIndex = i + groupCount;
+      final RelDataType fieldType = aggType.getFieldList().get(colIndex).getType();
+      final RelDataType oldFieldType = pigAgg.getType();
+      // If the data type is different, we need to do a type CAST
+      if (fieldType.equals(oldFieldType)) {
+        pigCallToNewProjections.put(pigAgg, relBuilder.field(colIndex));
+      } else {
+        pigCallToNewProjections.put(pigAgg,
+            relBuilder.getRexBuilder().makeCast(oldFieldType,
+                relBuilder.field(colIndex)));
+      }
+    }
+    // Now build all expression for the new top project
+    final List<RexNode> newTopProjects = new ArrayList<>();
+    final List<RexNode> oldUpperProjects = oldTopProject.getProjects();
+    for (RexNode rexNode : oldUpperProjects) {
+      int groupRefIndex = getGroupRefIndex(rexNode);
+      if (groupRefIndex >= 0) {
+        // project a field of the group
+        newTopProjects.add(relBuilder.field(groupRefIndex));
+      } else if (rexNode instanceof RexInputRef && ((RexInputRef) rexNode).getIndex() == 0) {
+        // project the whole group (as a record)
+        newTopProjects.add(oldMiddleProject.getProjects().get(0));
+      } else {
+        // aggregate funcs
+        RexCallReplacer replacer =
+            needGoupingCol ? new RexCallReplacer(relBuilder.getRexBuilder(),
+                pigCallToNewProjections, 1,
+                relBuilder.field(groupCount - 1))
+                : new RexCallReplacer(relBuilder.getRexBuilder(), pigCallToNewProjections);
+        newTopProjects.add(rexNode.accept(replacer));
+      }
+    }
+    // Finally make the top projection
+    relBuilder.project(newTopProjects, oldTopProject.getRowType().getFieldNames());
+
+    call.transformTo(relBuilder.build());
+  }
+
+  private static RelDataType createRecordType(RelBuilder relBuilder, List<Integer> fields) {
+    final List<String> destNames = new ArrayList<>();
+    final List<RelDataType> destTypes = new ArrayList<>();
+    for (Integer index : fields) {
+      final RelDataTypeField field = relBuilder.peek().getRowType().getFieldList().get(index);
+      destNames.add(field.getName());
+      destTypes.add(field.getType());
+    }
+    return TYPE_FACTORY.createStructType(destTypes, destNames);
+  }
+
+  private int getGroupRefIndex(RexNode rex) {
+    if (rex instanceof RexFieldAccess) {
+      final RexFieldAccess fieldAccess = (RexFieldAccess) rex;
+      if (fieldAccess.getReferenceExpr() instanceof RexInputRef) {
+        final RexInputRef inputRef = (RexInputRef) fieldAccess.getReferenceExpr();
+        if (inputRef.getIndex() == 0) {
+          // Project from 'group' column
+          return fieldAccess.getField().getIndex();
+        }
+      }
+    }
+    return -1;
+  }
+
+  /**
+   * Returns a list of columns accessed in a Pig aggregate UDF call.
+   *
+   * @param pigAggCall Pig aggregate UDF call
+   */
+  private List<Integer> getAggColumns(RexCall pigAggCall) {
+    if (isMultisetProjection(pigAggCall)) {
+      return getColsFromMultisetProjection(pigAggCall);
+    }
+
+    // The only operand should be PIG_BAG
+    assert pigAggCall.getOperands().size() == 1
+        && pigAggCall.getOperands().get(0) instanceof RexCall;
+    final RexCall pigBag = (RexCall) pigAggCall.getOperands().get(0);
+    assert pigBag.getOperands().size() == 1;
+    final RexNode pigBagInput = pigBag.getOperands().get(0);
+
+    if (pigBagInput instanceof RexCall) {
+      // Multiset-projection call
+      final RexCall multisetProjection = (RexCall) pigBagInput;
+      assert isMultisetProjection(multisetProjection);
+      return getColsFromMultisetProjection(multisetProjection);
+    }
+    return new ArrayList<>();
+  }
+
+  private List<Integer> getColsFromMultisetProjection(RexCall multisetProjection) {
+    final List<Integer> columns = new ArrayList<>();
+    assert multisetProjection.getOperands().size() >= 1;
+    for (int i = 1; i < multisetProjection.getOperands().size(); i++) {
+      final RexLiteral indexLiteral =
+          (RexLiteral) multisetProjection.getOperands().get(i);
+      columns.add(((BigDecimal) indexLiteral.getValue()).intValue());
+    }
+    return columns;
+  }
+
+  private static boolean isMultisetProjection(RexCall rexCall) {
+    return rexCall.getOperator().getName().equals(MULTISET_PROJECTION);
+  }
+}
+
+// End PigToSqlAggregateRule.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigTypes.java b/piglet/src/main/java/org/apache/calcite/piglet/PigTypes.java
new file mode 100644
index 0000000..1b98a16
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigTypes.java
@@ -0,0 +1,200 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.calcite.jdbc.JavaTypeFactoryImpl;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeSystem;
+import org.apache.calcite.sql.type.SqlTypeName;
+
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.DataType;
+import org.apache.pig.newplan.logical.relational.LogicalSchema;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Utility methods for converting Pig data types to SQL types.
+ */
+class PigTypes {
+  private PigTypes() {
+  }
+
+  private static final String PIG_TUPLE_WRAPPER = "PIG_WRAPPER";
+
+  // Specialized type factory to handle type conversion
+  static final PigRelDataTypeFactory TYPE_FACTORY =
+      new PigRelDataTypeFactory(RelDataTypeSystem.DEFAULT);
+
+  /**
+   * Type factory that produces types with the nullability when converting
+   * from Pig types. It also translates a Pig DataBag type into a multiset of
+   * objects type.
+   */
+  static class PigRelDataTypeFactory extends JavaTypeFactoryImpl {
+    private PigRelDataTypeFactory(RelDataTypeSystem typeSystem) {
+      super(typeSystem);
+    }
+
+    public RelDataType createSqlType(SqlTypeName typeName, boolean nullable) {
+      return createTypeWithNullability(super.createSqlType(typeName), nullable);
+    }
+
+    public RelDataType createStructType(List<RelDataType> typeList,
+        List<String> fieldNameList, boolean nullable) {
+      return createTypeWithNullability(
+          super.createStructType(typeList, fieldNameList), nullable);
+    }
+
+    public RelDataType createMultisetType(RelDataType type,
+        long maxCardinality, boolean nullable) {
+      return createTypeWithNullability(
+          super.createMultisetType(type, maxCardinality), nullable);
+    }
+
+    public RelDataType createMapType(RelDataType keyType,
+        RelDataType valueType, boolean nullable) {
+      return createTypeWithNullability(super.createMapType(keyType, valueType), nullable);
+    }
+
+    public RelDataType toSql(RelDataType type) {
+      if (type instanceof JavaType
+          && ((JavaType) type).getJavaClass() == DataBag.class) {
+        // We don't know the structure of each tuple inside the bag until the runtime.
+        // Thus just consider a bag as a multiset of unknown objects.
+        return createMultisetType(createSqlType(SqlTypeName.ANY, true), -1, true);
+      }
+      return super.toSql(type);
+    }
+  }
+
+  /**
+   * Converts a Pig schema field to relational type.
+   *
+   * @param pigField Pig schema field
+   * @return Relational type
+   */
+  static RelDataType convertSchemaField(LogicalSchema.LogicalFieldSchema pigField) {
+    return convertSchemaField(pigField, true);
+  }
+
+  /**
+   * Converts a Pig schema field to relational type.
+   *
+   * @param pigField Pig schema field
+   * @param nullable true if the type is nullable
+   * @return Relational type
+   */
+  static RelDataType convertSchemaField(LogicalSchema.LogicalFieldSchema pigField,
+      boolean nullable) {
+    switch (pigField.type) {
+    case DataType.UNKNOWN:
+      return TYPE_FACTORY.createSqlType(SqlTypeName.ANY, nullable);
+    case DataType.NULL:
+      return TYPE_FACTORY.createSqlType(SqlTypeName.NULL, nullable);
+    case DataType.BOOLEAN:
+      return TYPE_FACTORY.createSqlType(SqlTypeName.BOOLEAN, nullable);
+    case DataType.BYTE:
+      return TYPE_FACTORY.createSqlType(SqlTypeName.TINYINT, nullable);
+    case DataType.INTEGER:
+      return TYPE_FACTORY.createSqlType(SqlTypeName.INTEGER, nullable);
+    case DataType.LONG:
+      return TYPE_FACTORY.createSqlType(SqlTypeName.BIGINT, nullable);
+    case DataType.FLOAT:
+      return TYPE_FACTORY.createSqlType(SqlTypeName.FLOAT, nullable);
+    case DataType.DOUBLE:
+      return TYPE_FACTORY.createSqlType(SqlTypeName.DOUBLE, nullable);
+    case DataType.DATETIME:
+      return TYPE_FACTORY.createSqlType(SqlTypeName.DATE, nullable);
+    case DataType.BYTEARRAY:
+      return TYPE_FACTORY.createSqlType(SqlTypeName.BINARY, nullable);
+    case DataType.CHARARRAY:
+      return TYPE_FACTORY.createSqlType(SqlTypeName.VARCHAR, nullable);
+    case DataType.BIGINTEGER:
+    case DataType.BIGDECIMAL:
+      return TYPE_FACTORY.createSqlType(SqlTypeName.DECIMAL, nullable);
+    case DataType.TUPLE: {
+      if (pigField.alias != null && pigField.alias.equals(PIG_TUPLE_WRAPPER)) {
+        if (pigField.schema == null || pigField.schema.size() != 1) {
+          throw new IllegalArgumentException("Expect one subfield from " + pigField.schema);
+        }
+        return convertSchemaField(pigField.schema.getField(0), nullable);
+      }
+      return convertSchema(pigField.schema, nullable);
+    }
+    case DataType.MAP: {
+      final RelDataType relKey = TYPE_FACTORY.createSqlType(SqlTypeName.VARCHAR);
+      if (pigField.schema == null) {
+        // The default type of Pig Map value is bytearray
+        return TYPE_FACTORY.createMapType(relKey,
+            TYPE_FACTORY.createSqlType(SqlTypeName.BINARY), nullable);
+      } else {
+        assert pigField.schema.size() == 1;
+        return TYPE_FACTORY.createMapType(relKey,
+            convertSchemaField(pigField.schema.getField(0), nullable), nullable);
+      }
+    }
+    case DataType.BAG: {
+      if (pigField.schema == null) {
+        return TYPE_FACTORY.createMultisetType(TYPE_FACTORY.createSqlType(SqlTypeName.ANY, true),
+            -1, true);
+      }
+      assert pigField.schema.size() == 1;
+      return TYPE_FACTORY.createMultisetType(
+          convertSchemaField(pigField.schema.getField(0), nullable), -1, nullable);
+    }
+    default:
+      throw new IllegalArgumentException(
+          "Unsupported conversion for Pig Data type: "
+              + DataType.findTypeName(pigField.type));
+    }
+  }
+
+  /**
+   * Converts a Pig tuple schema to a SQL row type.
+   *
+   * @param pigSchema Pig tuple schema
+   * @return a SQL row type
+   */
+  static RelDataType convertSchema(LogicalSchema pigSchema) {
+    return convertSchema(pigSchema, true);
+  }
+
+  /**
+   * Converts a Pig tuple schema to a SQL row type.
+   *
+   * @param pigSchema Pig tuple schema
+   * @param nullable true if the type is nullable
+   * @return a SQL row type
+   */
+  static RelDataType convertSchema(LogicalSchema pigSchema, boolean nullable) {
+    if (pigSchema != null && pigSchema.size() > 0) {
+      List<String> fieldNameList = new ArrayList<>();
+      List<RelDataType> typeList = new ArrayList<>();
+      for (int i = 0; i < pigSchema.size(); i++) {
+        final LogicalSchema.LogicalFieldSchema subPigField = pigSchema.getField(i);
+        fieldNameList.add(subPigField.alias != null ? subPigField.alias : "$" + i);
+        typeList.add(convertSchemaField(subPigField, nullable));
+      }
+      return TYPE_FACTORY.createStructType(typeList, fieldNameList, nullable);
+    }
+    return new DynamicTupleRecordType(TYPE_FACTORY);
+  }
+}
+
+// End PigTypes.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigUdfFinder.java b/piglet/src/main/java/org/apache/calcite/piglet/PigUdfFinder.java
new file mode 100644
index 0000000..b2c4a9d
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigUdfFinder.java
@@ -0,0 +1,112 @@
+/*
+ * 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.calcite.piglet;
+
+import com.google.common.collect.ImmutableMap;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import javax.annotation.Nonnull;
+
+/**
+ * Utility class to find the implementation method object for a given Pig UDF
+ * class.
+ */
+class PigUdfFinder {
+  /**
+   * For Pig UDF classes where the "exec" method is declared in parent class,
+   * the Calcite enumerable engine will generate incorrect Java code that
+   * instantiates an object of the parent class, not object of the actual UDF
+   * class. If the parent class is an abstract class, the auto-generated code
+   * failed to compile (we can not instantiate an object of an abstract class).
+   *
+   * <p>Workaround is to write a wrapper for such UDFs to instantiate the
+   * correct UDF object. See method {@link PigUdfs#bigdecimalsum} as an example
+   * and add others if needed.
+   */
+  private final ImmutableMap<String, Method> udfWrapper;
+
+  PigUdfFinder() {
+    final Map<String, Method> map = new HashMap<>();
+    for (Method method : PigUdfs.class.getMethods()) {
+      if (Modifier.isPublic(method.getModifiers())
+          && method.getReturnType() != Method.class) {
+        map.put(method.getName(), method);
+      }
+    }
+    udfWrapper = ImmutableMap.copyOf(map);
+  }
+
+  /**
+   * Finds the implementation method object for a given Pig UDF class.
+   *
+   * @param clazz The Pig UDF class
+   *
+   * @throws IllegalArgumentException if not found
+   */
+  @Nonnull Method findPigUdfImplementationMethod(Class clazz) {
+    // Find implementation method in the wrapper map
+    Method returnedMethod =
+        udfWrapper.get(clazz.getSimpleName().toLowerCase(Locale.US));
+    if (returnedMethod != null) {
+      return returnedMethod;
+    }
+
+    // Find exec method in the declaring class
+    returnedMethod = findExecMethod(clazz.getDeclaredMethods());
+    if (returnedMethod != null) {
+      return returnedMethod;
+    }
+
+    // Find exec method in all parent classes.
+    returnedMethod = findExecMethod(clazz.getMethods());
+    if (returnedMethod != null) {
+      return returnedMethod;
+    }
+
+    throw new IllegalArgumentException(
+        "Could not find 'exec' method for PigUDF class of " + clazz.getName());
+  }
+
+  /**
+   * Finds "exec" method from a given array of methods.
+   */
+  private static Method findExecMethod(Method[] methods) {
+    if (methods == null) {
+      return null;
+    }
+
+    Method returnedMethod = null;
+    for (Method method : methods) {
+      if (method.getName().equals("exec")) {
+        // There may be two methods named "exec", one of them just returns a
+        // Java object. We will need to look for the other one if existing.
+        if (method.getReturnType() != Object.class) {
+          return method;
+        } else {
+          returnedMethod = method;
+        }
+      }
+    }
+    return returnedMethod;
+  }
+}
+
+// End PigUdfFinder.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigUdfs.java b/piglet/src/main/java/org/apache/calcite/piglet/PigUdfs.java
new file mode 100644
index 0000000..935d994
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigUdfs.java
@@ -0,0 +1,47 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.pig.builtin.BigDecimalMax;
+import org.apache.pig.builtin.BigDecimalSum;
+import org.apache.pig.data.Tuple;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+
+/**
+ * Implementation methods.
+ *
+ * <p>Found by {@link PigUdfFinder} using reflection.
+ */
+public class PigUdfs {
+  private PigUdfs() {}
+
+  public static BigDecimal bigdecimalsum(Tuple input) throws IOException {
+    // "exec" method is declared in the parent class of
+    // AlgebraicBigDecimalMathBase
+    return new BigDecimalSum().exec(input);
+  }
+
+  public static BigDecimal bigdecimalmax(Tuple input) throws IOException {
+    // "exec" method is declared in the parent class of
+    // AlgebraicBigDecimalMathBase
+    return new BigDecimalMax().exec(input);
+  }
+}
+
+// End PigUdfs.java
diff --git a/piglet/src/main/java/org/apache/calcite/piglet/PigUserDefinedFunction.java b/piglet/src/main/java/org/apache/calcite/piglet/PigUserDefinedFunction.java
new file mode 100644
index 0000000..59e5681
--- /dev/null
+++ b/piglet/src/main/java/org/apache/calcite/piglet/PigUserDefinedFunction.java
@@ -0,0 +1,78 @@
+/*
+ * 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.calcite.piglet;
+
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.schema.Function;
+import org.apache.calcite.sql.SqlFunctionCategory;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.type.SqlOperandTypeChecker;
+import org.apache.calcite.sql.type.SqlOperandTypeInference;
+import org.apache.calcite.sql.type.SqlReturnTypeInference;
+import org.apache.calcite.sql.validate.SqlUserDefinedFunction;
+
+import org.apache.pig.FuncSpec;
+
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+
+/**
+ * Class to represent Pig UDF objects
+ */
+public class PigUserDefinedFunction extends SqlUserDefinedFunction {
+  public final FuncSpec funcSpec;
+  private PigUserDefinedFunction(SqlIdentifier opName,
+      SqlReturnTypeInference returnTypeInference,
+      SqlOperandTypeInference operandTypeInference,
+      SqlOperandTypeChecker operandTypeChecker,
+      List<RelDataType> paramTypes,
+      Function function,
+      FuncSpec funcSpec) {
+    super(opName, returnTypeInference, operandTypeInference, operandTypeChecker, paramTypes,
+        function,
+        SqlFunctionCategory.USER_DEFINED_CONSTRUCTOR);
+    this.funcSpec = funcSpec;
+  }
+
+  public PigUserDefinedFunction(String name,
+      SqlReturnTypeInference returnTypeInference,
+      SqlOperandTypeChecker operandTypeChecker,
+      List<RelDataType> paramTypes,
+      Function function,
+      FuncSpec funcSpec) {
+    this(new SqlIdentifier(ImmutableList.of(name), SqlParserPos.ZERO),
+        returnTypeInference,
+        null,
+        operandTypeChecker,
+        paramTypes,
+        function,
+        funcSpec);
+  }
+
+  public PigUserDefinedFunction(String name,
+      SqlReturnTypeInference returnTypeInference,
+      SqlOperandTypeChecker operandTypeChecker,
+      List<RelDataType> paramTypes,
+      Function function) {
+    this(name, returnTypeInference, operandTypeChecker, paramTypes, function, null);
+  }
+
+}
+
+// End PigUserDefinedFunction.java
diff --git a/piglet/src/test/java/org/apache/calcite/test/CalciteHandler.java b/piglet/src/test/java/org/apache/calcite/test/CalciteHandler.java
index 17d993a..3edd688 100644
--- a/piglet/src/test/java/org/apache/calcite/test/CalciteHandler.java
+++ b/piglet/src/test/java/org/apache/calcite/test/CalciteHandler.java
@@ -43,15 +43,20 @@ class CalciteHandler extends Handler {
   }
 
   @Override protected void dump(RelNode rel) {
+    dump(rel, writer);
+  }
+
+  public static void dump(RelNode rel, Writer writer) {
     try (PreparedStatement preparedStatement = RelRunners.run(rel)) {
       final ResultSet resultSet = preparedStatement.executeQuery();
-      dump(resultSet, true);
+      dump(resultSet, true, new PrintWriter(writer));
     } catch (SQLException e) {
       throw TestUtil.rethrow(e);
     }
   }
 
-  private void dump(ResultSet resultSet, boolean newline) throws SQLException {
+  private static void dump(ResultSet resultSet, boolean newline, PrintWriter writer)
+      throws SQLException {
     final int columnCount = resultSet.getMetaData().getColumnCount();
     int r = 0;
     while (resultSet.next()) {
@@ -66,10 +71,10 @@ class CalciteHandler extends Handler {
         }
       } else {
         writer.print('(');
-        dumpColumn(resultSet, 1);
+        dumpColumn(resultSet, 1, writer);
         for (int i = 2; i <= columnCount; i++) {
           writer.print(',');
-          dumpColumn(resultSet, i);
+          dumpColumn(resultSet, i, writer);
         }
         if (newline) {
           writer.println(')');
@@ -84,13 +89,16 @@ class CalciteHandler extends Handler {
    *
    * @param i Column ordinal, 1-based
    */
-  private void dumpColumn(ResultSet resultSet, int i) throws SQLException {
+  private static void dumpColumn(ResultSet resultSet, int i, PrintWriter writer)
+      throws SQLException {
     final int t = resultSet.getMetaData().getColumnType(i);
     switch (t) {
     case Types.ARRAY:
       final Array array = resultSet.getArray(i);
       writer.print("{");
-      dump(array.getResultSet(), false);
+      if (array != null) {
+        dump(array.getResultSet(), false, writer);
+      }
       writer.print("}");
       return;
     case Types.REAL:
diff --git a/piglet/src/test/java/org/apache/calcite/test/PigRelExTest.java b/piglet/src/test/java/org/apache/calcite/test/PigRelExTest.java
new file mode 100644
index 0000000..b08d679
--- /dev/null
+++ b/piglet/src/test/java/org/apache/calcite/test/PigRelExTest.java
@@ -0,0 +1,250 @@
+/*
+ * 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.calcite.test;
+
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.util.TestUtil;
+
+import org.hamcrest.Matcher;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import java.io.IOException;
+
+import static org.apache.calcite.test.Matchers.inTree;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Tests for {@code PigRelExVisitor}.
+ */
+public class PigRelExTest extends PigRelTestBase {
+  private void checkTranslation(String pigExpr, Matcher<RelNode> relMatcher) {
+    String pigScript = ""
+        + "A = LOAD 'test' as (a:int, b:long, c:float, d:double,\n"
+        + "    e:chararray, f:bytearray, g:boolean, h:datetime,\n"
+        + "    i:biginteger, j:bigdecimal, k1:tuple(),\n"
+        + "    k2:tuple(k21:int, k22:(k221:long, k222:chararray)), l1:bag{},\n"
+        + "    l2:bag{(l21:int, l22:float)}, m1:map[], m2:map[int],\n"
+        + "    m3:map[(m31:float)]);\n"
+        + "B = FILTER A BY " + pigExpr + ";\n";
+    try {
+      final RelNode rel =
+          converter.pigQuery2Rel(pigScript, false, false, false).get(0);
+      assertThat(rel, relMatcher);
+    } catch (IOException e) {
+      throw TestUtil.rethrow(e);
+    }
+  }
+
+  private void checkType(String pigExpr, Matcher<String> rowTypeMatcher) {
+    String pigScript = ""
+        + "A = LOAD 'test' as (a:int);\n"
+        + "B = FOREACH A GENERATE a, " + pigExpr + ";\n";
+    try {
+      final RelNode rel =
+          converter.pigQuery2Rel(pigScript, false, false, false).get(0);
+      assertThat(rel.getRowType().toString(), rowTypeMatcher);
+    } catch (IOException e) {
+      throw TestUtil.rethrow(e);
+    }
+  }
+
+  @Test
+  public void testConstantBoolean() {
+    checkTranslation("g == false", inTree("NOT($6)"));
+  }
+
+  @Test
+  public void testConstantType() {
+    checkType("0L as longCol", containsString("BIGINT longCol"));
+    checkType("0 as intCol", containsString("INTEGER intCol"));
+    checkType("0.0 as doubleCol", containsString("DOUBLE doubleCol"));
+    checkType("'0.0' as charCol", containsString("CHAR(3) charCol"));
+    checkType("true as boolCol", containsString("BOOLEAN boolCol"));
+  }
+
+  @Test
+  public void testConstantFloat() {
+    checkTranslation(".1E6 == -2.3", inTree("=(1E5:DOUBLE, -2.3:DECIMAL(2, 1))"));
+  }
+
+  @Test
+  public void testConstantString() {
+    checkTranslation("'test' == 'passed'", inTree("=('test', 'passed')"));
+  }
+
+  @Test
+  public void testProjection() {
+    checkTranslation("g", inTree("=[$6]"));
+  }
+
+  @Test
+  public void testNegation() {
+    checkTranslation("-b == -6", inTree("=(-($1), -6)"));
+  }
+
+  @Test
+  public void testEqual() {
+    checkTranslation("a == 10", inTree("=($0, 10)"));
+  }
+
+  @Test
+  public void testNotEqual() {
+    checkTranslation("b != 10", inTree("<>($1, 10)"));
+  }
+
+  @Test
+  public void testLessThan() {
+    checkTranslation("b < 10", inTree("<($1, 10)"));
+  }
+
+  @Test
+  public void testLessThanEqual() {
+    checkTranslation("b <= 10", inTree("<=($1, 10)"));
+  }
+
+  @Test
+  public void testGreaterThan() {
+    checkTranslation("b > 10", inTree(">($1, 10)"));
+  }
+
+  @Test
+  public void testGreaterThanEqual() {
+    checkTranslation("b >= 10", inTree(">=($1, 10)"));
+  }
+
+  @Test
+  @Ignore
+  public void testMatch() {
+    checkTranslation("e matches 'A*BC.D'", inTree("LIKE($4, 'A%BC_D')"));
+  }
+
+  @Test// End PigRelExTest.java
+  public void testIsNull() {
+    checkTranslation("e is null", inTree("IS NULL($4)"));
+  }
+
+  @Test
+  public void testIsNotNull() {
+    checkTranslation("c is not null", inTree("IS NOT NULL($2)"));
+  }
+
+  @Test
+  public void testNot() {
+    checkTranslation("NOT(a is null)", inTree("IS NOT NULL($0)"));
+    checkTranslation("NOT(g)", inTree("NOT($6)"));
+  }
+
+  @Test
+  public void testAnd() {
+    checkTranslation("a > 10 and g", inTree("AND(>($0, 10), $6)"));
+  }
+
+  @Test
+  public void testOr() {
+    checkTranslation("a > 10 or g", inTree("OR(>($0, 10), $6)"));
+  }
+
+  @Test
+  public void testAdd() {
+    checkTranslation("b + 3", inTree("+($1, 3)"));
+  }
+
+  @Test
+  public void testSubtract() {
+    checkTranslation("b - 3", inTree("-($1, 3)"));
+  }
+
+  @Test
+  public void testMultiply() {
+    checkTranslation("b * 3", inTree("*($1, 3)"));
+  }
+
+  @Test
+  public void testMod() {
+    checkTranslation("b % 3", inTree("MOD($1, 3)"));
+  }
+
+  @Test
+  public void testDivide() {
+    checkTranslation("b / 3", inTree("/($1, 3)"));
+    checkTranslation("c / 3.1", inTree("/($2, 3.1E0:DOUBLE)"));
+  }
+
+  @Test
+  public void testBinCond() {
+    checkTranslation("(b == 1 ? 2 : 3)", inTree("CASE(=($1, 1), 2, 3)"));
+  }
+
+  @Test
+  public void testTupleDereference() {
+    checkTranslation("k2.k21", inTree("[$11.k21]"));
+    checkTranslation("k2.(k21, k22)", inTree("[ROW($11.k21, $11.k22)]"));
+    checkTranslation("k2.k22.(k221,k222)",
+        inTree("[ROW($11.k22.k221, $11.k22.k222)]"));
+  }
+
+  @Test
+  public void testBagDereference() {
+    checkTranslation("l2.l22", inTree("[MULTISET_PROJECTION($13, 1)]"));
+    checkTranslation("l2.(l21, l22)", inTree("[MULTISET_PROJECTION($13, 0, 1)]"));
+  }
+
+  @Test
+  public void testMapLookup() {
+    checkTranslation("m2#'testKey'", inTree("ITEM($15, 'testKey')"));
+  }
+
+  @Test
+  public void testCast() {
+    checkTranslation("(int) b", inTree("CAST($1):INTEGER"));
+    checkTranslation("(long) a", inTree("CAST($0):BIGINT"));
+    checkTranslation("(float) b", inTree("CAST($1):FLOAT"));
+    checkTranslation("(double) b", inTree("CAST($1):DOUBLE"));
+    checkTranslation("(chararray) b", inTree("CAST($1):VARCHAR"));
+    checkTranslation("(bytearray) b", inTree("CAST($1):BINARY"));
+    checkTranslation("(boolean) c", inTree("CAST($2):BOOLEAN"));
+    checkTranslation("(biginteger) b", inTree("CAST($1):DECIMAL(19, 0)"));
+    checkTranslation("(bigdecimal) b", inTree("CAST($1):DECIMAL(19, 0)"));
+    checkTranslation("(tuple()) b", inTree("CAST($1):(DynamicRecordRow[])"));
+    checkTranslation("(tuple(int, float)) b",
+        inTree("CAST($1):RecordType(INTEGER $0, FLOAT $1)"));
+    checkTranslation("(bag{}) b",
+        inTree("CAST($1):(DynamicRecordRow[]) NOT NULL MULTISET"));
+    checkTranslation("(bag{tuple(int)}) b",
+        inTree("CAST($1):RecordType(INTEGER $0) MULTISET"));
+    checkTranslation("(bag{tuple(int, float)}) b",
+        inTree("CAST($1):RecordType(INTEGER $0, FLOAT $1) MULTISET"));
+    checkTranslation("(map[]) b",
+        inTree("CAST($1):(VARCHAR NOT NULL, BINARY(1) NOT NULL) MAP"));
+    checkTranslation("(map[int]) b", inTree("CAST($1):(VARCHAR NOT NULL, INTEGER"));
+    checkTranslation("(map[tuple(int, float)]) b",
+        inTree("CAST($1):(VARCHAR NOT NULL, RecordType(INTEGER val_0, FLOAT val_1)) MAP"));
+  }
+
+  @Test
+  public void testPigBuiltinFunctions() {
+    checkTranslation("ABS(-5)", inTree("ABS(-5)"));
+    checkTranslation("AddDuration(h, 'P1D')",
+        inTree("AddDuration(PIG_TUPLE($7, 'P1D'))"));
+    checkTranslation("CEIL(1.2)", inTree("CEIL(1.2E0:DOUBLE)"));
+  }
+}
+
+// End PigRelExTest.java
diff --git a/piglet/src/test/java/org/apache/calcite/test/PigRelOpTest.java b/piglet/src/test/java/org/apache/calcite/test/PigRelOpTest.java
new file mode 100644
index 0000000..f41729e
--- /dev/null
+++ b/piglet/src/test/java/org/apache/calcite/test/PigRelOpTest.java
@@ -0,0 +1,1631 @@
+/*
+ * 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.calcite.test;
+
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.sql.SqlDialect;
+import org.apache.calcite.sql.dialect.CalciteSqlDialect;
+import org.apache.calcite.util.Bug;
+import org.apache.calcite.util.TestUtil;
+import org.apache.calcite.util.Util;
+
+import org.hamcrest.Matcher;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+
+import static org.apache.calcite.test.Matchers.hasTree;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Tests for {@code PigRelOpVisitor}.
+ */
+public class PigRelOpTest extends PigRelTestBase {
+  /**
+   * SQL dialect for the tests.
+   */
+  private static class PigRelSqlDialect extends SqlDialect {
+    static final SqlDialect DEFAULT =
+        new CalciteSqlDialect(emptyContext()
+            .withDatabaseProduct(DatabaseProduct.CALCITE));
+
+    private PigRelSqlDialect(Context context) {
+      super(context);
+    }
+  }
+
+  /** Contains a Pig script and has various methods to translate and
+   * run that script and check the results. Each method returns
+   * this, so that method calls for the same script can be
+   * chained. */
+  class Fluent {
+    private final String script;
+
+    Fluent(String script) {
+      this.script = script;
+    }
+
+    private Fluent assertRel(String pigAlias, boolean optimized,
+        Matcher<RelNode> relMatcher) {
+      try {
+        final RelNode rel;
+        final List<RelNode> relNodes =
+            converter.pigQuery2Rel(script, optimized, true, optimized);
+        if (pigAlias == null) {
+          rel = relNodes.get(0);
+        } else {
+          rel = converter.getBuilder().getRel(pigAlias);
+        }
+        assertThat(rel, relMatcher);
+      } catch (IOException e) {
+        throw TestUtil.rethrow(e);
+      }
+      return this;
+    }
+
+    private Fluent assertRel(Matcher<RelNode> relMatcher) {
+      return assertRel(null, false, relMatcher);
+    }
+
+    private Fluent assertOptimizedRel(Matcher<RelNode> relMatcher) {
+      return assertRel(null, true, relMatcher);
+    }
+
+    private Fluent assertSql(Matcher<String> sqlMatcher) {
+      try {
+        final String sql =
+            converter.pigToSql(script, PigRelSqlDialect.DEFAULT).get(0);
+        assertThat(sql, sqlMatcher);
+        return this;
+      } catch (IOException e) {
+        throw TestUtil.rethrow(e);
+      }
+    }
+
+    private Fluent assertResult(Matcher<String> resultMatcher) {
+      final RelNode rel;
+      try {
+        rel = converter.pigQuery2Rel(script, false, true, false).get(0);
+      } catch (IOException e) {
+        throw TestUtil.rethrow(e);
+      }
+      final StringWriter sw = new StringWriter();
+      CalciteHandler.dump(rel, new PrintWriter(sw));
+      assertThat(Util.toLinux(sw.toString()), resultMatcher);
+      return this;
+    }
+  }
+
+  private static void writeToFile(File f, String[] inputData) {
+    try (PrintWriter pw =
+             new PrintWriter(
+                 new OutputStreamWriter(new FileOutputStream(f),
+                     StandardCharsets.UTF_8))) {
+      for (String input : inputData) {
+        pw.print(input);
+        pw.print("\n");
+      }
+    } catch (FileNotFoundException e) {
+      throw TestUtil.rethrow(e);
+    }
+  }
+
+  /** Creates a {@link Fluent} containing a script, that can then be used to
+   * translate and execute that script. */
+  private Fluent pig(String script) {
+    return new Fluent(script);
+  }
+
+  @Test public void testLoadFromFile() {
+    final String datadir = "/tmp/pigdata";
+    final String schema = "{\"fields\":["
+        + "{\"name\":\"x\",\"type\":55,\"schema\":null},"
+        + "{\"name\":\"y\",\"type\":10,\"schema\":null},"
+        + "{\"name\":\"z\",\"type\":25,\"schema\":null}],"
+        + "\"version\":0,\"sortKeys\":[],\"sortKeyOrders\":[]}";
+    final File inputDir = new File(datadir, "testTable");
+    inputDir.mkdirs();
+    final File inputSchemaFile = new File(inputDir, ".pig_schema");
+    writeToFile(inputSchemaFile, new String[]{schema});
+
+    final String script = ""
+        + "A = LOAD '" + inputDir.getAbsolutePath() + "' using PigStorage();\n"
+        + "B = FILTER A BY z > 5.5;\n"
+        + "C = GROUP B BY x;\n";
+    final String plan = ""
+        + "LogicalProject(group=[$0], B=[$1])\n"
+        + "  LogicalAggregate(group=[{0}], B=[COLLECT($1)])\n"
+        + "    LogicalProject(x=[$0], $f1=[ROW($0, $1, $2)])\n"
+        + "      LogicalFilter(condition=[>($2, 5.5E0)])\n"
+        + "        LogicalTableScan(table=[[/tmp/pigdata/testTable]])\n";
+    pig(script).assertRel(hasTree(plan));
+  }
+
+  @Test public void testLoadWithoutSchema() {
+    final String script = "A = LOAD 'scott.DEPT';";
+    final String plan = "LogicalTableScan(table=[[scott, DEPT]])\n";
+    final String result = ""
+        + "(10,ACCOUNTING,NEW YORK)\n"
+        + "(20,RESEARCH,DALLAS)\n"
+        + "(30,SALES,CHICAGO)\n"
+        + "(40,OPERATIONS,BOSTON)\n";
+    pig(script).assertRel(hasTree(plan))
+        .assertResult(is(result));
+  }
+
+  @Test public void testLoadWithSchema() {
+    final String script = ""
+        + "A = LOAD 'testSchema.testTable' as (a:int, b:long, c:float, "
+        + "d:double, e:chararray, "
+        + "f:bytearray, g:boolean, "
+        + "h:datetime, i:biginteger, j:bigdecimal, k1:tuple(), k2:tuple"
+        + "(k21:int, k22:float), "
+        + "l1:bag{}, "
+        + "l2:bag{l21:(l22:int, l23:float)}, m1:map[], m2:map[int], m3:map["
+        + "(m3:float)])\n;";
+    final String plan = "LogicalTableScan(table=[[testSchema, testTable]])\n";
+    pig(script).assertRel(hasTree(plan));
+
+    final String script1 =
+        "A = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray, LOC:CHARARRAY);";
+    pig(script1)
+        .assertRel(hasTree("LogicalTableScan(table=[[scott, DEPT]])\n"));
+  }
+
+  @Test public void testFilter() {
+    final String script = ""
+        + "A = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray, LOC:CHARARRAY);\n"
+        + "B = FILTER A BY DEPTNO == 10;\n";
+    final String plan = ""
+        + "LogicalFilter(condition=[=($0, 10)])\n"
+        + "  LogicalTableScan(table=[[scott, DEPT]])\n";
+    final String result = "(10,ACCOUNTING,NEW YORK)\n";
+    final String sql = "SELECT *\n"
+        + "FROM scott.DEPT\n"
+        + "WHERE DEPTNO = 10";
+    pig(script).assertRel(hasTree(plan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testSample() {
+    final String script = ""
+        + "A = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray, LOC:CHARARRAY);\n"
+        + "B = SAMPLE A 0.5;\n";
+    final String plan = ""
+        + "LogicalFilter(condition=[<(RAND(), 5E-1)])\n"
+        + "  LogicalTableScan(table=[[scott, DEPT]])\n";
+    final String sql = ""
+        + "SELECT *\n"
+        + "FROM scott.DEPT\n"
+        + "WHERE RAND() < 0.5";
+    pig(script).assertRel(hasTree(plan))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testSplit() {
+    String script = ""
+        + "A = LOAD 'scott.EMP'as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "SPLIT A INTO B1 IF DEPTNO == 10, B2 IF  DEPTNO == 20;\n"
+        + "B = UNION B1, B2;\n";
+    final String scan = "  LogicalTableScan(table=[[scott, EMP]])\n";
+    final String plan = ""
+        + "LogicalUnion(all=[true])\n"
+        + "  LogicalFilter(condition=[=($7, 10)])\n"
+        + "    LogicalTableScan(table=[[scott, EMP]])\n"
+        + "  LogicalFilter(condition=[=($7, 20)])\n"
+        + "    LogicalTableScan(table=[[scott, EMP]])\n";
+
+    final String result = ""
+        + "(7782,CLARK,MANAGER,7839,1981-06-09,2450.00,null,10)\n"
+        + "(7839,KING,PRESIDENT,null,1981-11-17,5000.00,null,10)\n"
+        + "(7934,MILLER,CLERK,7782,1982-01-23,1300.00,null,10)\n"
+        + "(7369,SMITH,CLERK,7902,1980-12-17,800.00,null,20)\n"
+        + "(7566,JONES,MANAGER,7839,1981-02-04,2975.00,null,20)\n"
+        + "(7788,SCOTT,ANALYST,7566,1987-04-19,3000.00,null,20)\n"
+        + "(7876,ADAMS,CLERK,7788,1987-05-23,1100.00,null,20)\n"
+        + "(7902,FORD,ANALYST,7566,1981-12-03,3000.00,null,20)\n";
+
+    final String sql = ""
+        + "SELECT *\n"
+        + "  FROM scott.EMP\n"
+        + "  WHERE DEPTNO = 10\n"
+        + "UNION ALL\n"
+        + "  SELECT *\n"
+        + "  FROM scott.EMP\n"
+        + "  WHERE DEPTNO = 20";
+    pig(script)
+        .assertRel("B1", false,
+            hasTree("LogicalFilter(condition=[=($7, 10)])\n"
+              + scan))
+        .assertRel("B2", false,
+            hasTree("LogicalFilter(condition=[=($7, 20)])\n"
+              + scan))
+        .assertRel(hasTree(plan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testUdf() {
+    final String script = ""
+        + "A = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray, LOC:CHARARRAY);\n"
+        + "B = FILTER A BY ENDSWITH(DNAME, 'LES');\n";
+    final String plan = ""
+        + "LogicalFilter(condition=[ENDSWITH(PIG_TUPLE($1, 'LES'))])\n"
+        + "  LogicalTableScan(table=[[scott, DEPT]])\n";
+    final String result = "(30,SALES,CHICAGO)\n";
+    final String sql = ""
+        + "SELECT *\n"
+        + "FROM scott.DEPT\n"
+        + "WHERE ENDSWITH(PIG_TUPLE(DNAME, 'LES'))";
+    pig(script).assertRel(hasTree(plan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testSimpleForEach1() {
+    String script = ""
+        + "A = LOAD 'testSchema.testTable' as (a:int, b:long, c:float, "
+        + "d:double, e:chararray, f:bytearray, g:boolean, "
+        + "h:datetime, i:biginteger, j:bigdecimal, k1:tuple(), "
+        + "k2:tuple(k21:int, k22:float), l1:bag{}, "
+        + "l2:bag{l21:(l22:int, l23:float)}, "
+        + "m1:map[], m2:map[int], m3:map[(m3:float)]);\n"
+        + "B = FOREACH A GENERATE a, a as a2, b, c, d, e, f, g, h, i, j, k2, "
+        + "l2, m2, null as n:chararray; \n";
+    final String plan = ""
+        + "LogicalProject(a=[$0], a2=[$0], b=[$1], c=[$2], d=[$3], e=[$4], "
+        + "f=[$5], g=[$6], h=[$7], i=[$8], j=[$9], k2=[$11], l2=[$13], "
+        + "m2=[$15], n=[null:VARCHAR])\n"
+        + "  LogicalTableScan(table=[[testSchema, testTable]])\n";
+    final String sql = ""
+        + "SELECT a, a AS a2, b, c, d, e, f, g, h, i, j, k2, l2, m2, "
+        + "CAST(NULL AS VARCHAR CHARACTER SET ISO-8859-1) AS n\n"
+        + "FROM testSchema.testTable";
+    pig(script).assertRel(hasTree(plan))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testSimpleForEach2() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = FOREACH A GENERATE DEPTNO + 10 as dept, MGR;\n";
+    final String plan = ""
+        + "LogicalProject(dept=[+($7, 10)], MGR=[$3])\n"
+        + "  LogicalTableScan(table=[[scott, EMP]])\n";
+
+    final String result = ""
+        + "(30,7902)\n"
+        + "(40,7698)\n"
+        + "(40,7698)\n"
+        + "(30,7839)\n"
+        + "(40,7698)\n"
+        + "(40,7839)\n"
+        + "(20,7839)\n"
+        + "(30,7566)\n"
+        + "(20,null)\n"
+        + "(40,7698)\n"
+        + "(30,7788)\n"
+        + "(40,7698)\n"
+        + "(30,7566)\n"
+        + "(20,7782)\n";
+    final String sql = ""
+        + "SELECT DEPTNO + 10 AS dept, MGR\n"
+        + "FROM scott.EMP";
+    pig(script).assertRel(hasTree(plan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testSimpleForEach3() {
+    String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = FILTER A BY JOB != 'CLERK';\n"
+        + "C = GROUP B BY (DEPTNO, JOB);\n"
+        + "D = FOREACH C GENERATE flatten(group) as (dept, job), flatten(B);\n"
+        + "E = ORDER D BY dept, job;\n";
+    final String plan = ""
+        + "LogicalSort(sort0=[$0], sort1=[$1], dir0=[ASC], dir1=[ASC])\n"
+        + "  LogicalProject(dept=[$0], job=[$1], EMPNO=[$3], ENAME=[$4], "
+        + "JOB=[$5], MGR=[$6], HIREDATE=[$7], SAL=[$8], COMM=[$9], "
+        + "DEPTNO=[$10])\n"
+        + "    LogicalCorrelate(correlation=[$cor0], joinType=[inner], "
+        + "requiredColumns=[{2}])\n"
+        + "      LogicalProject(dept=[$0.DEPTNO], job=[$0.JOB], B=[$1])\n"
+        + "        LogicalProject(group=[ROW($0, $1)], B=[$2])\n"
+        + "          LogicalAggregate(group=[{0, 1}], B=[COLLECT($2)])\n"
+        + "            LogicalProject(DEPTNO=[$7], JOB=[$2], $f2=[ROW($0, "
+        + "$1, $2, $3, $4, $5, $6, $7)])\n"
+        + "              LogicalFilter(condition=[<>($2, 'CLERK')])\n"
+        + "                LogicalTableScan(table=[[scott, EMP]])\n"
+        + "      Uncollect\n"
+        + "        LogicalProject($f0=[$cor0.B])\n"
+        + "          LogicalValues(tuples=[[{ 0 }]])\n";
+
+    final String sql = ""
+        + "SELECT $cor1.DEPTNO AS dept, $cor1.JOB AS job, $cor1.EMPNO,"
+        + " $cor1.ENAME, $cor1.JOB0 AS JOB, $cor1.MGR, $cor1.HIREDATE,"
+        + " $cor1.SAL, $cor1.COMM, $cor1.DEPTNO0 AS DEPTNO\n"
+        + "FROM (SELECT DEPTNO, JOB, COLLECT(ROW(EMPNO, ENAME, JOB, MGR, "
+        + "HIREDATE, SAL, COMM, DEPTNO)) AS $f2\n"
+        + "      FROM scott.EMP\n"
+        + "      WHERE JOB <> 'CLERK'\n"
+        + "      GROUP BY DEPTNO, JOB) AS $cor1,\n"
+        + "    LATERAL UNNEST (SELECT $cor1.$f2 AS $f0\n"
+        + "        FROM (VALUES  (0)) AS t (ZERO)) AS t3 (EMPNO, ENAME, JOB,"
+        + " MGR, HIREDATE, SAL, COMM, DEPTNO) AS t30\n"
+        + "ORDER BY $cor1.DEPTNO, $cor1.JOB";
+    pig(script).assertRel(hasTree(plan))
+        .assertSql(is(sql));
+
+    // TODO fix Calcite execution
+    final String result = ""
+        + "(10,7782,CLARK,MANAGER,7839,1981-06-09,2450.00,null,10)\n"
+        + "(10,7839,KING,PRESIDENT,null,1981-11-17,5000.00,null,10)\n"
+        + "(20,7566,JONES,MANAGER,7839,1981-02-04,2975.00,null,20)\n"
+        + "(20,7788,SCOTT,ANALYST,7566,1987-04-19,3000.00,null,20)\n"
+        + "(20,7902,FORD,ANALYST,7566,1981-12-03,3000.00,null,20)\n"
+        + "(30,7499,ALLEN,SALESMAN,7698,1981-02-20,1600.00,300.00,30)\n"
+        + "(30,7521,WARD,SALESMAN,7698,1981-02-22,1250.00,500.00,30)\n"
+        + "(30,7654,MARTIN,SALESMAN,7698,1981-09-28,1250.00,1400.00,30)\n"
+        + "(30,7698,BLAKE,MANAGER,7839,1981-01-05,2850.00,null,30)\n"
+        + "(30,7844,TURNER,SALESMAN,7698,1981-09-08,1500.00,0.00,30)\n";
+    if (false) {
+      pig(script).assertResult(is(result));
+    }
+  }
+
+  @Test public void testForEachNested() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = GROUP A BY DEPTNO;\n"
+        + "C = FOREACH B {\n"
+        + "      S = FILTER A BY JOB != 'CLERK';\n"
+        + "      Y = FOREACH S GENERATE ENAME, JOB, DEPTNO, SAL;\n"
+        + "      X = ORDER Y BY SAL;\n"
+        + "      GENERATE group, COUNT(X) as cnt, flatten(X), BigDecimalMax(X.SAL);\n"
+        + "}\n"
+        + "D = ORDER C BY $0;\n";
+    final String plan = ""
+        + "LogicalSort(sort0=[$0], dir0=[ASC])\n"
+        + "  LogicalProject(group=[$0], cnt=[$1], ENAME=[$4], JOB=[$5], "
+        + "DEPTNO=[$6], SAL=[$7], $f3=[$3])\n"
+        + "    LogicalCorrelate(correlation=[$cor1], joinType=[inner], "
+        + "requiredColumns=[{2}])\n"
+        + "      LogicalProject(group=[$0], cnt=[COUNT(PIG_BAG($2))], X=[$2], "
+        + "$f3=[BigDecimalMax(PIG_BAG(MULTISET_PROJECTION($2, 3)))])\n"
+        + "        LogicalCorrelate(correlation=[$cor0], joinType=[inner], "
+        + "requiredColumns=[{1}])\n"
+        + "          LogicalProject(group=[$0], A=[$1])\n"
+        + "            LogicalAggregate(group=[{0}], A=[COLLECT($1)])\n"
+        + "              LogicalProject(DEPTNO=[$7], $f1=[ROW($0, $1, $2, "
+        + "$3, $4, $5, $6, $7)])\n"
+        + "                LogicalTableScan(table=[[scott, EMP]])\n"
+        + "          LogicalProject(X=[$1])\n"
+        + "            LogicalAggregate(group=[{0}], X=[COLLECT($1)])\n"
+        + "              LogicalProject($f0=['all'], $f1=[ROW($0, $1, $2, $3)])\n"
+        + "                LogicalSort(sort0=[$3], dir0=[ASC])\n"
+        + "                  LogicalProject(ENAME=[$1], JOB=[$2], "
+        + "DEPTNO=[$7], SAL=[$5])\n"
+        + "                    LogicalFilter(condition=[<>($2, 'CLERK')])\n"
+        + "                      Uncollect\n"
+        + "                        LogicalProject($f0=[$cor0.A])\n"
+        + "                          LogicalValues(tuples=[[{ 0 }]])\n"
+        + "      Uncollect\n"
+        + "        LogicalProject($f0=[$cor1.X])\n"
+        + "          LogicalValues(tuples=[[{ 0 }]])\n";
+
+    final String result = ""
+        + "(10,2,CLARK,MANAGER,10,2450.00,5000.00)\n"
+        + "(10,2,KING,PRESIDENT,10,5000.00,5000.00)\n"
+        + "(20,3,JONES,MANAGER,20,2975.00,3000.00)\n"
+        + "(20,3,SCOTT,ANALYST,20,3000.00,3000.00)\n"
+        + "(20,3,FORD,ANALYST,20,3000.00,3000.00)\n"
+        + "(30,5,WARD,SALESMAN,30,1250.00,2850.00)\n"
+        + "(30,5,MARTIN,SALESMAN,30,1250.00,2850.00)\n"
+        + "(30,5,TURNER,SALESMAN,30,1500.00,2850.00)\n"
+        + "(30,5,ALLEN,SALESMAN,30,1600.00,2850.00)\n"
+        + "(30,5,BLAKE,MANAGER,30,2850.00,2850.00)\n";
+
+    final String sql = ""
+        + "SELECT $cor5.group, $cor5.cnt, $cor5.ENAME, $cor5.JOB, "
+        + "$cor5.DEPTNO, $cor5.SAL, $cor5.$f3\n"
+        + "FROM (SELECT $cor4.DEPTNO AS group, "
+        + "COUNT(PIG_BAG($cor4.X)) AS cnt, $cor4.X, "
+        + "BigDecimalMax(PIG_BAG(MULTISET_PROJECTION($cor4.X, 3))) AS $f3\n"
+        + "      FROM (SELECT DEPTNO, COLLECT(ROW(EMPNO, ENAME, JOB, MGR, "
+        + "HIREDATE, SAL, COMM, DEPTNO)) AS A\n"
+        + "            FROM scott.EMP\n"
+        + "            GROUP BY DEPTNO) AS $cor4,\n"
+        + "          LATERAL (SELECT COLLECT(ROW(ENAME, JOB, DEPTNO, SAL)) AS X\n"
+        + "            FROM (SELECT ENAME, JOB, DEPTNO, SAL\n"
+        + "                  FROM UNNEST (SELECT $cor4.A AS $f0\n"
+        + "                        FROM (VALUES  (0)) AS t (ZERO)) "
+        + "AS t2 (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)\n"
+        + "                  WHERE JOB <> 'CLERK'\n"
+        + "                  ORDER BY SAL) AS t5\n"
+        + "            GROUP BY 'all') AS t8) AS $cor5,\n"
+        + "    LATERAL UNNEST (SELECT $cor5.X AS $f0\n"
+        + "        FROM (VALUES  (0)) AS t (ZERO)) "
+        + "AS t11 (ENAME, JOB, DEPTNO, SAL) AS t110\n"
+        + "ORDER BY $cor5.group";
+    pig(script).assertRel(hasTree(plan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testUnionSameSchema() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = FILTER A BY DEPTNO == 10;\n"
+        + "C = FILTER A BY DEPTNO == 20;\n"
+        + "D = UNION B, C;\n";
+    final String plan = ""
+        + "LogicalUnion(all=[true])\n"
+        + "  LogicalFilter(condition=[=($7, 10)])\n"
+        + "    LogicalTableScan(table=[[scott, EMP]])\n"
+        + "  LogicalFilter(condition=[=($7, 20)])\n"
+        + "    LogicalTableScan(table=[[scott, EMP]])\n";
+    final String result = ""
+        + "(7782,CLARK,MANAGER,7839,1981-06-09,2450.00,null,10)\n"
+        + "(7839,KING,PRESIDENT,null,1981-11-17,5000.00,null,10)\n"
+        + "(7934,MILLER,CLERK,7782,1982-01-23,1300.00,null,10)\n"
+        + "(7369,SMITH,CLERK,7902,1980-12-17,800.00,null,20)\n"
+        + "(7566,JONES,MANAGER,7839,1981-02-04,2975.00,null,20)\n"
+        + "(7788,SCOTT,ANALYST,7566,1987-04-19,3000.00,null,20)\n"
+        + "(7876,ADAMS,CLERK,7788,1987-05-23,1100.00,null,20)\n"
+        + "(7902,FORD,ANALYST,7566,1981-12-03,3000.00,null,20)\n";
+    final String sql = ""
+        + "SELECT *\n"
+        + "  FROM scott.EMP\n"
+        + "  WHERE DEPTNO = 10\n"
+        + "UNION ALL\n"
+        + "  SELECT *\n"
+        + "  FROM scott.EMP\n"
+        + "  WHERE DEPTNO = 20";
+    pig(script).assertRel(hasTree(plan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testUnionDifferentSchemas1() {
+    final String script = ""
+        + "A = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray, LOC:CHARARRAY);\n"
+        + "B = FOREACH A GENERATE DEPTNO, DNAME;\n"
+        + "C = UNION ONSCHEMA A, B;\n";
+    final String plan = ""
+        + "LogicalUnion(all=[true])\n"
+        + "  LogicalTableScan(table=[[scott, DEPT]])\n"
+        + "  LogicalProject(DEPTNO=[$0], DNAME=[$1], LOC=[null:VARCHAR])\n"
+        + "    LogicalProject(DEPTNO=[$0], DNAME=[$1])\n"
+        + "      LogicalTableScan(table=[[scott, DEPT]])\n";
+    final String optimizedPlan = ""
+        + "LogicalUnion(all=[true])\n"
+        + "  LogicalTableScan(table=[[scott, DEPT]])\n"
+        + "  LogicalProject(DEPTNO=[$0], DNAME=[$1], LOC=[null:VARCHAR])\n"
+        + "    LogicalTableScan(table=[[scott, DEPT]])\n";
+    final String result = ""
+        + "(10,ACCOUNTING,NEW YORK)\n"
+        + "(20,RESEARCH,DALLAS)\n"
+        + "(30,SALES,CHICAGO)\n"
+        + "(40,OPERATIONS,BOSTON)\n"
+        + "(10,ACCOUNTING,null)\n"
+        + "(20,RESEARCH,null)\n"
+        + "(30,SALES,null)\n"
+        + "(40,OPERATIONS,null)\n";
+    final String sql = ""
+        + "SELECT *\n"
+        + "  FROM scott.DEPT\n"
+        + "UNION ALL\n"
+        + "  SELECT DEPTNO, DNAME, "
+        + "CAST(NULL AS VARCHAR CHARACTER SET ISO-8859-1) AS LOC\n"
+        + "  FROM scott.DEPT";
+    pig(script).assertRel(hasTree(plan))
+        .assertOptimizedRel(hasTree(optimizedPlan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testUnionDifferentSchemas2() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = FILTER A BY DEPTNO == 10;\n"
+        + "C = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray, LOC:CHARARRAY);\n"
+        + "D = UNION ONSCHEMA B, C;\n";
+    final String plan = ""
+        + "LogicalUnion(all=[true])\n"
+        + "  LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], "
+        + "HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], "
+        + "DNAME=[null:VARCHAR], LOC=[null:VARCHAR])\n"
+        + "    LogicalFilter(condition=[=($7, 10)])\n"
+        + "      LogicalTableScan(table=[[scott, EMP]])\n"
+        + "  LogicalProject(EMPNO=[null:INTEGER], ENAME=[null:VARCHAR], "
+        + "JOB=[null:VARCHAR], MGR=[null:INTEGER], HIREDATE=[null:DATE], "
+        + "SAL=[null:DECIMAL(19, 0)], COMM=[null:DECIMAL(19, 0)], DEPTNO=[$0], "
+        + "DNAME=[$1], LOC=[$2])\n"
+        + "    LogicalTableScan(table=[[scott, DEPT]])\n";
+    final String result = ""
+        + "(7782,CLARK,MANAGER,7839,1981-06-09,2450.00,null,10,null,null)\n"
+        + "(7839,KING,PRESIDENT,null,1981-11-17,5000.00,null,10,null,"
+        + "null)\n"
+        + "(7934,MILLER,CLERK,7782,1982-01-23,1300.00,null,10,null,null)\n"
+        + "(null,null,null,null,null,null,null,10,ACCOUNTING,NEW YORK)\n"
+        + "(null,null,null,null,null,null,null,20,RESEARCH,DALLAS)\n"
+        + "(null,null,null,null,null,null,null,30,SALES,CHICAGO)\n"
+        + "(null,null,null,null,null,null,null,40,OPERATIONS,BOSTON)\n";
+    final String sql = ""
+        + "SELECT EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO, "
+        + "CAST(NULL AS VARCHAR CHARACTER SET ISO-8859-1) AS DNAME, "
+        + "CAST(NULL AS VARCHAR CHARACTER SET ISO-8859-1) AS LOC\n"
+        + "  FROM scott.EMP\n"
+        + "  WHERE DEPTNO = 10\n"
+        + "UNION ALL\n"
+        + "  SELECT CAST(NULL AS INTEGER) AS EMPNO, "
+        + "CAST(NULL AS VARCHAR CHARACTER SET ISO-8859-1) AS ENAME, "
+        + "CAST(NULL AS VARCHAR CHARACTER SET ISO-8859-1) AS JOB, "
+        + "CAST(NULL AS INTEGER) AS MGR, "
+        + "CAST(NULL AS DATE) AS HIREDATE, CAST(NULL AS DECIMAL(19, 0)) AS SAL, "
+        + "CAST(NULL AS DECIMAL(19, 0)) AS COMM, DEPTNO, DNAME, LOC\n"
+        + "  FROM scott.DEPT";
+    pig(script).assertRel(hasTree(plan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testJoin2Rels() {
+    final String scanScript = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray, LOC:CHARARRAY);\n";
+    final String scanPlan = ""
+        + "  LogicalTableScan(table=[[scott, EMP]])\n"
+        + "  LogicalTableScan(table=[[scott, DEPT]])\n";
+
+    final String innerScript = scanScript
+        + "C = JOIN A BY DEPTNO, B BY DEPTNO;\n";
+    final String plan = ""
+        + "LogicalJoin(condition=[=($7, $8)], joinType=[inner])\n"
+        + scanPlan;
+    final String innerSql = ""
+        + "SELECT *\n"
+        + "FROM scott.EMP\n"
+        + "  INNER JOIN scott.DEPT ON EMP.DEPTNO = DEPT.DEPTNO";
+    pig(innerScript).assertRel(hasTree(plan))
+        .assertSql(is(innerSql));
+
+    final String leftScript =
+        scanScript + "C = JOIN A BY DEPTNO LEFT OUTER, B BY DEPTNO;\n";
+    final String leftSql = ""
+        + "SELECT *\n"
+        + "FROM scott.EMP\n"
+        + "  LEFT JOIN scott.DEPT ON EMP.DEPTNO = DEPT.DEPTNO";
+    final String leftPlan = ""
+        + "LogicalJoin(condition=[=($7, $8)], joinType=[left])\n"
+        + scanPlan;
+    pig(leftScript).assertRel(hasTree(leftPlan))
+        .assertSql(is(leftSql));
+
+    final String rightScript = scanScript
+        + "C = JOIN A BY DEPTNO RIGHT OUTER, B BY DEPTNO;\n";
+    final String rightSql = ""
+        + "SELECT *\n"
+        + "FROM scott.EMP\n"
+        + "  RIGHT JOIN scott.DEPT ON EMP.DEPTNO = DEPT.DEPTNO";
+    final String rightPlan =
+        "LogicalJoin(condition=[=($7, $8)], joinType=[right])\n"
+            + scanPlan;
+    pig(rightScript)
+        .assertRel(hasTree(rightPlan))
+        .assertSql(is(rightSql));
+
+    final String fullScript = scanScript
+        + "C = JOIN A BY DEPTNO FULL, B BY DEPTNO;\n";
+    final String fullPlan = ""
+        + "LogicalJoin(condition=[=($7, $8)], joinType=[full])\n"
+        + scanPlan;
+    final String fullSql = ""
+        + "SELECT *\n"
+        + "FROM scott.EMP\n"
+        + "  FULL JOIN scott.DEPT ON EMP.DEPTNO = DEPT.DEPTNO";
+    final String fullResult = ""
+        + "(7369,SMITH,CLERK,7902,1980-12-17,800.00,null,20,20,"
+        + "RESEARCH,DALLAS)\n"
+        + "(7499,ALLEN,SALESMAN,7698,1981-02-20,1600.00,300.00,30,30,"
+        + "SALES,CHICAGO)\n"
+        + "(7521,WARD,SALESMAN,7698,1981-02-22,1250.00,500.00,30,30,"
+        + "SALES,CHICAGO)\n"
+        + "(7566,JONES,MANAGER,7839,1981-02-04,2975.00,null,20,20,"
+        + "RESEARCH,DALLAS)\n"
+        + "(7654,MARTIN,SALESMAN,7698,1981-09-28,1250.00,1400.00,30,30,"
+        + "SALES,CHICAGO)\n"
+        + "(7698,BLAKE,MANAGER,7839,1981-01-05,2850.00,null,30,30,"
+        + "SALES,CHICAGO)\n"
+        + "(7782,CLARK,MANAGER,7839,1981-06-09,2450.00,null,10,10,"
+        + "ACCOUNTING,NEW YORK)\n"
+        + "(7788,SCOTT,ANALYST,7566,1987-04-19,3000.00,null,20,20,"
+        + "RESEARCH,DALLAS)\n"
+        + "(7839,KING,PRESIDENT,null,1981-11-17,5000.00,null,10,10,"
+        + "ACCOUNTING,NEW YORK)\n"
+        + "(7844,TURNER,SALESMAN,7698,1981-09-08,1500.00,0.00,30,30,"
+        + "SALES,CHICAGO)\n"
+        + "(7876,ADAMS,CLERK,7788,1987-05-23,1100.00,null,20,20,"
+        + "RESEARCH,DALLAS)\n"
+        + "(7900,JAMES,CLERK,7698,1981-12-03,950.00,null,30,30,SALES,"
+        + "CHICAGO)\n"
+        + "(7902,FORD,ANALYST,7566,1981-12-03,3000.00,null,20,20,"
+        + "RESEARCH,DALLAS)\n"
+        + "(7934,MILLER,CLERK,7782,1982-01-23,1300.00,null,10,10,"
+        + "ACCOUNTING,NEW YORK)\n"
+        + "(null,null,null,null,null,null,null,null,40,OPERATIONS,"
+        + "BOSTON)\n";
+    pig(fullScript)
+        .assertRel(hasTree(fullPlan))
+        .assertSql(is(fullSql))
+        .assertResult(is(fullResult));
+  }
+
+  @Test public void testJoin3Rels() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray, LOC:CHARARRAY);\n"
+        + "C = FILTER B BY LOC == 'CHICAGO';\n"
+        + "D = JOIN A BY DEPTNO, B BY DEPTNO, C BY DEPTNO;\n";
+    final String plan = ""
+        + "LogicalJoin(condition=[=($7, $11)], joinType=[inner])\n"
+        + "  LogicalJoin(condition=[=($7, $8)], joinType=[inner])\n"
+        + "    LogicalTableScan(table=[[scott, EMP]])\n"
+        + "    LogicalTableScan(table=[[scott, DEPT]])\n"
+        + "  LogicalFilter(condition=[=($2, 'CHICAGO')])\n"
+        + "    LogicalTableScan(table=[[scott, DEPT]])\n";
+    final String sql = ""
+        + "SELECT *\n"
+        + "FROM scott.EMP\n"
+        + "  INNER JOIN scott.DEPT ON EMP.DEPTNO = DEPT.DEPTNO\n"
+        + "  INNER JOIN (SELECT *\n"
+        + "      FROM scott.DEPT\n"
+        + "      WHERE LOC = 'CHICAGO') AS t ON EMP.DEPTNO = t.DEPTNO";
+    final String result = ""
+        + "(7499,ALLEN,SALESMAN,7698,1981-02-20,1600.00,300.00,30,30,"
+        + "SALES,CHICAGO,30,SALES,"
+        + "CHICAGO)\n"
+        + "(7521,WARD,SALESMAN,7698,1981-02-22,1250.00,500.00,30,30,"
+        + "SALES,CHICAGO,30,SALES,"
+        + "CHICAGO)\n"
+        + "(7654,MARTIN,SALESMAN,7698,1981-09-28,1250.00,1400.00,30,30,"
+        + "SALES,CHICAGO,30,"
+        + "SALES,CHICAGO)\n"
+        + "(7698,BLAKE,MANAGER,7839,1981-01-05,2850.00,null,30,30,SALES,"
+        + "CHICAGO,30,SALES,"
+        + "CHICAGO)\n"
+        + "(7844,TURNER,SALESMAN,7698,1981-09-08,1500.00,0.00,30,30,"
+        + "SALES,CHICAGO,30,SALES,"
+        + "CHICAGO)\n"
+        + "(7900,JAMES,CLERK,7698,1981-12-03,950.00,null,30,30,SALES,"
+        + "CHICAGO,30,SALES,"
+        + "CHICAGO)\n";
+    pig(script).assertRel(hasTree(plan))
+        .assertSql(is(sql))
+        .assertResult(is(result));
+
+    final String script2 = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray,\n"
+        + "    LOC:CHARARRAY);\n"
+        + "C = FILTER B BY LOC == 'CHICAGO';\n"
+        + "D = JOIN A BY (DEPTNO, ENAME), B BY (DEPTNO, DNAME),\n"
+        + "    C BY (DEPTNO, DNAME);\n";
+    final String plan2 = ""
+        + "LogicalJoin(condition=[AND(=($7, $11), =($9, $12))], "
+        + "joinType=[inner])\n"
+        + "  LogicalJoin(condition=[AND(=($7, $8), =($1, $9))], "
+        + "joinType=[inner])\n"
+        + "    LogicalTableScan(table=[[scott, EMP]])\n"
+        + "    LogicalTableScan(table=[[scott, DEPT]])\n"
+        + "  LogicalFilter(condition=[=($2, 'CHICAGO')])\n"
+        + "    LogicalTableScan(table=[[scott, DEPT]])\n";
+    final String sql2 = ""
+        + "SELECT *\n"
+        + "FROM scott.EMP\n"
+        + "  INNER JOIN scott.DEPT ON EMP.DEPTNO = DEPT.DEPTNO "
+        + "AND EMP.ENAME = DEPT.DNAME\n"
+        + "  INNER JOIN (SELECT *\n"
+        + "      FROM scott.DEPT\n"
+        + "      WHERE LOC = 'CHICAGO') AS t ON EMP.DEPTNO = t.DEPTNO "
+        + "AND DEPT.DNAME = t.DNAME";
+    pig(script2).assertRel(hasTree(plan2))
+        .assertSql(is(sql2));
+  }
+
+  @Test public void testCross() {
+    final String script = ""
+        + "A = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray,\n"
+        + "    LOC:CHARARRAY);\n"
+        + "B = FOREACH A GENERATE DEPTNO; \n"
+        + "C = FILTER B BY DEPTNO <= 20;\n"
+        + "D = CROSS B, C;\n";
+    final String plan = ""
+        + "LogicalJoin(condition=[true], joinType=[inner])\n"
+        + "  LogicalProject(DEPTNO=[$0])\n"
+        + "    LogicalTableScan(table=[[scott, DEPT]])\n"
+        + "  LogicalFilter(condition=[<=($0, 20)])\n"
+        + "    LogicalProject(DEPTNO=[$0])\n"
+        + "      LogicalTableScan(table=[[scott, DEPT]])\n";
+    final String sql = ""
+        + "SELECT *\n"
+        + "FROM (SELECT DEPTNO\n"
+        + "      FROM scott.DEPT) AS t,\n"
+        + "      (SELECT DEPTNO\n"
+        + "      FROM scott.DEPT\n"
+        + "      WHERE DEPTNO <= 20) AS t1";
+    final String result = ""
+        + "(10,10)\n"
+        + "(10,20)\n"
+        + "(20,10)\n"
+        + "(20,20)\n"
+        + "(30,10)\n"
+        + "(30,20)\n"
+        + "(40,10)\n"
+        + "(40,20)\n";
+    pig(script).assertRel(hasTree(plan))
+        .assertSql(is(sql))
+        .assertResult(is(result));
+
+    final String script2 = ""
+        + "A = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray,"
+        + "    LOC:CHARARRAY);\n"
+        + "B = FOREACH A GENERATE DEPTNO; \n"
+        + "C = FILTER B BY DEPTNO <= 20;\n"
+        + "D = FILTER B BY DEPTNO > 20;\n"
+        + "E = CROSS B, C, D;\n";
+
+    final String plan2 = ""
+        + "LogicalJoin(condition=[true], joinType=[inner])\n"
+        + "  LogicalJoin(condition=[true], joinType=[inner])\n"
+        + "    LogicalProject(DEPTNO=[$0])\n"
+        + "      LogicalTableScan(table=[[scott, DEPT]])\n"
+        + "    LogicalFilter(condition=[<=($0, 20)])\n"
+        + "      LogicalProject(DEPTNO=[$0])\n"
+        + "        LogicalTableScan(table=[[scott, DEPT]])\n"
+        + "  LogicalFilter(condition=[>($0, 20)])\n"
+        + "    LogicalProject(DEPTNO=[$0])\n"
+        + "      LogicalTableScan(table=[[scott, DEPT]])\n";
+
+    final String result2 = ""
+        + "(10,10,30)\n"
+        + "(10,10,40)\n"
+        + "(10,20,30)\n"
+        + "(10,20,40)\n"
+        + "(20,10,30)\n"
+        + "(20,10,40)\n"
+        + "(20,20,30)\n"
+        + "(20,20,40)\n"
+        + "(30,10,30)\n"
+        + "(30,10,40)\n"
+        + "(30,20,30)\n"
+        + "(30,20,40)\n"
+        + "(40,10,30)\n"
+        + "(40,10,40)\n"
+        + "(40,20,30)\n"
+        + "(40,20,40)\n";
+    final String sql2 = ""
+        + "SELECT *\n"
+        + "FROM (SELECT DEPTNO\n"
+        + "      FROM scott.DEPT) AS t,\n"
+        + "      (SELECT DEPTNO\n"
+        + "      FROM scott.DEPT\n"
+        + "      WHERE DEPTNO <= 20) AS t1,\n"
+        + "      (SELECT DEPTNO\n"
+        + "      FROM scott.DEPT\n"
+        + "      WHERE DEPTNO > 20) AS t3";
+    pig(script2).assertRel(hasTree(plan2))
+        .assertResult(is(result2))
+        .assertSql(is(sql2));
+  }
+
+  @Test public void testGroupby() {
+    final String baseScript =
+        "A = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray, LOC:CHARARRAY);\n";
+    final String basePlan = "      LogicalTableScan(table=[[scott, DEPT]])\n";
+    final String script = baseScript + "B = GROUP A BY DEPTNO;\n";
+    final String plan = ""
+        + "LogicalProject(group=[$0], A=[$1])\n"
+        + "  LogicalAggregate(group=[{0}], A=[COLLECT($1)])\n"
+        + "    LogicalProject(DEPTNO=[$0], $f1=[ROW($0, $1, $2)])\n"
+        + basePlan;
+    final String result = ""
+        + "(20,{(20,RESEARCH,DALLAS)})\n"
+        + "(40,{(40,OPERATIONS,BOSTON)})\n"
+        + "(10,{(10,ACCOUNTING,NEW YORK)})\n"
+        + "(30,{(30,SALES,CHICAGO)})\n";
+
+    final String sql = ""
+        + "SELECT DEPTNO, COLLECT(ROW(DEPTNO, DNAME, LOC)) AS A\n"
+        + "FROM scott.DEPT\n"
+        + "GROUP BY DEPTNO";
+    pig(script).assertRel(hasTree(plan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+
+    final String script1 = baseScript + "B = GROUP A ALL;\n";
+    final String plan1 = ""
+        + "LogicalProject(group=[$0], A=[$1])\n"
+        + "  LogicalAggregate(group=[{0}], A=[COLLECT($1)])\n"
+        + "    LogicalProject($f0=['all'], $f1=[ROW($0, $1, $2)])\n"
+        + basePlan;
+    final String result1 = ""
+        + "(all,{(10,ACCOUNTING,NEW YORK),(20,RESEARCH,DALLAS),"
+        + "(30,SALES,CHICAGO),(40,OPERATIONS,BOSTON)})\n";
+    pig(script1).assertResult(is(result1))
+        .assertRel(hasTree(plan1));
+  }
+
+  @Test public void testGroupby2() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = FOREACH A GENERATE EMPNO, ENAME, JOB, MGR, SAL, COMM, DEPTNO;\n"
+        + "C = GROUP B BY (DEPTNO, JOB);\n";
+    final String plan = ""
+        + "LogicalProject(group=[ROW($0, $1)], B=[$2])\n"
+        + "  LogicalAggregate(group=[{0, 1}], B=[COLLECT($2)])\n"
+        + "    LogicalProject(DEPTNO=[$6], JOB=[$2], $f2=[ROW($0, $1, $2, "
+        + "$3, $4, $5, $6)])\n"
+        + "      LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3],"
+        + " SAL=[$5], COMM=[$6], DEPTNO=[$7])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    final String result = ""
+        + "({10, MANAGER},{(7782,CLARK,MANAGER,7839,2450.00,null,10)})\n"
+        + "({10, PRESIDENT},{(7839,KING,PRESIDENT,null,5000.00,null,10)})\n"
+        + "({20, CLERK},{(7369,SMITH,CLERK,7902,800.00,null,20),"
+        + "(7876,ADAMS,CLERK,7788,1100.00,null,20)})\n"
+        + "({30, MANAGER},{(7698,BLAKE,MANAGER,7839,2850.00,null,30)})\n"
+        + "({20, ANALYST},{(7788,SCOTT,ANALYST,7566,3000.00,null,20),"
+        + "(7902,FORD,ANALYST,7566,3000.00,null,20)})\n"
+        + "({30, SALESMAN},{(7499,ALLEN,SALESMAN,7698,1600.00,300.00,30),"
+        + "(7521,WARD,SALESMAN,7698,1250.00,500.00,30),"
+        + "(7654,MARTIN,SALESMAN,7698,1250.00,1400.00,30),"
+        + "(7844,TURNER,SALESMAN,7698,1500.00,0.00,30)})\n"
+        + "({30, CLERK},{(7900,JAMES,CLERK,7698,950.00,null,30)})\n"
+        + "({20, MANAGER},{(7566,JONES,MANAGER,7839,2975.00,null,20)})\n"
+        + "({10, CLERK},{(7934,MILLER,CLERK,7782,1300.00,null,10)})\n";
+
+    pig(script).assertRel(hasTree(plan))
+        .assertResult(is(result));
+  }
+
+  @Test public void testCubeCube() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = CUBE A BY CUBE(DEPTNO, JOB);\n"
+        + "C = FOREACH B GENERATE group, COUNT(cube.ENAME);\n";
+    final String plan = ""
+        + "LogicalProject(group=[$0], $f1=[COUNT(PIG_BAG"
+        + "(MULTISET_PROJECTION($1, 3)))])\n"
+        + "  LogicalProject(group=[ROW($0, $1)], cube=[$2])\n"
+        + "    LogicalAggregate(group=[{0, 1}], "
+        + "groups=[[{0, 1}, {0}, {1}, {}]], cube=[COLLECT($2)])\n"
+        + "      LogicalProject(DEPTNO=[$7], JOB=[$2], "
+        + "$f2=[ROW($7, $2, $0, $1, $3, $4, $5, $6)])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    final String optimizedPlan = ""
+        + "LogicalProject(group=[ROW($0, $1)], $f1=[CAST($2):BIGINT])\n"
+        + "  LogicalAggregate(group=[{0, 1}], "
+        + "groups=[[{0, 1}, {0}, {1}, {}]], agg#0=[COUNT($2)])\n"
+        + "    LogicalProject(DEPTNO=[$7], JOB=[$2], ENAME=[$1])\n"
+        + "      LogicalTableScan(table=[[scott, EMP]])\n";
+    final String result = ""
+        + "({30, SALESMAN},4)\n"
+        + "({30, null},6)\n"
+        + "({10, null},3)\n"
+        + "({null, PRESIDENT},1)\n"
+        + "({30, MANAGER},1)\n"
+        + "({20, MANAGER},1)\n"
+        + "({20, ANALYST},2)\n"
+        + "({10, MANAGER},1)\n"
+        + "({null, CLERK},4)\n"
+        + "({null, null},14)\n"
+        + "({20, null},5)\n"
+        + "({10, PRESIDENT},1)\n"
+        + "({null, ANALYST},2)\n"
+        + "({null, SALESMAN},4)\n"
+        + "({30, CLERK},1)\n"
+        + "({10, CLERK},1)\n"
+        + "({20, CLERK},2)\n"
+        + "({null, MANAGER},3)\n";
+
+    final String sql = ""
+        + "SELECT ROW(DEPTNO, JOB) AS group,"
+        + " CAST(COUNT(ENAME) AS BIGINT) AS $f1\n"
+        + "FROM scott.EMP\n"
+        + "GROUP BY CUBE(DEPTNO, JOB)";
+    pig(script).assertRel(hasTree(plan))
+        .assertOptimizedRel(hasTree(optimizedPlan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testCubeRollup() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = CUBE A BY ROLLUP(DEPTNO, JOB);\n"
+        + "C = FOREACH B GENERATE group, COUNT(cube.ENAME);\n";
+    final String plan = ""
+        + "LogicalProject(group=[$0], $f1=[COUNT(PIG_BAG"
+        + "(MULTISET_PROJECTION($1, 3)))])\n"
+        + "  LogicalProject(group=[ROW($0, $1)], cube=[$2])\n"
+        + "    LogicalAggregate(group=[{0, 1}], groups=[[{0, 1}, {1}, {}]],"
+        + " cube=[COLLECT($2)])\n"
+        + "      LogicalProject(DEPTNO=[$7], JOB=[$2], $f2=[ROW($7, $2, $0,"
+        + " $1, $3, $4, $5, $6)])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    final String optimizedPlan = ""
+        + "LogicalProject(group=[ROW($0, $1)], $f1=[CAST($2):BIGINT])\n"
+        + "  LogicalAggregate(group=[{0, 1}], groups=[[{0, 1}, {1}, {}]], "
+        + "agg#0=[COUNT($2)])\n"
+        + "    LogicalProject(DEPTNO=[$7], JOB=[$2], ENAME=[$1])\n"
+        + "      LogicalTableScan(table=[[scott, EMP]])\n";
+    final String result = ""
+        + "({30, SALESMAN},4)\n"
+        + "({null, PRESIDENT},1)\n"
+        + "({30, MANAGER},1)\n"
+        + "({20, MANAGER},1)\n"
+        + "({20, ANALYST},2)\n"
+        + "({10, MANAGER},1)\n"
+        + "({null, CLERK},4)\n"
+        + "({null, null},14)\n"
+        + "({10, PRESIDENT},1)\n"
+        + "({null, ANALYST},2)\n"
+        + "({null, SALESMAN},4)\n"
+        + "({30, CLERK},1)\n"
+        + "({10, CLERK},1)\n"
+        + "({20, CLERK},2)\n"
+        + "({null, MANAGER},3)\n";
+    final String sql = ""
+        + "SELECT ROW(DEPTNO, JOB) AS group, "
+        + "CAST(COUNT(ENAME) AS BIGINT) AS $f1\n"
+        + "FROM scott.EMP\n"
+        + "GROUP BY ROLLUP(DEPTNO, JOB)";
+    pig(script).assertRel(hasTree(plan))
+        .assertOptimizedRel(hasTree(optimizedPlan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testMultisetProjection() {
+    final String script = ""
+        + "A = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray,\n"
+        + "    LOC:CHARARRAY);\n"
+        + "B = GROUP A BY DEPTNO;\n"
+        + "C = FOREACH B GENERATE A.(DEPTNO, DNAME);\n";
+    final String plan = ""
+        + "LogicalProject($f0=[MULTISET_PROJECTION($1, 0, 1)])\n"
+        + "  LogicalProject(group=[$0], A=[$1])\n"
+        + "    LogicalAggregate(group=[{0}], A=[COLLECT($1)])\n"
+        + "      LogicalProject(DEPTNO=[$0], $f1=[ROW($0, $1, $2)])\n"
+        + "        LogicalTableScan(table=[[scott, DEPT]])\n";
+    String optimizedPlan = ""
+        + "LogicalProject($f0=[$1])\n"
+        + "  LogicalAggregate(group=[{0}], agg#0=[COLLECT($2)])\n"
+        + "    LogicalProject(DEPTNO=[$0], DNAME=[$1], $f2=[ROW"
+        + "($0, $1)])\n"
+        + "      LogicalTableScan(table=[[scott, DEPT]])\n";
+    final String result = ""
+        + "({(20,RESEARCH)})\n"
+        + "({(40,OPERATIONS)})\n"
+        + "({(10,ACCOUNTING)})\n"
+        + "({(30,SALES)})\n";
+    String sql = ""
+        + "SELECT COLLECT(ROW(DEPTNO, DNAME)) AS $f0\n"
+        + "FROM scott.DEPT\n"
+        + "GROUP BY DEPTNO";
+    // When
+    //   [CALCITE-3297] PigToSqlAggregateRule should be applied on multi-set
+    //   projection to produce an optimal plan
+    // is fixed we can remove the following block.
+    if (Bug.remark("[CALCITE-3297]") != null) {
+      optimizedPlan = ""
+          + "LogicalProject($f0=[MULTISET_PROJECTION($1, 0, 1)])\n"
+          + "  LogicalAggregate(group=[{0}], A=[COLLECT($1)])\n"
+          + "    LogicalProject(DEPTNO=[$0], $f1=[ROW($0, $1, $2)])\n"
+          + "      LogicalTableScan(table=[[scott, DEPT]])\n";
+      sql = "SELECT MULTISET_PROJECTION(COLLECT(ROW(DEPTNO, DNAME, LOC)), "
+          + "0, 1) AS $f0\n"
+          + "FROM scott.DEPT\n"
+          + "GROUP BY DEPTNO";
+    }
+    pig(script).assertRel(hasTree(plan))
+        .assertOptimizedRel(hasTree(optimizedPlan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testOrderBy() {
+    final String scan = ""
+        + "A = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray,\n"
+        + "    LOC:CHARARRAY);\n";
+    final String scanPlan =
+        "  LogicalTableScan(table=[[scott, DEPT]])\n";
+
+    final String plan0 = "LogicalSort(sort0=[$1], dir0=[ASC])\n"
+        + scanPlan;
+    final String script0 = scan + "B = ORDER A BY DNAME;\n";
+    final String sql0 = "SELECT *\n"
+        + "FROM scott.DEPT\n"
+        + "ORDER BY DNAME";
+    final String result0 = ""
+        + "(10,ACCOUNTING,NEW YORK)\n"
+        + "(40,OPERATIONS,BOSTON)\n"
+        + "(20,RESEARCH,DALLAS)\n"
+        + "(30,SALES,CHICAGO)\n";
+    pig(script0).assertRel(hasTree(plan0))
+        .assertSql(is(sql0))
+        .assertResult(is(result0));
+
+    final String plan1 = "LogicalSort(sort0=[$1], dir0=[DESC])\n"
+        + scanPlan;
+    final String script1 = scan + "B = ORDER A BY DNAME DESC;\n";
+    final String sql1 = "SELECT *\n"
+        + "FROM scott.DEPT\n"
+        + "ORDER BY DNAME DESC";
+    pig(script1).assertRel(hasTree(plan1))
+        .assertSql(is(sql1));
+
+    final String plan2 = ""
+        + "LogicalSort(sort0=[$2], sort1=[$0], dir0=[DESC], dir1=[ASC])\n"
+        + scanPlan;
+    final String script2 = scan + "B = ORDER A BY LOC DESC, DEPTNO;\n";
+    final String sql2 = "SELECT *\n"
+        + "FROM scott.DEPT\n"
+        + "ORDER BY LOC DESC, DEPTNO";
+    pig(script2).assertRel(hasTree(plan2))
+        .assertSql(is(sql2));
+
+    final String plan3 = ""
+        + "LogicalSort(sort0=[$0], sort1=[$1], sort2=[$2], dir0=[ASC], dir1=[ASC], dir2=[ASC])\n"
+        + scanPlan;
+    final String script3 = scan + "B = ORDER A BY *;\n";
+    final String sql3 = "SELECT *\n"
+        + "FROM scott.DEPT\n"
+        + "ORDER BY DEPTNO, DNAME, LOC";
+    pig(script3).assertRel(hasTree(plan3))
+        .assertSql(is(sql3));
+
+    final String plan4 = ""
+        + "LogicalSort(sort0=[$0], sort1=[$1], sort2=[$2], dir0=[DESC], dir1=[DESC], dir2=[DESC])\n"
+        + scanPlan;
+    final String script4 = scan + "B = ORDER A BY * DESC;\n";
+    final String result4 = ""
+        + "(40,OPERATIONS,BOSTON)\n"
+        + "(30,SALES,CHICAGO)\n"
+        + "(20,RESEARCH,DALLAS)\n"
+        + "(10,ACCOUNTING,NEW YORK)\n";
+    pig(script4).assertRel(hasTree(plan4))
+        .assertResult(is(result4));
+  }
+
+  @Test public void testRank() {
+    final String base = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = FOREACH A GENERATE EMPNO, JOB, DEPTNO;\n";
+    final String basePlan = ""
+        + "  LogicalProject(EMPNO=[$0], JOB=[$2], DEPTNO=[$7])\n"
+        + "    LogicalTableScan(table=[[scott, EMP]])\n";
+    final String optimizedPlan = ""
+        + "LogicalProject(rank_C=[$3], EMPNO=[$0], JOB=[$1], DEPTNO=[$2])\n"
+        + "  LogicalWindow(window#0=[window(partition {} order by [2, 1 DESC] "
+        + "range between UNBOUNDED PRECEDING and CURRENT ROW aggs [RANK()])])\n"
+        + "    LogicalProject(EMPNO=[$0], JOB=[$2], DEPTNO=[$7])\n"
+        + "      LogicalTableScan(table=[[scott, EMP]])\n";
+
+    final String script = base + "C = RANK B BY DEPTNO ASC, JOB DESC;\n";
+    final String plan = ""
+        + "LogicalProject(rank_C=[RANK() OVER (ORDER BY $2, "
+        + "$1 DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)" + "], "
+        + "EMPNO=[$0], JOB=[$1], DEPTNO=[$2])\n"
+        + basePlan;
+    final String result = ""
+        + "(1,7839,PRESIDENT,10)\n"
+        + "(2,7782,MANAGER,10)\n"
+        + "(3,7934,CLERK,10)\n"
+        + "(4,7566,MANAGER,20)\n"
+        + "(5,7369,CLERK,20)\n"
+        + "(5,7876,CLERK,20)\n"
+        + "(7,7788,ANALYST,20)\n"
+        + "(7,7902,ANALYST,20)\n"
+        + "(9,7499,SALESMAN,30)\n"
+        + "(9,7521,SALESMAN,30)\n"
+        + "(9,7654,SALESMAN,30)\n"
+        + "(9,7844,SALESMAN,30)\n"
+        + "(13,7698,MANAGER,30)\n"
+        + "(14,7900,CLERK,30)\n";
+    final String sql = ""
+        + "SELECT RANK() OVER (ORDER BY DEPTNO, JOB DESC RANGE BETWEEN "
+        + "UNBOUNDED PRECEDING AND CURRENT ROW) AS rank_C, EMPNO, JOB, DEPTNO\n"
+        + "FROM scott.EMP";
+    pig(script).assertRel(hasTree(plan))
+        .assertOptimizedRel(hasTree(optimizedPlan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+
+    final String script2 = base + "C = RANK B BY DEPTNO ASC, JOB DESC DENSE;\n";
+    final String optimizedPlan2 = ""
+        + "LogicalProject(rank_C=[$3], EMPNO=[$0], JOB=[$1], DEPTNO=[$2])\n"
+        + "  LogicalWindow(window#0=[window(partition {} "
+        + "order by [2, 1 DESC] "
+        + "range between UNBOUNDED PRECEDING and CURRENT ROW "
+        + "aggs [DENSE_RANK()])"
+        + "])\n"
+        + "    LogicalProject(EMPNO=[$0], JOB=[$2], DEPTNO=[$7])\n"
+        + "      LogicalTableScan(table=[[scott, EMP]])\n";
+    final String plan2 = ""
+        + "LogicalProject(rank_C=[DENSE_RANK() OVER (ORDER BY $2, "
+        + "$1 DESC RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT" + " ROW)], "
+        + "EMPNO=[$0], JOB=[$1], DEPTNO=[$2])\n"
+        + basePlan;
+    final String result2 = ""
+        + "(1,7839,PRESIDENT,10)\n"
+        + "(2,7782,MANAGER,10)\n"
+        + "(3,7934,CLERK,10)\n"
+        + "(4,7566,MANAGER,20)\n"
+        + "(5,7369,CLERK,20)\n"
+        + "(5,7876,CLERK,20)\n"
+        + "(6,7788,ANALYST,20)\n"
+        + "(6,7902,ANALYST,20)\n"
+        + "(7,7499,SALESMAN,30)\n"
+        + "(7,7521,SALESMAN,30)\n"
+        + "(7,7654,SALESMAN,30)\n"
+        + "(7,7844,SALESMAN,30)\n"
+        + "(8,7698,MANAGER,30)\n"
+        + "(9,7900,CLERK,30)\n";
+    final String sql2 = ""
+        + "SELECT DENSE_RANK() OVER (ORDER BY DEPTNO, JOB DESC RANGE BETWEEN "
+        + "UNBOUNDED PRECEDING AND CURRENT ROW) AS rank_C, EMPNO, JOB, DEPTNO\n"
+        + "FROM scott.EMP";
+    pig(script2).assertRel(hasTree(plan2))
+        .assertOptimizedRel(hasTree(optimizedPlan2))
+        .assertResult(is(result2))
+        .assertSql(is(sql2));
+  }
+
+  @Test public void testLimit() {
+    final String scan = ""
+        + "A = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray,\n"
+        + "    LOC:CHARARRAY);\n";
+    final String scanPlan = "  LogicalTableScan(table=[[scott, DEPT]])\n";
+
+    final String plan1 = "LogicalSort(sort0=[$1], dir0=[ASC], fetch=[2])\n"
+        + scanPlan;
+    final String script1 = scan
+        + "B = ORDER A BY DNAME;\n"
+        + "C = LIMIT B 2;\n";
+    final String sql1 = "SELECT *\n"
+        + "FROM scott.DEPT\n"
+        + "ORDER BY DNAME\n"
+        + "FETCH NEXT 2 ROWS ONLY";
+    final String result1 = ""
+        + "(10,ACCOUNTING,NEW YORK)\n"
+        + "(40,OPERATIONS,BOSTON)\n";
+    pig(script1).assertRel(hasTree(plan1))
+        .assertSql(is(sql1))
+        .assertResult(is(result1));
+
+    final String plan2 = "LogicalSort(fetch=[2])\n"
+        + scanPlan;
+    final String script2 = scan + "B = LIMIT A 2;\n";
+    final String sql2 = "SELECT *\n"
+        + "FROM scott.DEPT\n"
+        + "FETCH NEXT 2 ROWS ONLY";
+    pig(script2).assertRel(hasTree(plan2))
+        .assertSql(is(sql2));
+  }
+
+  @Test public void testDistinct() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = FOREACH A GENERATE DEPTNO;\n"
+        + "C = DISTINCT B;\n";
+    final String plan = ""
+        + "LogicalAggregate(group=[{0}])\n"
+        + "  LogicalProject(DEPTNO=[$7])\n"
+        + "    LogicalTableScan(table=[[scott, EMP]])\n";
+
+    final String result = ""
+        + "(20)\n"
+        + "(10)\n"
+        + "(30)\n";
+    final String sql = "SELECT DEPTNO\n"
+        + "FROM scott.EMP\n"
+        + "GROUP BY DEPTNO";
+    pig(script).assertRel(hasTree(plan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testAggregate() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = GROUP A BY DEPTNO;\n"
+        + "C = FOREACH B GENERATE group, COUNT(A), BigDecimalSum(A.SAL);\n";
+    final String plan = ""
+        + "LogicalProject(group=[$0], $f1=[COUNT(PIG_BAG($1))], "
+        + "$f2=[BigDecimalSum(PIG_BAG(MULTISET_PROJECTION($1, 5)))])\n"
+        + "  LogicalProject(group=[$0], A=[$1])\n"
+        + "    LogicalAggregate(group=[{0}], A=[COLLECT($1)])\n"
+        + "      LogicalProject(DEPTNO=[$7], $f1=[ROW($0, $1, $2, $3, $4, $5, $6, $7)])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    final String optimizedPlan = ""
+        + "LogicalProject(group=[$0], $f1=[CAST($1):BIGINT], $f2=[CAST($2):DECIMAL(19, 0)])\n"
+        + "  LogicalAggregate(group=[{0}], agg#0=[COUNT()], agg#1=[SUM($1)])\n"
+        + "    LogicalProject(DEPTNO=[$7], SAL=[$5])\n"
+        + "      LogicalTableScan(table=[[scott, EMP]])\n";
+    final String result = ""
+        + "(20,5,10875.00)\n"
+        + "(10,3,8750.00)\n"
+        + "(30,6,9400.00)\n";
+    final String sql = ""
+        + "SELECT DEPTNO AS group, CAST(COUNT(*) AS BIGINT) AS $f1, CAST(SUM(SAL) AS "
+        + "DECIMAL(19, 0)) AS $f2\n"
+        + "FROM scott.EMP\n"
+        + "GROUP BY DEPTNO";
+    pig(script).assertRel(hasTree(plan))
+        .assertOptimizedRel(hasTree(optimizedPlan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testAggregate2() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = GROUP A BY (DEPTNO, MGR, HIREDATE);\n"
+        + "C = FOREACH B GENERATE group, COUNT(A), SUM(A.SAL) as salSum;\n"
+        + "D = ORDER C BY salSum;\n";
+    final String plan = ""
+        + "LogicalSort(sort0=[$2], dir0=[ASC])\n"
+        + "  LogicalProject(group=[$0], $f1=[COUNT(PIG_BAG($1))], "
+        + "salSum=[BigDecimalSum(PIG_BAG(MULTISET_PROJECTION($1, 5)))])\n"
+        + "    LogicalProject(group=[ROW($0, $1, $2)], A=[$3])\n"
+        + "      LogicalAggregate(group=[{0, 1, 2}], A=[COLLECT($3)])\n"
+        + "        LogicalProject(DEPTNO=[$7], MGR=[$3], HIREDATE=[$4], "
+        + "$f3=[ROW($0, $1, $2, $3, $4, $5, $6, $7)])\n"
+        + "          LogicalTableScan(table=[[scott, EMP]])\n";
+    final String optimizedPlan = ""
+        + "LogicalSort(sort0=[$2], dir0=[ASC])\n"
+        + "  LogicalProject(group=[ROW($0, $1, $2)], $f1=[CAST($3):BIGINT], "
+        + "salSum=[CAST($4):DECIMAL(19, 0)])\n"
+        + "    LogicalAggregate(group=[{0, 1, 2}], agg#0=[COUNT()], agg#1=[SUM($3)])\n"
+        + "      LogicalProject(DEPTNO=[$7], MGR=[$3], HIREDATE=[$4], SAL=[$5])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    pig(script).assertRel(hasTree(plan))
+        .assertOptimizedRel(hasTree(optimizedPlan));
+
+    final String result = ""
+        + "({20, 7902, 1980-12-17},1,800.00)\n"
+        + "({30, 7698, 1981-12-03},1,950.00)\n"
+        + "({20, 7788, 1987-05-23},1,1100.00)\n"
+        + "({30, 7698, 1981-09-28},1,1250.00)\n"
+        + "({30, 7698, 1981-02-22},1,1250.00)\n"
+        + "({10, 7782, 1982-01-23},1,1300.00)\n"
+        + "({30, 7698, 1981-09-08},1,1500.00)\n"
+        + "({30, 7698, 1981-02-20},1,1600.00)\n"
+        + "({10, 7839, 1981-06-09},1,2450.00)\n"
+        + "({30, 7839, 1981-01-05},1,2850.00)\n"
+        + "({20, 7839, 1981-02-04},1,2975.00)\n"
+        + "({20, 7566, 1981-12-03},1,3000.00)\n"
+        + "({20, 7566, 1987-04-19},1,3000.00)\n"
+        + "({10, null, 1981-11-17},1,5000.00)\n";
+    final String sql = ""
+        + "SELECT ROW(DEPTNO, MGR, HIREDATE) AS group, CAST(COUNT(*) AS "
+        + "BIGINT) AS $f1, CAST(SUM(SAL) AS DECIMAL(19, 0)) AS salSum\n"
+        + "FROM scott.EMP\n"
+        + "GROUP BY DEPTNO, MGR, HIREDATE\n"
+        + "ORDER BY CAST(SUM(SAL) AS DECIMAL(19, 0))";
+    pig(script).assertResult(is(result))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testAggregate2half() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = GROUP A BY (DEPTNO, MGR, HIREDATE);\n"
+        + "C = FOREACH B GENERATE flatten(group) as (DEPTNO, MGR, HIREDATE),\n"
+        + "    COUNT(A), SUM(A.SAL) as salSum, MAX(A.DEPTNO) as maxDep,\n"
+        + "    MIN(A.HIREDATE) as minHire;\n"
+        + "D = ORDER C BY salSum;\n";
+    final String plan = ""
+        + "LogicalSort(sort0=[$4], dir0=[ASC])\n"
+        + "  LogicalProject(DEPTNO=[$0.DEPTNO], MGR=[$0.MGR], HIREDATE=[$0.HIREDATE], "
+        + "$f3=[COUNT(PIG_BAG($1))], salSum=[BigDecimalSum(PIG_BAG(MULTISET_PROJECTION($1, 5)))]"
+        + ", maxDep=[IntMax(PIG_BAG(MULTISET_PROJECTION($1, 7)))], minHire=[DateTimeMin(PIG_BAG"
+        + "(MULTISET_PROJECTION($1, 4)))])\n"
+        + "    LogicalProject(group=[ROW($0, $1, $2)], A=[$3])\n"
+        + "      LogicalAggregate(group=[{0, 1, 2}], A=[COLLECT($3)])\n"
+        + "        LogicalProject(DEPTNO=[$7], MGR=[$3], HIREDATE=[$4], "
+        + "$f3=[ROW($0, $1, $2, $3, $4, $5, $6, $7)])\n"
+        + "          LogicalTableScan(table=[[scott, EMP]])\n";
+
+    final String optimizedPlan = ""
+        + "LogicalSort(sort0=[$4], dir0=[ASC])\n"
+        + "  LogicalProject(DEPTNO=[$0], MGR=[$1], HIREDATE=[$2], $f3=[CAST($3):BIGINT], "
+        + "salSum=[CAST($4):DECIMAL(19, 0)], maxDep=[CAST($5):INTEGER], minHire=[$6])\n"
+        + "    LogicalAggregate(group=[{0, 1, 2}], agg#0=[COUNT()"
+        + "], agg#1=[SUM($3)], agg#2=[MAX($0)], agg#3=[MIN($2)])\n"
+        + "      LogicalProject(DEPTNO=[$7], MGR=[$3], HIREDATE=[$4], SAL=[$5])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    pig(script).assertRel(hasTree(plan))
+        .assertOptimizedRel(hasTree(optimizedPlan));
+
+    final String script2 = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = GROUP A BY (DEPTNO, MGR, HIREDATE);\n"
+        + "C = FOREACH B GENERATE group.DEPTNO, COUNT(A), SUM(A.SAL) as salSum, "
+        + "group.MGR, MAX(A.DEPTNO) as maxDep, MIN(A.HIREDATE) as minHire;\n"
+        + "D = ORDER C BY salSum;\n";
+    final String plan2 = ""
+        + "LogicalSort(sort0=[$2], dir0=[ASC])\n"
+        + "  LogicalProject(DEPTNO=[$0.DEPTNO], $f1=[COUNT(PIG_BAG($1))], "
+        + "salSum=[BigDecimalSum(PIG_BAG(MULTISET_PROJECTION($1, 5)))], "
+        + "MGR=[$0.MGR], maxDep=[IntMax(PIG_BAG(MULTISET_PROJECTION($1, 7)"
+        + "))], minHire=[DateTimeMin(PIG_BAG(MULTISET_PROJECTION($1, 4)))])\n"
+        + "    LogicalProject(group=[ROW($0, $1, $2)], A=[$3])\n"
+        + "      LogicalAggregate(group=[{0, 1, 2}], A=[COLLECT($3)])\n"
+        + "        LogicalProject(DEPTNO=[$7], MGR=[$3], HIREDATE=[$4], "
+        + "$f3=[ROW($0, $1, $2, $3, $4, $5, $6, $7)])\n"
+        + "          LogicalTableScan(table=[[scott, EMP]])\n";
+
+    final String optimizedPlan2 = ""
+        + "LogicalSort(sort0=[$2], dir0=[ASC])\n"
+        + "  LogicalProject(DEPTNO=[$0], $f1=[CAST($3):BIGINT], salSum=[CAST($4):DECIMAL(19, 0)]"
+        + ", MGR=[$1], maxDep=[CAST($5):INTEGER], minHire=[$6])\n"
+        + "    LogicalAggregate(group=[{0, 1, 2}], agg#0=[COUNT()], agg#1=[SUM($3)], "
+        + "agg#2=[MAX($0)], agg#3=[MIN($2)])\n"
+        + "      LogicalProject(DEPTNO=[$7], MGR=[$3], HIREDATE=[$4], SAL=[$5])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    pig(script2).assertRel(hasTree(plan2))
+        .assertOptimizedRel(hasTree(optimizedPlan2));
+  }
+
+  @Test public void testAggregate3() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = GROUP A BY (DEPTNO, MGR, HIREDATE);\n"
+        + "C = FOREACH B GENERATE group, COUNT(A) + 1, BigDecimalSum(A.SAL) as "
+        + "salSum, BigDecimalSum(A.SAL) / COUNT(A) as salAvg;\n"
+        + "D = ORDER C BY salSum;\n";
+    final String plan = ""
+        + "LogicalSort(sort0=[$2], dir0=[ASC])\n"
+        + "  LogicalProject(group=[$0], $f1=[+(COUNT(PIG_BAG($1)), 1)], "
+        + "salSum=[BigDecimalSum(PIG_BAG(MULTISET_PROJECTION($1, 5)))], "
+        + "salAvg=[/(BigDecimalSum(PIG_BAG(MULTISET_PROJECTION($1, 5))), "
+        + "CAST(COUNT(PIG_BAG($1))):DECIMAL(19, 0))])\n"
+        + "    LogicalProject(group=[ROW($0, $1, $2)], A=[$3])\n"
+        + "      LogicalAggregate(group=[{0, 1, 2}], A=[COLLECT($3)])\n"
+        + "        LogicalProject(DEPTNO=[$7], MGR=[$3], HIREDATE=[$4], "
+        + "$f3=[ROW($0, $1, $2, $3, $4, $5, $6, $7)])\n"
+        + "          LogicalTableScan(table=[[scott, EMP]])\n";
+    final String optimizedPlan = ""
+        + "LogicalSort(sort0=[$2], dir0=[ASC])\n"
+        + "  LogicalProject(group=[ROW($0, $1, $2)], $f1=[+(CAST($3):BIGINT, 1)], "
+        + "salSum=[CAST($4):DECIMAL(19, 0)], salAvg=[/(CAST($4):DECIMAL(19, 0), CAST(CAST($3)"
+        + ":BIGINT):DECIMAL(19, 0))])\n"
+        + "    LogicalAggregate(group=[{0, 1, 2}], agg#0=[COUNT()], agg#1=[SUM($3)])\n"
+        + "      LogicalProject(DEPTNO=[$7], MGR=[$3], HIREDATE=[$4], SAL=[$5])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    final String result = ""
+        + "({20, 7902, 1980-12-17},2,800.00,800.00)\n"
+        + "({30, 7698, 1981-12-03},2,950.00,950.00)\n"
+        + "({20, 7788, 1987-05-23},2,1100.00,1100.00)\n"
+        + "({30, 7698, 1981-09-28},2,1250.00,1250.00)\n"
+        + "({30, 7698, 1981-02-22},2,1250.00,1250.00)\n"
+        + "({10, 7782, 1982-01-23},2,1300.00,1300.00)\n"
+        + "({30, 7698, 1981-09-08},2,1500.00,1500.00)\n"
+        + "({30, 7698, 1981-02-20},2,1600.00,1600.00)\n"
+        + "({10, 7839, 1981-06-09},2,2450.00,2450.00)\n"
+        + "({30, 7839, 1981-01-05},2,2850.00,2850.00)\n"
+        + "({20, 7839, 1981-02-04},2,2975.00,2975.00)\n"
+        + "({20, 7566, 1981-12-03},2,3000.00,3000.00)\n"
+        + "({20, 7566, 1987-04-19},2,3000.00,3000.00)\n"
+        + "({10, null, 1981-11-17},2,5000.00,5000.00)\n";
+    final String sql = ""
+        + "SELECT ROW(DEPTNO, MGR, HIREDATE) AS group, CAST(COUNT(*) AS "
+        + "BIGINT) + 1 AS $f1, CAST(SUM(SAL) AS DECIMAL(19, 0)) AS salSum, "
+        + "CAST(SUM(SAL) AS DECIMAL(19, 0)) / CAST(CAST(COUNT(*) AS BIGINT) "
+        + "AS DECIMAL(19, 0)) AS salAvg\n"
+        + "FROM scott.EMP\n"
+        + "GROUP BY DEPTNO, MGR, HIREDATE\n"
+        + "ORDER BY CAST(SUM(SAL) AS DECIMAL(19, 0))";
+    pig(script).assertRel(hasTree(plan))
+        .assertOptimizedRel(hasTree(optimizedPlan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+
+    final String script2 = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = GROUP A BY (DEPTNO, MGR, HIREDATE);\n"
+        + "C = FOREACH B GENERATE group, COUNT(A) + 1, BigDecimalSum(A.SAL) as salSum, "
+        + "BigDecimalSum(A.SAL) / COUNT(A) as salAvg, A;\n"
+        + "D = ORDER C BY salSum;\n";
+    final String sql2 = ""
+        + "SELECT ROW(DEPTNO, MGR, HIREDATE) AS group, CAST(COUNT(*) AS BIGINT) + 1"
+        + " AS $f1, CAST(SUM(SAL) AS DECIMAL(19, 0)) AS salSum, CAST(SUM(SAL) AS "
+        + "DECIMAL(19, 0)) / CAST(CAST(COUNT(*) AS BIGINT) AS DECIMAL(19, 0)) AS "
+        + "salAvg, COLLECT(ROW(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)"
+        + ") AS A\n"
+        + "FROM scott.EMP\n"
+        + "GROUP BY DEPTNO, MGR, HIREDATE\n"
+        + "ORDER BY CAST(SUM(SAL) AS DECIMAL(19, 0))";
+    pig(script2).assertSql(is(sql2));
+
+    final String script3 = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = GROUP A BY (DEPTNO, MGR, HIREDATE);\n"
+        + "C = FOREACH B GENERATE group, A, COUNT(A);\n";
+    final String sql3 = ""
+        + "SELECT ROW(DEPTNO, MGR, HIREDATE) AS group, COLLECT(ROW(EMPNO, ENAME, "
+        + "JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)) AS A, CAST(COUNT(*) AS BIGINT) "
+        + "AS $f2\n"
+        + "FROM scott.EMP\n"
+        + "GROUP BY DEPTNO, MGR, HIREDATE";
+    pig(script3).assertSql(is(sql3));
+  }
+
+  @Test public void testAggregate4() {
+    final String script = ""
+        + "A = LOAD 'scott.EMP' as (EMPNO:int, ENAME:chararray,\n"
+        + "    JOB:chararray, MGR:int, HIREDATE:datetime, SAL:bigdecimal,\n"
+        + "    COMM:bigdecimal, DEPTNO:int);\n"
+        + "B = GROUP A BY (DEPTNO, MGR, HIREDATE);\n"
+        + "C = FOREACH B GENERATE FLATTEN(group) as (DEPTNO, MGR, HIREDATE), "
+        + "COUNT(A), 1L as newCol, A.COMM as comArray, SUM(A.SAL) as salSum;\n"
+        + "D = ORDER C BY salSum;\n";
+    final String plan = ""
+        + "LogicalSort(sort0=[$6], dir0=[ASC])\n"
+        + "  LogicalProject(DEPTNO=[$0.DEPTNO], MGR=[$0.MGR], HIREDATE=[$0.HIREDATE], "
+        + "$f3=[COUNT(PIG_BAG($1))], newCol=[1:BIGINT], comArray=[MULTISET_PROJECTION($1, 6)], "
+        + "salSum=[BigDecimalSum(PIG_BAG(MULTISET_PROJECTION($1, 5)))])\n"
+        + "    LogicalProject(group=[ROW($0, $1, $2)], A=[$3])\n"
+        + "      LogicalAggregate(group=[{0, 1, 2}], A=[COLLECT($3)])\n"
+        + "        LogicalProject(DEPTNO=[$7], MGR=[$3], HIREDATE=[$4], "
+        + "$f3=[ROW($0, $1, $2, $3, $4, $5, $6, $7)])\n"
+        + "          LogicalTableScan(table=[[scott, EMP]])\n";
+    final String optimizedPlan = ""
+        + "LogicalSort(sort0=[$6], dir0=[ASC])\n"
+        + "  LogicalProject(DEPTNO=[$0], MGR=[$1], HIREDATE=[$2], $f3=[CAST($3):BIGINT], "
+        + "newCol=[1:BIGINT], comArray=[$4], salSum=[CAST($5):DECIMAL(19, 0)])\n"
+        + "    LogicalAggregate(group=[{0, 1, 2}], agg#0=[COUNT()], agg#1=[COLLECT($3)], "
+        + "agg#2=[SUM($4)])\n"
+        + "      LogicalProject(DEPTNO=[$7], MGR=[$3], HIREDATE=[$4], COMM=[$6], SAL=[$5])\n"
+        + "        LogicalTableScan(table=[[scott, EMP]])\n";
+    final String sql = ""
+        + "SELECT DEPTNO, MGR, HIREDATE, CAST(COUNT(*) AS BIGINT) AS $f3, 1 AS newCol, "
+        + "COLLECT(COMM) AS comArray, CAST(SUM(SAL) AS DECIMAL(19, 0)) AS salSum\n"
+        + "FROM scott.EMP\n"
+        + "GROUP BY DEPTNO, MGR, HIREDATE\n"
+        + "ORDER BY CAST(SUM(SAL) AS DECIMAL(19, 0))";
+    pig(script).assertRel(hasTree(plan))
+        .assertOptimizedRel(hasTree(optimizedPlan))
+        .assertSql(is(sql));
+  }
+
+  @Test public void testCoGroup() {
+    final String script = ""
+        + "A = LOAD 'scott.DEPT' as (DEPTNO:int, DNAME:chararray, LOC:CHARARRAY);\n"
+        + "B = FILTER A BY DEPTNO <= 30;\n"
+        + "C = FILTER A BY DEPTNO >= 20;\n"
+        + "D = GROUP A BY DEPTNO + 10, B BY (int) DEPTNO, C BY (int) DEPTNO;\n"
+        + "E = ORDER D BY $0;\n";
+    final String plan = ""
+        + "LogicalSort(sort0=[$0], dir0=[ASC])\n"
+        + "  LogicalProject(group=[$0], A=[$1], B=[$2], C=[$3])\n"
+        + "    LogicalProject(DEPTNO=[CASE(IS NOT NULL($0), $0, $3)], A=[$1], B=[$2], C=[$4])\n"
+        + "      LogicalJoin(condition=[=($0, $3)], joinType=[full])\n"
+        + "        LogicalProject(DEPTNO=[CASE(IS NOT NULL($0), $0, $2)], A=[$1], B=[$3])\n"
+        + "          LogicalJoin(condition=[=($0, $2)], joinType=[full])\n"
+        + "            LogicalAggregate(group=[{0}], A=[COLLECT($1)])\n"
+        + "              LogicalProject($f0=[+($0, 10)], $f1=[ROW($0, $1, $2)])\n"
+        + "                LogicalTableScan(table=[[scott, DEPT]])\n"
+        + "            LogicalAggregate(group=[{0}], B=[COLLECT($1)])\n"
+        + "              LogicalProject(DEPTNO=[CAST($0):INTEGER], $f1=[ROW($0, $1, $2)])\n"
+        + "                LogicalFilter(condition=[<=($0, 30)])\n"
+        + "                  LogicalTableScan(table=[[scott, DEPT]])\n"
+        + "        LogicalAggregate(group=[{0}], C=[COLLECT($1)])\n"
+        + "          LogicalProject(DEPTNO=[CAST($0):INTEGER], $f1=[ROW($0, $1, $2)])\n"
+        + "            LogicalFilter(condition=[>=($0, 20)])\n"
+        + "              LogicalTableScan(table=[[scott, DEPT]])\n";
+
+    final String result = ""
+        + "(10,{},{(10,ACCOUNTING,NEW YORK)},{})\n"
+        + "(20,{(10,ACCOUNTING,NEW YORK)},{(20,RESEARCH,DALLAS)},{(20,RESEARCH,DALLAS)})\n"
+        + "(30,{(20,RESEARCH,DALLAS)},{(30,SALES,CHICAGO)},{(30,SALES,CHICAGO)})\n"
+        + "(40,{(30,SALES,CHICAGO)},{},{(40,OPERATIONS,BOSTON)})\n"
+        + "(50,{(40,OPERATIONS,BOSTON)},{},{})\n";
+
+    final String sql = ""
+        + "SELECT CASE WHEN t4.DEPTNO IS NOT NULL THEN t4.DEPTNO ELSE t7.DEPTNO END "
+        + "AS DEPTNO, t4.A, t4.B, t7.C\n"
+        + "FROM (SELECT CASE WHEN t0.$f0 IS NOT NULL THEN t0.$f0 ELSE t3.DEPTNO END "
+        + "AS DEPTNO, t0.A, t3.B\n"
+        + "      FROM (SELECT DEPTNO + 10 AS $f0, COLLECT(ROW(DEPTNO, DNAME, LOC)) AS A\n"
+        + "            FROM scott.DEPT\n"
+        + "            GROUP BY DEPTNO + 10) AS t0\n"
+        + "        FULL JOIN (SELECT CAST(DEPTNO AS INTEGER) AS DEPTNO, COLLECT(ROW"
+        + "(DEPTNO, DNAME, LOC)) AS B\n"
+        + "            FROM scott.DEPT\n"
+        + "            WHERE DEPTNO <= 30\n"
+        + "            GROUP BY CAST(DEPTNO AS INTEGER)) AS t3 ON t0.$f0 = t3.DEPTNO) AS t4\n"
+        + "  FULL JOIN (SELECT CAST(DEPTNO AS INTEGER) AS DEPTNO, COLLECT(ROW(DEPTNO, DNAME, "
+        + "LOC)) AS C\n"
+        + "      FROM scott.DEPT\n"
+        + "      WHERE DEPTNO >= 20\n"
+        + "      GROUP BY CAST(DEPTNO AS INTEGER)) AS t7 ON t4.DEPTNO = t7.DEPTNO\n"
+        + "ORDER BY CASE WHEN t4.DEPTNO IS NOT NULL THEN t4.DEPTNO ELSE t7.DEPTNO END";
+    pig(script).assertRel(hasTree(plan))
+        .assertResult(is(result))
+        .assertSql(is(sql));
+  }
+}
+
+// End PigRelOpTest.java
diff --git a/piglet/src/test/java/org/apache/calcite/test/PigRelTestBase.java b/piglet/src/test/java/org/apache/calcite/test/PigRelTestBase.java
new file mode 100644
index 0000000..3cee48e
--- /dev/null
+++ b/piglet/src/test/java/org/apache/calcite/test/PigRelTestBase.java
@@ -0,0 +1,38 @@
+/*
+ * 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.calcite.test;
+
+import org.apache.calcite.piglet.PigConverter;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.tools.FrameworkConfig;
+
+import org.junit.Before;
+
+/**
+ * Abstract class for Pig to {@link RelNode} tests.
+ */
+public abstract class PigRelTestBase {
+  PigConverter converter;
+
+  @Before
+  public void testSetup() throws Exception {
+    final FrameworkConfig config = PigRelBuilderTest.config().build();
+    converter = PigConverter.create(config);
+  }
+}
+
+// End PigRelTestBase.java
diff --git a/piglet/src/test/java/org/apache/calcite/test/PigScriptTest.java b/piglet/src/test/java/org/apache/calcite/test/PigScriptTest.java
new file mode 100644
index 0000000..19e3615
--- /dev/null
+++ b/piglet/src/test/java/org/apache/calcite/test/PigScriptTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.calcite.test;
+
+import org.apache.calcite.rel.RelNode;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.calcite.test.Matchers.hasTree;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+
+/**
+ * Test for converting a Pig script file.
+ */
+public class PigScriptTest extends PigRelTestBase {
+  private static String projectRootDir;
+  private static String dataFile;
+
+  @BeforeClass
+  public static void setUpOnce() throws IOException {
+    projectRootDir = System.getProperty("user.dir");
+    dataFile = projectRootDir + "/src/test/resources/input.data";
+    List<String> lines = Arrays.asList("yahoo 10", "twitter 3", "facebook 10",
+        "yahoo 15", "facebook 5", "twitter 2");
+    Files.write(Paths.get(dataFile), lines, StandardCharsets.UTF_8);
+  }
+
+  @AfterClass
+  public static void testClean() throws IOException {
+    Files.delete(Paths.get(dataFile));
+  }
+
+  @Test
+  public void testReadScript() throws IOException {
+    Map<String, String> params = new HashMap<>();
+    params.put("input", dataFile);
+    params.put("output", "outputFile");
+
+    final String pigFile = projectRootDir + "/src/test/resources/testPig.pig";
+    final RelNode rel = converter.pigScript2Rel(pigFile, params, true).get(0);
+
+    final String dataFile = projectRootDir + "/src/test/resources/input.data";
+    String expectedPlan = ""
+        + "LogicalSort(sort0=[$1], dir0=[DESC], fetch=[5])\n"
+        + "  LogicalProject(query=[$0], count=[CAST($1):BIGINT])\n"
+        + "    LogicalAggregate(group=[{0}], agg#0=[SUM($1)])\n"
+        + "      LogicalTableScan(table=[[" + dataFile + "]])\n";
+
+    assertThat(rel, hasTree(expectedPlan));
+  }
+}
+
+// End PigScriptTest.java
diff --git a/piglet/src/test/resources/log4j.properties b/piglet/src/test/resources/log4j.properties
index 834e2db..6066699 100644
--- a/piglet/src/test/resources/log4j.properties
+++ b/piglet/src/test/resources/log4j.properties
@@ -13,8 +13,8 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Root logger is configured at INFO and is sent to A1
-log4j.rootLogger=INFO, A1
+# Root logger is configured at ERROR and is sent to A1
+log4j.rootLogger=ERROR, A1
 
 # A1 goes to the console
 log4j.appender.A1=org.apache.log4j.ConsoleAppender
diff --git a/piglet/src/test/resources/testPig.pig b/piglet/src/test/resources/testPig.pig
new file mode 100644
index 0000000..04532e1
--- /dev/null
+++ b/piglet/src/test/resources/testPig.pig
@@ -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.
+
+---------------------------------------------------------------------
+-- Top N Queries.
+--   Counting how many times a query happened
+---------------------------------------------------------------------
+%DECLARE n 5
+data =
+    LOAD '$input'
+    USING PigStorage(' ')
+    AS (query:CHARARRAY, count:INT);
+
+queries_group =
+    GROUP data
+    BY query;
+
+queries_sum =
+     FOREACH queries_group
+     GENERATE
+         group AS query,
+         SUM(data.count) AS count;
+
+queries_ordered =
+    ORDER queries_sum
+    BY count DESC;
+
+queries_limit = LIMIT queries_ordered $n;
+
+STORE queries_limit INTO '$output';
+
+-- End testPig.pig
diff --git a/site/_docs/history.md b/site/_docs/history.md
index db5bc6f..3df8a35 100644
--- a/site/_docs/history.md
+++ b/site/_docs/history.md
@@ -193,7 +193,7 @@ other software versions as specified in `pom.xml`.
 * In `RelFieldCollation`, add a `withX` copy method
 * [<a href='https://issues.apache.org/jira/browse/CALCITE-2953'>CALCITE-2953</a>] `LatticeTest.testTileAlgorithm2` and `LatticeTest.testTileAlgorithm3` fail intermittently
 * [<a href='https://issues.apache.org/jira/browse/CALCITE-574'>CALCITE-574</a>] Remove `org.apache.calcite.util.Bug.CALCITE_461_FIXED`
-* [<a href='https://issues.apache.org/jira/browse/CALCITE-2951'>CALCITE-2951</a>] Support decorrelate sub-query that has aggregate with grouping sets (Haisheng Yuan)
+* [<a href='https://issues.apache.org/jira/browse/CALCITE-2951'>CALCITE-2951</a>] Support decorrelating a sub-query that has aggregate with grouping sets (Haisheng Yuan)
 * [<a href='https://issues.apache.org/jira/browse/CALCITE-2946'>CALCITE-2946</a>] `RelBuilder` wrongly skips creation of Aggregate that prunes columns if input produces one row at most
 * [<a href='https://issues.apache.org/jira/browse/CALCITE-2942'>CALCITE-2943</a>] Materialized view rewriting logic calls `getApplicableMaterializations` each time the rule is triggered
 * [<a href='https://issues.apache.org/jira/browse/CALCITE-2942'>CALCITE-2942</a>] Materialized view rewriting logic instantiates `RelMetadataQuery` each time the rule is triggered