haridsv commented on code in PR #2070: URL: https://github.com/apache/phoenix/pull/2070#discussion_r1944133013
########## phoenix-core-client/src/main/java/org/apache/phoenix/util/CDCUtil.java: ########## @@ -155,6 +162,73 @@ public static boolean isBinaryType(PDataType dataType) { || dataType.getSqlType() == PDataType.VARBINARY_ENCODED_TYPE); } + /** + * Return true if the parseNode or any of its children contains PARTITION_ID() function. + * + * @param parseNode The parseNode from Where clause. + * @return True if the parseNode or any of its children contains PARTITION_ID() + * function. False otherwise. + */ + public static boolean isPartitionIdIncludedInTree(ParseNode parseNode) { + if (parseNode instanceof PartitionIdParseNode) { + return true; + } + if (parseNode == null || CollectionUtils.isEmpty(parseNode.getChildren())) { + return false; + } + return parseNode.getChildren().stream() + .anyMatch(CDCUtil::isPartitionIdIncludedInTree); + } + + /** + * Add IN Operator for PARTITION_ID() so that the full table scan CDC query can be + * optimized to be range scan. + * + * @param conn The Connection. + * @param cdcName CDC Object name. + * @param query SQL Query Statement. + * @return Updated query including PartitionId with IN operator. + * @throws SQLException If the distinct partition ids retrieval fails. + */ + public static String addPartitionInList(final Connection conn, final String cdcName, + final String query) throws SQLException { + ResultSet rs = conn.createStatement().executeQuery("SELECT DISTINCT PARTITION_ID() FROM " + + cdcName); + List<String> partitionIds = new ArrayList<>(); + while (rs.next()) { + partitionIds.add(rs.getString(1)); + } + if (partitionIds.isEmpty()) { + return query; + } + StringBuilder builder; + boolean queryHasWhere = query.contains(" WHERE "); Review Comment: > we need QueryCompiler to execute query which would seem odd for CDC use case I agree. How about executing the query here, but passing the partition IDs via a field on the statement object? We could make this a generic "postProcessPlan" sort of a concept for which the base statement class does nothing, but a specialized statement class meant for CDC will override the method and modify the plan. -- 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