Adding support for accessing an item in list using non constant index
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java
index 4e82d1c..c52f4d5 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java
@@ -427,17 +427,16 @@
         Pair<ILogicalExpression, Mutable<ILogicalOperator>> p = aqlExprToAlgExpression(ia.getExpr(), tupSource);
         LogicalVariable v = context.newVar();
         AbstractFunctionCallExpression f;
-        int i = ia.getIndex();
-        if (i == IndexAccessor.ANY) {
+        if (ia.isAny()) {
             f = new ScalarFunctionCallExpression(
                     FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.ANY_COLLECTION_MEMBER));
             f.getArguments().add(new MutableObject<ILogicalExpression>(p.first));
         } else {
+            Pair<ILogicalExpression, Mutable<ILogicalOperator>> indexPair = aqlExprToAlgExpression(ia.getIndexExpr(), tupSource);
             f = new ScalarFunctionCallExpression(FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.GET_ITEM));
             f.getArguments().add(new MutableObject<ILogicalExpression>(p.first));
             f.getArguments().add(
-                    new MutableObject<ILogicalExpression>(new ConstantExpression(
-                            new AsterixConstantValue(new AInt32(i)))));
+                    new MutableObject<ILogicalExpression>(indexPair.first));
         }
         AssignOperator a = new AssignOperator(v, new MutableObject<ILogicalExpression>(f));
         a.getInputs().add(p.second);
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlPlusExpressionToPlanTranslator.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlPlusExpressionToPlanTranslator.java
index 1b37303..0c7ff7d 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlPlusExpressionToPlanTranslator.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlPlusExpressionToPlanTranslator.java
@@ -423,17 +423,16 @@
         Pair<ILogicalExpression, Mutable<ILogicalOperator>> p = aqlExprToAlgExpression(ia.getExpr(), tupSource);
         LogicalVariable v = context.newVar();
         AbstractFunctionCallExpression f;
-        int i = ia.getIndex();
-        if (i == IndexAccessor.ANY) {
+        if (ia.isAny()) {
             f = new ScalarFunctionCallExpression(
                     FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.ANY_COLLECTION_MEMBER));
             f.getArguments().add(new MutableObject<ILogicalExpression>(p.first));
         } else {
+            Pair<ILogicalExpression, Mutable<ILogicalOperator>> indexPair = aqlExprToAlgExpression(ia.getIndexExpr(), tupSource);
             f = new ScalarFunctionCallExpression(FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.GET_ITEM));
             f.getArguments().add(new MutableObject<ILogicalExpression>(p.first));
             f.getArguments().add(
-                    new MutableObject<ILogicalExpression>(new ConstantExpression(
-                            new AsterixConstantValue(new AInt32(i)))));
+                    new MutableObject<ILogicalExpression>(indexPair.first));
         }
         AssignOperator a = new AssignOperator(v, new MutableObject<ILogicalExpression>(f));
         a.getInputs().add(p.second);
diff --git a/asterix-algebra/src/main/javacc/AQLPlus.jj b/asterix-algebra/src/main/javacc/AQLPlus.jj
index 95c6b2d..dd72de4 100644
--- a/asterix-algebra/src/main/javacc/AQLPlus.jj
+++ b/asterix-algebra/src/main/javacc/AQLPlus.jj
@@ -699,7 +699,7 @@
   Expression expr = null;
   Identifier ident = null;
   AbstractAccessor fa = null;
-  int index;
+  Expression indexExpr = null;
 
 }
 {
@@ -719,12 +719,12 @@
 	}
 	)
 	| (
-		index = Index()
+		indexExpr = Index()
 		{
 		  if(fa == null)
-		  	fa = new IndexAccessor(expr, index);
+			fa = new IndexAccessor(expr, indexExpr);
 		  else
-		  	fa = new IndexAccessor(fa, index);
+			fa = new IndexAccessor(fa, indexExpr);
 		}
 	) 
 	)*
@@ -751,10 +751,9 @@
 	}
 }
 
-int Index() throws ParseException:
+Expression Index() throws ParseException:
 {
 	Expression expr = null;
-	int idx = -2;
 }
 {
   "[" ( expr = Expression()
@@ -762,20 +761,15 @@
 		if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
 		{
 			Literal lit = ((LiteralExpr)expr).getValue();
-			if(lit.getLiteralType() == Literal.Type.INTEGER || 
- 			   lit.getLiteralType() == Literal.Type.LONG) {
-				idx = Integer.valueOf(lit.getStringValue());
-			}	
-			else {
+			if(lit.getLiteralType() != Literal.Type.INTEGER &&
+			   lit.getLiteralType() != Literal.Type.LONG) {
 				throw new ParseException("Index should be an INTEGER");				
             }
 		}
-
 	}
 
   	| "?"
 	{
-		idx = IndexAccessor.ANY;
 	  // ANY
 	}
  	 
@@ -783,7 +777,7 @@
 
    "]"
 	{
-	  return idx;
+	  return expr;
 	}
 }
 
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/IndexAccessor.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/IndexAccessor.java
index bedb5e7..c3755c6 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/IndexAccessor.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/IndexAccessor.java
@@ -21,15 +21,15 @@
 
 public class IndexAccessor extends AbstractAccessor {
     private boolean any;
-    private int index;
+    private Expression indexExpr;
 
     public final static int ANY = -1;
 
-    public IndexAccessor(Expression expr, int index) {
+    public IndexAccessor(Expression expr, Expression indexExpr) {
         super(expr);
-        if (index == -1)
+        if (indexExpr == null)
             this.any = true;
-        this.index = index;
+        this.indexExpr = indexExpr;
     }
 
     public boolean isAny() {
@@ -40,12 +40,12 @@
         this.any = any;
     }
 
-    public int getIndex() {
-        return index;
+    public Expression getIndexExpr() {
+        return indexExpr;
     }
 
-    public void setIndex(int index) {
-        this.index = index;
+    public void setIndexExpr(Expression indexExpr) {
+        this.indexExpr = indexExpr;
     }
 
     @Override
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/AQLPrintVisitor.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/AQLPrintVisitor.java
index ca87a0e..ad8c791 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/AQLPrintVisitor.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/visitor/AQLPrintVisitor.java
@@ -329,10 +329,12 @@
         out.println(skip(step) + "IndexAccessor [");
         fa.getExpr().accept(this, step + 1);
         out.print(skip(step + 1) + "Index: ");
-        out.println((((IndexAccessor) fa).isAny() ? "ANY" : ((IndexAccessor) fa).getIndex()));
-
+        if (fa.isAny()) {
+            out.println("ANY");
+        } else {
+            fa.getIndexExpr().accept(this, step + 1);
+        }
         out.println(skip(step) + "]");
-
     }
 
     @Override
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/CloneAndSubstituteVariablesVisitor.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/CloneAndSubstituteVariablesVisitor.java
index 30a0ebb..1e6747e 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/CloneAndSubstituteVariablesVisitor.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/CloneAndSubstituteVariablesVisitor.java
@@ -257,7 +257,12 @@
     public Pair<IAqlExpression, List<VariableSubstitution>> visitIndexAccessor(IndexAccessor ia,
             List<VariableSubstitution> arg) throws AsterixException {
         Pair<IAqlExpression, List<VariableSubstitution>> p1 = ia.getExpr().accept(this, arg);
-        IndexAccessor i = new IndexAccessor((Expression) p1.first, ia.getIndex());
+        Expression indexExpr = null;
+        if (!ia.isAny()) {
+            Pair<IAqlExpression, List<VariableSubstitution>> p2 = ia.getIndexExpr().accept(this, arg);
+            indexExpr = (Expression) p2.first;
+        }
+        IndexAccessor i = new IndexAccessor((Expression) p1.first, indexExpr);
         i.setAny(ia.isAny());
         return new Pair<IAqlExpression, List<VariableSubstitution>>(i, arg);
     }
diff --git a/asterix-aql/src/main/javacc/AQL.jj b/asterix-aql/src/main/javacc/AQL.jj
index ba843f0..a0da3b2 100644
--- a/asterix-aql/src/main/javacc/AQL.jj
+++ b/asterix-aql/src/main/javacc/AQL.jj
@@ -1471,7 +1471,7 @@
   Expression expr = null;
   Identifier ident = null;
   AbstractAccessor fa = null;
-  int index;
+  Expression indexExpr = null;
 }
 {
   expr = PrimaryExpr() ( ident = Field()
@@ -1479,10 +1479,10 @@
 	  fa = (fa == null ? new FieldAccessor(expr, ident) 
                        : new FieldAccessor(fa, ident));
     }
-  | index = Index()
+  | indexExpr = Index()
     {
-      fa = (fa == null ? new IndexAccessor(expr, index)
-                       : new IndexAccessor(fa, index));
+      fa = (fa == null ? new IndexAccessor(expr, indexExpr)
+                       : new IndexAccessor(fa, indexExpr));
      }
   )*
     {
@@ -1501,10 +1501,9 @@
     }
 }
 
-int Index() throws ParseException:
+Expression Index() throws ParseException:
 {
 	Expression expr = null;
-	int idx = -2;
 }
 {
   <LEFTBRACKET> ( expr = Expression()
@@ -1512,20 +1511,15 @@
 		if(expr.getKind() == Expression.Kind.LITERAL_EXPRESSION)
 		{
 			Literal lit = ((LiteralExpr)expr).getValue();
-			if(lit.getLiteralType() == Literal.Type.INTEGER || 
-			   lit.getLiteralType() == Literal.Type.LONG) {
-				idx = Integer.valueOf(lit.getStringValue());
-			}
-			else {
+			if(lit.getLiteralType() != Literal.Type.INTEGER &&
+			   lit.getLiteralType() != Literal.Type.LONG) {
 				throw new ParseException("Index should be an INTEGER");				
             }
 		}
-
 	}
 
   	| <QUES>
 	{
-		idx = IndexAccessor.ANY;
 	  // ANY
 	}
  	 
@@ -1533,7 +1527,7 @@
 
    <RIGHTBRACKET>
 	{
-	  return idx;
+	  return expr;
 	}
 }