DSL and SQL syntax hybrid analysis method and device, storage medium and program product
By splitting and parsing DSL and SQL mixed statements, generating and combining abstract syntax trees, the syntax parsing and conversion complexity problems when mixing DSL and SQL are solved, and efficient and flexible query capabilities are achieved.
Patent Information
- Application Number
- CN202411786515.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-05
- Publication Date
- 2025-04-11
AI Technical Summary
DSL has different syntax structures and rules from SQL, resulting in complex syntax parsing and conversion during mixed use, with low flexibility and scalability.
By processing mixed statements, splitting them into DSL substatements and SQL substatements, parsing them respectively, generating SQL abstract syntax tree and DSL syntax tree nodes, and adding DSL syntax tree nodes to the SQL abstract syntax tree, and finally converting them into SQL statements for querying.
Automatic syntax parsing and conversion of mixed DSL and SQL statements is implemented, which reduces the burden of manually writing SQL statements, reduces the error rate, and improves query efficiency and syntax use flexibility.
Smart Images

Figure CN119807231A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data analysis, and in particular to a DSL and SQL grammar hybrid analysis method, device, storage medium and program product. Background Art
[0002] In database query technology, DSL (Domain Specific Language) and SQL (Structured Query Language) are currently common languages used to describe business logic and operate relational databases. DSL is a programming language specifically used to solve problems in a specific domain, while SQL is a standard query language used to operate relational databases.
[0003] Among them, SQL is the most commonly used query language. However, for non-professionals, SQL syntax may be difficult to understand and use. DSL can perform database queries more easily than SQL.
[0004] In actual business, the application scenarios of DSL and SQL often overlap and need to be mixed. However, due to the different grammatical structures and rules of DSL and SQL, the grammatical parsing and conversion when mixed is complex, and the flexibility and scalability are low. Summary of the invention
[0005] In view of this, the present invention provides a DSL and SQL grammar mixed analysis method, device, storage medium and program product to solve the problem of DSL and SQL mixed statement query, improve query efficiency, and increase grammar usage flexibility.
[0006] In a first aspect, the present invention provides a DSL and SQL grammar hybrid analysis method, which includes: processing a hybrid statement to obtain a DSL sub-statement and an SQL sub-statement, wherein the hybrid statement consists of a DSL statement and an SQL statement; parsing the SQL sub-statement to generate an SQL abstract syntax tree; parsing the DSL sub-statement to convert it into multiple syntax tree nodes; adding multiple syntax tree nodes to the SQL abstract syntax tree to obtain a hybrid abstract syntax tree; converting the hybrid abstract syntax tree into an SQL statement, and querying and analyzing based on the SQL statement.
[0007] In this implementation, for mixed statements of DSL and SQL, the mixed statements are split, and the DSL sub-statements and SQL sub-statements are parsed separately. For the SQL sub-statements, they are converted into abstract syntax trees, and for the DSL sub-statements, they are converted into syntax tree nodes that can be recognized by the SQL syntax tree. Using the syntax tree as transit information, the syntax tree nodes of DSL are added to the SQL abstract syntax tree, and finally converted into SQL statements for query. This implementation solves the problem of DSL and SQL mixed statement query, can automatically perform syntax parsing and conversion, reduce the burden of manually writing SQL statements, and reduce the error rate, which can improve query efficiency and improve the flexibility of syntax use.
[0008] In an optional implementation, processing the mixed statement to obtain the DSL sub-statement and the SQL sub-statement includes: saving the mixed statement as a mixed string; performing DSL and SQL identification on the mixed string, and cutting the mixed string into the DSL sub-statement and the SQL sub-statement.
[0009] In an optional implementation, parsing the SQL sub-statement to generate the SQL abstract syntax tree includes: performing lexical analysis on the SQL sub-statement; and performing syntax analysis on the lexically analyzed SQL sub-statement using a syntax parser to generate the SQL abstract syntax tree.
[0010] In this implementation, SQL substatements are converted into abstract syntax trees, providing connection points between SQL statements and DSL statements. The conversion between SQL and syntax trees is flexible and scalable, which improves the efficiency of subsequent syntax queries.
[0011] In an optional implementation, word segmentation processing is performed on the DSL sub-statement to convert it into multiple syntax tree nodes, including: using the Lucene package to process the DSL sub-statement to generate multiple Query objects; according to the type of the Query object, translating the Query object into SQL syntax; and converting the corresponding DSL sub-statement into syntax tree nodes based on the SQL syntax.
[0012] In this implementation, Query objects in DSL substatements are identified, syntax translation between DSL and SQL is performed based on each Query object, and multiple syntax tree nodes are obtained through conversion, thereby reducing the difficulty of syntax conversion and improving conversion efficiency.
[0013] In an optional implementation, the Query object includes TermQuery, BooleanQuery, RangeQuery, PrefixQuery, PhraseQuery, FuzzyQuery, WildcardQuery, MultiPhraseQuery, RegexpQuery, ConstantScoreQuery, DisjunctionMaxQuery, and MatchAllDocsQuery.
[0014] In an optional implementation, adding multiple syntax tree nodes to a SQL abstract syntax tree to obtain a hybrid abstract syntax tree includes: for a syntax tree node, traversing the SQL abstract syntax tree to find a corresponding target node; adding the syntax tree node to the target node; traversing all syntax tree nodes, adding all syntax tree nodes to the SQL abstract syntax tree to obtain a hybrid abstract syntax tree.
[0015] In this implementation, the syntax tree nodes are directly added to the SQL abstract syntax tree to achieve an easy combination of DSL syntax and SQL syntax, which reduces the difficulty of manually writing DSL syntax into SQL syntax, reduces the writing error rate, and improves the efficiency of syntax conversion. It can be applied to a variety of business scenarios and database applications, and has practicality and economic benefits.
[0016] In a second aspect, the present invention provides a DSL and SQL grammar hybrid analysis device, which includes: a segmentation module, used to segment a hybrid statement to obtain a DSL sub-statement and an SQL sub-statement, wherein the hybrid statement consists of a DSL statement and an SQL statement; a first parsing module, used to parse the SQL sub-statement to generate an SQL abstract syntax tree; a second parsing module, used to parse the DSL sub-statement and convert it into multiple syntax tree nodes; a combining module, used to add multiple syntax tree nodes to the SQL abstract syntax tree to obtain a hybrid abstract syntax tree; and a query module, used to convert the hybrid abstract syntax tree into an SQL statement, and perform query and analysis based on the SQL statement.
[0017] In a third aspect, the present invention provides a computer device, comprising: a memory and a processor, the memory and the processor being communicatively connected to each other, the memory storing computer instructions, and the processor executing the DSL and SQL grammar hybrid analysis method of the above-mentioned first aspect or any corresponding embodiment thereof by executing the computer instructions.
[0018] In a fourth aspect, the present invention provides a computer-readable storage medium having computer instructions stored thereon, the computer instructions being used to enable a computer to execute the DSL and SQL grammar hybrid analysis method of the first aspect or any corresponding embodiment thereof.
[0019] In a fifth aspect, the present invention provides a computer program product, including computer instructions, where the computer instructions are used to enable a computer to execute the DSL and SQL grammar hybrid analysis method of the first aspect or any corresponding embodiment thereof. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0021] Figure 1 It is a flowchart of a DSL and SQL grammar hybrid analysis method according to an embodiment of the present invention;
[0022] Figure 2 is a flow chart of another DSL and SQL grammar hybrid analysis method according to an embodiment of the present invention;
[0023] Figure 3 is a schematic diagram of an SQL abstract syntax tree according to an embodiment of the present invention;
[0024] Figure 4 is a structural block diagram of a DSL and SQL grammar hybrid analysis device according to an embodiment of the present invention;
[0025] Figure 5 It is a schematic diagram of the hardware structure of a computer device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0026] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0027] In the log service scenario, it is usually necessary to query and analyze a large amount of log data. Due to the complex format and content of log data, it is difficult to meet the query requirements using traditional SQL statements. Therefore, hybrid statements composed of DSL and SQL have become a more commonly used query method.
[0028] DSL is usually used to describe the format and structure of log data. For example, DSL can be used to specify field names, data types, separators, etc. in log data. DSL also supports some special query operations, for example, fuzzy matching of strings and range query of time.
[0029] Among them, SQL language is usually used to describe the content and relationship of log data. For example, SQL language can be used to query all access records of a certain IP address within a specific time period, or query all login records of a certain user. SQL language has strong query capabilities and flexibility, and can meet various complex query requirements.
[0030] Since DSL and SQL have different advantages and applicable scenarios, in log service scenarios, you usually need to use a mixed statement consisting of DSL and SQL for querying.
[0031] For example, when querying all user records that visited a website within a certain period of time, the following mixed statement can be used:
[0032] url:http: / / www.example.com AND status:success|SELECT count(*)FROMlogs WHERE time>='2021-01-01'AND time<='2021-01-31'AND url='http: / / www.example.com'AND user IN(SELECT user FROM logs WHERE time>='2021-01-01'AND time<='2021-01-31'AND duration<=1000)
[0033] Among them, DSL language is used to describe the format and structure of log data. For example, url and user are the field names in log data. SQL language is used to describe the content and relationship of log data. For example, query all user records that visit a website within a certain period of time, and further query the login records of these users.
[0034] Since the syntax structure and query rules of DSL and SQL are different, it is relatively complicated to perform syntax parsing and syntax conversion for the above mixed statements. Therefore, the present application proposes a DSL and SQL syntax mixed analysis method to perform mixed query syntax parsing on mixed statements. The method splits the mixed query statement according to the boundary between DSL and SQL, and then parses the DSL part and the SQL part respectively. The DSL part is parsed using a DSL parser, and the SQL part is parsed using an SQL parser. Finally, the parsing results of DSL and SQL are integrated to perform query operations. It can solve the problem of DSL and SQL mixed statement query, improve query efficiency, and increase the flexibility of grammar use.
[0035] According to an embodiment of the present invention, an embodiment of a DSL and SQL grammar hybrid analysis method is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0036] In this embodiment, a DSL and SQL grammar hybrid analysis method is provided. Figure 1 is a flow chart of a DSL and SQL grammar hybrid analysis method according to an embodiment of the present invention. It should be noted that if there are substantially the same results, this embodiment does not use Figure 1 The process sequence shown is limited. Figure 1 As shown, the process includes the following steps:
[0037] Step S101, processing the mixed statement to obtain a DSL sub-statement and an SQL sub-statement.
[0038] Identify the DSL part and the SQL part in the mixed statement, split them according to the boundary between DSL and SQL, and obtain DSL sub-statements and SQL sub-statements.
[0039] Step S102: parse the SQL sub-statements to generate a SQL abstract syntax tree.
[0040] SQL parsing refers to the process of parsing and processing the SQL part of the query statement. SQL has a set of standard syntax and rules, and the SQL parser is used to parse the SQL sub-statements. The SQL parser converts the SQL sub-statements into query plans or execution plans so that the database engine can execute the query operation.
[0041] In one implementation, a parse tree is used to parse the SQL substatement.
[0042] More specifically, in one implementation, the SQL sub-statements are parsed to generate a SQL abstract syntax tree.
[0043] Among them, the Abstract Syntax Tree (AST) is an abstract representation of the syntax structure of the source code. The Abstract Syntax Tree represents the structure of the programming language in a tree form, and each node ASTNode of the tree represents a structure in the source code. The Abstract Syntax Tree can ignore some syntax information in the parsed code and strip away unimportant details.
[0044] Step S103: parse the DSL sub-statements and convert them into multiple syntax tree nodes.
[0045] DSL parsing refers to the process of parsing and processing the query statements of the DSL part. DSL has corresponding syntax and rules. This application writes a corresponding DSL parser to parse the DSL sub-statements. The DSL parser converts the DSL sub-statements into executable operations, such as function calls, object method calls, etc., for subsequent execution.
[0046] Specifically, the present application writes a DSL parser to parse DSL sub-statements and convert the DSL sub-statements into multiple syntax tree nodes that can be understood and recognized by SQL syntax.
[0047] Each syntax tree node is similar to a tree node in the abstract syntax tree.
[0048] Step S104, adding multiple syntax tree nodes to the SQL abstract syntax tree to obtain a hybrid abstract syntax tree.
[0049] Step S105: convert the hybrid abstract syntax tree into an SQL statement, and perform query and analysis based on the SQL statement.
[0050] The DSL and SQL grammar mixed analysis method provided in this embodiment is for mixed statements of DSL and SQL, splits the mixed statements, parses the DSL sub-statements and SQL sub-statements respectively, converts the SQL sub-statements into abstract syntax trees, and converts the DSL sub-statements into syntax tree nodes that can be identified by the SQL syntax tree. Using the syntax tree as transit information, the syntax tree nodes of DSL are added to the SQL abstract syntax tree, and finally converted into SQL statements for query. This implementation solves the problem of mixed statement query of DSL and SQL, can automatically perform syntax parsing and conversion, reduces the burden of manually writing SQL statements, reduces the error rate, can improve query efficiency, and improve the flexibility of syntax use.
[0051] In this embodiment, a DSL and SQL grammar hybrid analysis method is provided. Figure 2is a flowchart of a DSL and SQL grammar hybrid analysis method according to an embodiment of the present invention. It should be noted that if there are substantially the same results, this embodiment does not use Figure 2 The process sequence shown is limited. Figure 2 As shown, the process includes the following steps:
[0052] Step S201, processing the mixed statement to obtain a DSL sub-statement and an SQL sub-statement.
[0053] Specifically, the above step S201 includes:
[0054] Step S2011, saving the mixed sentence as a mixed character string.
[0055] Receives mixed statements consisting of DSL and SQL and saves them as mixed strings.
[0056] Step S2012, performing DSL and SQL identification on the mixed character string, and cutting the mixed character string into DSL sub-statements and SQL sub-statements.
[0057] Lexical analysis is performed on the mixed string to identify the DSL part and the SQL part, and the mixed statement is cut into DSL sub-statements and SQL sub-statements according to the boundary between the DSL part and the SQL part.
[0058] In this implementation, when the search engine receives a query statement input by a user, it identifies the query statement. When the query statement is a mixed statement of DSL and SQL, it does not directly search the query statement, but instead splits the query statement so that the DSL sub-statements and SQL sub-statements can be analyzed and processed separately. This method can improve the search efficiency and the accuracy of the search results.
[0059] Step S202: parse the SQL sub-statements to generate a SQL abstract syntax tree.
[0060] Specifically, the above step S202 includes:
[0061] Step S2021, performing lexical analysis on the SQL substatement.
[0062] Specifically, the input SQL substatements are decomposed into a series of meaningful symbols or tokens, which include keywords, such as SELECT clause, FROM clause, WHERE condition, etc.; identifiers, such as table name, column name, etc.; operators, such as >, =, etc.; literals, such as numbers, strings, etc.
[0063] Step S2022: Use a syntax parser to perform syntax analysis on the SQL sub-statements after lexical analysis to generate an SQL abstract syntax tree.
[0064] The syntax analyzer is used to perform syntax analysis on the SQL substatements after lexical analysis.
[0065] Specifically, according to the grammatical rules of SQL, the morpheme sequence is constructed into an abstract syntax tree. The parser reads the morpheme sequence and gradually builds the tree structure according to the predefined grammatical rules. The parser verifies whether the SQL statement follows the correct grammatical structure. If it is illegal, a syntax error is thrown.
[0066] Each node in the SQL abstract syntax tree represents a component of a SQL substatement. The root node represents the entire SQL substatement query, and the child nodes represent the query details.
[0067] For example, for the SQL substatement "select username,ismale from userInfo where age>20and level>5and 1=1", see Figure 3 , Figure 3 It is a schematic diagram of an SQL abstract syntax tree according to an embodiment of the present invention.
[0068] Specifically, lexical analysis is performed to obtain morphemes. SELECT contains the selected columns username and ismaile, FROM contains the queried table userInfo, and WHERE contains the query conditions age>20and level>5and1=1. Further analysis of operators and literals is performed to construct the following: Figure 3 The SQL abstract syntax tree shown.
[0069] In one implementation, the present application uses ANTLR4 to complete the lexical analysis and syntax analysis of the SQL abstract syntax tree. ANTLR4 constructs a G4 file, describes the target syntax through regular expressions and specific syntax structures, and when performing syntax analysis, it relies on a structure like a syntax dictionary to disassemble and encapsulate SQL, and then extracts the required content to obtain a SQL abstract syntax tree.
[0070] Parse the SQL table name and convert it into a Java class using the G4 file that describes the MySQL syntax using the plugin.
[0071] Among them, the java classes include: console, SqlBase.interp, SqlBase.tokens, SqlBaseBaseListener, SqlBaseBaseVisitor, SqlBaseLexer, SqlBaseLexer.interp, SqlBaseLexer.tokens, SqlBaseListener, SqlBaseParser, SqlBaseVisitor.
[0072] Among them, SqlBase is obtained by converting the G4 file name, SqlBaseLexer is used for lexical analysis, SqlBaseParser is used for syntax analysis, and generates an abstract syntax tree object. SqlBaseVisitor and SqlBaseListener are used to traverse the abstract syntax tree, Visitor is the visitor mode and Listener is the listener mode. By inheriting these two mode interfaces, you can customize the traversal logic and implement the corresponding traversal method. SqlBaseBaseVisitor reads the table name by rewriting the key method. TableIdentifierContext is the content of the table definition.
[0073] Furthermore, for the generated SQL abstract syntax tree, syntax review, query optimization, SQL rewriting, SQL feature generation, etc. can be performed.
[0074] Among them, syntax audit is to audit and judge the legality of SQL according to built-in rules. Query optimization is to generate index optimization suggestions based on where conditions, aggregation conditions, and multi-table Join relationships. Rewrite SQLwield to increase or decrease nodes in the SQL abstract syntax tree. Generate SQL features to refer to the fingerprint generated in the slow SQL of JIRA.
[0075] In this implementation, SQL substatements are converted into abstract syntax trees, providing connection points between SQL statements and DSL statements. The conversion between SQL and syntax trees is flexible and scalable, which improves the efficiency of subsequent syntax queries.
[0076] Step S203: parse the DSL sub-statements and convert them into multiple syntax tree nodes.
[0077] Specifically, the above step S203 includes:
[0078] Step S2031: Use the Lucene package to process the DSL sub-statements to generate multiple Query objects.
[0079] Specifically, the Lucene package is used to process DSL substatements and construct Query objects.
[0080] The Query object is an abstract class in the search package of Lucene. The Query object includes multiple subclasses, each of which corresponds to a different type of search.
[0081] Step S2032: translate the Query object into SQL syntax according to the type of the Query object.
[0082] Among them, Query objects include TermQuery, BooleanQuery, RangeQuery, PrefixQuery, PhraseQuery, FuzzyQuery, WildcardQuery, MultiPhraseQuery, RegexpQuery, ConstantScoreQuery, DisjunctionMaxQuery, MatchAllDocsQuery, etc.
[0083] Among them, TermQuery can be understood as "term search". TermQuery is used to search for a term in the search engine. The translation of TermQuery into SQL statements is the syntax of where key = 'value'. The implementation code is as follows:
[0084] termQuery.getTerm().field()=termQuery.getTerm().text()
[0085] Among them, BooleanQuery is a Boolean search, which is actually a combined Query object. When searching, you can add various Query objects and indicate the logical relationship between them. All query types can be combined using BooleanQuery. BooleanQuery is a container for Boolean clauses. Use the corresponding API method to add clauses to BooleanQuery, indicate the relationship between clauses, and then perform recursive calls to finally generate multiple query syntax conversions.
[0086] Among them, RangeQuery is used to express the search condition within a certain range, and realizes the search function from a start term to an end term. When querying, the "start term" and "end term" can be included or not. In the parameter list, the last boolean value indicates whether the boundary condition itself is included, that is, when it is TRUE, it means that the boundary value is included, which can be expressed as "[begin TO end]" in characters; when it is FALSE, it means that the boundary value is not included, which can be expressed as "{begin TO end}" in characters.
[0087] Furthermore, RangeQuery includes TermRangeQuery and PointRangeQuery.
[0088] PrefixQuery is used to search using a prefix. First, define a term Term, which contains the field name to be searched and the prefix of the keyword, and then construct a PrefixQuery object through the term Term to implement prefix search.
[0089] Among them, PhraseQuery is used to find multiple different keywords. Specifically, when a user searches in a search engine, he searches for several different keywords. In the retrieved document, multiple keywords may be closely connected as a precise phrase; there may also be other irrelevant words between multiple keywords. It can be understood that when scoring the retrieved document, the document with short sentences has a higher score, and the document with irrelevant words has a lower score.
[0090] Among them, FuzzyQuery is a fuzzy query used to identify two similar words.
[0091] Among them, WildcardQuery is a query type that supports wildcards and is used to query all words in a field that match the specified wildcard expression. Wildcard queries can use "?" to represent any single character or "*" to represent any number of characters (including zero) in a wildcard expression.
[0092] Translate multiple Query objects into SQL syntax respectively to obtain multiple SQL syntax sentences.
[0093] Step S2033: convert the corresponding DSL sub-statement into a syntax tree node based on the SQL syntax.
[0094] Using the method of step S202, multiple SQL syntax sentences are converted into the form of an abstract syntax tree to obtain corresponding syntax tree nodes.
[0095] In this implementation, Query objects in DSL substatements are identified, syntax translation between DSL and SQL is performed based on each Query object, and multiple syntax tree nodes are obtained through conversion, thereby reducing the difficulty of syntax conversion and improving conversion efficiency.
[0096] Step 204: Add multiple syntax tree nodes to the SQL abstract syntax tree to obtain a hybrid abstract syntax tree.
[0097] For a syntax tree node, traverse the SQL abstract syntax tree to find the target node corresponding to the syntax tree node. The target node is the same as or corresponding to the root node in the syntax tree node. Add the corresponding syntax tree node to the target node.
[0098] Specifically, the above step S204 includes:
[0099] Step 2041, for a syntax tree node, traverse the SQL abstract syntax tree to find the corresponding target node.
[0100] Step 2042, adding the syntax tree node to the target node.
[0101] Specifically, obtain the column to be inserted, that is, the syntax tree node, identify whether the target column is included in the SQL abstract syntax tree, obtain the expression information of the Table, and check whether the SQL uses an alias. Add the column to be inserted, add the syntax tree node to the abstract syntax tree, and supplement the content in values.
[0102] All syntax tree nodes are traversed using the above method, and all syntax tree nodes are added to the SQL abstract syntax tree to obtain a hybrid abstract syntax tree.
[0103] Step 205: convert the hybrid abstract syntax tree into an SQL statement, and perform query and analysis based on the SQL statement.
[0104] Specifically, the hybrid abstract syntax tree is converted into an SQL statement, which is parsed by an SQL parser, which converts the SQL query statement into a query plan or an execution plan so that the database engine can execute the query operation.
[0105] The DSL and SQL grammar hybrid analysis method provided in this embodiment is aimed at the mixed statements of DSL and SQL, splits the mixed statements, parses the DSL sub-statements and SQL sub-statements respectively, converts the SQL sub-statements into abstract syntax trees, provides the connection points between SQL statements and DSL statements, and the conversion between SQL and syntax trees is flexible and scalable; for DSL sub-statements, the syntax translation between DSL and SQL is performed based on each Query object, and converted into syntax tree nodes that can be recognized by the SQL syntax tree, thereby reducing the difficulty of syntax conversion and improving the conversion efficiency. Using the syntax tree as the transit information, the syntax tree nodes of DSL are added to the SQL abstract syntax tree, and finally converted into SQL statements for query. This implementation method solves the problem of DSL and SQL mixed statement query, can automatically perform syntax parsing and conversion, realizes the easy combination of DSL syntax and SQL syntax, reduces the difficulty of manually writing DSL syntax into SQL syntax, reduces the writing error rate, and improves the efficiency of syntax conversion. It can be applied to a variety of business scenarios and database applications, and has practicality and economic benefits.
[0106] In this embodiment, a DSL and SQL grammar hybrid analysis device is also provided, which is used to implement the above-mentioned embodiments and preferred implementation modes, and will not be repeated hereafter. As used below, the term "module" can be a combination of software and / or hardware that implements a predetermined function. Although the device described in the following embodiments is preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and conceivable.
[0107] This embodiment provides a DSL and SQL grammar hybrid analysis device, such as Figure 4 As shown, Figure 4 : is a structural block diagram of a DSL and SQL grammar hybrid analysis device according to an embodiment of the present invention, the DSL and SQL grammar hybrid analysis device comprises:
[0108] The segmentation module 401 is used to segment the mixed statement to obtain DSL sub-statements and SQL sub-statements. The mixed statement consists of DSL statements and SQL statements.
[0109] The first parsing module 402 is used to parse the SQL sub-statements and generate a SQL abstract syntax tree.
[0110] The second parsing module 403 is used to parse the DSL sub-statements and convert them into multiple syntax tree nodes.
[0111] The combination module 404 is used to add multiple syntax tree nodes to the SQL abstract syntax tree to obtain a hybrid abstract syntax tree.
[0112] The query module 405 is used to convert the hybrid abstract syntax tree into an SQL statement, and perform query and analysis based on the SQL statement.
[0113] The further functional description of each of the above modules is the same as that of the above corresponding embodiments and will not be repeated here.
[0114] The DSL and SQL syntax hybrid analysis device in this embodiment is presented in the form of a functional unit, where the unit refers to an ASIC (Application Specific Integrated Circuit) circuit, a processor and memory that executes one or more software or fixed programs, and / or other devices that can provide the above functions.
[0115] The embodiment of the present invention also provides a computer device having the above Figure 4 The DSL and SQL syntax hybrid analysis device shown.
[0116] See also Figure 5 , Figure 5 is a schematic diagram of the structure of a computer device provided by an optional embodiment of the present invention, such as Figure 5 As shown, the computer device includes: one or more processors 10, a memory 20, and interfaces for connecting various components, including high-speed interfaces and low-speed interfaces. Various components are connected to each other using different buses for communication, and can be installed on a common mainboard or installed in other ways as needed. The processor can process instructions executed in the computer device, including instructions stored in or on the memory to display the graphical information of the GUI on an external input / output device (such as a display device coupled to the interface). In some optional embodiments, if necessary, multiple processors and / or multiple buses can be used together with multiple memories. Similarly, multiple computer devices can be connected, and each device provides some necessary operations (for example, as a server array, a group of blade servers, or a multi-processor system). Figure 5 A processor 10 is taken as an example.
[0117] The processor 10 may be a central processing unit, a network processor or a combination thereof. The processor 10 may further include a hardware chip. The hardware chip may be a dedicated integrated circuit, a programmable logic device or a combination thereof. The programmable logic device may be a complex programmable logic device, a field programmable gate array, a general purpose array logic or any combination thereof.
[0118] The memory 20 stores instructions executable by at least one processor 10, so that the at least one processor 10 executes the method shown in the above embodiment.
[0119] The memory 20 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the computer device, etc. In addition, the memory 20 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some optional embodiments, the memory 20 may optionally include a memory remotely arranged relative to the processor 10, and these remote memories may be connected to the computer device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0120] The memory 20 may include a volatile memory, such as a random access memory; the memory may also include a non-volatile memory, such as a flash memory, a hard disk or a solid state drive; the memory 20 may also include a combination of the above types of memory.
[0121] The computer device also includes an input device 30 and an output device 40. The processor 10, the memory 20, the input device 30 and the output device 40 may be connected via a bus or other means. Figure 5 The example of connecting through bus is taken in the following.
[0122] The input device 30 can receive input digital or character information, and generate key signal input related to the user settings and function control of the computer device, such as a touch screen, a keypad, a mouse, a track pad, a touch pad, an indicator bar, one or more mouse buttons, a trackball, a joystick, etc. The output device 40 may include a display device, an auxiliary lighting device (e.g., an LED) and a tactile feedback device (e.g., a vibration motor), etc. The above-mentioned display device includes but is not limited to a liquid crystal display, a light emitting diode, a display and a plasma display. In some optional embodiments, the display device can be a touch screen.
[0123] The embodiment of the present invention also provides a computer-readable storage medium. The method according to the embodiment of the present invention can be implemented in hardware, firmware, or can be implemented as a computer code that can be recorded in a storage medium, or can be implemented as a computer code that is originally stored in a remote storage medium or a non-temporary machine-readable storage medium and will be stored in a local storage medium through a network download, so that the method described herein can be stored in such software processing on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. Among them, the storage medium can be a magnetic disk, an optical disk, a read-only storage memory, a random access memory, a flash memory, a hard disk or a solid-state hard disk, etc.; further, the storage medium can also include a combination of the above types of memories. It can be understood that a computer, a processor, a microprocessor controller, or programmable hardware includes a storage component that can store or receive software or computer code. When the software or computer code is accessed and executed by a computer, a processor, or hardware, the method shown in the above embodiment is implemented.
[0124] A part of the present invention may be applied as a computer program product, such as a computer program instruction, which, when executed by a computer, can call or provide the method and / or technical solution according to the present invention through the operation of the computer. Those skilled in the art should understand that the existence of the computer program instruction in a computer-readable medium includes, but is not limited to, a source file, an executable file, an installation package file, etc., and accordingly, the way in which the computer program instruction is executed by the computer includes, but is not limited to: the computer directly executes the instruction, or the computer compiles the instruction and then executes the corresponding compiled program, or the computer reads and executes the instruction, or the computer reads and installs the instruction and then executes the corresponding installed program. Here, the computer-readable medium may be any available computer-readable storage medium or communication medium accessible to the computer.
[0125] Although the embodiments of the present invention have been described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present invention, and such modifications and variations are all within the scope defined by the appended claims.
Claims
1. A DSL and SQL grammar hybrid analysis method, characterized in that: The method comprises: Processing the mixed statement to obtain a DSL sub-statement and an SQL sub-statement, wherein the mixed statement is composed of a DSL statement and an SQL statement; Parsing the SQL sub-statements to generate an SQL abstract syntax tree; Parsing the DSL sub-statements and converting them into multiple syntax tree nodes; Adding a plurality of the syntax tree nodes to the SQL abstract syntax tree to obtain a hybrid abstract syntax tree; The hybrid abstract syntax tree is converted into an SQL statement, and query and analysis are performed based on the SQL statement.
2. The DSL and SQL grammar hybrid analysis method according to claim 1, characterized in that: The processing of the mixed statement to obtain the DSL sub-statement and the SQL sub-statement includes: Saving the mixed statement as a mixed character string; DSL and SQL identification is performed on the mixed character string, and the mixed character string is cut into the DSL sub-statement and the SQL sub-statement.
3. The DSL and SQL grammar hybrid analysis method according to claim 1, characterized in that: The parsing of the SQL substatements to generate an SQL abstract syntax tree comprises: Performing lexical analysis on the SQL substatement; The syntax parser is used to perform syntax analysis on the SQL sub-statements after lexical analysis to generate an SQL abstract syntax tree.
4. The DSL and SQL grammar hybrid analysis method according to claim 1, characterized in that: The word segmentation processing of the DSL sub-sentences to convert the DSL sub-sentences into multiple syntax tree nodes includes: Using the Lucene package to process the DSL sub-statements to generate multiple Query objects; According to the type of the Query object, the Query object is translated into SQL syntax; The corresponding DSL sub-statement is converted into the syntax tree node based on the SQL grammar.
5. The DSL and SQL grammar hybrid analysis method according to claim 4, characterized in that: The Query objects include TermQuery, BooleanQuery, RangeQuery, PrefixQuery, PhraseQuery, FuzzyQuery, WildcardQuery, MultiPhraseQuery, RegexpQuery, ConstantScoreQuery, DisjunctionMaxQuery, and MatchAllDocsQuery.
6. The DSL and SQL grammar hybrid analysis method according to claim 1, characterized in that: Adding a plurality of syntax tree nodes to the SQL abstract syntax tree to obtain a hybrid abstract syntax tree comprises: For a syntax tree node, traverse the SQL abstract syntax tree to find a corresponding target node; Adding the syntax tree node to the target node; All the syntax tree nodes are traversed, and all the syntax tree nodes are added to the SQL abstract syntax tree to obtain the hybrid abstract syntax tree.
7. A DSL and SQL grammar hybrid analysis device, characterized in that: The device comprises: A segmentation module, used for performing sentence segmentation on the mixed statement to obtain DSL sub-statements and SQL sub-statements, wherein the mixed statement is composed of DSL statements and SQL statements; A first parsing module, used to parse the SQL sub-statement and generate an SQL abstract syntax tree; A second parsing module, used to parse the DSL sub-statements and convert them into multiple syntax tree nodes; A combining module, used for adding a plurality of the syntax tree nodes to the SQL abstract syntax tree to obtain a hybrid abstract syntax tree; The query module is used to convert the hybrid abstract syntax tree into an SQL statement, and perform query and analysis based on the SQL statement.
8. A computer device, characterized in that: include: A memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the DSL and SQL grammar hybrid analysis method according to any one of claims 1 to 6 by executing the computer instructions.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a computer to execute the DSL and SQL grammar hybrid analysis method according to any one of claims 1 to 6.
10. A computer program product, characterized in that The method comprises computer instructions, wherein the computer instructions are used to cause a computer to execute the DSL and SQL grammar hybrid analysis method according to any one of claims 1 to 6.