tkhurana commented on code in PR #2026:
URL: https://github.com/apache/phoenix/pull/2026#discussion_r1862741717


##########
phoenix-core/src/it/java/org/apache/phoenix/end2end/ViewUtilIT.java:
##########
@@ -443,4 +445,108 @@ public void testOrphanViewDetection() throws Exception {
         }
     }
 
+    /**
+     * Test {@link ViewUtil#getViewIndexIds(PhoenixConnection, String, 
boolean)} for a table which is not view index and ensure it throws {@link 
IllegalArgumentException}
+     * @throws IOException
+     * @throws SQLException
+     */
+    @Test(expected=IllegalArgumentException.class)
+    public void testGetViewIndexIdsForNonViewIndexTable() throws IOException, 
SQLException {
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+            ViewUtil.getViewIndexIds(conn.unwrap(PhoenixConnection.class), 
"TEST_TABLE", true);
+        }
+    }
+
+    /**
+     * Test {@link ViewUtil#getViewIndexIds(PhoenixConnection, String, 
boolean)} for a table with non-null schema
+     * @throws SQLException
+     * @throws IOException
+     */
+    @Test
+    public void testGetViewIndexIdsWithSchema() throws SQLException, 
IOException {
+        testGetViewIndexIds(true);
+    }
+
+    /**
+     * Test {@link ViewUtil#getViewIndexIds(PhoenixConnection, String, 
boolean)} for a table without schema
+     * @throws SQLException
+     * @throws IOException
+     */
+    @Test
+    public void testGetViewIndexIdsWithoutSchema() throws SQLException, 
IOException {
+        testGetViewIndexIds(false);
+    }
+
+    /**
+     * Helper method to test {@link 
ViewUtil#getViewIndexIds(PhoenixConnection, String, boolean)} method
+     * 1. Create a multi-tenant table
+     * 2. Create 2 global views (globalViewName1 & globalViewName2) and 5 
global view indexes (2 on globalViewName1 & 3 on globalViewName2)
+     * 3. Create 2 tenant views (tenantViewName1 & tenantViewName2) and 3 
tenant view indexes (1 on tenantViewName1 & 2 on tenantViewName2)
+     * 4. Get view index ids EXCLUDING tenant view indexes and ensure the 
count is 5 (5 global view indexes)
+     * 5. Get view index ids INCLUDING tenant view indexes and ensure the 
count is 8 (5 global view index + 3 tenant view indexes)
+     * @param includeSchema - if schema needs to be added while creating the 
table
+     * @throws IOException
+     * @throws SQLException
+     */
+    private void testGetViewIndexIds(boolean includeSchema) throws 
IOException, SQLException {
+        final String schemaPrefix = (includeSchema ? (generateUniqueName() + 
".") : "");
+        final String tableName = schemaPrefix + generateUniqueName();
+        final String globalViewName1 = schemaPrefix + generateUniqueName();
+        final String globalViewName2 = schemaPrefix + generateUniqueName();
+        final String globalViewIndex11 = generateUniqueName() + "_INDEX11";
+        final String globalViewIndex12 = generateUniqueName() + "_INDEX12";
+        final String globalViewIndex21 = generateUniqueName() + "_INDEX21";
+        final String globalViewIndex22 = generateUniqueName() + "_INDEX22";
+        final String globalViewIndex23 = generateUniqueName() + "_INDEX23";
+
+        final String tenantId = generateUniqueName();
+        Properties tenantProps = PropertiesUtil.deepCopy(TEST_PROPERTIES);
+        tenantProps.setProperty(TENANT_ID_ATTRIB, tenantId);
+        final String tenantViewName1 = schemaPrefix + generateUniqueName();
+        final String tenantViewName2 = schemaPrefix + generateUniqueName();
+        final String tenantViewIndex11 = generateUniqueName() + "_INDEX11";
+        final String tenantViewIndex21 = generateUniqueName() + "_INDEX21";
+        final String tenantViewIndex22 = generateUniqueName() + "_INDEX22";
+
+        final String createTableDDL = "CREATE TABLE " + tableName +
+                "(TENANT_ID CHAR(10) NOT NULL, ID CHAR(10) NOT NULL, NUM 
BIGINT " +
+                "CONSTRAINT PK PRIMARY KEY (TENANT_ID, ID)) MULTI_TENANT=true";
+        final String globalViewDDL = "CREATE VIEW %s (PK1 BIGINT, PK2 BIGINT) 
" +
+                "AS SELECT * FROM " + tableName + " WHERE NUM > -1";
+        final String viewIndexDDL = "CREATE INDEX %s ON %s (NUM DESC) INCLUDE 
(ID)";
+        final String tenantViewDDL = "CREATE VIEW %s AS SELECT * FROM %s";
+
+        try (Connection conn = DriverManager.getConnection(getUrl())) {
+
+            // Create data table, global views and global view indexes
+            conn.createStatement().execute(createTableDDL);
+            conn.createStatement().execute(String.format(globalViewDDL, 
globalViewName1));
+            conn.createStatement().execute(String.format(globalViewDDL, 
globalViewName2));
+            conn.createStatement().execute(String.format(viewIndexDDL, 
globalViewIndex11, globalViewName1));
+            conn.createStatement().execute(String.format(viewIndexDDL, 
globalViewIndex12, globalViewName1));
+            conn.createStatement().execute(String.format(viewIndexDDL, 
globalViewIndex21, globalViewName2));
+            conn.createStatement().execute(String.format(viewIndexDDL, 
globalViewIndex22, globalViewName2));
+            conn.createStatement().execute(String.format(viewIndexDDL, 
globalViewIndex23, globalViewName2));
+
+
+            // Create tenant views and tenant view indexes
+            try (Connection tenantConn = DriverManager.getConnection(getUrl(), 
tenantProps)) {
+                tenantConn.createStatement().execute(
+                        String.format(tenantViewDDL, tenantViewName1, 
tableName));
+                tenantConn.createStatement().execute(
+                        String.format(tenantViewDDL, tenantViewName2, 
tableName));
+                
tenantConn.createStatement().execute(String.format(viewIndexDDL, 
tenantViewIndex11, tenantViewName1));
+                
tenantConn.createStatement().execute(String.format(viewIndexDDL, 
tenantViewIndex21, tenantViewName2));
+                
tenantConn.createStatement().execute(String.format(viewIndexDDL, 
tenantViewIndex22, tenantViewName2));
+            }
+
+            // Get view indexes ids only for global view indexes (excluding 
tenant view indexes)
+            List<String> list = 
ViewUtil.getViewIndexIds(conn.unwrap(PhoenixConnection.class), "_IDX_" + 
tableName, false);

Review Comment:
   use `MetatadataUtil.getViewIndexPhysicalName` instead of using hardcoded 
constants _IDX



-- 
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: issues-unsubscr...@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to