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


##########
core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/postgresql/UserRoleRelPostgreSQLProvider.java:
##########
@@ -42,11 +42,17 @@ 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} "
+        + "<if test='roleIds != null and roleIds.size() > 0'>"
+        + "AND role_id IN ("
         + "<foreach collection='roleIds' item='roleId' separator=','>"
         + "#{roleId}"
         + "</foreach>"
         + ") "
+        + "</if>"
+        + "<if test='roleIds == null or roleIds.size() == 0'>"
+        + "AND 1= 0 "

Review Comment:
   "AND 1= 0 "` has inconsistent spacing compared to the other providers (`AND 
1 = 0`). For readability/consistency (and to avoid style drift in generated 
SQL), align it to `AND 1 = 0` (also consider removing the extra space after 
`=`).
   ```suggestion
           + "AND 1 = 0 "
   ```



##########
core/src/test/java/org/apache/gravitino/storage/relational/mapper/provider/postgresql/TestUserRoleRelPostgreSQLProvider.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.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();
+
+    Assertions.assertFalse(
+        normalizedSql.matches(".*\\bIN\\s*\\(\\s*\\).*"),
+        "Empty roleIds should not generate invalid SQL IN (...) with no 
values");

Review Comment:
   This assertion only verifies the absence of `IN ()`, but it would still pass 
if the SQL accidentally dropped the role filter entirely (potentially updating 
all roles for the user/group). Consider strengthening the test by also 
asserting the presence of the intended guard predicate (e.g., `AND 1 = 0`) or 
otherwise verifying the WHERE clause becomes unsatisfiable when `roleIds` is 
empty. Same pattern applies to the other three new test classes.
   ```suggestion
           "Empty roleIds should not generate invalid SQL IN (...) with no 
values");
   
       Assertions.assertTrue(
           normalizedSql.matches(".*\\b1\\s*=\\s*0\\b.*"),
           "Empty roleIds should result in an unsatisfiable WHERE clause (e.g., 
AND 1 = 0)");
   ```



##########
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();
+
+    Assertions.assertFalse(
+        normalizedSql.matches(".*\\bIN\\s*\\(\\s*\\).*"),
+        "Empty roleIds should not generate invalid SQL IN (...) with no 
values");

Review Comment:
   The four new test classes appear to be near-identical aside from provider 
type and parameter key (`userId` vs `groupId`). To reduce duplication and ease 
future changes (e.g., adjusting normalization/assertions), consider extracting 
a small shared helper (e.g., `assertNoEmptyInClause(script, params)`) or 
converting to a parameterized test with inputs (provider/script supplier + id 
key).
   ```suggestion
       Map<String, Object> params = new HashMap<>();
       params.put("groupId", 1L);
       params.put("roleIds", Collections.emptyList());
   
       assertNoEmptyInClause(
           script, params, "Empty roleIds should not generate invalid SQL IN 
(...) with no values");
     }
   
     private void assertNoEmptyInClause(
         String script, Map<String, Object> params, String failureMessage) {
       SqlSource sqlSource =
           new XMLLanguageDriver().createSqlSource(new Configuration(), script, 
Map.class);
       BoundSql boundSql = sqlSource.getBoundSql(params);
       String normalizedSql = boundSql.getSql().replaceAll("\\s+", " ").trim();
   
       Assertions.assertFalse(
           normalizedSql.matches(".*\\bIN\\s*\\(\\s*\\).*"), failureMessage);
   ```



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