phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jamestay...@apache.org
Subject [4/4] phoenix git commit: PHOENIX-1704 Add year() built-in function (Alicia Ying Shu)
Date Tue, 17 Mar 2015 17:38:26 GMT
PHOENIX-1704 Add year() built-in function (Alicia Ying Shu)


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

Branch: refs/heads/master
Commit: 3e31ba61f2284d61c835ab396208b2ce8a94f69d
Parents: 95aa6db
Author: James Taylor <jtaylor@salesforce.com>
Authored: Tue Mar 17 10:37:43 2015 -0700
Committer: James Taylor <jtaylor@salesforce.com>
Committed: Tue Mar 17 10:37:43 2015 -0700

----------------------------------------------------------------------
 .../end2end/YearMonthSecondFunctionIT.java      | 125 +++++++++++++++++++
 .../phoenix/expression/ExpressionType.java      |   2 +
 .../expression/function/YearFunction.java       |  82 ++++++++++++
 3 files changed, 209 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/3e31ba61/phoenix-core/src/it/java/org/apache/phoenix/end2end/YearMonthSecondFunctionIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/YearMonthSecondFunctionIT.java
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/YearMonthSecondFunctionIT.java
new file mode 100644
index 0000000..d19314a
--- /dev/null
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/YearMonthSecondFunctionIT.java
@@ -0,0 +1,125 @@
+/*
+ * 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.end2end;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class YearMonthSecondFunctionIT extends BaseHBaseManagedTimeIT {
+    private Connection conn;
+
+    @Before
+    public void setUp() throws SQLException {
+        conn = DriverManager.getConnection(getUrl());
+    }
+
+    @After
+    public void tearDown() throws SQLException {
+        conn.close();
+    }
+
+    private static int callYearFunction(Connection conn, String invocation) throws SQLException
{
+        Statement stmt = conn.createStatement();
+        ResultSet rs =
+                stmt.executeQuery(String
+                    .format("SELECT %s FROM SYSTEM.CATALOG LIMIT 1", invocation));
+        assertTrue(rs.next());
+        int returnValue = rs.getInt(1);
+        assertFalse(rs.next());
+        rs.close();
+        stmt.close();
+        return returnValue;
+    }
+
+    private int callYearFunction(String invocation) throws SQLException {
+        return callYearFunction(conn, invocation);
+    }
+
+    @Test
+    public void testYearFunctionDate() throws SQLException {
+
+        assertEquals(2015, callYearFunction("YEAR(current_date())"));
+
+        assertEquals(2008, callYearFunction("YEAR(TO_DATE('2008-01-01', 'yyyy-MM-dd', 'local'))"));
+
+        assertEquals(2004,
+            callYearFunction("YEAR(TO_DATE('2004-12-13 10:13:18', 'yyyy-MM-dd hh:mm:ss'))"));
+
+        assertEquals(2015, callYearFunction("YEAR(TO_DATE('2015-01-27T16:17:57+00:00'))"));
+
+        assertEquals(2005, callYearFunction("YEAR(TO_DATE('2005-12-13 10:13:18'))"));
+
+        assertEquals(2006, callYearFunction("YEAR(TO_DATE('2006-12-13'))"));
+
+        assertEquals(2015, callYearFunction("YEAR(TO_DATE('2015-W05'))"));
+
+        assertEquals(
+            2008,
+            callYearFunction("YEAR(TO_DATE('Sat, 3 Feb 2008 03:05:06 GMT', 'EEE, d MMM yyyy
HH:mm:ss z', 'UTC'))"));
+    }
+
+    @Test
+    public void testYearFunctionTimestamp() throws SQLException {
+
+        assertEquals(2015, callYearFunction("YEAR(TO_TIMESTAMP('2015-01-27T16:17:57+00:00'))"));
+
+        assertEquals(2015, callYearFunction("YEAR(TO_TIMESTAMP('2015-01-27T16:17:57Z'))"));
+
+        assertEquals(2015, callYearFunction("YEAR(TO_TIMESTAMP('2015-W10-3'))"));
+
+        assertEquals(2015, callYearFunction("YEAR(TO_TIMESTAMP('2015-W05'))"));
+
+        assertEquals(2015, callYearFunction("YEAR(TO_TIMESTAMP('2015-063'))"));
+
+        assertEquals(2006, callYearFunction("YEAR(TO_TIMESTAMP('2006-12-13'))"));
+
+        assertEquals(2004,
+            callYearFunction("YEAR(TO_TIMESTAMP('2004-12-13 10:13:18', 'yyyy-MM-dd hh:mm:ss'))"));
+
+        assertEquals(
+            2008,
+            callYearFunction("YEAR(TO_TIMESTAMP('Sat, 3 Feb 2008 03:05:06 GMT', 'EEE, d MMM
yyyy HH:mm:ss z', 'UTC'))"));
+    }
+
+    @Test
+    public void testYearFuncAgainstColumns() throws Exception {
+        String ddl =
+                "CREATE TABLE IF NOT EXISTS T1 (k1 INTEGER NOT NULL, dates DATE, timestamps
TIMESTAMP, times TIME, " +
+                        "unsignedDates UNSIGNED_DATE, unsignedTimestamps UNSIGNED_TIMESTAMP,
unsignedTimes UNSIGNED_TIME CONSTRAINT pk PRIMARY KEY (k1))";
+        conn.createStatement().execute(ddl);
+        String dml = "UPSERT INTO T1 VALUES (1, TO_DATE('2004-03-01 00:00:00'), TO_TIMESTAMP('2006-02-01
00:00:00'), TO_TIME('2008-02-01 00:00:00'), " +
+                "TO_DATE('2010-03-01 00:00:00'), TO_TIMESTAMP('2012-02-01'), TO_TIME('2015-02-01
00:00:00'))";
+        conn.createStatement().execute(dml);
+        conn.commit();
+
+        ResultSet rs = conn.createStatement().executeQuery("SELECT k1, YEAR(timestamps),
YEAR(times), Year(unsignedDates), YEAR(unsignedTimestamps), " +
+                "YEAR(unsignedTimes) FROM T1 where YEAR(dates) = 2004");
+        assertTrue(rs.next());
+        assertEquals(1, rs.getInt(1));
+        assertEquals(2006, rs.getInt(2));
+        assertEquals(2008, rs.getInt(3));
+        assertEquals(2010, rs.getInt(4));
+        assertEquals(2012, rs.getInt(5));
+        assertEquals(2015, rs.getInt(6));
+        assertFalse(rs.next());
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/phoenix/blob/3e31ba61/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 e2db117..d02e3ce 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
@@ -82,6 +82,7 @@ import org.apache.phoenix.expression.function.ToTimestampFunction;
 import org.apache.phoenix.expression.function.TrimFunction;
 import org.apache.phoenix.expression.function.TruncFunction;
 import org.apache.phoenix.expression.function.UpperFunction;
+import org.apache.phoenix.expression.function.YearFunction;
 
 import com.google.common.collect.Maps;
 
@@ -194,6 +195,7 @@ public enum ExpressionType {
     ToTimeFunction(ToTimeFunction.class),
     ToTimestampFunction(ToTimestampFunction.class),
     SignFunction(SignFunction.class),
+    YearFunction(YearFunction.class)
     ;
     
     ExpressionType(Class<? extends Expression> clazz) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/3e31ba61/phoenix-core/src/main/java/org/apache/phoenix/expression/function/YearFunction.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/YearFunction.java
b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/YearFunction.java
new file mode 100644
index 0000000..f1958e1
--- /dev/null
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/YearFunction.java
@@ -0,0 +1,82 @@
+/*
+ * 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.function;
+
+import java.sql.SQLException;
+import java.util.List;
+
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.phoenix.expression.Expression;
+import org.apache.phoenix.parse.FunctionParseNode.Argument;
+import org.apache.phoenix.parse.FunctionParseNode.BuiltInFunction;
+import org.apache.phoenix.schema.tuple.Tuple;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PInteger;
+import org.apache.phoenix.schema.types.PTimestamp;
+import org.joda.time.DateTime;
+
+/**
+ * 
+ * Implementation of the Year() buildin. Input Date/Timestamp.
+ * 
+ */
+@BuiltInFunction(name=YearFunction.NAME, 
+args={@Argument(allowedTypes={PTimestamp.class})})
+public class YearFunction extends ScalarFunction {
+    public static final String NAME = "YEAR";
+
+    public YearFunction() {
+    }
+
+    public YearFunction(List<Expression> children) throws SQLException {
+        super(children);
+    }
+
+    @Override
+    public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
+        Expression expression = getChildExpression();
+        if (!expression.evaluate(tuple, ptr)) {
+            return false;
+        }
+        if ( ptr.getLength() == 0) {
+            return true; //means null
+        }
+        long dateTime = expression.getDataType().getCodec().decodeLong(ptr, expression.getSortOrder());
+        DateTime dt = new DateTime(dateTime);
+        int year = dt.getYear();
+        PDataType returnType = getDataType();
+        byte[] byteValue = new byte[returnType.getByteSize()];
+        returnType.getCodec().encodeInt(year, byteValue, 0);
+        ptr.set(byteValue);
+        return true;
+    }
+
+    @Override
+    public PDataType getDataType() {
+        return PInteger.INSTANCE;
+    }
+
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+    private Expression getChildExpression() {
+        return children.get(0);
+    }
+}


Mime
View raw message