You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2022/05/24 01:57:41 UTC

[GitHub] [calcite] chunweilei commented on a diff in pull request #2812: [CALCITE-5163] MysqlSqlDialect support to unparse LISTAGG aggregate function

chunweilei commented on code in PR #2812:
URL: https://github.com/apache/calcite/pull/2812#discussion_r879990874


##########
core/src/main/java/org/apache/calcite/sql/dialect/MysqlSqlDialect.java:
##########
@@ -223,11 +229,56 @@ public MysqlSqlDialect(Context context) {
       unparseFloor(writer, call);
       break;
 
+    case WITHIN_GROUP:
+      final List<SqlNode> operands = call.getOperandList();
+      if (operands.size() <= 0 || operands.get(0).getKind() != SqlKind.LISTAGG) {
+        super.unparseCall(writer, call, leftPrec, rightPrec);
+        return;
+      }
+      unparseListAggCall(writer, (SqlCall) operands.get(0),
+          operands.size() == 2 ? operands.get(1) : null, leftPrec, rightPrec);
+      break;
+
+    case LISTAGG:
+      unparseListAggCall(writer, call, null, leftPrec, rightPrec);
+      break;
+
     default:
       super.unparseCall(writer, call, leftPrec, rightPrec);
     }
   }
 
+  /**
+   * Unparses LISTAGG for MySQL. This call is translated to GROUP_CONCAT.
+   *
+   * @param writer Writer
+   * @param listAggCall Call of LISTAGG
+   * @param orderItemNode Elems of WITHIN_GROUP, NULL means none elem in the WITHIN_GROUP
+   * @param leftPrec Origin leftPrec
+   * @param rightPrec Origin rightPrec
+   */
+  private void unparseListAggCall(SqlWriter writer, SqlCall listAggCall,
+      @Nullable SqlNode orderItemNode, int leftPrec, int rightPrec) {
+    // LISTAGG(DISTINCT c1, ',') WITHIN GROUP (ORDER BY c2, c3)
+    // GROUP_CONCAT(DISTINCT c1 ORDER BY c2, c3 DESC SEPARATOR ',')
+    final List<SqlNode> listAggCallOperands = listAggCall.getOperandList();

Review Comment:
   Could you please move comments to the java doc comment?



##########
core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java:
##########
@@ -2457,6 +2457,30 @@ private SqlDialect nonOrdinalDialect() {
     sql(query).withMysql().ok(expected);
   }
 
+  @Test void testMySqlGroupConcat() {
+    final String query = "select\n"
+        + "listagg(distinct \"product_name\", ',') within group(order by \"cases_per_pallet\"),\n"
+        + "listagg(\"product_name\", ',') within group(order by \"cases_per_pallet\"),\n"

Review Comment:
   From my perspective, it would be better to change the test name to `testMySqlUnparseListAggCall`.



##########
core/src/main/java/org/apache/calcite/sql/dialect/MysqlSqlDialect.java:
##########
@@ -223,11 +229,56 @@ public MysqlSqlDialect(Context context) {
       unparseFloor(writer, call);
       break;
 
+    case WITHIN_GROUP:
+      final List<SqlNode> operands = call.getOperandList();
+      if (operands.size() <= 0 || operands.get(0).getKind() != SqlKind.LISTAGG) {
+        super.unparseCall(writer, call, leftPrec, rightPrec);
+        return;
+      }
+      unparseListAggCall(writer, (SqlCall) operands.get(0),
+          operands.size() == 2 ? operands.get(1) : null, leftPrec, rightPrec);
+      break;
+
+    case LISTAGG:
+      unparseListAggCall(writer, call, null, leftPrec, rightPrec);
+      break;
+
     default:
       super.unparseCall(writer, call, leftPrec, rightPrec);
     }
   }
 
+  /**
+   * Unparses LISTAGG for MySQL. This call is translated to GROUP_CONCAT.
+   *
+   * @param writer Writer
+   * @param listAggCall Call of LISTAGG
+   * @param orderItemNode Elems of WITHIN_GROUP, NULL means none elem in the WITHIN_GROUP
+   * @param leftPrec Origin leftPrec
+   * @param rightPrec Origin rightPrec
+   */
+  private void unparseListAggCall(SqlWriter writer, SqlCall listAggCall,
+      @Nullable SqlNode orderItemNode, int leftPrec, int rightPrec) {
+    // LISTAGG(DISTINCT c1, ',') WITHIN GROUP (ORDER BY c2, c3)
+    // GROUP_CONCAT(DISTINCT c1 ORDER BY c2, c3 DESC SEPARATOR ',')
+    final List<SqlNode> listAggCallOperands = listAggCall.getOperandList();

Review Comment:
   Could you please move comments to the java doc comment?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org