Skip to content

Commit

Permalink
[CALCITE-6196] Accept BigQuery PERCENTILE functions without OVER clause
Browse files Browse the repository at this point in the history
Address Mihai comment
  • Loading branch information
tanclary authored and olivrlee committed Oct 17, 2024
1 parent fa9d037 commit 5562ec1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,8 @@ public Aggregate.AggCallBinding createBinding(
final RelDataTypeFactory typeFactory =
aggregateRelBase.getCluster().getTypeFactory();

if (aggFunction.getKind() == SqlKind.PERCENTILE_DISC
|| aggFunction.getKind() == SqlKind.PERCENTILE_CONT) {
assert collation.getKeys().size() == 1;
if (this.argList.size() == 1 && (aggFunction.getKind() == SqlKind.PERCENTILE_DISC
|| aggFunction.getKind() == SqlKind.PERCENTILE_CONT)) {
return new Aggregate.PercentileDiscAggCallBinding(typeFactory,
aggFunction, SqlTypeUtil.projectTypes(rowType, argList),
SqlTypeUtil.projectTypes(rowType, collation.getKeys()).get(0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,6 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
ReturnTypes.DOUBLE,
OperandTypes.NUMERIC_UNIT_INTERVAL_NUMERIC_LITERAL)
.withFunctionType(SqlFunctionCategory.SYSTEM)
.withOver(true)
.withPercentile(true)
.withAllowsNullTreatment(true)
.withAllowsFraming(false);
Expand All @@ -886,7 +885,6 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
ReturnTypes.ARG0,
OperandTypes.NUMERIC_UNIT_INTERVAL_NUMERIC_LITERAL)
.withFunctionType(SqlFunctionCategory.SYSTEM)
.withOver(true)
.withPercentile(true)
.withAllowsNullTreatment(true)
.withAllowsFraming(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6561,7 +6561,7 @@ private SqlNode navigationInDefine(SqlNode node, String alpha) {
// Because there are two forms of the PERCENTILE_CONT/PERCENTILE_DISC functions,
// they are distinguished by their operand count and then validated accordingly.
// For example, the standard single operand form requires group order while the
// 2-operand form allows for null treatment and requires an OVER() clause.
// 2-operand form allows for null treatment.
if (op.isPercentile()) {
switch (aggCall.operandCount()) {
case 1:
Expand All @@ -6586,7 +6586,6 @@ private SqlNode navigationInDefine(SqlNode node, String alpha) {
break;
case 2:
assert op.allowsNullTreatment();
assert op.requiresOver();
assert op.requiresGroupOrder() == Optionality.FORBIDDEN;
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,30 @@ private static String toSql(RelNode root, SqlDialect dialect,
.withMysql().ok(expectedMysql);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6196">[CALCITE-6196]
* Remove OVER requirement for BigQuery PERCENTILE_CONT/DISC</a>. */
@Test void testPercentileContFunctionWithoutOver() {
final String query = "select percentile_cont(\"product_id\", .5) "
+ "from \"foodmart\".\"product\"";
final String expected = "SELECT PERCENTILE_CONT(product_id, 0.5)\n"
+ "FROM foodmart.product";

sql(query).withBigQuery().withLibrary(SqlLibrary.BIG_QUERY).ok(expected);
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-6196">[CALCITE-6196]
* Remove OVER requirement for BigQuery PERCENTILE_CONT/DISC</a>. */
@Test void testPercentileDiscFunctionWithoutOver() {
final String query = "select percentile_disc(\"product_id\", .5) "
+ "from \"foodmart\".\"product\"";
final String expected = "SELECT PERCENTILE_DISC(product_id, 0.5)\n"
+ "FROM foodmart.product";

sql(query).withBigQuery().withLibrary(SqlLibrary.BIG_QUERY).ok(expected);
}

/** As {@link #testSum0BecomesCoalesce()} but for windowed aggregates. */
@Test void testWindowedSum0BecomesCoalesce() {
final String query = "select\n"
Expand Down

0 comments on commit 5562ec1

Please sign in to comment.