All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.druid.sql.calcite.rel.Grouping Maven / Gradle / Ivy

/*
 * Licensed to Metamarkets Group Inc. (Metamarkets) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Metamarkets licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.druid.sql.calcite.rel;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import io.druid.java.util.common.ISE;
import io.druid.query.aggregation.AggregatorFactory;
import io.druid.query.aggregation.PostAggregator;
import io.druid.query.dimension.DimensionSpec;
import io.druid.sql.calcite.aggregation.Aggregation;

import java.util.List;
import java.util.Set;

public class Grouping
{
  private final List dimensions;
  private final List aggregations;

  private Grouping(
      final List dimensions,
      final List aggregations
  )
  {
    this.dimensions = ImmutableList.copyOf(dimensions);
    this.aggregations = ImmutableList.copyOf(aggregations);

    // Verify no collisions.
    final Set seen = Sets.newHashSet();
    for (DimensionSpec dimensionSpec : dimensions) {
      if (!seen.add(dimensionSpec.getOutputName())) {
        throw new ISE("Duplicate field name: %s", dimensionSpec.getOutputName());
      }
    }
    for (Aggregation aggregation : aggregations) {
      for (AggregatorFactory aggregatorFactory : aggregation.getAggregatorFactories()) {
        if (!seen.add(aggregatorFactory.getName())) {
          throw new ISE("Duplicate field name: %s", aggregatorFactory.getName());
        }
      }
      if (aggregation.getPostAggregator() != null && !seen.add(aggregation.getPostAggregator().getName())) {
        throw new ISE("Duplicate field name in rowOrder: %s", aggregation.getPostAggregator().getName());
      }
    }
  }

  public static Grouping create(
      final List dimensions,
      final List aggregations
  )
  {
    return new Grouping(dimensions, aggregations);
  }

  public List getDimensions()
  {
    return dimensions;
  }

  public List getAggregations()
  {
    return aggregations;
  }

  public List getAggregatorFactories()
  {
    final List retVal = Lists.newArrayList();
    for (final Aggregation aggregation : aggregations) {
      retVal.addAll(aggregation.getAggregatorFactories());
    }
    return retVal;
  }

  public List getPostAggregators()
  {
    final List retVal = Lists.newArrayList();
    for (final Aggregation aggregation : aggregations) {
      if (aggregation.getPostAggregator() != null) {
        retVal.add(aggregation.getPostAggregator());
      }
    }
    return retVal;
  }

  @Override
  public boolean equals(Object o)
  {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    Grouping grouping = (Grouping) o;

    if (dimensions != null ? !dimensions.equals(grouping.dimensions) : grouping.dimensions != null) {
      return false;
    }
    return aggregations != null ? aggregations.equals(grouping.aggregations) : grouping.aggregations == null;

  }

  @Override
  public int hashCode()
  {
    int result = dimensions != null ? dimensions.hashCode() : 0;
    result = 31 * result + (aggregations != null ? aggregations.hashCode() : 0);
    return result;
  }

  @Override
  public String toString()
  {
    return "Grouping{" +
           "dimensions=" + dimensions +
           ", aggregations=" + aggregations +
           '}';
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy