Copilot commented on code in PR #10261:
URL: https://github.com/apache/gravitino/pull/10261#discussion_r2893685402


##########
core/src/test/java/org/apache/gravitino/storage/relational/mapper/provider/base/TestGroupRoleRelBaseSQLProvider.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.storage.relational.mapper.provider.base;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.ibatis.mapping.BoundSql;
+import org.apache.ibatis.mapping.SqlSource;
+import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
+import org.apache.ibatis.session.Configuration;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestGroupRoleRelBaseSQLProvider {
+
+  @Test
+  void testSoftDeleteGroupRoleRelByGroupAndRolesWithEmptyRoles() {
+    GroupRoleRelBaseSQLProvider provider = new GroupRoleRelBaseSQLProvider();
+    String script = provider.softDeleteGroupRoleRelByGroupAndRoles(1L, 
Collections.emptyList());
+
+    SqlSource sqlSource =
+        new XMLLanguageDriver().createSqlSource(new Configuration(), script, 
Map.class);
+    Map<String, Object> params = new HashMap<>();
+    params.put("groupId", 1L);
+    params.put("roleIds", Collections.emptyList());
+
+    BoundSql boundSql = sqlSource.getBoundSql(params);
+    String normalizedSql = boundSql.getSql().replaceAll("\\s+", " ").trim();

Review Comment:
   The SQL-compilation + normalization logic is duplicated across all four new 
test classes. Consider extracting a small shared helper (e.g., a 
package-private utility method in the test package) to build `BoundSql` and 
normalize SQL, so future SQL-generation assertions can be added/updated in one 
place.
   ```suggestion
     static String normalizeSql(String script, Map<String, Object> params) {
       SqlSource sqlSource =
           new XMLLanguageDriver().createSqlSource(new Configuration(), script, 
Map.class);
       BoundSql boundSql = sqlSource.getBoundSql(params);
       return boundSql.getSql().replaceAll("\\s+", " ").trim();
     }
   
     @Test
     void testSoftDeleteGroupRoleRelByGroupAndRolesWithEmptyRoles() {
       GroupRoleRelBaseSQLProvider provider = new GroupRoleRelBaseSQLProvider();
       String script = provider.softDeleteGroupRoleRelByGroupAndRoles(1L, 
Collections.emptyList());
   
       Map<String, Object> params = new HashMap<>();
       params.put("groupId", 1L);
       params.put("roleIds", Collections.emptyList());
   
       String normalizedSql = normalizeSql(script, params);
   ```



##########
core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/UserRoleRelBaseSQLProvider.java:
##########
@@ -87,12 +87,18 @@ public String softDeleteUserRoleRelByUserAndRoles(
         + USER_ROLE_RELATION_TABLE_NAME
         + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
         + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
-        + " WHERE user_id = #{userId} AND role_id IN ("
+        + " WHERE user_id = #{userId} "
+        + "<if test='roleIds != null and roleIds.size() > 0'>"
+        + "AND role_id IN ("
         + "<foreach collection='roleIds' item='roleId' separator=','>"
         + "#{roleId}"
         + "</foreach>"
-        + " )"
-        + " AND deleted_at = 0"
+        + ") "
+        + "</if>"
+        + "<if test='roleIds == null or roleIds.size() == 0'>"
+        + "AND 1 = 0 "
+        + "</if>"

Review Comment:
   These two `<if>` blocks are mutually exclusive, but the intent is clearer 
and less repetitive using `<choose>/<when>/<otherwise>`. Refactoring to 
`<choose>` would make the “non-empty list” vs “empty/null list” branching more 
explicit and reduce the chance of future edits accidentally creating 
overlapping conditions.



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