| 33 | namespace zvec::sqlengine { |
| 34 | |
| 35 | SQLInfo::Ptr ZVecSQLParser::parse(const std::string &query, |
| 36 | bool need_formatted_tree) { |
| 37 | try { |
| 38 | ANTLRInputStream input(query); |
| 39 | CaseChangingCharStream in(&input, true); |
| 40 | |
| 41 | SQLLexer lexer(&in); |
| 42 | |
| 43 | CommonTokenStream tokens(&lexer); |
| 44 | |
| 45 | SQLParser parser(&tokens); |
| 46 | |
| 47 | // remove and add new error listeners |
| 48 | ErrorVerboseListener lexer_error_listener; |
| 49 | lexer.removeErrorListeners(); // remove all error listeners |
| 50 | lexer.addErrorListener((ANTLRErrorListener *)&lexer_error_listener); // add |
| 51 | ErrorVerboseListener parser_error_listener; |
| 52 | parser.removeErrorListeners(); // remove all error listeners |
| 53 | parser.addErrorListener( |
| 54 | (ANTLRErrorListener *)&parser_error_listener); // add |
| 55 | |
| 56 | // int64_t curtime = Util::cur_micro_second_time(); |
| 57 | ParseTree *tree = parser.compilation_unit(); |
| 58 | |
| 59 | if (lexer.getNumberOfSyntaxErrors() > 0 || |
| 60 | parser.getNumberOfSyntaxErrors() > 0) { |
| 61 | LOG_INFO("SLL failed. using LL"); |
| 62 | tokens.reset(); |
| 63 | parser.reset(); |
| 64 | parser.getInterpreter<ParserATNSimulator>()->setPredictionMode( |
| 65 | PredictionMode::LL); |
| 66 | tree = parser.compilation_unit(); |
| 67 | } |
| 68 | |
| 69 | // int64_t duration = Util::cur_micro_second_time() - curtime; |
| 70 | // printf("parsing time %ld\n", duration); |
| 71 | // LOG_DEBUG("antlr parsing time: [%ld]", duration); |
| 72 | |
| 73 | if (lexer.getNumberOfSyntaxErrors() > 0) { |
| 74 | err_msg_ = "lexer error [" + lexer_error_listener.err_msg() + "]"; |
| 75 | return nullptr; |
| 76 | } |
| 77 | if (parser.getNumberOfSyntaxErrors() > 0) { |
| 78 | err_msg_ = "syntax error [" + parser_error_listener.err_msg() + "]"; |
| 79 | return nullptr; |
| 80 | } |
| 81 | |
| 82 | if (need_formatted_tree) { |
| 83 | formatted_tree_ = to_formatted_string_tree(tree, &parser); |
| 84 | } |
| 85 | |
| 86 | SQLInfo::Ptr sqlInfo = sql_info(tree); |
| 87 | return sqlInfo; |
| 88 | } catch (std::exception &e) { |
| 89 | err_msg_ = "parse error [" + std::string(e.what()) + "]"; |
| 90 | return nullptr; |
| 91 | } |
| 92 | } |
nothing calls this directly
no test coverage detected