mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved odps sql parser, fix case when print comment error, for issue #5077
This commit is contained in:
@@ -283,12 +283,30 @@ public class SQLParser {
|
||||
case REPEAT:
|
||||
case USE:
|
||||
case MOD:
|
||||
case QUALIFY:
|
||||
case OUT: {
|
||||
String strVal = lexer.stringVal();
|
||||
lexer.nextToken();
|
||||
return strVal;
|
||||
}
|
||||
case QUALIFY: {
|
||||
String strVal = lexer.stringVal();
|
||||
Lexer.SavePoint mark = lexer.mark();
|
||||
lexer.nextToken();
|
||||
if (lexer.token != Token.WHERE
|
||||
&& lexer.token != Token.GROUP
|
||||
&& lexer.token != Token.HAVING
|
||||
&& lexer.token != Token.WINDOW
|
||||
&& lexer.token != Token.ORDER
|
||||
&& lexer.token != Token.LIMIT
|
||||
&& lexer.token != Token.EOF
|
||||
&& lexer.token != Token.COMMA
|
||||
) {
|
||||
lexer.reset(mark);
|
||||
return null;
|
||||
} else {
|
||||
return strVal;
|
||||
}
|
||||
}
|
||||
case DISTRIBUTE: {
|
||||
String strVal = lexer.stringVal();
|
||||
Lexer.SavePoint mark = lexer.mark();
|
||||
|
||||
@@ -1289,7 +1289,15 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
|
||||
printExpr(conditionExpr, parameterized);
|
||||
this.indentCount--;
|
||||
|
||||
if (lines != this.lines) {
|
||||
boolean println = lines != this.lines;
|
||||
if (isPrettyFormat() && !println) {
|
||||
List<String> afterComments = conditionExpr.getAfterCommentsDirect();
|
||||
if (afterComments != null && !afterComments.isEmpty()) {
|
||||
println = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (println) {
|
||||
println();
|
||||
} else {
|
||||
print(' ');
|
||||
|
||||
@@ -17,7 +17,13 @@ package com.alibaba.druid.bvt.sql.odps;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.DbType;
|
||||
import com.alibaba.druid.TestUtil;
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLParserFeature;
|
||||
import com.alibaba.druid.sql.parser.SQLParserUtils;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.visitor.VisitorFeature;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
@@ -27,6 +33,8 @@ import com.alibaba.druid.sql.dialect.odps.parser.OdpsStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.odps.visitor.OdpsSchemaStatVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class OdpsResourceTest extends TestCase {
|
||||
public void test_0() throws Exception {
|
||||
exec_test("bvt/parser/odps-0.txt");
|
||||
@@ -39,6 +47,10 @@ public class OdpsResourceTest extends TestCase {
|
||||
public void test_10() throws Exception {
|
||||
exec_test("bvt/parser/odps-10.txt");
|
||||
}
|
||||
//
|
||||
// public void test_13() throws Exception {
|
||||
// exec_test("bvt/parser/odps-13.txt");
|
||||
// }
|
||||
|
||||
public void exec_test(String resource) throws Exception {
|
||||
String input = TestUtil.getResource(resource);
|
||||
@@ -51,6 +63,7 @@ public class OdpsResourceTest extends TestCase {
|
||||
}
|
||||
|
||||
OdpsStatementParser parser = new OdpsStatementParser(sql);
|
||||
parser.config(SQLParserFeature.KeepComments, true);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
@@ -60,7 +73,7 @@ public class OdpsResourceTest extends TestCase {
|
||||
stmt.accept(visitor);
|
||||
|
||||
if (expect != null) {
|
||||
String result = stmt.toString();
|
||||
String result = stmt.toString(VisitorFeature.OutputPrettyFormat);
|
||||
assertEquals(expect, result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
--asdasdas \u001Aasdad
|
||||
select api_name
|
||||
from t_api
|
||||
where api_name is not null;
|
||||
---------------------------
|
||||
-- asdasdas \u001Aasdad
|
||||
SELECT api_name
|
||||
FROM t_api
|
||||
WHERE api_name IS NOT NULL;
|
||||
@@ -0,0 +1,9 @@
|
||||
--asdasdas \u001Aasdad
|
||||
select api_name
|
||||
from t_api
|
||||
where api_name is not null;
|
||||
---------------------------
|
||||
-- asdasdas \u001Aasdad
|
||||
SELECT api_name
|
||||
FROM t_api
|
||||
WHERE api_name IS NOT NULL;
|
||||
@@ -0,0 +1,23 @@
|
||||
select case when t1.type='JP' and t1.value>0
|
||||
then (case when t1.category_id =29 --注释1
|
||||
then ((0.95*t1.value)*1.04)+0.3
|
||||
when t1.category_id =48 --注释2
|
||||
then ((0.85*t1.value)*1.04)+0.3
|
||||
when t1.category_id in (2,92) --注释3
|
||||
then ((0.75*t1.value)*1.04)+0.3
|
||||
else ((0.65*t1.value)*1.04)+0.3
|
||||
end ) end from t_demo t1
|
||||
---------------------------
|
||||
SELECT CASE
|
||||
WHEN t1.type = 'JP'
|
||||
AND t1.value > 0
|
||||
THEN
|
||||
CASE
|
||||
WHEN t1.category_id = 29 THEN 0.95 * t1.value * 1.04 + 0.3
|
||||
WHEN t1.category_id = 48 THEN 0.85 * t1.value * 1.04 + 0.3
|
||||
WHEN t1.category_id IN (2, 92) -- 注释3
|
||||
THEN 0.75 * t1.value * 1.04 + 0.3
|
||||
ELSE 0.65 * t1.value * 1.04 + 0.3
|
||||
END
|
||||
END
|
||||
FROM t_demo t1
|
||||
Reference in New Issue
Block a user