kadirozde commented on code in PR #1855:
URL: https://github.com/apache/phoenix/pull/1855#discussion_r1546622127


##########
phoenix-core/src/main/java/org/apache/phoenix/util/ViewUtil.java:
##########
@@ -1090,4 +1099,202 @@ public static void 
addTagsToPutsForViewAlteredProperties(List<Mutation> tableMet
 
         }
     }
+
+    /// TODO : Needs optimization and remove logging
+    public static List<TableTTLInfo> 
getMatchPatternsForPartitionedTables(String fullTableName,
+            Configuration configuration, boolean globalViews, boolean 
tenantViews,
+            boolean viewIndexes) throws SQLException {
+
+        List<TableTTLInfo> tableTTLInfoList = Lists.newArrayList();
+        if (globalViews || viewIndexes) {
+            Set<TableInfo> globalViewSet = getGlobalViews(fullTableName, 
configuration);
+            if (globalViewSet.size() > 0) {
+                getTTLInfo(globalViewSet, configuration, viewIndexes, 
tableTTLInfoList);
+            }
+        }
+
+        if (tenantViews || viewIndexes) {
+            Set<TableInfo> tenantViewSet = getTenantViews(fullTableName, 
configuration);
+            if (tenantViewSet.size() > 0) {
+                getTTLInfo(tenantViewSet, configuration, viewIndexes, 
tableTTLInfoList);
+            }
+        }
+
+        return tableTTLInfoList;
+    }
+
+    private static Set<TableInfo> getGlobalViews(String fullTableName, 
Configuration configuration)
+            throws SQLException {
+
+        Set<TableInfo> globalViewSet = new HashSet<>();
+        try (Connection serverConnection = QueryUtil.getConnectionOnServer(new 
Properties(),
+                configuration)) {
+            String
+                    globalViewsSQLFormat =
+                    "SELECT TENANT_ID, TABLE_SCHEM, TABLE_NAME, " +
+                            "COLUMN_NAME AS PHYSICAL_TABLE_TENANT_ID, " +
+                            "COLUMN_FAMILY AS PHYSICAL_TABLE_FULL_NAME " +
+                            "FROM SYSTEM.CATALOG " +
+                            "WHERE " + "LINK_TYPE = 2 " +
+                            "AND TABLE_TYPE IS NULL " +
+                            "AND COLUMN_FAMILY = '%s' " +
+                            "AND TENANT_ID IS NULL";
+            String globalViewSQL = String.format(globalViewsSQLFormat, 
fullTableName);
+            logger.debug("globalViewSQL:" + globalViewSQL);
+            try (PhoenixPreparedStatement globalViewStmt = 
serverConnection.prepareStatement(
+                    globalViewSQL).unwrap(PhoenixPreparedStatement.class)) {
+                try (ResultSet globalViewRS = globalViewStmt.executeQuery()) {
+                    while (globalViewRS.next()) {
+                        String tid = globalViewRS.getString("TENANT_ID");
+                        String schem = globalViewRS.getString("TABLE_SCHEM");
+                        String tName = globalViewRS.getString("TABLE_NAME");
+                        String tenantId = tid == null || tid.isEmpty() ? 
"NULL" : "'" + tid + "'";
+                        String
+                                schemCol =
+                                schem == null || schem.isEmpty() ? "NULL" : 
"'" + schem + "'";
+                        TableInfo
+                                tableInfo =
+                                new TableInfo(tenantId.getBytes(), 
schemCol.getBytes(),
+                                        tName.getBytes());
+                        globalViewSet.add(tableInfo);
+                    }
+                }
+            }
+        }
+        return globalViewSet;
+    }
+
+    private static Set<TableInfo> getTenantViews(String fullTableName, 
Configuration configuration)
+            throws SQLException {
+
+        Set<TableInfo> tenantViewSet = new HashSet<>();
+        try (Connection serverConnection = QueryUtil.getConnectionOnServer(new 
Properties(),
+                configuration)) {
+            String
+                    tenantViewsSQLFormat =
+                    "SELECT TENANT_ID,TABLE_SCHEM,TABLE_NAME," +
+                            "COLUMN_NAME AS PHYSICAL_TABLE_TENANT_ID, " +
+                            "COLUMN_FAMILY AS PHYSICAL_TABLE_FULL_NAME " +
+                            "FROM SYSTEM.CATALOG " +
+                            "WHERE LINK_TYPE = 2 " +
+                            "AND COLUMN_FAMILY = '%s' " +
+                            "AND TENANT_ID IS NOT NULL";
+            String tenantViewSQL = String.format(tenantViewsSQLFormat, 
fullTableName);
+            logger.debug("tenantViewSQL:" + tenantViewSQL);
+
+            try (PhoenixPreparedStatement tenantViewStmt = 
serverConnection.prepareStatement(
+                    tenantViewSQL).unwrap(PhoenixPreparedStatement.class)) {
+                try (ResultSet tenantViewRS = tenantViewStmt.executeQuery()) {
+                    while (tenantViewRS.next()) {
+                        String tid = tenantViewRS.getString("TENANT_ID");
+                        String schem = tenantViewRS.getString("TABLE_SCHEM");
+                        String tName = tenantViewRS.getString("TABLE_NAME");
+                        String tenantId = tid == null || tid.isEmpty() ? 
"NULL" : "'" + tid + "'";
+                        String schemCol = schem == null || schem.isEmpty() ? 
"NULL" : "'" + schem + "'";
+                        TableInfo
+                                tableInfo =
+                                new TableInfo(tenantId.getBytes(), 
schemCol.getBytes(),
+                                        tName.getBytes());
+                        tenantViewSet.add(tableInfo);
+                    }
+                }
+            }
+        }
+        return tenantViewSet;
+    }
+
+    private static void getTTLInfo(Set<TableInfo> viewSet, Configuration 
configuration,
+            boolean isIndexTable, List<TableTTLInfo> tableTTLInfoList)
+            throws SQLException {
+
+        if (viewSet.size() == 0) {
+            return;
+        }
+        String
+                viewsClause =
+                new StringBuilder(viewSet.stream()
+                        .map((v) -> String.format("(%s, %s,'%s')",
+                                Bytes.toString(v.getTenantId()),
+                                Bytes.toString(v.getSchemaName()),
+                                Bytes.toString(v.getTableName())))
+                        .collect(Collectors.joining(","))).toString();
+        String
+                viewsWithTTLSQL =
+                "SELECT TENANT_ID, TABLE_SCHEM, TABLE_NAME, " +
+                        "TABLE_TYPE, TTL, ROW_KEY_MATCHER " +
+                        "FROM SYSTEM.CATALOG " +
+                        "WHERE TABLE_TYPE = 'v' AND " +
+                        "(TENANT_ID, TABLE_SCHEM, TABLE_NAME) IN " +
+                        "(" + viewsClause.toString() + ")";
+        logger.debug(
+                String.format("ViewsWithTTLSQL : %s", viewsWithTTLSQL));
+
+        try (Connection serverConnection = QueryUtil.getConnectionOnServer(new 
Properties(),
+                configuration)) {
+
+            try (
+                    PhoenixPreparedStatement
+                            viewTTLStmt =
+                            serverConnection.prepareStatement(viewsWithTTLSQL)
+                                    .unwrap(PhoenixPreparedStatement.class)) {
+
+                try (ResultSet viewTTLRS = viewTTLStmt.executeQuery()) {
+                    while (viewTTLRS.next()) {
+                        String tid = viewTTLRS.getString("TENANT_ID");
+                        String schem = viewTTLRS.getString("TABLE_SCHEM");
+                        String tName = viewTTLRS.getString("TABLE_NAME");
+                        byte[]
+                                tenantIdBytes =
+                                tid == null || tid.isEmpty() ? 
EMPTY_BYTE_ARRAY : tid.getBytes();
+
+                        String fullTableName = SchemaUtil.getTableName(schem, 
tName);
+                        Properties tenantProps = new Properties();
+                        if (tid != null) {
+                            
tenantProps.setProperty(PhoenixRuntime.TENANT_ID_ATTRIB, tid);
+                        }
+                        Connection
+                                tableConnection =
+                                QueryUtil.getConnectionOnServer(tenantProps, 
configuration);

Review Comment:
   The connection is not closed here. I suggest opening it within a try 
statement. Also, can we use one global connection instead of a separate 
connection for every view?



-- 
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