phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maryann...@apache.org
Subject [31/40] phoenix git commit: PHOENIX-2142 Implement octet_length build-in function for BINARY and VARBINARY(Shuxiong Ye)
Date Sun, 23 Aug 2015 17:15:56 GMT
PHOENIX-2142 Implement octet_length build-in function for BINARY and VARBINARY(Shuxiong Ye)


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

Branch: refs/heads/calcite
Commit: 274eb42d41c3c545f28491d845ec6e0b20bdbe5b
Parents: c435bba
Author: Samarth <samarth.jain@salesforce.com>
Authored: Wed Aug 12 09:00:57 2015 -0700
Committer: Samarth <samarth.jain@salesforce.com>
Committed: Wed Aug 12 09:00:57 2015 -0700

----------------------------------------------------------------------
 .../end2end/OctetLengthFunctionEnd2EndIT.java   | 73 ++++++++++++++++++++
 .../phoenix/expression/ExpressionType.java      |  4 +-
 .../function/OctetLengthFunction.java           | 66 ++++++++++++++++++
 .../phoenix/schema/types/PBinaryBase.java       | 12 ++++
 .../expression/OctetLengthFunctionTest.java     | 67 ++++++++++++++++++
 5 files changed, 221 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/274eb42d/phoenix-core/src/it/java/org/apache/phoenix/end2end/OctetLengthFunctionEnd2EndIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/OctetLengthFunctionEnd2EndIT.java
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/OctetLengthFunctionEnd2EndIT.java
new file mode 100644
index 0000000..1239e7a
--- /dev/null
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/OctetLengthFunctionEnd2EndIT.java
@@ -0,0 +1,73 @@
+/*
+ * 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.apache.phoenix.util.TestUtil.closeStmtAndConn;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+
+import org.apache.phoenix.expression.function.OctetLengthFunction;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * End to end tests for {@link OctetLengthFunction}
+ */
+public class OctetLengthFunctionEnd2EndIT extends BaseHBaseManagedTimeIT {
+
+    private static final String KEY = "key";
+
+    @Before
+    public void initTable() throws Exception {
+        Connection conn = null;
+        PreparedStatement stmt = null;
+        try {
+            conn = DriverManager.getConnection(getUrl());
+            String ddl;
+            ddl = "CREATE TABLE ta (k VARCHAR NOT NULL PRIMARY KEY, b BINARY(4), vb VARBINARY)";
+            conn.createStatement().execute(ddl);
+            conn.commit();
+        } finally {
+            closeStmtAndConn(stmt, conn);
+        }
+    }
+
+    @Test
+    public void test() throws Exception {
+        Connection conn = DriverManager.getConnection(getUrl());
+        PreparedStatement stmt = conn.prepareStatement("UPSERT INTO ta VALUES (?, ?, ?)");
+        stmt.setString(1, KEY);
+        stmt.setBytes(2, new byte[] { 1, 2, 3, 4 });
+        stmt.setBytes(3, new byte[] { 1, 2, 3, 4 });
+        stmt.executeUpdate();
+        conn.commit();
+        ResultSet rs =
+                conn.createStatement()
+                        .executeQuery(
+                            "SELECT OCTET_LENGTH(vb), OCTET_LENGTH(b) FROM ta WHERE OCTET_LENGTH(vb)=4
and OCTET_LENGTH(b)=4");
+        assertTrue(rs.next());
+        assertEquals(4, rs.getInt(1));
+        assertEquals(4, rs.getInt(2));
+        assertTrue(!rs.next());
+    }
+}

http://git-wip-us.apache.org/repos/asf/phoenix/blob/274eb42d/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 5dbab69..dbe2509 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
@@ -72,6 +72,7 @@ import org.apache.phoenix.expression.function.MinuteFunction;
 import org.apache.phoenix.expression.function.MonthFunction;
 import org.apache.phoenix.expression.function.NowFunction;
 import org.apache.phoenix.expression.function.NthValueFunction;
+import org.apache.phoenix.expression.function.OctetLengthFunction;
 import org.apache.phoenix.expression.function.PercentRankAggregateFunction;
 import org.apache.phoenix.expression.function.PercentileContAggregateFunction;
 import org.apache.phoenix.expression.function.PercentileDiscAggregateFunction;
@@ -261,7 +262,8 @@ public enum ExpressionType {
     GetByteFunction(GetByteFunction.class),
     SetByteFunction(SetByteFunction.class),
     GetBitFunction(GetBitFunction.class),
-    SetBitFunction(SetBitFunction.class)
+    SetBitFunction(SetBitFunction.class),
+    OctetLengthFunction(OctetLengthFunction.class),
     ;
 
     ExpressionType(Class<? extends Expression> clazz) {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/274eb42d/phoenix-core/src/main/java/org/apache/phoenix/expression/function/OctetLengthFunction.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/expression/function/OctetLengthFunction.java
b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/OctetLengthFunction.java
new file mode 100644
index 0000000..ef20734
--- /dev/null
+++ b/phoenix-core/src/main/java/org/apache/phoenix/expression/function/OctetLengthFunction.java
@@ -0,0 +1,66 @@
+/*
+ * 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.PBinary;
+import org.apache.phoenix.schema.types.PBinaryBase;
+import org.apache.phoenix.schema.types.PDataType;
+import org.apache.phoenix.schema.types.PInteger;
+import org.apache.phoenix.schema.types.PVarbinary;
+
+@BuiltInFunction(name = OctetLengthFunction.NAME, args = { @Argument(allowedTypes = {
+        PBinary.class, PVarbinary.class }), })
+public class OctetLengthFunction extends ScalarFunction {
+
+    public static final String NAME = "OCTET_LENGTH";
+
+    public OctetLengthFunction() {
+    }
+
+    public OctetLengthFunction(List<Expression> children) throws SQLException {
+        super(children);
+    }
+
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+    @Override
+    public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
+        // get binary data parameter
+        Expression dataExpr = children.get(0);
+        if (!dataExpr.evaluate(tuple, ptr)) return false;
+        // set result
+        ((PBinaryBase) dataExpr.getDataType()).octetLength(ptr, dataExpr.getSortOrder(),
ptr);
+        return true;
+    }
+
+    @Override
+    public PDataType getDataType() {
+        return PInteger.INSTANCE;
+    }
+}

http://git-wip-us.apache.org/repos/asf/phoenix/blob/274eb42d/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBinaryBase.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBinaryBase.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBinaryBase.java
index ec0793e..0ad4ce1 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBinaryBase.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PBinaryBase.java
@@ -83,4 +83,16 @@ public abstract class PBinaryBase extends PDataType<byte[]> {
         ret = (byte) ((ret & (~(1 << (off % Byte.SIZE)))) | (newValue <<
(off % Byte.SIZE)));
         setByte(bytes, offset, length, sortOrder, off / Byte.SIZE, ret, outPtr);
     }
+
+    public void octetLength(ImmutableBytesWritable ptr, SortOrder sortOrder,
+            ImmutableBytesWritable outPtr) {
+        octetLength(ptr.get(), ptr.getOffset(), ptr.getLength(), sortOrder, outPtr);
+    }
+
+    public void octetLength(byte[] bytes, int offset, int length, SortOrder sortOrder,
+            ImmutableBytesWritable outPtr) {
+        bytes = new byte[PInteger.INSTANCE.getByteSize()];
+        PInteger.INSTANCE.getCodec().encodeInt(length, bytes, 0);
+        outPtr.set(bytes);
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/274eb42d/phoenix-core/src/test/java/org/apache/phoenix/expression/OctetLengthFunctionTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/expression/OctetLengthFunctionTest.java
b/phoenix-core/src/test/java/org/apache/phoenix/expression/OctetLengthFunctionTest.java
new file mode 100644
index 0000000..9211777
--- /dev/null
+++ b/phoenix-core/src/test/java/org/apache/phoenix/expression/OctetLengthFunctionTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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 static org.junit.Assert.assertEquals;
+
+import java.sql.SQLException;
+import java.util.List;
+
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.phoenix.expression.function.OctetLengthFunction;
+import org.apache.phoenix.schema.SortOrder;
+import org.apache.phoenix.schema.types.PBinary;
+import org.apache.phoenix.schema.types.PBinaryBase;
+import org.apache.phoenix.schema.types.PVarbinary;
+import org.junit.Test;
+
+import com.google.common.collect.Lists;
+
+/**
+ * Unit tests for {@link OctetLengthFunction}
+ */
+public class OctetLengthFunctionTest {
+    private void testOctetLengthExpression(Expression data, int expected) throws SQLException
{
+        List<Expression> expressions = Lists.newArrayList(data);
+        Expression octetLengthFunction = new OctetLengthFunction(expressions);
+        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
+        octetLengthFunction.evaluate(null, ptr);
+        Integer result =
+                (Integer) octetLengthFunction.getDataType().toObject(ptr,
+                    octetLengthFunction.getSortOrder());
+        assertEquals(expected, result.intValue());
+    }
+
+    private void testOctetLength(byte[] bytes, PBinaryBase dataType, int expected)
+            throws SQLException {
+        LiteralExpression dataExpr;
+        dataExpr = LiteralExpression.newConstant(bytes, dataType, SortOrder.ASC);
+        testOctetLengthExpression(dataExpr, expected);
+        dataExpr = LiteralExpression.newConstant(bytes, dataType, SortOrder.DESC);
+        testOctetLengthExpression(dataExpr, expected);
+    }
+
+    @Test
+    public void testByteBatch() throws SQLException {
+        for (int len = 0; len < 300; ++len) {
+            byte[] bytes = new byte[len];
+            testOctetLength(bytes, PBinary.INSTANCE, bytes.length);
+            testOctetLength(bytes, PVarbinary.INSTANCE, bytes.length);
+        }
+    }
+}


Mime
View raw message