You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/07/20 11:42:26 UTC

[shardingsphere] branch master updated: Implements openGauss password related functions by calcite (#19393)

This is an automated email from the ASF dual-hosted git repository.

panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new f92c8408d85 Implements openGauss password related functions by calcite (#19393)
f92c8408d85 is described below

commit f92c8408d859347664498b1b7a75c7b6df8b0499
Author: 吴伟杰 <wu...@apache.org>
AuthorDate: Wed Jul 20 19:42:20 2022 +0800

    Implements openGauss password related functions by calcite (#19393)
    
    * Implements openGauss password related functions by calcite
    
    * Complete OpenGaussSystemCatalogAdminQueryExecutorTest
    
    * Add final modifier to parameter
---
 .../opengauss/OpenGaussAdminExecutorCreator.java   | 12 +++++++-
 .../OpenGaussSystemCatalogAdminQueryExecutor.java  | 35 +++++++++++++++++++++-
 ...enGaussSystemCatalogAdminQueryExecutorTest.java | 34 +++++++++++++++++++++
 3 files changed, 79 insertions(+), 2 deletions(-)

diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorCreator.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorCreator.java
index 2afe36b65c0..37f50dbcce5 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorCreator.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussAdminExecutorCreator.java
@@ -27,12 +27,22 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectState
 
 import java.util.Collection;
 import java.util.Optional;
+import java.util.Set;
+import java.util.TreeSet;
 
 /**
  * Database admin executor creator for openGauss.
  */
 public final class OpenGaussAdminExecutorCreator implements DatabaseAdminExecutorCreator {
     
+    private static final Set<String> SYSTEM_CATALOG_QUERY_EXPRESSIONS = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
+    
+    static {
+        SYSTEM_CATALOG_QUERY_EXPRESSIONS.add("VERSION()");
+        SYSTEM_CATALOG_QUERY_EXPRESSIONS.add("intervaltonum(gs_password_deadline())");
+        SYSTEM_CATALOG_QUERY_EXPRESSIONS.add("gs_password_notifytime()");
+    }
+    
     private static final String OG_DATABASE = "pg_database";
     
     private final PostgreSQLAdminExecutorCreator delegated = new PostgreSQLAdminExecutorCreator();
@@ -60,7 +70,7 @@ public final class OpenGaussAdminExecutorCreator implements DatabaseAdminExecuto
         SelectStatement selectStatement = (SelectStatement) sqlStatementContext.getSqlStatement();
         Collection<ProjectionSegment> projections = selectStatement.getProjections().getProjections();
         return 1 == projections.size() && projections.iterator().next() instanceof ExpressionProjectionSegment
-                && "VERSION()".equalsIgnoreCase(((ExpressionProjectionSegment) projections.iterator().next()).getText());
+                && SYSTEM_CATALOG_QUERY_EXPRESSIONS.contains(((ExpressionProjectionSegment) projections.iterator().next()).getText());
     }
     
     @Override
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutor.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutor.java
index 990cdce90bc..2595d435092 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutor.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutor.java
@@ -44,6 +44,7 @@ import java.util.Collections;
 /**
  * Select database executor for openGauss.
  */
+@SuppressWarnings("unused")
 public final class OpenGaussSystemCatalogAdminQueryExecutor implements DatabaseAdminQueryExecutor {
     
     private static final String PG_CATALOG = "pg_catalog";
@@ -67,6 +68,9 @@ public final class OpenGaussSystemCatalogAdminQueryExecutor implements DatabaseA
         try (CalciteConnection connection = DriverManager.getConnection("jdbc:calcite:caseSensitive=false").unwrap(CalciteConnection.class)) {
             connection.getRootSchema().add(PG_CATALOG, new ReflectiveSchema(constructOgCatalog()));
             connection.getRootSchema().add("version", ScalarFunctionImpl.create(getClass(), "version"));
+            connection.getRootSchema().add("gs_password_deadline", ScalarFunctionImpl.create(getClass(), "gsPasswordDeadline"));
+            connection.getRootSchema().add("intervaltonum", ScalarFunctionImpl.create(getClass(), "intervalToNum"));
+            connection.getRootSchema().add("gs_password_notifyTime", ScalarFunctionImpl.create(getClass(), "gsPasswordNotifyTime"));
             connection.setSchema(PG_CATALOG);
             try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql)) {
                 queryResultMetaData = new JDBCQueryResultMetaData(resultSet.getMetaData());
@@ -90,8 +94,37 @@ public final class OpenGaussSystemCatalogAdminQueryExecutor implements DatabaseA
      *
      * @return version message
      */
-    @SuppressWarnings("unused")
     public static String version() {
         return "ShardingSphere-Proxy " + ShardingSphereVersion.VERSION + ("-" + ShardingSphereVersion.BUILD_GIT_COMMIT_ID_ABBREV) + (ShardingSphereVersion.BUILD_GIT_DIRTY ? "-dirty" : "");
     }
+    
+    /**
+     * The type interval is not supported in standard JDBC.
+     * Indicates the number of remaining days before the password of the current user expires.
+     *
+     * @return 90 days
+     */
+    public static int gsPasswordDeadline() {
+        return 90;
+    }
+    
+    /**
+     * The type interval is not supported in standard JDBC.
+     * Convert interval to num.
+     *
+     * @param result result
+     * @return result
+     */
+    public static int intervalToNum(final int result) {
+        return result;
+    }
+    
+    /**
+     * Specifies the number of days prior to password expiration that a user will receive a reminder.
+     *
+     * @return 7 days
+     */
+    public static int gsPasswordNotifyTime() {
+        return 7;
+    }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutorTest.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutorTest.java
index 54770bbb16d..4c16a66488c 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutorTest.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/admin/opengauss/OpenGaussSystemCatalogAdminQueryExecutorTest.java
@@ -76,4 +76,38 @@ public final class OpenGaussSystemCatalogAdminQueryExecutorTest {
             assertThat((String) actualResult.getValue(1, String.class), containsString("ShardingSphere-Proxy"));
         }
     }
+    
+    @Test
+    public void assertExecuteSelectGsPasswordDeadlineAndIntervalToNum() throws SQLException {
+        try (MockedStatic<ProxyContext> mockedStatic = mockStatic(ProxyContext.class)) {
+            mockedStatic.when(ProxyContext::getInstance).thenReturn(mock(ProxyContext.class, RETURNS_DEEP_STUBS));
+            OpenGaussSystemCatalogAdminQueryExecutor executor = new OpenGaussSystemCatalogAdminQueryExecutor("select intervaltonum(gs_password_deadline())");
+            ConnectionSession connectionSession = mock(ConnectionSession.class);
+            when(connectionSession.getDatabaseType()).thenReturn(new OpenGaussDatabaseType());
+            executor.execute(connectionSession);
+            QueryResultMetaData actualMetaData = executor.getQueryResultMetaData();
+            assertThat(actualMetaData.getColumnCount(), is(1));
+            assertThat(actualMetaData.getColumnType(1), is(Types.INTEGER));
+            MergedResult actualResult = executor.getMergedResult();
+            assertTrue(actualResult.next());
+            assertThat(actualResult.getValue(1, Integer.class), is(90));
+        }
+    }
+    
+    @Test
+    public void assertExecuteSelectGsPasswordNotifyTime() throws SQLException {
+        try (MockedStatic<ProxyContext> mockedStatic = mockStatic(ProxyContext.class)) {
+            mockedStatic.when(ProxyContext::getInstance).thenReturn(mock(ProxyContext.class, RETURNS_DEEP_STUBS));
+            OpenGaussSystemCatalogAdminQueryExecutor executor = new OpenGaussSystemCatalogAdminQueryExecutor("select gs_password_notifytime()");
+            ConnectionSession connectionSession = mock(ConnectionSession.class);
+            when(connectionSession.getDatabaseType()).thenReturn(new OpenGaussDatabaseType());
+            executor.execute(connectionSession);
+            QueryResultMetaData actualMetaData = executor.getQueryResultMetaData();
+            assertThat(actualMetaData.getColumnCount(), is(1));
+            assertThat(actualMetaData.getColumnType(1), is(Types.INTEGER));
+            MergedResult actualResult = executor.getMergedResult();
+            assertTrue(actualResult.next());
+            assertThat(actualResult.getValue(1, Integer.class), is(7));
+        }
+    }
 }