/*
 * Druid - a distributed column store.
 * Copyright (C) 2012, 2013  Metamarkets Group Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package io.druid.segment.indexing;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.api.client.util.Sets;
import io.druid.data.input.impl.InputRowParser;
import io.druid.query.aggregation.AggregatorFactory;
import io.druid.segment.indexing.granularity.GranularitySpec;
import io.druid.segment.indexing.granularity.UniformGranularitySpec;

import java.util.Set;

/**
 */
public class DataSchema
{
  private final String dataSource;
  private final InputRowParser parser;
  private final AggregatorFactory[] aggregators;
  private final GranularitySpec granularitySpec;

  @JsonCreator
  public DataSchema(
      @JsonProperty("dataSource") String dataSource,
      @JsonProperty("parser") InputRowParser parser,
      @JsonProperty("metricsSpec") AggregatorFactory[] aggregators,
      @JsonProperty("granularitySpec") GranularitySpec granularitySpec
  )
  {
    this.dataSource = dataSource;

    final Set<String> dimensionExclusions = Sets.newHashSet();
    if (parser != null && parser.getParseSpec() != null && parser.getParseSpec().getTimestampSpec() != null) {
      dimensionExclusions.add(parser.getParseSpec().getTimestampSpec().getTimestampColumn());
      for (AggregatorFactory aggregator : aggregators) {
        dimensionExclusions.add(aggregator.getName());
      }
    }

    if (parser != null && parser.getParseSpec() != null && parser.getParseSpec().getDimensionsSpec() != null) {
      this.parser = parser.withParseSpec(
          parser.getParseSpec()
                .withDimensionsSpec(
                    parser.getParseSpec()
                          .getDimensionsSpec()
                          .withDimensionExclusions(dimensionExclusions)
                )
      );
    } else {
      this.parser = parser;
    }

    this.aggregators = aggregators;
    this.granularitySpec = granularitySpec == null
                           ? new UniformGranularitySpec(null, null, null, null)
                           : granularitySpec;
  }

  @JsonProperty
  public String getDataSource()
  {
    return dataSource;
  }

  @JsonProperty
  public InputRowParser getParser()
  {
    return parser;
  }

  @JsonProperty("metricsSpec")
  public AggregatorFactory[] getAggregators()
  {
    return aggregators;
  }

  @JsonProperty
  public GranularitySpec getGranularitySpec()
  {
    return granularitySpec;
  }

  public DataSchema withGranularitySpec(GranularitySpec granularitySpec)
  {
    return new DataSchema(dataSource, parser, aggregators, granularitySpec);
  }
}

@@ -1847,16 +1847,130 @@
 hSet();%0A
+    for (AggregatorFactory aggregator : aggregators) %7B%0A      dimensionExclusions.add(aggregator.getName());%0A    %7D%0A
     if (
@@ -2176,188 +2176,12 @@
     
-for (AggregatorFactory aggregator : aggregators) %7B%0A        dimensionExclusions.add(aggregator.getName());%0A      %7D%0A    %7D%0A%0A    if (parser != null && parser.getParseSpec() != null && 
+if (
 pars
@@ -2225,24 +2225,26 @@
  != null) %7B%0A
+  
       this.p
@@ -2275,32 +2275,34 @@
 Spec(%0A          
+ 
+ 
 parser.getParseS
@@ -2299,32 +2299,34 @@
 .getParseSpec()%0A
+  
                 
@@ -2358,32 +2358,34 @@
                 
+  
 parser.getParseS
@@ -2416,16 +2416,18 @@
         
+ 
+ 
 .getDime
@@ -2435,24 +2435,26 @@
 sionsSpec()%0A
+  
             
@@ -2513,16 +2513,18 @@
 usions)%0A
+  
         
@@ -2539,18 +2539,73 @@
 )%0A      
-);
+  );%0A      %7D else %7B%0A        this.parser = parser;%0A      %7D
 %0A    %7D e

address cr