[CALCITE-2473] SqlAdvisor: support -- comments
Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/1c913e1b Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/1c913e1b Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/1c913e1b Branch: refs/heads/master Commit: 1c913e1b0499e85b5bfcb6495e478d68ad34d280 Parents: 1ed6b75 Author: Vladimir Sitnikov <sitnikov.vladi...@gmail.com> Authored: Sat Aug 18 23:52:49 2018 +0300 Committer: Vladimir Sitnikov <sitnikov.vladi...@gmail.com> Committed: Wed Sep 5 15:24:24 2018 +0300 ---------------------------------------------------------------------- .../apache/calcite/sql/advise/SqlSimpleParser.java | 14 +++++++++++++- .../org/apache/calcite/sql/test/SqlAdvisorTest.java | 12 ++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/1c913e1b/core/src/main/java/org/apache/calcite/sql/advise/SqlSimpleParser.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/calcite/sql/advise/SqlSimpleParser.java b/core/src/main/java/org/apache/calcite/sql/advise/SqlSimpleParser.java index 9765cd9..28b66e3 100644 --- a/core/src/main/java/org/apache/calcite/sql/advise/SqlSimpleParser.java +++ b/core/src/main/java/org/apache/calcite/sql/advise/SqlSimpleParser.java @@ -156,6 +156,10 @@ public class SqlSimpleParser { if (token == null) { break; } + if (token.type == TokenType.COMMENT) { + // ignore comments + continue; + } list.add(token); } @@ -353,7 +357,7 @@ public class SqlSimpleParser { case '/': // possible start of '/*' or '//' comment - if (pos < sql.length()) { + if (pos + 1 < sql.length()) { char c1 = sql.charAt(pos + 1); if (c1 == '*') { int end = sql.indexOf("*/", pos + 2); @@ -372,6 +376,14 @@ public class SqlSimpleParser { // fall through } + case '-': + // possible start of '--' comment + if (c == '-' && pos + 1 < sql.length() && sql.charAt(pos + 1) == '-') { + pos = indexOfLineEnd(sql, pos + 2); + return new Token(TokenType.COMMENT); + } + // fall through + default: if (c == openQuote) { return parseQuotedIdentifier(); http://git-wip-us.apache.org/repos/asf/calcite/blob/1c913e1b/core/src/test/java/org/apache/calcite/sql/test/SqlAdvisorTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/calcite/sql/test/SqlAdvisorTest.java b/core/src/test/java/org/apache/calcite/sql/test/SqlAdvisorTest.java index 1e3b959..c67926a 100644 --- a/core/src/test/java/org/apache/calcite/sql/test/SqlAdvisorTest.java +++ b/core/src/test/java/org/apache/calcite/sql/test/SqlAdvisorTest.java @@ -1180,6 +1180,18 @@ public class SqlAdvisorTest extends SqlValidatorTestCase { "select // here is from clause\n 'cat' as foobar, 1 as x from t group by t.^ order by 123"; expected = "SELECT * FROM t GROUP BY t. _suggest_"; assertSimplify(sql, expected); + + // skip comments + sql = + "select -- here is from clause\n 'cat' as foobar, 1 as x from t group by t.^ order by 123"; + expected = "SELECT * FROM t GROUP BY t. _suggest_"; + assertSimplify(sql, expected); + + // skip comments + sql = + "-- test test \nselect -- here is from \n 'cat' as foobar, 1 as x from t group by t.^ order by 123"; + expected = "SELECT * FROM t GROUP BY t. _suggest_"; + assertSimplify(sql, expected); } @WithLex(Lex.SQL_SERVER) @Test public void testSimpleParserQuotedIdSqlServer() {