leo65535 commented on a change in pull request #930:
URL:
https://github.com/apache/incubator-seatunnel/pull/930#discussion_r780090186
##########
File path:
seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitter.java
##########
@@ -50,31 +54,32 @@
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));
+ statements.addAll(normalizeLine(buffer));
buffer.clear();
} else {
buffer.add(line);
}
}
if (!buffer.isEmpty()) {
- statements.add(normalizeLine(buffer));
+ statements.addAll(normalizeLine(buffer));
}
return statements;
}
/**
* Remove comment lines.
*/
- private static String normalizeLine(List<String> buffer) {
- return buffer.stream()
- .map(statementLine ->
statementLine.replaceAll(BEGINNING_COMMENT_MASK, ""))
- .collect(Collectors.joining("\n"));
+ private static List<String> normalizeLine(List<String> buffer) {
Review comment:
hi, if sql statements like following, it will return wrong statements.
```
String sqlContent = "--test is a comment \n select * from dual; select
now(); select * from logs where log_content like ';'";
```
##########
File path:
seatunnel-core/seatunnel-core-sql/src/test/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitterTest.java
##########
@@ -0,0 +1,46 @@
+/*
+ * 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.hamcrest.CoreMatchers.is;
Review comment:
Good job.
##########
File path:
seatunnel-core/seatunnel-core-sql/src/test/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitterTest.java
##########
@@ -0,0 +1,46 @@
+/*
+ * 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.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class SqlStatementSplitterTest {
+
+ @Test
+ public void normalizeStatementsWithMultiSqls() {
+ String sqlContent = "--test is a comment \n select * from dual; select
now();";
+ List<String> sqls =
SqlStatementSplitter.normalizeStatements(sqlContent);
+ assertThat(sqls.size(), is(2));
Review comment:
Good.
##########
File path:
seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitter.java
##########
@@ -50,31 +54,32 @@
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));
+ statements.addAll(normalizeLine(buffer));
buffer.clear();
} else {
buffer.add(line);
}
}
if (!buffer.isEmpty()) {
- statements.add(normalizeLine(buffer));
+ statements.addAll(normalizeLine(buffer));
}
return statements;
}
/**
* Remove comment lines.
*/
- private static String normalizeLine(List<String> buffer) {
- return buffer.stream()
- .map(statementLine ->
statementLine.replaceAll(BEGINNING_COMMENT_MASK, ""))
- .collect(Collectors.joining("\n"));
+ private static List<String> normalizeLine(List<String> buffer) {
Review comment:
hi, if sql statements like following, it will return wrong statements.
```
String sqlContent = "--test is a comment \n select * from dual; select
now(); select * from logs where log_content like ';'";
```
And, if `--` is the prefix of the statement, we should not handle it.

##########
File path:
seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitter.java
##########
@@ -50,31 +54,32 @@
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));
+ statements.addAll(normalizeLine(buffer));
buffer.clear();
} else {
buffer.add(line);
}
}
if (!buffer.isEmpty()) {
- statements.add(normalizeLine(buffer));
+ statements.addAll(normalizeLine(buffer));
}
return statements;
}
/**
* Remove comment lines.
*/
- private static String normalizeLine(List<String> buffer) {
- return buffer.stream()
- .map(statementLine ->
statementLine.replaceAll(BEGINNING_COMMENT_MASK, ""))
- .collect(Collectors.joining("\n"));
+ private static List<String> normalizeLine(List<String> buffer) {
Review comment:
hi, the sql statements like following, it will return wrong statements
after apply this patch.
```
String sqlContent = "--test is a comment \n select * from dual; select
now(); select * from logs where log_content like ';'";
```
And, if `--` is the prefix of the statement, we should not handle it.

##########
File path:
seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitter.java
##########
@@ -50,31 +54,32 @@
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));
+ statements.addAll(normalizeLine(buffer));
buffer.clear();
} else {
buffer.add(line);
}
}
if (!buffer.isEmpty()) {
- statements.add(normalizeLine(buffer));
+ statements.addAll(normalizeLine(buffer));
}
return statements;
}
/**
* Remove comment lines.
*/
- private static String normalizeLine(List<String> buffer) {
- return buffer.stream()
- .map(statementLine ->
statementLine.replaceAll(BEGINNING_COMMENT_MASK, ""))
- .collect(Collectors.joining("\n"));
+ private static List<String> normalizeLine(List<String> buffer) {
+ String sqls = buffer.stream()
+ .map(statementLine ->
statementLine.replaceAll(BEGINNING_COMMENT_MASK, EMPTY_STR))
+ .collect(Collectors.joining(LINE_SEPARATOR));
+ return Arrays.asList(sqls.split(SEMICOLON));
Review comment:
We can not split sql statements by `;` directly.
##########
File path:
seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/splitter/SqlStatementSplitter.java
##########
@@ -50,31 +54,32 @@
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));
+ statements.addAll(normalizeLine(buffer));
buffer.clear();
} else {
buffer.add(line);
}
}
if (!buffer.isEmpty()) {
- statements.add(normalizeLine(buffer));
+ statements.addAll(normalizeLine(buffer));
}
return statements;
}
/**
* Remove comment lines.
*/
- private static String normalizeLine(List<String> buffer) {
- return buffer.stream()
- .map(statementLine ->
statementLine.replaceAll(BEGINNING_COMMENT_MASK, ""))
- .collect(Collectors.joining("\n"));
+ private static List<String> normalizeLine(List<String> buffer) {
+ String sqls = buffer.stream()
+ .map(statementLine ->
statementLine.replaceAll(BEGINNING_COMMENT_MASK, EMPTY_STR))
+ .collect(Collectors.joining(LINE_SEPARATOR));
+ return Arrays.asList(sqls.split(SEMICOLON));
Review comment:
We should not split sql statements by `;` directly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]