weidong3630 commented on a change in pull request #1606: [CALCITE-3466] Do not
drop the "group by" clause of subquery in a select statement with "group by"
clause in JDBC adapter.
URL: https://github.com/apache/calcite/pull/1606#discussion_r365819703
##########
File path:
core/src/main/java/org/apache/calcite/rel/rel2sql/SqlImplementor.java
##########
@@ -1224,10 +1224,18 @@ public Builder builder(RelNode rel, Clause... clauses)
{
needNew = true;
}
}
- if (rel instanceof Aggregate
- && !dialect.supportsNestedAggregations()
- && hasNestedAggregations((Aggregate) rel)) {
- needNew = true;
+
+ if (rel instanceof Aggregate) {
+ Aggregate e = (Aggregate) rel;
+ final boolean nestedAgg = hasNestedAggregations(e);
+ if (withGroupBy() && (!e.getGroupSet().toList().isEmpty()
+ || !nestedAgg || !dialect.supportsNestedAggregations())) {
Review comment:
> How about this code:
>
> ```java
> if (rel instanceof Aggregate) {
> final Aggregate agg = (Aggregate) rel;
> final boolean hasNestedAgg = hasNestedAggregations(agg);
> if (!dialect.supportsNestedAggregations()
> && hasNestedAgg) {
> needNew = true;
> } else {
> if (this.clauses.contains(Clause.GROUP_BY)) {
> // Avoids to lose the distinct attribute of inner agg.
> if (!hasNestedAgg || Aggregate.isNotGrandTotal(agg)) {
> needNew = true;
> }
> }
> }
> }
> ```
Yeah, it's simpler to my code and it's equivalent to my code.But I think
that the logic is still a bit confusing.
How about this:
```java
if (rel instanceof Aggregate) {
final Aggregate agg = (Aggregate) rel;
final boolean hasNestedAgg = hasNestedAggregations(agg);
if (hasNestedAgg) {
if (!dialect.supportsNestedAggregations()) {
needNew = true;
}
} else {
if (this.clauses.contains(Clause.GROUP_BY) &&
Aggregate.isNotGrandTotal(agg)) {
// Avoids to lose the distinct attribute of inner agg.
needNew = true;
}
}
}
```
Is the logic more clear?
----------------------------------------------------------------
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.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services