You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/09/20 04:57:57 UTC

(shardingsphere) branch master updated: Add openGauss unsupported function check for SQL Federation (#39905)

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

terrymanu 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 091f86b360d Add openGauss unsupported function check for SQL Federation (#39905)
091f86b360d is described below

commit 091f86b360d94981e624f9995b457836720dc4ac
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Sep 20 12:57:51 2026 +0800

    Add openGauss unsupported function check for SQL Federation (#39905)
    
    * Remove duplicate sharding rule configuration test
    
    * Add openGauss unsupported function check for SQL Federation
    
    * Add openGauss unsupported function check for SQL Federation
---
 .../sql/ast/converter/SQLNodeConverterEngine.java  | 39 ++++++++++++-
 .../DialectSQLFederationFunctionRegister.java      | 14 +++++
 .../ast/converter/SQLNodeConverterEngineTest.java  | 68 ++++++++++++++++++++++
 .../DialectSQLFederationFunctionRegisterTest.java} | 25 ++++----
 .../OpenGaussSQLFederationFunctionRegister.java    |  8 +++
 ...OpenGaussSQLFederationFunctionRegisterTest.java |  6 ++
 6 files changed, 144 insertions(+), 16 deletions(-)

diff --git a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/SQLNodeConverterEngine.java b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/SQLNodeConverterEngine.java
index a1398941134..e8b4027d83a 100644
--- a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/SQLNodeConverterEngine.java
+++ b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/SQLNodeConverterEngine.java
@@ -19,7 +19,14 @@ package org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlFunction;
 import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.util.SqlBasicVisitor;
+import org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
+import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
+import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
 import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.DALStatement;
 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.ExplainStatement;
@@ -36,7 +43,9 @@ import org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.statem
 import org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.statement.type.MergeStatementConverter;
 import org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.statement.type.SelectStatementConverter;
 import org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.statement.type.UpdateStatementConverter;
+import org.apache.shardingsphere.sqlfederation.compiler.sql.function.DialectSQLFederationFunctionRegister;
 
+import java.util.Collection;
 import java.util.Optional;
 
 /**
@@ -51,7 +60,9 @@ public final class SQLNodeConverterEngine {
      * @param sqlStatement SQL sqlStatement to be converted
      * @return sqlNode converted SQL node
      * @throws SQLFederationSQLNodeConvertException SQL federation SQL node convert exception
+     * @throws UnsupportedSQLOperationException unsupported SQL operation exception
      */
+    @HighFrequencyInvocation
     public static SqlNode convert(final SQLStatement sqlStatement) {
         Optional<SqlNode> result = Optional.empty();
         if (sqlStatement instanceof DMLStatement) {
@@ -59,7 +70,9 @@ public final class SQLNodeConverterEngine {
         } else if (sqlStatement instanceof DALStatement) {
             result = convert((DALStatement) sqlStatement);
         }
-        return result.orElseThrow(() -> new SQLFederationSQLNodeConvertException(sqlStatement));
+        SqlNode sqlNode = result.orElseThrow(() -> new SQLFederationSQLNodeConvertException(sqlStatement));
+        checkUnsupportedFunctions(sqlStatement, sqlNode);
+        return sqlNode;
     }
     
     private static Optional<SqlNode> convert(final DMLStatement sqlStatement) {
@@ -87,4 +100,28 @@ public final class SQLNodeConverterEngine {
         }
         return Optional.empty();
     }
+    
+    private static void checkUnsupportedFunctions(final SQLStatement sqlStatement, final SqlNode sqlNode) {
+        Optional<DialectSQLFederationFunctionRegister> functionRegister = DatabaseTypedSPILoader.findService(DialectSQLFederationFunctionRegister.class, sqlStatement.getDatabaseType());
+        if (!functionRegister.isPresent()) {
+            return;
+        }
+        Collection<String> unsupportedFunctionNames = functionRegister.get().getUnsupportedFunctionNames();
+        if (unsupportedFunctionNames.isEmpty()) {
+            return;
+        }
+        sqlNode.accept(new SqlBasicVisitor<Void>() {
+            
+            @Override
+            public Void visit(final SqlCall call) {
+                if (call.getOperator() instanceof SqlFunction) {
+                    for (String each : unsupportedFunctionNames) {
+                        ShardingSpherePreconditions.checkState(!each.equalsIgnoreCase(call.getOperator().getName()),
+                                () -> new UnsupportedSQLOperationException(String.format("%s %s function in SQL Federation", sqlStatement.getDatabaseType().getType(), each)));
+                    }
+                }
+                return super.visit(call);
+            }
+        });
+    }
 }
diff --git a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/function/DialectSQLFederationFunctionRegister.java b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/function/DialectSQLFederationFunctionRegister.java
index b27498a7ec1..d83250c4fd1 100644
--- a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/function/DialectSQLFederationFunctionRegister.java
+++ b/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/function/DialectSQLFederationFunctionRegister.java
@@ -21,6 +21,9 @@ import org.apache.calcite.schema.SchemaPlus;
 import org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPI;
 import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
 
+import java.util.Collection;
+import java.util.Collections;
+
 /**
  * Dialect SQL federation function register.
  */
@@ -34,4 +37,15 @@ public interface DialectSQLFederationFunctionRegister extends DatabaseTypedSPI {
      * @param schemaName schema name
      */
     void registerFunction(SchemaPlus schemaPlus, String schemaName);
+    
+    /**
+     * Get names of functions that are unsupported by SQL Federation.
+     *
+     * <p>Function names are matched case-insensitively.</p>
+     *
+     * @return names of functions that are unsupported by SQL Federation
+     */
+    default Collection<String> getUnsupportedFunctionNames() {
+        return Collections.emptyList();
+    }
 }
diff --git a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/SQLNodeConverterEngineTest.java b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/SQLNodeConverterEngineTest.java
index cc3c1a616b1..336a7b2aabc 100644
--- a/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/SQLNodeConverterEngineTest.java
+++ b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/ast/converter/SQLNodeConverterEngineTest.java
@@ -17,8 +17,17 @@
 
 package org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter;
 
+import org.apache.calcite.sql.SqlBasicCall;
+import org.apache.calcite.sql.SqlFunctionCategory;
+import org.apache.calcite.sql.SqlIdentifier;
 import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlOrderBy;
+import org.apache.calcite.sql.SqlUnresolvedFunction;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
 import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
 import org.apache.shardingsphere.sql.parser.statement.core.statement.type.dal.DALStatement;
@@ -36,15 +45,22 @@ import org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.statem
 import org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.statement.type.MergeStatementConverter;
 import org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.statement.type.SelectStatementConverter;
 import org.apache.shardingsphere.sqlfederation.compiler.sql.ast.converter.statement.type.UpdateStatementConverter;
+import org.apache.shardingsphere.sqlfederation.compiler.sql.function.DialectSQLFederationFunctionRegister;
 import org.junit.jupiter.api.Test;
 import org.mockito.MockedConstruction;
+import org.mockito.MockedStatic;
+
+import java.util.Collections;
+import java.util.Optional;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.mockConstruction;
+import static org.mockito.Mockito.mockStatic;
 import static org.mockito.Mockito.when;
 
 class SQLNodeConverterEngineTest {
@@ -98,6 +114,58 @@ class SQLNodeConverterEngineTest {
         }
     }
     
+    @Test
+    void assertConvertUnsupportedFunction() {
+        SqlNode sqlNode = createOrderByWithFunction("nlssort");
+        DialectSQLFederationFunctionRegister register = mock(DialectSQLFederationFunctionRegister.class);
+        when(register.getUnsupportedFunctionNames()).thenReturn(Collections.singleton("NLSSORT"));
+        SelectStatement selectStatement = SelectStatement.builder().databaseType(databaseType).build();
+        try (
+                MockedStatic<DatabaseTypedSPILoader> databaseTypedSPILoader = mockStatic(DatabaseTypedSPILoader.class, CALLS_REAL_METHODS);
+                MockedConstruction<SelectStatementConverter> ignored = mockConstruction(SelectStatementConverter.class,
+                        (mock, context) -> when(mock.convert(any(SelectStatement.class))).thenReturn(sqlNode))) {
+            databaseTypedSPILoader.when(() -> DatabaseTypedSPILoader.findService(DialectSQLFederationFunctionRegister.class, databaseType)).thenReturn(Optional.of(register));
+            UnsupportedSQLOperationException actual = assertThrows(UnsupportedSQLOperationException.class, () -> SQLNodeConverterEngine.convert(selectStatement));
+            assertThat(actual.getMessage(), is("Unsupported SQL operation: FIXTURE NLSSORT function in SQL Federation."));
+        }
+    }
+    
+    @Test
+    void assertConvertUnconfiguredFunction() {
+        SqlNode sqlNode = createOrderByWithFunction("foo_func");
+        DialectSQLFederationFunctionRegister register = mock(DialectSQLFederationFunctionRegister.class);
+        when(register.getUnsupportedFunctionNames()).thenReturn(Collections.singleton("NLSSORT"));
+        SelectStatement selectStatement = SelectStatement.builder().databaseType(databaseType).build();
+        try (
+                MockedStatic<DatabaseTypedSPILoader> databaseTypedSPILoader = mockStatic(DatabaseTypedSPILoader.class, CALLS_REAL_METHODS);
+                MockedConstruction<SelectStatementConverter> ignored = mockConstruction(SelectStatementConverter.class,
+                        (mock, context) -> when(mock.convert(any(SelectStatement.class))).thenReturn(sqlNode))) {
+            databaseTypedSPILoader.when(() -> DatabaseTypedSPILoader.findService(DialectSQLFederationFunctionRegister.class, databaseType)).thenReturn(Optional.of(register));
+            assertThat(SQLNodeConverterEngine.convert(selectStatement), is(sqlNode));
+        }
+    }
+    
+    @Test
+    void assertConvertWithoutUnsupportedFunctions() {
+        SqlNode sqlNode = mock(SqlNode.class);
+        DialectSQLFederationFunctionRegister register = mock(DialectSQLFederationFunctionRegister.class);
+        when(register.getUnsupportedFunctionNames()).thenReturn(Collections.emptyList());
+        SelectStatement selectStatement = SelectStatement.builder().databaseType(databaseType).build();
+        try (
+                MockedStatic<DatabaseTypedSPILoader> databaseTypedSPILoader = mockStatic(DatabaseTypedSPILoader.class, CALLS_REAL_METHODS);
+                MockedConstruction<SelectStatementConverter> ignored = mockConstruction(SelectStatementConverter.class,
+                        (mock, context) -> when(mock.convert(any(SelectStatement.class))).thenReturn(sqlNode))) {
+            databaseTypedSPILoader.when(() -> DatabaseTypedSPILoader.findService(DialectSQLFederationFunctionRegister.class, databaseType)).thenReturn(Optional.of(register));
+            assertThat(SQLNodeConverterEngine.convert(selectStatement), is(sqlNode));
+        }
+    }
+    
+    private SqlNode createOrderByWithFunction(final String functionName) {
+        SqlUnresolvedFunction function = new SqlUnresolvedFunction(new SqlIdentifier(functionName, SqlParserPos.ZERO), null, null, null, null, SqlFunctionCategory.USER_DEFINED_FUNCTION);
+        SqlNode functionCall = new SqlBasicCall(function, Collections.emptyList(), SqlParserPos.ZERO);
+        return new SqlOrderBy(SqlParserPos.ZERO, mock(SqlNode.class), new SqlNodeList(Collections.singleton(functionCall), SqlParserPos.ZERO), null, null);
+    }
+    
     @Test
     void assertConvertUnsupportedDMLThrowsException() {
         assertThrows(SQLFederationSQLNodeConvertException.class, () -> SQLNodeConverterEngine.convert(mock(DMLStatement.class)));
diff --git a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/function/DialectSQLFederationFunctionRegister.java b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/function/DialectSQLFederationFunctionRegisterTest.java
similarity index 62%
copy from kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/function/DialectSQLFederationFunctionRegister.java
copy to kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/function/DialectSQLFederationFunctionRegisterTest.java
index b27498a7ec1..7a895508210 100644
--- a/kernel/sql-federation/compiler/src/main/java/org/apache/shardingsphere/sqlfederation/compiler/sql/function/DialectSQLFederationFunctionRegister.java
+++ b/kernel/sql-federation/compiler/src/test/java/org/apache/shardingsphere/sqlfederation/compiler/sql/function/DialectSQLFederationFunctionRegisterTest.java
@@ -17,21 +17,16 @@
 
 package org.apache.shardingsphere.sqlfederation.compiler.sql.function;
 
-import org.apache.calcite.schema.SchemaPlus;
-import org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPI;
-import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
+import org.junit.jupiter.api.Test;
 
-/**
- * Dialect SQL federation function register.
- */
-@SingletonSPI
-public interface DialectSQLFederationFunctionRegister extends DatabaseTypedSPI {
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class DialectSQLFederationFunctionRegisterTest {
     
-    /**
-     * Register function.
-     *
-     * @param schemaPlus schema plus
-     * @param schemaName schema name
-     */
-    void registerFunction(SchemaPlus schemaPlus, String schemaName);
+    @Test
+    void assertGetUnsupportedFunctionNames() {
+        DialectSQLFederationFunctionRegister register = (schemaPlus, schemaName) -> {
+        };
+        assertTrue(register.getUnsupportedFunctionNames().isEmpty());
+    }
 }
diff --git a/kernel/sql-federation/dialect/opengauss/src/main/java/org/apache/shardingsphere/sqlfederation/opengauss/OpenGaussSQLFederationFunctionRegister.java b/kernel/sql-federation/dialect/opengauss/src/main/java/org/apache/shardingsphere/sqlfederation/opengauss/OpenGaussSQLFederationFunctionRegister.java
index d7c01733e61..f39411179f1 100644
--- a/kernel/sql-federation/dialect/opengauss/src/main/java/org/apache/shardingsphere/sqlfederation/opengauss/OpenGaussSQLFederationFunctionRegister.java
+++ b/kernel/sql-federation/dialect/opengauss/src/main/java/org/apache/shardingsphere/sqlfederation/opengauss/OpenGaussSQLFederationFunctionRegister.java
@@ -23,6 +23,9 @@ import org.apache.shardingsphere.sqlfederation.compiler.sql.function.DialectSQLF
 import org.apache.shardingsphere.sqlfederation.compiler.sql.function.opengauss.impl.OpenGaussSystemFunction;
 import org.apache.shardingsphere.sqlfederation.postgresql.PostgreSQLSQLFederationFunctionRegister;
 
+import java.util.Collection;
+import java.util.Collections;
+
 /**
  * SQL federation function register for opengauss.
  */
@@ -42,6 +45,11 @@ public final class OpenGaussSQLFederationFunctionRegister implements DialectSQLF
         delegate.registerFunction(schemaPlus, schemaName);
     }
     
+    @Override
+    public Collection<String> getUnsupportedFunctionNames() {
+        return Collections.singleton("NLSSORT");
+    }
+    
     @Override
     public String getDatabaseType() {
         return "openGauss";
diff --git a/kernel/sql-federation/dialect/opengauss/src/test/java/org/apache/shardingsphere/sqlfederation/opengauss/OpenGaussSQLFederationFunctionRegisterTest.java b/kernel/sql-federation/dialect/opengauss/src/test/java/org/apache/shardingsphere/sqlfederation/opengauss/OpenGaussSQLFederationFunctionRegisterTest.java
index 08f7928567b..2bd03d7feea 100644
--- a/kernel/sql-federation/dialect/opengauss/src/test/java/org/apache/shardingsphere/sqlfederation/opengauss/OpenGaussSQLFederationFunctionRegisterTest.java
+++ b/kernel/sql-federation/dialect/opengauss/src/test/java/org/apache/shardingsphere/sqlfederation/opengauss/OpenGaussSQLFederationFunctionRegisterTest.java
@@ -32,6 +32,7 @@ import org.junit.jupiter.api.Test;
 import java.util.Collection;
 
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.is;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
@@ -67,6 +68,11 @@ class OpenGaussSQLFederationFunctionRegisterTest {
         assertThat(schemaPlus.getFunctions("pg_get_userbyid").size(), is(0));
     }
     
+    @Test
+    void assertGetUnsupportedFunctionNames() {
+        assertThat(register.getUnsupportedFunctionNames(), contains("NLSSORT"));
+    }
+    
     private void assertFunction(final Collection<Function> functions, final Class<?> expectedClass, final String expectedMethod) {
         assertThat(functions.size(), is(1));
         ScalarFunctionImpl actualFunction = (ScalarFunctionImpl) functions.iterator().next();