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


##########
core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/postgresql/UserRoleRelPostgreSQLProvider.java:
##########
@@ -42,11 +42,19 @@ public String softDeleteUserRoleRelByUserAndRoles(Long 
userId, List<Long> roleId
         + "UPDATE "
         + USER_ROLE_RELATION_TABLE_NAME
         + " SET deleted_at = CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000 
AS BIGINT)"
-        + " WHERE user_id = #{userId} AND role_id IN ("
+        + " WHERE user_id = #{userId} "
+        + "<choose>"
+        + "<when test='roleIds != null and roleIds.size() > 0'>"
+        + "AND role_id IN ("
         + "<foreach collection='roleIds' item='roleId' separator=','>"
         + "#{roleId}"
         + "</foreach>"
         + ") "
+        + "</when>"
+        + "<otherwise>"
+        + "AND 1 = 0 "
+        + "</otherwise>"
+        + "</choose>"

Review Comment:
   The new `<choose>` branch changes behavior for both empty and non-empty 
`roleIds`, but the added tests only assert the empty-list path. Add a test for 
a non-empty `roleIds` list to assert that (1) the generated SQL still contains 
`role_id IN (` with placeholders/values, and (2) it does not contain `1 = 0`, 
to prevent regressions where the wrong branch is selected.



##########
core/src/test/java/org/apache/gravitino/storage/relational/mapper/provider/postgresql/TestUserRoleRelPostgreSQLProvider.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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.postgresql;
+
+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 TestUserRoleRelPostgreSQLProvider {
+
+  @Test
+  void testSoftDeleteUserRoleRelByUserAndRolesWithEmptyRoles() {
+    UserRoleRelPostgreSQLProvider provider = new 
UserRoleRelPostgreSQLProvider();
+    String script = provider.softDeleteUserRoleRelByUserAndRoles(1L, 
Collections.emptyList());
+
+    SqlSource sqlSource =
+        new XMLLanguageDriver().createSqlSource(new Configuration(), script, 
Map.class);
+    Map<String, Object> params = new HashMap<>();
+    params.put("userId", 1L);
+    params.put("roleIds", Collections.emptyList());
+
+    BoundSql boundSql = sqlSource.getBoundSql(params);
+    String normalizedSql = boundSql.getSql().replaceAll("\\s+", " ").trim();

Review Comment:
   This SQL-building + MyBatis parsing + normalization block is duplicated 
across 4 new test classes. Consider extracting a small helper (e.g., a utility 
method that returns `normalizedSql` given `script` + `params`) or using a 
parameterized test to reduce duplication and make future SQL assertions easier 
to add/maintain.
   ```suggestion
     private 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 testSoftDeleteUserRoleRelByUserAndRolesWithEmptyRoles() {
       UserRoleRelPostgreSQLProvider provider = new 
UserRoleRelPostgreSQLProvider();
       String script = provider.softDeleteUserRoleRelByUserAndRoles(1L, 
Collections.emptyList());
   
       Map<String, Object> params = new HashMap<>();
       params.put("userId", 1L);
       params.put("roleIds", Collections.emptyList());
   
       String normalizedSql = normalizeSql(script, params);
   ```



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