[CALCITE-3300] In JDBC adapter, when generating SQL for count star, generates the star argument of the call (Wang Weidong)
Generates the "count(*)" instead of "count()" which is illegal.
close apache/calcite#1422
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 fb6153e..8795cbb 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
@@ -61,6 +61,7 @@
import org.apache.calcite.sql.SqlSetOperator;
import org.apache.calcite.sql.SqlWindow;
import org.apache.calcite.sql.fun.SqlCase;
+import org.apache.calcite.sql.fun.SqlCountAggFunction;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.fun.SqlSumEmptyIsZeroAggFunction;
import org.apache.calcite.sql.parser.SqlParserPos;
@@ -889,6 +890,12 @@
for (int arg : aggCall.getArgList()) {
operandList.add(field(arg));
}
+
+ if ((op instanceof SqlCountAggFunction) && operandList.isEmpty()) {
+ // If there is no parameter in "count" function, add a star identifier to it
+ operandList.add(SqlIdentifier.star(POS));
+ }
+
final SqlLiteral qualifier =
aggCall.isDistinct() ? SqlSelectKeyword.DISTINCT.symbol(POS) : null;
final SqlNode[] operands = operandList.toArray(new SqlNode[0]);
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 031cdb7..d3045eb 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
@@ -4038,6 +4038,14 @@
});
}
+ @Test public void testSelectCountStar() {
+ final String query = "select count(*) from \"product\"";
+ final String expected = "SELECT COUNT(*)\n"
+ + "FROM \"foodmart\".\"product\"";
+ Sql sql = sql(query);
+ sql.ok(expected);
+ }
+
/** Fluid interface to run tests. */
static class Sql {
private final SchemaPlus schema;