Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

格式化打印注释问题 #5689

Closed
dawsongzhao0523 opened this issue Jan 14, 2024 · 2 comments
Closed

格式化打印注释问题 #5689

dawsongzhao0523 opened this issue Jan 14, 2024 · 2 comments

Comments

@dawsongzhao0523
Copy link

1、使用以下代码,lexer可以正常解析注释。

    public void test_3() throws Exception {
        String sql ="----- 注释\n" +
                "select     bsd.line_id, --注释1\n  " +
                "bsd.line_id1 --注释2\n" +
                "from bsd; --行id\n" +
                "-- '有错误';";
//        Lexer lexer = new Lexer(sql);
        Lexer lexer = new HiveLexer(sql);
        lexer.skipComment = false;
        lexer.setKeepComments(true);
        for (;;) {
            lexer.nextToken();
            Token tok = lexer.token();


            if (tok == Token.IDENTIFIER) {
//                System.out.println("ID" + tok.name() + "\t\t" + lexer.stringVal());
            } else if (tok == Token.MULTI_LINE_COMMENT) {
                System.out.println("MLC" + tok.name() + "\t\t" + lexer.stringVal());
            } else if (tok == Token.COMMENT) {
                System.out.println("CO" + tok.name() + "\t\t" + lexer.stringVal());
            } else if (tok == Token.LINE_COMMENT) {
                System.out.println("LC" + tok.name() + "\t\t" + lexer.stringVal());
            }

            if (tok == Token.EOF) {
                break;
            }
        }

    }

2、修改HiveStatementParser源码,默认lexer.skipComment = false,使用以下代码,parser输出注释

public class HiveTest extends TestCase {
    public void testComment() {
        String sql ="----- 注释\n" +
                "select     bsd.line_id, --注释1\n  " +
                "bsd.line_id1 --注释2\n" +
                "from bsd; --行id\n" +
                "-- '有错误';";

        SQLStatementParser parser = new HiveStatementParser(sql, SQLParserFeature.IgnoreNameQuotes);
        SQLStatement stmt = parser.parseStatement(); //
        try {
            stmt.toString();
        } catch (ClassCastException e) {
            Assert.fail(e.getMessage());
        }

    }
}

输出异常:
com.alibaba.druid.sql.parser.ParserException: ERROR. pos 39, line 2, column 26, token LINE_COMMENT

at com.alibaba.druid.sql.parser.SQLExprParser.primary(SQLExprParser.java:1341)
at com.alibaba.druid.sql.dialect.hive.parser.HiveExprParser.primary(HiveExprParser.java:155)
at com.alibaba.druid.sql.parser.SQLExprParser.expr(SQLExprParser.java:106)
at com.alibaba.druid.sql.parser.SQLExprParser.parseSelectItem(SQLExprParser.java:6327)
at com.alibaba.druid.sql.parser.SQLSelectParser.parseSelectList(SQLSelectParser.java:1068)
at com.alibaba.druid.sql.parser.SQLSelectParser.query(SQLSelectParser.java:462)
at com.alibaba.druid.sql.parser.SQLSelectParser.select(SQLSelectParser.java:61)
at com.alibaba.druid.sql.parser.SQLStatementParser.parseSelect(SQLStatementParser.java:4430)
at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:200)
at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatement(SQLStatementParser.java:4833)
at com.alibaba.druid.sql.parser.HiveTest.testComment(HiveTest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at junit.framework.TestCase.runTest(TestCase.java:177)
at junit.framework.TestCase.runBare(TestCase.java:142)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:130)
at junit.framework.TestSuite.runTest(TestSuite.java:241)
at junit.framework.TestSuite.run(TestSuite.java:236)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
@lizongbo
Copy link
Collaborator

你连个版本信息都没提供
我基于最新代码验证运行正常。
请使用最新版验证。

image

@lizongbo
Copy link
Collaborator

测试代码文本:

   @Test
    public void testComment() {
        String sql ="----- 注释\n" +
            "select     bsd.line_id, --注释1\n  " +
            "bsd.line_id1 --注释2\n" +
            "from bsd; --行id\n" +
            "-- '有错误';";

        SQLStatementParser parser = new HiveStatementParser(sql, SQLParserFeature.IgnoreNameQuotes);
        SQLStatement stmt = parser.parseStatement(); //
        try {
           System.out.println(stmt.toString());
        } catch (ClassCastException e) {
            e.printStackTrace();
        }

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants