private List<Query> buildGroupIntervalQuery(DataSetGroup group) {
    if (group == null || !group.isSelect()) return null;

    List<Query> result = new LinkedList<Query>();
    String sourceId = group.getColumnGroup().getSourceId();
    ColumnType columnType = metadata.getColumnType(sourceId);
    ElasticSearchDataSetDef def = (ElasticSearchDataSetDef) metadata.getDefinition();

    List<Interval> intervals = group.getSelectedIntervalList();
    for (Interval interval : intervals) {
      Query _result = null;
      boolean isLabelCol = ColumnType.LABEL.equals(columnType);
      boolean isNumericCol = ColumnType.NUMBER.equals(columnType);
      boolean isDateCol = ColumnType.DATE.equals(columnType);
      boolean isTextCol = ColumnType.TEXT.equals(columnType);

      if (isTextCol) {
        throw new IllegalArgumentException(
            "Not supported type ["
                + columnType.name()
                + "] for column with id ["
                + sourceId
                + "] using grouping.");
      }

      if (isLabelCol) {
        String filterValue = valueTypeMapper.formatLabel(def, sourceId, interval.getName());
        _result = new Query(sourceId, Query.Type.TERM);
        _result.setParam(Query.Parameter.VALUE.name(), filterValue);
      } else if (isNumericCol || isDateCol) {
        Object maxValue = interval.getMaxValue();
        Object minValue = interval.getMinValue();
        Object value0 =
            isNumericCol
                ? valueTypeMapper.formatNumeric(def, sourceId, (Double) minValue)
                : valueTypeMapper.formatDate(def, sourceId, (Date) minValue);
        Object value1 =
            isNumericCol
                ? valueTypeMapper.formatNumeric(def, sourceId, (Double) maxValue)
                : valueTypeMapper.formatDate(def, sourceId, (Date) maxValue);
        _result = new Query(sourceId, Query.Type.RANGE);
        _result.setParam(Query.Parameter.GT.name(), value0);
        _result.setParam(Query.Parameter.LT.name(), value1);
      }

      result.add(_result);
    }

    return result;
  }
  private String formatValue(ElasticSearchDataSetDef definition, String columnId, Object value) {
    ColumnType columnType = metadata.getColumnType(columnId);
    boolean isLabelCol = ColumnType.LABEL.equals(columnType);
    boolean isNumericCol = ColumnType.NUMBER.equals(columnType);
    boolean isDateCol = ColumnType.DATE.equals(columnType);
    boolean isTextCol = ColumnType.TEXT.equals(columnType);
    if (isTextCol) {
      return valueTypeMapper.formatText(definition, columnId, (String) value);
    } else if (isLabelCol) {
      return valueTypeMapper.formatLabel(definition, columnId, (String) value);
    } else if (isDateCol) {
      return valueTypeMapper.formatDate(definition, columnId, (Date) value);
    } else if (isNumericCol) {
      return valueTypeMapper.formatNumeric(definition, columnId, (Double) value);
    }

    throw new IllegalArgumentException(
        "Not supported type [" + columnType.name() + "] for column id [" + columnId + "].");
  }