code: public AggregationBuilder buildAggregation(KunderaQuery query, EntityMetadata entityMetadata, QueryBuilder filter)
      {
          SelectStatement selectStatement = query.getSelectStatement();
  
          // To apply filter for where clause
          AggregationBuilder aggregationBuilder = buildWhereAggregations(entityMetadata, filter);
          if (KunderaQueryUtils.hasGroupBy(query.getJpqlExpression()))
          {
              TermsBuilder termsBuilder = processGroupByClause(selectStatement.getGroupByClause(), entityMetadata, query);
              aggregationBuilder.subAggregation(termsBuilder);
          }
          else
          {
              if (KunderaQueryUtils.hasHaving(query.getJpqlExpression()))
              {
                  logger.error("Identified having clause without group by, Throwing not supported operation Exception");
                  throw new UnsupportedOperationException(
                          "Currently, Having clause without group by caluse is not supported.");
              }
              else
              {
                  aggregationBuilder = (selectStatement != null) ? query.isAggregated() ? buildSelectAggregations(
                          aggregationBuilder, selectStatement, entityMetadata) : null : null;
              }
          }
  
          return aggregationBuilder;
      }
docs: Use aggregation.
  
  @param query
  the query
  @param entityMetadata
  the entity metadata
  @param filter
  the filter
  @return the filter aggregation builder