calcite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jh...@apache.org
Subject [7/7] incubator-calcite git commit: Fix Piglet DUMP applied to multisets and structs
Date Wed, 07 Oct 2015 19:15:30 GMT
Fix Piglet DUMP applied to multisets and structs


Project: http://git-wip-us.apache.org/repos/asf/incubator-calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-calcite/commit/26f303e8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-calcite/tree/26f303e8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-calcite/diff/26f303e8

Branch: refs/heads/master
Commit: 26f303e8334c44567b7584b2525f726919740b72
Parents: c050790
Author: Julian Hyde <jhyde@apache.org>
Authored: Tue Oct 6 14:09:59 2015 -0700
Committer: Julian Hyde <jhyde@apache.org>
Committed: Tue Oct 6 23:54:11 2015 -0700

----------------------------------------------------------------------
 .../org/apache/calcite/tools/PigRelBuilder.java |  8 ++-
 .../org/apache/calcite/test/CalciteHandler.java | 62 ++++++++++++++++----
 .../org/apache/calcite/test/PigletTest.java     |  6 +-
 3 files changed, 60 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/26f303e8/core/src/main/java/org/apache/calcite/tools/PigRelBuilder.java
----------------------------------------------------------------------
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 c4ef26a..be62155 100644
--- a/core/src/main/java/org/apache/calcite/tools/PigRelBuilder.java
+++ b/core/src/main/java/org/apache/calcite/tools/PigRelBuilder.java
@@ -132,9 +132,13 @@ public class PigRelBuilder extends RelBuilder {
         r = build();
       }
       String alias = getAlias();
+      // Create a ROW to pass to COLLECT. Interestingly, this is not allowed
+      // by standard SQL; see [CALCITE-877] Allow ROW as argument to COLLECT.
+      final RexNode row =
+          cluster.getRexBuilder().makeCall(peek(1, 0).getRowType(),
+              SqlStdOperatorTable.ROW, fields());
       aggregate(groupKey.e,
-          aggregateCall(SqlStdOperatorTable.COLLECT, false, alias,
-              fields()));
+          aggregateCall(SqlStdOperatorTable.COLLECT, false, null, alias, row));
       if (groupKey.i < n - 1) {
         push(r);
         List<RexNode> predicates = new ArrayList<>();

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/26f303e8/piglet/src/test/java/org/apache/calcite/test/CalciteHandler.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 c45e7b2..1c83e2f 100644
--- a/piglet/src/test/java/org/apache/calcite/test/CalciteHandler.java
+++ b/piglet/src/test/java/org/apache/calcite/test/CalciteHandler.java
@@ -25,9 +25,11 @@ import com.google.common.base.Throwables;
 
 import java.io.PrintWriter;
 import java.io.Writer;
+import java.sql.Array;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.sql.Types;
 
 /**
  * Extension to {@link org.apache.calcite.piglet.Handler} that can execute
@@ -44,22 +46,60 @@ class CalciteHandler extends Handler {
   @Override protected void dump(RelNode rel) {
     try (final PreparedStatement preparedStatement = RelRunners.run(rel)) {
       final ResultSet resultSet = preparedStatement.executeQuery();
-      final int columnCount = resultSet.getMetaData().getColumnCount();
-      while (resultSet.next()) {
-        if (columnCount == 0) {
+      dump(resultSet, true);
+    } catch (SQLException e) {
+      throw Throwables.propagate(e);
+    }
+  }
+
+  private void dump(ResultSet resultSet, boolean newline) throws SQLException {
+    final int columnCount = resultSet.getMetaData().getColumnCount();
+    int r = 0;
+    while (resultSet.next()) {
+      if (!newline && r++ > 0) {
+        writer.print(",");
+      }
+      if (columnCount == 0) {
+        if (newline) {
           writer.println("()");
         } else {
-          writer.print('(');
-          writer.print(resultSet.getObject(1));
-          for (int i = 1; i < columnCount; i++) {
-            writer.print(',');
-            writer.print(resultSet.getString(i + 1));
-          }
+          writer.print("()");
+        }
+      } else {
+        writer.print('(');
+        dumpColumn(resultSet, 1);
+        for (int i = 2; i <= columnCount; i++) {
+          writer.print(',');
+          dumpColumn(resultSet, i);
+        }
+        if (newline) {
           writer.println(')');
+        } else {
+          writer.print(")");
         }
       }
-    } catch (SQLException e) {
-      throw Throwables.propagate(e);
+    }
+  }
+
+  /** Dumps a column value.
+   *
+   * @param i Column ordinal, 1-based
+   */
+  private void dumpColumn(ResultSet resultSet, int i) 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);
+      writer.print("}");
+      return;
+    case Types.REAL:
+      writer.print(resultSet.getString(i));
+      writer.print("F");
+      return;
+    default:
+      writer.print(resultSet.getString(i));
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-calcite/blob/26f303e8/piglet/src/test/java/org/apache/calcite/test/PigletTest.java
----------------------------------------------------------------------
diff --git a/piglet/src/test/java/org/apache/calcite/test/PigletTest.java b/piglet/src/test/java/org/apache/calcite/test/PigletTest.java
index a3092ef..c40452a 100644
--- a/piglet/src/test/java/org/apache/calcite/test/PigletTest.java
+++ b/piglet/src/test/java/org/apache/calcite/test/PigletTest.java
@@ -135,12 +135,12 @@ public class PigletTest {
     final String s = "A = LOAD 'EMP';\n"
         + "B = GROUP A BY DEPTNO;";
     final String expected = ""
-        + "LogicalAggregate(group=[{7}], A=[COLLECT($0, $1, $2, $3, $4, $5, $6, $7)])\n"
-        + "  LogicalTableScan(table=[[scott, EMP]])\n";
+        + "LogicalAggregate(group=[{7}], A=[COLLECT($8)])\n"
+        + "  LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5],
COMM=[$6], DEPTNO=[$7], $f8=[ROW($0, $1, $2, $3, $4, $5, $6, $7)])\n"
+        + "    LogicalTableScan(table=[[scott, EMP]])\n";
     pig(s).explainContains(expected);
   }
 
-  @Ignore("only collect first column, should collect a struct")
   @Test public void testGroupExample() throws ParseException {
     final String pre = "A = VALUES ('John',18,4.0F),\n"
         + "('Mary',19,3.8F),\n"


Mime
View raw message