package org.verdictdb.core.coordinator;

import org.verdictdb.core.connection.DbmsConnection;
import org.verdictdb.core.execution.ExecutablePlanRunner;
import org.verdictdb.core.querying.QueryExecutionPlan;
import org.verdictdb.core.resulthandler.ExecutionResultReader;
import org.verdictdb.core.sqlobject.SelectQuery;

public class SelectQueryCoordinator {

  DbmsConnection conn;

  public SelectQueryCoordinator(DbmsConnection conn) {
    this.conn = conn;
  }

  public SelectQuery standardizeQuery(String query) {
    // parse the query
    SelectQuery selectQuery = null;
    return selectQuery;
  }

  public ExecutionResultReader process(String query) {

    SelectQuery selectQuery = standardizeQuery(query);

    // make plan
    // if the plan does not include any aggregates, it will simply be a parsed structure of the original query.
    QueryExecutionPlan plan;

    // convert it to an asynchronous plan
    // if the plan does not include any aggregates, this operation should not alter the original plan.
    QueryExecutionPlan asyncPlan;

    // simplify the plan
    QueryExecutionPlan simplifiedAsyncPlan = null;

    // execute the plan
    ExecutionResultReader reader = ExecutablePlanRunner.getResultReader(conn, simplifiedAsyncPlan);

    return reader;
  }

}

@@ -87,16 +87,69 @@
 ection;%0A
+import org.verdictdb.core.connection.StaticMetaData;%0A
 import o
@@ -283,155 +283,621 @@
 ore.
-resulthandler.ExecutionResultReader;%0Aimport org.verdictdb.core.sqlobject.SelectQuery;%0A%0Apublic class SelectQueryCoordinator %7B%0A%0A  DbmsConnection conn
+querying.QueryExecutionPlanSimplifier;%0Aimport org.verdictdb.core.querying.ola.AsyncQueryExecutionPlan;%0Aimport org.verdictdb.core.resulthandler.ExecutionResultReader;%0Aimport org.verdictdb.core.scrambling.ScrambleMetaSet;%0Aimport org.verdictdb.core.sqlobject.AbstractRelation;%0Aimport org.verdictdb.core.sqlobject.SelectQuery;%0Aimport org.verdictdb.exception.VerdictDBException;%0Aimport org.verdictdb.sqlreader.NonValidatingSQLParser;%0Aimport org.verdictdb.sqlreader.RelationStandardizer;%0A%0Apublic class SelectQueryCoordinator %7B%0A%0A  DbmsConnection conn;%0A%0A  ScrambleMetaSet scrambleMetaSet;%0A%0A  StaticMetaData staticMetaData
 ;%0A%0A 
@@ -1028,16 +1028,42 @@
 g query)
+ throws VerdictDBException
  %7B%0A    /
@@ -1088,62 +1088,673 @@
     
-SelectQuery selectQuery = null;%0A    return selectQuery
+NonValidatingSQLParser sqlToRelation = new NonValidatingSQLParser();%0A    SelectQuery relation = (SelectQuery) sqlToRelation.toRelation(query);%0A    RelationStandardizer gen = new RelationStandardizer(staticMetaData);%0A    relation = gen.standardize(relation);%0A    return relation;%0A  %7D%0A%0A  public ScrambleMetaSet getScrambleMetaSet() %7B%0A    return scrambleMetaSet;%0A  %7D%0A%0A  public void setScrambleMetaSet(ScrambleMetaSet scrambleMetaSet) %7B%0A    this.scrambleMetaSet = scrambleMetaSet;%0A  %7D%0A%0A  public StaticMetaData getStaticMetaData() %7B%0A    return staticMetaData;%0A  %7D%0A%0A  public void setStaticMetaData(StaticMetaData staticMetaData) %7B%0A    this.staticMetaData = staticMetaData
 ;%0A  
@@ -1808,16 +1808,42 @@
 g query)
+ throws VerdictDBException
  %7B%0A%0A    
@@ -2050,16 +2050,90 @@
 lan plan
+ = new QueryExecutionPlan(%22verdictdb_temp%22, scrambleMetaSet, selectQuery);
 ;%0A%0A    /
@@ -2304,16 +2304,56 @@
 syncPlan
+ = AsyncQueryExecutionPlan.create(plan);
 ;%0A%0A    /
@@ -2421,12 +2421,56 @@
 n = 
-null
+QueryExecutionPlanSimplifier.simplify(asyncPlan)
 ;%0A%0A 

Complete SelectQueryCoordinator