palashc commented on code in PR #2126:
URL: https://github.com/apache/phoenix/pull/2126#discussion_r2060707252


##########
phoenix-core/src/it/java/org/apache/phoenix/end2end/SubBinaryFunctionIT.java:
##########
@@ -0,0 +1,324 @@
+package org.apache.phoenix.end2end;
+
+import org.apache.phoenix.util.QueryUtil;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+@Category(ParallelStatsDisabledTest.class)
+public class SubBinaryFunctionIT extends ParallelStatsDisabledIT {
+
+    @Test
+    public void testBinary() throws Exception {
+        String tableName = generateUniqueName();
+        Connection conn = DriverManager.getConnection(getUrl());
+        conn.createStatement().execute("CREATE TABLE " + tableName + "(" +
+                "    id INTEGER NOT NULL,\n" +
+                "    BIN_PK BINARY(4) NOT NULL,\n" +
+                "    BIN_COL BINARY(8) \n" +
+                "    CONSTRAINT pk PRIMARY KEY (id, BIN_PK)" +
+                ")");
+
+        byte[] b11 = new byte[] {83, -101, -102, 91};
+        byte[] b12 = new byte[] {4, 1, -19, 8, 0, -73, 3, 4};
+        byte[] b21 = new byte[] {-1, 1, 20, -28,};
+        byte[] b22 = new byte[] {10, 55, 0, 19, -5, -34, 0, 0};
+        PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + 
tableName + " VALUES(?, ?, ?)");
+        upsertRow(stmt, 1, b11, b12);
+        upsertRow(stmt, 2, b21, b22);
+        conn.commit();
+
+        ResultSet rs = conn.createStatement().executeQuery("SELECT 
SUBBINARY(BIN_PK, 1, 3), SUBBINARY(BIN_COL, 0, 4) FROM " + tableName);
+        rs.next();
+        assertSubBinary(b11, rs.getBytes(1), 0, 3);
+        assertSubBinary(b12, rs.getBytes(2), 0, 4);
+        rs.next();
+        assertSubBinary(b21, rs.getBytes(1), 0, 3);
+        assertSubBinary(b22, rs.getBytes(2), 0, 4);
+
+        rs = conn.createStatement().executeQuery("SELECT SUBBINARY(BIN_PK, 2), 
SUBBINARY(BIN_COL, 5) FROM " + tableName);
+        rs.next();
+        assertSubBinary(b11, rs.getBytes(1), 1, 3);
+        assertSubBinary(b12, rs.getBytes(2), 4, 4);
+        rs.next();
+        assertSubBinary(b21, rs.getBytes(1), 1, 3);
+        assertSubBinary(b22, rs.getBytes(2), 4, 4);
+
+        rs = conn.createStatement().executeQuery("SELECT SUBBINARY(BIN_PK, -3, 
2), SUBBINARY(BIN_COL, -6, 3) FROM " + tableName);
+        rs.next();
+        assertSubBinary(b11, rs.getBytes(1), 1, 2);
+        assertSubBinary(b12, rs.getBytes(2), 2, 3);
+        rs.next();
+        assertSubBinary(b21, rs.getBytes(1), 1, 2);
+        assertSubBinary(b22, rs.getBytes(2), 2, 3);
+
+        rs = conn.createStatement().executeQuery("SELECT SUBBINARY(BIN_PK, 
-1), SUBBINARY(BIN_COL, -3) FROM " + tableName);
+        rs.next();
+        assertSubBinary(b11, rs.getBytes(1), 3, 1);
+        assertSubBinary(b12, rs.getBytes(2), 5, 3);
+        rs.next();
+        assertSubBinary(b21, rs.getBytes(1), 3, 1);
+        assertSubBinary(b22, rs.getBytes(2), 5, 3);
+
+        PreparedStatement stmt2 = conn.prepareStatement("SELECT id FROM " + 
tableName + " WHERE SUBBINARY(BIN_COL, 2, 6) = ?");
+        stmt2.setBytes(1, new byte[] {55, 0, 19, -5, -34, 0});
+        rs = stmt2.executeQuery();
+        Assert.assertTrue(rs.next());
+        Assert.assertEquals(2, rs.getInt(1));
+    }
+
+    @Test
+    public void testVarbinary() throws Exception {
+        String tableName = generateUniqueName();
+        Connection conn = DriverManager.getConnection(getUrl());
+        conn.createStatement().execute("CREATE TABLE " + tableName + "(" +
+                "    id INTEGER NOT NULL,\n" +
+                "    BIN_PK VARBINARY NOT NULL,\n" +
+                "    BIN_COL VARBINARY \n" +
+                "    CONSTRAINT pk PRIMARY KEY (id, BIN_PK)" +
+                ")");
+
+
+        byte[] b11 = new byte[] {56, 50, 19, 0, 34, 83, -101, -102, 91, 92};
+        byte[] b12 = new byte[] {10, 55, 0, 19, -5, -34, 0, -12, 0, 0, 0, 1};
+        byte[] b21 = new byte[] {-11, 55, -119, 0, 8, 0, 1, 2, -4, 33};
+        byte[] b22 = new byte[] {1, 1, 20, -28, 0, -1, 0, -11, -21, -1};
+        PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + 
tableName + " VALUES(?, ?, ?)");
+        upsertRow(stmt, 1, b11, b12);
+        upsertRow(stmt, 2, b21, b22);
+        conn.commit();
+
+        ResultSet rs = conn.createStatement().executeQuery("SELECT 
SUBBINARY(BIN_PK, 0, 4), SUBBINARY(BIN_COL, 1, 3) FROM " + tableName);
+        rs.next();
+        assertSubBinary(b11, rs.getBytes(1), 0, 4);
+        assertSubBinary(b12, rs.getBytes(2), 0, 3);
+        rs.next();
+        assertSubBinary(b21, rs.getBytes(1), 0, 4);
+        assertSubBinary(b22, rs.getBytes(2), 0, 3);
+
+        rs = conn.createStatement().executeQuery("SELECT SUBBINARY(BIN_PK, 5), 
SUBBINARY(BIN_COL, 7) FROM " + tableName);
+        rs.next();
+        assertSubBinary(b11, rs.getBytes(1), 4, 6);
+        assertSubBinary(b12, rs.getBytes(2), 6, 6);
+        rs.next();
+        assertSubBinary(b21, rs.getBytes(1), 4, 6);
+        assertSubBinary(b22, rs.getBytes(2), 6, 4);
+
+        rs = conn.createStatement().executeQuery("SELECT SUBBINARY(BIN_PK, -4, 
3), SUBBINARY(BIN_COL, -3, 1) FROM " + tableName);
+        rs.next();
+        assertSubBinary(b11, rs.getBytes(1), 6, 3);
+        assertSubBinary(b12, rs.getBytes(2), 9, 1);
+        rs.next();
+        assertSubBinary(b21, rs.getBytes(1), 6, 3);
+        assertSubBinary(b22, rs.getBytes(2), 7, 1);
+
+        rs = conn.createStatement().executeQuery("SELECT SUBBINARY(BIN_PK, 
-2), SUBBINARY(BIN_COL, -2) FROM " + tableName);
+        rs.next();
+        assertSubBinary(b11, rs.getBytes(1), 8, 2);
+        assertSubBinary(b12, rs.getBytes(2), 10, 2);
+        rs.next();
+        assertSubBinary(b21, rs.getBytes(1), 8, 2);
+        assertSubBinary(b22, rs.getBytes(2), 8, 2);
+
+        PreparedStatement stmt2 = conn.prepareStatement("SELECT id FROM " + 
tableName + " WHERE SUBBINARY(BIN_COL, 2, 6) = ?");
+        stmt2.setBytes(1, new byte[] {1, 20, -28, 0, -1, 0});
+        rs = stmt2.executeQuery();
+        Assert.assertTrue(rs.next());
+        Assert.assertEquals(2, rs.getInt(1));
+    }
+
+    @Test
+    public void testVarbinaryEncoded() throws Exception {
+        String tableName = generateUniqueName();
+        Connection conn = DriverManager.getConnection(getUrl());
+        conn.createStatement().execute("CREATE TABLE " + tableName + "(" +
+                "    id INTEGER NOT NULL,\n" +
+                "    BIN_PK VARBINARY_ENCODED NOT NULL,\n" +
+                "    BIN_COL VARBINARY_ENCODED \n" +
+                "    CONSTRAINT pk PRIMARY KEY (id, BIN_PK)" +
+                ")");
+
+
+        byte[] b11 = new byte[] {56, 50, 19, 0, 34, 83, -101, -102, 91, 92};
+        byte[] b12 = new byte[] {10, 55, -1, 19, -5, -34, 0, -12, 0, 0, 0, 1};
+        byte[] b21 = new byte[] {-11, 55, -119, 0, 8, 0, 1, 2, -4, 33};
+        byte[] b22 = new byte[] {1, 1, 20, -28, 0, -1, 0, -11, -21, -1};
+        PreparedStatement stmt = conn.prepareStatement("UPSERT INTO " + 
tableName + " VALUES(?, ?, ?)");
+        upsertRow(stmt, 1, b11, b12);
+        upsertRow(stmt, 2, b21, b22);

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to