phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jamestay...@apache.org
Subject [4/5] phoenix git commit: PHOENIX-653 Support ANSI-standard date literals from SQL 2003
Date Tue, 10 Feb 2015 02:37:38 GMT
PHOENIX-653 Support ANSI-standard date literals from SQL 2003


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/2d5913b8
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/2d5913b8
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/2d5913b8

Branch: refs/heads/master
Commit: 2d5913b80349179da5aa18a1abbb56c230ee0542
Parents: 11a76b2
Author: James Taylor <jtaylor@salesforce.com>
Authored: Sun Feb 8 20:46:46 2015 -0800
Committer: James Taylor <jtaylor@salesforce.com>
Committed: Mon Feb 9 18:37:14 2015 -0800

----------------------------------------------------------------------
 phoenix-core/src/main/antlr3/PhoenixSQL.g       | 17 ++++-
 .../expression/ArrayConstructorExpression.java  | 12 ++++
 .../phoenix/expression/LiteralExpression.java   | 17 +++--
 .../apache/phoenix/parse/ParseNodeFactory.java  | 14 ++++
 .../phoenix/schema/types/PArrayDataType.java    | 17 +++++
 .../apache/phoenix/schema/types/PBinary.java    | 15 ++--
 .../org/apache/phoenix/schema/types/PChar.java  | 14 ++--
 .../apache/phoenix/schema/types/PDataType.java  | 12 ++--
 .../org/apache/phoenix/schema/types/PDate.java  | 14 ++--
 .../apache/phoenix/schema/types/PDecimal.java   | 19 +++--
 .../org/apache/phoenix/schema/types/PTime.java  |  4 +-
 .../apache/phoenix/schema/types/PTimestamp.java |  4 +-
 .../phoenix/schema/types/PUnsignedDate.java     | 12 ++--
 .../phoenix/schema/types/PUnsignedTime.java     | 11 ++-
 .../schema/types/PUnsignedTimestamp.java        | 11 ++-
 .../apache/phoenix/schema/types/PVarbinary.java | 25 ++++---
 .../apache/phoenix/schema/types/PVarchar.java   | 15 ++--
 .../compile/StatementHintsCompilationTest.java  |  2 +-
 .../apache/phoenix/parse/QueryParserTest.java   | 76 ++++++++++++++++++++
 .../org/apache/phoenix/query/QueryPlanTest.java |  4 +-
 20 files changed, 242 insertions(+), 73 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/antlr3/PhoenixSQL.g
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/antlr3/PhoenixSQL.g b/phoenix-core/src/main/antlr3/PhoenixSQL.g
index fad5fb3..cda93fe 100644
--- a/phoenix-core/src/main/antlr3/PhoenixSQL.g
+++ b/phoenix-core/src/main/antlr3/PhoenixSQL.g
@@ -152,6 +152,12 @@ import org.apache.phoenix.schema.PTableType;
 import org.apache.phoenix.schema.PTable.IndexType;
 import org.apache.phoenix.schema.stats.StatisticsCollectionScope;
 import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PDate;
+import org.apache.phoenix.schema.types.PTime;
+import org.apache.phoenix.schema.types.PTimestamp;
+import org.apache.phoenix.schema.types.PUnsignedDate;
+import org.apache.phoenix.schema.types.PUnsignedTime;
+import org.apache.phoenix.schema.types.PUnsignedTimestamp;
 import org.apache.phoenix.util.SchemaUtil;
 import org.apache.phoenix.parse.LikeParseNode.LikeType;
 }
@@ -864,7 +870,9 @@ literal_or_bind returns [ParseNode ret]
 
 // Get a string, integer, double, date, boolean, or NULL value.
 literal returns [LiteralParseNode ret]
-    :   t=STRING_LITERAL { ret = factory.literal(t.getText()); }
+    :   t=STRING_LITERAL {
+            ret = factory.literal(t.getText()); 
+        }
     |   l=int_literal { ret = l; }
     |   l=long_literal { ret = l; }
     |   l=double_literal { ret = l; }
@@ -878,6 +886,13 @@ literal returns [LiteralParseNode ret]
     |   NULL {ret = factory.literal(null);}
     |   TRUE {ret = factory.literal(Boolean.TRUE);} 
     |   FALSE {ret = factory.literal(Boolean.FALSE);}
+    |   dt=identifier t=STRING_LITERAL { 
+            try {
+                ret = factory.literal(t.getText(), dt);
+            } catch (SQLException e) {
+                throw new RuntimeException(e);
+            }
+        }
     ;
     
 int_literal returns [LiteralParseNode ret]

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/java/org/apache/phoenix/expression/ArrayConstructorExpression.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/ArrayConstructorExpression.java
b/phoenix-core/src/main/java/org/apache/phoenix/expression/ArrayConstructorExpression.java
index 9b0ee8f..15cd14c 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/ArrayConstructorExpression.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/ArrayConstructorExpression.java
@@ -166,4 +166,16 @@ public class ArrayConstructorExpression extends BaseCompoundExpression
{
         }
         return t;
     }
+    
+    @Override
+    public String toString() {
+        StringBuilder buf = new StringBuilder(PArrayDataType.ARRAY_TYPE_SUFFIX + "[");
+        if (children.size()==0)
+            return buf.append("]").toString();
+        for (int i = 0; i < children.size() - 1; i++) {
+            buf.append(children.get(i) + ",");
+        }
+        buf.append(children.get(children.size()-1) + "]");
+        return buf.toString();
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
b/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
index e2bdc82..26c076c 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
@@ -25,17 +25,17 @@ import java.sql.SQLException;
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.io.WritableUtils;
 import org.apache.phoenix.expression.visitor.ExpressionVisitor;
-import org.apache.phoenix.schema.types.PChar;
+import org.apache.phoenix.schema.SortOrder;
+import org.apache.phoenix.schema.TypeMismatchException;
+import org.apache.phoenix.schema.tuple.Tuple;
 import org.apache.phoenix.schema.types.PBoolean;
+import org.apache.phoenix.schema.types.PChar;
 import org.apache.phoenix.schema.types.PDataType;
 import org.apache.phoenix.schema.types.PDate;
 import org.apache.phoenix.schema.types.PTime;
 import org.apache.phoenix.schema.types.PTimestamp;
 import org.apache.phoenix.schema.types.PVarchar;
 import org.apache.phoenix.schema.types.PhoenixArray;
-import org.apache.phoenix.schema.SortOrder;
-import org.apache.phoenix.schema.TypeMismatchException;
-import org.apache.phoenix.schema.tuple.Tuple;
 import org.apache.phoenix.util.ByteUtil;
 import org.apache.phoenix.util.StringUtil;
 
@@ -220,7 +220,14 @@ public class LiteralExpression extends BaseTerminalExpression {
     
     @Override
     public String toString() {
-        return value == null ? "null" : type.toStringLiteral(byteValue, null);
+        if (value == null) {
+            return "null";
+        }
+        // TODO: move into PDataType?
+        if (type.isCoercibleTo(PTimestamp.INSTANCE)) {
+            return type + " " + type.toStringLiteral(value, null);
+        }
+        return type.toStringLiteral(value, null);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/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 57507b8..c92dbb6 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
@@ -48,6 +48,7 @@ import org.apache.phoenix.schema.SortOrder;
 import org.apache.phoenix.schema.TypeMismatchException;
 import org.apache.phoenix.schema.stats.StatisticsCollectionScope;
 import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PTimestamp;
 import org.apache.phoenix.util.SchemaUtil;
 
 import com.google.common.collect.ListMultimap;
@@ -467,6 +468,19 @@ public class ParseNodeFactory {
             value = expectedType.toObject(value, actualType);
         }
         return new LiteralParseNode(value);
+        /*
+        Object typedValue = expectedType.toObject(value.toString());
+        return new LiteralParseNode(typedValue);
+        */
+    }
+
+    public LiteralParseNode literal(String value, String sqlTypeName) throws SQLException
{
+        PDataType expectedType = sqlTypeName == null ? null : PDataType.fromSqlTypeName(SchemaUtil.normalizeIdentifier(sqlTypeName));
+        if (expectedType == null || !expectedType.isCoercibleTo(PTimestamp.INSTANCE)) {
+            throw TypeMismatchException.newException(expectedType, PTimestamp.INSTANCE);
+        }
+        Object typedValue = expectedType.toObject(value);
+        return new LiteralParseNode(typedValue);
     }
 
     public LiteralParseNode coerce(LiteralParseNode literalNode, PDataType expectedType)
throws SQLException {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java
b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java
index 30fab95..c183b7a 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PArrayDataType.java
@@ -20,6 +20,7 @@ package org.apache.phoenix.schema.types;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.text.Format;
 
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.hbase.util.Bytes;
@@ -653,4 +654,20 @@ public abstract class PArrayDataType<T> extends PDataType<T>
{
         return instantiatePhoenixArray(baseType, array);
     }
 
+    @Override
+    public String toStringLiteral(Object o, Format formatter) {
+        StringBuilder buf = new StringBuilder(PArrayDataType.ARRAY_TYPE_SUFFIX + "[");
+        PhoenixArray array = (PhoenixArray)o;
+        PDataType baseType = PDataType.arrayBaseType(this);
+        int len = array.getDimensions();
+        if (len != 0)  {
+            for (int i = 0; i < len; i++) {
+                buf.append(baseType.toStringLiteral(array.getElement(i), null));
+                buf.append(',');
+            }
+            buf.setLength(buf.length()-1);
+        }
+        buf.append(']');
+        return buf.toString();
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBinary.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBinary.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBinary.java
index d188387..69d3796 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBinary.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBinary.java
@@ -17,15 +17,15 @@
  */
 package org.apache.phoenix.schema.types;
 
+import java.sql.Types;
+import java.text.Format;
+
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.hbase.util.Base64;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.exception.ValueTypeIncompatibleException;
 import org.apache.phoenix.schema.SortOrder;
 
-import java.sql.Types;
-import java.text.Format;
-
 public class PBinary extends PDataType<byte[]> {
 
   public static final PBinary INSTANCE = new PBinary();
@@ -176,13 +176,18 @@ public class PBinary extends PDataType<byte[]> {
 
   @Override
   public String toStringLiteral(byte[] b, int offset, int length, Format formatter) {
-    if (formatter == null && b.length == 1) {
-      return Integer.toString(0xFF & b[0]);
+    if (length == 1) {
+      return Integer.toString(0xFF & b[offset]);
     }
     return PVarbinary.INSTANCE.toStringLiteral(b, offset, length, formatter);
   }
 
   @Override
+  public String toStringLiteral(Object o, Format formatter) {
+    return toStringLiteral((byte[])o, 0, ((byte[]) o).length, formatter);
+  }
+
+  @Override
   public Object getSampleValue(Integer maxLength, Integer arrayLength) {
     return PVarbinary.INSTANCE.getSampleValue(maxLength, arrayLength);
   }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java
index 3100f89..aaee1ba 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PChar.java
@@ -17,16 +17,17 @@
  */
 package org.apache.phoenix.schema.types;
 
-import com.google.common.base.Strings;
+import java.sql.Types;
+import java.text.Format;
+import java.util.Arrays;
+
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.exception.ValueTypeIncompatibleException;
 import org.apache.phoenix.schema.SortOrder;
 import org.apache.phoenix.util.StringUtil;
 
-import java.sql.Types;
-import java.text.Format;
-import java.util.Arrays;
+import com.google.common.base.Strings;
 
 /**
  * Fixed length single byte characters
@@ -197,6 +198,11 @@ public class PChar extends PDataType<String> {
     }
 
     @Override
+    public String toStringLiteral(Object o, Format formatter) {
+      return PVarchar.INSTANCE.toStringLiteral(o, formatter);
+    }
+
+    @Override
     public Object getSampleValue(Integer maxLength, Integer arrayLength) {
       return PVarchar.INSTANCE.getSampleValue(maxLength, arrayLength);
     }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDataType.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDataType.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDataType.java
index 85e5711..8f46a3b 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDataType.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDataType.java
@@ -1091,10 +1091,14 @@ public abstract class PDataType<T> implements DataType<T>,
Comparable<PDataType<
 
   public String toStringLiteral(byte[] b, int offset, int length, Format formatter) {
     Object o = toObject(b, offset, length);
-    if (formatter != null) {
-      return formatter.format(o);
-    }
-    return o.toString();
+    return toStringLiteral(o, formatter);
+  }
+  
+  public String toStringLiteral(Object o, Format formatter) {
+      if (formatter != null) {
+          return formatter.format(o);
+        }
+        return o.toString();
   }
 
   private static final PhoenixArrayFactory DEFAULT_ARRAY_FACTORY = new PhoenixArrayFactory()
{

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/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 bbd0a35..fa070d3 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
@@ -147,13 +147,13 @@ public class PDate extends PDataType<Date> {
   }
 
   @Override
-  public String toStringLiteral(byte[] b, int offset, int length, Format formatter) {
-    if (formatter == null || formatter == DateUtil.DEFAULT_DATE_FORMATTER) {
-      // If default formatter has not been overridden,
-      // use one that displays milliseconds.
-      formatter = DateUtil.DEFAULT_MS_DATE_FORMATTER;
-    }
-    return "'" + super.toStringLiteral(b, offset, length, formatter) + "'";
+  public String toStringLiteral(Object o, Format formatter) {
+      if (formatter == null) {
+          // If default formatter has not been overridden,
+          // use one that displays milliseconds.
+          formatter = DateUtil.DEFAULT_DATE_FORMATTER;
+        }
+        return "'" + super.toStringLiteral(o, formatter) + "'";
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDecimal.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDecimal.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDecimal.java
index 6b2dc84..e90491b 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDecimal.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PDecimal.java
@@ -17,7 +17,11 @@
  */
 package org.apache.phoenix.schema.types;
 
-import com.google.common.base.Preconditions;
+import java.math.BigDecimal;
+import java.sql.Timestamp;
+import java.sql.Types;
+import java.text.Format;
+
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.query.QueryConstants;
@@ -25,10 +29,7 @@ import org.apache.phoenix.schema.SortOrder;
 import org.apache.phoenix.util.ByteUtil;
 import org.apache.phoenix.util.NumberUtil;
 
-import java.math.BigDecimal;
-import java.sql.Timestamp;
-import java.sql.Types;
-import java.text.Format;
+import com.google.common.base.Preconditions;
 
 public class PDecimal extends PDataType<BigDecimal> {
 
@@ -390,6 +391,14 @@ public class PDecimal extends PDataType<BigDecimal> {
   }
 
   @Override
+  public String toStringLiteral(Object o, Format formatter) {
+      if (formatter == null) {
+          return ((BigDecimal)o).toPlainString();
+        }
+        return super.toStringLiteral(o, formatter);
+  }
+
+  @Override
   public Object getSampleValue(Integer maxLength, Integer arrayLength) {
     return new BigDecimal((Long) PLong.INSTANCE.getSampleValue(maxLength, arrayLength));
   }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PTime.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PTime.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PTime.java
index 81cbaff..0cfb0e8 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PTime.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PTime.java
@@ -127,11 +127,11 @@ public class PTime extends PDataType<Time> {
   }
 
   @Override
-  public String toStringLiteral(byte[] b, int offset, int length, Format formatter) {
+  public String toStringLiteral(Object o, Format formatter) {
       if (formatter == null) {
           formatter = DateUtil.DEFAULT_TIME_FORMATTER;
         }
-        return "'" + super.toStringLiteral(b, offset, length, formatter) + "'";
+        return "'" + super.toStringLiteral(o, formatter) + "'";
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PTimestamp.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PTimestamp.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PTimestamp.java
index 8182e33..9a82cc0 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PTimestamp.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PTimestamp.java
@@ -180,11 +180,11 @@ public class PTimestamp extends PDataType<Timestamp> {
   }
 
   @Override
-  public String toStringLiteral(byte[] b, int offset, int length, Format formatter) {
+  public String toStringLiteral(Object o, Format formatter) {
       if (formatter == null) {
           formatter = DateUtil.DEFAULT_TIMESTAMP_FORMATTER;
         }
-        return "'" + super.toStringLiteral(b, offset, length, formatter) + "'";
+        return "'" + super.toStringLiteral(o, formatter) + "'";
   }
 
 

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedDate.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedDate.java
b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedDate.java
index 8b63fbb..a6b1bc3 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedDate.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedDate.java
@@ -17,14 +17,14 @@
  */
 package org.apache.phoenix.schema.types;
 
+import java.sql.Date;
+import java.sql.Types;
+import java.text.Format;
+
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.phoenix.schema.SortOrder;
 import org.apache.phoenix.util.DateUtil;
 
-import java.sql.Types;
-import java.sql.Date;
-import java.text.Format;
-
 public class PUnsignedDate extends PDataType<Date> {
 
   public static final PUnsignedDate INSTANCE = new PUnsignedDate();
@@ -109,14 +109,14 @@ public class PUnsignedDate extends PDataType<Date> {
   }
 
   @Override
-  public String toStringLiteral(byte[] b, int offset, int length, Format formatter) {
+  public String toStringLiteral(Object o, Format formatter) {
     // Can't delegate, as the super.toStringLiteral calls this.toBytes
     if (formatter == null || formatter == DateUtil.DEFAULT_DATE_FORMATTER) {
       // If default formatter has not been overridden,
       // use one that displays milliseconds.
       formatter = DateUtil.DEFAULT_MS_DATE_FORMATTER;
     }
-    return "'" + super.toStringLiteral(b, offset, length, formatter) + "'";
+    return "'" + super.toStringLiteral(o, formatter) + "'";
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedTime.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedTime.java
b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedTime.java
index f738f44..4173be1 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedTime.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedTime.java
@@ -17,13 +17,13 @@
  */
 package org.apache.phoenix.schema.types;
 
-import org.apache.hadoop.hbase.util.Bytes;
-import org.apache.phoenix.schema.SortOrder;
-
 import java.sql.Time;
 import java.sql.Types;
 import java.text.Format;
 
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.schema.SortOrder;
+
 public class PUnsignedTime extends PDataType<Time> {
 
   public static final PUnsignedTime INSTANCE = new PUnsignedTime();
@@ -103,6 +103,11 @@ public class PUnsignedTime extends PDataType<Time> {
   }
 
   @Override
+  public String toStringLiteral(Object o, Format formatter) {
+    return PUnsignedDate.INSTANCE.toStringLiteral(o, formatter);
+  }
+
+  @Override
   public int getResultSetSqlType() {
     return Types.TIME;
   }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedTimestamp.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedTimestamp.java
b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedTimestamp.java
index c13de56..450408f 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedTimestamp.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedTimestamp.java
@@ -114,14 +114,11 @@ public class PUnsignedTimestamp extends PDataType<Timestamp> {
   }
 
   @Override
-  public String toStringLiteral(byte[] b, int offset, int length, Format formatter) {
-    java.sql.Timestamp value = (java.sql.Timestamp) toObject(b, offset, length);
-    if (formatter == null || formatter == DateUtil.DEFAULT_DATE_FORMATTER) {
-      // If default formatter has not been overridden,
-      // use one that displays milliseconds.
-      formatter = DateUtil.DEFAULT_MS_DATE_FORMATTER;
+  public String toStringLiteral(Object o, Format formatter) {
+    if (formatter == null) {
+      formatter = DateUtil.DEFAULT_TIMESTAMP_FORMATTER;
     }
-    return "'" + super.toStringLiteral(b, offset, length, formatter) + "." + value.getNanos()
+ "'";
+    return "'" + super.toStringLiteral(o, formatter) + "'";
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarbinary.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarbinary.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarbinary.java
index 6ba4dc4..bb1d4c6 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarbinary.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PVarbinary.java
@@ -17,15 +17,15 @@
  */
 package org.apache.phoenix.schema.types;
 
+import java.sql.Types;
+import java.text.Format;
+
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.hbase.util.Base64;
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.phoenix.schema.SortOrder;
 import org.apache.phoenix.util.ByteUtil;
 
-import java.sql.Types;
-import java.text.Format;
-
 public class PVarbinary extends PDataType<byte[]> {
 
   public static final PVarbinary INSTANCE = new PVarbinary();
@@ -148,20 +148,25 @@ public class PVarbinary extends PDataType<byte[]> {
 
   @Override
   public String toStringLiteral(byte[] b, int o, int length, Format formatter) {
-    if (formatter != null) {
-      return formatter.format(b);
-    }
     StringBuilder buf = new StringBuilder();
     buf.append('[');
-    for (int i = 0; i < b.length; i++) {
-      buf.append(0xFF & b[i]);
-      buf.append(',');
+    if (length > 0) {
+        for (int i = o; i < length; i++) {
+          buf.append(0xFF & b[i]);
+          buf.append(',');
+        }
+        buf.setLength(buf.length()-1);
     }
-    buf.setCharAt(buf.length() - 1, ']');
+    buf.append(']');
     return buf.toString();
   }
 
   @Override
+  public String toStringLiteral(Object o, Format formatter) {
+      return toStringLiteral((byte[])o, 0, ((byte[]) o).length, formatter);
+  }
+  
+  @Override
   public Object getSampleValue(Integer maxLength, Integer arrayLength) {
     int length = maxLength != null && maxLength > 0 ? maxLength : 1;
     byte[] b = new byte[length];

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/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 9ecfb4e..6956942 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
@@ -17,14 +17,15 @@
  */
 package org.apache.phoenix.schema.types;
 
-import com.google.common.base.Preconditions;
+import java.sql.Types;
+import java.text.Format;
+
 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 java.sql.Types;
-import java.text.Format;
+import com.google.common.base.Preconditions;
 
 public class PVarchar extends PDataType<String> {
 
@@ -137,15 +138,11 @@ public class PVarchar extends PDataType<String> {
   }
 
   @Override
-  public String toStringLiteral(byte[] b, int offset, int length, Format formatter) {
-    while (b[length - 1] == 0) {
-      length--;
-    }
+  public String toStringLiteral(Object o, Format formatter) {
     if (formatter != null) {
-      Object o = toObject(b, offset, length);
       return "'" + formatter.format(o) + "'";
     }
-    return "'" + Bytes.toStringBinary(b, offset, length) + "'";
+    return "'" + o + "'";
   }
 
   private char[] sampleChars = new char[1];

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/test/java/org/apache/phoenix/compile/StatementHintsCompilationTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/compile/StatementHintsCompilationTest.java
b/phoenix-core/src/test/java/org/apache/phoenix/compile/StatementHintsCompilationTest.java
index 13e2860..7f8adfa 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/compile/StatementHintsCompilationTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/compile/StatementHintsCompilationTest.java
@@ -99,7 +99,7 @@ public class StatementHintsCompilationTest extends BaseConnectionlessQueryTest
{
         conn.createStatement().execute("create table eh (organization_id char(15) not null,parent_id
char(15) not null, created_date date not null, entity_history_id char(15) not null constraint
pk primary key (organization_id, parent_id, created_date, entity_history_id))");
         ResultSet rs = conn.createStatement().executeQuery("explain select /*+ RANGE_SCAN
*/ ORGANIZATION_ID, PARENT_ID, CREATED_DATE, ENTITY_HISTORY_ID from eh where ORGANIZATION_ID='111111111111111'
and SUBSTR(PARENT_ID, 1, 3) = 'foo' and CREATED_DATE >= TO_DATE ('2012-11-01 00:00:00')
and CREATED_DATE < TO_DATE ('2012-11-30 00:00:00') order by ORGANIZATION_ID, PARENT_ID,
CREATED_DATE DESC, ENTITY_HISTORY_ID limit 100");
         assertEquals("CLIENT PARALLEL 1-WAY RANGE SCAN OVER EH ['111111111111111','foo  
         ','2012-11-01 00:00:00.000'] - ['111111111111111','fop            ','2012-11-30 00:00:00.000']\n"
+ 
-                "    SERVER FILTER BY FIRST KEY ONLY AND (CREATED_DATE >= '2012-11-01
00:00:00.000' AND CREATED_DATE < '2012-11-30 00:00:00.000')\n" + 
+                "    SERVER FILTER BY FIRST KEY ONLY AND (CREATED_DATE >= DATE '2012-11-01
00:00:00.000' AND CREATED_DATE < DATE '2012-11-30 00:00:00.000')\n" + 
                 "    SERVER TOP 100 ROWS SORTED BY [ORGANIZATION_ID, PARENT_ID, CREATED_DATE
DESC, ENTITY_HISTORY_ID]\n" + 
                 "CLIENT MERGE SORT",QueryUtil.getExplainPlan(rs));
     }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/test/java/org/apache/phoenix/parse/QueryParserTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/parse/QueryParserTest.java b/phoenix-core/src/test/java/org/apache/phoenix/parse/QueryParserTest.java
index 201172b..866365a 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/parse/QueryParserTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/parse/QueryParserTest.java
@@ -677,4 +677,80 @@ public class QueryParserTest {
                 new StringReader("select * from date_test where d in (to_date('2013-11-04
09:12:00'))"));
         parser.parseStatement();
     }
+    
+    @Test
+    public void testDateLiteral() throws Exception {
+        SQLParser parser = new SQLParser(
+                new StringReader(
+                        "select * from t where d = DATE '2013-11-04 09:12:00'"));
+        parser.parseStatement();
+    }
+
+    @Test
+    public void testTimeLiteral() throws Exception {
+        SQLParser parser = new SQLParser(
+                new StringReader(
+                        "select * from t where d = TIME '2013-11-04 09:12:00'"));
+        parser.parseStatement();
+    }
+
+
+    @Test
+    public void testTimestampLiteral() throws Exception {
+        SQLParser parser = new SQLParser(
+                new StringReader(
+                        "select * from t where d = TIMESTAMP '2013-11-04 09:12:00'"));
+        parser.parseStatement();
+    }
+    
+    @Test
+    public void testUnsignedDateLiteral() throws Exception {
+        SQLParser parser = new SQLParser(
+                new StringReader(
+                        "select * from t where d = UNSIGNED_DATE '2013-11-04 09:12:00'"));
+        parser.parseStatement();
+    }
+
+    @Test
+    public void testUnsignedTimeLiteral() throws Exception {
+        SQLParser parser = new SQLParser(
+                new StringReader(
+                        "select * from t where d = UNSIGNED_TIME '2013-11-04 09:12:00'"));
+        parser.parseStatement();
+    }
+
+
+    @Test
+    public void testUnsignedTimestampLiteral() throws Exception {
+        SQLParser parser = new SQLParser(
+                new StringReader(
+                        "select * from t where d = UNSIGNED_TIMESTAMP '2013-11-04 09:12:00'"));
+        parser.parseStatement();
+    }
+    
+    @Test
+    public void testUnknownLiteral() throws Exception {
+        SQLParser parser = new SQLParser(
+                new StringReader(
+                        "select * from t where d = FOO '2013-11-04 09:12:00'"));
+        try {
+            parser.parseStatement();
+            fail();
+        } catch (SQLException e) {
+            assertEquals(SQLExceptionCode.ILLEGAL_DATA.getErrorCode(), e.getErrorCode());
+        }
+    }
+    
+    @Test
+    public void testUnsupportedLiteral() throws Exception {
+        SQLParser parser = new SQLParser(
+                new StringReader(
+                        "select * from t where d = DECIMAL '2013-11-04 09:12:00'"));
+        try {
+            parser.parseStatement();
+            fail();
+        } catch (SQLException e) {
+            assertEquals(SQLExceptionCode.TYPE_MISMATCH.getErrorCode(), e.getErrorCode());
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/2d5913b8/phoenix-core/src/test/java/org/apache/phoenix/query/QueryPlanTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/query/QueryPlanTest.java b/phoenix-core/src/test/java/org/apache/phoenix/query/QueryPlanTest.java
index 6139aa5..7ad3e25 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/query/QueryPlanTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/query/QueryPlanTest.java
@@ -52,12 +52,12 @@ public class QueryPlanTest extends BaseConnectionlessQueryTest {
 
                 "SELECT host FROM PTSDB WHERE inst IS NULL AND host IS NOT NULL AND date
>= to_date('2013-01-01')",
                 "CLIENT PARALLEL 1-WAY RANGE SCAN OVER PTSDB [null,not null]\n" + 
-                "    SERVER FILTER BY FIRST KEY ONLY AND DATE >= '2013-01-01 00:00:00.000'",
+                "    SERVER FILTER BY FIRST KEY ONLY AND DATE >= DATE '2013-01-01 00:00:00.000'",
 
                 // Since inst IS NOT NULL is unbounded, we won't continue optimizing
                 "SELECT host FROM PTSDB WHERE inst IS NOT NULL AND host IS NULL AND date
>= to_date('2013-01-01')",
                 "CLIENT PARALLEL 1-WAY RANGE SCAN OVER PTSDB [not null]\n" + 
-                "    SERVER FILTER BY FIRST KEY ONLY AND (HOST IS NULL AND DATE >= '2013-01-01
00:00:00.000')",
+                "    SERVER FILTER BY FIRST KEY ONLY AND (HOST IS NULL AND DATE >= DATE
'2013-01-01 00:00:00.000')",
 
                 "SELECT a_string,b_string FROM atable WHERE organization_id = '000000000000001'
AND entity_id = '000000000000002' AND x_integer = 2 AND a_integer < 5 ",
                 "CLIENT PARALLEL 1-WAY POINT LOOKUP ON 1 KEY OVER ATABLE\n" + 


Mime
View raw message