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

[shardingsphere] branch master updated: Restructure SQL filter of loading cases (#22100)

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

sunnianjun 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 d52c20893d4 Restructure SQL filter of loading cases (#22100)
d52c20893d4 is described below

commit d52c20893d4e4bf6baa37db5b97fc2c51c2b4ac9
Author: Junfeng <i...@jacob953.com>
AuthorDate: Sat Nov 12 15:34:44 2022 +0800

    Restructure SQL filter of loading cases (#22100)
    
    * feat: add 3rd param sqlCaseResultURI & update sqlFilter
    
    * feat: update loader with sqlFilter
    
    * feat: update dynamic loading with sqlFilter
    
    Co-authored-by: Jacob953 <ja...@csu.edu.cn>
---
 .../DynamicLoadingSQLParserParameterizedTest.java  | 77 +++++++++++++++++-----
 .../parser/loader/DynamicSQLCaseGitHubLoader.java  | 14 ++--
 .../parser/loader/DynamicSQLCaseGiteeLoader.java   | 14 ++--
 .../loader/DynamicSQLCaseLoaderStrategy.java       |  6 +-
 .../parser/loader/DynamicSQLCaseLocalLoader.java   |  8 ++-
 .../DynamicLoadingMySQLParserParameterizedIT.java  |  5 +-
 ...amicLoadingPostgreSQLParserParameterizedIT.java |  4 +-
 7 files changed, 96 insertions(+), 32 deletions(-)

diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/base/DynamicLoadingSQLParserParameterizedTest.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/base/DynamicLoadingSQLParserParameterizedTest.java
index c736a0c7a57..c09a344f317 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/base/DynamicLoadingSQLParserParameterizedTest.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/base/DynamicLoadingSQLParserParameterizedTest.java
@@ -37,6 +37,7 @@ import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.HashMap;
 import java.util.Properties;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
@@ -54,19 +55,30 @@ public abstract class DynamicLoadingSQLParserParameterizedTest {
     // TODO this will refactor as an abstract
     private final SQLParserResultProcessor resultGenerator;
     
-    protected static Collection<Object[]> getTestParameters(final String sqlCaseAPI, final URI sqlCaseURI) {
+    protected static Collection<Object[]> getTestParameters(final String sqlCaseAPI, final URI sqlCaseTestURI, final URI sqlCaseResultURI) {
         Collection<Object[]> result = new LinkedList<>();
         if (sqlCaseAPI.isEmpty()) {
-            result.addAll(getSQLCases("localFile", getContent(sqlCaseURI)));
+            result.addAll(getSQLCases("localFile", getContent(sqlCaseTestURI), ""));
         } else {
-            for (Map<String, String> each : getResponse(sqlCaseAPI, sqlCaseURI)) {
+            Map<String, String> resultResponse = getResultResponse(sqlCaseAPI, sqlCaseResultURI);
+            for (Map<String, String> each : getResponse(sqlCaseAPI, sqlCaseTestURI)) {
                 String sqlCaseFileName = each.get("name").split("\\.")[0];
-                String sqlCaseFileContent = getContent(URI.create(each.get("download_url")));
-                result.addAll(getSQLCases(sqlCaseFileName, sqlCaseFileContent));
+                String sqlCaseTestFileContent = getContent(URI.create(each.get("download_url")));
+                String sqlCaseResultDownloadURL = resultResponse.get(sqlCaseFileName);
+                String sqlCaseResultFileContent = null == sqlCaseResultDownloadURL ? "" : getContent(URI.create(sqlCaseResultDownloadURL));
+                result.addAll(getSQLCases(sqlCaseFileName, sqlCaseTestFileContent, sqlCaseResultFileContent));
             }
         }
         if (result.isEmpty()) {
-            result.add(new Object[]{null, null});
+            result.add(new Object[]{"", ""});
+        }
+        return result;
+    }
+    
+    protected static Map<String, String> getResultResponse(final String sqlCaseAPI, final URI sqlCaseURI) {
+        Map<String, String> result = new HashMap<>();
+        for (Map<String, String> each : getResponse(sqlCaseAPI, sqlCaseURI)) {
+            result.put(each.get("name").split("\\.")[0], each.get("download_url"));
         }
         return result;
     }
@@ -112,22 +124,57 @@ public abstract class DynamicLoadingSQLParserParameterizedTest {
         return result;
     }
     
-    protected static Collection<Object[]> getSQLCases(final String sqlCaseFileName, final String sqlCaseFileContent) {
+    protected static Collection<Object[]> getSQLCases(final String sqlCaseFileName, final String sqlCaseTestFileContent, final String sqlCaseResultFileContent) {
         Collection<Object[]> result = new LinkedList<>();
-        String[] lines = sqlCaseFileContent.split("\n");
+        String[] testLines = sqlCaseTestFileContent.split("\n");
+        String[] resultLines = sqlCaseResultFileContent.split("\n");
+        String completedSQL = "";
         int sqlCaseEnum = 1;
-        for (int i = 0; i < lines.length; i++) {
-            if (isStatement(lines[i]) && (i + 1 == lines.length || !lines[i + 1].contains("ERROR"))) {
-                String sqlCaseId = sqlCaseFileName + sqlCaseEnum;
-                result.add(new Object[]{sqlCaseId, lines[i]});
-                sqlCaseEnum++;
+        int statementLines = 0;
+        int resultIndex = 0;
+        boolean inProcedure = false;
+        for (String testLine : testLines) {
+            inProcedure = isInProcedure(inProcedure, testLine.trim());
+            completedSQL = getStatement(completedSQL, testLine.trim(), inProcedure);
+            statementLines = completedSQL.isEmpty() ? 0 : statementLines + 1;
+            if (completedSQL.contains(";") && !inProcedure) {
+                resultIndex = searchResult(resultIndex, resultLines, completedSQL, statementLines);
+                if (resultIndex >= resultLines.length || !resultLines[resultIndex].contains("ERROR")) {
+                    String sqlCaseId = sqlCaseFileName + sqlCaseEnum;
+                    result.add(new Object[]{sqlCaseId, completedSQL});
+                    sqlCaseEnum++;
+                }
+                completedSQL = "";
+                statementLines = 0;
             }
         }
         return result;
     }
     
-    private static boolean isStatement(final String statement) {
-        return !statement.isEmpty() && Character.isLetter(statement.charAt(0)) && statement.charAt(statement.length() - 1) == ';';
+    private static boolean isInProcedure(final boolean inProcedure, final String statementLines) {
+        if (statementLines.contains("{") && statementLines.contains("}")) {
+            return inProcedure;
+        }
+        return statementLines.contains("{") || statementLines.contains("}") || statementLines.contains("$$") ? !inProcedure : inProcedure;
+    }
+    
+    private static int searchResult(final int resultIndex, final String[] resultLines, final String completedSQL, final int statementLines) {
+        int index = resultIndex;
+        while (index < resultLines.length && !completedSQL.startsWith(resultLines[index].trim())) {
+            index++;
+        }
+        if (index != resultLines.length) {
+            return index + statementLines;
+        }
+        return resultIndex;
+    }
+    
+    private static String getStatement(final String completedSQL, final String statementTest, final boolean inProcedure) {
+        return (statementTest.isEmpty() || isComment(statementTest)) && !inProcedure ? "" : completedSQL + statementTest + " ";
+    }
+    
+    private static boolean isComment(final String statement) {
+        return statement.startsWith("#") || statement.startsWith("/") || statement.startsWith("--") || statement.startsWith(":") || statement.startsWith("\\");
     }
     
     @Test
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGitHubLoader.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGitHubLoader.java
index 7acfc3b08ca..6b30d86b430 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGitHubLoader.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGitHubLoader.java
@@ -34,16 +34,20 @@ public class DynamicSQLCaseGitHubLoader extends DynamicLoadingSQLParserParameter
     /**
      * Get test parameters.
      *
-     * @param sqlCaseURI the URI of sql case
+     * @param sqlCaseTestURI the URI of sql test case
+     *
+     * @param sqlCaseResultURI the URI of sql result case
      *
      * @return Test cases from GitHub.
      **/
-    public Collection<Object[]> getTestParameters(final URI sqlCaseURI) {
+    public Collection<Object[]> getTestParameters(final URI sqlCaseTestURI, final URI sqlCaseResultURI) {
         Collection<Object[]> result = new LinkedList<>();
-        for (Map<String, String> each : getResponse("https://api.github.com/repos/", sqlCaseURI)) {
+        Map<String, String> resultResponse = getResultResponse("https://api.github.com/repos/", sqlCaseResultURI);
+        for (Map<String, String> each : getResponse("https://api.github.com/repos/", sqlCaseTestURI)) {
             String sqlCaseFileName = each.get("name").split("\\.")[0];
-            String sqlCaseFileContent = getContent(URI.create(each.get("download_url")));
-            result.addAll(getSQLCases(sqlCaseFileName, sqlCaseFileContent));
+            String sqlCaseTestFileContent = getContent(URI.create(each.get("download_url")));
+            String sqlCaseResultFileContent = getContent(URI.create(resultResponse.get(each.get("name"))));
+            result.addAll(getSQLCases(sqlCaseFileName, sqlCaseTestFileContent, sqlCaseResultFileContent));
         }
         if (result.isEmpty()) {
             result.add(new Object[]{"", ""});
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGiteeLoader.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGiteeLoader.java
index 9acfae04da5..3db9fdf01eb 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGiteeLoader.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGiteeLoader.java
@@ -34,16 +34,20 @@ public class DynamicSQLCaseGiteeLoader extends DynamicLoadingSQLParserParameteri
     /**
      * Get test parameters.
      *
-     * @param sqlCaseURI the URI of sql case
+     * @param sqlCaseTestURI the URI of sql test case
+     *
+     * @param sqlCaseResultURI the URI of sql result case
      *
      * @return Test cases from Gitee.
      **/
-    public Collection<Object[]> getTestParameters(final URI sqlCaseURI) {
+    public Collection<Object[]> getTestParameters(final URI sqlCaseTestURI, final URI sqlCaseResultURI) {
         Collection<Object[]> result = new LinkedList<>();
-        for (Map<String, String> each : getResponse("https://gitee.com/api/v5/repos/", sqlCaseURI)) {
+        Map<String, String> resultResponse = getResultResponse("https://gitee.com/api/v5/repos/", sqlCaseResultURI);
+        for (Map<String, String> each : getResponse("https://gitee.com/api/v5/repos/", sqlCaseTestURI)) {
             String sqlCaseFileName = each.get("name").split("\\.")[0];
-            String sqlCaseFileContent = getContent(URI.create(each.get("download_url")));
-            result.addAll(getSQLCases(sqlCaseFileName, sqlCaseFileContent));
+            String sqlCaseTestFileContent = getContent(URI.create(each.get("download_url")));
+            String sqlCaseResultFileContent = getContent(URI.create(resultResponse.get(each.get("name"))));
+            result.addAll(getSQLCases(sqlCaseFileName, sqlCaseTestFileContent, sqlCaseResultFileContent));
         }
         if (result.isEmpty()) {
             result.add(new Object[]{"", ""});
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseLoaderStrategy.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseLoaderStrategy.java
index 5c6ba2c4f30..a02aea56f33 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseLoaderStrategy.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseLoaderStrategy.java
@@ -25,9 +25,11 @@ public interface DynamicSQLCaseLoaderStrategy {
     /**
      * Get test parameters.
      *
-     * @param sqlCaseURI the URI of sql case
+     * @param sqlCaseTestURI the URI of sql test case
+     *
+     * @param sqlCaseResultURI the URI of sql result case
      *
      * @return Test cases from with strategy
      */
-    Collection<Object[]> getTestParameters(URI sqlCaseURI);
+    Collection<Object[]> getTestParameters(URI sqlCaseTestURI, URI sqlCaseResultURI);
 }
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseLocalLoader.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseLocalLoader.java
index 07c2fb2688e..7d0d59668b9 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseLocalLoader.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseLocalLoader.java
@@ -33,13 +33,15 @@ public class DynamicSQLCaseLocalLoader extends DynamicLoadingSQLParserParameteri
     /**
      * Get test parameters.
      *
-     * @param sqlCaseURI the URI of sql case
+     * @param sqlCaseTestURI the URI of sql test case
+     *
+     * @param sqlCaseResultURI the URI of sql result case
      *
      * @return Test cases from localhost.
      **/
-    public Collection<Object[]> getTestParameters(final URI sqlCaseURI) {
+    public Collection<Object[]> getTestParameters(final URI sqlCaseTestURI, final URI sqlCaseResultURI) {
         Collection<Object[]> result = new LinkedList<>();
-        result.addAll(getSQLCases("localFile", getContent(sqlCaseURI)));
+        result.addAll(getSQLCases("localFile", getContent(sqlCaseTestURI), getContent(sqlCaseResultURI)));
         if (result.isEmpty()) {
             result.add(new Object[]{"", ""});
         }
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/mysql/DynamicLoadingMySQLParserParameterizedIT.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/mysql/DynamicLoadingMySQLParserParameterizedIT.java
index 7f1d0d31b98..bc12d049e41 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/mysql/DynamicLoadingMySQLParserParameterizedIT.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/mysql/DynamicLoadingMySQLParserParameterizedIT.java
@@ -43,7 +43,10 @@ public final class DynamicLoadingMySQLParserParameterizedIT extends DynamicLoadi
     @Parameters(name = "{0} (MySQL) -> {1}")
     public static Collection<Object[]> getTestParameters() {
         return IntegrationTestEnvironment.getInstance().isSqlParserITEnabled()
-                ? DynamicLoadingSQLParserParameterizedTest.getTestParameters("https://api.github.com/repos/", URI.create("https://github.com/mysql/mysql-server/tree/8.0/mysql-test/r"))
+                ? DynamicLoadingSQLParserParameterizedTest.getTestParameters(
+                        "https://api.github.com/repos/",
+                        URI.create("https://github.com/mysql/mysql-server/tree/8.0/mysql-test/t"),
+                        URI.create("https://github.com/mysql/mysql-server/tree/8.0/mysql-test/r"))
                 : Collections.emptyList();
     }
 }
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/postgresql/DynamicLoadingPostgreSQLParserParameterizedIT.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/postgresql/DynamicLoadingPostgreSQLParserParameterizedIT.java
index 6c7e1bd1632..92db6cd8a67 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/postgresql/DynamicLoadingPostgreSQLParserParameterizedIT.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/postgresql/DynamicLoadingPostgreSQLParserParameterizedIT.java
@@ -44,7 +44,9 @@ public final class DynamicLoadingPostgreSQLParserParameterizedIT extends Dynamic
     public static Collection<Object[]> getTestParameters() {
         return IntegrationTestEnvironment.getInstance().isSqlParserITEnabled()
                 ? DynamicLoadingSQLParserParameterizedTest.getTestParameters(
-                        "https://api.github.com/repos/", URI.create("https://github.com/postgres/postgres/tree/master/src/test/regress/expected"))
+                        "https://api.github.com/repos/",
+                        URI.create("https://github.com/postgres/postgres/tree/master/src/test/regress/sql"),
+                        URI.create("https://github.com/postgres/postgres/tree/master/src/test/regress/expected"))
                 : Collections.emptyList();
     }
 }