Skip to content

Commit 641f5b6

Browse files
tanclaryolivrlee
authored andcommitted
[CALCITE-6196] Accept BigQuery PERCENTILE functions without OVER clause
Address Mihai comment
1 parent 0fa8dae commit 641f5b6

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
@@ -512,9 +512,8 @@ public Aggregate.AggCallBinding createBinding(
512512
final RelDataTypeFactory typeFactory =
513513
aggregateRelBase.getCluster().getTypeFactory();
514514

515-
if (aggFunction.getKind() == SqlKind.PERCENTILE_DISC
516-
|| aggFunction.getKind() == SqlKind.PERCENTILE_CONT) {
517-
assert collation.getKeys().size() == 1;
515+
if (this.argList.size() == 1 && (aggFunction.getKind() == SqlKind.PERCENTILE_DISC
516+
|| aggFunction.getKind() == SqlKind.PERCENTILE_CONT)) {
518517
return new Aggregate.PercentileDiscAggCallBinding(typeFactory,
519518
aggFunction, SqlTypeUtil.projectTypes(rowType, argList),
520519
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
@@ -871,7 +871,6 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
871871
ReturnTypes.DOUBLE,
872872
OperandTypes.NUMERIC_UNIT_INTERVAL_NUMERIC_LITERAL)
873873
.withFunctionType(SqlFunctionCategory.SYSTEM)
874-
.withOver(true)
875874
.withPercentile(true)
876875
.withAllowsNullTreatment(true)
877876
.withAllowsFraming(false);
@@ -886,7 +885,6 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
886885
ReturnTypes.ARG0,
887886
OperandTypes.NUMERIC_UNIT_INTERVAL_NUMERIC_LITERAL)
888887
.withFunctionType(SqlFunctionCategory.SYSTEM)
889-
.withOver(true)
890888
.withPercentile(true)
891889
.withAllowsNullTreatment(true)
892890
.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
@@ -6561,7 +6561,7 @@ private SqlNode navigationInDefine(SqlNode node, String alpha) {
65616561
// Because there are two forms of the PERCENTILE_CONT/PERCENTILE_DISC functions,
65626562
// they are distinguished by their operand count and then validated accordingly.
65636563
// For example, the standard single operand form requires group order while the
6564-
// 2-operand form allows for null treatment and requires an OVER() clause.
6564+
// 2-operand form allows for null treatment.
65656565
if (op.isPercentile()) {
65666566
switch (aggCall.operandCount()) {
65676567
case 1:
@@ -6586,7 +6586,6 @@ private SqlNode navigationInDefine(SqlNode node, String alpha) {
65866586
break;
65876587
case 2:
65886588
assert op.allowsNullTreatment();
6589-
assert op.requiresOver();
65906589
assert op.requiresGroupOrder() == Optionality.FORBIDDEN;
65916590
break;
65926591
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
@@ -1374,6 +1374,30 @@ private static String toSql(RelNode root, SqlDialect dialect,
13741374
.withMysql().ok(expectedMysql);
13751375
}
13761376

1377+
/** Test case for
1378+
* <a href="https://issues.apache.org/jira/browse/CALCITE-6196">[CALCITE-6196]
1379+
* Remove OVER requirement for BigQuery PERCENTILE_CONT/DISC</a>. */
1380+
@Test void testPercentileContFunctionWithoutOver() {
1381+
final String query = "select percentile_cont(\"product_id\", .5) "
1382+
+ "from \"foodmart\".\"product\"";
1383+
final String expected = "SELECT PERCENTILE_CONT(product_id, 0.5)\n"
1384+
+ "FROM foodmart.product";
1385+
1386+
sql(query).withBigQuery().withLibrary(SqlLibrary.BIG_QUERY).ok(expected);
1387+
}
1388+
1389+
/** Test case for
1390+
* <a href="https://issues.apache.org/jira/browse/CALCITE-6196">[CALCITE-6196]
1391+
* Remove OVER requirement for BigQuery PERCENTILE_CONT/DISC</a>. */
1392+
@Test void testPercentileDiscFunctionWithoutOver() {
1393+
final String query = "select percentile_disc(\"product_id\", .5) "
1394+
+ "from \"foodmart\".\"product\"";
1395+
final String expected = "SELECT PERCENTILE_DISC(product_id, 0.5)\n"
1396+
+ "FROM foodmart.product";
1397+
1398+
sql(query).withBigQuery().withLibrary(SqlLibrary.BIG_QUERY).ok(expected);
1399+
}
1400+
13771401
/** As {@link #testSum0BecomesCoalesce()} but for windowed aggregates. */
13781402
@Test void testWindowedSum0BecomesCoalesce() {
13791403
final String query = "select\n"

0 commit comments

Comments
 (0)