You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ch...@apache.org on 2021/06/08 07:53:07 UTC
[iotdb] 01/01: Revert "fix import csv split by comma bug (#3253)"
This is an automated email from the ASF dual-hosted git repository.
chaow pushed a commit to branch revert_not_ready_fix_csv_pr
in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit f56ad572887f9fb6c52079166ac2ad69b985811a
Author: chaow <94...@qq.com>
AuthorDate: Tue Jun 8 15:31:49 2021 +0800
Revert "fix import csv split by comma bug (#3253)"
This reverts commit 5ea3c2e52ade6658621d798a131367bcdf438406.
---
.../main/java/org/apache/iotdb/tool/ImportCsv.java | 41 +++++++++++++++-------
.../org/apache/iotdb/tool/CsvLineSplitTest.java | 11 ++----
2 files changed, 31 insertions(+), 21 deletions(-)
diff --git a/cli/src/main/java/org/apache/iotdb/tool/ImportCsv.java b/cli/src/main/java/org/apache/iotdb/tool/ImportCsv.java
index a3179d3..6959360 100644
--- a/cli/src/main/java/org/apache/iotdb/tool/ImportCsv.java
+++ b/cli/src/main/java/org/apache/iotdb/tool/ImportCsv.java
@@ -424,21 +424,38 @@ public class ImportCsv extends AbstractCsvTool {
public static String[] splitCsvLine(String path) {
List<String> nodes = new ArrayList<>();
- int start = 0;
- boolean singleQuotes = false;
- boolean doubleQuotes = false;
- // split by comma if the comma is followed by an even number of quotes
- for (int i = 0; i < path.length(); i++) {
- if (path.charAt(i) == '\"') {
- doubleQuotes = !doubleQuotes; // toggle state
+ startIndex = 0;
+ for (i = 0; i < path.length(); i++) {
+ if (path.charAt(i) == ',') {
+ nodes.add(path.substring(startIndex, i));
+ startIndex = i + 1;
+ } else if (path.charAt(i) == '"') {
+ nextNode(path, nodes, '"');
} else if (path.charAt(i) == '\'') {
- singleQuotes = !singleQuotes;
- } else if (path.charAt(i) == ',' && (!singleQuotes && !doubleQuotes)) {
- nodes.add(path.substring(start, i));
- start = i + 1;
+ nextNode(path, nodes, '\'');
}
}
- nodes.add(path.substring(start));
+ if (path.charAt(path.length() - 1) == ',') {
+ nodes.add("");
+ }
+ if (startIndex <= path.length() - 1) {
+ nodes.add(path.substring(startIndex));
+ }
return nodes.toArray(new String[0]);
}
+
+ public static void nextNode(String path, List<String> nodes, char enclose) {
+ int endIndex = path.indexOf(enclose, i + 1);
+ // if a double quotes with escape character
+ while (endIndex != -1 && path.charAt(endIndex - 1) == '\\') {
+ endIndex = path.indexOf(enclose, endIndex + 1);
+ }
+ if (endIndex != -1 && (endIndex == path.length() - 1 || path.charAt(endIndex + 1) == ',')) {
+ nodes.add(path.substring(startIndex + 1, endIndex));
+ i = endIndex + 1;
+ startIndex = endIndex + 2;
+ } else {
+ throw new IllegalArgumentException("Illegal csv line" + path);
+ }
+ }
}
diff --git a/cli/src/test/java/org/apache/iotdb/tool/CsvLineSplitTest.java b/cli/src/test/java/org/apache/iotdb/tool/CsvLineSplitTest.java
index 2b54434..fd1c9ba 100644
--- a/cli/src/test/java/org/apache/iotdb/tool/CsvLineSplitTest.java
+++ b/cli/src/test/java/org/apache/iotdb/tool/CsvLineSplitTest.java
@@ -26,15 +26,8 @@ public class CsvLineSplitTest {
@Test
public void testSplit() {
Assert.assertArrayEquals(
- new String[] {"", "a", "b", "c", "\\\""}, ImportCsv.splitCsvLine(",a,b,c,\\\""));
+ new String[] {"", "a", "b", "c", "\\\""}, ImportCsv.splitCsvLine(",a,b,c,\"\\\"\""));
Assert.assertArrayEquals(
- new String[] {"", "a", "b", "\\'"}, ImportCsv.splitCsvLine(",a,b,\\'"));
- Assert.assertArrayEquals(
- new String[] {"", "a\",\"a", "\"a,,\"", "'"}, ImportCsv.splitCsvLine(",a\",\"a,\"a,,\",'"));
- Assert.assertArrayEquals(
- new String[] {"True", "a=\",\"a''"}, ImportCsv.splitCsvLine("True,a=\",\"a''"));
- Assert.assertArrayEquals(
- new String[] {"True", "\"a=,,,a=z//z'a\""},
- ImportCsv.splitCsvLine("True,\"a=,,,a=z//z'a\""));
+ new String[] {"", "a", "b", "\\'"}, ImportCsv.splitCsvLine(",a,b,\"\\'\""));
}
}