You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by ki...@apache.org on 2022/01/12 09:47:29 UTC

[incubator-seatunnel] branch dev updated: [SeaTunnel #783][UT] Add SqlStatementSplitter test case (#930)

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

kirs pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-seatunnel.git


The following commit(s) were added to refs/heads/dev by this push:
     new 297345c  [SeaTunnel #783][UT] Add SqlStatementSplitter test case  (#930)
297345c is described below

commit 297345cb22e8d80e68a92042f058a6ce6d882e39
Author: xbkaishui <bi...@mycredigo.com>
AuthorDate: Wed Jan 12 17:47:21 2022 +0800

    [SeaTunnel #783][UT] Add SqlStatementSplitter test case  (#930)
    
    * fix sql splitter multi line bug, add spliter test
    
    * cr changes
    
    * revert split line changes and modify test case
    
    * fix test
    
    * remove hard code constants
    
    Co-authored-by: 飘辰 <pi...@trustbe.cn>
---
 seatunnel-core/seatunnel-core-sql/pom.xml          |  4 ++
 .../core/sql/splitter/SqlStatementSplitter.java    | 13 ++++---
 .../sql/splitter/SqlStatementSplitterTest.java     | 45 ++++++++++++++++++++++
 3 files changed, 57 insertions(+), 5 deletions(-)

diff --git a/seatunnel-core/seatunnel-core-sql/pom.xml b/seatunnel-core/seatunnel-core-sql/pom.xml
index 6b5877a..53c46ee 100644
--- a/seatunnel-core/seatunnel-core-sql/pom.xml
+++ b/seatunnel-core/seatunnel-core-sql/pom.xml
@@ -66,5 +66,9 @@
             <artifactId>flink-streaming-scala_${scala.binary.version}</artifactId>
             <version>${flink.version}</version>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
     </dependencies>
 </project>
diff --git a/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitter.java b/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitter.java
index cbc9e16..8982593 100644
--- a/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitter.java
+++ b/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitter.java
@@ -28,13 +28,16 @@ public class SqlStatementSplitter {
 
     private static final String COMMENT_MASK = "--.*$";
     private static final String BEGINNING_COMMENT_MASK = "^(\\s)*--.*$";
+    private static final String SEMICOLON = ";";
+    private static final String LINE_SEPARATOR = "\n";
+    private static final String EMPTY_STR = "";
 
     public static List<String> normalizeStatements(String content) {
         List<String> normalizedStatements = new ArrayList<>();
 
         for (String stmt : splitContent(content)) {
             stmt = stmt.trim();
-            if (stmt.endsWith(";")) {
+            if (stmt.endsWith(SEMICOLON)) {
                 stmt = stmt.substring(0, stmt.length() - 1).trim();
             }
 
@@ -50,7 +53,7 @@ public class SqlStatementSplitter {
         List<String> statements = new ArrayList<>();
         List<String> buffer = new ArrayList<>();
 
-        for (String line : content.split("\n")) {
+        for (String line : content.split(LINE_SEPARATOR)) {
             if (isEndOfStatement(line)) {
                 buffer.add(line);
                 statements.add(normalizeLine(buffer));
@@ -70,11 +73,11 @@ public class SqlStatementSplitter {
      */
     private static String normalizeLine(List<String> buffer) {
         return buffer.stream()
-                .map(statementLine -> statementLine.replaceAll(BEGINNING_COMMENT_MASK, ""))
-                .collect(Collectors.joining("\n"));
+                     .map(statementLine -> statementLine.replaceAll(BEGINNING_COMMENT_MASK, EMPTY_STR))
+                     .collect(Collectors.joining(LINE_SEPARATOR));
     }
 
     private static boolean isEndOfStatement(String line) {
-        return line.replaceAll(COMMENT_MASK, "").trim().endsWith(";");
+        return line.replaceAll(COMMENT_MASK, EMPTY_STR).trim().endsWith(SEMICOLON);
     }
 }
diff --git a/seatunnel-core/seatunnel-core-sql/src/test/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitterTest.java b/seatunnel-core/seatunnel-core-sql/src/test/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitterTest.java
new file mode 100644
index 0000000..ecd118c
--- /dev/null
+++ b/seatunnel-core/seatunnel-core-sql/src/test/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitterTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.seatunnel.core.sql.splitter;
+
+import java.util.List;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class SqlStatementSplitterTest {
+
+    @Test
+    public void normalizeStatementsWithMultiSqls() {
+        // this is bad case, multi sql should split by line \n
+        String sqlContent = "--test is a comment \n select * from dual; select now(); select * from logs where log_content like ';'";
+        List<String> sqls = SqlStatementSplitter.normalizeStatements(sqlContent);
+        assertEquals(1, sqls.size());
+        assertEquals("select * from dual; select now(); select * from logs where log_content like ';'", sqls.get(0));
+    }
+
+    @Test
+    public void normalizeStatementsWithMultiLines() {
+        String sqlContent = "--test is a comment \n select * from dual;\n select now();";
+        List<String> sqls = SqlStatementSplitter.normalizeStatements(sqlContent);
+        assertEquals(2, sqls.size());
+        assertEquals("select * from dual", sqls.get(0));
+        assertEquals("select now()", sqls.get(1));
+    }
+
+}