code: private Terms filterBuckets(Terms buckets, KunderaQuery query)
      {
          Expression havingClause = query.getSelectStatement().getHavingClause();
  
          if (!(havingClause instanceof NullExpression) && havingClause != null)
          {
              Expression conditionalExpression = ((HavingClause) havingClause).getConditionalExpression();
  
              for (Iterator<Bucket> bucketIterator = buckets.getBuckets().iterator(); bucketIterator.hasNext();)
              {
                  InternalAggregations internalAgg = (InternalAggregations) bucketIterator.next().getAggregations();
                  if (!isValidBucket(internalAgg, query, conditionalExpression))
                  {
                      bucketIterator.remove();
                  }
              }
          }
          return buckets;
      }
docs: Filter buckets.
  
  @param buckets
  the buckets
  @param query
  the query
  @return the terms