Skip to content

Commit ec1a0fd

Browse files
committed
[CALCITE-6196] Accept BigQuery PERCENTILE functions without OVER clause
Address Mihai comment
1 parent 0f5ffb5 commit ec1a0fd

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

core/src/main/java/org/apache/calcite/rel/core/AggregateCall.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,8 @@ public Aggregate.AggCallBinding createBinding(
467467
final RelDataTypeFactory typeFactory =
468468
aggregateRelBase.getCluster().getTypeFactory();
469469

470-
if (aggFunction.getKind() == SqlKind.PERCENTILE_DISC
471-
|| aggFunction.getKind() == SqlKind.PERCENTILE_CONT) {
472-
assert collation.getKeys().size() == 1;
470+
if (this.argList.size() == 1 && (aggFunction.getKind() == SqlKind.PERCENTILE_DISC
471+
|| aggFunction.getKind() == SqlKind.PERCENTILE_CONT)) {
473472
return new Aggregate.PercentileDiscAggCallBinding(typeFactory,
474473
aggFunction, SqlTypeUtil.projectTypes(rowType, argList),
475474
SqlTypeUtil.projectTypes(rowType, collation.getKeys()).get(0),

core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperators.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,6 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
711711
ReturnTypes.DOUBLE,
712712
OperandTypes.NUMERIC_UNIT_INTERVAL_NUMERIC_LITERAL)
713713
.withFunctionType(SqlFunctionCategory.SYSTEM)
714-
.withOver(true)
715714
.withPercentile(true)
716715
.withAllowsNullTreatment(true)
717716
.withAllowsFraming(false);
@@ -726,7 +725,6 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
726725
ReturnTypes.ARG0,
727726
OperandTypes.NUMERIC_UNIT_INTERVAL_NUMERIC_LITERAL)
728727
.withFunctionType(SqlFunctionCategory.SYSTEM)
729-
.withOver(true)
730728
.withPercentile(true)
731729
.withAllowsNullTreatment(true)
732730
.withAllowsFraming(false);

core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6209,7 +6209,7 @@ private SqlNode navigationInDefine(SqlNode node, String alpha) {
62096209
// Because there are two forms of the PERCENTILE_CONT/PERCENTILE_DISC functions,
62106210
// they are distinguished by their operand count and then validated accordingly.
62116211
// For example, the standard single operand form requires group order while the
6212-
// 2-operand form allows for null treatment and requires an OVER() clause.
6212+
// 2-operand form allows for null treatment.
62136213
if (op.isPercentile()) {
62146214
switch (aggCall.operandCount()) {
62156215
case 1:
@@ -6235,7 +6235,6 @@ private SqlNode navigationInDefine(SqlNode node, String alpha) {
62356235
break;
62366236
case 2:
62376237
assert op.allowsNullTreatment();
6238-
assert op.requiresOver();
62396238
assert op.requiresGroupOrder() == Optionality.FORBIDDEN;
62406239
break;
62416240
default:

core/src/test/java/org/apache/calcite/rel/rel2sql/RelToSqlConverterTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,30 @@ private static String toSql(RelNode root, SqlDialect dialect,
12951295
.withMysql().ok(expectedMysql);
12961296
}
12971297

1298+
/** Test case for
1299+
* <a href="https://issues.apache.org/jira/browse/CALCITE-6196">[CALCITE-6196]
1300+
* Remove OVER requirement for BigQuery PERCENTILE_CONT/DISC</a>. */
1301+
@Test void testPercentileContFunctionWithoutOver() {
1302+
final String query = "select percentile_cont(\"product_id\", .5) "
1303+
+ "from \"foodmart\".\"product\"";
1304+
final String expected = "SELECT PERCENTILE_CONT(product_id, 0.5)\n"
1305+
+ "FROM foodmart.product";
1306+
1307+
sql(query).withBigQuery().withLibrary(SqlLibrary.BIG_QUERY).ok(expected);
1308+
}
1309+
1310+
/** Test case for
1311+
* <a href="https://issues.apache.org/jira/browse/CALCITE-6196">[CALCITE-6196]
1312+
* Remove OVER requirement for BigQuery PERCENTILE_CONT/DISC</a>. */
1313+
@Test void testPercentileDiscFunctionWithoutOver() {
1314+
final String query = "select percentile_disc(\"product_id\", .5) "
1315+
+ "from \"foodmart\".\"product\"";
1316+
final String expected = "SELECT PERCENTILE_DISC(product_id, 0.5)\n"
1317+
+ "FROM foodmart.product";
1318+
1319+
sql(query).withBigQuery().withLibrary(SqlLibrary.BIG_QUERY).ok(expected);
1320+
}
1321+
12981322
/** As {@link #testSum0BecomesCoalesce()} but for windowed aggregates. */
12991323
@Test void testWindowedSum0BecomesCoalesce() {
13001324
final String query = "select\n"

0 commit comments

Comments
 (0)