Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.apache.zeppelin.interpreter.util;


import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -79,11 +81,9 @@ public List<String> splitSql(String text) {
}

// end of multiple line comment
if (multiLineComment && character == '/' && text.charAt(index - 1) == '*') {
if (multiLineComment && (index - 1) >= 0 && text.charAt(index - 1) == '/'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we replace (index - 1) >= 0 with (index - 2) >= 0 and just skip it later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't get it. Here I just check whether it is the end of multiple comment via checking whether (index-1) is / and (index-2) is *

&& (index - 2) >= 0 && text.charAt(index - 2) == '*') {
multiLineComment = false;
if (query.toString().trim().isEmpty()) {
continue;
}
}

if (character == '\'') {
Expand All @@ -106,39 +106,30 @@ public List<String> splitSql(String text) {
&& text.length() > (index + 1)) {
if (isSingleLineComment(text.charAt(index), text.charAt(index + 1))) {
singleLineComment = true;
} else if (text.charAt(index) == '/' && text.charAt(index + 1) == '*') {
} else if (text.charAt(index) == '/' && text.length() > (index + 2)
&& text.charAt(index + 1) == '*' && text.charAt(index + 2) != '+') {
multiLineComment = true;
}
}

if (character == ';' && !singleQuoteString && !doubleQuoteString && !multiLineComment
&& !singleLineComment) {
// meet semicolon
queries.add(query.toString().trim());
query = new StringBuilder();
if (character == ';' && !singleQuoteString && !doubleQuoteString && !multiLineComment && !singleLineComment) {
// meet the end of semicolon
if (!query.toString().trim().isEmpty()) {
queries.add(query.toString().trim());
query = new StringBuilder();
}
} else if (index == (text.length() - 1)) {
// meet the last character
if (!singleLineComment && !multiLineComment) {
query.append(character);
}
if (!query.toString().trim().isEmpty()) {
queries.add(query.toString().trim());
query = new StringBuilder();
}
} else if (!singleLineComment && !multiLineComment) {
// normal case, not in single line comment and not in multiple line comment
query.append(character);
} else if (singleLineComment && !query.toString().trim().isEmpty()) {
// in single line comment, only add it to query when the single line comment is
// in the middle of sql statement
// e.g.
// select a -- comment
// from table_1
query.append(character);
} else if (multiLineComment && !query.toString().trim().isEmpty()) {
// in multiple line comment, only add it to query when the multiple line comment
// is in the middle of sql statement.
// e.g.
// select a /* comment */
// from table_1
query.append(character);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,16 @@ public void testSingleLineComment() {

sqls = sqlSplitter.splitSql("select a -- comment\n from table_1");
assertEquals(1, sqls.size());
assertEquals("select a -- comment\n from table_1", sqls.get(0));
assertEquals("select a \n from table_1", sqls.get(0));

sqls = sqlSplitter.splitSql("--comment 1\nselect a from table_1\n--comment 2");
assertEquals(1, sqls.size());
assertEquals("select a from table_1", sqls.get(0));

sqls = sqlSplitter.splitSql("--comment 1\nselect a from table_1;\n--comment 2\nselect b from table_1");
assertEquals(2, sqls.size());
assertEquals("select a from table_1", sqls.get(0));
assertEquals("select b from table_1", sqls.get(1));
}

@Test
Expand Down Expand Up @@ -145,7 +154,21 @@ public void testMultipleLineComment() {

sqls = sqlSplitter.splitSql("select a /*comment*/ from table_1");
assertEquals(1, sqls.size());
assertEquals("select a /*comment*/ from table_1", sqls.get(0));
assertEquals("select a from table_1", sqls.get(0));

sqls = sqlSplitter.splitSql("/*comment 1*/\nselect a from table_1\n/*comment 2*/");
assertEquals(1, sqls.size());
assertEquals("select a from table_1", sqls.get(0));

sqls = sqlSplitter.splitSql("/*comment 1*/\nselect a from table_1;\n/*comment 2*/select b from table_1");
assertEquals(2, sqls.size());
assertEquals("select a from table_1", sqls.get(0));
assertEquals("select b from table_1", sqls.get(1));

sqls = sqlSplitter.splitSql("/*comment 1*/\nselect a /*+ hint*/ from table_1;\n/*comment 2*/select b from table_1");
assertEquals(2, sqls.size());
assertEquals("select a /*+ hint*/ from table_1", sqls.get(0));
assertEquals("select b from table_1", sqls.get(1));
}

@Test
Expand Down Expand Up @@ -231,7 +254,7 @@ public void testCustomSplitter_1() {

sqls = sqlSplitter.splitSql("select a // comment\n from table_1");
assertEquals(1, sqls.size());
assertEquals("select a // comment\n from table_1", sqls.get(0));
assertEquals("select a \n from table_1", sqls.get(0));
}

@Test
Expand Down Expand Up @@ -281,6 +304,6 @@ public void testCustomSplitter_2() {

sqls = sqlSplitter.splitSql("select a # comment\n from table_1");
assertEquals(1, sqls.size());
assertEquals("select a # comment\n from table_1", sqls.get(0));
assertEquals("select a \n from table_1", sqls.get(0));
}
}