/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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 org.apache.drill.exec.planner.physical;

import net.hydromatic.optiq.tools.FrameworkContext;

import org.apache.drill.exec.server.options.OptionManager;
import org.apache.drill.exec.server.options.OptionValidator;
import org.apache.drill.exec.server.options.TypeValidators.BooleanValidator;

public class PlannerSettings implements FrameworkContext{
  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(PlannerSettings.class);

  private int numEndPoints = 0;
  private boolean useDefaultCosting = false; // True: use default Optiq costing, False: use Drill costing
  private int broadcastThreshold = 10000; // Consider broadcast inner plans if estimated rows is less than this threshold

  public static final OptionValidator EXCHANGE = new BooleanValidator("planner.disable_exchanges", false);
  public static final OptionValidator HASHAGG = new BooleanValidator("planner.enable_hashagg", true);
  public static final OptionValidator STREAMAGG = new BooleanValidator("planner.enable_streamagg", true);  
  public static final OptionValidator HASHJOIN = new BooleanValidator("planner.enable_hashjoin", true);  
  public static final OptionValidator MERGEJOIN = new BooleanValidator("planner.enable_mergejoin", true);  
  public static final OptionValidator MULTIPHASE = new BooleanValidator("planner.enable_multiphase_agg", false);  
  public static final OptionValidator BROADCAST = new BooleanValidator("planner.enable_broadcast_join", false);

  public OptionManager options = null;

  public PlannerSettings(OptionManager options){
    this.options = options;
  }

  public boolean isSingleMode() {
    return options.getOption(EXCHANGE.getOptionName()).bool_val;
  }

  public int numEndPoints() {
    return numEndPoints;  
  }
  
  public boolean useDefaultCosting() {
    return useDefaultCosting;
  }
    
  public void setNumEndPoints(int numEndPoints) {
    this.numEndPoints = numEndPoints;
  }

  public void setUseDefaultCosting(boolean defcost) {
    this.useDefaultCosting = defcost;
  }
  
  public boolean isHashAggEnabled() {
    return options.getOption(HASHAGG.getOptionName()).bool_val;  
  }
  
  public boolean isStreamAggEnabled() {
    return options.getOption(STREAMAGG.getOptionName()).bool_val;  
  }
  
  public boolean isHashJoinEnabled() {
    return options.getOption(HASHJOIN.getOptionName()).bool_val;
  }
  
  public boolean isMergeJoinEnabled() {
    return options.getOption(MERGEJOIN.getOptionName()).bool_val;  
  }
  
  public boolean isMultiPhaseAggEnabled() {
    return options.getOption(MULTIPHASE.getOptionName()).bool_val;
  }
  
  public boolean isBroadcastJoinEnabled() {
    return options.getOption(BROADCAST.getOptionName()).bool_val;  
  }

  public int getBroadcastThreshold() {
    return broadcastThreshold;  
  }
  
  @Override
  public <T> T unwrap(Class<T> clazz) {
    if(clazz == PlannerSettings.class){
      return (T) this;
    }else{
      return null;
    }
  }


}

@@ -1821,34 +1821,32 @@
 reamagg%22, true);
-  
 %0A  public static
@@ -1925,34 +1925,32 @@
 ashjoin%22, true);
-  
 %0A  public static
@@ -2039,18 +2039,16 @@
 , true);
-  
 %0A  publi
@@ -2149,17 +2149,14 @@
 g%22, 
-fals
+tru
 e);
-  
 %0A  p
@@ -2545,26 +2545,22 @@
 dPoints;
-  
 %0A  %7D%0A
-  
 %0A
+
   public
@@ -2628,13 +2628,9 @@
   %7D%0A
-    
 %0A
+
   pu
@@ -2810,27 +2810,25 @@
 efcost;%0A  %7D%0A
-  
 %0A
+
   public boo
@@ -2912,34 +2912,30 @@
 )).bool_val;
-  
 %0A  %7D%0A
-  
 %0A
+
   public boo
@@ -3023,34 +3023,30 @@
 )).bool_val;
-  
 %0A  %7D%0A
-  
 %0A
+
   public boo
@@ -3133,35 +3133,33 @@
 ).bool_val;%0A  %7D%0A
-  
 %0A
+
   public boolean
@@ -3247,26 +3247,22 @@
 ool_val;
-  
 %0A  %7D%0A
-  
 %0A
+
   public
@@ -3368,19 +3368,17 @@
 al;%0A  %7D%0A
-  
 %0A
+
   public
@@ -3478,18 +3478,16 @@
 ool_val;
-  
 %0A  %7D%0A%0A  
@@ -3557,17 +3557,13 @@
 old;
-  
 %0A  %7D%0A
-  
 %0A  @

enable multi-phase aggregate by default