merged asterix_stabilization r620:1109

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_stabilization_temporal_functionality@1113 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/base/AbstractExpression.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/base/AbstractExpression.java
new file mode 100644
index 0000000..e83a1ed
--- /dev/null
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/base/AbstractExpression.java
@@ -0,0 +1,25 @@
+package edu.uci.ics.asterix.aql.base;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
+
+public abstract class AbstractExpression implements Expression {
+	protected List<IExpressionAnnotation> hints;
+	
+	public void addHint(IExpressionAnnotation hint) {
+    	if (hints == null) {
+    		hints = new ArrayList<IExpressionAnnotation>();
+    	}
+    	hints.add(hint);
+    }
+    
+    public boolean hasHints() {
+    	return hints != null;
+    }
+    
+    public List<IExpressionAnnotation> getHints() {
+    	return hints;
+    }
+}
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/context/FunctionExpressionMap.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/context/FunctionExpressionMap.java
index f30ae09..f013f18 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/context/FunctionExpressionMap.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/context/FunctionExpressionMap.java
@@ -2,9 +2,9 @@
 
 import java.util.HashMap;
 
-import edu.uci.ics.asterix.om.functions.AsterixFunction;
+import edu.uci.ics.asterix.common.functions.FunctionSignature;
 
-public class FunctionExpressionMap extends HashMap<Integer, AsterixFunction> {
+public class FunctionExpressionMap extends HashMap<Integer, FunctionSignature> {
     /**
      * 
      */
@@ -24,7 +24,7 @@
         this.varargs = varargs;
     }
 
-    public AsterixFunction get(int arity) {
+    public FunctionSignature get(int arity) {
         if (varargs) {
             return super.get(-1);
         } else {
@@ -32,7 +32,7 @@
         }
     }
 
-    public AsterixFunction put(int arity, AsterixFunction fd) {
+    public FunctionSignature put(int arity, FunctionSignature fd) {
         if (varargs) {
             return super.put(-1, fd);
         } else {
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/context/FunctionSignatures.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/context/FunctionSignatures.java
index 1027c42..88fff83 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/context/FunctionSignatures.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/context/FunctionSignatures.java
@@ -3,17 +3,18 @@
 import java.util.HashMap;
 import java.util.Map;
 
-import edu.uci.ics.asterix.om.functions.AsterixFunction;
+import edu.uci.ics.asterix.common.functions.FunctionSignature;
 
 public class FunctionSignatures {
-    private final Map<String, FunctionExpressionMap> functionMap;
+    private final Map<FunctionSignature, FunctionExpressionMap> functionMap;
 
     public FunctionSignatures() {
-        functionMap = new HashMap<String, FunctionExpressionMap>();
+        functionMap = new HashMap<FunctionSignature, FunctionExpressionMap>();
     }
 
-    public AsterixFunction get(String name, int arity) {
-        FunctionExpressionMap possibleFD = functionMap.get(name);
+    public FunctionSignature get(String dataverse, String name, int arity) {
+        FunctionSignature fid = new FunctionSignature(dataverse, name, arity);
+        FunctionExpressionMap possibleFD = functionMap.get(fid);
         if (possibleFD == null) {
             return null;
         } else {
@@ -21,12 +22,11 @@
         }
     }
 
-    public void put(AsterixFunction fd, boolean varargs) {
-        String name = fd.getFunctionName();
-        FunctionExpressionMap func = functionMap.get(name);
+    public void put(FunctionSignature fd, boolean varargs) {
+        FunctionExpressionMap func = functionMap.get(fd);
         if (func == null) {
             func = new FunctionExpressionMap(varargs);
-            functionMap.put(name, func);
+            functionMap.put(fd, func);
         }
         func.put(fd.getArity(), fd);
     }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/context/Scope.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/context/Scope.java
index ef338d4..4c1339a 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/context/Scope.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/context/Scope.java
@@ -5,7 +5,7 @@
 import edu.uci.ics.asterix.aql.expression.Identifier;
 import edu.uci.ics.asterix.aql.expression.VarIdentifier;
 import edu.uci.ics.asterix.aql.parser.ScopeChecker;
-import edu.uci.ics.asterix.om.functions.AsterixFunction;
+import edu.uci.ics.asterix.common.functions.FunctionSignature;
 
 public final class Scope {
     private Scope parent;
@@ -78,11 +78,11 @@
      * @param varargs
      *            whether this function has varargs
      */
-    public void addFunctionDescriptor(AsterixFunction fd, boolean varargs) {
+    public void addFunctionDescriptor(FunctionSignature signature, boolean varargs) {
         if (functionSignatures == null) {
             functionSignatures = new FunctionSignatures();
         }
-        functionSignatures.put(fd, varargs);
+        functionSignatures.put(signature, varargs);
     }
 
     /**
@@ -94,13 +94,13 @@
      *            # of arguments
      * @return FunctionDescriptor of the function found; otherwise null
      */
-    public AsterixFunction findFunctionSignature(String name, int arity) {
-        AsterixFunction fd = null;
+    public FunctionSignature findFunctionSignature(String dataverse, String name, int arity) {
+        FunctionSignature fd = null;
         if (functionSignatures != null) {
-            fd = functionSignatures.get(name, arity);
+            fd = functionSignatures.get(dataverse, name, arity);
         }
         if (fd == null && parent != null) {
-            fd = parent.findFunctionSignature(name, arity);
+            fd = parent.findFunctionSignature(dataverse, name, arity);
         }
         return fd;
     }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/BeginFeedStatement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/BeginFeedStatement.java
index a142534..422ca79 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/BeginFeedStatement.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/BeginFeedStatement.java
@@ -1,6 +1,7 @@
 package edu.uci.ics.asterix.aql.expression;
 
 import java.io.StringReader;
+import java.util.List;
 
 import edu.uci.ics.asterix.aql.base.Statement;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
@@ -8,38 +9,71 @@
 import edu.uci.ics.asterix.aql.parser.AQLParser;
 import edu.uci.ics.asterix.aql.parser.ParseException;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
+import edu.uci.ics.asterix.common.functions.FunctionSignature;
+import edu.uci.ics.asterix.metadata.MetadataException;
+import edu.uci.ics.asterix.metadata.MetadataManager;
+import edu.uci.ics.asterix.metadata.MetadataTransactionContext;
+import edu.uci.ics.asterix.metadata.entities.Dataset;
 import edu.uci.ics.asterix.metadata.entities.FeedDatasetDetails;
+import edu.uci.ics.asterix.metadata.entities.Function;
 
 public class BeginFeedStatement implements Statement {
 
-    private Identifier datasetName;
+    private final Identifier dataverseName;
+    private final Identifier datasetName;
     private Query query;
     private int varCounter;
 
-    public BeginFeedStatement(Identifier datasetName, int varCounter) {
+    public BeginFeedStatement(Identifier dataverseName, Identifier datasetName, int varCounter) {
+        this.dataverseName = dataverseName;
         this.datasetName = datasetName;
         this.varCounter = varCounter;
     }
-    
-    public void initialize(FeedDatasetDetails feedDetails){
+
+    public void initialize(MetadataTransactionContext mdTxnCtx, Dataset dataset) throws MetadataException {
         query = new Query();
-        String functionName = feedDetails.getFunctionIdentifier();
-        String stmt;
-        if(functionName == null){
-         stmt = "insert into dataset " + datasetName + " (" + " for $x in feed-ingest ('" + datasetName + "')"
-                + " return $x" + " );";
+        FeedDatasetDetails feedDetails = (FeedDatasetDetails) dataset.getDatasetDetails();
+        String functionName = feedDetails.getFunction() == null ? null : feedDetails.getFunction().getName();
+        StringBuilder builder = new StringBuilder();
+        builder.append("insert into dataset " + datasetName + " ");
+
+        if (functionName == null) {
+            builder.append(" (" + " for $x in feed-ingest ('" + datasetName + "') ");
+            builder.append(" return $x");
         } else {
-           stmt = "insert into dataset " + datasetName + " (" + " for $x in feed-ingest ('" + datasetName + "')"
-           + " return " + functionName + "(" + "$x" + ")" + ");";
+            int arity = feedDetails.getFunction().getArity();
+            FunctionSignature signature = new FunctionSignature(dataset.getDataverseName(), functionName, arity);
+            Function function = MetadataManager.INSTANCE.getFunction(mdTxnCtx, signature);
+            if (function == null) {
+                throw new MetadataException(" Unknown function " + feedDetails.getFunction());
+            }
+            if (function.getLanguage().equalsIgnoreCase(Function.LANGUAGE_AQL)) {
+                String param = function.getParams().get(0);
+                builder.append(" (" + " for" + " " + param + " in feed-ingest ('" + datasetName + "') ");
+                builder.append(" let $y:=(" + function.getFunctionBody() + ")" + " return $y");
+            } else {
+                builder.append(" (" + " for $x in feed-ingest ('" + datasetName + "') ");
+                builder.append(" let $y:=" + function.getName() + "(" + "$x" + ")");
+                builder.append(" return $y");
+            }
+
         }
-        AQLParser parser = new AQLParser(new StringReader(stmt));
+        builder.append(")");
+        builder.append(";");
+        AQLParser parser = new AQLParser(new StringReader(builder.toString()));
+
+        List<Statement> statements;
         try {
-            query = (Query) parser.Statement();
+            statements = parser.Statement();
+            query = ((InsertStatement) statements.get(0)).getQuery();
         } catch (ParseException pe) {
-            throw new RuntimeException(pe);
+            throw new MetadataException(pe);
         }
 
-        query = ((InsertStatement) query.getPrologDeclList().get(0)).getQuery();
+    }
+
+    public Identifier getDataverseName() {
+        return dataverseName;
     }
 
     public Identifier getDatasetName() {
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/CallExpr.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/CallExpr.java
index cda7e69..9bd59d4 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/CallExpr.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/CallExpr.java
@@ -2,54 +2,44 @@
 
 import java.util.List;
 
+import edu.uci.ics.asterix.aql.base.AbstractExpression;
 import edu.uci.ics.asterix.aql.base.Expression;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlVisitorWithVoidReturn;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
-import edu.uci.ics.asterix.om.functions.AsterixFunction;
+import edu.uci.ics.asterix.common.functions.FunctionSignature;
 
-public class CallExpr implements Expression {
-    private AsterixFunction ident;
+public class CallExpr extends AbstractExpression {
+    private final FunctionSignature functionSignature;
     private List<Expression> exprList;
     private boolean isBuiltin;
 
-    public CallExpr() {
-    }
-
-    public CallExpr(AsterixFunction ident, List<Expression> exprList) {
-        this.ident = ident;
+    public CallExpr(FunctionSignature functionSignature, List<Expression> exprList) {
+        this.functionSignature = functionSignature;
         this.exprList = exprList;
     }
 
-    public AsterixFunction getIdent() {
-        return ident;
-    }
-
-    public void setIdent(AsterixFunction ident) {
-        this.ident = ident;
+    public FunctionSignature getFunctionSignature() {
+        return functionSignature;
     }
 
     public List<Expression> getExprList() {
         return exprList;
     }
 
-    public void setExprList(List<Expression> exprList) {
-        this.exprList = exprList;
-    }
-
     public boolean isBuiltin() {
         return isBuiltin;
     }
 
-    public void setIsBuiltin(boolean builtin) {
-        this.isBuiltin = builtin;
-    }
-
     @Override
     public Kind getKind() {
         return Kind.CALL_EXPRESSION;
     }
 
+    public void setExprList(List<Expression> exprList) {
+        this.exprList = exprList;
+    }
+
     @Override
     public <T> void accept(IAqlVisitorWithVoidReturn<T> visitor, T arg) throws AsterixException {
         visitor.visit(this, arg);
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/ControlFeedStatement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/ControlFeedStatement.java
index 844fec6..4ceb0e3 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/ControlFeedStatement.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/ControlFeedStatement.java
@@ -9,7 +9,8 @@
 
 public class ControlFeedStatement implements Statement {
 
-    private Identifier datasetName;
+    private final Identifier dataverseName;
+    private final Identifier datasetName;
 
     public enum OperationType {
         BEGIN,
@@ -22,18 +23,24 @@
     private OperationType operationType;
     private Map<String, String> alterAdapterConfParams;
 
-    public ControlFeedStatement(OperationType operation, Identifier datasetName) {
+    public ControlFeedStatement(OperationType operation, Identifier dataverseName, Identifier datasetName) {
         this.operationType = operation;
         this.datasetName = datasetName;
+        this.dataverseName = dataverseName;
     }
 
-    public ControlFeedStatement(OperationType operation, Identifier datasetName,
+    public ControlFeedStatement(OperationType operation, Identifier dataverseName, Identifier datasetName,
             Map<String, String> alterAdapterConfParams) {
         this.operationType = operation;
         this.datasetName = datasetName;
+        this.dataverseName = dataverseName;
         this.alterAdapterConfParams = alterAdapterConfParams;
     }
-    
+
+    public Identifier getDataverseName() {
+        return dataverseName;
+    }
+
     public Identifier getDatasetName() {
         return datasetName;
     }
@@ -54,7 +61,7 @@
     public Map<String, String> getAlterAdapterConfParams() {
         return alterAdapterConfParams;
     }
-    
+
     @Override
     public <R, T> R accept(IAqlExpressionVisitor<R, T> visitor, T arg) throws AsterixException {
         return visitor.visitControlFeedStatement(this, arg);
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/CreateFunctionStatement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/CreateFunctionStatement.java
index 6ffd39d..6a8fec6 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/CreateFunctionStatement.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/CreateFunctionStatement.java
@@ -7,39 +7,26 @@
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlVisitorWithVoidReturn;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
-import edu.uci.ics.asterix.om.functions.AsterixFunction;
+import edu.uci.ics.asterix.common.functions.FunctionSignature;
 
 public class CreateFunctionStatement implements Statement {
 
-    private AsterixFunction asterixFunction;
-    private String functionBody;
-    private boolean ifNotExists;
-    private List<String> paramList;
+    private final FunctionSignature signature;
+    private final String functionBody;
+    private final boolean ifNotExists;
+    private final List<String> paramList;
 
-    public AsterixFunction getFunctionIdentifier() {
-        return asterixFunction;
-    }
-
-    public void setFunctionIdentifier(AsterixFunction AsterixFunction) {
-        this.asterixFunction = AsterixFunction;
+    public FunctionSignature getaAterixFunction() {
+        return signature;
     }
 
     public String getFunctionBody() {
         return functionBody;
     }
 
-    public void setFunctionBody(String functionBody) {
-        this.functionBody = functionBody;
-    }
-
-    public void setIfNotExists(boolean ifNotExists) {
-        this.ifNotExists = ifNotExists;
-    }
-
-    public CreateFunctionStatement(AsterixFunction AsterixFunction, List<VarIdentifier> parameterList, String functionBody,
+    public CreateFunctionStatement(FunctionSignature signature, List<VarIdentifier> parameterList, String functionBody,
             boolean ifNotExists) {
-        
-        this.asterixFunction = AsterixFunction;
+        this.signature = signature;
         this.functionBody = functionBody;
         this.ifNotExists = ifNotExists;
         this.paramList = new ArrayList<String>();
@@ -61,8 +48,8 @@
         return paramList;
     }
 
-    public void setParamList(List<String> paramList) {
-        this.paramList = paramList;
+    public FunctionSignature getSignature() {
+        return signature;
     }
 
     @Override
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/CreateIndexStatement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/CreateIndexStatement.java
index ab75af5..ffd0534 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/CreateIndexStatement.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/CreateIndexStatement.java
@@ -13,6 +13,7 @@
 
     private Identifier indexName;
     private boolean needToCreate = true;
+    private Identifier dataverseName;
     private Identifier datasetName;
     private List<String> fieldExprs = new ArrayList<String>();
     private IndexType indexType = IndexType.BTREE;
@@ -48,6 +49,14 @@
         this.indexName = indexName;
     }
 
+    public Identifier getDataverseName() {
+        return dataverseName;
+    }
+
+    public void setDataverseName(Identifier dataverseName) {
+        this.dataverseName = dataverseName;
+    }
+
     public Identifier getDatasetName() {
         return datasetName;
     }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/DatasetDecl.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/DatasetDecl.java
index 7996062..463a256 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/DatasetDecl.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/DatasetDecl.java
@@ -21,17 +21,21 @@
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
 
 public class DatasetDecl implements Statement {
-    protected Identifier name;
-    protected Identifier itemTypeName;
-    protected DatasetType datasetType;
-    protected IDatasetDetailsDecl datasetDetailsDecl;
+    protected final Identifier name;
+    protected final Identifier dataverse;
+    protected final Identifier itemTypeName;
+    protected final DatasetType datasetType;
+    protected final IDatasetDetailsDecl datasetDetailsDecl;
 
     public boolean ifNotExists;
 
-    public DatasetDecl(Identifier name, Identifier itemTypeName, IDatasetDetailsDecl idd, boolean ifNotExists) {
+    public DatasetDecl(Identifier dataverse, Identifier name, Identifier itemTypeName, DatasetType datasetType,
+            IDatasetDetailsDecl idd, boolean ifNotExists) {
+        this.dataverse = dataverse;
         this.name = name;
         this.itemTypeName = itemTypeName;
         this.ifNotExists = ifNotExists;
+        this.datasetType = datasetType;
         datasetDetailsDecl = idd;
     }
 
@@ -39,10 +43,6 @@
         return this.ifNotExists;
     }
 
-    public void setDatasetType(DatasetType datasetType) {
-        this.datasetType = datasetType;
-    }
-
     public DatasetType getDatasetType() {
         return datasetType;
     }
@@ -51,18 +51,10 @@
         return name;
     }
 
-    public void setName(Identifier name) {
-        this.name = name;
-    }
-
     public Identifier getItemTypeName() {
         return itemTypeName;
     }
 
-    public void setItemTypeName(Identifier itemTypeName) {
-        this.itemTypeName = itemTypeName;
-    }
-
     @Override
     public <R, T> R accept(IAqlExpressionVisitor<R, T> visitor, T arg) throws AsterixException {
         return visitor.visitDatasetDecl(this, arg);
@@ -82,7 +74,8 @@
         return datasetDetailsDecl;
     }
 
-    public void setDatasetDetailsDecl(IDatasetDetailsDecl datasetDetailsDecl) {
-        this.datasetDetailsDecl = datasetDetailsDecl;
+    public Identifier getDataverse() {
+        return dataverse;
     }
+
 }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/DeleteStatement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/DeleteStatement.java
index 9d957cf..48b7909 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/DeleteStatement.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/DeleteStatement.java
@@ -10,14 +10,16 @@
 public class DeleteStatement implements Statement {
 
     private VariableExpr vars;
+    private Identifier dataverseName;
     private Identifier datasetName;
     private Expression condition;
     private Clause dieClause;
     private int varCounter;
 
-    public DeleteStatement(VariableExpr vars, Identifier datasetName, Expression condition, Clause dieClause,
-            int varCounter) {
+    public DeleteStatement(VariableExpr vars, Identifier dataverseName, Identifier datasetName, Expression condition,
+            Clause dieClause, int varCounter) {
         this.vars = vars;
+        this.dataverseName = dataverseName;
         this.datasetName = datasetName;
         this.condition = condition;
         this.dieClause = dieClause;
@@ -33,6 +35,10 @@
         return vars;
     }
 
+    public Identifier getDataverseName() {
+        return dataverseName;
+    }
+
     public Identifier getDatasetName() {
         return datasetName;
     }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/DropStatement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/DropStatement.java
index 9ddec0e..76d952b 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/DropStatement.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/DropStatement.java
@@ -7,10 +7,12 @@
 
 public class DropStatement implements Statement {
 
-    private Identifier datasetName;
+    private final Identifier dataverseName;
+    private final Identifier datasetName;
     private boolean ifExists;
 
-    public DropStatement(Identifier datasetName, boolean ifExists) {
+    public DropStatement(Identifier dataverseName, Identifier datasetName, boolean ifExists) {
+        this.dataverseName = dataverseName;
         this.datasetName = datasetName;
         this.ifExists = ifExists;
     }
@@ -20,6 +22,10 @@
         return Kind.DATASET_DROP;
     }
 
+    public Identifier getDataverseName() {
+        return dataverseName;
+    }
+
     public Identifier getDatasetName() {
         return datasetName;
     }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/FeedDetailsDecl.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/FeedDetailsDecl.java
index cb32ac6..2c99a3d 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/FeedDetailsDecl.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/FeedDetailsDecl.java
@@ -14,34 +14,37 @@
  */
 package edu.uci.ics.asterix.aql.expression;
 
+import java.util.List;
 import java.util.Map;
 
+import edu.uci.ics.asterix.common.functions.FunctionSignature;
+
 public class FeedDetailsDecl extends InternalDetailsDecl {
-    private Map<String, String> properties;
-    private String adapterClassname;
-    private String functionIdentifier;
+    private final Map<String, String> configuration;
+    private final String adapterFactoryClassname;
+    private final FunctionSignature functionSignature;
 
-    public void setFunctionIdentifier(String functionIdentifier) {
-        this.functionIdentifier = functionIdentifier;
+    public FeedDetailsDecl(String adapterFactoryClassname, Map<String, String> configuration,
+            FunctionSignature signature, Identifier nodeGroupName, List<String> partitioningExpr) {
+        super(nodeGroupName, partitioningExpr);
+        this.adapterFactoryClassname = adapterFactoryClassname;
+        this.configuration = configuration;
+        this.functionSignature = signature;
     }
 
-    public void setAdapterClassname(String adapter) {
-        this.adapterClassname = adapter;
+    public Map<String, String> getConfiguration() {
+        return configuration;
     }
 
-    public void setProperties(Map<String, String> properties) {
-        this.properties = properties;
+    public String getAdapterFactoryClassname() {
+        return adapterFactoryClassname;
     }
 
-    public String getAdapterClassname() {
-        return adapterClassname;
+    public FunctionSignature getSignature() {
+        return functionSignature;
     }
 
-    public Map<String, String> getProperties() {
-        return properties;
-    }
-
-    public String getFunctionIdentifier() {
-        return functionIdentifier;
+    public FunctionSignature getFunctionSignature() {
+        return functionSignature;
     }
 }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/FunctionDecl.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/FunctionDecl.java
index e69035a..8871c79 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/FunctionDecl.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/FunctionDecl.java
@@ -7,38 +7,27 @@
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlVisitorWithVoidReturn;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
-import edu.uci.ics.asterix.om.functions.AsterixFunction;
+import edu.uci.ics.asterix.common.functions.FunctionSignature;
 
 public class FunctionDecl implements Statement {
-    private AsterixFunction ident;
+    private FunctionSignature signature;
     private List<VarIdentifier> paramList;
     private Expression funcBody;
 
-    public FunctionDecl() {
-    }
-
-    public FunctionDecl(AsterixFunction ident, List<VarIdentifier> paramList, Expression funcBody) {
-        this.ident = ident;
+    public FunctionDecl(FunctionSignature signature, List<VarIdentifier> paramList, Expression funcBody) {
+        this.signature = signature;
         this.paramList = paramList;
         this.funcBody = funcBody;
     }
 
-    public AsterixFunction getIdent() {
-        return ident;
-    }
-
-    public void setIdent(AsterixFunction ident) {
-        this.ident = ident;
+    public FunctionSignature getSignature() {
+        return signature;
     }
 
     public List<VarIdentifier> getParamList() {
         return paramList;
     }
 
-    public void setParamList(List<VarIdentifier> paramList) {
-        this.paramList = paramList;
-    }
-
     public Expression getFuncBody() {
         return funcBody;
     }
@@ -47,6 +36,24 @@
         this.funcBody = funcBody;
     }
 
+    public void setSignature(FunctionSignature signature) {
+        this.signature = signature;
+    }
+
+    public void setParamList(List<VarIdentifier> paramList) {
+        this.paramList = paramList;
+    }
+
+    @Override
+    public int hashCode() {
+        return signature.hashCode();
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        return (o instanceof FunctionDecl && ((FunctionDecl) o).getSignature().equals(signature));
+    }
+
     @Override
     public Kind getKind() {
         return Kind.FUNCTION_DECL;
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/FunctionDropStatement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/FunctionDropStatement.java
index 4b96143..054bc5a 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/FunctionDropStatement.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/FunctionDropStatement.java
@@ -4,16 +4,15 @@
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlVisitorWithVoidReturn;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
+import edu.uci.ics.asterix.common.functions.FunctionSignature;
 
 public class FunctionDropStatement implements Statement {
 
-    private Identifier functionName;
-    private int arity;
+    private final FunctionSignature signature;
     private boolean ifExists;
 
-    public FunctionDropStatement(Identifier functionName, int arity, boolean ifExists) {
-        this.functionName = functionName;
-        this.arity = arity;
+    public FunctionDropStatement(FunctionSignature signature, boolean ifExists) {
+        this.signature = signature;
         this.ifExists = ifExists;
     }
 
@@ -22,22 +21,14 @@
         return Kind.FUNCTION_DROP;
     }
 
-    public Identifier getFunctionName() {
-        return functionName;
+    public FunctionSignature getFunctionSignature() {
+        return signature;
     }
 
     public boolean getIfExists() {
         return ifExists;
     }
 
-    public int getArity() {
-        return arity;
-    }
-
-    public void setArity(int arity) {
-        this.arity = arity;
-    }
-
     @Override
     public <R, T> R accept(IAqlExpressionVisitor<R, T> visitor, T arg) throws AsterixException {
         return visitor.visitFunctionDropStatement(this, arg);
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/IndexDropStatement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/IndexDropStatement.java
index b69ccd1..43831d0 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/IndexDropStatement.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/IndexDropStatement.java
@@ -7,12 +7,14 @@
 
 public class IndexDropStatement implements Statement {
 
+    private Identifier dataverseName;
     private Identifier datasetName;
     private Identifier indexName;
     private boolean ifExists;
 
-    public IndexDropStatement(Identifier dataverseName, Identifier indexName, boolean ifExists) {
-        this.datasetName = dataverseName;
+    public IndexDropStatement(Identifier dataverseName, Identifier datasetName, Identifier indexName, boolean ifExists) {
+        this.dataverseName = dataverseName;
+        this.datasetName = datasetName;
         this.indexName = indexName;
         this.ifExists = ifExists;
     }
@@ -22,6 +24,10 @@
         return Kind.INDEX_DROP;
     }
 
+    public Identifier getDataverseName() {
+        return dataverseName;
+    }
+
     public Identifier getDatasetName() {
         return datasetName;
     }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/InsertStatement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/InsertStatement.java
index 7faa33e..c7b1f6d 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/InsertStatement.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/InsertStatement.java
@@ -7,11 +7,13 @@
 
 public class InsertStatement implements Statement {
 
-    private Identifier datasetName;
-    private Query query;
-    private int varCounter;
+    private final Identifier dataverseName;
+    private final Identifier datasetName;
+    private final Query query;
+    private final int varCounter;
 
-    public InsertStatement(Identifier datasetName, Query query, int varCounter) {
+    public InsertStatement(Identifier dataverseName, Identifier datasetName, Query query, int varCounter) {
+        this.dataverseName = dataverseName;
         this.datasetName = datasetName;
         this.query = query;
         this.varCounter = varCounter;
@@ -22,6 +24,10 @@
         return Kind.INSERT;
     }
 
+    public Identifier getDataverseName() {
+        return dataverseName;
+    }
+
     public Identifier getDatasetName() {
         return datasetName;
     }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/InternalDetailsDecl.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/InternalDetailsDecl.java
index a680e48..2a7c89c 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/InternalDetailsDecl.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/InternalDetailsDecl.java
@@ -14,31 +14,24 @@
  */
 package edu.uci.ics.asterix.aql.expression;
 
-import java.util.ArrayList;
 import java.util.List;
 
 import edu.uci.ics.asterix.metadata.bootstrap.MetadataConstants;
 
 public class InternalDetailsDecl implements IDatasetDetailsDecl {
-    private Identifier nodegroupName = new Identifier(MetadataConstants.METADATA_DEFAULT_NODEGROUP_NAME);
-    private List<String> partitioningExprs = new ArrayList<String>();
+    private final Identifier nodegroupName;
+    private final List<String> partitioningExprs;
 
-    public void addPartitioningExpr(String pe) {
-        this.partitioningExprs.add(pe);
-    }
-
-    public void addPartitioningExprList(List<String> peList) {
-        this.partitioningExprs = peList;
+    public InternalDetailsDecl(Identifier nodeGroupName, List<String> partitioningExpr) {
+        this.nodegroupName = nodeGroupName == null ? new Identifier(MetadataConstants.METADATA_DEFAULT_NODEGROUP_NAME)
+                : nodeGroupName;
+        this.partitioningExprs = partitioningExpr;
     }
 
     public List<String> getPartitioningExprs() {
         return partitioningExprs;
     }
 
-    public void setNodegroupName(Identifier nodegroupName) {
-        this.nodegroupName = nodegroupName;
-    }
-
     public Identifier getNodegroupName() {
         return nodegroupName;
     }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/LoadFromFileStatement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/LoadFromFileStatement.java
index 011ee898..ecbdb42 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/LoadFromFileStatement.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/LoadFromFileStatement.java
@@ -10,12 +10,14 @@
 public class LoadFromFileStatement implements Statement {
 
     private Identifier datasetName;
+    private Identifier dataverseName;
     private String adapter;
     private Map<String, String> properties;
     private boolean dataIsLocallySorted;
 
-    public LoadFromFileStatement(Identifier datasetName, String adapter, Map<String, String> propertiees,
-            boolean dataIsLocallySorted) {
+    public LoadFromFileStatement(Identifier dataverseName, Identifier datasetName, String adapter,
+            Map<String, String> propertiees, boolean dataIsLocallySorted) {
+        this.dataverseName = dataverseName;
         this.datasetName = datasetName;
         this.adapter = adapter;
         this.properties = propertiees;
@@ -38,6 +40,14 @@
         this.properties = properties;
     }
 
+    public Identifier getDataverseName() {
+        return dataverseName;
+    }
+
+    public void setDataverseName(Identifier dataverseName) {
+        this.dataverseName = dataverseName;
+    }
+
     @Override
     public Kind getKind() {
         return Kind.LOAD_FROM_FILE;
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/OperatorExpr.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/OperatorExpr.java
index b6bb55b..23d2179 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/OperatorExpr.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/OperatorExpr.java
@@ -2,12 +2,13 @@
 
 import java.util.ArrayList;
 
+import edu.uci.ics.asterix.aql.base.AbstractExpression;
 import edu.uci.ics.asterix.aql.base.Expression;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlVisitorWithVoidReturn;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
 
-public class OperatorExpr implements Expression {
+public class OperatorExpr extends AbstractExpression {
     private ArrayList<Expression> exprList;
     private ArrayList<OperatorType> opList;
     private ArrayList<Integer> exprBroadcastIdx;
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/Query.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/Query.java
index 6e851b0..92ad0ee 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/Query.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/Query.java
@@ -1,8 +1,5 @@
 package edu.uci.ics.asterix.aql.expression;
 
-import java.util.ArrayList;
-import java.util.List;
-
 import edu.uci.ics.asterix.aql.base.Expression;
 import edu.uci.ics.asterix.aql.base.Statement;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
@@ -10,59 +7,39 @@
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
 
 public class Query implements Statement {
-    private Expression body;
-    private List<Statement> prologDeclList = new ArrayList<Statement>();
-    private boolean isDummyQuery = false;
+	private Expression body;
+	private int varCounter;
 
-    public Query() {
-    }
+	public Expression getBody() {
+		return body;
+	}
 
-    public Query(boolean isDummyQuery) {
-        this.isDummyQuery = isDummyQuery;
-    }
+	public void setBody(Expression body) {
+		this.body = body;
+	}
 
-    public boolean isDummyQuery() {
-        return isDummyQuery;
-    }
+	public int getVarCounter() {
+		return varCounter;
+	}
 
-    public Expression getBody() {
-        return body;
-    }
+	public void setVarCounter(int varCounter) {
+		this.varCounter = varCounter;
+	}
 
-    public void setBody(Expression body) {
-        this.body = body;
-    }
+	@Override
+	public Kind getKind() {
+		return Kind.QUERY;
+	}
 
-    public void addPrologDecl(Statement stmt) {
-        this.prologDeclList.add(stmt);
-    }
+	@Override
+	public <T> void accept(IAqlVisitorWithVoidReturn<T> visitor, T step)
+			throws AsterixException {
+		visitor.visit(this, step);
+	}
 
-    public List<Statement> getPrologDeclList() {
-        return prologDeclList;
-    }
-
-    public void setPrologDeclList(List<Statement> prologDeclList) {
-        this.prologDeclList = prologDeclList;
-    }
-
-    // public void addFunctionDecl(FunctionDeclClass fc){
-    // if(functionDeclList == null){
-    // functionDeclList = new ArrayList<FunctionDeclClass>();
-    // }
-    // functionDeclList.add(fc);
-    // }
-    @Override
-    public Kind getKind() {
-        return Kind.QUERY;
-    }
-
-    @Override
-    public <T> void accept(IAqlVisitorWithVoidReturn<T> visitor, T step) throws AsterixException {
-        visitor.visit(this, step);
-    }
-
-    @Override
-    public <R, T> R accept(IAqlExpressionVisitor<R, T> visitor, T arg) throws AsterixException {
-        return visitor.visitQuery(this, arg);
-    }
+	@Override
+	public <R, T> R accept(IAqlExpressionVisitor<R, T> visitor, T arg)
+			throws AsterixException {
+		return visitor.visitQuery(this, arg);
+	}
 }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/TypeDecl.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/TypeDecl.java
index b7ccc8f..2e75ca7 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/TypeDecl.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/TypeDecl.java
@@ -8,26 +8,33 @@
 
 public class TypeDecl implements Statement {
 
+    private final Identifier dataverseName;
     private final Identifier ident;
     private final TypeExpression typeDef;
     private final TypeDataGen datagenAnnotation;
     private final boolean ifNotExists;
 
-    public TypeDecl(Identifier ident, TypeExpression typeDef, TypeDataGen datagen, boolean ifNotExists) {
+    public TypeDecl(Identifier dataverseName, Identifier ident, TypeExpression typeDef, TypeDataGen datagen,
+            boolean ifNotExists) {
+        this.dataverseName = dataverseName;
         this.ident = ident;
         this.typeDef = typeDef;
         this.datagenAnnotation = datagen;
         this.ifNotExists = ifNotExists;
     }
 
-    public TypeDecl(Identifier ident, TypeExpression typeDef) {
-        this(ident, typeDef, null, false);
+    public TypeDecl(Identifier dataverse, Identifier ident, TypeExpression typeDef) {
+        this(dataverse, ident, typeDef, null, false);
     }
 
     public Identifier getIdent() {
         return ident;
     }
 
+    public Identifier getDataverseName() {
+        return dataverseName;
+    }
+
     public TypeExpression getTypeDef() {
         return typeDef;
     }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/TypeDropStatement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/TypeDropStatement.java
index b50dbdd..4776b51 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/TypeDropStatement.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/TypeDropStatement.java
@@ -7,10 +7,12 @@
 
 public class TypeDropStatement implements Statement {
 
+    private final Identifier dataverseName;
     private Identifier typeName;
     private boolean ifExists;
 
-    public TypeDropStatement(Identifier typeName, boolean ifExists) {
+    public TypeDropStatement(Identifier dataverseName, Identifier typeName, boolean ifExists) {
+        this.dataverseName = dataverseName;
         this.typeName = typeName;
         this.ifExists = ifExists;
     }
@@ -20,6 +22,10 @@
         return Kind.TYPE_DROP;
     }
 
+    public Identifier getDataverseName() {
+        return dataverseName;
+    }
+
     public Identifier getTypeName() {
         return typeName;
     }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/TypeReferenceExpression.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/TypeReferenceExpression.java
index 88673fa..dfd4dc8 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/TypeReferenceExpression.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/TypeReferenceExpression.java
@@ -6,7 +6,7 @@
 
 public class TypeReferenceExpression extends TypeExpression {
 
-    private Identifier ident;
+    private final Identifier ident;
 
     public TypeReferenceExpression(Identifier ident) {
         this.ident = ident;
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/WriteFromQueryResultStatement.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/WriteFromQueryResultStatement.java
index b45f5da..10a9bd6 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/WriteFromQueryResultStatement.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/expression/WriteFromQueryResultStatement.java
@@ -7,12 +7,14 @@
 
 public class WriteFromQueryResultStatement implements Statement {
 
+    private Identifier dataverseName;
     private Identifier datasetName;
 
     private Query query;
     private int varCounter;
 
-    public WriteFromQueryResultStatement(Identifier datasetName, Query query, int varCounter) {
+    public WriteFromQueryResultStatement(Identifier dataverseName, Identifier datasetName, Query query, int varCounter) {
+        this.dataverseName = dataverseName;
         this.datasetName = datasetName;
         this.query = query;
         this.varCounter = varCounter;
@@ -23,6 +25,10 @@
         return Kind.WRITE_FROM_QUERY_RESULT;
     }
 
+    public Identifier getDataverseName() {
+        return dataverseName;
+    }
+
     public Identifier getDatasetName() {
         return datasetName;
     }
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 1c681b1..ab55e34 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
@@ -33,6 +33,7 @@
 import edu.uci.ics.asterix.aql.expression.IndexAccessor;
 import edu.uci.ics.asterix.aql.expression.IndexDropStatement;
 import edu.uci.ics.asterix.aql.expression.InsertStatement;
+import edu.uci.ics.asterix.aql.expression.InternalDetailsDecl;
 import edu.uci.ics.asterix.aql.expression.LetClause;
 import edu.uci.ics.asterix.aql.expression.LimitClause;
 import edu.uci.ics.asterix.aql.expression.ListConstructor;
@@ -67,7 +68,6 @@
 import edu.uci.ics.asterix.aql.expression.WriteStatement;
 import edu.uci.ics.asterix.common.config.DatasetConfig.DatasetType;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
-import edu.uci.ics.asterix.aql.expression.InternalDetailsDecl;
 
 public class AQLPrintVisitor implements IAqlVisitorWithVoidReturn<Integer> {
     // private int level =0;
@@ -90,9 +90,6 @@
 
     @Override
     public void visit(Query q, Integer step) throws AsterixException {
-        for (Statement d : q.getPrologDeclList()) {
-            d.accept(this, step);
-        }
         if (q.getBody() != null) {
             out.println("Query:");
             q.getBody().accept(this, step);
@@ -103,7 +100,7 @@
 
     @Override
     public void visit(LiteralExpr l, Integer step) {
-    	Literal lc = l.getValue();
+        Literal lc = l.getValue();
         if (lc.getLiteralType().equals(Literal.Type.TRUE) || lc.getLiteralType().equals(Literal.Type.FALSE)
                 || lc.getLiteralType().equals(Literal.Type.NULL)) {
             out.println(skip(step) + "LiteralExpr [" + l.getValue().getLiteralType() + "]");
@@ -148,7 +145,7 @@
 
     @Override
     public void visit(CallExpr pf, Integer step) throws AsterixException {
-        out.println(skip(step) + "FunctionCall " + pf.getIdent().toString() + "[");
+        out.println(skip(step) + "FunctionCall " + pf.getFunctionSignature().toString() + "[");
         for (Expression expr : pf.getExprList()) {
             expr.accept(this, step + 1);
         }
@@ -294,7 +291,7 @@
 
     @Override
     public void visit(FunctionDecl fd, Integer step) throws AsterixException {
-        out.println(skip(step) + "FunctionDecl " + fd.getIdent().getFunctionName() + "(" + fd.getParamList().toString()
+        out.println(skip(step) + "FunctionDecl " + fd.getSignature().getName() + "(" + fd.getParamList().toString()
                 + ") {");
         fd.getFuncBody().accept(this, step + 1);
         out.println(skip(step) + "}");
@@ -522,19 +519,19 @@
     @Override
     public void visit(CreateFunctionStatement cfs, Integer arg) throws AsterixException {
         // TODO Auto-generated method stub
-        
+
     }
 
     @Override
     public void visit(FunctionDropStatement fds, Integer arg) throws AsterixException {
         // TODO Auto-generated method stub
-        
+
     }
 
     @Override
     public void visit(BeginFeedStatement stmtDel, Integer arg) throws AsterixException {
         // TODO Auto-generated method stub
-        
+
     }
 
 }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/parser/ScopeChecker.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/parser/ScopeChecker.java
index 5171d5a..39edfda 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/parser/ScopeChecker.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/parser/ScopeChecker.java
@@ -4,7 +4,7 @@
 
 import edu.uci.ics.asterix.aql.context.Scope;
 import edu.uci.ics.asterix.aql.expression.Identifier;
-import edu.uci.ics.asterix.om.functions.AsterixFunction;
+import edu.uci.ics.asterix.common.functions.FunctionSignature;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.Counter;
 
 public abstract class ScopeChecker {
@@ -15,6 +15,14 @@
 
     protected Stack<Scope> forbiddenScopeStack = new Stack<Scope>();
 
+    protected String[] inputLines;
+
+    protected String defaultDataverse;
+
+    protected void setInput(String s) {
+        inputLines = s.split("\n");
+    }
+
     // Forbidden scopes are used to disallow, in a limit clause, variables
     // having the same name as a variable defined by the FLWOR in which that
     // limit clause appears.
@@ -95,9 +103,9 @@
      * 
      * @return functionDescriptor
      */
-    public final AsterixFunction lookupFunctionSignature(String name, int arity) {
-        if (name != null) {
-            return getCurrentScope().findFunctionSignature(name, arity);
+    public final FunctionSignature lookupFunctionSignature(String dataverse, String name, int arity) {
+        if (dataverse != null) {
+            return getCurrentScope().findFunctionSignature(dataverse, name, arity);
         } else {
             return null;
         }
@@ -137,4 +145,18 @@
         String stripped = s.substring(1, s.length() - 1);
         return stripped.replaceAll("\\\\" + q, "\\" + q);
     }
+
+    public String extractFragment(int beginLine, int beginColumn, int endLine, int endColumn) {
+        StringBuilder extract = new StringBuilder();
+        extract.append(inputLines[beginLine - 1].trim().length() > 1 ? inputLines[beginLine - 1].trim().substring(beginColumn)
+                : "");
+        for (int i = beginLine + 1; i < endLine; i++) {
+            extract.append("\n");
+            extract.append(inputLines[i - 1]);
+        }
+        extract.append("\n");
+        extract.append(inputLines[endLine - 1].substring(0, endColumn - 1));
+        return extract.toString().trim();
+    }
+
 }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/AqlRewriter.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/AqlRewriter.java
index 8f0a3ff..62e40fa 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/AqlRewriter.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/AqlRewriter.java
@@ -2,12 +2,13 @@
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import edu.uci.ics.asterix.aql.base.Clause;
 import edu.uci.ics.asterix.aql.base.Expression;
-import edu.uci.ics.asterix.aql.base.Statement;
 import edu.uci.ics.asterix.aql.base.Expression.Kind;
 import edu.uci.ics.asterix.aql.expression.BeginFeedStatement;
 import edu.uci.ics.asterix.aql.expression.CallExpr;
@@ -67,6 +68,7 @@
 import edu.uci.ics.asterix.aql.util.FunctionUtils;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
 import edu.uci.ics.asterix.common.functions.FunctionConstants;
+import edu.uci.ics.asterix.common.functions.FunctionSignature;
 import edu.uci.ics.asterix.metadata.MetadataManager;
 import edu.uci.ics.asterix.metadata.MetadataTransactionContext;
 import edu.uci.ics.asterix.metadata.entities.Function;
@@ -77,10 +79,10 @@
 
 public final class AqlRewriter {
 
-    private Query topExpr;
-    private AqlRewritingContext context;
-    private MetadataTransactionContext mdTxnCtx;
-    private String dataverseName;
+    private final Query topExpr;
+    private final List<FunctionDecl> declaredFunctions;
+    private final AqlRewritingContext context;
+    private final MetadataTransactionContext mdTxnCtx;
 
     private enum DfsColor {
         WHITE,
@@ -88,12 +90,11 @@
         BLACK
     }
 
-    public AqlRewriter(Query topExpr, int varCounter, MetadataTransactionContext txnContext, String dataverseName) {
+    public AqlRewriter(List<FunctionDecl> declaredFunctions, Query topExpr, MetadataTransactionContext mdTxnCtx) {
         this.topExpr = topExpr;
-        context = new AqlRewritingContext(varCounter);
-        mdTxnCtx = txnContext;
-        this.dataverseName = dataverseName;
-
+        context = new AqlRewritingContext(topExpr.getVarCounter());
+        this.declaredFunctions = declaredFunctions;
+        this.mdTxnCtx = mdTxnCtx;
     }
 
     public Query getExpr() {
@@ -131,130 +132,98 @@
         if (topExpr == null) {
             return;
         }
-        List<FunctionDecl> fdecls = buildFunctionDeclList(topExpr);
-        List<AsterixFunction> funIds = new ArrayList<AsterixFunction>();
-        for (FunctionDecl fdecl : fdecls) {
-            funIds.add(fdecl.getIdent());
+        List<FunctionSignature> funIds = new ArrayList<FunctionSignature>();
+        for (FunctionDecl fdecl : declaredFunctions) {
+            funIds.add(fdecl.getSignature());
         }
 
         List<FunctionDecl> otherFDecls = new ArrayList<FunctionDecl>();
         buildOtherUdfs(topExpr.getBody(), otherFDecls, funIds);
-        fdecls.addAll(otherFDecls);
-        if (!fdecls.isEmpty()) {
-            checkRecursivity(fdecls);
+        declaredFunctions.addAll(otherFDecls);
+        if (!declaredFunctions.isEmpty()) {
             InlineUdfsVisitor visitor = new InlineUdfsVisitor(context);
-            while (topExpr.accept(visitor, fdecls)) {
+            while (topExpr.accept(visitor, declaredFunctions)) {
                 // loop until no more changes
             }
         }
     }
 
     private void buildOtherUdfs(Expression expression, List<FunctionDecl> functionDecls,
-            List<AsterixFunction> declaredFunctions) throws AsterixException {
+            List<FunctionSignature> declaredFunctions) throws AsterixException {
         if (expression == null) {
             return;
         }
 
-        List<AsterixFunction> functionCalls = getFunctionCalls(expression);
-        for (AsterixFunction funId : functionCalls) {
-            if (AsterixBuiltinFunctions.isBuiltinCompilerFunction(new FunctionIdentifier(FunctionConstants.ASTERIX_NS,
-                    funId.getFunctionName()))) {
+        Set<FunctionSignature> functionCalls = getFunctionCalls(expression);
+        for (FunctionSignature signature : functionCalls) {
+
+            if (declaredFunctions != null && declaredFunctions.contains(signature)) {
                 continue;
             }
 
-            if (AsterixBuiltinFunctions.isBuiltinCompilerFunction(new FunctionIdentifier(
-                    AlgebricksBuiltinFunctions.ALGEBRICKS_NS, funId.getFunctionName()))) {
-                continue;
+            FunctionDecl functionDecl = lookupUserDefinedFunctionDecl(signature);
+            if (functionDecl != null) {
+                if (functionDecls.contains(functionDecl)) {
+                    throw new AsterixException(" Detected recursvity!");
+                } else {
+                    functionDecls.add(functionDecl);
+                    buildOtherUdfs(functionDecl.getFuncBody(), functionDecls, declaredFunctions);
+                }
+            } else {
+                if (isBuiltinFunction(signature)) {
+                    continue;
+                } else {
+                    throw new AsterixException(" unknown function " + signature);
+                }
             }
-
-            if (declaredFunctions != null && declaredFunctions.contains(funId)) {
-                continue;
-            }
-
-            FunctionDecl functionDecl = getFunctionDecl(funId);
-            if (functionDecls.contains(functionDecl)) {
-                throw new AsterixException(" Detected recursvity!");
-            }
-            functionDecls.add(functionDecl);
-            buildOtherUdfs(functionDecl.getFuncBody(), functionDecls, declaredFunctions);
         }
     }
 
-    private FunctionDecl getFunctionDecl(AsterixFunction funId) throws AsterixException {
-        Function function = MetadataManager.INSTANCE.getFunction(mdTxnCtx, dataverseName, funId.getFunctionName(),
-                funId.getArity());
+    private FunctionDecl lookupUserDefinedFunctionDecl(FunctionSignature signature) throws AsterixException {
+        if (signature.getNamespace() == null) {
+            return null;
+        }
+        Function function = MetadataManager.INSTANCE.getFunction(mdTxnCtx, signature);
         if (function == null) {
-            throw new AsterixException(" unknown function " + funId);
+            return null;
         }
         return FunctionUtils.getFunctionDecl(function);
 
     }
 
-    private List<AsterixFunction> getFunctionCalls(Expression expression) throws AsterixException {
+    private boolean isBuiltinFunction(FunctionSignature functionSignature) {
+        if (AsterixBuiltinFunctions.isBuiltinCompilerFunction(new FunctionIdentifier(FunctionConstants.ASTERIX_NS,
+                functionSignature.getName(), functionSignature.getArity()))) {
+            return true;
+        }
+
+        if (AsterixBuiltinFunctions.isBuiltinCompilerFunction(new FunctionIdentifier(
+                AlgebricksBuiltinFunctions.ALGEBRICKS_NS, functionSignature.getName(), functionSignature.getArity()))) {
+            return true;
+        }
+
+        return false;
+
+    }
+
+    private Set<FunctionSignature> getFunctionCalls(Expression expression) throws AsterixException {
         Map<AsterixFunction, DfsColor> color = new HashMap<AsterixFunction, DfsColor>();
         Map<AsterixFunction, List<AsterixFunction>> arcs = new HashMap<AsterixFunction, List<AsterixFunction>>();
         GatherFunctionCalls gfc = new GatherFunctionCalls();
         expression.accept(gfc, null);
-        List<AsterixFunction> calls = gfc.getCalls();
-        return calls;
-    }
-
-    private void checkRecursivity(List<FunctionDecl> fdecls) throws AsterixException {
-        Map<AsterixFunction, DfsColor> color = new HashMap<AsterixFunction, DfsColor>();
-        Map<AsterixFunction, List<AsterixFunction>> arcs = new HashMap<AsterixFunction, List<AsterixFunction>>();
-        for (FunctionDecl fd : fdecls) {
-            GatherFunctionCalls gfc = new GatherFunctionCalls();
-            fd.getFuncBody().accept(gfc, null);
-            List<AsterixFunction> calls = gfc.getCalls();
-            arcs.put(fd.getIdent(), calls);
-            color.put(fd.getIdent(), DfsColor.WHITE);
-        }
-        for (AsterixFunction a : arcs.keySet()) {
-            if (color.get(a) == DfsColor.WHITE) {
-                checkRecursivityDfs(a, arcs, color);
-            }
-        }
-    }
-
-    private void checkRecursivityDfs(AsterixFunction a, Map<AsterixFunction, List<AsterixFunction>> arcs,
-            Map<AsterixFunction, DfsColor> color) throws AsterixException {
-        color.put(a, DfsColor.GRAY);
-        List<AsterixFunction> next = arcs.get(a);
-        if (next != null) {
-            for (AsterixFunction f : next) {
-                DfsColor dc = color.get(f);
-                if (dc == DfsColor.GRAY) {
-                    throw new AsterixException("Recursive function calls, created by calling " + f + " starting from "
-                            + a);
-                }
-                if (dc == DfsColor.WHITE) {
-                    checkRecursivityDfs(f, arcs, color);
-                }
-            }
-        }
-        color.put(a, DfsColor.BLACK);
-    }
-
-    private List<FunctionDecl> buildFunctionDeclList(Query q) {
-        ArrayList<FunctionDecl> fdecls = new ArrayList<FunctionDecl>();
-        for (Statement s : q.getPrologDeclList()) {
-            if (s.getKind() == Statement.Kind.FUNCTION_DECL) {
-                fdecls.add((FunctionDecl) s);
-            }
-        }
-        return fdecls;
+        return gfc.getCalls();
     }
 
     private static class GatherFunctionCalls implements IAqlExpressionVisitor<Void, Void> {
 
-        private final List<AsterixFunction> calls = new ArrayList<AsterixFunction>();
+        private final Set<FunctionSignature> calls = new HashSet<FunctionSignature>();
 
         public GatherFunctionCalls() {
         }
 
         @Override
         public Void visitCallExpr(CallExpr pf, Void arg) throws AsterixException {
-            calls.add(pf.getIdent());
+            calls.add(pf.getFunctionSignature());
             for (Expression e : pf.getExprList()) {
                 e.accept(this, arg);
             }
@@ -532,7 +501,7 @@
             return null;
         }
 
-        public List<AsterixFunction> getCalls() {
+        public Set<FunctionSignature> getCalls() {
             return calls;
         }
 
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 53c051b..5742ac6 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
@@ -207,7 +207,7 @@
     public Pair<IAqlExpression, List<VariableSubstitution>> visitCallExpr(CallExpr pf, List<VariableSubstitution> arg)
             throws AsterixException {
         List<Expression> exprList = visitAndCloneExprList(pf.getExprList(), arg);
-        CallExpr f = new CallExpr(pf.getIdent(), exprList);
+        CallExpr f = new CallExpr(pf.getFunctionSignature(), exprList);
         return new Pair<IAqlExpression, List<VariableSubstitution>>(f, arg);
     }
 
@@ -224,7 +224,7 @@
         }
 
         Pair<IAqlExpression, List<VariableSubstitution>> p1 = fd.getFuncBody().accept(this, arg);
-        FunctionDecl newF = new FunctionDecl(fd.getIdent(), newList, (Expression) p1.first);
+        FunctionDecl newF = new FunctionDecl(fd.getSignature(), newList, (Expression) p1.first);
         return new Pair<IAqlExpression, List<VariableSubstitution>>(newF, arg);
     }
 
@@ -308,7 +308,6 @@
         Query newQ = new Query();
         Pair<IAqlExpression, List<VariableSubstitution>> p1 = q.getBody().accept(this, arg);
         newQ.setBody((Expression) p1.first);
-        newQ.setPrologDeclList(q.getPrologDeclList());
         return new Pair<IAqlExpression, List<VariableSubstitution>>(newQ, p1.second);
     }
 
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/InlineUdfsVisitor.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/InlineUdfsVisitor.java
index 3561539..f178d88 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/InlineUdfsVisitor.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/rewrites/InlineUdfsVisitor.java
@@ -64,7 +64,7 @@
 import edu.uci.ics.asterix.aql.expression.WriteStatement;
 import edu.uci.ics.asterix.aql.expression.visitor.IAqlExpressionVisitor;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
-import edu.uci.ics.asterix.om.functions.AsterixFunction;
+import edu.uci.ics.asterix.common.functions.FunctionSignature;
 import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
 
 public class InlineUdfsVisitor implements IAqlExpressionVisitor<Boolean, List<FunctionDecl>> {
@@ -104,12 +104,20 @@
     public Boolean visitRecordConstructor(RecordConstructor rc, List<FunctionDecl> arg) throws AsterixException {
         boolean changed = false;
         for (FieldBinding b : rc.getFbList()) {
-            if (b.getLeftExpr().accept(this, arg)) {
+        	Pair<Boolean, Expression> leftExprInlined = inlineUdfsInExpr(b.getLeftExpr(), arg);
+        	b.setLeftExpr(leftExprInlined.second);
+        	changed = changed | leftExprInlined.first;
+        	Pair<Boolean, Expression> rightExprInlined = inlineUdfsInExpr(b.getRightExpr(), arg);
+        	b.setRightExpr(rightExprInlined.second);
+        	changed = changed | rightExprInlined.first;
+        	
+        	/*
+        	if (b.getLeftExpr().accept(this, arg)) {
                 changed = true;
             }
             if (b.getRightExpr().accept(this, arg)) {
                 changed = true;
-            }
+            }*/
         }
         return changed;
     }
@@ -286,7 +294,7 @@
             return new Pair<Boolean, Expression>(r, expr);
         } else {
             CallExpr f = (CallExpr) expr;
-            FunctionDecl implem = findFuncDeclaration(f.getIdent(), arg);
+            FunctionDecl implem = findFuncDeclaration(f.getFunctionSignature(), arg);
             if (implem == null) {
                 boolean r = expr.accept(this, arg);
                 return new Pair<Boolean, Expression>(r, expr);
@@ -337,9 +345,9 @@
         return new Pair<Boolean, ArrayList<Expression>>(changed, newList);
     }
 
-    private static FunctionDecl findFuncDeclaration(AsterixFunction fid, List<FunctionDecl> sequence) {
+    private static FunctionDecl findFuncDeclaration(FunctionSignature fid, List<FunctionDecl> sequence) {
         for (FunctionDecl f : sequence) {
-            if (f.getIdent().equals(fid)) {
+            if (f.getSignature().equals(fid)) {
                 return f;
             }
         }
diff --git a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/util/FunctionUtils.java b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/util/FunctionUtils.java
index 1a30693..e3f3641 100644
--- a/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/util/FunctionUtils.java
+++ b/asterix-aql/src/main/java/edu/uci/ics/asterix/aql/util/FunctionUtils.java
@@ -1,5 +1,3 @@
-package edu.uci.ics.asterix.aql.util;
-
 /*
  * Copyright 2009-2011 by The Regents of the University of California
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,25 +12,21 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+package edu.uci.ics.asterix.aql.util;
+
 import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.List;
 
+import edu.uci.ics.asterix.aql.base.Statement;
 import edu.uci.ics.asterix.aql.expression.FunctionDecl;
-import edu.uci.ics.asterix.aql.expression.Query;
 import edu.uci.ics.asterix.aql.expression.VarIdentifier;
 import edu.uci.ics.asterix.aql.parser.AQLParser;
 import edu.uci.ics.asterix.aql.parser.ParseException;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
-import edu.uci.ics.asterix.common.functions.FunctionConstants;
-import edu.uci.ics.asterix.metadata.MetadataException;
-import edu.uci.ics.asterix.metadata.MetadataManager;
-import edu.uci.ics.asterix.metadata.MetadataTransactionContext;
 import edu.uci.ics.asterix.metadata.entities.Function;
 import edu.uci.ics.asterix.om.functions.AsterixBuiltinFunctions;
-import edu.uci.ics.asterix.om.functions.AsterixFunction;
-import edu.uci.ics.asterix.om.functions.AsterixFunctionInfo;
-import edu.uci.ics.hyracks.algebricks.core.algebra.functions.AlgebricksBuiltinFunctions;
 import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
 import edu.uci.ics.hyracks.algebricks.core.algebra.functions.IFunctionInfo;
 
@@ -44,7 +38,8 @@
         List<VarIdentifier> varIdentifiers = new ArrayList<VarIdentifier>();
 
         StringBuilder builder = new StringBuilder();
-        builder.append(" declare function " + function.getFunctionName());
+        builder.append(" use dataverse " + function.getDataverseName() + ";");
+        builder.append(" declare function " + function.getName().split("@")[0]);
         builder.append("(");
         for (String param : params) {
             VarIdentifier varId = new VarIdentifier(param);
@@ -52,21 +47,26 @@
             builder.append(param);
             builder.append(",");
         }
-        builder.delete(builder.length() - 1, builder.length());
+        if (params.size() > 0) {
+            builder.delete(builder.length() - 1, builder.length());
+        }
         builder.append(")");
         builder.append("{");
+        builder.append("\n");
         builder.append(functionBody);
+        builder.append("\n");
         builder.append("}");
+
         AQLParser parser = new AQLParser(new StringReader(new String(builder)));
 
-        Query query = null;
+        List<Statement> statements = null;
         try {
-            query = (Query) parser.Statement();
+            statements = parser.Statement();
         } catch (ParseException pe) {
             throw new AsterixException(pe);
         }
 
-        FunctionDecl decl = (FunctionDecl) query.getPrologDeclList().get(0);
+        FunctionDecl decl = (FunctionDecl) statements.get(1);
         return decl;
     }
 
@@ -74,22 +74,4 @@
         return AsterixBuiltinFunctions.getAsterixFunctionInfo(fi);
     }
 
-    public static IFunctionInfo getFunctionInfo(MetadataTransactionContext mdTxnCtx, String dataverseName,
-            AsterixFunction asterixFunction) throws MetadataException {
-        FunctionIdentifier fid = new FunctionIdentifier(FunctionConstants.ASTERIX_NS,
-                asterixFunction.getFunctionName(), asterixFunction.getArity());
-        IFunctionInfo finfo = AsterixBuiltinFunctions.getAsterixFunctionInfo(fid);
-        if (fid == null) {
-            fid = new FunctionIdentifier(AlgebricksBuiltinFunctions.ALGEBRICKS_NS, asterixFunction.getFunctionName(),
-                    asterixFunction.getArity());
-        }
-        if (fid == null) {
-            Function function = MetadataManager.INSTANCE.getFunction(mdTxnCtx, dataverseName,
-                    asterixFunction.getFunctionName(), asterixFunction.getArity());
-            if (function != null) {
-                finfo = new AsterixFunctionInfo(dataverseName, asterixFunction);
-            }
-        }
-        return finfo; // could be null
-    }
 }
diff --git a/asterix-aql/src/main/javacc/AQL.jj b/asterix-aql/src/main/javacc/AQL.jj
index 0767c4e..8672fd1 100644
--- a/asterix-aql/src/main/javacc/AQL.jj
+++ b/asterix-aql/src/main/javacc/AQL.jj
@@ -26,6 +26,7 @@
 import edu.uci.ics.asterix.aql.literal.NullLiteral;
 import edu.uci.ics.asterix.aql.literal.StringLiteral;
 import edu.uci.ics.asterix.aql.literal.TrueLiteral;
+import edu.uci.ics.asterix.metadata.bootstrap.MetadataConstants;
 
 import edu.uci.ics.asterix.aql.base.*;
 import edu.uci.ics.asterix.aql.expression.*;
@@ -39,26 +40,22 @@
 import edu.uci.ics.asterix.common.annotations.*;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
 import edu.uci.ics.asterix.om.functions.AsterixFunction;
+import edu.uci.ics.asterix.common.functions.FunctionSignature;
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IExpressionAnnotation;
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IndexedNLJoinExpressionAnnotation;
+import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
+import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
+import edu.uci.ics.hyracks.algebricks.common.utils.Triple;
+
+
 
 
 public class AQLParser extends ScopeChecker {
 
-/*
-    private void printHints(Token t) {
-       //System.err.println("token="+t.image+"\t special="+t.specialToken); 
-       if (t.specialToken == null) return;
-       Token tmp_t = t.specialToken;
-       while (tmp_t.specialToken != null) tmp_t = tmp_t.specialToken;    
-       while (tmp_t != null) {
-         System.out.println(tmp_t.image);
-         tmp_t = tmp_t.next;
-       }
-    }
-*/
-  
     // optimizer hints
     private static final String HASH_GROUP_BY_HINT = "hash";
     private static final String BROADCAST_JOIN_HINT = "bcast";
+    private static final String INDEXED_NESTED_LOOP_JOIN_HINT = "indexnl";
     private static final String INMEMORY_HINT = "inmem";
     private static final String VAL_FILE_HINT = "val-files";
     private static final String VAL_FILE_SAME_INDEX_HINT = "val-file-same-idx";
@@ -89,12 +86,17 @@
        return s.substring(1).trim();
     }
 
+    public AQLParser(String s){
+		this(new StringReader(s));
+		super.setInput(s);
+	}
+	
 	public static void main(String args[]) throws ParseException, TokenMgrError, IOException, FileNotFoundException, AsterixException {
 			File file = new File(args[0]);
 			Reader fis = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
 		    AQLParser parser = new AQLParser(fis);
-		    Statement st = parser.Statement();
-		    st.accept(new AQLPrintVisitor(), 0);
+		    List<Statement> st = parser.Statement();
+		    //st.accept(new AQLPrintVisitor(), 0);
 	}
 
 
@@ -103,11 +105,11 @@
 PARSER_END(AQLParser)
 
 
-Statement Statement() throws ParseException:
+List<Statement> Statement() throws ParseException:
 {
-  Query query = null;
   scopeStack.push(RootScopeFactory.createRootScope(this));
   List<Statement> decls = new ArrayList<Statement>();
+  Query query=null;
 }
 {
     (
@@ -187,6 +189,10 @@
           	{
             		decls.add(DataverseDropStatement());
           	}
+          	| "function"
+          	{
+            		decls.add(FunctionDropStatement());
+          	}
         	)
           | "write" {
                        decls.add(WriteStatement());
@@ -203,66 +209,78 @@
           | "update" {
 	  		decls.add(UpdateStatement());		
 	  	    } 
-	  	  | "begin" "feed"  <IDENTIFIER> {
-	  	    Identifier datasetName = new Identifier(token.image); 
-	  	    decls.add(new BeginFeedStatement(datasetName, getVarCounter()));
+	  	  | "begin" "feed"  
+	  	      {
+                Pair<Identifier,Identifier> nameComponents = getDotSeparatedPair();
+                decls.add(new BeginFeedStatement(nameComponents.first, nameComponents.second, getVarCounter()));
+	  	      } ";"
+	  	      
+	  	  | "suspend" "feed"  
+	  	  	 {
+                decls.add(ControlFeedDeclaration(ControlFeedStatement.OperationType.SUSPEND));
+             } ";"
+	  	   | "resume" "feed"   {
+                decls.add(ControlFeedDeclaration(ControlFeedStatement.OperationType.RESUME));
 	  	   } ";"
-	  	  | "suspend" "feed"  <IDENTIFIER> {
-	  	    datasetName = new Identifier(token.image); 
-	  	    decls.add(new ControlFeedStatement(ControlFeedStatement.OperationType.SUSPEND, datasetName));
-	  	   } ";"
-	  	   | "resume" "feed"  <IDENTIFIER> {
-	  	    datasetName = new Identifier(token.image); 
-	  	    decls.add(new ControlFeedStatement(ControlFeedStatement.OperationType.RESUME, datasetName));
-	  	   } ";"
-	  	   | "end" "feed"  <IDENTIFIER> {
-	  	    datasetName = new Identifier(token.image); 
-	  	    decls.add(new ControlFeedStatement(ControlFeedStatement.OperationType.END, datasetName));
+	  	   | "end" "feed"   {
+	  	        decls.add(ControlFeedDeclaration(ControlFeedStatement.OperationType.END));
 	  	   } ";" 
-	  	   | "alter" "feed" <IDENTIFIER> {
-             datasetName = new Identifier(token.image);
-             decls.add(AlterFeedDeclaration(datasetName));
-           }             
-                                                     
-        )*
-        (query = Query())?
+	  	   | "alter" "feed"  {
+      	        decls.add(AlterFeedDeclaration());
+           } ";"
+           
+           | (query = Query()) {
+               decls.add(query);
+           }
+           )*
+          //  (query = Query())?
       )
 
       <EOF>
     )
     {
-      if (query == null) {
-        query = new Query(true);
-      }
-      query.setPrologDeclList(decls);
-    
-      return query;
+     return decls;  
     }
 }
 
 InsertStatement InsertStatement() throws ParseException:
 {
+	Identifier dataverseName;
 	Identifier datasetName;
+	Pair<Identifier,Identifier> nameComponents = null;
 	Query query;
 }
 {
-   "into" <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
-     		<LEFTPAREN> query = Query() <RIGHTPAREN> ";"
-   {return new InsertStatement(datasetName, query,  getVarCounter());}
+   "into" <DATASET>
+   
+   {
+    nameComponents = getDotSeparatedPair();
+    dataverseName = nameComponents.first;
+    datasetName = nameComponents.second;
+   }
+    
+    <LEFTPAREN> query = Query() <RIGHTPAREN> ";"
+   {return new InsertStatement(dataverseName, datasetName, query,  getVarCounter());}
 }
 
 DeleteStatement DeleteStatement() throws ParseException:
 {
 	VariableExpr var = null;
+    Identifier dataverseName;
     Identifier datasetName = null;
 	Expression condition = null;
 	Clause dieClause = null;
+	Pair<Identifier, Identifier> nameComponents;
 }
 {
    var = Variable() { getCurrentScope().addNewVarSymbolToScope(var.getVar());  }
-	    "from" <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
-	    ("where" condition = Expression())?  (dieClause = DieClause())? ";"
-   {return new DeleteStatement(var, datasetName, condition,  dieClause, getVarCounter()); }
+   "from" 
+   <DATASET> 
+   { 
+	  nameComponents  = getDotSeparatedPair();
+   }
+   ("where" condition = Expression())?  (dieClause = DieClause())? ";"
+   {return new DeleteStatement(var, nameComponents.first, nameComponents.second, condition,  dieClause, getVarCounter()); }
 }
 
 UpdateStatement UpdateStatement() throws ParseException:
@@ -322,10 +340,10 @@
 {
   Identifier nodeName = null;
   String fileName = null;
-  Identifier datasetName = null;
   Statement stmt = null;
   Query query;
   String writerClass = null;
+  Pair<Identifier,Identifier> nameComponents = null;
 }
 {
   (( "output" "to" 
@@ -337,10 +355,15 @@
          } )
     |
    ( "into" 
-     <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
+     <DATASET> 
+     
+      {
+       nameComponents = getDotSeparatedPair();
+      }
+     
      <LEFTPAREN> query = Query() <RIGHTPAREN>
      {
-        stmt = new WriteFromQueryResultStatement(datasetName, query, getVarCounter());
+        stmt = new WriteFromQueryResultStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
      } ))  
         
     ";"
@@ -352,6 +375,7 @@
 CreateIndexStatement CreateIndexStatement() throws ParseException:
 {
   CreateIndexStatement cis = new CreateIndexStatement();
+  Pair<Identifier,Identifier> nameComponents = null;
 }
 {
   <IDENTIFIER> { cis.setIndexName(new Identifier(token.image)); }
@@ -362,7 +386,13 @@
     }
   )?
   "on"  
-  <IDENTIFIER> { cis.setDatasetName(new Identifier(token.image)); }
+  
+   {
+   nameComponents = getDotSeparatedPair();
+   cis.setDataverseName(nameComponents.first);
+   cis.setDatasetName(nameComponents.second);
+   }
+  
   <LEFTPAREN>
   	( <IDENTIFIER> { cis.addFieldExpr(token.image); } )
   	("," <IDENTIFIER> { cis.addFieldExpr(token.image); })*
@@ -394,23 +424,28 @@
   Identifier dvName = null;  
 }
 {
-  "dataverse" <IDENTIFIER> { dvName = new Identifier(token.image); }
+  "dataverse" <IDENTIFIER> { defaultDataverse = token.image;}
   ";"
   {
-    return new DataverseDecl(dvName);
+    return new DataverseDecl(new Identifier(defaultDataverse));
   }
 }
 
 DropStatement DropStatement() throws ParseException :
 {
+  Identifier dataverseName = null;
   Identifier datasetName = null;
   boolean ifExists = false;
+  Pair<Identifier,Identifier> nameComponents=null;
 }
 {
-  < IDENTIFIER >
-  {
-    datasetName = new Identifier(token.image);
-  }
+   {
+   nameComponents = getDotSeparatedPair();
+   dataverseName = nameComponents.first;
+   datasetName = nameComponents.second;
+   }
+   
+   
   (
     "if exists"
     {
@@ -418,25 +453,27 @@
     }
   )? ";"
   {
-    return new DropStatement(datasetName, ifExists);
+    return new DropStatement(dataverseName, datasetName, ifExists);
   }
 }
 
 IndexDropStatement IndexDropStatement() throws ParseException :
 {
+  Identifier dataverseName = null;
   Identifier datasetName = null;
   Identifier indexName = null;
   boolean ifExists = false;
+  Triple<Identifier,Identifier,Identifier> nameComponents=null;
 }
 {
-  < IDENTIFIER >
+  
   {
-    datasetName = new Identifier(token.image);
-  }
-  "." < IDENTIFIER >
-  {
-    indexName = new Identifier(token.image);
-  }
+   nameComponents = getDotSeparatedTriple();
+   dataverseName = nameComponents.first;
+   datasetName = nameComponents.second;
+   indexName = nameComponents.third;
+   }
+  
   (
     "if exists"
     {
@@ -444,7 +481,7 @@
     }
   )? ";"
   {
-    return new IndexDropStatement(datasetName, indexName, ifExists);
+    return new IndexDropStatement(dataverseName, datasetName, indexName, ifExists);
   }
 }
 
@@ -471,13 +508,16 @@
 
 TypeDropStatement TypeDropStatement() throws ParseException :
 {
+  Identifier dataverseName = null;
   Identifier typeName = null;
   boolean ifExists = false;
+  Pair<Identifier,Identifier> nameComponents;
 }
 {
-  < IDENTIFIER >
   {
-    typeName = new Identifier(token.image);
+    nameComponents = getDotSeparatedPair();
+    dataverseName = nameComponents.first == null ? new Identifier(defaultDataverse) : nameComponents.first;
+    typeName = nameComponents.second;
   }
   (
     "if exists"
@@ -486,7 +526,7 @@
     }
   )? ";"
   {
-    return new TypeDropStatement(typeName, ifExists);
+    return new TypeDropStatement(dataverseName, typeName, ifExists);
   }
 }
 
@@ -540,16 +580,61 @@
   }
 }
 
+
+FunctionDropStatement FunctionDropStatement() throws ParseException :
+{
+  String dataverse;
+  String functionName;
+  int arity=0;
+  boolean ifExists = false;
+  Pair<Identifier, Identifier> nameComponents=null;
+}
+{
+  {
+     nameComponents = getDotSeparatedPair();
+     dataverse = nameComponents.first != null ? nameComponents.first.getValue() : defaultDataverse;
+     functionName = nameComponents.second.getValue(); 
+  }
+  
+   "@"
+  <INTEGER_LITERAL> 
+  {  
+     Token t= getToken(0);
+	 arity = new Integer(t.image);
+  	 if( arity < 0 && arity != FunctionIdentifier.VARARGS){
+  	 	throw new ParseException(" invalid arity:" + arity);
+  	 } 
+  }
+  
+  (
+    "if exists"
+    {
+      ifExists = true;
+    }
+  )? ";"
+  {
+    return new FunctionDropStatement(new FunctionSignature(dataverse, functionName, arity), ifExists);
+  }
+}
+
+
 LoadFromFileStatement LoadStatement() throws ParseException:
 {
+  Identifier dataverseName = null;
   Identifier datasetName = null;
   boolean alreadySorted = false;
   String adapterClassname;
   Map<String,String> properties;
+  Pair<Identifier,Identifier> nameComponents = null;
 }
 {
-   <DATASET> <IDENTIFIER> { datasetName = new Identifier(token.image); }
- 
+   <DATASET> 
+   {
+   nameComponents = getDotSeparatedPair();
+   dataverseName = nameComponents.first;
+   datasetName = nameComponents.second;
+   }
+   
    "using"
 
     <STRING_LITERAL>
@@ -567,7 +652,7 @@
         
   ";"
   {
-     return new LoadFromFileStatement(datasetName, adapterClassname, properties, alreadySorted);
+     return new LoadFromFileStatement(dataverseName, datasetName, adapterClassname, properties, alreadySorted);
   }   
 }
 
@@ -577,15 +662,22 @@
 {
   DatasetDecl dd = null;
   Identifier datasetName = null;
+  Identifier dataverseName = null;
+  Identifier itemDataverseName = null;
   Identifier itemTypeName = null;
+  String nameComponentFirst = null;
+  String nameComponentSecond = null;
   boolean ifNotExists = false;
-  IDatasetDetailsDecl idd = null;
+  IDatasetDetailsDecl datasetDetails = null;
+  Pair<Identifier,Identifier> nameComponents = null;	
 }
 {
-  < IDENTIFIER >
   {
-    datasetName = new Identifier(token.image);
-  }
+   nameComponents = getDotSeparatedPair();
+   dataverseName = nameComponents.first;
+   datasetName = nameComponents.second;
+   }
+    
   (
     "if not exists"
     {
@@ -593,26 +685,24 @@
     }
   )?
   (
-  	< LEFTPAREN > < IDENTIFIER >
+  	< LEFTPAREN > <IDENTIFIER>
   	{
     	itemTypeName = new Identifier(token.image);
   	}
   	< RIGHTPAREN >
-  )?
+  )
   {
   	  if(datasetType == DatasetType.INTERNAL) {
-      	idd = InternalDatasetDeclaration();
-      	dd = new DatasetDecl(datasetName, itemTypeName, idd, ifNotExists);
+      	datasetDetails = InternalDatasetDeclaration();
       }
       else if(datasetType == DatasetType.EXTERNAL) {
-      	idd = ExternalDatasetDeclaration();
-      	dd = new DatasetDecl(datasetName, itemTypeName, idd,ifNotExists);
+      	datasetDetails = ExternalDatasetDeclaration();
       }
       else if(datasetType == DatasetType.FEED) {
-      	idd = FeedDatasetDeclaration();
-      	dd = new DatasetDecl(datasetName, itemTypeName, idd,ifNotExists);
+      	datasetDetails = FeedDatasetDeclaration();
       }
-      dd.setDatasetType(datasetType);
+      dd = new DatasetDecl(dataverseName, datasetName, itemTypeName, datasetType, datasetDetails,ifNotExists);
+      
   }
   {
     return dd;
@@ -622,30 +712,30 @@
 InternalDetailsDecl InternalDatasetDeclaration() throws ParseException :
 {
     InternalDetailsDecl idd = null;
+    List<String> partitioningExprs = new ArrayList<String>();
+    Identifier nodeGroupName=null;
 }
 {
-  {
-    idd = new InternalDetailsDecl();
-  }
   "partitioned" "by" "key"
   < IDENTIFIER >
   {
-    	 idd.addPartitioningExpr(token.image);
+         partitioningExprs.add(token.image); 
   }
   (
     "," < IDENTIFIER >
     {
-      	idd.addPartitioningExpr(token.image);
+         partitioningExprs.add(token.image); 
     }
   )*
   (
   "on" < IDENTIFIER >
     {
-    	idd.setNodegroupName(new Identifier(token.image));
+        nodeGroupName = new Identifier(token.image);
     }
   )?
   ";"
   {
+    idd = new InternalDetailsDecl(nodeGroupName, partitioningExprs);
     return idd;
   }
 }
@@ -687,19 +777,22 @@
 FeedDetailsDecl FeedDatasetDeclaration() throws ParseException :
 {
     FeedDetailsDecl fdd = null;
-    String adapterClassname = null;
+    String adapterFactoryClassname = null;
     Map < String, String > properties;
+	Pair<Identifier,Identifier> nameComponents;
+	List<String> partitioningExprs = new ArrayList<String>();
+    Identifier nodeGroupName=null;
+    FunctionSignature appliedFunction=null;
+	String dataverse;
+	String functionName;
+	int arity;
 }
 {
-  {
-    fdd = new FeedDetailsDecl();
-  }
-  
    "using"
    
     <STRING_LITERAL>
     {
-      adapterClassname = removeQuotesAndEscapes(token.image);
+      adapterFactoryClassname = removeQuotesAndEscapes(token.image);
     }
 
     {
@@ -707,51 +800,75 @@
     }
   
   ("apply" "function" 
-  < IDENTIFIER >
   {
-      fdd.setFunctionIdentifier(token.image);
+  nameComponents = getDotSeparatedPair();
+  dataverse = nameComponents.first != null ? nameComponents.first.getValue() : defaultDataverse;
+  functionName = nameComponents.second.getValue();
   }
+  ("@" <INTEGER_LITERAL> 
+     {
+        arity = Integer.parseInt(token.image);
+     }
+  )
+  
+  {
+    appliedFunction = new FunctionSignature(dataverse, functionName, arity);
+  }   
   )?
   
   "partitioned" "by" "key"
   < IDENTIFIER >
   {
-    	 fdd.addPartitioningExpr(token.image);
+    	 partitioningExprs.add(token.image);
   }
   (
     "," < IDENTIFIER >
     {
-      	fdd.addPartitioningExpr(token.image);
+    	 partitioningExprs.add(token.image);
     }
   )*
   (
   "on" < IDENTIFIER >
   {
-    	fdd.setNodegroupName(new Identifier(token.image));
+    	nodeGroupName = new Identifier(token.image);
   }
   )?
   ";"
   {
-    fdd.setAdapterClassname(adapterClassname);
-    fdd.setProperties(properties);
+    fdd = new FeedDetailsDecl(adapterFactoryClassname, properties, appliedFunction, nodeGroupName, partitioningExprs);
     return fdd;
   }
 }
 
-ControlFeedStatement AlterFeedDeclaration(Identifier datasetName) throws ParseException :
+ControlFeedStatement ControlFeedDeclaration(ControlFeedStatement.OperationType operationType) throws ParseException :
 {
-    String name = null;
-    String value = null;
+  Pair<Identifier,Identifier> nameComponents = null;
+}
+{
+    {
+    nameComponents = getDotSeparatedPair();
+    return new ControlFeedStatement(operationType, nameComponents.first, nameComponents.second);
+    }
+}
+
+
+ControlFeedStatement AlterFeedDeclaration() throws ParseException :
+{
+    Pair<Identifier,Identifier> nameComponents = null;
     Map < String, String > configuration = new HashMap < String, String > ();
 }
 {
+   {
+    nameComponents = getDotSeparatedPair();
+   }
+
    "set"
    { 
    configuration = getConfiguration();
    }
-  ";"
+  
   {
-    return new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, datasetName, configuration);
+    return new ControlFeedStatement(ControlFeedStatement.OperationType.ALTER, nameComponents.first, nameComponents.second, configuration);
   }
 }
 
@@ -843,15 +960,19 @@
 
 TypeDecl TypeDeclaration(boolean dgen, String hint) throws ParseException:
 {
+  Identifier dataverse;
   Identifier ident;
   TypeExpression typeExpr;
   boolean ifNotExists = false;
+  Pair<Identifier,Identifier> nameComponents=null;	
 }
 {
-  <IDENTIFIER>
   {
-    ident = new Identifier(token.image.toString());
+    nameComponents = getDotSeparatedPair();
+    dataverse = nameComponents.first;
+    ident = nameComponents.second; 
   }
+  
   (
     "if not exists"
     {
@@ -860,6 +981,7 @@
   )?
   "as"
   ( typeExpr = TypeExpr() )
+  (";")?
   {
     long numValues = -1;
     String filename = null;
@@ -872,7 +994,7 @@
       numValues = Long.parseLong(splits[2]);
     }  
     TypeDataGen tddg = new TypeDataGen(dgen, filename, numValues);
-    return new TypeDecl(ident, typeExpr, tddg, ifNotExists);
+    return new TypeDecl(dataverse, ident, typeExpr, tddg, ifNotExists);
   }
 }
 
@@ -998,12 +1120,12 @@
 TypeReferenceExpression TypeReference() throws ParseException:
 {}
 {
-  <IDENTIFIER>
-  	{
-	  Token t = getToken(0);
-	  Identifier id = new Identifier(t.toString());
-	  return new TypeReferenceExpression(id);
-	}      
+ <IDENTIFIER>
+ {
+     Token t = getToken(0);
+     Identifier id = new Identifier(t.toString());
+     return new TypeReferenceExpression(id);
+ }
 }
 
 OrderedListTypeDefinition OrderedListTypeDef() throws ParseException:
@@ -1033,11 +1155,72 @@
   }
 }
 
+Pair<Identifier,Identifier> getDotSeparatedPair() throws ParseException:
+{
+ Identifier first = null;
+ Identifier second = null;
+}
+{
+  < IDENTIFIER >
+  {
+    first = new Identifier(token.image);
+  } 
+  ("." <IDENTIFIER>
+  {
+    second = new Identifier(token.image);
+  }
+  )?
+  
+  {
+   if(second == null){
+   	second = first;
+   	first = null;
+   } 
+   
+   return new Pair<Identifier,Identifier>(first,second);
+  }  
+}  
+  
+Triple<Identifier,Identifier,Identifier> getDotSeparatedTriple() throws ParseException:
+{
+ Identifier first = null;
+ Identifier second = null;
+ Identifier third = null;
+}
+{
+  < IDENTIFIER >
+  {
+    first = new Identifier(token.image);
+  } 
+  "." <IDENTIFIER>
+  {
+    second = new Identifier(token.image);
+  }
+  (
+  "." <IDENTIFIER>
+  {
+    third = new Identifier(token.image);
+  }
+  )?
+  
+  {
+   if(third == null){
+   	third  = second;
+   	second = first;
+   	first = null;
+   } 
+   
+   return new Triple<Identifier,Identifier,Identifier>(first,second,third);
+  }  
+}  
+
+
+  
 
 FunctionDecl FunctionDeclaration() throws ParseException:
 {
-  FunctionDecl func = new FunctionDecl();
-  AsterixFunction ident;
+  FunctionDecl funcDecl;
+  FunctionSignature signature;
   String functionName;
   int arity = 0;
   List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
@@ -1070,33 +1253,34 @@
     })*)? <RIGHTPAREN> "{" funcBody = Expression() "}"
 
     {
-      ident = new AsterixFunction(functionName,arity);
-      getCurrentScope().addFunctionDescriptor(ident, false);
-      func.setIdent(ident);
-      func.setFuncBody(funcBody);
-      func.setParamList(paramList);
-      return func;
+      signature = new FunctionSignature(defaultDataverse, functionName, arity);
+      getCurrentScope().addFunctionDescriptor(signature, false);
+      funcDecl = new FunctionDecl(signature, paramList, funcBody);
+      return funcDecl;
     }
 }
 
 CreateFunctionStatement FunctionCreation() throws ParseException:
 {
   CreateFunctionStatement cfs = null;
-  AsterixFunction ident;
+  FunctionSignature signature;
+  String dataverse;
   String functionName;
-  int arity = 0;
   boolean ifNotExists = false;
   List<VarIdentifier> paramList = new ArrayList<VarIdentifier>();
-  String funcBody;
+  String functionBody;
   VarIdentifier var = null;
   createNewScope();
+  Expression functionBodyExpr;
+  Token beginPos;
+  Token endPos;
+  Pair<Identifier,Identifier> nameComponents=null;
 }
 {
-
-    <IDENTIFIER>
-	{
-	  Token t = getToken(0);
-	  functionName= t.toString();
+    {
+      nameComponents = getDotSeparatedPair();
+   	  dataverse = nameComponents.first != null ? nameComponents.first.getValue() : defaultDataverse;
+   	  functionName= nameComponents.second.getValue();
 	}
 	
 	(
@@ -1112,7 +1296,6 @@
       var.setValue(getToken(0).toString());
       paramList.add(var);
       getCurrentScope().addNewVarSymbolToScope(var);
-      arity++;
     }
     ("," <VARIABLE>
     {
@@ -1120,16 +1303,20 @@
       var.setValue(getToken(0).toString());
       paramList.add(var);
       getCurrentScope().addNewVarSymbolToScope(var);
-      arity++;
-    })*)? <RIGHTPAREN>  "{" <STRING_LITERAL>
+    })*)? <RIGHTPAREN>  "{"
           {
-            funcBody = removeQuotesAndEscapes(token.image);
-          }
+          beginPos = getToken(0);
+          } 
+          functionBodyExpr = Expression() 
           "}"
+          {
+            endPos = getToken(0);
+            functionBody = extractFragment(beginPos.beginLine, beginPos.beginColumn, endPos.beginLine, endPos.beginColumn);
+          }
     {
-      ident = new AsterixFunction(functionName, arity);
-      getCurrentScope().addFunctionDescriptor(ident, false);
-      cfs = new CreateFunctionStatement(ident, paramList, funcBody, ifNotExists);
+      signature = new FunctionSignature(dataverse, functionName, paramList.size());
+      getCurrentScope().addFunctionDescriptor(signature, false);
+      cfs = new CreateFunctionStatement(signature, paramList, functionBody, ifNotExists);
       return cfs;
     }
 }
@@ -1143,11 +1330,13 @@
 }
 {
     expr = Expression()
-
+    (";")?
     {
       query.setBody(expr);
+      query.setVarCounter(getVarCounter());
       return query;
     }
+     
 }
 
 
@@ -1165,7 +1354,7 @@
     | expr = IfThenElse()
     | expr = FLWOGR()
     | expr = QuantifiedExpression()
-
+   
 
 )
 	{
@@ -1246,14 +1435,15 @@
   OperatorExpr op = null;
   Expression operand = null;
   boolean broadcast = false;
+  IExpressionAnnotation annotation = null;
 }
 {
     operand = AddExpr()
-    {
-      if (operand instanceof VariableExpr) {
-        String hint = getHint(token);
+    { 
+      if (operand instanceof VariableExpr) {        
+        String hint = getHint(token);        
         if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
-          broadcast = true;       
+          broadcast = true;
         }
       }
     } 
@@ -1261,6 +1451,10 @@
     (
       LOOKAHEAD(2)( "<" | ">" | "<=" | ">=" | "=" | "!=" |"~=")
   	  {
+  	    String mhint = getHint(token);
+  	    if (mhint != null && mhint.equals(INDEXED_NESTED_LOOP_JOIN_HINT)) {
+          annotation = IndexedNLJoinExpressionAnnotation.INSTANCE;
+        }
   	    if (op == null) {
   	      op = new OperatorExpr();
   	      op.addOperand(operand, broadcast);
@@ -1273,18 +1467,21 @@
 	  
  	  operand = AddExpr()
 	  {
-         broadcast = false;
-	     if (operand instanceof VariableExpr) {
-           String hint = getHint(token);
+         broadcast = false;	     
+         if (operand instanceof VariableExpr) {
+           String hint = getHint(token);        
            if (hint != null && hint.equals(BROADCAST_JOIN_HINT)) {
              broadcast = true;
            }
-         } 
+         }
          op.addOperand(operand, broadcast);
       } 
     )?
  	
  	{
+ 	  if (annotation != null) {
+ 	    op.addHint(annotation);
+ 	  }
  	  return op==null? operand: op;
  	}
 }
@@ -1725,18 +1922,23 @@
     }
 }
 
+
 Expression FunctionCallExpr() throws ParseException:
 {
-  CallExpr pf = new CallExpr();
-  List<Expression > argList = new ArrayList<Expression >();
+  CallExpr callExpr;
+  List<Expression> argList = new ArrayList<Expression>();
   Expression tmp;
   int arity = 0;
-  Token funcName;
+  String funcName;
+  String dataverse;
+  String hint=null;
+  String id1=null;
+  String id2=null;
 }
-{
-    ( <IDENTIFIER> | <DATASET> )
+{  
+    ( <IDENTIFIER> { dataverse = defaultDataverse; funcName = token.image;} ("." <IDENTIFIER> { dataverse = funcName; funcName = token.image;})? | <DATASET> {dataverse = MetadataConstants.METADATA_DATAVERSE_NAME; funcName = getToken(0).toString();})
     {
-		funcName = getToken(0);
+       hint = getHint(token);
     }
      <LEFTPAREN> (tmp = Expression()
      {
@@ -1745,20 +1947,21 @@
      } ("," tmp = Expression() { argList.add(tmp); arity++; })*)? <RIGHTPAREN>
 
      {
-       AsterixFunction fd = lookupFunctionSignature(funcName.toString(), arity);
-	     if(fd == null)
-	     {
-	        fd = new AsterixFunction(funcName.toString(), arity);
-//	     	notFoundFunctionList.add(fd);
-	     }
-//	     	throw new ParseException("can't find function "+ funcName.toString() + "@" + arity);
-       pf.setIdent(fd);
-       pf.setExprList(argList);
-       return pf;
+       FunctionSignature signature = lookupFunctionSignature(dataverse, funcName.toString(), arity);
+             if(signature == null)
+             {
+                signature = new FunctionSignature(dataverse, funcName.toString(), arity);
+             }
+       callExpr = new CallExpr(signature,argList);
+       if (hint != null && hint.startsWith(INDEXED_NESTED_LOOP_JOIN_HINT)) {
+          callExpr.addHint(IndexedNLJoinExpressionAnnotation.INSTANCE);
+        }
+       return callExpr;
      }
 }
 
 
+
 Expression ParenthesizedExpression() throws ParseException:
 {
   Expression expr;
@@ -2214,6 +2417,7 @@
 	<IDENTIFIER : (<LETTER>)+ (<LETTER> | <DIGIT> | <SPECIALCHARS>)*>
 }
 
+
 <DEFAULT>
 TOKEN :
 {