Data lineage analysis method, device and server
By establishing an abstract syntax tree of SQL statements in data lineage analysis, extracting query statements and obtaining related data tables and columns, the problem of insufficient flexibility and accuracy of SQL statement analysis in the existing technology is solved, and the analysis of complex SQL statements is realized. Accurate analysis and flexible application.
Patent Information
- Application Number
- CN202011249929.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-11-10
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2040-11-10
AI Technical Summary
In data lineage analysis, the existing technology is not flexible enough and the analysis results are not accurate enough, especially for complex SQL statements, and cannot meet the needs of complex data lineage analysis.
By establishing an abstract syntax tree of the target SQL statement, the query statement related to the query operation is extracted, the associated data table and data column are obtained according to the target column name, and the data lineage analysis results are determined. This method is suitable for all types of SQL statement source codes, can handle complex SQL statements, and improves the accuracy and flexibility of analysis results.
It realizes accurate data lineage analysis of complex SQL statements, is suitable for a wide range of data lineage analysis needs, and improves the flexibility of analysis applications and the accuracy of results.
Smart Images

Figure CN112347123B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing technology, and in particular to a data lineage analysis method, device and server. Background Art
[0002] Data lineage refers to the relationship between data during the process of data generation, processing, integration, circulation, and eventual extinction. Data lineage analysis can be used to analyze the impact of changes in upstream data on downstream data, and to track the source of upstream problems when downstream data changes.
[0003] In the related technology, when performing lineage analysis on data, if you want to analyze which physical table a column of data in a SQL (Structured Query Language) statement comes from, you can use string matching to perform the analysis. For example, you can match the column name and table name contained in the SQL statement with each physical table. However, the SQL statements of different versions of databases may be different. The diversity of SQL statements causes this string matching method to be unable to process complex SQL statements, and at the same time cannot guarantee the accuracy of the analysis results. In another way, for open source databases, you can obtain the input table and output table from the internal interface in the source code of the database, and analyze the physical table of the data source based on the obtained input table and output table. However, this method lacks flexibility, cannot meet the more complex data lineage analysis needs, and has a relatively limited scope of application. Summary of the invention
[0004] In view of this, an object of the present invention is to provide a data lineage analysis method, device and server to improve the flexibility of data lineage analysis applications and the accuracy of analysis results.
[0005] In a first aspect, an embodiment of the present invention provides a method for analyzing data lineage, the method comprising: establishing an abstract syntax tree of a target SQL statement; based on the abstract syntax tree, extracting query statements related to query operations in the target SQL statement; according to a target column name contained in the query statement, obtaining a target data table associated with the target column name, and a target data column associated with the target column name in the target data table; and determining a data lineage analysis result of the target SQL statement according to the target data table and the target data column.
[0006] Furthermore, the step of extracting query statements related to the query operation in the target SQL statement includes: if the operation type of the target SQL statement is a query operation, determining the target SQL statement as the query statement related to the query operation; if the operation type of the target SQL statement is an insert operation, an update operation or a delete operation, extracting query statements related to the query operation from the target SQL statement based on a preset query operation keyword.
[0007] Furthermore, according to the target column name contained in the query statement, the step of obtaining the target data table associated with the target column name and the target data column associated with the target column name in the target data table includes: extracting the target data table corresponding to the target column name contained in the query statement and the hierarchical relationship between the target data tables from the abstract syntax tree; establishing a multi-branch tree of the query statement according to the from statement in the query statement; wherein each of the from statement corresponds to a node in the multi-branch tree; traversing the multi-branch tree according to the target data table and the hierarchical relationship between the target data tables, obtaining the target data table associated with the target column name and the target data column associated with the target column name in the target data table from the multi-branch tree, until reaching the bottom-level data source table.
[0008] Furthermore, the step of extracting the target data table corresponding to the target column name contained in the query statement and the hierarchical relationship between the target data tables from the abstract syntax tree includes: extracting the item entries contained in the query statement and the types of the item entries from the abstract syntax tree; for each of the item entries, extracting the data columns involved in the item entry from the item entry in a manner that matches the type of the item entry; determining the data table to which the data columns involved in each of the item entries belong as the target data table corresponding to the target column name contained in the query statement; and determining the hierarchical relationship between the data tables to which the data columns involved in the item entries belong as the hierarchical relationship between the target data tables corresponding to the target column names.
[0009] Furthermore, the step of extracting item entries contained in the query statement includes: if the query statement includes a union statement, extracting item entries from statements on both sides of the union statement respectively.
[0010] Furthermore, the step of establishing a multi-branch tree of the query statement according to the from statement in the query statement includes: establishing an initial node of the multi-branch tree and determining the initial node as the current node; obtaining the from statement in the query statement from the abstract syntax tree according to the hierarchical structure of the query statement; if the from statement is obtained, determining the from statement as a child node of the current node; and performing subsequent processing on the child node according to the type of statement connected to the from statement.
[0011] Furthermore, the step of obtaining the from statement in the query statement from the abstract syntax tree includes: if the query statement contains a union statement, statements on both sides of the union statement are respectively used as updated query statements, and the step of obtaining the from statement in the query statement from the abstract syntax tree is executed.
[0012] Furthermore, the step of performing subsequent processing on the child node according to the type of statement connected to the from statement includes: if the from statement is connected to a data table, extracting the data table and recording the data table in the child node; if the from statement is connected to a join statement, performing subsequent processing on the statements on both sides of the join statement according to the types of statements on both sides of the join statement; if the from statement is connected to a sub-query statement, determining the child node as an updated current node, determining the sub-query statement as an updated query statement, and continuing to execute the step of obtaining the from statement in the query statement from the abstract syntax tree until the bottom level of the abstract syntax tree is reached.
[0013] Furthermore, the step of determining the data lineage analysis result of the target SQL statement based on the target data table and the target data column includes: obtaining the data source information and database information of the target data table; determining the data lineage analysis result of the target SQL statement based on the target data table, the target data column, and the data source information and database information of the target data table.
[0014] Furthermore, before the step of determining the data lineage analysis result of the target SQL statement according to the target data table, the target data column, and the data source information and database information of the target data table, the method also includes: if the operation type of the target SQL statement is an insert operation, an update operation, or a delete operation, obtaining the data table to be written corresponding to the target column name, and the column name to be written in the data table to be written corresponding to the target column name; obtaining the data source information and database information of the data table to be written; the step of determining the data lineage analysis result of the target SQL statement according to the target data table, the target data column, and the data source information and database information of the target data table includes: determining the target data table, the target data column, the data source information and database information of the target data table, the data table to be written, the column name to be written, and the data source information and database information of the data table to be written as the data lineage analysis result of the target SQL statement.
[0015] Furthermore, the step of obtaining the column names to be written in the data table to be written corresponding to the target column names includes: obtaining the column names to be written in the data table to be written corresponding to the target column names from the target SQL statement; if the column names to be written cannot be obtained from the target SQL statement, obtaining all column names of the data table to be written according to the metadata of the data table to be written, and determining all column names as the column names to be written.
[0016] In a second aspect, an embodiment of the present invention provides a data lineage analysis device, the device comprising: an establishment module, used to establish an abstract syntax tree of a target SQL statement; an extraction module, used to extract query statements related to query operations in the target SQL statement based on the abstract syntax tree; an acquisition module, used to acquire a target data table associated with the target column name and a target data column associated with the target column name in the target data table according to a target column name included in the query statement; and a determination module, used to determine a data lineage analysis result of the target SQL statement based on the target data table and the target data column.
[0017] In a third aspect, an embodiment of the present invention provides a server, including a processor and a memory, wherein the memory stores machine executable instructions that can be executed by the processor, and the processor executes the machine executable instructions to implement the data lineage analysis method described in any one of the first aspects above.
[0018] In a fourth aspect, an embodiment of the present invention provides a machine-readable storage medium, which stores machine-executable instructions. When the machine-executable instructions are called and executed by a processor, the machine-executable instructions prompt the processor to implement the data lineage analysis method described in any one of the first aspects above.
[0019] The data lineage analysis method, device and server provided by the present invention extract the query statement related to the query operation in the target SQL statement based on the abstract syntax tree of the established target SQL statement, obtain the target data table and the associated target data column associated with the target column name according to the target column name contained in the query statement, and then determine the data lineage analysis result of the target SQL statement. In this method, based on the abstract syntax tree corresponding to the SQL statement, the column name involved in the SQL statement is analyzed to obtain the data table and data column related to the column name, and then the data lineage analysis result of the SQL statement is obtained; even for more complex SQL statements, more accurate data lineage analysis results can be obtained through this method; at the same time, this method is more flexible, applicable to various types of SQL statement source codes, applicable to more complex data lineage analysis needs, and has a wide range of applications.
[0020] Other features and advantages of the present invention will be described in the following description, and partly become apparent from the description, or understood by practicing the present invention. The purpose and other advantages of the present invention are realized and obtained by the structures particularly pointed out in the description, claims and drawings.
[0021] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] 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 those skilled in the art, other drawings can be obtained based on these drawings without paying any creative work.
[0023] Figure 1 A flow chart of a method for analyzing data lineage provided by an embodiment of the present invention;
[0024] Figure 2 A flowchart of another data lineage analysis method provided by an embodiment of the present invention;
[0025] Figure 3 A flowchart of another data lineage analysis method provided by an embodiment of the present invention;
[0026] Figure 4 A flowchart of another data lineage analysis method provided by an embodiment of the present invention;
[0027] Figure 5 An overall flow chart of a data lineage analysis method provided by an embodiment of the present invention;
[0028] Figure 6 A flowchart for obtaining a correspondence between a column and a physical table provided by an embodiment of the present invention;
[0029] Figure 7 A flowchart of obtaining a correspondence between a downstream column and a current level table provided by an embodiment of the present invention;
[0030] Figure 8 A flowchart of constructing a multi-branch tree provided by an embodiment of the present invention;
[0031] Fig. 9 A schematic diagram of the structure of a data lineage analysis device provided by an embodiment of the present invention;
[0032] Fig.10 A schematic diagram of the structure of a server provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0033] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the accompanying drawings. 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.
[0034] Data lineage refers to the relationship between data formed during the process of data generation, processing, integration, circulation, and final extinction. This relationship between data can be expressed by borrowing a similar relationship in human society, which can be called the blood relationship of data. Data lineage is one of the components of metadata, which can be used to analyze the blood path of a table or field from the data source to the current table, and whether the relationship between the blood fields is satisfied, focusing on data consistency and the rationality of table design. It can also be used to analyze the impact of changes in upstream data on downstream data, and to track the source of upstream problems when downstream data changes. In big data cloud products, data developers will develop various tasks to process data, and these tasks are interdependent. When one of the jobs fails, the data developer needs to locate the problem; or when modifying the upstream table structure, analyze which related downstream tables there are and evaluate the resulting impact; or after the upstream table data changes, analyze which related data needs to be re-run.
[0035] In the related technology, when performing lineage analysis on data, the user needs to analyze which physical table a column of data in a SQL statement actually comes from. Usually, the following two methods can be used. One method is to simply parse based on string matching. Considering that there are many subqueries in SQL statements, the SQL statements of different versions of databases vary greatly. The flexibility and diversity of SQL statements make it difficult to achieve complete matching based solely on strings, resulting in this method being unable to handle complex SQL statements and unable to guarantee the accuracy of the analysis results.
[0036] In another way, the input table and output table obtained from the internal interface in the source code of some open source databases can be used to parse the data lineage. For a certain column of data, the input table can be understood as the source table of this column of data; the output table can be understood as the data table where this column of data is to be stored; for example, the LineageInfo in the Hive database can be used to obtain the input table and the output table; however, this method lacks flexibility. For example, while tracing the source of a certain column of data, it is also necessary to trace which methods and functions are used in the process of lineage transmission of this column of data. For this customized lineage relationship requirement, it is necessary to go deep into each source code and carry out a lot of development; in addition, the SQL standards of different data sources are not consistent, and they will have their own unique syntax or functions. Each data source needs to develop a lineage parsing function separately. For example, the SQL syntax of MySQL database and Hive database is usually inconsistent, and may also include some custom functions, special syntax or functions, etc.; furthermore, for non-open source projects, their internal parsing methods cannot be obtained, and this method cannot be associated with database metadata.
[0037] Based on this, the embodiment of the present invention provides a data lineage analysis method, device and server, which can be applied to the scenario of analyzing data lineage. To facilitate understanding of this embodiment, the data lineage analysis method disclosed in the embodiment of the present invention is first introduced in detail. Figure 1 As shown, the method comprises the following steps:
[0038] Step S102: Create an abstract syntax tree of the target SQL statement.
[0039] The above-mentioned SQL statement can be understood as a database query and programming language, which can be used to access data as well as query, update and manage relational database systems; the above-mentioned abstract syntax tree (Abstract Syntax Tree, AST for short) can be understood as a tree representation of the abstract syntax structure of the source code, and each node on the tree represents a structure in the source code. The abstract syntax tree does not depend on the syntax of the source language and will not show every detail of the real syntax. After the source code is converted into AST, the AST can be operated on to achieve the required functions; in actual implementation, the target SQL statement can be a SQL statement input by the user, or a SQL statement called through an API (Application Programming Interface). When data lineage analysis is required for the target SQL statement, it is necessary to first establish an abstract syntax tree that matches the SQL statement.
[0040] Step S104: extracting query statements related to the query operation in the target SQL statements based on the abstract syntax tree.
[0041] The query statements related to the query operation may be statements related to the select part in the SQL statement, etc.; after an abstract syntax tree matching the target SQL statement is established, query statements related to the query operation may be extracted from the abstract syntax tree, for example, statements related to the select part may be extracted.
[0042] Step S106, according to the target column name included in the query statement, obtain the target data table associated with the target column name, and the target data column associated with the target column name in the target data table.
[0043] The above-mentioned target column name can be understood as the column name to be queried contained in the query statement, and the target column name can be obtained by parsing the query statement in the target SQL statement; the above-mentioned target data table can contain the target column name, and the data column associated with the target column name in the target data table can be understood as the target data column; for example, taking select c1from t as an example, it means obtaining the c1 column from the c1 column in table t; wherein c1 is the target column name, t is the target data table, and c1 in the target data table t is the target data column; in actual implementation, there can be multiple target data tables associated with the target column name, and accordingly, there can also be multiple target data columns associated with the target column name in the target data table.
[0044] Step S108, determining the data lineage analysis result of the target SQL statement according to the target data table and the target data column.
[0045] The above-mentioned data lineage analysis results usually include the link relationship between the acquired target data tables, the processing methods or processes adopted between the target data columns corresponding to the target data tables at different levels, etc.; based on the target data tables and target data columns acquired in the above steps, the data lineage analysis results of the target SQL statement can be determined.
[0046] The embodiment of the present invention provides a data lineage analysis method, which extracts query statements related to query operations in the target SQL statement based on the abstract syntax tree of the established target SQL statement, obtains the target data table and the associated target data column associated with the target column name according to the target column name contained in the query statement, and then determines the data lineage analysis result of the target SQL statement. In this method, based on the abstract syntax tree corresponding to the SQL statement, the column name involved in the SQL statement is analyzed to obtain the data table and data column related to the column name, and then the data lineage analysis result of the SQL statement is obtained; even for more complex SQL statements, more accurate data lineage analysis results can be obtained through this method; at the same time, this method is more flexible, applicable to various types of SQL statement source codes, applicable to more complex data lineage analysis needs, and has a wide range of applications.
[0047] The embodiment of the present invention also provides another data lineage analysis method, which is implemented on the basis of the above embodiment method; the method focuses on describing the specific process of extracting query statements related to the query operation in the target SQL statement, such as Figure 2 As shown, the method comprises the following steps:
[0048] Step S202: Create an abstract syntax tree of the target SQL statement.
[0049] In actual implementation, you can use Druid (an open source database connection pool) to build an SQL Statement, that is, an abstract syntax tree, according to the target SQL statement and the data source type. You can also use other methods to build the abstract syntax tree of the target SQL statement. Among them, the data source can be understood as the database or database server used by the database application. The data source type can include different types such as MySQL database or Hive database. In general, the same SQL statement and different data source types construct basically the same AST tree, and usually only the construction process will be different. Druid can be understood as an open source database connection pool. Druid usually includes Druid SQL Parser. Druid uses built-in SQL Parser to implement defense against SQL injection, merge statistics of non-parameterized SQL, SQL formatting, and sub-library and table sharding.
[0050] Step S204: based on the abstract syntax tree, if the operation type of the target SQL statement is a query operation, the target SQL statement is determined as a query statement related to the query operation.
[0051] In actual implementation, it is usually determined first whether the operation type of the target SQL statement is a query operation, an insert operation, an update operation or a delete operation. If it is a query operation, such as a select query, the target SQL statement is determined to be a query statement related to the query operation.
[0052] Step S206: If the operation type of the target SQL statement is an insert operation, an update operation or a delete operation, based on a preset query operation keyword, a query statement related to the query operation is extracted from the target SQL statement.
[0053] The above-mentioned query operation keyword can be understood as the keyword used to execute the query operation contained in the target SQL statement whose operation type is an insert operation, an update operation or a delete operation, such as a select query, etc. The keyword can be set in advance according to actual conditions; if the operation type of the target SQL statement is an insert operation, an update operation or a delete operation, that is, an insert operation, an update operation or a delete operation, the query statement related to the query operation can be extracted from the target SQL statement based on the pre-set query operation keyword and the abstract syntax tree. It can also be understood that when the operation type of the target SQL statement is an insert operation, an update operation or a delete operation, it is usually necessary to first parse the query part of the statement from the target SQL statement.
[0054] Step S208: According to the target column name included in the query statement, a target data table associated with the target column name and a target data column associated with the target column name in the target data table are obtained.
[0055] Step S210, determining the data lineage analysis result of the target SQL statement according to the target data table and the target data column.
[0056] Another data lineage analysis method provided by an embodiment of the present invention focuses on the specific process of extracting query statements related to query operations in a target SQL statement. After establishing an abstract syntax tree of the target SQL statement, if the operation type of the target SQL statement is a query operation, the target SQL statement is determined as a query statement related to the query operation. If the operation type of the target SQL statement is an insert operation, an update operation, or a delete operation, based on a preset query operation keyword, a query statement related to the query operation is extracted from the target SQL statement; according to the target column name contained in the query statement, a target data table and an associated target data column associated with the target column name are obtained, and then the data lineage analysis result of the target SQL statement is determined. In this method, based on the abstract syntax tree corresponding to the SQL statement, the column name involved in the SQL statement is analyzed to obtain the data table and data column related to the column name, and then the data lineage analysis result of the SQL statement is obtained; even for a relatively complex SQL statement, a relatively accurate data lineage analysis result can be obtained through this method; at the same time, this method is relatively flexible, applicable to various types of SQL statement source codes, applicable to relatively complex data lineage analysis requirements, and has a wide range of applications.
[0057] The embodiment of the present invention also provides another data lineage analysis method, which is implemented on the basis of the method of the above embodiment; the method focuses on describing the specific process of obtaining the target data table associated with the target column name and the target data column associated with the target column name in the target data table according to the target column name contained in the query statement, such as Figure 3 As shown, the method comprises the following steps:
[0058] Step S302: Create an abstract syntax tree of the target SQL statement.
[0059] Step S304: extracting query statements related to the query operation in the target SQL statement based on the abstract syntax tree.
[0060] Step S306: extracting the target data tables corresponding to the target column names contained in the query statement and the hierarchical relationships between the target data tables from the abstract syntax tree.
[0061] The hierarchical relationship between the above target data tables can be implemented in a subquery manner, wherein a subquery can be understood as a form of writing in which several small queries with different functions are nested in a complete query statement, thereby completing a complex query together. It can also be understood as another select query statement being nested in a select query statement. In actual implementation, the target data tables corresponding to the target column names contained in the query statement are extracted from the abstract syntax tree. The number of the target data tables can be multiple, and the hierarchical relationship between the multiple target data tables is obtained. Specifically, step S306 can be implemented by following steps 1 to 4:
[0062] Step 1: Extract the item entries and item entry types contained in the query statement from the abstract syntax tree.
[0063] The above-mentioned item entries may be target column names, functions or subqueries contained in the query statement; the types of the above-mentioned item entries may be constants, methods or aggregate functions; in actual implementation, it is usually necessary to first determine whether the query statement includes a union statement, wherein a union statement generally combines two or more select statements to merge the result sets of two or more select statements; if the query statement does not include a union statement, the item entries and the types of the item entries contained in the select query statement are extracted; if the query statement includes a union statement, the item entries and the types of the item entries are extracted from the statements on both sides of the union statement respectively.
[0064] Step 2: For each item entry, extract the data column involved in the item entry in a manner that matches the type of the item entry.
[0065] In actual implementation, considering that item entries usually include multiple types, it is necessary to extract the data columns involved in the item entries based on different types; for example, if the type of the item entry is a constant, such as 1, "a", then usually no data columns are involved; if the type of the item entry is a method, such as replace(ta,"a"), then the data columns involved in the method, such as ta, are extracted; if the type of the item entry is an aggregate function, such as the conversion type method, case syntax, binary operator, multi-element operator, etc., then corresponding processing is performed according to different aggregate function types to extract the data columns involved in the item entry.
[0066] Step three: determine the data table to which the data column involved in each item belongs as the target data table corresponding to the target column name included in the query statement.
[0067] Based on the corresponding data columns extracted from each item through the above steps, the data tables to which each data column belongs are determined, and these data tables are determined as target data tables corresponding to the target column names.
[0068] Step 4: determine the hierarchical relationship between the data tables to which the data columns involved in the item entry belong as the hierarchical relationship between the target data tables corresponding to the target column names.
[0069] Step S308, establishing a multi-branch tree of the query statement according to the from statement in the query statement; wherein each from statement corresponds to a node in the multi-branch tree.
[0070] Each node in the above multi-branch tree usually has one data item, and each node can have two or more child nodes; in the above from statement, from usually contains a table name, and the data table corresponding to the table name can be parsed based on AST. If it is a subquery, the data table involved in the subquery is the child node; it can also be understood that each from statement corresponds to a node in the multi-branch tree, and the multi-branch tree corresponding to the query statement can be constructed according to the from statement in the query statement, and the multi-branch tree contains the relationship between the subqueries. Specifically, step S308 can be implemented by following steps five to eight:
[0071] Step 5: Establish an initial node of the multi-branch tree and determine the initial node as the current node.
[0072] The above initial node can be understood as the root node of the multi-branch tree. The initial node is used as the current node to continue to execute the subsequent steps of establishing the multi-branch tree.
[0073] Step 6: According to the hierarchical structure of the query statement, obtain the from statement in the query statement from the abstract syntax tree.
[0074] The above hierarchical structure can be understood as a hierarchical recursive structure of a query statement. In actual implementation, it is usually necessary to first determine whether the query statement includes a union statement. If the query statement does not include a union statement, obtain the from statement in the query statement from the abstract syntax tree according to the hierarchical structure; if the query statement contains a union statement, use the statements on both sides of the union statement as updated query statements and execute the step of obtaining the from statement in the query statement from the abstract syntax tree.
[0075] Step seven: if a from statement is obtained, the from statement is determined as a child node of the current node.
[0076] The above child nodes can be understood as the next level nodes of the current node; if the from statement in the query statement is obtained from the abstract syntax tree, the from statement is inserted as a child node of the current node.
[0077] Step eight, performing subsequent processing on the child node according to the type of the statement connected to the from statement.
[0078] In actual implementation, the statements connected by the from statement may include multiple different types, such as single table type, join statement type or subquery type, etc., and the child nodes need to be processed accordingly according to different types; specifically, step eight can be implemented by the following steps A to C:
[0079] Step A: If the from statement is connected to a data table, extract the data table and record the data table in the child node.
[0080] If the from statement is connected to a data table, it is equivalent to that the type of the statement connected by the from statement is the above-mentioned single table type. In this case, the data table can be extracted and recorded in the corresponding child node.
[0081] Step B: If the from statement is connected with a join statement, the statements on both sides of the join statement are subsequently processed according to the types of the statements on both sides of the join statement.
[0082] The From statement can be used to specify data query from one table or multiple tables. When querying multiple tables, multiple tables are usually joined through Join. If the From statement is connected to a Join statement, it is usually necessary to process the statements on both sides of the Join statement accordingly according to the type of the statements on both sides of the Join statement. For example, if the type of the statements on both sides of the Join statement is a single table type, the data tables on both sides of the Join statement can be extracted separately.
[0083] Step C: if the from statement is connected to a sub-query statement, the child node is determined as the updated current node, the sub-query statement is determined as the updated query statement, and the step of obtaining the from statement in the query statement from the abstract syntax tree is continued until the bottom level of the abstract syntax tree is reached.
[0084] If the from statement is connected to a subquery statement, each subquery statement needs to be processed separately, and the from statement in the subquery statement is obtained from the abstract syntax tree. If the from statement is obtained, the from statement is determined as the next-level child node of the above child node, and corresponding processing is performed according to the type of statement connected to the from statement. During the execution process, it is determined whether the from statement is the most basic from t form. If not, the from statement is recursively processed according to the different types of from statements until the bottom level of the abstract syntax tree is reached.
[0085] Step S310, traverse the multitree according to the target data table and the hierarchical relationship between the target data tables, obtain the target data table associated with the target column name and the target data column associated with the target column name in the target data table from the multitree, until reaching the bottom-level data source table.
[0086] According to the target data table extracted from the abstract syntax tree and the hierarchical relationship between the target data tables, the multi-branch tree obtained by the above steps is traversed. For example, a recursive post-order traversal method can be used to first visit the current node and then traverse the child nodes of the current node; or a layer-order traversal method can be used to traverse downward layer by layer starting from the current node, etc. The appropriate traversal method can be selected according to actual needs. By traversing the multi-branch tree, the target data table associated with the target column name and the target data column associated with the target column name in the target data table can be obtained from the multi-branch tree, until the final physical table associated with the target column name is found, and the final physical table is equivalent to the above-mentioned lowest-level data source table; for example, the target column name c comes from the t1 table, and the c column in the t1 table may come from the t2 table, and so on, until the lowest-level data source table associated with the target column name c is queried.
[0087] Step S312, determining the data lineage analysis result of the target SQL statement according to the target data table and the target data column.
[0088] An embodiment of the present invention also provides another data lineage analysis method, which focuses on describing the specific process of obtaining a target data table associated with a target column name and a target data column associated with the target column name in the target data table according to a target column name included in a query statement. Based on the established abstract syntax tree of the target SQL statement, the query statement related to the query operation in the target SQL statement is extracted, and the target data table corresponding to the target column name included in the query statement and the hierarchical relationship between the target data tables are extracted from the abstract syntax tree; a multi-branch tree of the query statement is established according to the from statement in the query statement; the multi-branch tree is traversed to obtain the target data table associated with the target column name and the target data column associated with the target column name in the target data table from the multi-branch tree until the bottom-level data source table is reached; and then the data lineage analysis result of the target SQL statement is determined. In this method, based on the abstract syntax tree corresponding to the SQL statement, the column names involved in the SQL statement are analyzed to obtain the data table and data column related to the column name, and then the data lineage analysis result of the SQL statement is obtained; even for more complex SQL statements, more accurate data lineage analysis results can be obtained through this method; at the same time, this method is more flexible and suitable for various types of SQL statement source codes, can meet more complex data lineage analysis needs, and has a wide range of applications.
[0089] The embodiment of the present invention also provides another data lineage analysis method, which is implemented on the basis of the above embodiment method; the method focuses on describing the specific process of determining the data lineage analysis result of the target SQL statement according to the target data table and the target data column, such as Figure 4 As shown, the method comprises the following steps:
[0090] Step S402: Create an abstract syntax tree of the target SQL statement.
[0091] Step S404: extracting query statements related to the query operation in the target SQL statements based on the abstract syntax tree.
[0092] Step S406: According to the target column name included in the query statement, a target data table associated with the target column name and a target data column associated with the target column name in the target data table are obtained.
[0093] Step S408, obtaining data source information and database information of the target data table.
[0094] The above-mentioned database information usually includes information such as the database name; the data source information usually includes information for establishing a database connection, such as the data source name. By providing the correct data source name, the corresponding database connection can be found; the data source name usually includes the data structure of the database information, and the data source name is the necessary information for the open database connection driver to connect to the database; in actual implementation, the target SQL statement generally only includes the table name of the target data table, and there is no need to specify specific database information, such as the database name, etc. The user can specify the relevant database information and data source information of the database to which the target data table belongs in the task or other locations, and the user can also set default data source information and database information; after obtaining the target data table associated with the target column name, the database information and data source information of the target data table are obtained to complete the relevant information of the target data table.
[0095] Step S410, if the operation type of the target SQL statement is an insert operation, an update operation or a delete operation, obtain the data table to be written corresponding to the target column name and the column name to be written in the data table to be written corresponding to the target column name.
[0096] The above-mentioned data table to be written can be understood as a data table into which the target column name and the corresponding data are to be inserted, a data table into which the target column name and the corresponding data are to be updated, or a data table into which the target column name and the corresponding data are to be deleted; the above-mentioned column name to be written can be understood as the table header name of the table column corresponding to the target column name in the data table to be written. Specifically, the step of obtaining the column name to be written in the data table to be written corresponding to the target column name can be implemented by the following steps nine to ten:
[0097] Step nine: Obtain the column name to be written in the data table to be written corresponding to the target column name from the target SQL statement.
[0098] If the target SQL statement specifies the name of the column to be written in the data table to be written, for example, the target SQL statement specifies that the target column name and the corresponding data need to be inserted into a column of the data table to be written, or the target column name and the corresponding data need to be updated to a column of the data table to be written, or a column corresponding to the target column name in the data table to be written needs to be deleted, then the name of the column to be written corresponding to the target column name in the data table to be written can be obtained from the target SQL statement.
[0099] Step 10: If the column name to be written cannot be obtained from the target SQL statement, all column names of the data table to be written are obtained according to the metadata of the data table to be written, and all column names are determined as the column names to be written.
[0100] The above metadata can be understood as structured data extracted from the data table to be written to describe its characteristics and content, such as column names and other information; if the target SQL statement does not clearly specify the column names to be written in the data table to be written, all column names can be obtained from the metadata of the data table to be written, and these column names are determined as the column names to be written; in actual implementation, the SQL statement may only contain the data table to be written, without specifying the column names to be written. In this case, it can usually be understood that all column names of the data table to be written need to be determined as the column names to be written.
[0101] Step S412, obtaining data source information and database information of the data table to be written.
[0102] In actual implementation, the target SQL statement generally only includes the table name of the data table to be written, and there is no need to specify specific database information, such as the database name, etc. The user can specify the relevant database information and data source information of the database to which the data table to be written belongs in the task or other locations, and the user can also set the default data source information and database information; if the data table to be written parsed from the target SQL statement does not include the data source information and database information, the database information and data source information of the data table to be written can be obtained from other locations or default settings by the user to complete the relevant information of the data table to be written.
[0103] Step S414, determining the data lineage analysis result of the target SQL statement according to the target data table, the target data column, and the data source information and database information of the target data table. Specifically, step S414 can be implemented by the following step 11:
[0104] Step eleven, determining the target data table, target data column, data source information and database information of the target data table, the data table to be written, the column name to be written, and the data source information and database information of the data table to be written as the data lineage analysis result of the target SQL statement.
[0105] The embodiment of the present invention also provides another data lineage analysis method, which focuses on describing the specific process of determining the data lineage analysis result of the target SQL statement according to the target data table and the target data column, extracting the query statement related to the query operation in the target SQL statement based on the abstract syntax tree of the established target SQL statement, obtaining the target data table and the associated target data column associated with the target column name according to the target column name contained in the query statement, and obtaining the data source information and database information of the target data table; determining the data lineage analysis result of the target SQL statement according to the target data table, the target data column, and the data source information and database information of the target data table. In this method, based on the abstract syntax tree corresponding to the SQL statement, the column name involved in the SQL statement is analyzed to obtain the data table and data column related to the column name, and then obtain the data lineage analysis result of the SQL statement; even for a relatively complex SQL statement, a relatively accurate data lineage analysis result can be obtained through this method; at the same time, this method is relatively flexible, applicable to various types of SQL statement source codes, applicable to relatively complex data lineage analysis requirements, and has a wide range of applications.
[0106] To further understand the above embodiment, an overall flow chart of a data lineage analysis method is provided below. Figure 5 As shown, according to the SQL statement and the data source type, Druid is used to build SQLStatement, that is, AST tree (equivalent to the abstract syntax tree of the target SQL statement established above); determine whether the operation type of the SQL statement is a select query operation, if it is a select query operation, obtain the correspondence between the downstream columns and the upstream physical table columns (equivalent to the above-mentioned acquisition of the target data table associated with the target column name and the target data column associated with the target column name in the target data table according to the target column name contained in the query statement; wherein the downstream column is equivalent to the target column name, and the upstream physical table is equivalent to the target data table); complete according to the user's default data source and database information (equivalent to the above-mentioned acquisition of the data source information and database information of the target data table; according to the target data table, the target data column, and the data source information and database information of the target data table, determine the data lineage analysis result of the target SQL statement).
[0107] If the operation type of the SQL statement is not a select query operation, obtain the query part of the SQL statement according to the AST tree (equivalent to extracting the query statement related to the query operation from the target SQL statement based on the preset query operation keywords if the operation type of the target SQL statement is an insert operation, an update operation, or a delete operation); obtain the correspondence between the query columns of the query part of the SQL statement and the columns of the upstream physical table (equivalent to obtaining the target data table associated with the target column name and the target data column associated with the target column name in the target data table according to the target column name contained in the query statement); determine whether the columns of the downstream table are specified when the SQL statement is an insert / update / delete operation. If the columns of the downstream table are specified, then The default data source and database information is completed (equivalent to the above-mentioned acquisition of the column name to be written in the data table to be written corresponding to the target column name from the target SQL statement; the target data table, target data column, data source information and database information of the target data table, the data table to be written, the column name to be written and the data source information and database information of the data table to be written are determined as the data lineage analysis result of the target SQL statement); if the columns of the downstream table are not specified in the SQL statement, the metadata information of the table is obtained to obtain all the columns in the table (equivalent to the above-mentioned acquisition of all the column names of the data table to be written according to the metadata of the data table to be written if the column name to be written cannot be obtained from the target SQL statement, and all the column names are determined as the column names to be written); completion is performed according to the user's default data source and database information.
[0108] Furthermore, Figure 5 The steps for obtaining the correspondence between the downstream columns and the upstream physical table columns can be specifically performed as follows: Figure 6 The flowchart of obtaining the correspondence between columns and physical tables is shown in the figure. After building the AST tree of the SQL statement, the correspondence between the downstream column and the current level table is obtained (equivalent to extracting the target data table corresponding to the target column name contained in the query statement and the hierarchical relationship between the target data tables from the abstract syntax tree as mentioned above); wherein the current level table can be understood as the data table where the downstream column is located; according to the table corresponding to the from part of the AST tree, a multi-branch tree is built according to the from statement (equivalent to building a multi-branch tree of the query statement according to the from statement in the query statement); according to the results of the above two steps, the correspondence between the downstream column and the current level table is traversed downward layer by layer until the correspondence between the downstream column and the final physical table is found (equivalent to traversing the multi-branch tree according to the target data table and the hierarchical relationship between the target data tables, obtaining the target data table associated with the target column name and the target data column associated with the target column name in the target data table from the multi-branch tree, until reaching the bottom-level data source table).
[0109] Furthermore, Figure 6 The steps for obtaining the correspondence between the downstream columns and the current level table can be specifically performed as follows: Figure 7 A flowchart for obtaining the correspondence between downstream columns and the current hierarchical table is shown. After building the AST tree of the SQL statement, it is determined whether the query statement is a union statement. If it is not a union statement, the item of the query part is obtained (equivalent to the above-mentioned extraction of the item entries and the type of item entries contained in the query statement); if it is a union statement, the obtained SQL on the left and right parts of the union are processed separately to obtain the item of the query part (equivalent to the above-mentioned if the query statement includes a union statement, extracting the item entries from the statements on both sides of the union statement respectively); according to the different types of item, all columns are obtained (equivalent to the above-mentioned method of extracting the data columns involved in each item entry from the item entry in a manner matching the type of the item entry); according to the obtained columns, the hierarchical table involved is determined, and then the correspondence between the current hierarchical tables is determined.
[0110] Furthermore, Figure 6 The steps to construct a multi-branch tree can be specifically described as follows: Figure 8The flowchart of constructing a multi-branch tree is shown in the figure. After establishing the initial node of the multi-branch tree and determining the initial node as the current node, it is determined whether the query statement is a union statement. If it is not a union statement, the from part is obtained (equivalent to obtaining the from statement in the query statement from the abstract syntax tree according to the hierarchical structure of the query statement); if it is a union statement, the SQL of the left and right parts of the union are obtained, and they are processed separately to obtain the from part (equivalent to the above-mentioned step of taking the statements on both sides of the union statement as updated query statements and executing the step of obtaining the from statement in the query statement from the abstract syntax tree if the query statement contains a union statement); it is determined whether the obtained from part is in the most basic from t form. If it is in the most basic from t form, from is inserted as a child node of the current Node (equivalent to the above-mentioned step of extracting the data table if the from statement is connected to a data table and recording the data table in the child node); if it is not the most basic from t, recursively process from according to different types of from, and continue to execute the step of obtaining the from part (equivalent to the above-mentioned if the from statement is connected with a join statement, according to the types of statements on both sides of the join statement, subsequent processing is performed on the statements on both sides of the join statement respectively; if the from statement is connected with a sub-query statement, the child node is determined as the updated current node, the sub-query statement is determined as the updated query statement, and the step of obtaining the from statement in the query statement from the abstract syntax tree is continued until the bottom level of the abstract syntax tree is reached), and the from part is constructed into a multi-branch tree according to the hierarchical structure.
[0111] The above-mentioned data lineage analysis method can help build a relationship diagram of data warehouse tables and column flows to ensure data traceability; the method can support lineage analysis of data sources such as Mysql, Oracle, Mpp, Hive, Spark, etc.; in addition, in addition to analyzing the lineage of data returned by SQL statements during execution, the method can also analyze the lineage of tasks. For example, in the data development process, there is a task that needs to run some data. The task usually includes SQL statements. Based on these SQL statements, the lineage relationship of the task can be analyzed; it should be noted that the above-mentioned data lineage analysis method is for one SQL statement. If there are multiple SQL statements, the method of the present invention needs to be executed for each SQL statement. Specifically, the role of data lineage analysis is mainly reflected in the following aspects:
[0112] 1. Data traceability
[0113] The blood relationship of data reflects the origin and development of data, and can be used to track the source of data and the data processing process. When data anomalies occur, it is necessary to track the cause of the anomaly and control the risk at an appropriate level. As enterprises develop, data sources are numerous and the quality is uneven. These factors will have a certain impact on data results, so having data traceability capabilities is of high value.
[0114] 2. Impact Analysis
[0115] As data applications increase, the data flow chain becomes longer and longer. Changes to the core business of a source must be synchronized with downstream analysis applications. If there is no impact analysis, abnormal access to various data services will occur. Data traceability allows the development department to conduct impact assessments more easily.
[0116] 3. Life Cycle
[0117] Through data lineage, you can intuitively get the entire life cycle of the data; for data of little value, you can consider stratification, archiving, or even destruction.
[0118] 4. Security control
[0119] Security and compliance departments usually need to know how data is used and who the data is used for. All this information can be obtained from data lineage. Data lineage will also provide a basis for permission management (table-level and field-level authorization), thereby ensuring data security at a higher level. In conjunction with security identifiers in metadata (for example, which data needs to be desensitized, etc.), global security management and control can be performed.
[0120] 5. Data assets
[0121] For company management, data lineage can be used to understand the overall data flow, which helps in formulating the company's data asset strategy.
[0122] Corresponding to the above method embodiment, the present invention provides a schematic diagram of the structure of a data lineage analysis device, such as Fig. 9 As shown, the device includes: an establishment module 90, which is used to establish an abstract syntax tree of the target SQL statement; an extraction module 91, which is used to extract the query statement related to the query operation in the target SQL statement based on the abstract syntax tree; an acquisition module 92, which is used to acquire the target data table associated with the target column name and the target data column associated with the target column name in the target data table according to the target column name included in the query statement; a determination module 93, which is used to determine the data lineage analysis result of the target SQL statement according to the target data table and the target data column.
[0123] The embodiment of the present invention provides a data lineage analysis device, which extracts query statements related to query operations in the target SQL statement based on the abstract syntax tree of the established target SQL statement, obtains the target data table and the associated target data column associated with the target column name according to the target column name contained in the query statement, and then determines the data lineage analysis result of the target SQL statement. In the device, based on the abstract syntax tree corresponding to the SQL statement, the column name involved in the SQL statement is analyzed to obtain the data table and data column related to the column name, and then the data lineage analysis result of the SQL statement is obtained; even for more complex SQL statements, more accurate data lineage analysis results can be obtained through this method; at the same time, this method is more flexible, applicable to various types of SQL statement source codes, applicable to more complex data lineage analysis needs, and has a wide range of applications.
[0124] Furthermore, the extraction module 91 is also used to: if the operation type of the target SQL statement is a query operation, determine the target SQL statement as a query statement related to the query operation; if the operation type of the target SQL statement is an insert operation, an update operation or a delete operation, based on a preset query operation keyword, extract the query statement related to the query operation from the target SQL statement.
[0125] Furthermore, the acquisition module 92 is also used to: extract the target data table corresponding to the target column name contained in the query statement from the abstract syntax tree, and the hierarchical relationship between the target data tables; establish a multi-branch tree of the query statement according to the from statement in the query statement; wherein each from statement corresponds to a node in the multi-branch tree; traverse the multi-branch tree according to the target data table and the hierarchical relationship between the target data tables, obtain the target data table associated with the target column name, and the target data column associated with the target column name in the target data table from the multi-branch tree, until reaching the bottom-level data source table.
[0126] Furthermore, the acquisition module 92 is also used to: extract the item entries and the types of item entries contained in the query statement from the abstract syntax tree; for each item entry, extract the data columns involved in the item entry from the item entry in a manner that matches the type of the item entry; determine the data table to which the data columns involved in each item entry belong as the target data table corresponding to the target column name contained in the query statement; determine the hierarchical relationship between the data tables to which the data columns involved in the item entry belong as the hierarchical relationship between the target data tables corresponding to the target column names.
[0127] Furthermore, the acquisition module 92 is further configured to: if the query statement includes a union statement, extract item entries from statements on both sides of the union statement respectively.
[0128] Furthermore, the acquisition module 92 is also used to: establish the initial node of the multi-branch tree and determine the initial node as the current node; obtain the from statement in the query statement from the abstract syntax tree according to the hierarchical structure of the query statement; if the from statement is obtained, determine the from statement as a child node of the current node; and perform subsequent processing on the child node according to the type of statement connected to the from statement.
[0129] Furthermore, the acquisition module 92 is further configured to: if the query statement includes a union statement, use statements on both sides of the union statement as updated query statements, and execute the step of acquiring a from statement in the query statement from the abstract syntax tree.
[0130] Furthermore, the acquisition module 92 is also used for: if the from statement is connected to a data table, extracting the data table and recording the data table in a child node; if the from statement is connected to a join statement, performing subsequent processing on the statements on both sides of the join statement according to the types of the statements on both sides of the join statement; if the from statement is connected to a sub-query statement, determining the child node as an updated current node, determining the sub-query statement as an updated query statement, and continuing to execute the step of obtaining the from statement in the query statement from the abstract syntax tree until the bottom level of the abstract syntax tree is reached.
[0131] Furthermore, the determination module 93 is also used to: obtain the data source information and database information of the target data table; and determine the data lineage analysis result of the target SQL statement according to the target data table, target data column, and the data source information and database information of the target data table.
[0132] Furthermore, the determination module 93 is also used for: if the operation type of the target SQL statement is an insert operation, an update operation or a delete operation, obtaining the data table to be written corresponding to the target column name, and the column name to be written in the data table to be written corresponding to the target column name; obtaining the data source information and database information of the data table to be written; and determining the data lineage analysis result of the target SQL statement according to the target data table, the target data column, and the data source information and database information of the target data table, comprising: determining the target data table, the target data column, the data source information and database information of the target data table, the data table to be written, the column name to be written, and the data source information and database information of the data table to be written as the data lineage analysis result of the target SQL statement.
[0133] Furthermore, the determination module 93 is also used to: obtain the column name to be written in the data table to be written corresponding to the target column name from the target SQL statement; if the column name to be written cannot be obtained from the target SQL statement, obtain all column names of the data table to be written according to the metadata of the data table to be written, and determine all column names as the column names to be written.
[0134] The data lineage analysis device provided in the embodiment of the present invention has the same implementation principle and technical effects as those in the aforementioned method embodiment. For the sake of brief description, for matters not mentioned in the data lineage analysis device embodiment, reference may be made to the corresponding contents in the aforementioned method embodiment.
[0135] The embodiment of the present invention also provides a server for executing a data lineage analysis method; see Fig.10 As shown, it includes a processor 101 and a memory 100. The memory 100 stores machine executable instructions that can be executed by the processor 101. The processor 101 executes the machine executable instructions to implement the data lineage analysis method shown in the above embodiment.
[0136] Further, Fig.10 The server shown also includes a bus 102 and a communication interface 103 , and the processor 101 , the communication interface 103 and the memory 100 are connected via the bus 102 .
[0137] The memory 100 may include a high-speed random access memory (RAM), and may also include a non-volatile memory, such as at least one disk storage. The communication connection between the system network element and at least one other network element is realized through at least one communication interface 103 (which may be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. may be used. The bus 102 may be an ISA bus, a PCI bus, or an EISA bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Fig.10 Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0138] The processor 101 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit in the processor 101 or the instruction in the form of software. The above processor 101 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present invention can be implemented or executed. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc. The steps of the method disclosed in conjunction with the embodiments of the present invention can be directly embodied as a hardware decoding processor for execution, or a combination of hardware and software modules in the decoding processor for execution. The software module may be located in a storage medium mature in the art, such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 100, and the processor 101 reads the information in the memory 100 and completes the steps of the method of the above embodiment in combination with its hardware.
[0139] An embodiment of the present invention also provides a machine-readable storage medium, which stores machine-executable instructions. When the machine-executable instructions are called and executed by a processor, the machine-executable instructions prompt the processor to implement the above-mentioned data lineage analysis method. The specific implementation can be found in the method embodiment, which will not be repeated here.
[0140] The computer program products of the data lineage analysis method, device and server provided in the embodiments of the present invention include a computer-readable storage medium storing program code. The instructions included in the program code can be used to execute the methods described in the previous method embodiments. The specific implementation can be found in the method embodiments, which will not be repeated here.
[0141] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc., which can store program codes.
[0142] Finally, it should be noted that the above embodiments are only specific implementations of the present invention, which are used to illustrate the technical solutions of the present invention, rather than to limit them. The protection scope of the present invention is not limited thereto. Although the present invention is described in detail with reference to the above embodiments, those skilled in the art should understand that any person skilled in the art can still modify the technical solutions recorded in the above embodiments within the technical scope disclosed by the present invention, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention, and should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be based on the protection scope of the claims.
Claims
1. A method for analyzing data lineage, characterized in that: The method comprises: Build an abstract syntax tree for the target SQL statement; Extracting query statements related to the query operation in the target SQL statement based on the abstract syntax tree; According to the target column name contained in the query statement, a target data table associated with the target column name and a target data column associated with the target column name in the target data table are acquired; Determine the data lineage analysis result of the target SQL statement according to the target data table and the target data column; The step of acquiring, according to the target column name contained in the query statement, a target data table associated with the target column name and a target data column associated with the target column name in the target data table comprises: Extracting, from the abstract syntax tree, target data tables corresponding to target column names contained in the query statement, and hierarchical relationships between the target data tables; According to the from statement in the query statement, a multi-branch tree of the query statement is established; wherein each of the from statements corresponds to a node in the multi-branch tree; According to the target data table and the hierarchical relationship between the target data tables, the multi-branch tree is traversed to obtain the target data table associated with the target column name and the target data column associated with the target column name in the target data table from the multi-branch tree until the bottom-level data source table is reached.
2. The method according to claim 1, characterized in that The step of extracting query statements related to the query operation in the target SQL statement includes: If the operation type of the target SQL statement is a query operation, determining the target SQL statement as the query statement related to the query operation; If the operation type of the target SQL statement is an insert operation, an update operation or a delete operation, based on a preset query operation keyword, a query statement related to the query operation is extracted from the target SQL statement.
3. The method according to claim 1, characterized in that The step of extracting, from the abstract syntax tree, target data tables corresponding to target column names contained in the query statement and hierarchical relationships between the target data tables comprises: Extracting item entries and types of item entries contained in the query statement from the abstract syntax tree; For each of the item entries, extract the data columns involved in the item entry in a manner that matches the type of the item entry; Determine the data table to which the data column involved in each item belongs as the target data table corresponding to the target column name contained in the query statement; The hierarchical relationship between the data tables to which the data columns involved in the item entry belong is determined as the hierarchical relationship between the target data tables corresponding to the target column name.
4. The method according to claim 3, characterized in that The step of extracting item entries contained in the query statement includes: if the query statement includes a union statement, extracting item entries from statements on both sides of the union statement respectively.
5. The method according to claim 1, characterized in that The step of establishing a multi-branch tree of the query statement according to the from statement in the query statement comprises: Establishing an initial node of the multi-branch tree, and determining the initial node as the current node; According to the hierarchical structure of the query statement, obtaining a from statement in the query statement from the abstract syntax tree; If a from statement is obtained, the from statement is determined as a child node of the current node; and the child node is subsequently processed according to the type of statement connected to the from statement.
6. The method according to claim 5, characterized in that The step of obtaining a from statement in the query statement from the abstract syntax tree comprises: If the query statement includes a union statement, statements on both sides of the union statement are respectively used as updated query statements, and the step of obtaining a from statement in the query statement from the abstract syntax tree is performed.
7. The method according to claim 5, characterized in that The step of performing subsequent processing on the child node according to the type of the statement connected to the from statement includes: If the from statement is connected to a data table, the data table is extracted and recorded in the child node; If the from statement is connected with a join statement, the statements on both sides of the join statement are subsequently processed respectively according to the types of the statements on both sides of the join statement; If the from statement is connected to a sub-query statement, the child node is determined as the updated current node, the sub-query statement is determined as the updated query statement, and the step of obtaining the from statement in the query statement from the abstract syntax tree is continued until the bottom level of the abstract syntax tree is reached.
8. The method according to claim 1, characterized in that The step of determining the data lineage analysis result of the target SQL statement according to the target data table and the target data column comprises: Obtaining data source information and database information of the target data table; The data lineage analysis result of the target SQL statement is determined according to the target data table, the target data column, and the data source information and database information of the target data table.
9. The method according to claim 8, characterized in that Before the step of determining the data lineage analysis result of the target SQL statement according to the target data table, the target data column, and the data source information and database information of the target data table, the method further includes: If the operation type of the target SQL statement is an insert operation, an update operation or a delete operation, obtaining a data table to be written corresponding to the target column name and a column name to be written in the data table to be written corresponding to the target column name; Obtaining data source information and database information of the data table to be written; The step of determining the data lineage analysis result of the target SQL statement according to the target data table, the target data column, and the data source information and database information of the target data table comprises: The target data table, the target data column, the data source information and database information of the target data table, the data table to be written, the column name to be written, and the data source information and database information of the data table to be written are determined as the data lineage analysis result of the target SQL statement.
10. The method according to claim 9, characterized in that The step of obtaining the column name to be written in the data table to be written corresponding to the target column name comprises: Obtaining, from the target SQL statement, the name of the column to be written in the data table to be written corresponding to the target column name; If the column name to be written cannot be obtained from the target SQL statement, all column names of the data table to be written are obtained according to the metadata of the data table to be written, and all the column names are determined as the column names to be written.
11. A data lineage analysis device, characterized in that: The device comprises: Establishing a module for establishing an abstract syntax tree of a target SQL statement; An extraction module, configured to extract query statements related to the query operation in the target SQL statement based on the abstract syntax tree; An acquisition module, configured to acquire, according to the target column name contained in the query statement, a target data table associated with the target column name and a target data column associated with the target column name in the target data table; A determination module, used to determine the data lineage analysis result of the target SQL statement according to the target data table and the target data column; The acquisition module is also used for: Extracting, from the abstract syntax tree, target data tables corresponding to target column names contained in the query statement, and hierarchical relationships between the target data tables; According to the from statement in the query statement, a multi-branch tree of the query statement is established; wherein each of the from statements corresponds to a node in the multi-branch tree; According to the target data table and the hierarchical relationship between the target data tables, the multi-branch tree is traversed to obtain the target data table associated with the target column name and the target data column associated with the target column name in the target data table from the multi-branch tree until the bottom-level data source table is reached.
12. A server, characterized in that: It comprises a processor and a memory, wherein the memory stores machine executable instructions that can be executed by the processor, and the processor executes the machine executable instructions to implement the data lineage analysis method described in any one of claims 1-10.
13. A machine-readable storage medium, characterized in that: The machine-readable storage medium stores machine-executable instructions. When the machine-executable instructions are called and executed by a processor, the machine-executable instructions prompt the processor to implement the data lineage analysis method described in any one of claims 1-10.
Citation Information
Patent Citations
Data blood relationship construction method and device, server and readable storage medium
CN110908997A
