phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maryann...@apache.org
Subject phoenix git commit: PHOENIX-3669 YEAR/MONTH/DAY/HOUR/MINUTES/SECOND built-in functions do not work in Calcite-Phoenix
Date Thu, 23 Feb 2017 01:17:43 GMT
Repository: phoenix
Updated Branches:
  refs/heads/calcite 3c750bbec -> ad4df8597


PHOENIX-3669 YEAR/MONTH/DAY/HOUR/MINUTES/SECOND built-in functions do not work in Calcite-Phoenix


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

Branch: refs/heads/calcite
Commit: ad4df859760858801788c7720c2a5f1f71b181e3
Parents: 3c750bb
Author: maryannxue <maryann.xue@gmail.com>
Authored: Wed Feb 22 17:17:18 2017 -0800
Committer: maryannxue <maryann.xue@gmail.com>
Committed: Wed Feb 22 17:17:36 2017 -0800

----------------------------------------------------------------------
 .../apache/phoenix/calcite/CalciteUtils.java    |  21 ++-
 .../phoenix/expression/ExpressionType.java      |   1 +
 .../expression/ReinterpretCastExpression.java   | 171 +++++++++++++++++++
 .../visitor/BaseExpressionVisitor.java          |   6 +
 .../visitor/CloneExpressionVisitor.java         |   6 +
 .../expression/visitor/ExpressionVisitor.java   |   4 +
 .../StatelessTraverseAllExpressionVisitor.java  |   6 +
 .../StatelessTraverseNoExpressionVisitor.java   |   6 +
 8 files changed, 220 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/ad4df859/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java b/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java
index c9c8102..065ae36 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/calcite/CalciteUtils.java
@@ -79,8 +79,10 @@ import org.apache.phoenix.expression.LongAddExpression;
 import org.apache.phoenix.expression.LongDivideExpression;
 import org.apache.phoenix.expression.LongMultiplyExpression;
 import org.apache.phoenix.expression.LongSubtractExpression;
+import org.apache.phoenix.expression.ModulusExpression;
 import org.apache.phoenix.expression.NotExpression;
 import org.apache.phoenix.expression.OrExpression;
+import org.apache.phoenix.expression.ReinterpretCastExpression;
 import org.apache.phoenix.expression.StringBasedLikeExpression;
 import org.apache.phoenix.expression.TimestampAddExpression;
 import org.apache.phoenix.expression.TimestampSubtractExpression;
@@ -757,6 +759,21 @@ public class CalciteUtils {
                 }
             }
         });
+        EXPRESSION_MAP.put(SqlKind.REINTERPRET, new ExpressionFactory() {
+
+            @SuppressWarnings("rawtypes")
+            @Override
+            public Expression newExpression(RexNode node,
+                    PhoenixRelImplementor implementor) {                
+                List<Expression> children = convertChildren((RexCall) node, implementor);
+                PDataType targetType = relDataTypeToPDataType(node.getType());
+                try {
+                    return ReinterpretCastExpression.create(children.get(0), targetType);
+                } catch (SQLException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        });
         EXPRESSION_MAP.put(SqlKind.DEFAULT, new ExpressionFactory() {
             @Override
             public Expression newExpression(RexNode node, PhoenixRelImplementor implementor)
{
@@ -820,7 +837,9 @@ public class CalciteUtils {
                         return new UpperFunction(children);
                     } else if (op == SqlStdOperatorTable.COALESCE) {
                         return new CoalesceFunction(children);
-                    }
+                    } else if (op == SqlStdOperatorTable.MOD) {
+                        return new ModulusExpression(children);
+                    };
                 } catch (SQLException e) {
                     throw new RuntimeException(e);
                 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ad4df859/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java
b/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java
index 7fe93ca..fb6ea98 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/ExpressionType.java
@@ -164,6 +164,7 @@ public enum ExpressionType {
     ToCharFunction(ToCharFunction.class),
     ToNumberFunction(ToNumberFunction.class),
     CoerceFunction(CoerceExpression.class),
+    ReinterpretCastFunction(ReinterpretCastExpression.class),
     SubstrFunction(SubstrFunction.class),
     AndExpression(AndExpression.class),
     OrExpression(OrExpression.class),

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ad4df859/phoenix-core/src/main/java/org/apache/phoenix/expression/ReinterpretCastExpression.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/ReinterpretCastExpression.java
b/phoenix-core/src/main/java/org/apache/phoenix/expression/ReinterpretCastExpression.java
new file mode 100644
index 0000000..9e447bd
--- /dev/null
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/ReinterpretCastExpression.java
@@ -0,0 +1,171 @@
+/*
+ * 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.expression;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.List;
+
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.io.WritableUtils;
+import org.apache.phoenix.exception.SQLExceptionCode;
+import org.apache.phoenix.exception.SQLExceptionInfo;
+import org.apache.phoenix.expression.visitor.ExpressionVisitor;
+import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PDate;
+import org.apache.phoenix.schema.types.PInteger;
+import org.apache.phoenix.schema.types.PLong;
+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.schema.types.PDataType.PDataCodec;
+import org.apache.phoenix.util.DateUtil;
+
+import com.google.common.collect.ImmutableList;
+
+
+public class ReinterpretCastExpression extends BaseSingleExpression {
+    private PDataType toType;
+    private PDataCodec inputCodec;
+    
+    public ReinterpretCastExpression() {
+    }
+
+    public static Expression create(Expression expression, PDataType toType) throws SQLException
{
+        final PDataType sourceType = expression.getDataType();
+        if (sourceType == PDate.INSTANCE
+                || sourceType == PUnsignedDate.INSTANCE
+                || sourceType == PTime.INSTANCE
+                || sourceType == PUnsignedTime.INSTANCE) {
+            if (toType != PInteger.INSTANCE
+                    && toType != PLong.INSTANCE) {
+                throw new SQLExceptionInfo.Builder(SQLExceptionCode.TYPE_MISMATCH)
+                        .setMessage("Reinterpret cast cannot covert " + sourceType + " to
" + toType)
+                        .build().buildException();
+            }
+        } else if (sourceType == PTimestamp.INSTANCE
+                || sourceType == PUnsignedTimestamp.INSTANCE) {
+            if (toType != PLong.INSTANCE) {
+                throw new SQLExceptionInfo.Builder(SQLExceptionCode.TYPE_MISMATCH)
+                        .setMessage("Reinterpret cast cannot covert " + sourceType + " to
" + toType)
+                        .build().buildException();
+            }
+        }
+        return new ReinterpretCastExpression(ImmutableList.of(expression), toType);
+    }
+    
+    //Package protected for tests
+    ReinterpretCastExpression(List<Expression> children, PDataType toType) {
+        super(children);
+        this.toType = toType;
+        init();
+    }
+    
+    public ReinterpretCastExpression clone(List<Expression> children) {
+        return new ReinterpretCastExpression(children, this.getDataType());
+    }
+    
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = prime * result + toType.hashCode();
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) return true;
+        if (!super.equals(obj)) return false;
+        if (getClass() != obj.getClass()) return false;
+        ReinterpretCastExpression other = (ReinterpretCastExpression)obj;
+        return toType.equals(other.toType);
+    }
+
+    @Override
+    public void readFields(DataInput input) throws IOException {
+        super.readFields(input);
+        int ordinal = WritableUtils.readVInt(input);
+        toType = PDataType.values()[ordinal];
+        init();
+    }
+
+    @Override
+    public void write(DataOutput output) throws IOException {
+        super.write(output);
+        WritableUtils.writeVInt(output, toType.ordinal());
+    }
+
+    @Override
+    public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
+        final Expression child = getChild();
+        final PDataType childType = child.getDataType();
+        if (child.evaluate(tuple, ptr)) {
+            long dateTime = inputCodec.decodeLong(ptr, child.getSortOrder());
+            if (toType == PLong.INSTANCE) {
+                toType.getCodec().encodeLong(dateTime, ptr);
+            } else { // PInteger
+                if (childType == PDate.INSTANCE
+                        || childType == PUnsignedDate.INSTANCE) {
+                    int date = (int) dateTime / 86400000;
+                    toType.getCodec().encodeInt(date, ptr);
+                } else { // Time
+                    int time = (int) dateTime % 86400000;
+                    toType.getCodec().encodeInt(time, ptr);
+                }
+            }
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public PDataType getDataType() {
+        return toType;
+    }
+
+    @Override
+    public <T> T accept(ExpressionVisitor<T> visitor) {
+        List<T> l = acceptChildren(visitor, visitor.visitEnter(this));
+        T t = visitor.visitLeave(this, l);
+        if (t == null) {
+            t = visitor.defaultReturn(this, l);
+        }
+        return t;
+    }
+    
+    @Override
+    public String toString() {
+        StringBuilder buf = new StringBuilder("TO_" + toType.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();
+    }
+    
+    private void init() {
+        PDataType childType = getChild().getDataType();
+        inputCodec = DateUtil.getCodecFor(childType);
+    }
+}

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ad4df859/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/BaseExpressionVisitor.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/BaseExpressionVisitor.java
b/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/BaseExpressionVisitor.java
index 8e8b32d..5d8e451 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/BaseExpressionVisitor.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/BaseExpressionVisitor.java
@@ -35,6 +35,7 @@ import org.apache.phoenix.expression.ModulusExpression;
 import org.apache.phoenix.expression.MultiplyExpression;
 import org.apache.phoenix.expression.NotExpression;
 import org.apache.phoenix.expression.OrExpression;
+import org.apache.phoenix.expression.ReinterpretCastExpression;
 import org.apache.phoenix.expression.RowValueConstructorExpression;
 import org.apache.phoenix.expression.StringConcatExpression;
 import org.apache.phoenix.expression.SubtractExpression;
@@ -137,6 +138,11 @@ public abstract class BaseExpressionVisitor<E> implements ExpressionVisitor<E>
{
     }
     
     @Override
+    public Iterator<Expression> visitEnter(ReinterpretCastExpression node) {
+        return null;
+    }
+    
+    @Override
     public Iterator<Expression> visitEnter(ArrayConstructorExpression node) {
         return null;
     }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ad4df859/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/CloneExpressionVisitor.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/CloneExpressionVisitor.java
b/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/CloneExpressionVisitor.java
index 31c71c0..c7771f9 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/CloneExpressionVisitor.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/CloneExpressionVisitor.java
@@ -40,6 +40,7 @@ import org.apache.phoenix.expression.MultiplyExpression;
 import org.apache.phoenix.expression.NotExpression;
 import org.apache.phoenix.expression.OrExpression;
 import org.apache.phoenix.expression.ProjectedColumnExpression;
+import org.apache.phoenix.expression.ReinterpretCastExpression;
 import org.apache.phoenix.expression.RowKeyColumnExpression;
 import org.apache.phoenix.expression.RowValueConstructorExpression;
 import org.apache.phoenix.expression.StringConcatExpression;
@@ -185,6 +186,11 @@ public abstract class CloneExpressionVisitor extends TraverseAllExpressionVisito
     }
 
     @Override
+    public Expression visitLeave(ReinterpretCastExpression node, List<Expression> l)
{
+        return isCloneNode(node, l) ? node.clone(l) : node;
+    }
+
+    @Override
     public Expression visitLeave(ArrayConstructorExpression node, List<Expression>
l) {
         return isCloneNode(node, l) ? node.clone(l) : node;
     }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ad4df859/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/ExpressionVisitor.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/ExpressionVisitor.java
b/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/ExpressionVisitor.java
index 1503698..2d270f3 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/ExpressionVisitor.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/ExpressionVisitor.java
@@ -41,6 +41,7 @@ import org.apache.phoenix.expression.MultiplyExpression;
 import org.apache.phoenix.expression.NotExpression;
 import org.apache.phoenix.expression.OrExpression;
 import org.apache.phoenix.expression.ProjectedColumnExpression;
+import org.apache.phoenix.expression.ReinterpretCastExpression;
 import org.apache.phoenix.expression.RowKeyColumnExpression;
 import org.apache.phoenix.expression.RowValueConstructorExpression;
 import org.apache.phoenix.expression.StringConcatExpression;
@@ -106,6 +107,9 @@ public interface ExpressionVisitor<E> {
     
     public Iterator<Expression> visitEnter(CoerceExpression node);
     public E visitLeave(CoerceExpression node, List<E> l);
+    
+    public Iterator<Expression> visitEnter(ReinterpretCastExpression node);
+    public E visitLeave(ReinterpretCastExpression node, List<E> l);
 
     public Iterator<Expression> visitEnter(ArrayConstructorExpression node);
     public E visitLeave(ArrayConstructorExpression node, List<E> l);

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ad4df859/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/StatelessTraverseAllExpressionVisitor.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/StatelessTraverseAllExpressionVisitor.java
b/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/StatelessTraverseAllExpressionVisitor.java
index 534937c..2c0f4e1 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/StatelessTraverseAllExpressionVisitor.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/StatelessTraverseAllExpressionVisitor.java
@@ -39,6 +39,7 @@ import org.apache.phoenix.expression.MultiplyExpression;
 import org.apache.phoenix.expression.NotExpression;
 import org.apache.phoenix.expression.OrExpression;
 import org.apache.phoenix.expression.ProjectedColumnExpression;
+import org.apache.phoenix.expression.ReinterpretCastExpression;
 import org.apache.phoenix.expression.RowKeyColumnExpression;
 import org.apache.phoenix.expression.RowValueConstructorExpression;
 import org.apache.phoenix.expression.StringConcatExpression;
@@ -164,6 +165,11 @@ public class StatelessTraverseAllExpressionVisitor<E> extends TraverseAllExpress
     public E visitLeave(CoerceExpression node, List<E> l) {
         return null;
     }
+    
+    @Override
+    public E visitLeave(ReinterpretCastExpression node, List<E> l) {
+        return null;
+    }
 
     @Override
     public E visitLeave(ArrayConstructorExpression node, List<E> l) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ad4df859/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/StatelessTraverseNoExpressionVisitor.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/StatelessTraverseNoExpressionVisitor.java
b/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/StatelessTraverseNoExpressionVisitor.java
index 36e47f3..f108c46 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/StatelessTraverseNoExpressionVisitor.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/visitor/StatelessTraverseNoExpressionVisitor.java
@@ -39,6 +39,7 @@ import org.apache.phoenix.expression.MultiplyExpression;
 import org.apache.phoenix.expression.NotExpression;
 import org.apache.phoenix.expression.OrExpression;
 import org.apache.phoenix.expression.ProjectedColumnExpression;
+import org.apache.phoenix.expression.ReinterpretCastExpression;
 import org.apache.phoenix.expression.RowKeyColumnExpression;
 import org.apache.phoenix.expression.RowValueConstructorExpression;
 import org.apache.phoenix.expression.StringConcatExpression;
@@ -164,6 +165,11 @@ public class StatelessTraverseNoExpressionVisitor<E> extends TraverseNoExpressio
     public E visitLeave(CoerceExpression node, List<E> l) {
         return null;
     }
+    
+    @Override
+    public E visitLeave(ReinterpretCastExpression node, List<E> l) {
+        return null;
+    }
 
     @Override
     public E visitLeave(ArrayConstructorExpression node, List<E> l) {


Mime
View raw message