You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@asterixdb.apache.org by dl...@apache.org on 2018/05/30 17:44:49 UTC

[22/24] asterixdb git commit: [ASTERIXDB-2393][COMP][RT] Add source location to error messages

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushFieldAccessRule.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushFieldAccessRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushFieldAccessRule.java
index 769894d..cf8935a 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushFieldAccessRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushFieldAccessRule.java
@@ -25,6 +25,8 @@ import java.util.List;
 
 import org.apache.asterix.algebra.base.OperatorAnnotation;
 import org.apache.asterix.common.config.DatasetConfig.DatasetType;
+import org.apache.asterix.common.exceptions.CompilationException;
+import org.apache.asterix.common.exceptions.ErrorCode;
 import org.apache.asterix.metadata.declared.DataSource;
 import org.apache.asterix.metadata.declared.DataSourceId;
 import org.apache.asterix.metadata.declared.MetadataProvider;
@@ -125,7 +127,8 @@ public class PushFieldAccessRule implements IAlgebraicRewriteRule {
 
         Dataset dataset = mp.findDataset(asid.getDataverseName(), asid.getDatasourceName());
         if (dataset == null) {
-            throw new AlgebricksException("Dataset " + asid.getDatasourceName() + " not found.");
+            throw new CompilationException(ErrorCode.COMPILATION_ERROR, scan.getSourceLocation(),
+                    "Dataset " + asid.getDatasourceName() + " not found.");
         }
         if (dataset.getDatasetType() != DatasetType.INTERNAL) {
             return false;
@@ -228,8 +231,10 @@ public class PushFieldAccessRule implements IAlgebraicRewriteRule {
                 for (Pair<LogicalVariable, LogicalVariable> m : varMappings) {
                     LogicalVariable v2 = context.newVar();
                     LogicalVariable oldVar = access.getVariables().get(0);
+                    VariableReferenceExpression v2Ref = new VariableReferenceExpression(v2);
+                    v2Ref.setSourceLocation(g.getSourceLocation());
                     g.getDecorList().add(new Pair<LogicalVariable, Mutable<ILogicalExpression>>(oldVar,
-                            new MutableObject<ILogicalExpression>(new VariableReferenceExpression(v2))));
+                            new MutableObject<ILogicalExpression>(v2Ref)));
                     changed = true;
                     access.getVariables().set(0, v2);
                     VariableUtilities.substituteVariables(access, m.first, m.second, context);
@@ -275,8 +280,9 @@ public class PushFieldAccessRule implements IAlgebraicRewriteRule {
                     }
                 }
             }
-            throw new AlgebricksException("Field access " + access.getExpressions().get(0).getValue()
-                    + " does not correspond to any input of operator " + op2);
+            throw new CompilationException(ErrorCode.COMPILATION_ERROR, access.getSourceLocation(),
+                    "Field access " + access.getExpressions().get(0).getValue()
+                            + " does not correspond to any input of operator " + op2);
         } else {
             // Check if the accessed field is not one of the partitioning key
             // fields. If yes, we can equate the two variables.
@@ -294,7 +300,8 @@ public class PushFieldAccessRule implements IAlgebraicRewriteRule {
                 MetadataProvider mp = (MetadataProvider) context.getMetadataProvider();
                 Dataset dataset = mp.findDataset(asid.getDataverseName(), asid.getDatasourceName());
                 if (dataset == null) {
-                    throw new AlgebricksException("Dataset " + asid.getDatasourceName() + " not found.");
+                    throw new CompilationException(ErrorCode.COMPILATION_ERROR, scan.getSourceLocation(),
+                            "Dataset " + asid.getDatasourceName() + " not found.");
                 }
                 if (dataset.getDatasetType() != DatasetType.INTERNAL) {
                     setAsFinal(access, context, finalAnnot);
@@ -319,7 +326,9 @@ public class PushFieldAccessRule implements IAlgebraicRewriteRule {
                             return false;
                         }
                         LogicalVariable keyVar = scan.getVariables().get(p);
-                        access.getExpressions().get(0).setValue(new VariableReferenceExpression(keyVar));
+                        VariableReferenceExpression keyVarRef = new VariableReferenceExpression(keyVar);
+                        keyVarRef.setSourceLocation(varRef.getSourceLocation());
+                        access.getExpressions().get(0).setValue(keyVarRef);
                         return true;
 
                     }
@@ -368,8 +377,11 @@ public class PushFieldAccessRule implements IAlgebraicRewriteRule {
             return false;
         }
         AssignOperator a2 = (AssignOperator) op2;
-        if (getFirstExpr(access).equals(getFirstExpr(a2))) {
-            access.getExpressions().get(0).setValue(new VariableReferenceExpression(a2.getVariables().get(0)));
+        ILogicalExpression accessExpr0 = getFirstExpr(access);
+        if (accessExpr0.equals(getFirstExpr(a2))) {
+            VariableReferenceExpression varRef = new VariableReferenceExpression(a2.getVariables().get(0));
+            varRef.setSourceLocation(accessExpr0.getSourceLocation());
+            access.getExpressions().get(0).setValue(varRef);
             return true;
         } else {
             return false;

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushLimitIntoOrderByRule.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushLimitIntoOrderByRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushLimitIntoOrderByRule.java
index 4c0247f..56dc675 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushLimitIntoOrderByRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushLimitIntoOrderByRule.java
@@ -133,6 +133,7 @@ public class PushLimitIntoOrderByRule implements IAlgebraicRewriteRule {
 
         // Create the new ORDER operator, set the topK value, and replace the current one.
         OrderOperator newOrderOp = new OrderOperator(orderOp.getOrderExpressions(), (int) topK);
+        newOrderOp.setSourceLocation(orderOp.getSourceLocation());
         newOrderOp.setPhysicalOperator(
                 new StableSortPOperator(physicalOptimizationConfig.getMaxFramesExternalSort(), newOrderOp.getTopK()));
         newOrderOp.getInputs().addAll(orderOp.getInputs());

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/RemoveLeftOuterUnnestForLeftOuterJoinRule.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/RemoveLeftOuterUnnestForLeftOuterJoinRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/RemoveLeftOuterUnnestForLeftOuterJoinRule.java
index 1d7a61a..364816b 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/RemoveLeftOuterUnnestForLeftOuterJoinRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/RemoveLeftOuterUnnestForLeftOuterJoinRule.java
@@ -262,13 +262,16 @@ public class RemoveLeftOuterUnnestForLeftOuterJoinRule implements IAlgebraicRewr
         List<LogicalVariable> lhs = new ArrayList<>();
         List<Mutable<ILogicalExpression>> rhs = new ArrayList<>();
         lhs.add(outerUnnest.getVariable());
-        rhs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(listifyVar)));
+        VariableReferenceExpression listifyVarRef = new VariableReferenceExpression(listifyVar);
+        listifyVarRef.setSourceLocation(gbyOperator.getSourceLocation());
+        rhs.add(new MutableObject<ILogicalExpression>(listifyVarRef));
         List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> gbyList = gbyOperator.getGroupByList();
         for (Pair<LogicalVariable, Mutable<ILogicalExpression>> gbyPair : gbyList) {
             lhs.add(gbyPair.first);
             rhs.add(gbyPair.second);
         }
         AssignOperator assignOp = new AssignOperator(lhs, rhs);
+        assignOp.setSourceLocation(outerUnnest.getSourceLocation());
         assignOp.getInputs().add(new MutableObject<ILogicalOperator>(lojOperator));
         context.computeAndSetTypeEnvironmentForOperator(assignOp);
         opRef.setValue(assignOp);

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/RemoveRedundantListifyRule.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/RemoveRedundantListifyRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/RemoveRedundantListifyRule.java
index 7b46b39..96b052b 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/RemoveRedundantListifyRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/RemoveRedundantListifyRule.java
@@ -205,8 +205,11 @@ public class RemoveRedundantListifyRule implements IAlgebraicRewriteRule {
         List<LogicalVariable> assgnVars = new ArrayList<>(1);
         assgnVars.add(unnest1.getVariable());
         List<Mutable<ILogicalExpression>> assgnExprs = new ArrayList<>(1);
-        assgnExprs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(paramVar)));
+        VariableReferenceExpression paramVarRef = new VariableReferenceExpression(paramVar);
+        paramVarRef.setSourceLocation(arg0.getSourceLocation());
+        assgnExprs.add(new MutableObject<ILogicalExpression>(paramVarRef));
         AssignOperator assign = new AssignOperator(assgnVars, assgnExprs);
+        assign.setSourceLocation(agg.getSourceLocation());
         assign.getInputs().add(agg.getInputs().get(0));
         context.computeAndSetTypeEnvironmentForOperator(assign);
         LogicalVariable posVar = unnest1.getPositionalVariable();
@@ -220,8 +223,10 @@ public class RemoveRedundantListifyRule implements IAlgebraicRewriteRule {
             List<Mutable<ILogicalExpression>> rAggExprs = new ArrayList<>(1);
             StatefulFunctionCallExpression tidFun = new StatefulFunctionCallExpression(
                     FunctionUtil.getFunctionInfo(BuiltinFunctions.TID), UnpartitionedPropertyComputer.INSTANCE);
+            tidFun.setSourceLocation(agg.getSourceLocation());
             rAggExprs.add(new MutableObject<ILogicalExpression>(tidFun));
             RunningAggregateOperator rAgg = new RunningAggregateOperator(raggVars, rAggExprs);
+            rAgg.setSourceLocation(agg.getSourceLocation());
             rAgg.getInputs().add(new MutableObject<ILogicalOperator>(assign));
             aggregateParentRef.setValue(rAgg);
             context.computeAndSetTypeEnvironmentForOperator(rAgg);
@@ -289,6 +294,7 @@ public class RemoveRedundantListifyRule implements IAlgebraicRewriteRule {
         List<LogicalVariable> assgnVars = new ArrayList<>(1);
         assgnVars.add(aggVar);
         AssignOperator assign = new AssignOperator(assgnVars, scanFunc.getArguments());
+        assign.setSourceLocation(agg.getSourceLocation());
         assign.getInputs().add(unnest.getInputs().get(0));
         context.computeAndSetTypeEnvironmentForOperator(assign);
         opRef.setValue(assign);

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetAsterixPhysicalOperatorsRule.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetAsterixPhysicalOperatorsRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetAsterixPhysicalOperatorsRule.java
index 4afccb0..fed17bd 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetAsterixPhysicalOperatorsRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetAsterixPhysicalOperatorsRule.java
@@ -25,6 +25,8 @@ import org.apache.asterix.algebra.operators.physical.BTreeSearchPOperator;
 import org.apache.asterix.algebra.operators.physical.InvertedIndexPOperator;
 import org.apache.asterix.algebra.operators.physical.RTreeSearchPOperator;
 import org.apache.asterix.common.config.DatasetConfig.IndexType;
+import org.apache.asterix.common.exceptions.CompilationException;
+import org.apache.asterix.common.exceptions.ErrorCode;
 import org.apache.asterix.metadata.declared.DataSourceId;
 import org.apache.asterix.metadata.declared.MetadataProvider;
 import org.apache.asterix.metadata.entities.Dataset;
@@ -131,6 +133,7 @@ public class SetAsterixPhysicalOperatorsRule implements IAlgebraicRewriteRule {
                                     AggregateFunctionCallExpression serialAggExpr =
                                             BuiltinFunctions.makeSerializableAggregateFunctionExpression(
                                                     expr.getFunctionIdentifier(), expr.getArguments());
+                                    serialAggExpr.setSourceLocation(expr.getSourceLocation());
                                     if (mergeAggregationExpressionFactory.createMergeAggregation(
                                             originalVariables.get(i), serialAggExpr, context) == null) {
                                         hasIntermediateAgg = false;
@@ -157,6 +160,7 @@ public class SetAsterixPhysicalOperatorsRule implements IAlgebraicRewriteRule {
                                         AggregateFunctionCallExpression serialAggExpr =
                                                 BuiltinFunctions.makeSerializableAggregateFunctionExpression(
                                                         expr.getFunctionIdentifier(), expr.getArguments());
+                                        serialAggExpr.setSourceLocation(expr.getSourceLocation());
                                         aggOp.getExpressions().get(i).setValue(serialAggExpr);
                                     }
                                     ExternalGroupByPOperator externalGby = new ExternalGroupByPOperator(
@@ -184,8 +188,7 @@ public class SetAsterixPhysicalOperatorsRule implements IAlgebraicRewriteRule {
                                         context.getPhysicalOptimizationConfig().getMaxFramesForGroupBy()));
                             }
                         }
-                    } else if (((AbstractLogicalOperator) (r0.getValue())).getOperatorTag()
-                            .equals(LogicalOperatorTag.RUNNINGAGGREGATE)) {
+                    } else if (r0.getValue().getOperatorTag().equals(LogicalOperatorTag.RUNNINGAGGREGATE)) {
                         List<Pair<LogicalVariable, Mutable<ILogicalExpression>>> gbyList = gby.getGroupByList();
                         List<LogicalVariable> columnList = new ArrayList<LogicalVariable>(gbyList.size());
                         for (Pair<LogicalVariable, Mutable<ILogicalExpression>> p : gbyList) {
@@ -198,8 +201,9 @@ public class SetAsterixPhysicalOperatorsRule implements IAlgebraicRewriteRule {
                         op.setPhysicalOperator(new PreclusteredGroupByPOperator(columnList, gby.isGroupAll(),
                                 context.getPhysicalOptimizationConfig().getMaxFramesForGroupBy()));
                     } else {
-                        throw new AlgebricksException("Unsupported nested operator within a group-by: "
-                                + ((AbstractLogicalOperator) (r0.getValue())).getOperatorTag().name());
+                        throw new CompilationException(ErrorCode.COMPILATION_ERROR, gby.getSourceLocation(),
+                                "Unsupported nested operator within a group-by: "
+                                        + r0.getValue().getOperatorTag().name());
                     }
                 }
             }
@@ -235,8 +239,9 @@ public class SetAsterixPhysicalOperatorsRule implements IAlgebraicRewriteRule {
                                 mp.findDataSourceIndex(jobGenParams.getIndexName(), dataSourceId);
                         INodeDomain storageDomain = mp.findNodeDomain(dataset.getNodeGroupName());
                         if (dsi == null) {
-                            throw new AlgebricksException("Could not find index " + jobGenParams.getIndexName()
-                                    + " for dataset " + dataSourceId);
+                            throw new CompilationException(ErrorCode.COMPILATION_ERROR, op.getSourceLocation(),
+                                    "Could not find index " + jobGenParams.getIndexName() + " for dataset "
+                                            + dataSourceId);
                         }
                         IndexType indexType = jobGenParams.getIndexType();
                         boolean requiresBroadcast = jobGenParams.getRequiresBroadcast();
@@ -288,13 +293,13 @@ public class SetAsterixPhysicalOperatorsRule implements IAlgebraicRewriteRule {
     private static void generateMergeAggregationExpressions(GroupByOperator gby, IOptimizationContext context)
             throws AlgebricksException {
         if (gby.getNestedPlans().size() != 1) {
-            throw new AlgebricksException(
+            throw new CompilationException(ErrorCode.COMPILATION_ERROR, gby.getSourceLocation(),
                     "External group-by currently works only for one nested plan with one root containing"
                             + "an aggregate and a nested-tuple-source.");
         }
         ILogicalPlan p0 = gby.getNestedPlans().get(0);
         if (p0.getRoots().size() != 1) {
-            throw new AlgebricksException(
+            throw new CompilationException(ErrorCode.COMPILATION_ERROR, gby.getSourceLocation(),
                     "External group-by currently works only for one nested plan with one root containing"
                             + "an aggregate and a nested-tuple-source.");
         }
@@ -303,8 +308,9 @@ public class SetAsterixPhysicalOperatorsRule implements IAlgebraicRewriteRule {
         Mutable<ILogicalOperator> r0 = p0.getRoots().get(0);
         AbstractLogicalOperator r0Logical = (AbstractLogicalOperator) r0.getValue();
         if (r0Logical.getOperatorTag() != LogicalOperatorTag.AGGREGATE) {
-            throw new AlgebricksException("The merge aggregation expression generation should not process a "
-                    + r0Logical.getOperatorTag() + " operator.");
+            throw new CompilationException(ErrorCode.COMPILATION_ERROR, gby.getSourceLocation(),
+                    "The merge aggregation expression generation should not process a " + r0Logical.getOperatorTag()
+                            + " operator.");
         }
         AggregateOperator aggOp = (AggregateOperator) r0.getValue();
         List<Mutable<ILogicalExpression>> aggFuncRefs = aggOp.getExpressions();
@@ -312,11 +318,13 @@ public class SetAsterixPhysicalOperatorsRule implements IAlgebraicRewriteRule {
         int n = aggOp.getExpressions().size();
         List<Mutable<ILogicalExpression>> mergeExpressionRefs = new ArrayList<Mutable<ILogicalExpression>>();
         for (int i = 0; i < n; i++) {
+            ILogicalExpression aggFuncExpr = aggFuncRefs.get(i).getValue();
             ILogicalExpression mergeExpr = mergeAggregationExpressionFactory
-                    .createMergeAggregation(aggProducedVars.get(i), aggFuncRefs.get(i).getValue(), context);
+                    .createMergeAggregation(aggProducedVars.get(i), aggFuncExpr, context);
             if (mergeExpr == null) {
-                throw new AlgebricksException("The aggregation function " + aggFuncRefs.get(i).getValue()
-                        + " does not have a registered intermediate aggregation function.");
+                throw new CompilationException(ErrorCode.COMPILATION_ERROR, aggFuncExpr.getSourceLocation(),
+                        "The aggregation function " + aggFuncExpr
+                                + " does not have a registered intermediate aggregation function.");
             }
             mergeExpressionRefs.add(new MutableObject<ILogicalExpression>(mergeExpr));
         }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetClosedRecordConstructorsRule.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetClosedRecordConstructorsRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetClosedRecordConstructorsRule.java
index 48a744d..52858e5 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetClosedRecordConstructorsRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetClosedRecordConstructorsRule.java
@@ -19,6 +19,8 @@
 package org.apache.asterix.optimizer.rules;
 
 import org.apache.asterix.common.config.GlobalConfig;
+import org.apache.asterix.common.exceptions.CompilationException;
+import org.apache.asterix.common.exceptions.ErrorCode;
 import org.apache.asterix.lang.common.util.FunctionUtil;
 import org.apache.asterix.om.functions.BuiltinFunctions;
 import org.apache.asterix.om.typecomputer.base.TypeCastUtils;
@@ -110,7 +112,7 @@ public class SetClosedRecordConstructorsRule implements IAlgebraicRewriteRule {
                 if (reqType == null || !reqType.isOpen()) {
                     int n = expr.getArguments().size();
                     if (n % 2 > 0) {
-                        throw new AlgebricksException(
+                        throw new CompilationException(ErrorCode.COMPILATION_ERROR, expr.getSourceLocation(),
                                 "Record constructor expected to have an even number of arguments: " + expr);
                     }
                     for (int i = 0; i < n / 2; i++) {
@@ -166,7 +168,7 @@ public class SetClosedRecordConstructorsRule implements IAlgebraicRewriteRule {
                 throws AlgebricksException {
             Object varType = env.getVarType(expr.getVariableReference());
             if (varType == null) {
-                throw new AlgebricksException(
+                throw new CompilationException(ErrorCode.COMPILATION_ERROR, expr.getSourceLocation(),
                         "Could not infer type for variable '" + expr.getVariableReference() + "'.");
             }
             return new ClosedDataInfo(false, TypeHelper.isClosed((IAType) varType), expr);

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetupCommitExtensionOpRule.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetupCommitExtensionOpRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetupCommitExtensionOpRule.java
index 90491d3..cc50dce 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetupCommitExtensionOpRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SetupCommitExtensionOpRule.java
@@ -104,6 +104,7 @@ public class SetupCommitExtensionOpRule implements IAlgebraicRewriteRule {
 
         //create ExtensionOperator and put the commitOperator in it.
         DelegateOperator extensionOperator = new DelegateOperator(commitOperator);
+        extensionOperator.setSourceLocation(eOp.getSourceLocation());
         extensionOperator.setPhysicalOperator(commitPOperator);
 
         //update plan link

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SimilarityCheckRule.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SimilarityCheckRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SimilarityCheckRule.java
index 0c3de91..d4061e0 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SimilarityCheckRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SimilarityCheckRule.java
@@ -50,6 +50,7 @@ import org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperat
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator;
 import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
+import org.apache.hyracks.api.exceptions.SourceLocation;
 
 /**
  * Looks for a select operator, containing a condition:
@@ -181,10 +182,12 @@ public class SimilarityCheckRule implements IAlgebraicRewriteRule {
 
         // Only non-null if we found that varRefExpr refers to an optimizable similarity function call.
         if (simCheckFuncExpr != null) {
+            SourceLocation sourceLoc = simCheckFuncExpr.getSourceLocation();
             // Create a new assign under matchingAssign which assigns the result of our similarity-check function to a variable.
             LogicalVariable newVar = context.newVar();
             AssignOperator newAssign =
                     new AssignOperator(newVar, new MutableObject<ILogicalExpression>(simCheckFuncExpr));
+            newAssign.setSourceLocation(sourceLoc);
             // Hook up inputs.
             newAssign.getInputs()
                     .add(new MutableObject<ILogicalOperator>(matchingAssign.getInputs().get(0).getValue()));
@@ -193,12 +196,15 @@ public class SimilarityCheckRule implements IAlgebraicRewriteRule {
             // Replace select condition with a get-item on newVarFromExpression.
             List<Mutable<ILogicalExpression>> selectGetItemArgs = new ArrayList<Mutable<ILogicalExpression>>();
             // First arg is a variable reference expr on newVarFromExpression.
-            selectGetItemArgs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(newVar)));
+            VariableReferenceExpression newVarRef1 = new VariableReferenceExpression(newVar);
+            newVarRef1.setSourceLocation(sourceLoc);
+            selectGetItemArgs.add(new MutableObject<ILogicalExpression>(newVarRef1));
             // Second arg is the item index to be accessed, here 0.
             selectGetItemArgs.add(new MutableObject<ILogicalExpression>(
                     new ConstantExpression(new AsterixConstantValue(new AInt32(0)))));
-            ILogicalExpression selectGetItemExpr = new ScalarFunctionCallExpression(
+            ScalarFunctionCallExpression selectGetItemExpr = new ScalarFunctionCallExpression(
                     FunctionUtil.getFunctionInfo(BuiltinFunctions.GET_ITEM), selectGetItemArgs);
+            selectGetItemExpr.setSourceLocation(sourceLoc);
             // Replace the old similarity function call with the new getItemExpr.
             expRef.setValue(selectGetItemExpr);
 
@@ -206,12 +212,15 @@ public class SimilarityCheckRule implements IAlgebraicRewriteRule {
             // newVarFromExpression.
             List<Mutable<ILogicalExpression>> assignGetItemArgs = new ArrayList<Mutable<ILogicalExpression>>();
             // First arg is a variable reference expr on newVarFromExpression.
-            assignGetItemArgs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(newVar)));
+            VariableReferenceExpression newVarRef2 = new VariableReferenceExpression(newVar);
+            newVarRef2.setSourceLocation(sourceLoc);
+            assignGetItemArgs.add(new MutableObject<ILogicalExpression>(newVarRef2));
             // Second arg is the item index to be accessed, here 1.
             assignGetItemArgs.add(new MutableObject<ILogicalExpression>(
                     new ConstantExpression(new AsterixConstantValue(new AInt32(1)))));
-            ILogicalExpression assignGetItemExpr = new ScalarFunctionCallExpression(
+            ScalarFunctionCallExpression assignGetItemExpr = new ScalarFunctionCallExpression(
                     FunctionUtil.getFunctionInfo(BuiltinFunctions.GET_ITEM), assignGetItemArgs);
+            assignGetItemExpr.setSourceLocation(sourceLoc);
             // Replace the original assign expr with the get-item expr.
             simFuncExprRef.setValue(assignGetItemExpr);
 
@@ -238,8 +247,9 @@ public class SimilarityCheckRule implements IAlgebraicRewriteRule {
             // Second arg is the item index to be accessed.
             getItemArgs.add(new MutableObject<ILogicalExpression>(
                     new ConstantExpression(new AsterixConstantValue(new AInt32(0)))));
-            ILogicalExpression getItemExpr = new ScalarFunctionCallExpression(
+            ScalarFunctionCallExpression getItemExpr = new ScalarFunctionCallExpression(
                     FunctionUtil.getFunctionInfo(BuiltinFunctions.GET_ITEM), getItemArgs);
+            getItemExpr.setSourceLocation(simCheckFuncExpr.getSourceLocation());
             // Replace the old similarity function call with the new getItemExpr.
             expRef.setValue(getItemExpr);
             return true;
@@ -282,6 +292,7 @@ public class SimilarityCheckRule implements IAlgebraicRewriteRule {
                     new ConstantExpression(new AsterixConstantValue(jaccThresh))));
             simCheckFuncExpr = new ScalarFunctionCallExpression(
                     FunctionUtil.getFunctionInfo(BuiltinFunctions.SIMILARITY_JACCARD_CHECK), similarityArgs);
+            simCheckFuncExpr.setSourceLocation(funcExpr.getSourceLocation());
         }
 
         // Look for edit-distance function call, and LE or LT.
@@ -310,6 +321,7 @@ public class SimilarityCheckRule implements IAlgebraicRewriteRule {
                     new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(edThresh))));
             simCheckFuncExpr = new ScalarFunctionCallExpression(
                     FunctionUtil.getFunctionInfo(BuiltinFunctions.EDIT_DISTANCE_CHECK), similarityArgs);
+            simCheckFuncExpr.setSourceLocation(funcExpr.getSourceLocation());
         }
         // Preserve all annotations.
         if (simCheckFuncExpr != null) {

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SweepIllegalNonfunctionalFunctions.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SweepIllegalNonfunctionalFunctions.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SweepIllegalNonfunctionalFunctions.java
index b1f646a..8372851 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SweepIllegalNonfunctionalFunctions.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SweepIllegalNonfunctionalFunctions.java
@@ -20,6 +20,8 @@ package org.apache.asterix.optimizer.rules;
 
 import java.util.List;
 
+import org.apache.asterix.common.exceptions.CompilationException;
+import org.apache.asterix.common.exceptions.ErrorCode;
 import org.apache.commons.lang3.mutable.Mutable;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
 import org.apache.hyracks.algebricks.common.utils.Pair;
@@ -102,7 +104,7 @@ public class SweepIllegalNonfunctionalFunctions extends AbstractExtractExprRule
             if (expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                 if (!expr.isFunctional()) {
                     AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) expr;
-                    throw new AlgebricksException(
+                    throw new CompilationException(ErrorCode.COMPILATION_ERROR, fce.getSourceLocation(),
                             "Found non-functional function " + fce.getFunctionIdentifier() + " in op " + op);
                 }
             }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
index 1a4f1c0..c8bec85 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
@@ -625,7 +625,9 @@ public abstract class AbstractIntroduceAccessMethodRule implements IAlgebraicRew
         optFuncExpr.setOptimizableSubTree(funcVarIndex, subTree);
         List<String> fieldName = null;
         if (subTree.getDataSourceType() == DataSourceType.COLLECTION_SCAN) {
-            optFuncExpr.setLogicalExpr(funcVarIndex, new VariableReferenceExpression(var));
+            VariableReferenceExpression varRef = new VariableReferenceExpression(var);
+            varRef.setSourceLocation(unnestOp.getSourceLocation());
+            optFuncExpr.setLogicalExpr(funcVarIndex, varRef);
         } else {
             fieldName = getFieldNameFromSubTree(optFuncExpr, subTree, assignOrUnnestIndex, 0, subTree.getRecordType(),
                     funcVarIndex, optFuncExpr.getFuncExpr().getArguments().get(funcVarIndex).getValue(),
@@ -731,7 +733,9 @@ public abstract class AbstractIntroduceAccessMethodRule implements IAlgebraicRew
             optFuncExpr.setFieldName(funcVarIndex, fieldName);
             optFuncExpr.setOptimizableSubTree(funcVarIndex, subTree);
             optFuncExpr.setSourceVar(funcVarIndex, var);
-            optFuncExpr.setLogicalExpr(funcVarIndex, new VariableReferenceExpression(var));
+            VariableReferenceExpression varRef = new VariableReferenceExpression(var);
+            varRef.setSourceLocation(subTree.getDataSourceRef().getValue().getSourceLocation());
+            optFuncExpr.setLogicalExpr(funcVarIndex, varRef);
             setTypeTag(context, subTree, optFuncExpr, funcVarIndex);
             if (subTree.hasDataSourceScan()) {
                 fillIndexExprs(datasetIndexes, fieldName, fieldType, optFuncExpr, optFuncExprIndex, funcVarIndex,

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodJobGenParams.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodJobGenParams.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodJobGenParams.java
index e541a79..cba4411 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodJobGenParams.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodJobGenParams.java
@@ -107,8 +107,8 @@ public class AccessMethodJobGenParams {
                 new MutableObject<>(new ConstantExpression(new AsterixConstantValue(new AInt32(varList.size()))));
         funcArgs.add(numKeysRef);
         for (LogicalVariable keyVar : varList) {
-            Mutable<ILogicalExpression> keyVarRef = new MutableObject<>(new VariableReferenceExpression(keyVar));
-            funcArgs.add(keyVarRef);
+            VariableReferenceExpression keyVarRef = new VariableReferenceExpression(keyVar);
+            funcArgs.add(new MutableObject<>(keyVarRef));
         }
     }
 

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodUtils.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodUtils.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodUtils.java
index eafbdaf..6368058 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodUtils.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodUtils.java
@@ -95,6 +95,7 @@ import org.apache.hyracks.algebricks.core.algebra.operators.logical.visitors.Var
 import org.apache.hyracks.algebricks.core.algebra.plan.ALogicalPlanImpl;
 import org.apache.hyracks.algebricks.core.algebra.util.OperatorPropertiesUtil;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
+import org.apache.hyracks.api.exceptions.SourceLocation;
 import org.apache.hyracks.storage.am.lsm.invertedindex.tokenizers.DelimitedUTF8StringBinaryTokenizer;
 
 /**
@@ -274,7 +275,8 @@ public class AccessMethodUtils {
                 break;
             default:
                 throw new CompilationException(ErrorCode.COMPILATION_TYPE_UNSUPPORTED,
-                        BuiltinFunctions.FULLTEXT_CONTAINS.getName(), objectFromExpr.getType().getTypeTag());
+                        constExpression.getSourceLocation(), BuiltinFunctions.FULLTEXT_CONTAINS.getName(),
+                        objectFromExpr.getType().getTypeTag());
         }
     }
 
@@ -421,7 +423,8 @@ public class AccessMethodUtils {
             case CONDITIONAL_SPLIT_VAR:
                 // Sanity check - the given unnest map should generate this variable.
                 if (!abstractUnnestMapOp.getGenerateCallBackProceedResultVar()) {
-                    throw CompilationException.create(ErrorCode.CANNOT_GET_CONDITIONAL_SPLIT_KEY_VARIABLE);
+                    throw CompilationException.create(ErrorCode.CANNOT_GET_CONDITIONAL_SPLIT_KEY_VARIABLE,
+                            unnestMapOp.getSourceLocation());
                 }
                 // Fetches conditional splitter - the last position
                 start = numSecondaryKeys + numPrimaryKeys;
@@ -465,6 +468,7 @@ public class AccessMethodUtils {
     public static Triple<ILogicalExpression, ILogicalExpression, Boolean> createSearchKeyExpr(Index index,
             IOptimizableFuncExpr optFuncExpr, IAType indexedFieldType, OptimizableOperatorSubTree probeSubTree)
             throws AlgebricksException {
+        SourceLocation sourceLoc = optFuncExpr.getFuncExpr().getSourceLocation();
         if (probeSubTree == null) {
             // We are optimizing a selection query. Search key is a constant.
             // Type Checking and type promotion is done here
@@ -473,7 +477,9 @@ public class AccessMethodUtils {
                 //We are looking at a selection case, but using two variables
                 //This means that the second variable comes from a nonPure function call
                 //TODO: Right now we miss on type promotion for nonpure functions
-                return new Triple<>(new VariableReferenceExpression(optFuncExpr.getLogicalVar(1)), null, false);
+                VariableReferenceExpression varRef = new VariableReferenceExpression(optFuncExpr.getLogicalVar(1));
+                varRef.setSourceLocation(sourceLoc);
+                return new Triple<>(varRef, null, false);
             }
 
             ILogicalExpression constantAtRuntimeExpression = optFuncExpr.getConstantExpr(0);
@@ -539,25 +545,25 @@ public class AccessMethodUtils {
                         case LT:
                         case GE:
                             // round-up
-                            replacedConstantValue =
-                                    getReplacedConstantValue(constantValue.getObject(), constantValueTag,
-                                            indexedFieldTypeTag, index.isEnforced(), TypeCastingMathFunctionType.CEIL);
+                            replacedConstantValue = getReplacedConstantValue(constantValue.getObject(),
+                                    constantValueTag, indexedFieldTypeTag, index.isEnforced(),
+                                    TypeCastingMathFunctionType.CEIL, sourceLoc);
                             break;
                         case LE:
                         case GT:
                             // round-down
-                            replacedConstantValue =
-                                    getReplacedConstantValue(constantValue.getObject(), constantValueTag,
-                                            indexedFieldTypeTag, index.isEnforced(), TypeCastingMathFunctionType.FLOOR);
+                            replacedConstantValue = getReplacedConstantValue(constantValue.getObject(),
+                                    constantValueTag, indexedFieldTypeTag, index.isEnforced(),
+                                    TypeCastingMathFunctionType.FLOOR, sourceLoc);
                             break;
                         case EQ:
                             // equality case - both CEIL and FLOOR need to be applied.
-                            replacedConstantValue =
-                                    getReplacedConstantValue(constantValue.getObject(), constantValueTag,
-                                            indexedFieldTypeTag, index.isEnforced(), TypeCastingMathFunctionType.FLOOR);
-                            replacedConstantValueForEQCase =
-                                    getReplacedConstantValue(constantValue.getObject(), constantValueTag,
-                                            indexedFieldTypeTag, index.isEnforced(), TypeCastingMathFunctionType.CEIL);
+                            replacedConstantValue = getReplacedConstantValue(constantValue.getObject(),
+                                    constantValueTag, indexedFieldTypeTag, index.isEnforced(),
+                                    TypeCastingMathFunctionType.FLOOR, sourceLoc);
+                            replacedConstantValueForEQCase = getReplacedConstantValue(constantValue.getObject(),
+                                    constantValueTag, indexedFieldTypeTag, index.isEnforced(),
+                                    TypeCastingMathFunctionType.CEIL, sourceLoc);
                             break;
                         default:
                             // NEQ should not be a case.
@@ -566,7 +572,7 @@ public class AccessMethodUtils {
                 } else {
                     // Type conversion only case: (e.g., INT -> BIGINT)
                     replacedConstantValue = getReplacedConstantValue(constantValue.getObject(), constantValueTag,
-                            indexedFieldTypeTag, index.isEnforced(), TypeCastingMathFunctionType.NONE);
+                            indexedFieldTypeTag, index.isEnforced(), TypeCastingMathFunctionType.NONE, sourceLoc);
                 }
             }
             // No type-casting at all
@@ -586,7 +592,8 @@ public class AccessMethodUtils {
             OptimizableOperatorSubTree opSubTree0 = optFuncExpr.getOperatorSubTree(0);
             int probeVarIndex = opSubTree0 == null || opSubTree0 == probeSubTree ? 0 : 1;
             LogicalVariable probeVar = optFuncExpr.getLogicalVar(probeVarIndex);
-            ILogicalExpression probeExpr = new VariableReferenceExpression(probeVar);
+            VariableReferenceExpression probeExpr = new VariableReferenceExpression(probeVar);
+            probeExpr.setSourceLocation(sourceLoc);
 
             ATypeTag indexedFieldTypeTag = TypeComputeUtils.getActualType(indexedFieldType).getTypeTag();
             if (ATypeHierarchy.getTypeDomain(indexedFieldTypeTag) == ATypeHierarchy.Domain.NUMERIC) {
@@ -595,6 +602,7 @@ public class AccessMethodUtils {
                 if (probeTypeTypeTag != indexedFieldTypeTag) {
                     ScalarFunctionCallExpression castFunc = new ScalarFunctionCallExpression(
                             FunctionUtil.getFunctionInfo(BuiltinFunctions.CAST_TYPE_LAX));
+                    castFunc.setSourceLocation(sourceLoc);
                     castFunc.getArguments().add(new MutableObject<>(probeExpr));
                     TypeCastUtils.setRequiredAndInputTypes(castFunc, indexedFieldType, probeType);
                     boolean realTypeConvertedToIntegerType =
@@ -607,14 +615,14 @@ public class AccessMethodUtils {
     }
 
     private static AsterixConstantValue getReplacedConstantValue(IAObject sourceObject, ATypeTag sourceTypeTag,
-            ATypeTag targetTypeTag, boolean strictDemote, TypeCastingMathFunctionType mathFunction)
-            throws CompilationException {
+            ATypeTag targetTypeTag, boolean strictDemote, TypeCastingMathFunctionType mathFunction,
+            SourceLocation sourceLoc) throws CompilationException {
         try {
             return ATypeHierarchy.getAsterixConstantValueFromNumericTypeObject(sourceObject, targetTypeTag,
                     strictDemote, mathFunction);
         } catch (HyracksDataException e) {
-            throw new CompilationException(ErrorCode.ERROR_OCCURRED_BETWEEN_TWO_TYPES_CONVERSION, e, sourceTypeTag,
-                    targetTypeTag);
+            throw new CompilationException(ErrorCode.ERROR_OCCURRED_BETWEEN_TWO_TYPES_CONVERSION, e, sourceLoc,
+                    sourceTypeTag, targetTypeTag);
         }
     }
 
@@ -749,6 +757,7 @@ public class AccessMethodUtils {
                 // Non-index only plan case
                 indexSubTree.getDataSourceRef().setValue(finalIndexSearchOp);
                 SelectOperator topSelectOp = new SelectOperator(conditionRef, isLeftOuterJoin, newNullPlaceHolderVar);
+                topSelectOp.setSourceLocation(finalIndexSearchOp.getSourceLocation());
                 topSelectOp.getInputs().add(indexSubTree.getRootRef());
                 topSelectOp.setExecutionMode(ExecutionMode.LOCAL);
                 context.computeAndSetTypeEnvironmentForOperator(topSelectOp);
@@ -768,6 +777,7 @@ public class AccessMethodUtils {
             ARecordType metaRecordType, Index index, ILogicalOperator inputOp, AccessMethodJobGenParams jobGenParams,
             IOptimizationContext context, boolean retainInput, boolean retainNull,
             boolean generateInstantTrylockResultFromIndexSearch) throws AlgebricksException {
+        SourceLocation sourceLoc = inputOp.getSourceLocation();
         // The job gen parameters are transferred to the actual job gen via the UnnestMapOperator's function arguments.
         ArrayList<Mutable<ILogicalExpression>> secondaryIndexFuncArgs = new ArrayList<>();
         jobGenParams.writeToFuncArgs(secondaryIndexFuncArgs);
@@ -784,6 +794,7 @@ public class AccessMethodUtils {
         IFunctionInfo secondaryIndexSearch = FunctionUtil.getFunctionInfo(BuiltinFunctions.INDEX_SEARCH);
         UnnestingFunctionCallExpression secondaryIndexSearchFunc =
                 new UnnestingFunctionCallExpression(secondaryIndexSearch, secondaryIndexFuncArgs);
+        secondaryIndexSearchFunc.setSourceLocation(sourceLoc);
         secondaryIndexSearchFunc.setReturnsUniqueValues(true);
         // This is the operator that jobgen will be looking for. It contains an unnest function that has all
         // necessary arguments to determine which index to use, which variables contain the index-search keys,
@@ -796,6 +807,7 @@ public class AccessMethodUtils {
                 LeftOuterUnnestMapOperator secondaryIndexLeftOuterUnnestOp = new LeftOuterUnnestMapOperator(
                         secondaryIndexUnnestVars, new MutableObject<ILogicalExpression>(secondaryIndexSearchFunc),
                         secondaryIndexOutputTypes, true);
+                secondaryIndexLeftOuterUnnestOp.setSourceLocation(sourceLoc);
                 secondaryIndexLeftOuterUnnestOp
                         .setGenerateCallBackProceedResultVar(generateInstantTrylockResultFromIndexSearch);
                 secondaryIndexLeftOuterUnnestOp.getInputs().add(new MutableObject<>(inputOp));
@@ -804,13 +816,15 @@ public class AccessMethodUtils {
                 return secondaryIndexLeftOuterUnnestOp;
             } else {
                 // Left-outer-join without retainInput doesn't make sense.
-                throw new AlgebricksException("Left-outer-join should propagate all inputs from the outer branch.");
+                throw new CompilationException(ErrorCode.COMPILATION_ERROR, sourceLoc,
+                        "Left-outer-join should propagate all inputs from the outer branch.");
             }
         } else {
             // If this is not a left-outer-join case, then we use UNNEST-MAP operator.
             UnnestMapOperator secondaryIndexUnnestOp = new UnnestMapOperator(secondaryIndexUnnestVars,
                     new MutableObject<ILogicalExpression>(secondaryIndexSearchFunc), secondaryIndexOutputTypes,
                     retainInput);
+            secondaryIndexUnnestOp.setSourceLocation(sourceLoc);
             secondaryIndexUnnestOp.setGenerateCallBackProceedResultVar(generateInstantTrylockResultFromIndexSearch);
             secondaryIndexUnnestOp.getInputs().add(new MutableObject<>(inputOp));
             context.computeAndSetTypeEnvironmentForOperator(secondaryIndexUnnestOp);
@@ -824,12 +838,16 @@ public class AccessMethodUtils {
             boolean retainMissing, boolean requiresBroadcast, List<LogicalVariable> primaryKeyVars,
             List<LogicalVariable> primaryIndexUnnestVars, List<Object> primaryIndexOutputTypes)
             throws AlgebricksException {
+        SourceLocation sourceLoc = inputOp.getSourceLocation();
         // Optionally add a sort on the primary-index keys before searching the primary index.
         OrderOperator order = null;
         if (sortPrimaryKeys) {
             order = new OrderOperator();
+            order.setSourceLocation(sourceLoc);
             for (LogicalVariable pkVar : primaryKeyVars) {
-                Mutable<ILogicalExpression> vRef = new MutableObject<>(new VariableReferenceExpression(pkVar));
+                VariableReferenceExpression pkVarRef = new VariableReferenceExpression(pkVar);
+                pkVarRef.setSourceLocation(sourceLoc);
+                Mutable<ILogicalExpression> vRef = new MutableObject<>(pkVarRef);
                 order.getOrderExpressions().add(new Pair<>(OrderOperator.ASC_ORDER, vRef));
             }
             // The secondary-index search feeds into the sort.
@@ -837,9 +855,11 @@ public class AccessMethodUtils {
             order.setExecutionMode(ExecutionMode.LOCAL);
             context.computeAndSetTypeEnvironmentForOperator(order);
         }
+
         // Creates the primary-index search unnest-map operator.
-        AbstractUnnestMapOperator primaryIndexUnnestMapOp = createPrimaryIndexUnnestMapOp(dataset, retainInput,
-                retainMissing, requiresBroadcast, primaryKeyVars, primaryIndexUnnestVars, primaryIndexOutputTypes);
+        AbstractUnnestMapOperator primaryIndexUnnestMapOp =
+                createPrimaryIndexUnnestMapOp(dataset, retainInput, retainMissing, requiresBroadcast, primaryKeyVars,
+                        primaryIndexUnnestVars, primaryIndexOutputTypes, sourceLoc);
         if (sortPrimaryKeys) {
             primaryIndexUnnestMapOp.getInputs().add(new MutableObject<ILogicalOperator>(order));
         } else {
@@ -859,6 +879,7 @@ public class AccessMethodUtils {
             LogicalVariable newMissingPlaceHolderForLOJ, List<LogicalVariable> pkVarsFromSIdxUnnestMapOp,
             List<LogicalVariable> primaryIndexUnnestVars, List<Object> primaryIndexOutputTypes)
             throws AlgebricksException {
+        SourceLocation sourceLoc = inputOp.getSourceLocation();
         Quadruple<Boolean, Boolean, Boolean, Boolean> indexOnlyPlanInfo = analysisCtx.getIndexOnlyPlanInfo();
         // From now on, we deal with the index-only plan.
         // Initializes the information required for the index-only plan optimization.
@@ -930,19 +951,24 @@ public class AccessMethodUtils {
             switch (spatialType.getTypeTag()) {
                 case POINT:
                     // Reconstructs a POINT value.
-                    AbstractFunctionCallExpression createPointExpr = createPointExpression(skVarsFromSIdxUnnestMap);
+                    AbstractFunctionCallExpression createPointExpr =
+                            createPointExpression(skVarsFromSIdxUnnestMap, sourceLoc);
                     restoredSKVarFromRTree.add(context.newVar());
                     restoredSKFromRTreeExprs.add(new MutableObject<ILogicalExpression>(createPointExpr));
                     skVarAssignOpInRightPath = new AssignOperator(restoredSKVarFromRTree, restoredSKFromRTreeExprs);
+                    skVarAssignOpInRightPath.setSourceLocation(sourceLoc);
                     break;
                 case RECTANGLE:
                     // Reconstructs a RECTANGLE value.
-                    AbstractFunctionCallExpression expr1 = createPointExpression(skVarsFromSIdxUnnestMap.subList(0, 2));
-                    AbstractFunctionCallExpression expr2 = createPointExpression(skVarsFromSIdxUnnestMap.subList(2, 4));
+                    AbstractFunctionCallExpression expr1 =
+                            createPointExpression(skVarsFromSIdxUnnestMap.subList(0, 2), sourceLoc);
+                    AbstractFunctionCallExpression expr2 =
+                            createPointExpression(skVarsFromSIdxUnnestMap.subList(2, 4), sourceLoc);
                     AbstractFunctionCallExpression createRectangleExpr = createRectangleExpression(expr1, expr2);
                     restoredSKVarFromRTree.add(context.newVar());
                     restoredSKFromRTreeExprs.add(new MutableObject<ILogicalExpression>(createRectangleExpr));
                     skVarAssignOpInRightPath = new AssignOperator(restoredSKVarFromRTree, restoredSKFromRTreeExprs);
+                    skVarAssignOpInRightPath.setSourceLocation(sourceLoc);
                     break;
                 default:
                     break;
@@ -1022,6 +1048,7 @@ public class AccessMethodUtils {
                 // If this assign operator is not used in the SELECT or JOIN operator,
                 // we will add this operator after creating UNION operator in the last part of this method.
                 constAssignOp = new AssignOperator(constAssignVars, constAssignExprs);
+                constAssignOp.setSourceLocation(sourceLoc);
                 if (constantAssignVarUsedInTopOp) {
                     // Places this assign after the secondary index-search op.
                     constAssignOp.getInputs().add(new MutableObject<ILogicalOperator>(inputOp));
@@ -1052,6 +1079,7 @@ public class AccessMethodUtils {
         // Adds a SPLIT operator after the given secondary index-search unnest-map operator.
         splitOp = new SplitOperator(2,
                 new MutableObject<ILogicalExpression>(new VariableReferenceExpression(condSplitVars.get(0))));
+        splitOp.setSourceLocation(sourceLoc);
         splitOp.getInputs().add(new MutableObject<ILogicalOperator>(currentOp));
         splitOp.setExecutionMode(ExecutionMode.PARTITIONED);
         context.computeAndSetTypeEnvironmentForOperator(splitOp);
@@ -1075,9 +1103,12 @@ public class AccessMethodUtils {
             LogicalVariable newVar = context.newVar();
             liveVarAfterSplitToLeftPathMap.put(v, newVar);
             assignVars.add(newVar);
-            assignExprs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(v)));
+            VariableReferenceExpression vRef = new VariableReferenceExpression(v);
+            vRef.setSourceLocation(sourceLoc);
+            assignExprs.add(new MutableObject<ILogicalExpression>(vRef));
         }
         AssignOperator origVarsToLeftPathVarsAssignOp = new AssignOperator(assignVars, assignExprs);
+        origVarsToLeftPathVarsAssignOp.setSourceLocation(sourceLoc);
         origVarsToLeftPathVarsAssignOp.getInputs().add(new MutableObject<ILogicalOperator>(splitOp));
         context.computeAndSetTypeEnvironmentForOperator(origVarsToLeftPathVarsAssignOp);
         origVarsToLeftPathVarsAssignOp.setExecutionMode(ExecutionMode.PARTITIONED);
@@ -1199,7 +1230,8 @@ public class AccessMethodUtils {
         // The job gen parameters are transferred to the actual job gen via the UnnestMapOperator's function arguments.
         AbstractUnnestMapOperator primaryIndexUnnestMapOp = createPrimaryIndexUnnestMapOp(dataset, retainInput,
                 retainMissing, requiresBroadcast, pkVarsInLeftPathFromSIdxSearchBeforeSplit,
-                pkVarsFromPIdxSearchInLeftPath, primaryIndexOutputTypes);
+                pkVarsFromPIdxSearchInLeftPath, primaryIndexOutputTypes, sourceLoc);
+        primaryIndexUnnestMapOp.setSourceLocation(sourceLoc);
         primaryIndexUnnestMapOp.getInputs().add(new MutableObject<ILogicalOperator>(origVarsToLeftPathVarsAssignOp));
         context.computeAndSetTypeEnvironmentForOperator(primaryIndexUnnestMapOp);
         primaryIndexUnnestMapOp.setExecutionMode(ExecutionMode.PARTITIONED);
@@ -1217,6 +1249,7 @@ public class AccessMethodUtils {
         LogicalVariable newMissingPlaceHolderVar = retainMissing ? newMissingPlaceHolderForLOJ : null;
         newSelectOpInLeftPath = new SelectOperator(new MutableObject<ILogicalExpression>(conditionRefExpr),
                 retainMissing, newMissingPlaceHolderVar);
+        newSelectOpInLeftPath.setSourceLocation(conditionRefExpr.getSourceLocation());
         VariableUtilities.substituteVariables(newSelectOpInLeftPath, origVarToNewVarInLeftPathMap, context);
 
         // If there are ASSIGN operators before the SELECT or JOIN operator,
@@ -1276,6 +1309,7 @@ public class AccessMethodUtils {
             ILogicalExpression conditionRefExpr2 = conditionRef.getValue().cloneExpression();
             newSelectOpInRightPath = new SelectOperator(new MutableObject<ILogicalExpression>(conditionRefExpr2),
                     retainMissing, newMissingPlaceHolderVar);
+            newSelectOpInRightPath.setSourceLocation(conditionRefExpr2.getSourceLocation());
             newSelectOpInRightPath.getInputs().add(new MutableObject<ILogicalOperator>(currentTopOpInRightPath));
             VariableUtilities.substituteVariables(newSelectOpInRightPath, origVarToSIdxUnnestMapOpVarMap, context);
             VariableUtilities.substituteVariables(newSelectOpInRightPath, origSKFieldVarToNewSKFieldVarMap, context);
@@ -1294,6 +1328,7 @@ public class AccessMethodUtils {
 
         // UNIONALL operator that combines both paths.
         unionAllOp = new UnionAllOperator(unionVarMap);
+        unionAllOp.setSourceLocation(sourceLoc);
         unionAllOp.getInputs().add(new MutableObject<ILogicalOperator>(newSelectOpInLeftPath));
         unionAllOp.getInputs().add(new MutableObject<ILogicalOperator>(currentTopOpInRightPath));
 
@@ -1322,8 +1357,8 @@ public class AccessMethodUtils {
 
     private static AbstractUnnestMapOperator createPrimaryIndexUnnestMapOp(Dataset dataset, boolean retainInput,
             boolean retainMissing, boolean requiresBroadcast, List<LogicalVariable> primaryKeyVars,
-            List<LogicalVariable> primaryIndexUnnestVars, List<Object> primaryIndexOutputTypes)
-            throws AlgebricksException {
+            List<LogicalVariable> primaryIndexUnnestVars, List<Object> primaryIndexOutputTypes,
+            SourceLocation sourceLoc) throws AlgebricksException {
         // The job gen parameters are transferred to the actual job gen via the UnnestMapOperator's function arguments.
         List<Mutable<ILogicalExpression>> primaryIndexFuncArgs = new ArrayList<>();
         BTreeJobGenParams jobGenParams = new BTreeJobGenParams(dataset.getDatasetName(), IndexType.BTREE,
@@ -1339,6 +1374,7 @@ public class AccessMethodUtils {
         IFunctionInfo primaryIndexSearch = FunctionUtil.getFunctionInfo(BuiltinFunctions.INDEX_SEARCH);
         AbstractFunctionCallExpression primaryIndexSearchFunc =
                 new ScalarFunctionCallExpression(primaryIndexSearch, primaryIndexFuncArgs);
+        primaryIndexSearchFunc.setSourceLocation(sourceLoc);
         // This is the operator that jobgen will be looking for. It contains an unnest function that has
         // all necessary arguments to determine which index to use, which variables contain the index-search keys,
         // what is the original dataset, etc.
@@ -1348,14 +1384,17 @@ public class AccessMethodUtils {
                 primaryIndexUnnestMapOp = new LeftOuterUnnestMapOperator(primaryIndexUnnestVars,
                         new MutableObject<ILogicalExpression>(primaryIndexSearchFunc), primaryIndexOutputTypes,
                         retainInput);
+                primaryIndexUnnestMapOp.setSourceLocation(sourceLoc);
             } else {
                 // Left-outer-join without retainNull and retainInput doesn't make sense.
-                throw new AlgebricksException("Left-outer-join should propagate all inputs from the outer branch.");
+                throw new CompilationException(ErrorCode.COMPILATION_ERROR, sourceLoc,
+                        "Left-outer-join should propagate all inputs from the outer branch.");
             }
         } else {
             primaryIndexUnnestMapOp = new UnnestMapOperator(primaryIndexUnnestVars,
                     new MutableObject<ILogicalExpression>(primaryIndexSearchFunc), primaryIndexOutputTypes,
                     retainInput);
+            primaryIndexUnnestMapOp.setSourceLocation(sourceLoc);
         }
         return primaryIndexUnnestMapOp;
     }
@@ -1416,12 +1455,18 @@ public class AccessMethodUtils {
         }
     }
 
-    private static AbstractFunctionCallExpression createPointExpression(List<LogicalVariable> pointVars) {
+    private static AbstractFunctionCallExpression createPointExpression(List<LogicalVariable> pointVars,
+            SourceLocation sourceLoc) {
         List<Mutable<ILogicalExpression>> expressions = new ArrayList<>();
         AbstractFunctionCallExpression createPointExpr1 =
                 new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.CREATE_POINT));
-        expressions.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(pointVars.get(0))));
-        expressions.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(pointVars.get(1))));
+        createPointExpr1.setSourceLocation(sourceLoc);
+        VariableReferenceExpression pointVarRef0 = new VariableReferenceExpression(pointVars.get(0));
+        pointVarRef0.setSourceLocation(sourceLoc);
+        expressions.add(new MutableObject<ILogicalExpression>(pointVarRef0));
+        VariableReferenceExpression pointVarRef1 = new VariableReferenceExpression(pointVars.get(1));
+        pointVarRef1.setSourceLocation(sourceLoc);
+        expressions.add(new MutableObject<ILogicalExpression>(pointVarRef1));
         createPointExpr1.getArguments().addAll(expressions);
         return createPointExpr1;
     }
@@ -1431,6 +1476,7 @@ public class AccessMethodUtils {
         List<Mutable<ILogicalExpression>> expressions = new ArrayList<>();
         AbstractFunctionCallExpression createRectangleExpr =
                 new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.CREATE_RECTANGLE));
+        createRectangleExpr.setSourceLocation(createPointExpr1.getSourceLocation());
         expressions.add(new MutableObject<ILogicalExpression>(createPointExpr1));
         expressions.add(new MutableObject<ILogicalExpression>(createPointExpr2));
         createRectangleExpr.getArguments().addAll(expressions);
@@ -1475,7 +1521,8 @@ public class AccessMethodUtils {
         }
 
         if (!foundSelectNonMissing) {
-            throw CompilationException.create(ErrorCode.CANNOT_FIND_NON_MISSING_SELECT_OPERATOR);
+            throw CompilationException.create(ErrorCode.CANNOT_FIND_NON_MISSING_SELECT_OPERATOR,
+                    lojGroupbyOp.getSourceLocation());
         }
         return isMissingFuncExpr;
     }
@@ -1486,8 +1533,9 @@ public class AccessMethodUtils {
         //reset the missing placeholder variable in groupby operator
         ScalarFunctionCallExpression isMissingFuncExpr = analysisCtx.getLOJIsMissingFuncInGroupBy();
         isMissingFuncExpr.getArguments().clear();
-        isMissingFuncExpr.getArguments().add(
-                new MutableObject<ILogicalExpression>(new VariableReferenceExpression(newMissingPlaceholderVaraible)));
+        VariableReferenceExpression newMissingVarRef = new VariableReferenceExpression(newMissingPlaceholderVaraible);
+        newMissingVarRef.setSourceLocation(isMissingFuncExpr.getSourceLocation());
+        isMissingFuncExpr.getArguments().add(new MutableObject<ILogicalExpression>(newMissingVarRef));
 
         //recompute type environment.
         OperatorPropertiesUtil.typeOpRec(analysisCtx.getLOJGroupbyOpRef(), context);
@@ -1526,12 +1574,16 @@ public class AccessMethodUtils {
             Dataset dataset, ARecordType recordType, ARecordType metaRecordType, ILogicalOperator inputOp,
             IOptimizationContext context, Index secondaryIndex, boolean retainInput, boolean retainNull)
             throws AlgebricksException {
+        SourceLocation sourceLoc = inputOp.getSourceLocation();
         List<LogicalVariable> primaryKeyVars = AccessMethodUtils.getKeyVarsFromSecondaryUnnestMap(dataset, recordType,
                 metaRecordType, inputOp, secondaryIndex, SecondaryUnnestMapOutputVarType.PRIMARY_KEY);
         // add a sort on the RID fields before fetching external data.
         OrderOperator order = new OrderOperator();
+        order.setSourceLocation(sourceLoc);
         for (LogicalVariable pkVar : primaryKeyVars) {
-            Mutable<ILogicalExpression> vRef = new MutableObject<>(new VariableReferenceExpression(pkVar));
+            VariableReferenceExpression pkVarRef = new VariableReferenceExpression(pkVar);
+            pkVarRef.setSourceLocation(sourceLoc);
+            Mutable<ILogicalExpression> vRef = new MutableObject<>(pkVarRef);
             order.getOrderExpressions().add(new Pair<>(OrderOperator.ASC_ORDER, vRef));
         }
         // The secondary-index search feeds into the sort.
@@ -1556,8 +1608,10 @@ public class AccessMethodUtils {
         IFunctionInfo externalLookup = FunctionUtil.getFunctionInfo(BuiltinFunctions.EXTERNAL_LOOKUP);
         AbstractFunctionCallExpression externalLookupFunc =
                 new ScalarFunctionCallExpression(externalLookup, externalLookupArgs);
+        externalLookupFunc.setSourceLocation(sourceLoc);
         UnnestMapOperator unnestOp = new UnnestMapOperator(externalUnnestVars,
                 new MutableObject<ILogicalExpression>(externalLookupFunc), outputTypes, retainInput);
+        unnestOp.setSourceLocation(sourceLoc);
         // Fed by the order operator or the secondaryIndexUnnestOp.
         unnestOp.getInputs().add(new MutableObject<ILogicalOperator>(order));
 

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/BTreeAccessMethod.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/BTreeAccessMethod.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/BTreeAccessMethod.java
index 60d4d3d..099f769 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/BTreeAccessMethod.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/BTreeAccessMethod.java
@@ -68,6 +68,7 @@ import org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractBina
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractDataSourceOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator.ExecutionMode;
+import org.apache.hyracks.algebricks.core.algebra.operators.logical.AbstractUnnestMapOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.LeftOuterUnnestMapOperator;
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.SelectOperator;
@@ -353,7 +354,8 @@ public class BTreeAccessMethod implements IAccessMethod {
                 keyPos = indexOf(optFuncExpr.getFieldName(1), chosenIndex.getKeyFieldNames());
             }
             if (keyPos < 0) {
-                throw CompilationException.create(ErrorCode.NO_INDEX_FIELD_NAME_FOR_GIVEN_FUNC_EXPR);
+                throw CompilationException.create(ErrorCode.NO_INDEX_FIELD_NAME_FOR_GIVEN_FUNC_EXPR,
+                        optFuncExpr.getFuncExpr().getSourceLocation());
             }
             // returnedSearchKeyExpr contains a pair of search expression.
             // The second expression will not be null only if we are creating an EQ search predicate
@@ -368,7 +370,9 @@ public class BTreeAccessMethod implements IAccessMethod {
             if (searchKeyExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
                 constantAtRuntimeExpressions[keyPos] = searchKeyExpr;
                 constAtRuntimeExprVars[keyPos] = context.newVar();
-                searchKeyExpr = new VariableReferenceExpression(constAtRuntimeExprVars[keyPos]);
+                VariableReferenceExpression varRef = new VariableReferenceExpression(constAtRuntimeExprVars[keyPos]);
+                varRef.setSourceLocation(searchKeyExpr.getSourceLocation());
+                searchKeyExpr = varRef;
             }
 
             LimitType limit = getLimitType(optFuncExpr, probeSubTree);
@@ -576,6 +580,7 @@ public class BTreeAccessMethod implements IAccessMethod {
         if (!assignKeyVarList.isEmpty()) {
             // Assign operator that sets the constant secondary-index search-key fields if necessary.
             AssignOperator assignSearchKeys = new AssignOperator(assignKeyVarList, assignKeyExprList);
+            assignSearchKeys.setSourceLocation(dataSourceOp.getSourceLocation());
             if (probeSubTree == null) {
                 // We are optimizing a selection query.
                 // Input to this assign is the EmptyTupleSource (which the dataSourceScan also must have had as input).
@@ -694,26 +699,33 @@ public class BTreeAccessMethod implements IAccessMethod {
                 IFunctionInfo primaryIndexSearch = FunctionUtil.getFunctionInfo(BuiltinFunctions.INDEX_SEARCH);
                 UnnestingFunctionCallExpression primaryIndexSearchFunc =
                         new UnnestingFunctionCallExpression(primaryIndexSearch, primaryIndexFuncArgs);
+                primaryIndexSearchFunc.setSourceLocation(dataSourceOp.getSourceLocation());
                 primaryIndexSearchFunc.setReturnsUniqueValues(true);
+                AbstractUnnestMapOperator unnestMapOp;
                 if (!leftOuterUnnestMapRequired) {
-                    indexSearchOp = new UnnestMapOperator(scanVariables,
+                    unnestMapOp = new UnnestMapOperator(scanVariables,
                             new MutableObject<ILogicalExpression>(primaryIndexSearchFunc), primaryIndexOutputTypes,
                             retainInput);
                 } else {
-                    indexSearchOp = new LeftOuterUnnestMapOperator(scanVariables,
+                    unnestMapOp = new LeftOuterUnnestMapOperator(scanVariables,
                             new MutableObject<ILogicalExpression>(primaryIndexSearchFunc), primaryIndexOutputTypes,
                             true);
                 }
+                unnestMapOp.setSourceLocation(dataSourceOp.getSourceLocation());
+                indexSearchOp = unnestMapOp;
             } else {
+                AbstractUnnestMapOperator unnestMapOp;
                 if (!leftOuterUnnestMapRequired) {
-                    indexSearchOp = new UnnestMapOperator(scanVariables,
+                    unnestMapOp = new UnnestMapOperator(scanVariables,
                             ((UnnestMapOperator) secondaryIndexUnnestOp).getExpressionRef(), primaryIndexOutputTypes,
                             retainInput);
                 } else {
-                    indexSearchOp = new LeftOuterUnnestMapOperator(scanVariables,
+                    unnestMapOp = new LeftOuterUnnestMapOperator(scanVariables,
                             ((LeftOuterUnnestMapOperator) secondaryIndexUnnestOp).getExpressionRef(),
                             primaryIndexOutputTypes, true);
                 }
+                unnestMapOp.setSourceLocation(dataSourceOp.getSourceLocation());
+                indexSearchOp = unnestMapOp;
             }
 
             indexSearchOp.getInputs().add(new MutableObject<>(inputOp));
@@ -775,7 +787,7 @@ public class BTreeAccessMethod implements IAccessMethod {
         // The original select cond must be an AND. Check it just to be sure.
         if (funcExpr.getFunctionIdentifier() != AlgebricksBuiltinFunctions.AND) {
             throw new CompilationException(ErrorCode.COMPILATION_FUNC_EXPRESSION_CANNOT_UTILIZE_INDEX,
-                    funcExpr.toString());
+                    funcExpr.getSourceLocation(), funcExpr.toString());
         }
         // Clean the conjuncts.
         for (Mutable<ILogicalExpression> arg : funcExpr.getArguments()) {
@@ -902,7 +914,9 @@ public class BTreeAccessMethod implements IAccessMethod {
     private ILogicalExpression createSelectCondition(List<Mutable<ILogicalExpression>> predList) {
         if (predList.size() > 1) {
             IFunctionInfo finfo = FunctionUtil.getFunctionInfo(AlgebricksBuiltinFunctions.AND);
-            return new ScalarFunctionCallExpression(finfo, predList);
+            ScalarFunctionCallExpression andExpr = new ScalarFunctionCallExpression(finfo, predList);
+            andExpr.setSourceLocation(predList.get(0).getValue().getSourceLocation());
+            return andExpr;
         }
         return predList.get(0).getValue();
     }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceLSMComponentFilterRule.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceLSMComponentFilterRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceLSMComponentFilterRule.java
index 7938f49..7739833 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceLSMComponentFilterRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceLSMComponentFilterRule.java
@@ -66,6 +66,7 @@ import org.apache.hyracks.algebricks.core.algebra.operators.logical.SplitOperato
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestMapOperator;
 import org.apache.hyracks.algebricks.core.algebra.util.OperatorPropertiesUtil;
 import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
+import org.apache.hyracks.api.exceptions.SourceLocation;
 
 public class IntroduceLSMComponentFilterRule implements IAlgebraicRewriteRule {
 
@@ -134,7 +135,8 @@ public class IntroduceLSMComponentFilterRule implements IAlgebraicRewriteRule {
     }
 
     private AssignOperator createAssignOperator(List<IOptimizableFuncExpr> optFuncExprs,
-            List<LogicalVariable> minFilterVars, List<LogicalVariable> maxFilterVars, IOptimizationContext context) {
+            List<LogicalVariable> minFilterVars, List<LogicalVariable> maxFilterVars, IOptimizationContext context,
+            SourceLocation sourceLoc) {
         List<LogicalVariable> assignKeyVarList = new ArrayList<>();
         List<Mutable<ILogicalExpression>> assignKeyExprList = new ArrayList<>();
 
@@ -154,7 +156,9 @@ public class IntroduceLSMComponentFilterRule implements IAlgebraicRewriteRule {
                 maxFilterVars.add(var);
             }
         }
-        return new AssignOperator(assignKeyVarList, assignKeyExprList);
+        AssignOperator assignOp = new AssignOperator(assignKeyVarList, assignKeyExprList);
+        assignOp.setSourceLocation(sourceLoc);
+        return assignOp;
     }
 
     private void assignFilterFromQuery(List<IOptimizableFuncExpr> optFuncExprs, AbstractLogicalOperator op,
@@ -175,15 +179,17 @@ public class IntroduceLSMComponentFilterRule implements IAlgebraicRewriteRule {
                     List<LogicalVariable> minFilterVars = new ArrayList<>();
                     List<LogicalVariable> maxFilterVars = new ArrayList<>();
 
-                    AssignOperator assignOp = createAssignOperator(optFuncExprs, minFilterVars, maxFilterVars, context);
+                    AssignOperator assignOp = createAssignOperator(optFuncExprs, minFilterVars, maxFilterVars, context,
+                            dataSourceScanOp.getSourceLocation());
 
                     dataSourceScanOp.setMinFilterVars(minFilterVars);
                     dataSourceScanOp.setMaxFilterVars(maxFilterVars);
 
                     List<Mutable<ILogicalExpression>> additionalFilteringExpressions = new ArrayList<>();
                     for (LogicalVariable var : assignOp.getVariables()) {
-                        additionalFilteringExpressions
-                                .add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(var)));
+                        VariableReferenceExpression varRef = new VariableReferenceExpression(var);
+                        varRef.setSourceLocation(assignOp.getSourceLocation());
+                        additionalFilteringExpressions.add(new MutableObject<ILogicalExpression>(varRef));
                     }
 
                     dataSourceScanOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
@@ -206,16 +212,17 @@ public class IntroduceLSMComponentFilterRule implements IAlgebraicRewriteRule {
                         List<LogicalVariable> minFilterVars = new ArrayList<>();
                         List<LogicalVariable> maxFilterVars = new ArrayList<>();
 
-                        AssignOperator assignOp =
-                                createAssignOperator(optFuncExprs, minFilterVars, maxFilterVars, context);
+                        AssignOperator assignOp = createAssignOperator(optFuncExprs, minFilterVars, maxFilterVars,
+                                context, unnestMapOp.getSourceLocation());
 
                         unnestMapOp.setMinFilterVars(minFilterVars);
                         unnestMapOp.setMaxFilterVars(maxFilterVars);
 
                         List<Mutable<ILogicalExpression>> additionalFilteringExpressions = new ArrayList<>();
                         for (LogicalVariable var : assignOp.getVariables()) {
-                            additionalFilteringExpressions
-                                    .add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(var)));
+                            VariableReferenceExpression varRef = new VariableReferenceExpression(var);
+                            varRef.setSourceLocation(assignOp.getSourceLocation());
+                            additionalFilteringExpressions.add(new MutableObject<ILogicalExpression>(varRef));
                         }
                         unnestMapOp.setAdditionalFilteringExpressions(additionalFilteringExpressions);
                         assignOp.getInputs().add(new MutableObject<>(unnestMapOp.getInputs().get(0).getValue()));
@@ -320,6 +327,7 @@ public class IntroduceLSMComponentFilterRule implements IAlgebraicRewriteRule {
         }
 
         IntersectOperator intersectWithFilter = new IntersectOperator(outputVars, compareVars, filterVars);
+        intersectWithFilter.setSourceLocation(intersect.getSourceLocation());
         intersectWithFilter.getInputs().addAll(intersect.getInputs());
         return intersectWithFilter;
     }
@@ -343,9 +351,12 @@ public class IntroduceLSMComponentFilterRule implements IAlgebraicRewriteRule {
         primaryOp.setMinFilterVars(Collections.singletonList(minFilterVar));
         primaryOp.setMaxFilterVars(Collections.singletonList(maxFilterVar));
 
+        VariableReferenceExpression minFilterVarRef = new VariableReferenceExpression(minFilterVar);
+        minFilterVarRef.setSourceLocation(primaryOp.getSourceLocation());
+        VariableReferenceExpression maxFilterVarRef = new VariableReferenceExpression(maxFilterVar);
+        maxFilterVarRef.setSourceLocation(primaryOp.getSourceLocation());
         List<Mutable<ILogicalExpression>> indexFilterExpression =
-                Arrays.asList(new MutableObject<>(new VariableReferenceExpression(minFilterVar)),
-                        new MutableObject<>(new VariableReferenceExpression(maxFilterVar)));
+                Arrays.asList(new MutableObject<>(minFilterVarRef), new MutableObject<>(maxFilterVarRef));
 
         primaryOp.setAdditionalFilteringExpressions(indexFilterExpression);
         context.computeAndSetTypeEnvironmentForOperator(primaryOp);
@@ -366,7 +377,8 @@ public class IntroduceLSMComponentFilterRule implements IAlgebraicRewriteRule {
                     AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) unnestExpr;
                     FunctionIdentifier fid = f.getFunctionIdentifier();
                     if (!fid.equals(BuiltinFunctions.INDEX_SEARCH)) {
-                        throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, fid.getName());
+                        throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, f.getSourceLocation(),
+                                fid.getName());
                     }
                     AccessMethodJobGenParams jobGenParams = new AccessMethodJobGenParams();
                     jobGenParams.readFromFuncArgs(f.getArguments());
@@ -414,7 +426,8 @@ public class IntroduceLSMComponentFilterRule implements IAlgebraicRewriteRule {
                         dataverseName = jobGenParams.dataverseName;
                         datasetName = jobGenParams.datasetName;
                     } else {
-                        throw new AlgebricksException("Unexpected function for Unnest Map: " + fid);
+                        throw new CompilationException(ErrorCode.COMPILATION_ERROR, f.getSourceLocation(),
+                                "Unexpected function for Unnest Map: " + fid);
                     }
                     return ((MetadataProvider) context.getMetadataProvider()).findDataset(dataverseName, datasetName);
                 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/ee54cc02/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceSelectAccessMethodRule.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceSelectAccessMethodRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceSelectAccessMethodRule.java
index 27e5645..d2b456f 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceSelectAccessMethodRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceSelectAccessMethodRule.java
@@ -262,10 +262,12 @@ public class IntroduceSelectAccessMethodRule extends AbstractIntroduceAccessMeth
         List<List<LogicalVariable>> inputVars = new ArrayList<>(subRoots.size());
         for (int i = 0; i < subRoots.size(); i++) {
             if (lop.getOperatorTag() != subRoots.get(i).getOperatorTag()) {
-                throw new AlgebricksException("The data source root should have the same operator type.");
+                throw new CompilationException(ErrorCode.COMPILATION_ERROR, lop.getSourceLocation(),
+                        "The data source root should have the same operator type.");
             }
             if (lop.getInputs().size() != 1) {
-                throw new AlgebricksException("The primary search has multiple inputs.");
+                throw new CompilationException(ErrorCode.COMPILATION_ERROR, lop.getSourceLocation(),
+                        "The primary search has multiple inputs.");
             }
 
             ILogicalOperator curRoot = subRoots.get(i);
@@ -274,7 +276,8 @@ public class IntroduceSelectAccessMethodRule extends AbstractIntroduceAccessMeth
             for (Pair<OrderOperator.IOrder, Mutable<ILogicalExpression>> orderExpression : order
                     .getOrderExpressions()) {
                 if (orderExpression.second.getValue().getExpressionTag() != LogicalExpressionTag.VARIABLE) {
-                    throw new AlgebricksException(
+                    throw new CompilationException(ErrorCode.COMPILATION_ERROR,
+                            orderExpression.second.getValue().getSourceLocation(),
                             "The order by expression should be variables, but they aren't variables.");
                 }
                 VariableReferenceExpression orderedVar =
@@ -286,6 +289,7 @@ public class IntroduceSelectAccessMethodRule extends AbstractIntroduceAccessMeth
 
         List<LogicalVariable> outputVar = inputVars.get(0);
         IntersectOperator intersect = new IntersectOperator(outputVar, inputVars);
+        intersect.setSourceLocation(lop.getSourceLocation());
         for (ILogicalOperator secondarySearch : subRoots) {
             intersect.getInputs().add(secondarySearch.getInputs().get(0));
         }