lasdf1234 commented on code in PR #11066:
URL: https://github.com/apache/gravitino/pull/11066#discussion_r3238869671


##########
plugins/idp-basic/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/IdpUserMetaBaseSQLProvider.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * 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.List;
+import org.apache.gravitino.storage.relational.mapper.IdpUserMetaMapper;
+import org.apache.gravitino.storage.relational.po.IdpUserPO;
+import org.apache.ibatis.annotations.Param;
+
+public class IdpUserMetaBaseSQLProvider {
+
+  public String selectIdpUser(@Param("userName") String userName) {
+    return "SELECT user_id as userId, user_name as userName, password_hash as 
passwordHash,"
+        + " current_version as currentVersion,"
+        + " last_version as lastVersion, deleted_at as deletedAt"
+        + " FROM "
+        + IdpUserMetaMapper.IDP_USER_TABLE_NAME
+        + " WHERE user_name = #{userName} AND deleted_at = 0";
+  }
+
+  public String selectIdpUsers(@Param("userNames") List<String> userNames) {
+    return "<script>"
+        + "SELECT user_id as userId, user_name as userName, password_hash as 
passwordHash,"
+        + " current_version as currentVersion,"
+        + " last_version as lastVersion, deleted_at as deletedAt"
+        + " FROM "
+        + IdpUserMetaMapper.IDP_USER_TABLE_NAME
+        + " WHERE deleted_at = 0 "
+        + "<choose>"
+        + "<when test='userNames != null and userNames.size() > 0'>"
+        + "AND user_name IN ("
+        + "<foreach item='item' collection='userNames' separator=','>"
+        + "#{item}"
+        + "</foreach>"
+        + ") "
+        + "</when>"
+        + "<otherwise>"
+        + "AND 1 = 0 "
+        + "</otherwise>"
+        + "</choose>"
+        + "</script>";
+  }
+
+  public String insertIdpUser(@Param("userMeta") IdpUserPO userPO) {
+    return "INSERT INTO "
+        + IdpUserMetaMapper.IDP_USER_TABLE_NAME
+        + " (user_id, user_name, password_hash, current_version, last_version, 
deleted_at)"
+        + " VALUES ("
+        + " #{userMeta.userId},"
+        + " #{userMeta.userName},"
+        + " #{userMeta.passwordHash},"
+        + " #{userMeta.currentVersion},"
+        + " #{userMeta.lastVersion},"
+        + " #{userMeta.deletedAt}"
+        + " )";
+  }
+
+  public String updateIdpUserPassword(
+      @Param("userId") Long userId,
+      @Param("passwordHash") String passwordHash,
+      @Param("currentVersion") Long currentVersion,
+      @Param("newCurrentVersion") Long newCurrentVersion,
+      @Param("newLastVersion") Long newLastVersion) {
+    return "UPDATE "
+        + IdpUserMetaMapper.IDP_USER_TABLE_NAME
+        + " SET password_hash = #{passwordHash},"
+        + " current_version = #{newCurrentVersion},"
+        + " last_version = #{newLastVersion}"
+        + " WHERE user_id = #{userId}"
+        + " AND current_version = #{currentVersion}"
+        + " AND deleted_at = 0";
+  }
+
+  public String softDeleteIdpUser(@Param("userId") Long userId) {
+    return "UPDATE "
+        + IdpUserMetaMapper.IDP_USER_TABLE_NAME
+        + " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+        + " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000,"
+        + " current_version = current_version + 1,"
+        + " last_version = last_version + 1"
+        + " WHERE user_id = #{userId} AND deleted_at = 0";
+  }

Review Comment:
   No FLOOR/CAST was introduced (as it was used with * 1000.0 throughout the 
project), but the problematic non-standard EXTRACT expression that caused the 
issue was removed.



##########
plugins/idp-basic/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/postgresql/IdpUserMetaPostgreSQLProvider.java:
##########
@@ -0,0 +1,46 @@
+/*
+ * 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 org.apache.gravitino.storage.relational.mapper.IdpUserMetaMapper;
+import 
org.apache.gravitino.storage.relational.mapper.provider.base.IdpUserMetaBaseSQLProvider;
+import org.apache.ibatis.annotations.Param;
+
+public class IdpUserMetaPostgreSQLProvider extends IdpUserMetaBaseSQLProvider {
+
+  @Override
+  public String softDeleteIdpUser(Long userId) {
+    return "UPDATE "
+        + IdpUserMetaMapper.IDP_USER_TABLE_NAME
+        + " SET deleted_at = CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000 
AS BIGINT),"
+        + " current_version = current_version + 1,"
+        + " last_version = last_version + 1"
+        + " WHERE user_id = #{userId} AND deleted_at = 0";
+  }
+
+  @Override
+  public String deleteIdpUserMetasByLegacyTimeline(Long legacyTimeline, 
@Param("limit") int limit) {

Review Comment:
   The expression UNIX_TIMESTAMP() * 1000.0 is consistent with the project-wide 
pattern used in all other base providers (FilesetMeta, ModelMeta, ViewMeta, 
etc.). No change needed.



##########
plugins/idp-basic/src/main/java/org/apache/gravitino/storage/relational/po/IdpUserPO.java:
##########
@@ -0,0 +1,151 @@
+/*
+ * 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.po;
+
+import com.google.common.base.Objects;
+import com.google.common.base.Preconditions;
+
+public class IdpUserPO {

Review Comment:
   Got, lombok has already been used and the code has been modified.



##########
plugins/idp-basic/src/test/java/org/apache/gravitino/storage/relational/mapper/provider/postgresql/TestIdpUserMetaPostgreSQLProvider.java:
##########
@@ -0,0 +1,41 @@
+/*
+ * 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 
org.apache.gravitino.storage.relational.mapper.provider.base.IdpUserMetaBaseSQLProvider;
+import 
org.apache.gravitino.storage.relational.mapper.provider.base.TestIdpUserMetaBaseSQLProvider;
+
+public class TestIdpUserMetaPostgreSQLProvider extends 
TestIdpUserMetaBaseSQLProvider {
+
+  @Override
+  protected IdpUserMetaBaseSQLProvider createProvider() {
+    return new IdpUserMetaPostgreSQLProvider();
+  }
+
+  @Override
+  protected String expectedDeleteAtClause() {
+    return "deleted_at = CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000 AS 
BIGINT)";
+  }
+
+  @Override
+  protected String expectedDeleteIdpUserMetasByLegacyTimelineSql() {
+    return "DELETE FROM idp_user_meta WHERE user_id IN (SELECT user_id FROM 
idp_user_meta WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT 
#{limit})";

Review Comment:
   Got, The code has been modified and I have also asked the AI to review other 
sections with more than 100 chars for me. The code has been changed.



##########
plugins/idp-basic/src/test/java/org/apache/gravitino/storage/relational/mapper/provider/base/TestIdpUserMetaBaseSQLProvider.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * 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.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.gravitino.storage.relational.po.IdpUserPO;
+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 TestIdpUserMetaBaseSQLProvider {
+
+  protected IdpUserMetaBaseSQLProvider createProvider() {
+    return new IdpUserMetaBaseSQLProvider();
+  }
+
+  protected String expectedDeleteAtClause() {
+    return "deleted_at = (UNIX_TIMESTAMP() * 1000.0) + EXTRACT(MICROSECOND 
FROM CURRENT_TIMESTAMP(3)) / 1000";
+  }
+
+  protected String expectedDeleteIdpUserMetasByLegacyTimelineSql() {
+    return "DELETE FROM idp_user_meta WHERE deleted_at > 0 AND deleted_at < 
#{legacyTimeline} LIMIT #{limit}";
+  }
+
+  @Test
+  public void testSelectIdpUser() {
+    String normalizedSql = 
createProvider().selectIdpUser("tom").replaceAll("\\s+", " ").trim();
+
+    Assertions.assertTrue(normalizedSql.contains("SELECT user_id as userId"));
+    Assertions.assertTrue(normalizedSql.contains("FROM idp_user_meta"));
+    Assertions.assertTrue(
+        normalizedSql.contains("WHERE user_name = #{userName} AND deleted_at = 
0"));
+  }
+
+  @Test
+  public void testSelectIdpUsers() {
+    String script = createProvider().selectIdpUsers(Arrays.asList("tom", 
"jerry"));
+    Map<String, Object> params = new HashMap<>();
+    params.put("userNames", Arrays.asList("tom", "jerry"));
+
+    String normalizedSql = renderScript(script, params);
+
+    Assertions.assertTrue(normalizedSql.contains("SELECT user_id as userId"));
+    Assertions.assertTrue(normalizedSql.contains("FROM idp_user_meta"));
+    Assertions.assertTrue(normalizedSql.matches(".*user_name IN \\( \\? , \\? 
\\).*"));
+    Assertions.assertFalse(normalizedSql.matches(".*\\b1\\s*=\\s*0\\b.*"));
+  }
+
+  @Test
+  public void testSelectIdpUsersWithEmptyUserNames() {
+    String script = createProvider().selectIdpUsers(Collections.emptyList());
+    Map<String, Object> params = new HashMap<>();
+    params.put("userNames", Collections.emptyList());
+
+    String normalizedSql = renderScript(script, params);
+
+    Assertions.assertFalse(
+        normalizedSql.matches(".*\\bIN\\s*\\(\\s*\\).*"),
+        "Empty userNames should not generate invalid SQL IN (...) with no 
values");
+    Assertions.assertTrue(
+        normalizedSql.matches(".*\\b1\\s*=\\s*0\\b.*"),
+        "Empty userNames should result in an unsatisfiable WHERE clause (e.g., 
AND 1 = 0)");
+  }
+
+  @Test
+  public void testSelectIdpUsersWithNullUserNames() {
+    String script = createProvider().selectIdpUsers(null);
+    Map<String, Object> params = new HashMap<>();
+    params.put("userNames", null);
+
+    String normalizedSql = renderScript(script, params);
+
+    Assertions.assertFalse(
+        normalizedSql.matches(".*\\bIN\\s*\\(\\s*\\).*"),
+        "Null userNames should not generate invalid SQL IN (...) with no 
values");
+    Assertions.assertTrue(
+        normalizedSql.matches(".*\\b1\\s*=\\s*0\\b.*"),
+        "Null userNames should result in an unsatisfiable WHERE clause (e.g., 
AND 1 = 0)");
+  }
+
+  @Test
+  public void testInsertIdpUser() {
+    String normalizedSql =
+        createProvider().insertIdpUser(newUserPO()).replaceAll("\\s+", " 
").trim();
+
+    Assertions.assertTrue(normalizedSql.contains("INSERT INTO idp_user_meta"));
+    Assertions.assertTrue(
+        normalizedSql.contains(
+            "(user_id, user_name, password_hash, current_version, 
last_version, deleted_at)"));
+    Assertions.assertTrue(
+        normalizedSql.contains(
+            "VALUES ( #{userMeta.userId}, #{userMeta.userName}, 
#{userMeta.passwordHash}, #{userMeta.currentVersion}, #{userMeta.lastVersion}, 
#{userMeta.deletedAt} )"));

Review Comment:
   Got, The code has been modified and I have also asked the AI to review other 
sections with more than 100 chars for me. The code has been changed.



##########
plugins/idp-basic/src/test/java/org/apache/gravitino/storage/relational/mapper/provider/h2/TestIdpUserMetaH2Provider.java:
##########
@@ -0,0 +1,31 @@
+/*
+ * 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.h2;
+
+import 
org.apache.gravitino.storage.relational.mapper.provider.base.IdpUserMetaBaseSQLProvider;
+import 
org.apache.gravitino.storage.relational.mapper.provider.base.TestIdpUserMetaBaseSQLProvider;
+
+public class TestIdpUserMetaH2Provider extends TestIdpUserMetaBaseSQLProvider {

Review Comment:
   Got, I have already used @Tag("gravitino-docker-test") for MySQL and PG test.



##########
plugins/idp-basic/build.gradle.kts:
##########
@@ -24,10 +24,27 @@ plugins {
 }
 
 dependencies {
+  implementation(project(":core"))
   implementation(libs.bcprov.jdk18on)
   implementation(libs.commons.lang3)
   implementation(libs.guava)
+  implementation(libs.mybatis)
+  testImplementation(project(":api"))

Review Comment:
   Got,remove testImplementation(project(":api")),The code has been revised.



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