roryqi commented on code in PR #11201:
URL: https://github.com/apache/gravitino/pull/11201#discussion_r3298098625


##########
core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java:
##########
@@ -479,6 +489,24 @@ private List<SchemaPO> listSchemaPOs(Namespace namespace) {
         mapper -> POStorageReadRouting.listPOs(mapper, namespace, ops, 
Entity.EntityType.SCHEMA));
   }
 
+  /**
+   * Collects the schema ids that participate in a cascade delete: the target 
schema itself plus
+   * every HierarchicalSchema descendant. The {@link SchemaPO} arrives in 
logical form (e.g. {@code
+   * A:B}); {@link HierarchicalConversionPOStorageOps} translates to storage 
form before running the
+   * SQL prefix match, so this method only deals in logical names.
+   */
+  private List<Long> listSchemaIdsForCascade(SchemaPO schemaPO) {
+    List<SchemaPO> matched =
+        SessionUtils.getWithoutCommit(
+            SchemaMetaMapper.class,
+            mapper ->
+                ops.listPOsByNamePrefix(mapper, schemaPO.getCatalogId(), 
schemaPO.getSchemaName()));
+    if (matched == null || matched.isEmpty()) {
+      return Collections.singletonList(schemaPO.getSchemaId());
+    }
+    return 
matched.stream().map(SchemaPO::getSchemaId).collect(Collectors.toList());

Review Comment:
   Actually I should remove this 504line - 506 line.



##########
core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaPOStorageOps.java:
##########
@@ -109,4 +113,36 @@ public List<SchemaPO> listPOsByNSFullName(
   public boolean supportsParentIdRelationalRead() {
     return true;
   }
+
+  /**
+   * For HierarchicalSchema, the name passed in is in storage (physical) form. 
Descendants are
+   * stored as {@code <name><physicalSeparator>...}; we build that prefix once 
and let SQL match the
+   * exact row plus everything starting with the prefix.
+   *
+   * <p>Schema names may contain SQL {@code LIKE} metacharacters ({@code %} 
and {@code _}), so the
+   * literal prefix is escaped here and matched with an {@code ESCAPE} clause 
(see {@link
+   * 
org.apache.gravitino.storage.relational.mapper.provider.base.SchemaMetaBaseSQLProvider}).
 This
+   * guarantees a literal prefix match and prevents e.g. {@code a_b} from also 
matching {@code axb}.
+   */
+  @Override
+  public List<SchemaPO> listPOsByNamePrefix(
+      SchemaMetaMapper mapper, Long catalogId, String physicalName) {
+    String descendantPrefix =
+        escapeLikeMetacharacters(physicalName) + 
HierarchicalSchemaUtil.physicalSeparator();
+    return mapper.listSchemaPOsByCatalogIdAndNamePrefix(catalogId, 
physicalName, descendantPrefix);
+  }
+
+  /**
+   * Escapes SQL {@code LIKE} metacharacters so the value matches literally. 
The escape character
+   * itself is escaped first, then the {@code %} and {@code _} wildcards. The 
{@code !} escape
+   * character is chosen to avoid the backslash string-literal escaping 
differences between MySQL,
+   * H2, and PostgreSQL, and must stay in sync with the {@code ESCAPE} clause 
in {@link
+   * 
org.apache.gravitino.storage.relational.mapper.provider.base.SchemaMetaBaseSQLProvider#listSchemaPOsByCatalogIdAndNamePrefix}.
+   */
+  private static String escapeLikeMetacharacters(String value) {
+    return value

Review Comment:
   You can see the comment
   ```
           + " WHERE catalog_id = #{catalogId}"
           // The descendantPrefix has its LIKE metacharacters escaped with '!' 
by the caller
           // (SchemaPOStorageOps#escapeLikeMetacharacters), so '!' is declared 
as the ESCAPE
           // character to keep the prefix match literal across MySQL, H2, and 
PostgreSQL.
           + " AND (schema_name = #{schemaName}"
           + " OR schema_name LIKE CONCAT(#{descendantPrefix}, '%') ESCAPE '!')"
           + " AND deleted_at = 0";
   ```



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