geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dschnei...@apache.org
Subject [36/53] [partial] incubator-geode git commit: Initial import of geode-1.0.0.0-SNAPSHOT-2. All the new sub-project directories (like jvsd) were not imported. A diff was done to confirm that this commit is exactly the same as the open directory the snapsho
Date Mon, 06 Jul 2015 18:15:43 GMT
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructBag.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructBag.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructBag.java
index 7e4803d..4691845 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructBag.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructBag.java
@@ -18,7 +18,9 @@ import java.util.Iterator;
 import java.util.Set;
 
 import com.gemstone.gemfire.cache.query.Struct;
+import com.gemstone.gemfire.cache.query.internal.types.CollectionTypeImpl;
 import com.gemstone.gemfire.cache.query.internal.types.StructTypeImpl;
+import com.gemstone.gemfire.cache.query.types.CollectionType;
 import com.gemstone.gemfire.cache.query.types.ObjectType;
 import com.gemstone.gemfire.cache.query.types.StructType;
 import com.gemstone.gemfire.internal.cache.CachePerfStats;
@@ -35,7 +37,7 @@ import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
  * @author Eric Zoerner
  * @since 5.1
  */
-public final class StructBag extends ResultsBag {
+public final class StructBag extends ResultsBag implements StructFields {
   /**
    * Holds value of property modifiable.
    */
@@ -163,7 +165,8 @@ public final class StructBag extends ResultsBag {
     }
     StructImpl s = (StructImpl) obj;
     if (!this.elementType.equals(s.getStructType())) {
-      throw new IllegalArgumentException(LocalizedStrings.StructBag_OBJ_DOES_NOT_HAVE_THE_SAME_STRUCTTYPE.toLocalizedString());
+      throw new IllegalArgumentException(LocalizedStrings.StructBag_OBJ_DOES_NOT_HAVE_THE_SAME_STRUCTTYPE.
+          toLocalizedString(this.elementType, s.getStructType()));
     }
     return addFieldValues(s.getFieldValues());
   }
@@ -269,28 +272,32 @@ public final class StructBag extends ResultsBag {
     }
   }
 
+  public CollectionType getCollectionType() {
+    return new CollectionTypeImpl(StructBag.class, this.elementType);
+  }
+  
   // downcast StructBags to call more efficient methods
   @Override
   public boolean addAll(Collection c) {
-    if (c instanceof StructBag) { return addAll((StructBag) c); }
+    if (c instanceof StructFields) { return addAll((StructFields) c); }
     return super.addAll(c);
   }
 
   @Override
   public boolean removeAll(Collection c) {
-    if (c instanceof StructBag) { return removeAll((StructBag) c); }
+    if (c instanceof StructFields) { return removeAll((StructFields) c); }
     return super.removeAll(c);
   }
 
   @Override
   public boolean retainAll(Collection c) {
-    if (c instanceof StructBag) { return retainAll((StructBag) c); }
+    if (c instanceof StructFields) { return retainAll((StructFields) c); }
     return super.retainAll(c);
   }
 
-  public boolean addAll(StructBag sb) {
+  public boolean addAll(StructFields sb) {
     boolean modified = false;
-    if (!this.elementType.equals(sb.elementType)) {
+    if (!this.elementType.equals(sb.getCollectionType().getElementType())) {
       throw new IllegalArgumentException(LocalizedStrings.StructBag_TYPES_DONT_MATCH.toLocalizedString());
     }
     
@@ -306,9 +313,9 @@ public final class StructBag extends ResultsBag {
     return modified;
   }
 
-  public boolean removeAll(StructBag ss) {
+  public boolean removeAll(StructFields ss) {
     boolean modified = false;
-    if (!this.elementType.equals(ss.elementType)) {
+    if (!this.elementType.equals(ss.getCollectionType().getElementType())) {
       return false; // nothing // modified
     }
     for (Iterator itr = ss.fieldValuesIterator(); itr.hasNext();) {
@@ -320,8 +327,8 @@ public final class StructBag extends ResultsBag {
     return modified;
   }
 
-  public boolean retainAll(StructBag ss) {
-    if (!this.elementType.equals(ss.elementType)) {
+  public boolean retainAll(StructFields ss) {
+    if (!this.elementType.equals(ss.getCollectionType().getElementType())) {
       if (isEmpty()) {
         return false; // nothing modified
       }
@@ -428,7 +435,7 @@ public final class StructBag extends ResultsBag {
   /**
    * Iterator wrapper to construct Structs on demand.
    */
-  private class StructBagIterator extends ResultsBagIterator {
+  private class StructBagIterator extends BagIterator {
 
     private final Iterator itr;
 

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructFields.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructFields.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructFields.java
new file mode 100644
index 0000000..aec755a
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructFields.java
@@ -0,0 +1,26 @@
+package com.gemstone.gemfire.cache.query.internal;
+
+import java.util.Iterator;
+
+import com.gemstone.gemfire.cache.query.types.CollectionType;
+
+/**
+ * This interface is to be implemented by all SelectResults implementation which
+ * can hold struct using field values array ( Object[]) 
+ * 
+ * @see SortedStructSet
+ * @see StructSet
+ * @see StructBag
+ * @see SortedStructBag
+ * 
+ * @author ashahid
+ *
+ */
+public interface StructFields {
+
+  public boolean addFieldValues(Object[] fieldValues);
+  public boolean removeFieldValues(Object[] fieldValues);
+  public Iterator fieldValuesIterator();
+  public CollectionType getCollectionType();
+  public boolean containsFieldValues(Object[] fieldValues);
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructImpl.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructImpl.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructImpl.java
index 06f1d02..9fd4bd8 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructImpl.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructImpl.java
@@ -26,9 +26,11 @@ import com.gemstone.gemfire.pdx.internal.PdxString;
  * @since 4.0
  */
 public class StructImpl
-  implements Struct, DataSerializableFixedID {
+  implements Struct, DataSerializableFixedID, Serializable {
+  private static final long serialVersionUID = -8474955084549542156L;
   private StructTypeImpl type;
   private Object[] values;
+  //hasPdx will not be initialized by Java deserialization
   private transient boolean hasPdx = false;
   /** no-arg constructor required for DataSerializable */
   public StructImpl() {};

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructSet.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructSet.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructSet.java
index 4f23745..3587a89 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructSet.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/StructSet.java
@@ -35,7 +35,8 @@ import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
  * @author Eric Zoerner
  * @since 4.0
  */
-public final class StructSet /*extends ObjectOpenCustomHashSet*/ implements Set, SelectResults, DataSerializableFixedID {
+public final class StructSet /*extends ObjectOpenCustomHashSet*/ implements Set, SelectResults, 
+DataSerializableFixedID, StructFields {
   private static final long serialVersionUID = -1228835506930611510L;
 
   protected StructType structType;
@@ -283,7 +284,7 @@ public final class StructSet /*extends ObjectOpenCustomHashSet*/ implements Set,
   }
 
   public CollectionType getCollectionType() {
-    return new CollectionTypeImpl(java.util.Set.class, this.structType);
+    return new CollectionTypeImpl(StructSet.class, this.structType);
   }
 
   // note: this method is dangerous in that it could result in undefined
@@ -300,7 +301,7 @@ public final class StructSet /*extends ObjectOpenCustomHashSet*/ implements Set,
   }
 
   public Set asSet() {
-    return this.contents;
+    return this;
   }
 
   /**
@@ -409,21 +410,35 @@ public final class StructSet /*extends ObjectOpenCustomHashSet*/ implements Set,
 
   @Override
   public Object[] toArray() {
-    return this.contents.toArray();
+    Struct[] structs = new Struct[this.contents.size()];
+    int i = 0;
+    for (Iterator iter = this.iterator(); iter.hasNext();) {      
+      structs[i++]  = (Struct)iter.next();
+    }
+    return structs;    
   }
 
   @Override
-  public Object[] toArray(Object[] a) {
-    return this.contents.toArray(a);
+  public Object[] toArray(Object[] a) {    
+    Object[] array = this.contents.toArray(a);
+    int i = 0;
+    for(Object o : array) {
+      array[i++] = new StructImpl((StructTypeImpl)this.structType, (Object[])o);
+    }
+    return array;
   }
 
   @Override
   public boolean remove(Object o) {
+    if(o instanceof Struct) {
+      o = ((Struct)o).getFieldValues();
+    }
     return this.contents.remove(o);
   }
 
   @Override
   public boolean containsAll(Collection c) {
+    //TODO: Asif : This is wrong ,we need to fix this.
     return this.contents.containsAll(c);
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/Undefined.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/Undefined.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/Undefined.java
index 61205aa..a89e433 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/Undefined.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/Undefined.java
@@ -26,7 +26,9 @@ import com.gemstone.gemfire.internal.Version;
  * 
  */
 
-public final class Undefined implements DataSerializableFixedID, Comparable {
+public final class Undefined implements DataSerializableFixedID, Comparable, Serializable {
+
+  private static final long serialVersionUID = 6643107525908324141L;
 
   public Undefined() {
     Support.assertState(QueryService.UNDEFINED == null,

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AbstractAggregator.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AbstractAggregator.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AbstractAggregator.java
new file mode 100644
index 0000000..5809df8
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AbstractAggregator.java
@@ -0,0 +1,31 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+import com.gemstone.gemfire.cache.query.Aggregator;
+
+/**
+ * Abstract Aggregator class providing support for downcasting the result
+ * 
+ * @author ashahid
+ *
+ */
+public abstract class AbstractAggregator implements Aggregator {
+
+  public static Number downCast(double value) {
+    Number retVal;
+    if (value % 1 == 0) {
+      long longValue = (long) value;
+      if (longValue <= Integer.MAX_VALUE && longValue >= Integer.MIN_VALUE) {
+        retVal = Integer.valueOf((int) longValue);
+      } else {
+        retVal = Long.valueOf(longValue);
+      }
+    } else {
+      if (value <= Float.MAX_VALUE && value >= Float.MIN_VALUE) {
+        retVal = Float.valueOf((float) value);
+      } else {
+        retVal = Double.valueOf(value);
+      }
+    }
+    return retVal;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/Avg.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/Avg.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/Avg.java
new file mode 100644
index 0000000..e1da36d
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/Avg.java
@@ -0,0 +1,34 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+import com.gemstone.gemfire.cache.query.QueryService;
+
+/**
+ * Computes the non distinct average for replicated region based queries
+ * 
+ * @author ashahid
+ *
+ */
+public class Avg extends Sum {
+  private int num = 0;
+
+  @Override
+  public void accumulate(Object value) {
+    if (value != null && value != QueryService.UNDEFINED) {
+      super.accumulate(value);
+      ++num;
+    }
+  }
+
+  @Override
+  public void init() {
+
+  }
+
+  @Override
+  public Object terminate() {
+    double sum = ((Number) super.terminate()).doubleValue();
+    double result = sum / num;
+    return downCast(result);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgBucketNode.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgBucketNode.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgBucketNode.java
new file mode 100644
index 0000000..9611321
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgBucketNode.java
@@ -0,0 +1,33 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+import com.gemstone.gemfire.cache.query.QueryService;
+
+/**
+ * The aggregator for compuing average which is used on the bucket node for
+ * partitioned region based queries.
+ * 
+ * @author ashahid
+ *
+ */
+public class AvgBucketNode extends Sum {
+
+  private int count = 0;
+
+  @Override
+  public void accumulate(Object value) {
+    if (value != null && value != QueryService.UNDEFINED) {
+      super.accumulate(value);
+      ++count;
+    }
+  }
+
+  /**
+   * Returns a two element array of the total number of values & the computed
+   * sum of the values.
+   */
+  @Override
+  public Object terminate() {
+    return new Object[] { Integer.valueOf(count), super.terminate() };
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgDistinct.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgDistinct.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgDistinct.java
new file mode 100644
index 0000000..3863b72
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgDistinct.java
@@ -0,0 +1,27 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+import com.gemstone.gemfire.cache.query.QueryService;
+
+/**
+ * Computes the average of distinct values for replicated region based queries.
+ * 
+ * @author ashahid
+ *
+ */
+public class AvgDistinct extends SumDistinct {
+
+  @Override
+  public void accumulate(Object value) {
+    if (value != null && value != QueryService.UNDEFINED) {
+      super.accumulate(value);
+    }
+  }
+
+  @Override
+  public Object terminate() {
+    double sum = ((Number) super.terminate()).doubleValue();
+    double result = sum / this.distinct.size();
+    return downCast(result);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgDistinctPRQueryNode.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgDistinctPRQueryNode.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgDistinctPRQueryNode.java
new file mode 100644
index 0000000..2bbc67d
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgDistinctPRQueryNode.java
@@ -0,0 +1,19 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+/**
+ * Computes the final average of distinct values for the partitioned region
+ * based queries. This aggregator is initialized on the PR query node & acts on
+ * the results obtained from bucket nodes.
+ * 
+ * @author ashahid
+ *
+ */
+public class AvgDistinctPRQueryNode extends SumDistinctPRQueryNode {
+
+  @Override
+  public Object terminate() {
+    double sum = ((Number) super.terminate()).doubleValue();
+    double result = sum / this.distinct.size();
+    return downCast(result);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgPRQueryNode.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgPRQueryNode.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgPRQueryNode.java
new file mode 100644
index 0000000..2365474
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/AvgPRQueryNode.java
@@ -0,0 +1,31 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+/**
+ * Computes the final non distinct average for a partitioned region based query.
+ * This aggregator is instantiated on the PR query node.
+ * 
+ * @author ashahid
+ *
+ */
+public class AvgPRQueryNode extends Sum {
+  private int count = 0;
+
+  /**
+   * Takes the input of data received from bucket nodes. The data is of the form
+   * of two element array. The first element is the number of values, while the
+   * second element is the sum of the values.
+   */
+  @Override
+  public void accumulate(Object value) {
+    Object[] array = (Object[]) value;
+    this.count += ((Integer) array[0]).intValue();
+    super.accumulate(array[1]);
+  }
+
+  @Override
+  public Object terminate() {
+    double sum = ((Number) super.terminate()).doubleValue();
+    double result = sum / count;
+    return downCast(result);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/Count.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/Count.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/Count.java
new file mode 100644
index 0000000..3026b63
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/Count.java
@@ -0,0 +1,33 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+import com.gemstone.gemfire.cache.query.Aggregator;
+import com.gemstone.gemfire.cache.query.QueryService;
+
+/**
+ * Computes the count of the non distinct rows for replicated & PR based
+ * queries.
+ * 
+ * @author ashahid
+ *
+ */
+public class Count implements Aggregator {
+  private int count = 0;
+
+  @Override
+  public void accumulate(Object value) {
+    if (value != null && value != QueryService.UNDEFINED) {
+      ++this.count;
+    }
+  }
+
+  @Override
+  public void init() {
+
+  }
+
+  @Override
+  public Object terminate() {
+    return Integer.valueOf(count);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/CountDistinct.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/CountDistinct.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/CountDistinct.java
new file mode 100644
index 0000000..03af70b
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/CountDistinct.java
@@ -0,0 +1,17 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+/**
+ * 
+ * Computes the count of the distinct rows for replicated region based queries.
+ * 
+ * @author ashahid
+ */
+
+public class CountDistinct extends DistinctAggregator {
+
+  @Override
+  public Object terminate() {
+    return Integer.valueOf(this.distinct.size());
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/CountDistinctPRQueryNode.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/CountDistinctPRQueryNode.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/CountDistinctPRQueryNode.java
new file mode 100644
index 0000000..4e56f30
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/CountDistinctPRQueryNode.java
@@ -0,0 +1,28 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+import java.util.Set;
+
+/**
+ * Computes the count of the distinct rows on the PR query node.
+ * 
+ * @author ashahid
+ *
+ */
+public class CountDistinctPRQueryNode extends DistinctAggregator {
+
+  /**
+   * The input data is the Set containing distinct values from each of the
+   * bucket nodes.
+   */
+  @Override
+  public void accumulate(Object value) {
+    this.distinct.addAll((Set) value);
+
+  }
+
+  @Override
+  public Object terminate() {
+    return Integer.valueOf(this.distinct.size());
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/CountPRQueryNode.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/CountPRQueryNode.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/CountPRQueryNode.java
new file mode 100644
index 0000000..34d34ac
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/CountPRQueryNode.java
@@ -0,0 +1,32 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+import com.gemstone.gemfire.cache.query.Aggregator;
+
+/**
+ * Computes the count of the rows on the PR query node
+ * 
+ * @author ashahid
+ *
+ */
+public class CountPRQueryNode implements Aggregator {
+  private int count = 0;
+
+  /**
+   * Recieves the input of the individual counts from the bucket nodes.
+   */
+  @Override
+  public void accumulate(Object value) {
+    this.count += ((Integer) value).intValue();
+  }
+
+  @Override
+  public void init() {
+
+  }
+
+  @Override
+  public Object terminate() {
+    return Integer.valueOf(count);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/DistinctAggregator.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/DistinctAggregator.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/DistinctAggregator.java
new file mode 100644
index 0000000..0328d9a
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/DistinctAggregator.java
@@ -0,0 +1,40 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import com.gemstone.gemfire.cache.query.QueryService;
+
+/**
+ * The class used to hold the distinct values. This will get instantiated on the
+ * bucket node as part of distinct queries for sum, count, average.
+ * 
+ * @author ashahid
+ *
+ */
+public class DistinctAggregator extends AbstractAggregator {
+  protected final Set<Object> distinct;
+
+  public DistinctAggregator() {
+    this.distinct = new HashSet<Object>();
+  }
+
+  @Override
+  public void accumulate(Object value) {
+    if (value != null && value != QueryService.UNDEFINED) {
+      this.distinct.add(value);
+    }
+  }
+
+  @Override
+  public void init() {
+    // TODO Auto-generated method stub
+
+  }
+
+  @Override
+  public Object terminate() {
+    return this.distinct;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/MaxMin.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/MaxMin.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/MaxMin.java
new file mode 100644
index 0000000..443e62d
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/MaxMin.java
@@ -0,0 +1,52 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+import com.gemstone.gemfire.cache.query.Aggregator;
+import com.gemstone.gemfire.cache.query.QueryService;
+
+/**
+ * Computes the Max or Min
+ * 
+ * @author ashahid
+ *
+ */
+
+public class MaxMin implements Aggregator {
+  private final boolean findMax;
+  private Comparable currentOptima;
+
+  public MaxMin(boolean findMax) {
+    this.findMax = findMax;
+  }
+
+  @Override
+  public void accumulate(Object value) {
+    if (value == null || value == QueryService.UNDEFINED) {
+      return;
+    }
+    Comparable comparable = (Comparable) value;
+    
+    if (currentOptima == null) {
+      currentOptima = comparable;
+    } else {
+      int compare = currentOptima.compareTo(comparable);
+      if (findMax) {
+        currentOptima = compare < 0 ? comparable : currentOptima;
+      } else {
+        currentOptima = compare > 0 ? comparable : currentOptima;
+      }
+    }
+
+  }
+
+  @Override
+  public void init() {
+    // TODO Auto-generated method stub
+
+  }
+
+  @Override
+  public Object terminate() {
+    return currentOptima;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/Sum.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/Sum.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/Sum.java
new file mode 100644
index 0000000..c41d0a5
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/Sum.java
@@ -0,0 +1,32 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+import com.gemstone.gemfire.cache.query.QueryService;
+
+/**
+ * Computes the sum for replicated & PR based queries.
+ * 
+ * @author ashahid
+ *
+ */
+public class Sum extends AbstractAggregator {
+
+  private double result = 0;
+
+  @Override
+  public void accumulate(Object value) {
+    if (value != null && value != QueryService.UNDEFINED) {
+      Number number = (Number) value;
+      result += number.doubleValue();
+    }
+  }
+
+  @Override
+  public void init() {
+
+  }
+
+  @Override
+  public Object terminate() {
+    return downCast(result);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/SumDistinct.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/SumDistinct.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/SumDistinct.java
new file mode 100644
index 0000000..e4741dc
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/SumDistinct.java
@@ -0,0 +1,19 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+/**
+ * Computes the sum of distinct values for replicated region based queries.
+ * @author ashahid
+ *
+ */
+public class SumDistinct extends DistinctAggregator {
+
+  @Override
+  public Object terminate() {
+    double sum = 0;
+    for (Object o : this.distinct) {
+      sum += ((Number) o).doubleValue();
+    }
+    return downCast(sum);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/SumDistinctPRQueryNode.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/SumDistinctPRQueryNode.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/SumDistinctPRQueryNode.java
new file mode 100644
index 0000000..99c6fe7
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/aggregate/SumDistinctPRQueryNode.java
@@ -0,0 +1,30 @@
+package com.gemstone.gemfire.cache.query.internal.aggregate;
+
+import java.util.Set;
+
+/**
+ * Computes the sum of distinct values on the PR query node.
+ * 
+ * @author ashahid
+ *
+ */
+public class SumDistinctPRQueryNode extends DistinctAggregator {
+
+  /**
+   * The input data is the Set of values(distinct) receieved from each of the
+   * bucket nodes.
+   */
+  @Override
+  public void accumulate(Object value) {
+    this.distinct.addAll((Set) value);
+  }
+
+  @Override
+  public Object terminate() {
+    double sum = 0;
+    for (Object o : this.distinct) {
+      sum += ((Number) o).doubleValue();
+    }
+    return downCast(sum);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/AbstractIndex.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/AbstractIndex.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/AbstractIndex.java
index b89b8ca..2ab3dd8 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/AbstractIndex.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/AbstractIndex.java
@@ -34,6 +34,7 @@ import com.gemstone.gemfire.cache.query.QueryService;
 import com.gemstone.gemfire.cache.query.SelectResults;
 import com.gemstone.gemfire.cache.query.Struct;
 import com.gemstone.gemfire.cache.query.TypeMismatchException;
+import com.gemstone.gemfire.cache.query.internal.Bag;
 import com.gemstone.gemfire.cache.query.internal.CompiledID;
 import com.gemstone.gemfire.cache.query.internal.CompiledIndexOperation;
 import com.gemstone.gemfire.cache.query.internal.CompiledIteratorDef;
@@ -43,13 +44,11 @@ import com.gemstone.gemfire.cache.query.internal.CqEntry;
 import com.gemstone.gemfire.cache.query.internal.DefaultQuery;
 import com.gemstone.gemfire.cache.query.internal.ExecutionContext;
 import com.gemstone.gemfire.cache.query.internal.IndexInfo;
-import com.gemstone.gemfire.cache.query.internal.LinkedStructSet;
 import com.gemstone.gemfire.cache.query.internal.QRegion;
 import com.gemstone.gemfire.cache.query.internal.QueryMonitor;
 import com.gemstone.gemfire.cache.query.internal.QueryUtils;
-import com.gemstone.gemfire.cache.query.internal.ResultsBag;
 import com.gemstone.gemfire.cache.query.internal.RuntimeIterator;
-import com.gemstone.gemfire.cache.query.internal.StructBag;
+import com.gemstone.gemfire.cache.query.internal.StructFields;
 import com.gemstone.gemfire.cache.query.internal.StructImpl;
 import com.gemstone.gemfire.cache.query.internal.Support;
 import com.gemstone.gemfire.cache.query.internal.index.IndexStore.IndexStoreEntry;
@@ -67,6 +66,9 @@ import com.gemstone.gemfire.internal.cache.RegionEntry;
 import com.gemstone.gemfire.internal.cache.persistence.query.CloseableIterator;
 import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 import com.gemstone.gemfire.internal.logging.LogService;
+import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl.Chunk;
+import com.gemstone.gemfire.internal.offheap.annotations.Released;
+import com.gemstone.gemfire.internal.offheap.annotations.Retained;
 import com.gemstone.gemfire.pdx.PdxInstance;
 import com.gemstone.gemfire.pdx.internal.PdxString;
 
@@ -275,7 +277,7 @@ public abstract class AbstractIndex implements IndexProtocol
   }
 
   public void query(Object key, int operator, Collection results,
-      CompiledValue iterOp, RuntimeIterator indpndntIr,
+      @Retained CompiledValue iterOp, RuntimeIterator indpndntIr,
       ExecutionContext context, List projAttrib,
       SelectResults intermediateResults, boolean isIntersection)
   throws TypeMismatchException, FunctionDomainException,
@@ -544,7 +546,7 @@ public abstract class AbstractIndex implements IndexProtocol
     return value;
   }
 
-  private void addToResultsBagWithUnionOrIntersection(Collection results,
+  private void addToResultsWithUnionOrIntersection(Collection results,
       SelectResults intermediateResults, boolean isIntersection, Object value)
   {
     value = verifyAndGetPdxDomainObject(value);
@@ -567,7 +569,7 @@ public abstract class AbstractIndex implements IndexProtocol
     }
   }
 
-  private void addToStructBagWithUnionOrIntersection(Collection results,
+  private void addToStructsWithUnionOrIntersection(Collection results,
       SelectResults intermediateResults, boolean isIntersection, Object[] values)
   {
     for (int i=0; i < values.length; i++) {
@@ -575,38 +577,45 @@ public abstract class AbstractIndex implements IndexProtocol
     }
     
     if (intermediateResults == null) {
-      if( results instanceof StructBag) {
-        ((StructBag)results).addFieldValues(values);
+      if( results instanceof StructFields) {
+        ((StructFields)results).addFieldValues(values);
       }else {
-        LinkedStructSet lss = (LinkedStructSet)results;
-        StructImpl structImpl = new StructImpl( (StructTypeImpl)lss.getCollectionType().getElementType(), values);
-        lss.add(structImpl);
+        //The results could be LinkedStructSet or SortedResultsBag or StructSet
+        //LinkedStructSet lss = (LinkedStructSet)results;
+        SelectResults sr = (SelectResults)results;
+        StructImpl structImpl = new StructImpl( (StructTypeImpl)sr.getCollectionType().getElementType(), values);
+        //lss.add(structImpl);
+        sr.add(structImpl);
       }
     }
     else {
       if (isIntersection) {
-        if(results instanceof StructBag) {
-          int numOcc = ((StructBag)intermediateResults).occurrences(values);
+        if(results instanceof StructFields) {
+          int numOcc = intermediateResults.occurrences(values);
           if (numOcc > 0) {
-            ((StructBag)results).addFieldValues(values);
-            ((StructBag)intermediateResults).removeFieldValues(values);
+            ((StructFields)results).addFieldValues(values);
+            ((StructFields)intermediateResults).removeFieldValues(values);
           }
         }else {
-          LinkedStructSet lss = (LinkedStructSet)results;
-          StructImpl structImpl = new StructImpl( (StructTypeImpl)lss.getCollectionType().getElementType(), values);
-          if( ((LinkedStructSet)intermediateResults).remove(structImpl)) {
-            lss.add(structImpl);
+          //LinkedStructSet lss = (LinkedStructSet)results;
+          // could be LinkedStructSet or SortedResultsBag
+          SelectResults sr = (SelectResults)results;
+          StructImpl structImpl = new StructImpl( (StructTypeImpl)sr.getCollectionType().getElementType(), values);
+          if( intermediateResults.remove(structImpl)) {
+            sr.add(structImpl);
           }          
         }
       }
       else {
-        if( results instanceof StructBag) {
-          ((StructBag)results).addFieldValues(values);
+        if( results instanceof StructFields) {
+          ((StructFields)results).addFieldValues(values);
         }else {
-          LinkedStructSet lss = (LinkedStructSet)results;
-          StructImpl structImpl = new StructImpl( (StructTypeImpl)lss.getCollectionType().getElementType(), values);
-          if( ((LinkedStructSet)intermediateResults).remove(structImpl)) {
-            lss.add(structImpl);
+          // could be LinkedStructSet or SortedResultsBag
+          SelectResults sr = (SelectResults)results;
+          //LinkedStructSet lss = (LinkedStructSet)results;
+          StructImpl structImpl = new StructImpl( (StructTypeImpl)sr.getCollectionType().getElementType(), values);
+          if( ((SelectResults)intermediateResults).remove(structImpl)) {
+            sr.add(structImpl);
           }
         }
       }
@@ -621,11 +630,17 @@ public abstract class AbstractIndex implements IndexProtocol
   {
     if (projAttrib == null) {
       iterValue = deserializePdxForLocalDistinctQuery(context, iterValue);
-      this.addToResultsBagWithUnionOrIntersection(result, intermediateResults,
+      this.addToResultsWithUnionOrIntersection(result, intermediateResults,
           isIntersection, iterValue);
     }
     else {
-      if (result instanceof StructBag || result instanceof LinkedStructSet) {
+      //TODO : Asif : Optimize this . This condition looks ugly.
+     /* if (result instanceof StructBag || result instanceof LinkedStructSet
+          || result instanceof LinkedStructBag) {*/
+      boolean isStruct = result instanceof SelectResults 
+          && ((SelectResults)result).getCollectionType().getElementType() != null
+          && ((SelectResults)result).getCollectionType().getElementType().isStructType();
+      if (isStruct) {
         int projCount = projAttrib.size();
         Object[] values = new Object[projCount];
         Iterator projIter = projAttrib.iterator();
@@ -635,13 +650,13 @@ public abstract class AbstractIndex implements IndexProtocol
           values[i] = deserializePdxForLocalDistinctQuery(context, ((CompiledValue)projDef[1]).evaluate(context));
           i++;
         }
-        this.addToStructBagWithUnionOrIntersection(result, intermediateResults,
+        this.addToStructsWithUnionOrIntersection(result, intermediateResults,
             isIntersection, values);
       }
       else {
         Object[] temp = (Object[])projAttrib.get(0);
         Object val = deserializePdxForLocalDistinctQuery(context, ((CompiledValue)temp[1]).evaluate(context));
-        this.addToResultsBagWithUnionOrIntersection(result,
+        this.addToResultsWithUnionOrIntersection(result,
             intermediateResults, isIntersection, val);
       }
     }
@@ -673,7 +688,7 @@ public abstract class AbstractIndex implements IndexProtocol
     return val;
   }
   
-  private void removeFromResultsBagWithUnionOrIntersection(Collection results,
+  private void removeFromResultsWithUnionOrIntersection(Collection results,
       SelectResults intermediateResults, boolean isIntersection, Object value)
   {
     if (intermediateResults == null) {
@@ -681,7 +696,7 @@ public abstract class AbstractIndex implements IndexProtocol
     }
     else {
       if (isIntersection) {
-        int numOcc = ((ResultsBag)results).occurrences(value);
+        int numOcc = ((SelectResults)results).occurrences(value);
         if (numOcc > 0) {
           results.remove(value);
           intermediateResults.add(value);
@@ -693,23 +708,24 @@ public abstract class AbstractIndex implements IndexProtocol
     }
   }
 
-  private void removeFromStructBagWithUnionOrIntersection(Collection results,
+  private void removeFromStructsWithUnionOrIntersection(Collection results,
       SelectResults intermediateResults, boolean isIntersection,
-      Object values[])
+      Object values[], ExecutionContext context)
   {
-    if (intermediateResults == null) {
-      ((StructBag)results).removeFieldValues(values);
+    if (intermediateResults == null) {      
+        ((StructFields)results).removeFieldValues(values);      
     }
     else {
       if (isIntersection) {
-        int numOcc = ((StructBag)results).occurrences(values);
+        int numOcc = ((SelectResults)results).occurrences(values);
         if (numOcc > 0) {
-          ((StructBag)results).removeFieldValues(values);
-          ((StructBag)intermediateResults).addFieldValues(values);
+            ((StructFields)results).removeFieldValues(values);
+            ((StructFields)intermediateResults).addFieldValues(values);
+          
         }
       }
-      else {
-        ((StructBag)results).removeFieldValues(values);
+      else {        
+        ((StructFields)results).removeFieldValues(values);        
       }
     }
   }
@@ -721,11 +737,11 @@ public abstract class AbstractIndex implements IndexProtocol
       QueryInvocationTargetException
   {
     if (projAttrib == null) {
-      this.removeFromResultsBagWithUnionOrIntersection(result,
+      this.removeFromResultsWithUnionOrIntersection(result,
           intermediateResults, isIntersection, iterValue);
     }
     else {
-      if (result instanceof StructBag) {
+      if (result instanceof StructFields) {
         int projCount = projAttrib.size();
         Object[] values = new Object[projCount];
         Iterator projIter = projAttrib.iterator();
@@ -734,13 +750,13 @@ public abstract class AbstractIndex implements IndexProtocol
           Object projDef[] = (Object[])projIter.next();
           values[i++] = ((CompiledValue)projDef[1]).evaluate(context);
         }
-        this.removeFromStructBagWithUnionOrIntersection(result,
-            intermediateResults, isIntersection, values);
+        this.removeFromStructsWithUnionOrIntersection(result,
+            intermediateResults, isIntersection, values, context);
       }
       else {
         Object[] temp = (Object[])projAttrib.get(0);
         Object val = ((CompiledValue)temp[1]).evaluate(context);
-        this.removeFromResultsBagWithUnionOrIntersection(result,
+        this.removeFromResultsWithUnionOrIntersection(result,
             intermediateResults, isIntersection, val);
       }
     }
@@ -1400,17 +1416,14 @@ public abstract class AbstractIndex implements IndexProtocol
    * @return true if limit is satisfied.
    */
   protected boolean verifyLimit(Collection result, int limit,
-      ExecutionContext context) {
+      ExecutionContext context) {   
     if (limit > 0) {
-      if (((DefaultQuery)context.getQuery()).getSimpleSelect().isDistinct()) {
-        if (result instanceof ResultsBag) {
-          return (((ResultsBag)result).distinctElementsSize() == limit);
-        } else if (result instanceof StructBag) {
-          return (((StructBag)result).distinctElementsSize() == limit);
-        }
+     /* if (!context.isDistinct()) {
+        return ((Bag)result).size() == limit;
       } else if (result.size() == limit) {
         return true;
-      }
+      }*/
+      return result.size() == limit;
     }
     return false;
   }
@@ -1458,13 +1471,23 @@ public abstract class AbstractIndex implements IndexProtocol
         valuesInRegion = evaluateIndexIteratorsFromRE(re, context);
         valueInIndex = verifyAndGetPdxDomainObject(value);
       } else{
-        Object val = re.getValueInVM(context.getPartitionedRegion()); // OFFHEAP incrc, deserialize, decrc
+        @Released Object val = re.getValueInVM(context.getPartitionedRegion());
+        Chunk valToFree = null;
+        if (val instanceof Chunk) {
+          valToFree = (Chunk)val;
+        }
+        try {
         if (val instanceof CachedDeserializable) {
           val = ((CachedDeserializable)val).getDeserializedValue(getRegion(), re);
         }
         val = verifyAndGetPdxDomainObject(val);   
         valueInIndex = verifyAndGetPdxDomainObject(value);
         valuesInRegion = evaluateIndexIteratorsFromRE(val, context);
+        } finally {
+          if (valToFree != null) {
+            valToFree.release();
+          }
+        }
       }
     } catch (Exception e) {
       // TODO: Create a new LocalizedString for this.
@@ -2110,14 +2133,10 @@ public abstract class AbstractIndex implements IndexProtocol
     }
 
     private boolean verifylimit(Collection result, int limit,
-        ExecutionContext context) {
+        ExecutionContext context) {     
       if (limit > 0) {
-        if (((DefaultQuery)context.getQuery()).getSimpleSelect().isDistinct()) {
-          if (result instanceof ResultsBag) {
-            return (((ResultsBag)result).distinctElementsSize() == limit);
-          } else if (result instanceof StructBag) {
-            return (((StructBag)result).distinctElementsSize() == limit);
-          }
+        if (!context.isDistinct()) {
+          return ((Bag)result).size() == limit;
         } else if (result.size() == limit) {
           return true;
         }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/CompactRangeIndex.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/CompactRangeIndex.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/CompactRangeIndex.java
index cc13791..cceeb8a 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/CompactRangeIndex.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/CompactRangeIndex.java
@@ -741,8 +741,13 @@ public class CompactRangeIndex extends AbstractIndex {
       NameResolutionException, QueryInvocationTargetException {
 
     QueryObserver observer = QueryObserverHolder.getInstance();
-   
-      if (entriesIter == null || verifyLimit(result, limit, context)) {
+      boolean limitApplied = false;
+      if (entriesIter == null || (limitApplied =verifyLimit(result, limit, context))) {
+        if(limitApplied) {          
+          if(observer != null) {
+            observer.limitAppliedAtIndexLevel(this, limit, result);
+          }
+        }
         return;
       }
     

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/DummyQRegion.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/DummyQRegion.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/DummyQRegion.java
index 2fbd936..5f9285e 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/DummyQRegion.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/DummyQRegion.java
@@ -13,24 +13,28 @@
 
 package com.gemstone.gemfire.cache.query.internal.index;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
 import com.gemstone.gemfire.cache.Region;
 import com.gemstone.gemfire.cache.query.SelectResults;
 import com.gemstone.gemfire.cache.query.internal.QRegion;
-import com.gemstone.gemfire.cache.query.internal.ResultsSet;
 import com.gemstone.gemfire.cache.query.internal.ResultsBag;
-import com.gemstone.gemfire.cache.query.types.*;
-import com.gemstone.gemfire.cache.query.internal.types.*;
-import com.gemstone.gemfire.internal.cache.LocalRegion;
-import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
+import com.gemstone.gemfire.cache.query.internal.ResultsSet;
+import com.gemstone.gemfire.cache.query.internal.types.TypeUtils;
+import com.gemstone.gemfire.cache.query.types.ObjectType;
 import com.gemstone.gemfire.internal.cache.CachedDeserializable;
+import com.gemstone.gemfire.internal.cache.GemFireCacheImpl;
+import com.gemstone.gemfire.internal.cache.LocalRegion;
 import com.gemstone.gemfire.internal.cache.RegionEntry;
 import com.gemstone.gemfire.internal.cache.RegionEntryContext;
 import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
+import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl.Chunk;
+import com.gemstone.gemfire.internal.offheap.annotations.Released;
+import com.gemstone.gemfire.internal.offheap.annotations.Retained;
 
 /**
  *
@@ -120,8 +124,16 @@ public class DummyQRegion extends QRegion {
       valueInList = new  ArrayList(1);      
     }
     valueInList.clear();
-    Object val = this.entry.getValueInVMOrDiskWithoutFaultIn((LocalRegion) getRegion());
-    if (val instanceof CachedDeserializable) {
+    Object val = this.entry.getValueOffHeapOrDiskWithoutFaultIn((LocalRegion) getRegion());
+    if (val instanceof Chunk) {
+      @Retained @Released Chunk ohval = (Chunk) val;
+      try {
+        // TODO OFFHEAP: val may be off-heap PdxInstance
+        val = ohval.getDeserializedValue(getRegion(), this.entry);
+      } finally {
+        ohval.release();
+      }
+    } else if (val instanceof CachedDeserializable) {
       val = ((CachedDeserializable)val).getDeserializedValue(getRegion(), this.entry);
     } 
     valueInList.add(val);
@@ -133,8 +145,16 @@ public class DummyQRegion extends QRegion {
     if(valueInArray == null){
       valueInArray = new  Object[1];      
     }   
-    Object val = this.entry.getValueInVMOrDiskWithoutFaultIn((LocalRegion) getRegion());
-    if (val instanceof CachedDeserializable) {
+    Object val = this.entry.getValueOffHeapOrDiskWithoutFaultIn((LocalRegion) getRegion());
+    if (val instanceof Chunk) {      
+      @Retained @Released Chunk ohval = (Chunk) val;
+      try {
+        // TODO OFFHEAP: val may be off-heap PdxInstance
+        val = ohval.getDeserializedValue(getRegion(), this.entry);
+      } finally {
+        ohval.release();
+      }
+    } else if (val instanceof CachedDeserializable) {
       val = ((CachedDeserializable)val).getDeserializedValue(getRegion(), this.entry);
     } 
     valueInArray[0] = val;
@@ -148,8 +168,16 @@ public class DummyQRegion extends QRegion {
       values.setElementType(valueType);
     }
     values.clear();
-    Object val = this.entry.getValueInVMOrDiskWithoutFaultIn((LocalRegion) getRegion());
-    if (val instanceof CachedDeserializable) {
+    Object val = this.entry.getValueOffHeapOrDiskWithoutFaultIn((LocalRegion) getRegion());
+    if (val instanceof Chunk) {
+      @Retained @Released Chunk ohval = (Chunk) val;
+      try {
+        // TODO OFFHEAP: val may be off-heap PdxInstance
+        val = ohval.getDeserializedValue(getRegion(), this.entry);
+      } finally {
+        ohval.release();
+      }
+    } else if (val instanceof CachedDeserializable) {
       val = ((CachedDeserializable)val).getDeserializedValue(getRegion(), this.entry);
     } 
     values.add(val);

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndex.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndex.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndex.java
index 9f84664..ed45742 100755
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndex.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndex.java
@@ -70,6 +70,9 @@ import com.gemstone.gemfire.internal.cache.Token;
 import com.gemstone.gemfire.internal.cache.persistence.query.CloseableIterator;
 import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
 import com.gemstone.gemfire.internal.logging.LogService;
+import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl.Chunk;
+import com.gemstone.gemfire.internal.offheap.annotations.Released;
+import com.gemstone.gemfire.internal.offheap.annotations.Retained;
 
 /**
  * A HashIndex is an index that can be used for equal and not equals queries It
@@ -815,12 +818,20 @@ public class HashIndex extends AbstractIndex {
     return ((LocalRegion) getRegion()).new NonTXEntry(entry);
   }
   
+  // TODO OFFHEAP: may return PdxInstance
   private Object getTargetObjectForUpdate(RegionEntry entry) {
     if (this.indexOnValues) {
-      Object o = entry.getValueInVMOrDiskWithoutFaultIn((LocalRegion) getRegion()); // OFFHEAP: incrc, deserialize, decrc
+      Object o = entry.getValueOffHeapOrDiskWithoutFaultIn((LocalRegion) getRegion());
       try {
-        if (o instanceof CachedDeserializable) {
-          return ((CachedDeserializable) o).getDeserializedForReading();
+        if (o instanceof Chunk) {
+          Chunk ohval = (Chunk) o;
+          try {
+            o = ohval.getDeserializedForReading();
+          } finally {
+            ohval.release();
+          }
+        } else if (o instanceof CachedDeserializable) {
+          o = ((CachedDeserializable)o).getDeserializedForReading();
         }
       } catch (EntryDestroyedException ede) {
         return Token.INVALID;
@@ -1416,6 +1427,7 @@ public class HashIndex extends AbstractIndex {
 
     Object evaluateKey(Object object) {
       Object value = object;
+      
       ExecutionContext newContext = null;
 
       if (object instanceof RegionEntry) {

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndexSet.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndexSet.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndexSet.java
index 187d601..c0bd719 100755
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndexSet.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/HashIndexSet.java
@@ -24,6 +24,7 @@ import com.gemstone.gemfire.cache.query.internal.types.TypeUtils;
 import com.gemstone.gemfire.internal.cache.CachePerfStats;
 import com.gemstone.gemfire.internal.cache.CachedDeserializable;
 import com.gemstone.gemfire.internal.cache.RegionEntry;
+import com.gemstone.gemfire.internal.offheap.StoredObject;
 import com.gemstone.gemfire.internal.util.ObjectProcedure;
 import com.gemstone.gemfire.internal.util.PrimeFinder;
 
@@ -1077,7 +1078,16 @@ public class HashIndexSet implements Set {
           for (Object o : ((Collection) object)) {
             if (o != null) {
               RegionEntry re = (RegionEntry) o;
-              Object val = re._getValue();
+              Object val = re._getValue(); // OFFHEAP _getValue ok
+              if (val instanceof StoredObject) {
+                // We don't have enough info here to deserialize an off-heap value
+                // so we can't call getDeserializedForReading.
+                // Also we can't call _getValueRetain because we do not
+                // know what region to pass in to it.
+                // So for now we just convert it to a String which all StoredObject
+                // impls can do without needing a refcount or to decompress.
+                val = val.toString();
+              }
               if (val instanceof CachedDeserializable) {
                 val = ((CachedDeserializable) val).getDeserializedForReading();
               }
@@ -1086,7 +1096,16 @@ public class HashIndexSet implements Set {
           }
         } else {
           RegionEntry re = (RegionEntry) object;
-          Object val = re._getValue();
+          Object val = re._getValue(); // OFFHEAP _getValue ok
+          if (val instanceof StoredObject) {
+            // We don't have enough info here to deserialize an off-heap value
+            // so we can't call getDeserializedForReading.
+            // Also we can't call _getValueRetain because we do not
+            // know what region to pass in to it.
+            // So for now we just convert it to a String which all StoredObject
+            // impls can do without needing a refcount or to decompress.
+            val = val.toString();
+          }
           if (val instanceof CachedDeserializable) {
             val = ((CachedDeserializable) val).getDeserializedForReading();
           }

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexManager.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexManager.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexManager.java
index a6b1bc1..0a9b166 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexManager.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/IndexManager.java
@@ -84,8 +84,9 @@ public class IndexManager  {
   public static final int RECREATE_INDEX = 4;
   protected final Region region;
 
-  private boolean isOverFlowToDisk;
-  private boolean indexMaintenanceSynchronous = true;
+  private final boolean isOverFlowToDisk;
+  private final boolean offHeap;
+  private final boolean indexMaintenanceSynchronous;
   private int numCreators = 0;
   private int numUpdatersInProgress = 0;
   private int numUpdatersInWaiting = 0;
@@ -152,6 +153,7 @@ public class IndexManager  {
         .getIndexMaintenanceSynchronous();
     isOverFlowToDisk = region.getAttributes().getEvictionAttributes()
         .getAction().isOverflowToDisk();
+    this.offHeap = region.getAttributes().getOffHeap();
     if (!indexMaintenanceSynchronous) {
       final LoggingThreadGroup group =
         LoggingThreadGroup.createThreadGroup("QueryMonitor Thread Group", logger);
@@ -303,15 +305,22 @@ public class IndexManager  {
       if (!isCompactOrHash && indexType != IndexType.PRIMARY_KEY) {
         
         if (indexType == IndexType.HASH ) {
-          if (!getRegion().getAttributes().getIndexMaintenanceSynchronous()) {
+          if (!isIndexMaintenanceTypeSynchronous()) {
             throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_HASH_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_ASYNC_MAINTENANCE.toLocalizedString());
           } 
           throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_HASH_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_MULTIPLE_ITERATORS.toLocalizedString());
         }
         // Overflow is not supported with range index.
-        if(isOverFlowToDisk) {
+        if(isOverFlowRegion()) {
           throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_REGIONS_WHICH_OVERFLOW_TO_DISK_THE_REGION_INVOLVED_IS_0.toLocalizedString(region.getFullPath()));
         }
+        // OffHeap is not supported with range index.
+        if(isOffHeap()) {
+          if (!isIndexMaintenanceTypeSynchronous()) {
+            throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_OFF_HEAP_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_ASYNC_MAINTENANCE_THE_REGION_IS_0.toLocalizedString(region.getFullPath()));
+          } 
+          throw new UnsupportedOperationException(LocalizedStrings.DefaultQueryService_OFF_HEAP_INDEX_CREATION_IS_NOT_SUPPORTED_FOR_MULTIPLE_ITERATORS_THE_REGION_IS_0.toLocalizedString(region.getFullPath()));
+        }
       }
 
       if (logger.isDebugEnabled()) {
@@ -875,7 +884,7 @@ public class IndexManager  {
         logger.debug("IndexMananger::rerunIndexCreationQuery: Exception in callback beforeRerunningIndexcreationQuery", e);
       }
     }
-    if (indexMaintenanceSynchronous) {
+    if (isIndexMaintenanceTypeSynchronous()) {
       recreateAllIndexesForRegion();
     }
     else {
@@ -904,6 +913,14 @@ public class IndexManager  {
         // Fault in the value once before index update so that every index
         // update does not have
         // to read the value from disk every time.
+        // TODO OFFHEAP: this optimization (calling getValue to make sure it is faulted in to disk) has a performance problem.
+        // It also decompresses and deserializes the value and then throws that away. In the case of a heap region the deserialized
+        // value would be cached in a VMCachedDeserializable. But for compression and/or off-heap the decompression and/or deserialization
+        // this call does is lost and has to be done again. We could just add a method to RegionEntry that faults the value in without returning it.
+        // Even better (but more work): could we create a wrapper around RegionEntry that we create here to wrap "entry" and pass the wrapper to addIndexMapping?
+        // Any indexes that store a reference to the RegionEntry would need to ask the wrapper for the real one but any of them
+        // that want the value could get it from the wrapper. The first time the wrapper is asked for the value it could get it from
+        // the real RegionEntry it wraps and cache a reference to that value. I think that gives us the best of both worlds.
         entry.getValue((LocalRegion)this.region);
         Iterator<Index> indexSetIterator = indexSet.iterator();
         while(indexSetIterator.hasNext()) {
@@ -974,7 +991,7 @@ public class IndexManager  {
       logger.debug("IndexManager.updateIndexes {} + action: {}", entry.getKey(), action);
     }
     if (entry == null) return;
-    if (indexMaintenanceSynchronous) {
+    if (isIndexMaintenanceTypeSynchronous()) {
       //System.out.println("Synchronous update");
       processAction(entry, action, opCode);
     }
@@ -1273,7 +1290,7 @@ public class IndexManager  {
    */
   public void destroy() throws QueryException {
     this.indexes.clear();
-    if (!indexMaintenanceSynchronous) updater.shutdown();
+    if (!isIndexMaintenanceTypeSynchronous()) updater.shutdown();
   }
   
   /**
@@ -1319,6 +1336,9 @@ public class IndexManager  {
   public boolean isOverFlowRegion() {
     return this.isOverFlowToDisk;
   }
+  public boolean isOffHeap() {
+    return this.offHeap;
+  }
 
   public static boolean isObjectModificationInplace() {
     return (INPLACE_OBJECT_MODIFICATION || INPLACE_OBJECT_MODIFICATION_FOR_TEST);
@@ -1583,7 +1603,7 @@ public class IndexManager  {
       }
       //Hash index not supported for overflow but we "thought" we were so let's maintain backwards compatibility
       //and create a regular compact range index instead
-      if (indexType == IndexType.HASH && isOverFlowToDisk) {
+      if (indexType == IndexType.HASH && isOverFlowRegion()) {
         indexType = IndexType.FUNCTIONAL;
       }
       if (indexType == IndexType.PRIMARY_KEY) {

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/RangeIndex.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/RangeIndex.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/RangeIndex.java
index 1af94e7..a1e84da 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/RangeIndex.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/index/RangeIndex.java
@@ -1122,8 +1122,16 @@ private void addValuesToResult(Object entriesMap, Collection result,
     throws FunctionDomainException, TypeMismatchException,
     NameResolutionException, QueryInvocationTargetException
 {
-  if (entriesMap == null || result == null || verifyLimit(result, limit, context))
+  boolean limitApplied = false;
+  if (entriesMap == null || result == null || (limitApplied = verifyLimit(result, limit, context))) {
+    if(limitApplied) {
+      QueryObserver observer = QueryObserverHolder.getInstance();
+      if(observer != null) {
+        observer.limitAppliedAtIndexLevel(this, limit, result);
+      }
+    }
     return;
+  }
   QueryObserver observer = QueryObserverHolder.getInstance();
   if (entriesMap instanceof SortedMap) {
     Iterator entriesIter = ((SortedMap)entriesMap).entrySet().iterator();

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTAggregateFunc.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTAggregateFunc.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTAggregateFunc.java
new file mode 100644
index 0000000..1bfe010
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTAggregateFunc.java
@@ -0,0 +1,50 @@
+package com.gemstone.gemfire.cache.query.internal.parse;
+
+import antlr.Token;
+
+import com.gemstone.gemfire.cache.query.QueryInvalidException;
+import com.gemstone.gemfire.cache.query.internal.CompiledValue;
+import com.gemstone.gemfire.cache.query.internal.QCompiler;
+
+/**
+ * 
+ * @author ashahid
+ *
+ */
+public class ASTAggregateFunc extends GemFireAST {
+  private static final long serialVersionUID = 8713004765228379685L;
+  private int  aggFunctionType;
+  private boolean  distinctOnly = false;
+  
+  
+  public ASTAggregateFunc() { 
+    
+  }
+  
+  
+  public ASTAggregateFunc(Token t) {
+    super(t);    
+  }
+  
+  public void setAggregateFunctionType(int type) {
+    this.aggFunctionType = type;
+  }
+  
+  public void setDistinctOnly(boolean distinctOnly) {
+    this.distinctOnly = distinctOnly;
+  }
+  
+  @Override
+  public void compile(QCompiler compiler) {
+    super.compile(compiler);
+    Object expr = compiler.pop();
+    if(expr instanceof String) {
+      if(((String)expr).equals("*")) {
+        expr = null;
+      }else {
+        throw new QueryInvalidException("invalid parameter to aggregate function");
+      }
+    }
+    compiler.aggregateFunction((CompiledValue)expr, this.aggFunctionType, this.distinctOnly);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTCount.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTCount.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTCount.java
deleted file mode 100644
index 8b89dee..0000000
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTCount.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/*=========================================================================
- * Copyright (c) 2010-2014 Pivotal Software, Inc. All Rights Reserved.
- * This product is protected by U.S. and international copyright
- * and intellectual property laws. Pivotal products are covered by
- * one or more patents listed at http://www.pivotal.io/patents.
- *=========================================================================
- */
-/**
- * 
- */
-package com.gemstone.gemfire.cache.query.internal.parse;
-
-import com.gemstone.gemfire.cache.query.internal.QCompiler;
-
-import antlr.Token;
-
-/**
- * @author shobhit
- * @since 6.6
- */
-public class ASTCount extends GemFireAST {
-
-  /**
-   * 
-   */
-  public ASTCount() {
-  }
-
-  /**
-   * @param tok
-   */
-  public ASTCount(Token tok) {
-    super(tok);
-  }
-
-  @Override
-  public void compile(QCompiler compiler) {
-    GemFireAST child = (GemFireAST)getFirstChild();
-    int tokenType = child.getType();
-    if (tokenType == OQLLexerTokenTypes.TOK_STAR) {
-      compiler.push("COUNT");
-    }
-  }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTDummy.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTDummy.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTDummy.java
new file mode 100644
index 0000000..4abfedf
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTDummy.java
@@ -0,0 +1,23 @@
+package com.gemstone.gemfire.cache.query.internal.parse;
+
+import antlr.Token;
+
+import com.gemstone.gemfire.cache.query.internal.QCompiler;
+
+public class ASTDummy extends GemFireAST {
+private static final long serialVersionUID = -5390937473819850292L;
+  
+  public ASTDummy() { }
+
+  /** Creates a new instance of ASTCombination */
+  public ASTDummy(Token t) {
+    super(t);
+  }
+    
+  @Override
+  public void compile(QCompiler compiler) {
+    super.compile(compiler);
+    compiler.push(this.getText());
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTGroupBy.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTGroupBy.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTGroupBy.java
new file mode 100644
index 0000000..25924f9
--- /dev/null
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTGroupBy.java
@@ -0,0 +1,35 @@
+/*=========================================================================
+ * Copyright (c) 2005-2014 Pivotal Software, Inc. All Rights Reserved.
+ * This product is protected by U.S. and international copyright
+ * and intellectual property laws. Pivotal products are covered by
+ * more patents listed at http://www.pivotal.io/patents.
+ *========================================================================
+ */
+package com.gemstone.gemfire.cache.query.internal.parse;
+
+import antlr.*;
+import com.gemstone.gemfire.cache.query.internal.QCompiler;
+/**
+ * 
+ * @author Yogesh Mahajan
+ *
+ */
+public class ASTGroupBy extends GemFireAST {
+  private static final long serialVersionUID = 2262777181888775078L;
+
+  public ASTGroupBy(Token t) {
+    super(t);
+  }
+  
+  public ASTGroupBy() { }
+
+
+  @Override
+    public void compile(QCompiler compiler)
+    {
+        super.compile(compiler);
+        compiler.compileGroupByClause(this.getNumberOfChildren());
+    }
+
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/31d1b20e/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTSelect.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTSelect.java b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTSelect.java
index b74ef63..250c860 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTSelect.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/cache/query/internal/parse/ASTSelect.java
@@ -9,9 +9,14 @@
 
 package com.gemstone.gemfire.cache.query.internal.parse;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import antlr.*;
 import antlr.collections.*;
+
 import com.gemstone.gemfire.cache.query.internal.QCompiler;
+import com.gemstone.gemfire.cache.query.internal.CompiledValue;
 
 public class ASTSelect extends GemFireAST {
   private static final long serialVersionUID = 1389351692304773456L;
@@ -27,98 +32,27 @@ public class ASTSelect extends GemFireAST {
   
   @Override
   public void compile(QCompiler compiler) {
-  	AST child = getFirstChild();
-        int clauseType = child.getType();
-  	if(clauseType == OQLLexerTokenTypes.LITERAL_hint) {
-          ((GemFireAST)child).compile(compiler);  
-          child = child.getNextSibling();
-        }else {
-          //hint is null
-          compiler.pushNull();
-        }
-  	
-    // check for DISTINCT or ALL token
-    // if DISTINCT, push "DISTINCT" onto stack, otherwise push null
-    // if found, then advance to next sibling, otherwise this child
-    // must be projection
-    if (child.getType() == OQLLexerTokenTypes.LITERAL_distinct) {
-      compiler.push("DISTINCT"); // anything non-null works here for distinct
-      child = child.getNextSibling();
-    }
-    else if (child.getType() == OQLLexerTokenTypes.LITERAL_all) {
-      compiler.pushNull();
+    AST child = getFirstChild();
+    Map<Integer, Object> queryComponents = new HashMap<Integer, Object>();
+    // The query components are :
+    // 1) Distinct | All
+    // 2) Hint
+    // 3) count
+    // 4) Projection Attrib
+    // 5) From Clause
+    // 6) where clause
+    // 7) group by clause
+    // 8) order by clause
+    // 9) limit
+    while (child != null) {
+      int clauseType = child.getType();
+      ((GemFireAST) child).compile(compiler);
+      Object compiledObject = compiler.pop();
+      queryComponents.put(clauseType, compiledObject);
       child = child.getNextSibling();
-    } 
-    else {
-      compiler.pushNull(); // let child be next in line
-    }
-    
-    //Count(*) expression
-    if (child.getType() == OQLLexerTokenTypes.LITERAL_count) {
-      ((ASTCount)child).compile(compiler);
-      compiler.pushNull(); //For No projectionAttributes
-    } else {
-      compiler.pushNull();
-      // projectionAttributes
-      if (child.getType() == OQLLexerTokenTypes.TOK_STAR) {
-        compiler.pushNull();
-      }
-      else {
-        // child is ASTCombination; compile it
-        ((ASTCombination)child).compile(compiler);
-      }
     }
 
-    // fromClause
-    child = child.getNextSibling(); 
-    ((GemFireAST)child).compile(compiler);
-   
-      
-	/*If WHERE clause ,order by clause as well as Limit clause is missing, then push 3 null as a placeholder */ 
-    if (child.getNextSibling() == null) {
-      compiler.pushNull();
-      compiler.pushNull();
-      //Asif: This placeholder is for limit 
-      compiler.pushNull();
-    }
-    else { 
-       child = child.getNextSibling();      
-       clauseType = child.getType();
-       if( clauseType != OQLLexerTokenTypes.LITERAL_order && clauseType != OQLLexerTokenTypes.LIMIT ) {
-         //  where is present , order by & limit may present |  may !present
-         ((GemFireAST)child).compile(compiler);
-         child = child.getNextSibling();
-         if(child != null) {
-           clauseType = child.getType();
-         }
-         
-       }else {
-         //Where clause is null
-         compiler.pushNull();         
-       }
-       if(clauseType == OQLLexerTokenTypes.LITERAL_order) {
-         ((GemFireAST)child).compile(compiler);
-         child = child.getNextSibling();
-         if(child != null) {
-           clauseType = child.getType();
-         } 
-       }else {
-         //Order by clause is null
-         compiler.pushNull();
-       }
-       
-       if(clauseType == OQLLexerTokenTypes.LIMIT) {
-         ((GemFireAST)child).compile(compiler);  
-         child = child.getNextSibling();
-         if(child != null) {
-           clauseType = child.getType();
-         } 
-       }else {
-         //Limit clause is null
-         compiler.pushNull();
-       }
-    }    
-    compiler.select();
+    compiler.select(queryComponents);
   }
   
   


Mime
View raw message