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

chengzhang 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 7ff0750e81f Refactor SQL federation function register logic, and split 
SQLFederationFunctionUtils to spi (#32405)
7ff0750e81f is described below

commit 7ff0750e81f11a3f8a7e80ba8982af361cac2095
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Mon Aug 5 16:15:35 2024 +0800

    Refactor SQL federation function register logic, and split 
SQLFederationFunctionUtils to spi (#32405)
    
    * Refactor SQL federation function register logic, and split 
SQLFederationFunctionUtils to spi
    
    * fix build error
---
 .../sqlfederation/engine/SQLFederationEngine.java  |   3 +-
 .../SQLFederationColumnTypeConverter.java          |   2 +
 .../function/SQLFederationFunctionRegister.java}   |  29 ++---
 .../function/mysql/MySQLFunctionRegister.java      |  44 +++++++
 .../mysql/{ => impl}/MySQLBinFunction.java         |   5 +-
 .../mysql/{ => impl}/MySQLBitCountFunction.java    |   2 +-
 .../opengauss/OpenGaussFunctionRegister.java       |  52 ++++++++
 .../opengauss/impl/OpenGaussSystemFunction.java    |  86 +++++++++++++
 .../postgresql/PostgreSQLFunctionRegister.java     |  42 +++++++
 .../postgresql/impl/PostgreSQLSystemFunction.java} |  32 ++---
 .../planner/util/SQLFederationFunctionUtils.java   | 137 ---------------------
 .../planner/util/SQLFederationPlannerUtils.java    |   8 +-
 ...ptimizer.function.SQLFederationFunctionRegister |  20 +++
 .../optimizer/it/SQLStatementCompilerIT.java       |   3 +-
 14 files changed, 288 insertions(+), 177 deletions(-)

diff --git 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java
 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java
index 3816e900109..bc44ef0d702 100644
--- 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java
+++ 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/engine/SQLFederationEngine.java
@@ -197,7 +197,8 @@ public final class SQLFederationEngine implements 
AutoCloseable {
             String schemaName = 
selectStatementContext.getTablesContext().getSchemaName().orElse(currentSchemaName);
             OptimizerMetaData optimizerMetaData = 
sqlFederationRule.getOptimizerContext().getMetaData(databaseName);
             CalciteConnectionConfig connectionConfig = new 
CalciteConnectionConfigImpl(sqlFederationRule.getOptimizerContext().getParserContext(databaseName).getDialectProps());
-            CalciteCatalogReader catalogReader = 
SQLFederationPlannerUtils.createCatalogReader(schemaName, 
optimizerMetaData.getSchema(schemaName), DEFAULT_DATA_TYPE_FACTORY, 
connectionConfig);
+            CalciteCatalogReader catalogReader = 
SQLFederationPlannerUtils.createCatalogReader(schemaName, 
optimizerMetaData.getSchema(schemaName), DEFAULT_DATA_TYPE_FACTORY, 
connectionConfig,
+                    selectStatementContext.getDatabaseType());
             SqlValidator validator = 
SQLFederationPlannerUtils.createSqlValidator(catalogReader, 
DEFAULT_DATA_TYPE_FACTORY,
                     
sqlFederationRule.getOptimizerContext().getParserContext(databaseName).getDatabaseType(),
 connectionConfig);
             SqlToRelConverter converter = 
SQLFederationPlannerUtils.createSqlToRelConverter(catalogReader, validator, 
SQLFederationPlannerUtils.createRelOptCluster(DEFAULT_DATA_TYPE_FACTORY),
diff --git 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/converter/SQLFederationColumnTypeConverter.java
 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/converter/SQLFederationColumnTypeConverter.java
index e345af2142a..3de3f4c735c 100644
--- 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/converter/SQLFederationColumnTypeConverter.java
+++ 
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/converter/SQLFederationColumnTypeConverter.java
@@ -18,10 +18,12 @@
 package org.apache.shardingsphere.sqlfederation.resultset.converter;
 
 import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPI;
+import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
 
 /**
  * SQL federation column type converter.
  */
+@SingletonSPI
 public interface SQLFederationColumnTypeConverter extends DatabaseTypedSPI {
     
     /**
diff --git 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/converter/SQLFederationColumnTypeConverter.java
 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/SQLFederationFunctionRegister.java
similarity index 60%
copy from 
kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/converter/SQLFederationColumnTypeConverter.java
copy to 
kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/SQLFederationFunctionRegister.java
index e345af2142a..75e5649fcee 100644
--- 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/converter/SQLFederationColumnTypeConverter.java
+++ 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/SQLFederationFunctionRegister.java
@@ -15,32 +15,23 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sqlfederation.resultset.converter;
+package org.apache.shardingsphere.sqlfederation.optimizer.function;
 
+import org.apache.calcite.schema.SchemaPlus;
 import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPI;
+import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
 
 /**
- * SQL federation column type converter.
+ * SQL federation function register.
  */
-public interface SQLFederationColumnTypeConverter extends DatabaseTypedSPI {
+@SingletonSPI
+public interface SQLFederationFunctionRegister extends DatabaseTypedSPI {
     
     /**
-     * Convert column value.
+     * Register function.
      *
-     * @param columnValue column value
-     * @return converted column value
+     * @param schemaPlus schema plus
+     * @param schemaName schema name
      */
-    default Object convertColumnValue(Object columnValue) {
-        return columnValue;
-    }
-    
-    /**
-     * Convert column type.
-     *
-     * @param columnType column type
-     * @return converted column type
-     */
-    default int convertColumnType(int columnType) {
-        return columnType;
-    }
+    void registerFunction(SchemaPlus schemaPlus, String schemaName);
 }
diff --git 
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/MySQLFunctionRegister.java
 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/MySQLFunctionRegister.java
new file mode 100644
index 00000000000..dbb56e12dea
--- /dev/null
+++ 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/MySQLFunctionRegister.java
@@ -0,0 +1,44 @@
+/*
+ * 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.shardingsphere.sqlfederation.optimizer.function.mysql;
+
+import org.apache.calcite.runtime.SqlFunctions;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.impl.ScalarFunctionImpl;
+import 
org.apache.shardingsphere.sqlfederation.optimizer.function.SQLFederationFunctionRegister;
+import 
org.apache.shardingsphere.sqlfederation.optimizer.function.mysql.impl.MySQLBinFunction;
+import 
org.apache.shardingsphere.sqlfederation.optimizer.function.mysql.impl.MySQLBitCountFunction;
+
+/**
+ * MySQL function register.
+ */
+public final class MySQLFunctionRegister implements 
SQLFederationFunctionRegister {
+    
+    @Override
+    public void registerFunction(final SchemaPlus schemaPlus, final String 
schemaName) {
+        schemaPlus.add("bin", 
ScalarFunctionImpl.create(MySQLBinFunction.class, "bin"));
+        schemaPlus.add("bit_count", 
ScalarFunctionImpl.create(MySQLBitCountFunction.class, "bitCount"));
+        schemaPlus.add("atan", ScalarFunctionImpl.create(SqlFunctions.class, 
"atan2"));
+        schemaPlus.add("atan2", ScalarFunctionImpl.create(SqlFunctions.class, 
"atan"));
+    }
+    
+    @Override
+    public String getDatabaseType() {
+        return "MySQL";
+    }
+}
diff --git 
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/MySQLBinFunction.java
 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBinFunction.java
similarity index 90%
rename from 
kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/MySQLBinFunction.java
rename to 
kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBinFunction.java
index 70ed9d99f57..dac2122a1cc 100644
--- 
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/MySQLBinFunction.java
+++ 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBinFunction.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sqlfederation.optimizer.function.mysql;
+package org.apache.shardingsphere.sqlfederation.optimizer.function.mysql.impl;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
@@ -34,6 +34,7 @@ public final class MySQLBinFunction {
      * @param value value
      * @return binary string
      */
+    @SuppressWarnings("unused")
     public static String bin(final Object value) {
         if (null == value) {
             return null;
@@ -42,7 +43,7 @@ public final class MySQLBinFunction {
             return Long.toBinaryString(((Number) value).longValue());
         }
         BigInteger bigIntegerValue = value instanceof BigInteger ? 
(BigInteger) value : new BigInteger(getFirstNumbers(String.valueOf(value)));
-        return -1 != bigIntegerValue.signum() ? bigIntegerValue.toString(2) : 
bigIntegerValue.add(BigInteger.ONE.shiftLeft(Long.SIZE).subtract(BigInteger.ONE)).add(BigInteger.ONE).toString(2);
+        return -1 == bigIntegerValue.signum() ? 
bigIntegerValue.add(BigInteger.ONE.shiftLeft(Long.SIZE).subtract(BigInteger.ONE)).add(BigInteger.ONE).toString(2)
 : bigIntegerValue.toString(2);
     }
     
     private static String getFirstNumbers(final String value) {
diff --git 
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/MySQLBitCountFunction.java
 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBitCountFunction.java
similarity index 99%
rename from 
kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/MySQLBitCountFunction.java
rename to 
kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBitCountFunction.java
index c070cd85b8e..a36ab752e78 100644
--- 
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/MySQLBitCountFunction.java
+++ 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBitCountFunction.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sqlfederation.optimizer.function.mysql;
+package org.apache.shardingsphere.sqlfederation.optimizer.function.mysql.impl;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
diff --git 
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/opengauss/OpenGaussFunctionRegister.java
 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/opengauss/OpenGaussFunctionRegister.java
new file mode 100644
index 00000000000..5a6bb65c7a8
--- /dev/null
+++ 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/opengauss/OpenGaussFunctionRegister.java
@@ -0,0 +1,52 @@
+/*
+ * 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.shardingsphere.sqlfederation.optimizer.function.opengauss;
+
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.impl.ScalarFunctionImpl;
+import 
org.apache.shardingsphere.sqlfederation.optimizer.function.SQLFederationFunctionRegister;
+import 
org.apache.shardingsphere.sqlfederation.optimizer.function.opengauss.impl.OpenGaussSystemFunction;
+import 
org.apache.shardingsphere.sqlfederation.optimizer.function.postgresql.PostgreSQLFunctionRegister;
+
+/**
+ * Opengauss function register.
+ */
+public final class OpenGaussFunctionRegister implements 
SQLFederationFunctionRegister {
+    
+    private final SQLFederationFunctionRegister delegate = new 
PostgreSQLFunctionRegister();
+    
+    @Override
+    public void registerFunction(final SchemaPlus schemaPlus, final String 
schemaName) {
+        schemaPlus.add("version", 
ScalarFunctionImpl.create(OpenGaussSystemFunction.class, "version"));
+        schemaPlus.add("opengauss_version", 
ScalarFunctionImpl.create(OpenGaussSystemFunction.class, "openGaussVersion"));
+        schemaPlus.add("gs_password_deadline", 
ScalarFunctionImpl.create(OpenGaussSystemFunction.class, "gsPasswordDeadline"));
+        schemaPlus.add("intervaltonum", 
ScalarFunctionImpl.create(OpenGaussSystemFunction.class, "intervalToNum"));
+        schemaPlus.add("gs_password_notifyTime", 
ScalarFunctionImpl.create(OpenGaussSystemFunction.class, 
"gsPasswordNotifyTime"));
+        if ("pg_catalog".equalsIgnoreCase(schemaName)) {
+            schemaPlus.add("pg_catalog.gs_password_deadline", 
ScalarFunctionImpl.create(OpenGaussSystemFunction.class, "gsPasswordDeadline"));
+            schemaPlus.add("pg_catalog.intervaltonum", 
ScalarFunctionImpl.create(OpenGaussSystemFunction.class, "intervalToNum"));
+            schemaPlus.add("pg_catalog.gs_password_notifyTime", 
ScalarFunctionImpl.create(OpenGaussSystemFunction.class, 
"gsPasswordNotifyTime"));
+        }
+        delegate.registerFunction(schemaPlus, schemaName);
+    }
+    
+    @Override
+    public String getDatabaseType() {
+        return "openGauss";
+    }
+}
diff --git 
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/opengauss/impl/OpenGaussSystemFunction.java
 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/opengauss/impl/OpenGaussSystemFunction.java
new file mode 100644
index 00000000000..a435bcce5f8
--- /dev/null
+++ 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/opengauss/impl/OpenGaussSystemFunction.java
@@ -0,0 +1,86 @@
+/*
+ * 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.shardingsphere.sqlfederation.optimizer.function.opengauss.impl;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
+
+/**
+ * PostgreSQL system function.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class OpenGaussSystemFunction {
+    
+    private static final int DEFAULT_PASSWORD_DEADLINE = 90;
+    
+    private static final int DEFAULT_PASSWORD_NOTIFY_TIME = 7;
+    
+    /**
+     * Get version of ShardingSphere-Proxy.
+     *
+     * @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" : "");
+    }
+    
+    /**
+     * Get version of ShardingSphere-Proxy for openGauss.
+     *
+     * @return version message
+     */
+    @SuppressWarnings("unused")
+    public static String openGaussVersion() {
+        return ShardingSphereVersion.VERSION;
+    }
+    
+    /**
+     * 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
+     */
+    @SuppressWarnings("unused")
+    public static int gsPasswordDeadline() {
+        return DEFAULT_PASSWORD_DEADLINE;
+    }
+    
+    /**
+     * The type interval is not supported in standard JDBC.
+     * Convert interval to num.
+     *
+     * @param result result
+     * @return result
+     */
+    @SuppressWarnings("unused")
+    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
+     */
+    @SuppressWarnings("unused")
+    public static int gsPasswordNotifyTime() {
+        return DEFAULT_PASSWORD_NOTIFY_TIME;
+    }
+}
diff --git 
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/postgresql/PostgreSQLFunctionRegister.java
 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/postgresql/PostgreSQLFunctionRegister.java
new file mode 100644
index 00000000000..715620453f2
--- /dev/null
+++ 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/postgresql/PostgreSQLFunctionRegister.java
@@ -0,0 +1,42 @@
+/*
+ * 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.shardingsphere.sqlfederation.optimizer.function.postgresql;
+
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.impl.ScalarFunctionImpl;
+import 
org.apache.shardingsphere.sqlfederation.optimizer.function.SQLFederationFunctionRegister;
+import 
org.apache.shardingsphere.sqlfederation.optimizer.function.postgresql.impl.PostgreSQLSystemFunction;
+
+/**
+ * PostgreSQL function register.
+ */
+public final class PostgreSQLFunctionRegister implements 
SQLFederationFunctionRegister {
+    
+    @Override
+    public void registerFunction(final SchemaPlus schemaPlus, final String 
schemaName) {
+        if ("pg_catalog".equalsIgnoreCase(schemaName)) {
+            schemaPlus.add("pg_catalog.pg_table_is_visible", 
ScalarFunctionImpl.create(PostgreSQLSystemFunction.class, "pgTableIsVisible"));
+            schemaPlus.add("pg_catalog.pg_get_userbyid", 
ScalarFunctionImpl.create(PostgreSQLSystemFunction.class, "pgGetUserById"));
+        }
+    }
+    
+    @Override
+    public String getDatabaseType() {
+        return "PostgreSQL";
+    }
+}
diff --git 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/converter/SQLFederationColumnTypeConverter.java
 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/postgresql/impl/PostgreSQLSystemFunction.java
similarity index 56%
copy from 
kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/converter/SQLFederationColumnTypeConverter.java
copy to 
kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/postgresql/impl/PostgreSQLSystemFunction.java
index e345af2142a..71e09a3abe0 100644
--- 
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/converter/SQLFederationColumnTypeConverter.java
+++ 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/postgresql/impl/PostgreSQLSystemFunction.java
@@ -15,32 +15,36 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.sqlfederation.resultset.converter;
+package 
org.apache.shardingsphere.sqlfederation.optimizer.function.postgresql.impl;
 
-import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPI;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
 
 /**
- * SQL federation column type converter.
+ * PostgreSQL system function.
  */
-public interface SQLFederationColumnTypeConverter extends DatabaseTypedSPI {
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class PostgreSQLSystemFunction {
     
     /**
-     * Convert column value.
+     * Mock pg_table_is_visible function.
      *
-     * @param columnValue column value
-     * @return converted column value
+     * @param oid oid
+     * @return true
      */
-    default Object convertColumnValue(Object columnValue) {
-        return columnValue;
+    @SuppressWarnings("unused")
+    public static boolean pgTableIsVisible(final Long oid) {
+        return true;
     }
     
     /**
-     * Convert column type.
+     * Mock pg_get_userbyid function.
      *
-     * @param columnType column type
-     * @return converted column type
+     * @param oid oid
+     * @return user name
      */
-    default int convertColumnType(int columnType) {
-        return columnType;
+    @SuppressWarnings("unused")
+    public static String pgGetUserById(final Long oid) {
+        return "mock user";
     }
 }
diff --git 
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/planner/util/SQLFederationFunctionUtils.java
 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/planner/util/SQLFederationFunctionUtils.java
deleted file mode 100644
index 0228352f728..00000000000
--- 
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/planner/util/SQLFederationFunctionUtils.java
+++ /dev/null
@@ -1,137 +0,0 @@
-/*
- * 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.shardingsphere.sqlfederation.optimizer.planner.util;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.apache.calcite.runtime.SqlFunctions;
-import org.apache.calcite.schema.SchemaPlus;
-import org.apache.calcite.schema.impl.ScalarFunctionImpl;
-import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
-import 
org.apache.shardingsphere.sqlfederation.optimizer.function.mysql.MySQLBinFunction;
-import 
org.apache.shardingsphere.sqlfederation.optimizer.function.mysql.MySQLBitCountFunction;
-
-/**
- * SQL federation function utility class.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class SQLFederationFunctionUtils {
-    
-    private static final int DEFAULT_PASSWORD_DEADLINE = 90;
-    
-    private static final int DEFAULT_PASSWORD_NOTIFY_TIME = 7;
-    
-    /**
-     * Registry user defined function.
-     *
-     * @param schemaName schema name
-     * @param schemaPlus schema plus
-     */
-    public static void registryUserDefinedFunction(final String schemaName, 
final SchemaPlus schemaPlus) {
-        schemaPlus.add("version", 
ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "version"));
-        schemaPlus.add("opengauss_version", 
ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, 
"openGaussVersion"));
-        schemaPlus.add("gs_password_deadline", 
ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, 
"gsPasswordDeadline"));
-        schemaPlus.add("intervaltonum", 
ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "intervalToNum"));
-        schemaPlus.add("gs_password_notifyTime", 
ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, 
"gsPasswordNotifyTime"));
-        schemaPlus.add("pg_catalog.gs_password_deadline", 
ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, 
"gsPasswordDeadline"));
-        schemaPlus.add("pg_catalog.intervaltonum", 
ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "intervalToNum"));
-        schemaPlus.add("pg_catalog.gs_password_notifyTime", 
ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, 
"gsPasswordNotifyTime"));
-        schemaPlus.add("bit_count", 
ScalarFunctionImpl.create(MySQLBitCountFunction.class, "bitCount"));
-        schemaPlus.add("atan", ScalarFunctionImpl.create(SqlFunctions.class, 
"atan2"));
-        schemaPlus.add("atan2", ScalarFunctionImpl.create(SqlFunctions.class, 
"atan"));
-        schemaPlus.add("bin", 
ScalarFunctionImpl.create(MySQLBinFunction.class, "bin"));
-        if ("pg_catalog".equalsIgnoreCase(schemaName)) {
-            schemaPlus.add("pg_catalog.pg_table_is_visible", 
ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, 
"pgTableIsVisible"));
-            schemaPlus.add("pg_catalog.pg_get_userbyid", 
ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "pgGetUserById"));
-        }
-    }
-    
-    /**
-     * Mock pg_table_is_visible function.
-     *
-     * @param oid oid
-     * @return true
-     */
-    @SuppressWarnings("unused")
-    public static boolean pgTableIsVisible(final Long oid) {
-        return true;
-    }
-    
-    /**
-     * Mock pg_get_userbyid function.
-     *
-     * @param oid oid
-     * @return user name
-     */
-    @SuppressWarnings("unused")
-    public static String pgGetUserById(final Long oid) {
-        return "mock user";
-    }
-    
-    /**
-     * Get version of ShardingSphere-Proxy.
-     *
-     * @return version message
-     */
-    public static String version() {
-        return "ShardingSphere-Proxy " + ShardingSphereVersion.VERSION + ("-" 
+ ShardingSphereVersion.BUILD_GIT_COMMIT_ID_ABBREV) + 
(ShardingSphereVersion.BUILD_GIT_DIRTY ? "-dirty" : "");
-    }
-    
-    /**
-     * Get version of ShardingSphere-Proxy for openGauss.
-     *
-     * @return version message
-     */
-    @SuppressWarnings("unused")
-    public static String openGaussVersion() {
-        return ShardingSphereVersion.VERSION;
-    }
-    
-    /**
-     * 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
-     */
-    @SuppressWarnings("unused")
-    public static int gsPasswordDeadline() {
-        return DEFAULT_PASSWORD_DEADLINE;
-    }
-    
-    /**
-     * The type interval is not supported in standard JDBC.
-     * Convert interval to num.
-     *
-     * @param result result
-     * @return result
-     */
-    @SuppressWarnings("unused")
-    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
-     */
-    @SuppressWarnings("unused")
-    public static int gsPasswordNotifyTime() {
-        return DEFAULT_PASSWORD_NOTIFY_TIME;
-    }
-}
diff --git 
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/planner/util/SQLFederationPlannerUtils.java
 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/planner/util/SQLFederationPlannerUtils.java
index 4041ad0a426..6701c4a729f 100644
--- 
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/planner/util/SQLFederationPlannerUtils.java
+++ 
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/planner/util/SQLFederationPlannerUtils.java
@@ -48,8 +48,10 @@ import org.apache.calcite.sql.validate.SqlValidatorUtil;
 import org.apache.calcite.sql2rel.SqlToRelConverter;
 import org.apache.calcite.sql2rel.SqlToRelConverter.Config;
 import org.apache.calcite.sql2rel.StandardConvertletTable;
+import 
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
 import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
 import org.apache.shardingsphere.parser.rule.SQLParserRule;
+import 
org.apache.shardingsphere.sqlfederation.optimizer.function.SQLFederationFunctionRegister;
 import 
org.apache.shardingsphere.sqlfederation.optimizer.metadata.view.ShardingSphereViewExpander;
 import 
org.apache.shardingsphere.sqlfederation.optimizer.planner.rule.converter.EnumerableModifyConverterRule;
 import 
org.apache.shardingsphere.sqlfederation.optimizer.planner.rule.converter.EnumerableScanConverterRule;
@@ -198,12 +200,14 @@ public final class SQLFederationPlannerUtils {
      * @param schema schema
      * @param relDataTypeFactory rel data type factory
      * @param connectionConfig connection config
+     * @param databaseType database type
      * @return calcite catalog reader
      */
-    public static CalciteCatalogReader createCatalogReader(final String 
schemaName, final Schema schema, final RelDataTypeFactory relDataTypeFactory, 
final CalciteConnectionConfig connectionConfig) {
+    public static CalciteCatalogReader createCatalogReader(final String 
schemaName, final Schema schema, final RelDataTypeFactory relDataTypeFactory, 
final CalciteConnectionConfig connectionConfig,
+                                                           final DatabaseType 
databaseType) {
         CalciteSchema rootSchema = CalciteSchema.createRootSchema(true);
         rootSchema.add(schemaName, schema);
-        SQLFederationFunctionUtils.registryUserDefinedFunction(schemaName, 
rootSchema.plus());
+        
DatabaseTypedSPILoader.findService(SQLFederationFunctionRegister.class, 
databaseType).ifPresent(optional -> 
optional.registerFunction(rootSchema.plus(), schemaName));
         return new CalciteCatalogReader(rootSchema, 
Collections.singletonList(schemaName), relDataTypeFactory, connectionConfig);
     }
     
diff --git 
a/kernel/sql-federation/optimizer/src/main/resources/META-INF/services/org.apache.shardingsphere.sqlfederation.optimizer.function.SQLFederationFunctionRegister
 
b/kernel/sql-federation/optimizer/src/main/resources/META-INF/services/org.apache.shardingsphere.sqlfederation.optimizer.function.SQLFederationFunctionRegister
new file mode 100644
index 00000000000..469299d5733
--- /dev/null
+++ 
b/kernel/sql-federation/optimizer/src/main/resources/META-INF/services/org.apache.shardingsphere.sqlfederation.optimizer.function.SQLFederationFunctionRegister
@@ -0,0 +1,20 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.sqlfederation.optimizer.function.mysql.MySQLFunctionRegister
+org.apache.shardingsphere.sqlfederation.optimizer.function.opengauss.OpenGaussFunctionRegister
+org.apache.shardingsphere.sqlfederation.optimizer.function.postgresql.PostgreSQLFunctionRegister
diff --git 
a/kernel/sql-federation/optimizer/src/test/java/org/apache/shardingsphere/sqlfederation/optimizer/it/SQLStatementCompilerIT.java
 
b/kernel/sql-federation/optimizer/src/test/java/org/apache/shardingsphere/sqlfederation/optimizer/it/SQLStatementCompilerIT.java
index 84a32953cbb..be105af3f96 100644
--- 
a/kernel/sql-federation/optimizer/src/test/java/org/apache/shardingsphere/sqlfederation/optimizer/it/SQLStatementCompilerIT.java
+++ 
b/kernel/sql-federation/optimizer/src/test/java/org/apache/shardingsphere/sqlfederation/optimizer/it/SQLStatementCompilerIT.java
@@ -235,7 +235,8 @@ class SQLStatementCompilerIT {
         RelDataTypeFactory relDataTypeFactory = new JavaTypeFactoryImpl();
         DatabaseType databaseType = 
TypedSPILoader.getService(DatabaseType.class, "H2");
         SQLFederationSchema sqlFederationSchema = new 
SQLFederationSchema(SCHEMA_NAME, schema, databaseType, new 
JavaTypeFactoryImpl());
-        CalciteCatalogReader catalogReader = 
SQLFederationPlannerUtils.createCatalogReader(SCHEMA_NAME, sqlFederationSchema, 
relDataTypeFactory, connectionConfig);
+        CalciteCatalogReader catalogReader =
+                SQLFederationPlannerUtils.createCatalogReader(SCHEMA_NAME, 
sqlFederationSchema, relDataTypeFactory, connectionConfig, databaseType);
         SqlValidator validator = 
SQLFederationPlannerUtils.createSqlValidator(catalogReader, relDataTypeFactory, 
databaseType, connectionConfig);
         RelOptCluster cluster = 
RelOptCluster.create(SQLFederationPlannerUtils.createVolcanoPlanner(), new 
RexBuilder(relDataTypeFactory));
         return 
SQLFederationPlannerUtils.createSqlToRelConverter(catalogReader, validator, 
cluster, mock(SQLParserRule.class), databaseType, false);


Reply via email to