You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by ma...@apache.org on 2015/03/04 23:40:56 UTC

[21/50] [abbrv] phoenix git commit: PHOENIX-1646 Views and functional index expressions may lose information when stringified

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/parse/OuterJoinParseNode.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/OuterJoinParseNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/OuterJoinParseNode.java
deleted file mode 100644
index 97f636b..0000000
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/OuterJoinParseNode.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.phoenix.parse;
-
-import java.sql.SQLException;
-import java.util.Collections;
-import java.util.List;
-
-
-
-/**
- * 
- * Node representing an outer join qualifier (+) in SQL
- * TODO: remove Oracle specific syntax
- *
- * 
- * @since 0.1
- */
-public class OuterJoinParseNode extends UnaryParseNode{
-    OuterJoinParseNode(ParseNode node) {
-        super(node);
-    }
-
-    @Override
-    public <T> T accept(ParseNodeVisitor<T> visitor) throws SQLException {
-        List<T> l = Collections.emptyList();
-        if (visitor.visitEnter(this)) {
-            l = acceptChildren(visitor);
-        }
-        return visitor.visitLeave(this, l);
-    }
-}

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNode.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNode.java
index 2ee8a83..b32674e 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNode.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNode.java
@@ -20,6 +20,8 @@ package org.apache.phoenix.parse;
 import java.sql.SQLException;
 import java.util.List;
 
+import org.apache.phoenix.compile.ColumnResolver;
+
 
 
 
@@ -47,4 +49,13 @@ public abstract class ParseNode {
     public String getAlias() {
         return null;
     }
+    
+    @Override
+    public final String toString() {
+        StringBuilder buf = new StringBuilder();
+        toSQL(null, buf);
+        return buf.toString();
+    }
+    
+    public abstract void toSQL(ColumnResolver resolver, StringBuilder buf);
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
index c92dbb6..ddfaa03 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/ParseNodeFactory.java
@@ -19,7 +19,6 @@ package org.apache.phoenix.parse;
 
 import java.lang.reflect.Constructor;
 import java.sql.SQLException;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -52,6 +51,7 @@ import org.apache.phoenix.schema.types.PTimestamp;
 import org.apache.phoenix.util.SchemaUtil;
 
 import com.google.common.collect.ListMultimap;
+import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 
 /**
@@ -391,12 +391,22 @@ public class ParseNodeFactory {
     public FunctionParseNode function(String name, List<ParseNode> valueNodes,
             List<ParseNode> columnNodes, boolean isAscending) {
 
-        List<ParseNode> children = new ArrayList<ParseNode>();
-        children.addAll(columnNodes);
-        children.add(new LiteralParseNode(Boolean.valueOf(isAscending)));
-        children.addAll(valueNodes);
+        List<ParseNode> args = Lists.newArrayListWithExpectedSize(columnNodes.size() + valueNodes.size() + 1);
+        args.addAll(columnNodes);
+        args.add(new LiteralParseNode(Boolean.valueOf(isAscending)));
+        args.addAll(valueNodes);
 
-        return function(name, children);
+        BuiltInFunctionInfo info = getInfo(name, args);
+        Constructor<? extends FunctionParseNode> ctor = info.getNodeCtor();
+        if (ctor == null) {
+            return new AggregateFunctionWithinGroupParseNode(name, args, info);
+        } else {
+            try {
+                return ctor.newInstance(name, args, info);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+        }
     }
 
     public HintNode hint(String hint) {
@@ -561,8 +571,12 @@ public class ParseNodeFactory {
     	return new ArrayConstructorNode(upsertStmtArray);
     }
 
-    public MultiplyParseNode negate(ParseNode child) {
-        return new MultiplyParseNode(Arrays.asList(child,this.literal(-1)));
+    public ParseNode negate(ParseNode child) {
+        // Prevents reparsing of -1 from becoming 1*-1 and 1*1*-1 with each re-parsing
+        if (LiteralParseNode.ONE.equals(child)) {
+            return LiteralParseNode.MINUS_ONE;
+        }
+        return new MultiplyParseNode(Arrays.asList(child,LiteralParseNode.MINUS_ONE));
     }
 
     public NotEqualParseNode notEqual(ParseNode lhs, ParseNode rhs) {
@@ -588,10 +602,6 @@ public class ParseNodeFactory {
     }
 
 
-    public OuterJoinParseNode outer(ParseNode node) {
-        return new OuterJoinParseNode(node);
-    }
-
     public SelectStatement select(TableNode from, HintNode hint, boolean isDistinct, List<AliasedNode> select, ParseNode where,
             List<ParseNode> groupBy, ParseNode having, List<OrderByNode> orderBy, LimitNode limit, int bindCount, boolean isAggregate, boolean hasSequence) {
 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/parse/RowValueConstructorParseNode.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/RowValueConstructorParseNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/RowValueConstructorParseNode.java
index 87038c7..3d6d7f1 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/RowValueConstructorParseNode.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/RowValueConstructorParseNode.java
@@ -21,6 +21,8 @@ import java.sql.SQLException;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.phoenix.compile.ColumnResolver;
+
 /**
  * 
  * Node representing a row value constructor in SQL.  
@@ -43,4 +45,18 @@ public class RowValueConstructorParseNode extends CompoundParseNode {
         return visitor.visitLeave(this, l);
     }
 
+    @Override
+    public void toSQL(ColumnResolver resolver, StringBuilder buf) {
+        List<ParseNode> children = getChildren();
+        buf.append(' ');
+        buf.append('(');
+        if (!children.isEmpty()) {
+            for (ParseNode child : children) {
+                child.toSQL(resolver, buf);
+                buf.append(',');
+            }
+            buf.setLength(buf.length()-1);
+        }
+        buf.append(')');
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/parse/SelectStatement.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/SelectStatement.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/SelectStatement.java
index 961846b..71cabd6 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/SelectStatement.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/SelectStatement.java
@@ -21,6 +21,7 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.phoenix.compile.ColumnResolver;
 import org.apache.phoenix.expression.function.CountAggregateFunction;
 import org.apache.phoenix.jdbc.PhoenixStatement.Operation;
 import org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction;
@@ -93,6 +94,104 @@ public class SelectStatement implements FilterableStatement {
     private final boolean isAggregate;
     private final boolean hasSequence;
     
+    @Override
+    public final String toString() {
+        StringBuilder buf = new StringBuilder();
+        toSQL(null,buf);
+        return buf.toString();
+    }
+
+    public void toSQL(ColumnResolver resolver, StringBuilder buf) {
+        buf.append("SELECT ");
+        if (hint != null) buf.append(hint);
+        if (isDistinct) buf.append("DISTINCT ");
+        for (AliasedNode selectNode : select) {
+            selectNode.toSQL(resolver, buf);
+            buf.append(',');
+        }
+        buf.setLength(buf.length()-1);
+        buf.append(" FROM ");
+        fromTable.toSQL(resolver, buf);
+        if (where != null) {
+            buf.append(" WHERE ");
+            where.toSQL(resolver, buf);
+        }
+        if (!groupBy.isEmpty()) {
+            buf.append(" GROUP BY ");
+            for (ParseNode node : groupBy) {
+                node.toSQL(resolver, buf);
+                buf.append(',');
+            }
+            buf.setLength(buf.length()-1);
+        }
+        if (having != null) {
+            buf.append(" HAVING ");
+            having.toSQL(resolver, buf);            
+        }
+        if (!orderBy.isEmpty()) {
+            buf.append(" ORDER BY ");
+            for (OrderByNode node : orderBy) {
+                node.toSQL(resolver, buf);
+                buf.append(',');
+            }
+            buf.setLength(buf.length()-1);
+        }
+        if (limit != null) {
+            buf.append(" LIMIT " + limit.toString());
+        }
+    }    
+
+    
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((fromTable == null) ? 0 : fromTable.hashCode());
+        result = prime * result + ((groupBy == null) ? 0 : groupBy.hashCode());
+        result = prime * result + ((having == null) ? 0 : having.hashCode());
+        result = prime * result + ((hint == null) ? 0 : hint.hashCode());
+        result = prime * result + (isDistinct ? 1231 : 1237);
+        result = prime * result + ((limit == null) ? 0 : limit.hashCode());
+        result = prime * result + ((orderBy == null) ? 0 : orderBy.hashCode());
+        result = prime * result + ((select == null) ? 0 : select.hashCode());
+        result = prime * result + ((where == null) ? 0 : where.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) return true;
+        if (obj == null) return false;
+        if (getClass() != obj.getClass()) return false;
+        SelectStatement other = (SelectStatement)obj;
+        if (fromTable == null) {
+            if (other.fromTable != null) return false;
+        } else if (!fromTable.equals(other.fromTable)) return false;
+        if (groupBy == null) {
+            if (other.groupBy != null) return false;
+        } else if (!groupBy.equals(other.groupBy)) return false;
+        if (having == null) {
+            if (other.having != null) return false;
+        } else if (!having.equals(other.having)) return false;
+        if (hint == null) {
+            if (other.hint != null) return false;
+        } else if (!hint.equals(other.hint)) return false;
+        if (isDistinct != other.isDistinct) return false;
+        if (limit == null) {
+            if (other.limit != null) return false;
+        } else if (!limit.equals(other.limit)) return false;
+        if (orderBy == null) {
+            if (other.orderBy != null) return false;
+        } else if (!orderBy.equals(other.orderBy)) return false;
+        if (select == null) {
+            if (other.select != null) return false;
+        } else if (!select.equals(other.select)) return false;
+        if (where == null) {
+            if (other.where != null) return false;
+        } else if (!where.equals(other.where)) return false;
+        return true;
+    }
+
     // Count constant expressions
     private static int countConstants(List<ParseNode> nodes) {
         int count = 0;

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/parse/SequenceValueParseNode.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/SequenceValueParseNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/SequenceValueParseNode.java
index 260584f..a5d60fe 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/SequenceValueParseNode.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/SequenceValueParseNode.java
@@ -20,6 +20,8 @@ package org.apache.phoenix.parse;
 
 import java.sql.SQLException;
 
+import org.apache.phoenix.compile.ColumnResolver;
+
 
 public class SequenceValueParseNode extends TerminalParseNode {
     public enum Op {
@@ -89,4 +91,12 @@ public class SequenceValueParseNode extends TerminalParseNode {
 			return false;
 		return true;
 	}
+
+    @Override
+    public void toSQL(ColumnResolver resolver, StringBuilder buf) {
+        buf.append(' ');
+        buf.append(op.getName());
+        buf.append(" VALUE FOR ");
+        buf.append(tableName);
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/parse/StringConcatParseNode.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/StringConcatParseNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/StringConcatParseNode.java
index 3fd27de..5eba979 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/StringConcatParseNode.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/StringConcatParseNode.java
@@ -21,6 +21,8 @@ import java.sql.SQLException;
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.phoenix.compile.ColumnResolver;
+
 
 
 
@@ -46,4 +48,16 @@ public class StringConcatParseNode extends CompoundParseNode {
         return visitor.visitLeave(this, l);
     }
     
+    
+    @Override
+    public void toSQL(ColumnResolver resolver, StringBuilder buf) {
+        buf.append('(');
+        List<ParseNode> children = getChildren();
+        children.get(0).toSQL(resolver, buf);
+        for (int i = 1 ; i < children.size(); i++) {
+            buf.append(" || ");
+            children.get(i).toSQL(resolver, buf);
+        }
+        buf.append(')');
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/parse/SubqueryParseNode.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/SubqueryParseNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/SubqueryParseNode.java
index b7bcb64..d73958e 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/SubqueryParseNode.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/SubqueryParseNode.java
@@ -19,6 +19,8 @@ package org.apache.phoenix.parse;
 
 import java.sql.SQLException;
 
+import org.apache.phoenix.compile.ColumnResolver;
+
 
 
 /**
@@ -78,4 +80,10 @@ public class SubqueryParseNode extends TerminalParseNode {
 		return true;
 	}
     
+    @Override
+    public void toSQL(ColumnResolver resolver, StringBuilder buf) {
+        buf.append('(');
+        select.toSQL(resolver, buf);
+        buf.append(')');
+    }    
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/parse/SubtractParseNode.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/SubtractParseNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/SubtractParseNode.java
index 01e6654..decc3ac 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/SubtractParseNode.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/SubtractParseNode.java
@@ -31,6 +31,13 @@ import java.util.List;
  * @since 0.1
  */
 public class SubtractParseNode extends ArithmeticParseNode {
+    public static final String OPERATOR = "-";
+
+    @Override
+    public String getOperator() {
+        return OPERATOR;
+    }
+
     SubtractParseNode(List<ParseNode> children) {
         super(children);
     }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/parse/TableName.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/TableName.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/TableName.java
index 654e899..61bfa6b 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/TableName.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/TableName.java
@@ -60,7 +60,9 @@ public class TableName {
     
     @Override
     public String toString() {
-        return (schemaName == null ? "" : schemaName + QueryConstants.NAME_SEPARATOR)  + tableName;
+        return (schemaName == null ? "" : ((isSchemaNameCaseSensitive ? "\"" : "") + schemaName
+                + (isSchemaNameCaseSensitive ? "\"" : "") + QueryConstants.NAME_SEPARATOR))
+                + ((isTableNameCaseSensitive ? "\"" : "") + tableName + (isTableNameCaseSensitive ? "\"" : ""));
     }
     
 	@Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/parse/TableNode.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/TableNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/TableNode.java
index 7ab8d0c..7c37234 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/TableNode.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/TableNode.java
@@ -19,6 +19,8 @@ package org.apache.phoenix.parse;
 
 import java.sql.SQLException;
 
+import org.apache.phoenix.compile.ColumnResolver;
+
 
 
 /**
@@ -39,6 +41,14 @@ public abstract class TableNode {
         return alias;
     }
 
+    @Override
+    public final String toString() {
+        StringBuilder buf = new StringBuilder();
+        toSQL(null,buf);
+        return buf.toString();
+    }
+
     public abstract <T> T accept(TableNodeVisitor<T> visitor) throws SQLException;
+    public abstract void toSQL(ColumnResolver resolver, StringBuilder buf);
 }
 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/parse/TableWildcardParseNode.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/TableWildcardParseNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/TableWildcardParseNode.java
index 7292347..7c7f416 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/TableWildcardParseNode.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/TableWildcardParseNode.java
@@ -19,6 +19,8 @@ package org.apache.phoenix.parse;
 
 import java.sql.SQLException;
 
+import org.apache.phoenix.compile.ColumnResolver;
+
 public class TableWildcardParseNode extends NamedParseNode {
     private final TableName tableName;
     private final boolean isRewrite;
@@ -75,5 +77,10 @@ public class TableWildcardParseNode extends NamedParseNode {
 		return true;
 	}
 
+    @Override
+    public void toSQL(ColumnResolver resolver, StringBuilder buf) {
+        toSQL(buf);
+        buf.append(".*");
+    }
 }
 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/parse/WildcardParseNode.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/parse/WildcardParseNode.java b/phoenix-core/src/main/java/org/apache/phoenix/parse/WildcardParseNode.java
index fdfb64f..9922c3f 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/parse/WildcardParseNode.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/parse/WildcardParseNode.java
@@ -19,6 +19,8 @@ package org.apache.phoenix.parse;
 
 import java.sql.SQLException;
 
+import org.apache.phoenix.compile.ColumnResolver;
+
 
 
 /**
@@ -44,11 +46,6 @@ public class WildcardParseNode extends TerminalParseNode {
         return visitor.visit(this);
     }
 
-    @Override
-    public String toString() {
-        return NAME;
-    }
-
     public boolean isRewrite() {
         return isRewrite;
     }
@@ -73,6 +70,13 @@ public class WildcardParseNode extends TerminalParseNode {
 		if (isRewrite != other.isRewrite)
 			return false;
 		return true;
-	}    
+	}
+
+    @Override
+    public void toSQL(ColumnResolver resolver, StringBuilder buf) {
+        buf.append(' ');
+        buf.append(NAME);
+        buf.append(' ');
+    }    
     
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
index fceb724..2722cb6 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/MetaDataClient.java
@@ -1002,6 +1002,12 @@ public class MetaDataClient {
                     }
                     unusedPkColumns.remove(expression);
                     
+                    // Go through parse node to get string as otherwise we
+                    // can lose information during compilation
+                    StringBuilder buf = new StringBuilder();
+                    parseNode.toSQL(resolver, buf);
+                    String expressionStr = buf.toString();
+                    
                     ColumnName colName = null;
                     ColumnRef colRef = expressionIndexCompiler.getColumnRef();
 					if (colRef!=null) { 
@@ -1013,13 +1019,13 @@ public class MetaDataClient {
 					else { 
 						// if this is an expression
 					    // TODO column names cannot have double quotes, remove this once this PHOENIX-1621 is fixed
-						String name = expression.toString().replaceAll("\"", "'");
+						String name = expressionStr.replaceAll("\"", "'");
                         colName = ColumnName.caseSensitiveColumnName(IndexUtil.getIndexColumnName(null, name));
 					}
 					indexedColumnNames.add(colName);
                 	PDataType dataType = IndexUtil.getIndexColumnDataType(expression.isNullable(), expression.getDataType());
                     allPkColumns.add(new Pair<ColumnName, SortOrder>(colName, pair.getSecond()));
-                    columnDefs.add(FACTORY.columnDef(colName, dataType.getSqlTypeName(), expression.isNullable(), expression.getMaxLength(), expression.getScale(), false, pair.getSecond(), expression.toString()));
+                    columnDefs.add(FACTORY.columnDef(colName, dataType.getSqlTypeName(), expression.isNullable(), expression.getMaxLength(), expression.getScale(), false, pair.getSecond(), expressionStr));
                 }
 
                 // Next all the PK columns from the data table that aren't indexed

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDate.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDate.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDate.java
index fa070d3..b926afb 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDate.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDate.java
@@ -26,6 +26,7 @@ import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.schema.SortOrder;
 import org.apache.phoenix.util.DateUtil;
+import org.apache.phoenix.util.StringUtil;
 
 public class PDate extends PDataType<Date> {
 
@@ -150,10 +151,10 @@ public class PDate extends PDataType<Date> {
   public String toStringLiteral(Object o, Format formatter) {
       if (formatter == null) {
           // If default formatter has not been overridden,
-          // use one that displays milliseconds.
+          // use default one.
           formatter = DateUtil.DEFAULT_DATE_FORMATTER;
         }
-        return "'" + super.toStringLiteral(o, formatter) + "'";
+        return "'" + StringUtil.escapeStringConstant(super.toStringLiteral(o, formatter)) + "'";
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarchar.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarchar.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarchar.java
index 6956942..9883e12 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarchar.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarchar.java
@@ -24,6 +24,7 @@ import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.schema.SortOrder;
 import org.apache.phoenix.util.ByteUtil;
+import org.apache.phoenix.util.StringUtil;
 
 import com.google.common.base.Preconditions;
 
@@ -142,7 +143,7 @@ public class PVarchar extends PDataType<String> {
     if (formatter != null) {
       return "'" + formatter.format(o) + "'";
     }
-    return "'" + o + "'";
+    return "'" + StringUtil.escapeStringConstant(o.toString()) + "'";
   }
 
   private char[] sampleChars = new char[1];

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/util/IndexUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/IndexUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/IndexUtil.java
index c058eb8..31b6350 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/IndexUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/IndexUtil.java
@@ -453,8 +453,11 @@ public class IndexUtil {
         };
         ColumnResolver indexResolver = FromCompiler.getResolver(indexTableRef);
         StatementContext context = new StatementContext(statement, indexResolver);
-        Expression whereClause = WhereCompiler.compile(context, whereNode);
-        return QueryUtil.getViewStatement(index.getSchemaName().getString(), index.getTableName().getString(), whereClause);
+        // Compile to ensure validity
+        WhereCompiler.compile(context, whereNode);
+        StringBuilder buf = new StringBuilder();
+        whereNode.toSQL(indexResolver, buf);
+        return QueryUtil.getViewStatement(index.getSchemaName().getString(), index.getTableName().getString(), buf.toString());
     }
     
     public static void wrapResultUsingOffset(final ObserverContext<RegionCoprocessorEnvironment> c,

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/util/QueryUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/QueryUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/QueryUtil.java
index 88b68b0..e0b4c2e 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/QueryUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/QueryUtil.java
@@ -34,9 +34,9 @@ import javax.annotation.Nullable;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
 import org.apache.hadoop.hbase.util.Addressing;
 import org.apache.hadoop.hbase.zookeeper.ZKConfig;
-import org.apache.phoenix.expression.Expression;
 import org.apache.phoenix.iterate.ResultIterator;
 import org.apache.phoenix.jdbc.PhoenixDriver;
 import org.apache.phoenix.parse.WildcardParseNode;
@@ -76,6 +76,19 @@ public final class QueryUtil {
     private static final String SELECT = "SELECT";
     private static final String FROM = "FROM";
     private static final String WHERE = "WHERE";
+    private static final String[] CompareOpString = new String[CompareOp.values().length];
+    static {
+        CompareOpString[CompareOp.EQUAL.ordinal()] = "=";
+        CompareOpString[CompareOp.NOT_EQUAL.ordinal()] = "!=";
+        CompareOpString[CompareOp.GREATER.ordinal()] = ">";
+        CompareOpString[CompareOp.LESS.ordinal()] = "<";
+        CompareOpString[CompareOp.GREATER_OR_EQUAL.ordinal()] = ">=";
+        CompareOpString[CompareOp.LESS_OR_EQUAL.ordinal()] = "<=";
+    }
+
+    public static String toSQL(CompareOp op) {
+        return CompareOpString[op.ordinal()];
+    }
     
     /**
      * Private constructor
@@ -262,11 +275,11 @@ public final class QueryUtil {
         return getUrl(server, port);
     }
     
-    public static String getViewStatement(String schemaName, String tableName, Expression whereClause) {
+    public static String getViewStatement(String schemaName, String tableName, String where) {
         // Only form we currently support for VIEWs: SELECT * FROM t WHERE ...
         return SELECT + " " + WildcardParseNode.NAME + " " + FROM + " " +
                 (schemaName == null || schemaName.length() == 0 ? "" : ("\"" + schemaName + "\".")) +
                 ("\"" + tableName + "\" ") +
-                (WHERE + " " + whereClause.toString());
+                (WHERE + " " + where);
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/main/java/org/apache/phoenix/util/StringUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/StringUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/StringUtil.java
index d65af15..a83098a 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/StringUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/StringUtil.java
@@ -19,6 +19,7 @@ package org.apache.phoenix.util;
 
 import java.util.Arrays;
 
+import org.apache.commons.lang.StringEscapeUtils;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.exception.UndecodableByteException;
 import org.apache.phoenix.schema.SortOrder;
@@ -325,5 +326,9 @@ public class StringUtil {
         if (toIndex > length) {
             throw new ArrayIndexOutOfBoundsException(toIndex);
         }
+    }
+
+    public static String escapeStringConstant(String pattern) {
+        return StringEscapeUtils.escapeSql(pattern); // Need to escape double quotes
     }   
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereCompilerTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereCompilerTest.java b/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereCompilerTest.java
index 69c1bbf..01f28ae 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereCompilerTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereCompilerTest.java
@@ -79,12 +79,18 @@ import com.google.common.collect.ImmutableList;
 
 public class WhereCompilerTest extends BaseConnectionlessQueryTest {
 
+    private PhoenixPreparedStatement newPreparedStatement(PhoenixConnection pconn, String query) throws SQLException {
+        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        assertRoundtrip(query);
+        return pstmt;
+    }
+    
     @Test
     public void testSingleEqualFilter() throws SQLException {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and a_integer=0";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -101,7 +107,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
         pconn.createStatement().execute("CREATE TABLE t (k bigint not null primary key, v varchar) SALT_BUCKETS=20");
         String query = "select * from t where k=" + 1;
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -122,7 +128,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
         pconn.createStatement().execute("CREATE TABLE t (k varchar primary key, v varchar) SALT_BUCKETS=20");
         String query = "select * from t where k='a'";
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -143,7 +149,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
         pconn.createStatement().execute("CREATE TABLE t (k bigint not null primary key, v varchar) SALT_BUCKETS=20");
         String query = "select * from t where k in (1,3)";
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -195,7 +201,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and a_string=b_string";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -212,7 +218,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and substr(entity_id,null) = 'foo'";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -229,7 +235,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         List<Object> binds = Arrays.<Object>asList(tenantId);
 
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         bindParams(pstmt, binds);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
@@ -253,7 +259,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and 0 >= a_integer";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
 
@@ -272,7 +278,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String dateStr = "2012-01-01 12:00:00";
         String query = "select * from atable where organization_id='" + tenantId + "' and a_date >= to_date('" + dateStr + "')";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -291,7 +297,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and x_decimal >= " + toNumberClause;
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -348,7 +354,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = "select * from atable where substr(entity_id,1,3)=?";
         List<Object> binds = Arrays.<Object>asList(keyPrefix);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         bindParams(pstmt, binds);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
@@ -374,7 +380,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = "select * from atable where entity_id=?";
         List<Object> binds = Arrays.<Object>asList(keyPrefix);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         bindParams(pstmt, binds);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
@@ -390,7 +396,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = "select * from atable where organization_id=? AND entity_id=?";
         List<Object> binds = Arrays.<Object>asList(tenantId,keyPrefix);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         bindParams(pstmt, binds);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
@@ -405,7 +411,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = "select * from atable where substr(entity_id,1,3)=?";
         List<Object> binds = Arrays.<Object>asList(keyPrefix);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         bindParams(pstmt, binds);
         QueryPlan plan = pstmt.optimizeQuery();
         // Degenerate b/c "foobar" is more than 3 characters
@@ -420,7 +426,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = "select * from atable where a_string=?";
         List<Object> binds = Arrays.<Object>asList(aString);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         bindParams(pstmt, binds);
         QueryPlan plan = pstmt.optimizeQuery();
         // Degenerate b/c a_string length is 100
@@ -435,7 +441,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = "select * from atable where organization_id=? and (substr(entity_id,1,3)=? or a_integer=?)";
         List<Object> binds = Arrays.<Object>asList(tenantId, keyPrefix, aInt);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         bindParams(pstmt, binds);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
@@ -464,7 +470,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and a_integer > 'foo'";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
 
         try {
             pstmt.optimizeQuery();
@@ -479,7 +485,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and a_integer=0 and 2=3";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         assertDegenerate(plan.getContext());
     }
@@ -489,7 +495,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and 2=3";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         assertDegenerate(plan.getContext());
     }
@@ -499,7 +505,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and 2<=2";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         assertNull(scan.getFilter());
@@ -514,7 +520,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and a_integer=0 and 2<3";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -536,7 +542,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and (a_integer=0 or 3!=3)";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -557,7 +563,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and (a_integer=0 or 3>2)";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -573,7 +579,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and a_string IN ('a','b')";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         byte[] startRow = PVarchar.INSTANCE.toBytes(tenantId);
@@ -598,7 +604,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = String.format("select * from %s where organization_id IN ('%s','%s','%s')",
                 ATABLE_NAME, tenantId1, tenantId3, tenantId2);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         byte[] startRow = PVarchar.INSTANCE.toBytes(tenantId1);
@@ -625,7 +631,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = String.format("select * from %s where organization_id='%s' OR organization_id='%s' OR organization_id='%s'",
                 ATABLE_NAME, tenantId1, tenantId3, tenantId2);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
 
@@ -653,7 +659,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = String.format("select * from %s where organization_id='%s' AND entity_id IN ('%s','%s')",
                 ATABLE_NAME, tenantId, entityId1, entityId2);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         byte[] startRow = PVarchar.INSTANCE.toBytes(tenantId + entityId1);
@@ -683,7 +689,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = String.format("select * from %s where organization_id IN ('%s','%s','%s') AND entity_id>='%s' AND entity_id<='%s'",
                 ATABLE_NAME, tenantId1, tenantId3, tenantId2, entityId1, entityId2);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -712,7 +718,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = String.format("select * from %s where organization_id IN ('%s','%s','%s') AND entity_id='%s'",
                 ATABLE_NAME, tenantId1, tenantId3, tenantId2, entityId);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -735,7 +741,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = String.format("select * from %s where organization_id IN ('%s','%s','%s') AND entity_id='%s'",
                 ATABLE_NAME, tenantId1, tenantId3, tenantId2, entityId);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId1), PVarchar.INSTANCE.toBytes(entityId));
@@ -765,7 +771,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = String.format("select * from %s where organization_id IN ('%s','%s') AND entity_id IN ('%s', '%s')",
                 ATABLE_NAME, tenantId1, tenantId2, entityId1, entityId2);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
 
@@ -789,7 +795,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = String.format("select * from %s where organization_id > '%s' AND organization_id < '%s'",
                 ATABLE_NAME, tenantId1, tenantId2);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
 
@@ -810,7 +816,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String query = String.format("select * from %s where organization_id IN ('%s','%s','%s') AND entity_id IN ('%s', '%s')",
                 ATABLE_NAME, tenantId1, tenantId3, tenantId2, entityId1, entityId2);
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         byte[] startRow = ByteUtil.concat(PVarchar.INSTANCE.toBytes(tenantId1), PVarchar.INSTANCE.toBytes(entityId1));
@@ -825,7 +831,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and a_integer between 0 and 10";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -847,7 +853,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
         String tenantId = "000000000000001";
         String query = "select * from atable where organization_id='" + tenantId + "' and a_integer not between 0 and 10";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -875,7 +881,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
 
         String query = "select * from tenant_filter_test where a_integer=0 and a_string='foo'";
         PhoenixConnection pconn = DriverManager.getConnection(url, PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -907,7 +913,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
 
         String query = "select * from tenant_filter_test where a_integer=0 and a_string='foo'";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(tenantId), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         Filter filter = scan.getFilter();
@@ -934,7 +940,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
     public void testScanCaching_Default() throws SQLException {
         String query = "select * from atable where a_integer=0";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         QueryPlan plan = pstmt.optimizeQuery();
         Scan scan = plan.getContext().getScan();
         assertEquals(QueryServicesOptions.DEFAULT_SCAN_CACHE_SIZE, pstmt.getFetchSize());
@@ -945,7 +951,7 @@ public class WhereCompilerTest extends BaseConnectionlessQueryTest {
     public void testScanCaching_CustomFetchSizeOnStatement() throws SQLException {
         String query = "select * from atable where a_integer=0";
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
-        PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        PhoenixPreparedStatement pstmt = newPreparedStatement(pconn, query);
         final int FETCH_SIZE = 25;
         pstmt.setFetchSize(FETCH_SIZE);
         QueryPlan plan = pstmt.optimizeQuery();

http://git-wip-us.apache.org/repos/asf/phoenix/blob/abeaa74a/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereOptimizerTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereOptimizerTest.java b/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereOptimizerTest.java
index ad8e5f5..ddbacb7 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereOptimizerTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/compile/WhereOptimizerTest.java
@@ -54,8 +54,8 @@ import org.apache.phoenix.jdbc.PhoenixPreparedStatement;
 import org.apache.phoenix.query.BaseConnectionlessQueryTest;
 import org.apache.phoenix.query.KeyRange;
 import org.apache.phoenix.query.QueryConstants;
-import org.apache.phoenix.schema.types.PChar;
 import org.apache.phoenix.schema.ColumnNotFoundException;
+import org.apache.phoenix.schema.types.PChar;
 import org.apache.phoenix.schema.types.PDate;
 import org.apache.phoenix.schema.types.PUnsignedLong;
 import org.apache.phoenix.schema.types.PVarchar;
@@ -86,6 +86,7 @@ public class WhereOptimizerTest extends BaseConnectionlessQueryTest {
     private static StatementContext compileStatement(String query, List<Object> binds, Integer limit) throws SQLException {
         PhoenixConnection pconn = DriverManager.getConnection(getUrl(), PropertiesUtil.deepCopy(TEST_PROPERTIES)).unwrap(PhoenixConnection.class);
         PhoenixPreparedStatement pstmt = new PhoenixPreparedStatement(pconn, query);
+        assertRoundtrip(query);
         TestUtil.bindParams(pstmt, binds);
         QueryPlan plan = pstmt.compileQuery();
         assertEquals(limit, plan.getLimit());