Storage process code conversion method and device, equipment, medium and program product
By generating and converting the abstract syntax tree of database stored procedure code and using deep learning technology for migration, the problems of low efficiency and insufficient accuracy of database stored procedure code migration in the existing technology are solved, and efficient and accurate stored procedure code migration is achieved.
Patent Information
- Application Number
- CN202411893700.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-20
- Publication Date
- 2025-04-11
AI Technical Summary
The prior art is inefficient and insufficient in the migration process of database stored procedure code, resulting in logical errors or missing functions of the migrated code.
By obtaining the original stored procedure code from the source database, generating the original abstract syntax tree (AST), and inputting it into the object code conversion model, obtaining the target AST, and then generating the target stored procedure code. This method uses deep learning technology to automatically complete the migration of stored procedure codes, reducing manual intervention.
It realizes accurate and efficient migration of database stored procedure code, improves migration efficiency, reduces labor costs, and enhances the functional consistency and accuracy of the migrated code.
Smart Images

Figure CN119807177A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the fields of financial technology and artificial intelligence technology, and in particular to a method, device, equipment, medium and program product for converting stored procedure codes. Background Art
[0002] With the development of science and technology, new database systems such as GaussDB and GoldenDB have been widely used in various fields due to their excellent scalability, performance and cost-effectiveness. Therefore, more and more systems need to migrate databases to migrate existing applications and data from the original database system to the new database system. The database migration process involves data migration, architecture migration, application migration, etc. Among them, the migration of database stored procedure code is the most complex and difficult part.
[0003] In the related art, the migration of database stored procedure codes is usually achieved by manually rewriting the database stored procedure codes or converting the original database stored procedure codes using automated migration tools. However, the manual rewriting method is inefficient and has extremely high labor costs, and the code conversion method using automated migration tools is not accurate enough, resulting in the possibility of logical errors or functional deficiencies in the converted code after migration to the new database system.
[0004] Therefore, how to accurately and efficiently realize the conversion of database stored procedure codes and then realize the migration of database stored procedure codes is a problem that needs to be solved urgently. Summary of the invention
[0005] The present application provides a method, apparatus, device, medium and program product for converting stored procedure code, which can accurately and efficiently realize the conversion of database stored procedure code, thereby improving the migration efficiency of database stored procedure code, while greatly reducing labor costs.
[0006] In a first aspect, an embodiment of the present application provides a method for converting a stored procedure code, the method comprising:
[0007] Obtain the original stored procedure code that needs to be migrated to the target database from the source database;
[0008] Generate an original abstract syntax tree AST based on the original stored procedure code; the original AST is used to represent the syntax structure of the original stored procedure code in the source database;
[0009] The original AST is input into the target code conversion model to obtain the target AST output by the target code conversion model; wherein the target AST is used to represent the grammatical structure of the target stored procedure code in the target database, and the target stored procedure code is equivalent to the original stored procedure code and can run normally in the target database; the target code conversion model is obtained by training the initial code conversion model based on the original stored procedure code sample and the target stored procedure code sample;
[0010] Generate target stored procedure code based on target AST.
[0011] In a second aspect, an embodiment of the present application further provides a device for converting a stored procedure code, the device comprising:
[0012] The code acquisition module is used to obtain the original stored procedure code that needs to be migrated to the target database from the source database;
[0013] An abstract syntax tree generation module is used to generate an original AST based on the original stored procedure code; the original AST is used to represent the syntax structure of the original stored procedure code in the source database;
[0014] A code conversion module is used to input the original AST into the target code conversion model to obtain a target AST output by the target code conversion model; wherein the target AST is used to represent the grammatical structure of the target stored procedure code in the target database, and the target stored procedure code is equivalent to the original stored procedure code and can run normally in the target database; the target code conversion model is obtained by training the initial code conversion model based on the original stored procedure code sample and the target stored procedure code sample;
[0015] The code generation module is used to generate the target stored procedure code based on the target AST.
[0016] In a third aspect, an embodiment of the present invention further provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, a method for converting stored procedure code as described in any one of the embodiments of the present invention is implemented.
[0017] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements a method for converting stored procedure codes as described in any one of the embodiments of the present invention.
[0018] In a fifth aspect, an embodiment of the present invention provides a computer program product, including a computer program, which, when executed by a processor, implements a method for converting stored procedure codes as described in any one of the embodiments of the present invention.
[0019] The embodiment of the present application proposes a method, device, equipment, medium and program product for converting stored procedure code, which obtains the original stored procedure code that needs to be migrated to the target database from the source database; generates the original AST based on the original stored procedure code; the original AST is used to characterize the grammatical structure of the original stored procedure code in the source database; the original AST is input into the target code conversion model to obtain the target AST output by the target code conversion model; wherein the target AST is used to characterize the grammatical structure of the target stored procedure code in the target database, and the target stored procedure code is equivalent to the original stored procedure code and can run normally in the target database; the target code conversion model is obtained by training the initial code conversion model based on the original stored procedure code sample and the target stored procedure code sample; based on the target AST, the target stored procedure code is generated. In the technical solution of the present application, the original AST is constructed through the original stored procedure code, and the original AST is output to the target code conversion model, so that the target code conversion model can understand the complex business logic of the original stored procedure code based on the grammatical structure of the original stored procedure code in the source database, and then automatically generate the target AST that can characterize the grammatical structure of the target stored procedure code in the target database, and the target stored procedure code can be generated based on the target AST. That is to say, through deep learning technology, the original stored procedure code in the source database can be automatically converted into the equivalent target stored procedure code supported by the target database, reducing manual intervention in the code conversion process, significantly improving the efficiency of stored procedure migration, and reducing labor costs; and, based on the original stored procedure code samples and the target stored procedure code samples, the initial code conversion model is trained to obtain the target code conversion model, so that the target code conversion model can fully learn the mapping relationship between the original stored procedure code samples and the target stored procedure code samples, reduce the errors that may occur in the code conversion process, and then accurately convert the original stored procedure code in actual applications, thereby improving the accuracy and functional consistency of database stored procedure code conversion.
[0020] Furthermore, the target code conversion model can gradually adapt to the characteristics of different database systems through continuous training and optimization, and then support the migration of stored procedure codes between multiple database systems to achieve wide versatility. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] Figure 1 A flowchart of a method for converting stored procedure codes provided in one embodiment of the present application;
[0022] Figure 2 A schematic diagram of a process for generating a target AST from a target code conversion model provided in an embodiment of the present application;
[0023] Figure 3 A schematic diagram of a process for training an initial code conversion model provided in an embodiment of the present application;
[0024] Figure 4 A schematic diagram of a process flow of a method for converting stored procedure codes provided in another embodiment of the present application;
[0025] Figure 5 A schematic diagram of the structure of a storage procedure code conversion device provided in one embodiment of the present application;
[0026] Figure 6 A schematic diagram of the structure of an electronic device provided in one embodiment of the present application. DETAILED DESCRIPTION
[0027] The present application will be further described in detail below in conjunction with the accompanying drawings and embodiments. It is to be understood that the specific embodiments described herein are only used to explain the present application, rather than to limit the present application. It should also be noted that, for ease of description, only the parts related to the present application, rather than all structures, are shown in the accompanying drawings.
[0028] In order to facilitate a clearer understanding of the embodiments of the present application, some relevant knowledge is first introduced as follows.
[0029] AST: A data structure that represents the grammatical structure of program code in the form of a tree (including the logical information and structural information of the code), but does not include content that is not related to the specific grammatical details (such as punctuation, blanks, etc.). It decomposes the code into different nodes, each of which represents a grammatical element in the code (such as expressions, statements, operators, function calls, etc.). AST is an important tool for compilers and interpreters to analyze and process code. AST nodes usually include the following types:
[0030] 1. Root Node: It represents the starting point of the entire program or code segment.
[0031] 2. Internal Nodes: represent various grammatical structures, such as conditional statements, loops, function calls, etc.
[0032] 3. Leaf Nodes: represent basic syntax elements, such as variables, constants, and operators.
[0033] With the development of science and technology, more and more systems need to migrate existing applications and data from the original database to a new database system. The database migration process involves multiple aspects such as data migration, architecture migration, and application migration, and the migration of stored procedures is often the most complex and difficult part. This is because the implementation mechanisms of stored procedures in different database systems vary greatly, including differences in programming languages, syntax structures, built-in functions, data types, and transaction processing. This difference makes it impossible to migrate stored procedures simply by copying and pasting or basic code conversion.
[0034] In the related art, the migration of stored procedure code is achieved through the following means:
[0035] 1. Manually rewrite the database stored procedure code.
[0036] Developers manually analyze and understand the logic of the stored procedure in the source database, and manually write new stored procedure code supported by the target database based on the syntax and characteristics of the target database system. Manual rewriting requires developers to have a deep understanding of the characteristics of the source and target databases, and to adapt and test the code line by line.
[0037] 2. Use the code conversion tool based on the rule engine to convert the database stored procedure code.
[0038] Some automated tools, such as the Schema Conversion Tool (SCT) and the data integration platform (InfoSphere DataStage), convert the stored procedure code in the source database into the code of the target database system based on predefined conversion rules. Such tools usually use rule engines to identify common grammatical structures and function calls, and perform corresponding conversions according to the rules. This can handle simple stored procedure conversion tasks, such as data type mapping, replacement of common functions, etc.
[0039] 3. Use the migration tools provided by the database manufacturer to convert the database stored procedure code.
[0040] Some database vendors provide dedicated database migration tools that integrate stored procedure migration functions, automatically analyze stored procedure code, and generate corresponding implementations for the target database system. These tools usually have built-in database-specific conversion rules and can handle most basic stored procedure migration work.
[0041] However, the above-mentioned means have the following defects:
[0042] 1. Manually rewriting database stored procedure code is inefficient.
[0043] Manual migration of stored procedures requires developers to analyze and rewrite the code in detail, which is a huge workload and inefficient. Especially when faced with a large number of stored procedures, manual migration can hardly be completed within an acceptable time. At the same time, manual migration is prone to errors, especially when it involves complex business logic, which can easily lead to wrong changes or omissions. It is difficult to troubleshoot and debug errors, and the migration cost is high.
[0044] 2. Lack of accuracy in using automated code conversion tools.
[0045] Automation tools based on rule engines usually rely on predefined conversion rules, which are not capable of handling complex stored procedure logic and uncommon syntax structures. The conversion results of the tool often need to be manually corrected, making it difficult to achieve full automation. In addition, existing tools lack a deep understanding of the business logic of stored procedures, resulting in logical errors or missing functions in the converted code.
[0046] 3. Migration tools have poor versatility:
[0047] Most of the existing migration tools are aimed at migration between specific databases and lack universality. Migration tools may not be applicable to migration between other database systems, which limits the applicability of migration tools in different projects.
[0048] 4. Automated code conversion tools have difficulty handling complex database feature differences.
[0049] Different database systems have significant differences in transaction processing, exception handling, and concurrency control. Existing automated code conversion tools are difficult to handle these complex features, resulting in the risk of inconsistent functionality in the stored procedures after migration. Existing tools usually only support basic syntax conversions, and lack support for migration tasks that require a deep understanding of database features and business logic.
[0050] In response to the above-mentioned technical problems, the embodiments of the present application provide a method, device, equipment, medium and program product for converting stored procedure code, which can effectively solve the problems in the prior art of low manual migration efficiency, insufficient accuracy of automated code conversion tools, poor versatility of migration tools and difficulty in handling complex database characteristic differences.
[0051] Figure 1 This is a flow chart of a method for converting stored procedure codes provided in an embodiment of the present application. The method can be executed by a stored procedure code conversion device or electronic device. The device or electronic device can be implemented by software and / or hardware. The device or electronic device can be integrated in any intelligent device with network communication function. Figure 1 As shown, the method for converting the stored procedure code may include the following steps:
[0052] S101. Obtain the original stored procedure code that needs to be migrated to the target database from the source database.
[0053] In the embodiment of the present application, the original stored procedure code in the source database needs to be converted to generate a target stored procedure code equivalent to the original stored procedure code. After the target stored procedure code is migrated to the target database, the target stored procedure code can run normally in the target database.
[0054] For example, the original stored procedure code in the relational database management system (Mysql) is converted into the target stored procedure code in GoldenDB, and the target stored procedure code is migrated to GoldenDB. For another example, the original stored procedure code in the relational database management system (oracle) is converted into the target stored procedure code in GaussDB, and the target stored procedure code is migrated to GaussDB.
[0055] S102. Generate an original AST based on the original stored procedure code; the original AST is used to represent the grammatical structure of the original stored procedure code in the source database.
[0056] In the embodiment of the present application, the grammatical structure includes the code logic information and code structure information of the original stored procedure code in the source database. Based on the structure information, the target code conversion model can fully understand the content of the original stored procedure code and the logical relationship between the codes.
[0057] In practical applications, the original stored procedure code is converted into the original AST tree structure through syntax parsing tools, such as Another Tool for Language Recognition (ANTLR) and SQL Parser, which can better represent the code structure and logic, so that the subsequent target code conversion model can be based on the syntax structure of the original stored procedure code in the original AST, making the analysis and processing of the original stored procedure code easier and more efficient.
[0058] S103. Input the original AST into the target code conversion model to obtain the target AST output by the target code conversion model; wherein the target AST is used to represent the grammatical structure of the target stored procedure code in the target database, and the target stored procedure code is equivalent to the original stored procedure code and can run normally in the target database; the target code conversion model is obtained by training the initial code conversion model based on the original stored procedure code samples and the target stored procedure code samples.
[0059] In the embodiment of the present application, the target code conversion model can be a transformer model (Transformer), a bidirectional transformer model (Bidirectional Encoder Representations from Transformers, referred to as BERT) and a generative pre-trained transformer model (Generative Pre-trained Transformer, referred to as GPT), etc. The following takes the target code conversion model as a Transformer model as an example to describe the relevant steps involved in detail.
[0060] In the process of processing data by the target code conversion model, the model automatically completes operations such as adjusting the grammatical structure, replacing function calls, and mapping data types. The embodiment of the present application aims to automatically convert the stored procedures in the source database (such as Oracle) into equivalent codes supported by the target database (such as GaussDB) through deep learning technology, thereby achieving efficient and accurate stored procedure migration. In addition, the embodiment of the present application can not only handle common grammatical conversions and function replacements, but also automatically identify and understand complex business logic through deep learning models, and accurately map and convert between different databases.
[0061] By converting the original AST into the target AST through the target code conversion model, manual intervention can be reduced during the migration of stored procedure code, significantly improving the efficiency and accuracy of stored procedure migration. In addition, the deep learning model can gradually adapt to the characteristics of different database systems through continuous learning and optimization, achieving wide versatility.
[0062] S104. Generate target stored procedure code based on the target AST.
[0063] In the embodiment of the present invention, after obtaining the target AST, it is necessary to use a corresponding inverse parser (such as Python's own tool ast.unparse) to parse the target AST into a target stored procedure code.
[0064] The embodiment of the present application proposes a method for converting stored procedure code, which obtains the original stored procedure code that needs to be migrated to the target database from the source database; generates the original AST based on the original stored procedure code; the original AST is used to characterize the grammatical structure of the original stored procedure code in the source database; the original AST is input into the target code conversion model to obtain the target AST output by the target code conversion model; wherein the target AST is used to characterize the grammatical structure of the target stored procedure code in the target database, and the target stored procedure code is equivalent to the original stored procedure code and can run normally in the target database; the target code conversion model is obtained by training the initial code conversion model based on the original stored procedure code sample and the target stored procedure code sample; based on the target AST, the target stored procedure code is generated. In the technical solution of the present application, the original AST is constructed through the original stored procedure code, and the original AST is output to the target code conversion model, so that the target code conversion model can understand the complex business logic of the original stored procedure code based on the grammatical structure of the original stored procedure code in the source database, and then automatically generate the target AST that can characterize the grammatical structure of the target stored procedure code in the target database, and the target stored procedure code can be generated based on the target AST. That is to say, through deep learning technology, the original stored procedure code in the source database can be automatically converted into the equivalent target stored procedure code supported by the target database, reducing manual intervention in the code conversion process, significantly improving the efficiency of stored procedure migration, and reducing labor costs; and, based on the original stored procedure code samples and the target stored procedure code samples, the initial code conversion model is trained to obtain the target code conversion model, so that the target code conversion model can fully learn the mapping relationship between the original stored procedure code samples and the target stored procedure code samples, reduce the errors that may occur in the code conversion process, and then accurately convert the original stored procedure code in actual applications, thereby improving the accuracy and functional consistency of database stored procedure code conversion.
[0065] Furthermore, the target code conversion model can gradually adapt to the characteristics of different database systems through continuous training and optimization, and then support the migration of stored procedure codes between multiple database systems to achieve wide versatility.
[0066] In some embodiments of the present application, after generating the target stored procedure code, the following steps need to be performed:
[0067] Step 1) determines whether the target stored procedure code meets the migration conditions; the migration conditions include that the target stored procedure code is equivalent to the original stored procedure code, and the target stored procedure code can run normally in the target database.
[0068] Step 2), when the target stored procedure code meets the migration conditions, migrate the target stored procedure code to the target database.
[0069] In the above implementation, after the target stored procedure code is generated, the code needs to be subjected to syntax checking and unit testing to ensure that it can run normally in the target database and achieve the same functions as the source database. For the part that fails the test, the target code conversion model will adjust according to the error information and generate a new code version.
[0070] In the above implementation, by verifying the target stored procedure code, the accuracy of the generated target stored procedure code is ensured, thereby improving the migration efficiency of the target stored procedure code.
[0071] In some embodiments of the present application, the original AST is generated based on the original stored procedure code, which can be specifically implemented in the following ways:
[0072] Step 1) performs lexical analysis on the original stored procedure code to generate a token list corresponding to the original stored procedure code. The token list includes multiple tokens, and each token represents a minimum grammatical unit in the original stored procedure code.
[0073] In the embodiment of the present application, the lexical analysis of the original stored procedure code is a process of converting the original stored procedure code into a token list using a lexical analyzer. It should be noted that each token represents the smallest grammatical unit in the original stored procedure code, such as a keyword, identifier, operation algorithm, separator, etc.
[0074] Each token has a type and a value. For example, if the keyword "SELECT" is input, the token output after lexical analysis is recognized as "Keyword" type, and the corresponding value is "SELECT".
[0075] Step 2) Based on the predefined grammar rules, perform grammatical analysis (SyntaxAnalysis) on each token in the token list to generate the original AST.
[0076] In an embodiment of the present application, a token list generated by a lexical analyzer is taken as input, and an original AST is generated according to predefined grammar rules of a programming language to represent the hierarchical structure of the original stored procedure code.
[0077] For example, an "IF" statement will be parsed into an "IFStatement" node, which contains a conditional expression and a branch code block.
[0078] The following example illustrates the process of parsing the original stored procedure code into the original AST:
[0079] For example, the original stored procedure code is:
[0080]
[0081] ├──ConditionNode(IF)
[0082] │ ├──ExpressionNode(v_result IS NULL)
[0083] │ └──AssignmentNode(v_result:=0)
[0084] └──EndNode
[0085] In the above implementation, by performing lexical analysis and syntactic analysis on the original stored procedure code, the original stored procedure code is converted into an original AST tree structure, which can better represent the code structure and logic, so that the subsequent target code conversion model can be based on the grammatical structure of the original stored procedure code in the original AST, making the analysis and processing of the original stored procedure code easier and more efficient.
[0086] In some embodiments of the present application, the original AST includes multiple original nodes, each of which represents a grammatical structure of the original stored procedure code in the source database; the target AST includes multiple target nodes, each of which represents a grammatical structure of the target stored procedure code in the target database.
[0087] Input the original AST into the target code conversion model to obtain the target AST output by the target code conversion model. This can be achieved by the following steps:
[0088] Step 1) Input each original node into the encoder in the target code conversion model to obtain the target encoding vector corresponding to each original node output by the encoder; the target encoding vector includes the semantic encoding vector of the original node and the target position encoding vector in the original AST.
[0089] In the embodiment of the present application, according to the characteristics of the original stored procedure code, the Transformer model is used as the target code conversion model because the Transformer model can capture long-distance dependencies in the code and can more accurately map complex code structures.
[0090] The architecture of the Transformer model consists of two main parts: the encoder and the decoder. The encoder converts the original AST into a hidden state, and the decoder generates a target AST based on the hidden state, and then generates the target stored procedure code based on the target AST.
[0091] Step 2) Input each target coding vector into the decoder in the target code conversion model, so that the decoder converts each original node into a corresponding target node based on the semantic coding vector corresponding to each original node and the target position coding vector, and generates a target AST based on each target node.
[0092] In an embodiment of the present application, the decoder receives each target encoding vector output by the encoder, and gradually generates the target node corresponding to the original stored procedure code in the target AST. Each time a target node is generated, the information of the target node is fed back to the decoder to help it generate the next target node. Finally, the target AST that needs to be converted is generated.
[0093] Step 3) Receive the target AST output by the decoder.
[0094] It should be noted that after the decoder generates the target AST, the corresponding inverse parser needs to be used to process the target AST to obtain the final target stored procedure code.
[0095] In the above implementation, for each original node, the encoder is used to encode the original node, and a target encoding vector including a semantic encoding vector and a target position encoding vector can be obtained, so that the target code conversion model can use the target encoding vector to accurately understand the content of each original node and its position in the code logic, so as to more accurately perform subsequent decoding tasks, thereby improving the accuracy of converting the target stored procedure code.
[0096] In some embodiments of the present application, in order for the target code conversion model to understand and learn the input original AST, it is necessary to convert the original nodes and structural information in the original AST into a high-dimensional feature vector that can be processed by the target code conversion model, that is, the target encoding vector mentioned above.
[0097] For each original node, the target encoding vector is determined as follows:
[0098] Step 1) Input the original node into the pre-trained word vector model to obtain the semantic encoding vector output by the word vector model.
[0099] In the embodiment of the present application, step 1) is node embedding, and the word vector model is the Word2Vec model. First, since the nodes (tokens) in the original AST are all natural languages (such as variables, operators, etc.), it is necessary to use a pre-trained Word2Vec model to learn the similarities between closely related words or symbols based on a large-scale corpus, and generate corresponding digital vectors for the English words of the token, which are also called embedding vectors, word vectors (Node Embedding) or semantic encoding vectors.
[0100] Step 2) Based on the depth parameter, order parameter and predefined dimension parameter corresponding to the original node, determine the depth position encoding vector and order position encoding vector corresponding to the original node; the dimension parameter is used to characterize the relationship between the current original node and other original nodes in the original AST.
[0101] In the embodiment of the present application, step 2) is structure embedding, that is, for the original AST tree structure, the relative position and hierarchical relationship between each original node is converted into a vector representation (ie, a depth position encoding vector and a sequence position encoding vector).
[0102] It should be noted that the dimensionality parameter is a predefined hyperparameter. The larger the dimensionality parameter is defined, the more it can fully express the relationship between the current original node and other original nodes. However, it will also bring problems such as noise and computational complexity. Therefore, the specific selection of the dimensionality parameter needs to be determined based on the complexity of the task. In the process of training the initial code conversion model, the dimensionality parameter needs to be continuously adjusted to find the value that makes the initial code conversion model training effect the best. In practical applications, due to the small number of keywords in the stored procedure, it is recommended to start training from dimensionality=256, and then make further adjustments based on factors such as training results and training data size.
[0103] The embodiment of the present application calculates a depth position encoding vector and a sequence position encoding vector based on position encoding of sine and cosine functions.
[0104] First, for each original node, the depth parameter and the order parameter are used as its unique position identifier in the original AST. The depth position encoding vector and the order position encoding vector can be calculated by the following formulas (1) and (2), where formula (1) is the sine part and formula (2) is the cosine part:
[0105]
[0106] Among them, P (pos,2i) represents the sinusoidal part of the depth position encoding vector or the sequential position encoding vector, P(pos,2i+1) Represents the cosine part of the deep position encoding vector or the sequential position encoding vector, pos represents the depth parameter or the sequence parameter, i is the index of the dimensionality parameter, from 0 to (dimensionality / 2)-1, 2i represents the even dimensionality parameter (sine part), 2i+1 represents the odd dimensionality parameter (cosine part), and each pair of (2i, 2i+1) dimensionality parameters corresponds to a complete sine and cosine cycle. This representation is periodic and smooth, which is conducive to the model learning the position information of the vector.
[0107] The following takes the original node ExpressionNode as an example. The depth of ExpressionNode is 2 and the order is 0.
[0108] Assuming dimensionality = 4, the depth position encoding vector of the original node ExpressionNode can be expressed by the following formulas (3)-(6):
[0109] For 2i=0, that is, the calculation of the 0th dimension:
[0110]
[0111] For 2i+1=1, that is, the calculation of the first dimension:
[0112]
[0113] For 2i=2, that is, the calculation of the second dimension:
[0114]
[0115] For 2i+1=3, that is, the calculation of the third dimension:
[0116]
[0117] Based on the above formulas (3)-(6), the depth position encoding vector of the original node ExpressionNode can be expressed as P depth =[0.9093,0.9801,0.02,0.999998].
[0118] The sequential position encoding vector of the above original node ExpressionNode can be expressed by the following formulas (7)-(10):
[0119] For 2i=0, that is, the calculation of the 0th dimension:
[0120]
[0121] For 2i+1=1, that is, the calculation of the first dimension:
[0122]
[0123] For 2i=2, that is, the calculation of the second dimension:
[0124]
[0125] For 2i+1=3, that is, the calculation of the third dimension:
[0126]
[0127] Based on the above formulas (7)-(10), the sequential position encoding vector of the original node ExpressionNode can be expressed as P pos =[0,1,0,1].
[0128] Step 3) Determine the target position encoding vector based on the depth position encoding vector and the sequential position encoding vector.
[0129] In the embodiment of the present application, the depth position encoding vector is combined with the sequence position encoding vector to obtain the final target position encoding vector.
[0130] Assume that the depth position encoding vector of the original node ExpressionNode is combined with the sequential position encoding vector, which can be expressed as P depth +P pos =[0.9093+0,0.9801+1,0.02+0,0.999998+1].
[0131] Then the target position encoding vector of the original node ExpressionNode is expressed as: P = [0.9093, 1.9801, 0.02, 1.999998].
[0132] In the above embodiment, the generated depth position encoding vector and the sequential position encoding vector are periodic and distinguishable. No matter whether the values of the depth position encoding vector and the sequential position encoding vector are larger or smaller, the encoder can still maintain sufficient difference to distinguish different positions.
[0133] Step 4) Determine the target coding vector based on the semantic coding vector and the target position coding vector.
[0134] In the embodiment of the present application, the target coding vector (ie, the embedded vector with position coding) can be obtained by concatenating or adding the semantic coding vector and the target position coding vector.
[0135] In the above implementation, the target encoding vector is input into the rest of the encoder so that the model can use this information to accurately understand the content of each node and its position in the code logic, thereby performing the decoding task more accurately.
[0136] In practical applications, the code for determining the target encoding vector can be expressed as follows:
[0137]
[0138]
[0139] Figure 2 A schematic diagram of a process for generating a target AST from a target code conversion model provided in an embodiment of the present application. Figure 2 As shown, first, the original AST corresponding to the original stored procedure code in the source database is input into the encoder, and the encoder performs node embedding and structure embedding on each original node of the original AST, and then obtains the final encoding result (i.e., the target encoding vector) through a multi-head self-attention mechanism and a feedforward neural network.
[0140] After receiving the encoding result output by the encoder, the decoder uses the multi-head attention mechanism and feedforward neural network to obtain the target AST corresponding to the target database. Finally, the target AST is processed to obtain the target stored procedure code. For each input of the Transformer model, the multi-head self-attention mechanism of the Transformer model weighs the relevance of each other input and extracts information from it to generate output.
[0141] In some embodiments of the present application, the target code conversion model is trained in the following way:
[0142] Step 1) preprocessing the original stored procedure code sample and the target stored procedure code sample.
[0143] First, you need to obtain the original stored procedure code sample and the target stored procedure code sample.
[0144] Specifically, from the stored procedures that have completed migration, the specific codes of the same stored procedure in the source database and the target database are selected as a set of sample data as the original stored procedure code sample and the target stored procedure code sample, and are labeled to clearly identify the source database and the target database types.
[0145] In actual applications, stored procedure code samples cover common database operations, business logic, exception handling, and data types. During training, a single model is used for training. In this way, the initial code conversion model can learn specific conversion rules between different databases, thereby training a universal target code conversion model.
[0146] In order for the initial code conversion model to effectively learn the mapping relationship between the original stored procedure code samples and the target stored procedure code samples, data preprocessing is required. The preprocessing steps include removing comments, unifying the format, and standardizing the syntax to ensure the consistency and quality of the data.
[0147] Step 2), based on the preprocessed original stored procedure code sample, generate an original AST sample; based on the preprocessed target stored procedure code sample, generate a target AST sample; the original AST sample is used to characterize the grammatical structure of the original stored procedure code sample in the source database, and the target AST sample is used to characterize the grammatical structure of the target stored procedure code sample in the target database.
[0148] Step 3) Input the original AST sample into the initial code conversion model for training, and adjust the model parameters of the initial code conversion model based on the target AST sample during the training process, so that the initial code conversion model learns the mapping relationship between the original AST sample and the target AST sample, until the initial code conversion model converges to obtain the target code conversion model.
[0149] In the embodiment set of the present application, in the process of training the initial code conversion model, in order to improve the generalization ability of the model, data enhancement technology can be used, for example, by inserting different code snippets, modifying parameter types, changing grammatical structures, etc., to increase the diversity of training data.
[0150] Furthermore, the cross entropy loss function is used to optimize the parameters of the initial code conversion model. Through the back propagation algorithm and gradient descent method, the model is continuously optimized to better adapt to the storage procedure migration of different database systems.
[0151] Furthermore, in order to continuously improve the performance of the target code conversion model, this application introduces a feedback mechanism. During the migration of the target stored procedure code, any code conversion errors or functional inconsistencies will be fed back to the target code conversion model. Through continuous training and optimization, the target code conversion model will gradually improve its accuracy. This can be achieved in the following ways:
[0152] 1. Automatic feedback: The system automatically executes test cases after migration, captures any errors or functional inconsistencies, and feeds them back to the model as new training data for learning and optimization.
[0153] 2. Manual feedback: Users can manually intervene and modify the migration results through the system's visual interface. The modified code and adjustment content will be fed back to the model to further optimize the model's conversion capabilities.
[0154] Furthermore, the present application also provides a user interaction interface for displaying the migration results of the target stored procedure code and providing interactive functions. Users can view the differences between the stored procedure codes of the source database and the target database through the code comparison view of the interface, intuitively view the effect of the migration, and manually correct or confirm the migration results. In addition, the system generates a migration log, records the key steps and possible problems in the migration process, and provides users with detailed migration logs and error reports to help users understand the overall situation of the migration, quickly locate problems, and further analyze and optimize the migration results.
[0155] Figure 3 The following is a flow chart of training the initial code conversion model provided by an embodiment of the present application. Figure 3 As shown, the training process includes:
[0156] First, data collection and cleaning are performed.
[0157] Then, the source code (ie, the original stored procedure code sample) is preprocessed into an AST tree (ie, the original AST sample), and feature extraction is performed on the AST tree.
[0158] The neural network is trained (i.e., the initial code conversion model is trained using the extracted features) to obtain a database migration model (i.e., the target code conversion model). During the training process, any code conversion errors or functional inconsistencies can be automatically or manually fed back.
[0159] In the actual use stage, the source database stored procedure source code (i.e. the original stored procedure code) is input into the database migration model to obtain the target database stored procedure equivalent code (i.e. the target stored procedure code). The AST conversion process and the code conversion and migration process can be viewed through the visual interface.
[0160] Figure 4 A flowchart of a method for converting stored procedure code provided in another embodiment of the present application.
[0161] like Figure 4 As shown, the method comprises the following steps:
[0162] S401. Obtain the original stored procedure code that needs to be migrated to the target database from the source database.
[0163] S402: Perform lexical analysis on the original stored procedure code to generate a token list corresponding to the original stored procedure code.
[0164] S403: Based on predefined grammar rules, perform grammar analysis on each token in the token list to generate an original AST.
[0165] S404. Input each original node in the original AST into the encoder in the target code conversion model to obtain the target coding vector corresponding to each original node output by the encoder; the target coding vector includes the semantic coding vector of the original node and the target position coding vector in the original AST.
[0166] S405. Input each target coding vector into the decoder in the target code conversion model, so that the decoder converts each original node into a corresponding target node based on the semantic coding vector and the target position coding vector corresponding to each original node, and generates a target AST based on each target node.
[0167] S406: Receive the target AST output by the decoder.
[0168] S407: Generate target stored procedure code based on the target AST.
[0169] S408. Determine whether the target stored procedure code meets the migration conditions; the migration conditions include that the target stored procedure code is equivalent to the original stored procedure code, and the target stored procedure code can run normally in the target database.
[0170] S409: When the target stored procedure code meets the migration condition, migrate the target stored procedure code to the target database.
[0171] Based on the above embodiments, the stored procedure code conversion method provided in the embodiments of the present application can achieve the following effects:
[0172] 1. Improve migration efficiency and reduce manual intervention.
[0173] The embodiment of the present application utilizes a deep learning model to automatically complete the migration of stored procedure code, and no longer relies on cumbersome manual rewriting process.
[0174] Compared with the traditional manual migration method, the embodiment of the present application significantly improves the migration efficiency. Through the deep learning model, the conversion task of the stored procedure code can be processed quickly and in batches, reducing manual intervention and time investment. Especially when processing large-scale stored procedure migration, the automated processing method can greatly save time and labor costs.
[0175] 2. Improve migration accuracy and reduce errors.
[0176] The embodiments of the present application achieve accurate conversion of complex stored procedure logic through training and optimization of deep learning models, and can better handle the syntax and functional differences between different database systems.
[0177] Compared with existing rule engine-based tools, the deep learning model in the embodiment of the present application can more accurately handle complex business logic and database characteristics during the conversion process. By learning from a large amount of sample data, the model can identify and adapt to the subtle differences between different database systems, thereby reducing possible errors during the conversion process and ensuring the functional consistency and correctness of the migrated code.
[0178] 3. Strong adaptability and versatility, supporting multiple database systems.
[0179] The embodiment of the present application discovers the universal characteristics of the stored procedure by converting the stored procedure code into an AST tree and training a data-driven deep learning model, rather than relying on conversion rules between specific databases, so that it has wide versatility.
[0180] The deep learning model of the embodiment of the present application can be adaptively trained according to different source databases and target databases, supporting the migration of stored procedures between multiple database systems. This versatility can extend the embodiment of the present application to migration tasks between other database systems, providing a more flexible solution.
[0181] 4. Implement a feedback mechanism to continuously optimize migration results.
[0182] The embodiment of the present application introduces an automated and manual feedback mechanism to gradually improve the conversion capability of the model through feedback on migration results and model optimization.
[0183] Compared with the existing tools that generate conversion results in one go, the embodiments of the present application continuously optimize the model through a feedback mechanism, and can dynamically adjust and improve the migration effect. Errors or anomalies encountered during the migration process can flow back into the model through the feedback mechanism, prompting the model to adjust itself, so that it can perform more accurately and reliably in subsequent migration tasks.
[0184] Figure 5 A schematic diagram of the structure of a conversion device for a stored procedure code provided in an embodiment of the present application. Figure 5 As shown, the conversion device of the stored procedure code includes:
[0185] The code acquisition module 501 is used to acquire the original stored procedure code that needs to be migrated to the target database from the source database;
[0186] Abstract syntax tree generation module 502, used to generate original AST based on original stored procedure code; original AST is used to represent the syntax structure of the original stored procedure code in the source database;
[0187] The code conversion module 503 is used to input the original AST into the target code conversion model to obtain the target AST output by the target code conversion model; wherein the target AST is used to represent the grammatical structure of the target stored procedure code in the target database, and the target stored procedure code is equivalent to the original stored procedure code and can run normally in the target database; the target code conversion model is obtained by training the initial code conversion model based on the original stored procedure code sample and the target stored procedure code sample;
[0188] The code generation module 504 is used to generate target stored procedure code based on the target AST.
[0189] The above-mentioned stored procedure code conversion device can execute the method provided by any embodiment of the present application, and has the corresponding functional modules and beneficial effects of the execution method. For technical details not described in detail in this embodiment, please refer to the stored procedure code conversion method provided by any embodiment of the present application.
[0190] An embodiment of the present invention also provides a computer program product.
[0191] Various embodiments of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments can include: being implemented in one or more computer program products, which can include one or more computer programs, which can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor, which can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0192] Figure 6 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention, referring to Figure 6 , Figure 6 The electronic device 12 shown is only an example and should not limit the functions and scope of use of the embodiments of the present application. Figure 6 As shown, the electronic device 12 is in the form of a general purpose computing device. The components of the electronic device 12 may include, but are not limited to: one or more processors or processing units 16, a system memory 28, and a bus 18 that connects various system components (including the system memory 28 and the processing unit 16).
[0193] Bus 18 represents one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a processor or a local bus using any of a variety of bus architectures. By way of example, these architectures include, but are not limited to, an Industry Standard Architecture (ISA) bus, a Micro Channel Architecture (MAC) bus, an Enhanced ISA bus, a Video Electronics Standards Association (VESA) local bus, and a Peripheral Component Interconnect (PCI) bus.
[0194] The electronic device 12 typically includes a variety of computer system readable media. These media can be any available media that can be accessed by the electronic device 12, including volatile and non-volatile media, removable and non-removable media.
[0195] The system memory 28 may include computer system readable media in the form of volatile memory, such as random access memory (RAM) 30 and / or cache memory 32. The electronic device 12 may further include other removable / non-removable, volatile / non-volatile computer system storage media. By way of example only, the storage system 34 may be used to read and write non-removable, non-volatile magnetic media ( Figure 6 not shown, usually called a "hard drive"). Although Figure 6 Not shown, a disk drive for reading and writing to a removable non-volatile disk (e.g., a "floppy disk"), and an optical drive for reading and writing to a removable non-volatile optical disk (e.g., a CD-ROM, a DVD-ROM, or other optical medium) may be provided. In these cases, each drive may be connected to the bus 18 via one or more data medium interfaces. The memory 28 may include at least one program product having a set (e.g., at least one) of program modules that are configured to perform the functions of the various embodiments of the present application.
[0196] A program / utility 40 having a set (at least one) of program modules 46 may be stored, for example, in the memory 28, such program modules 46 including, but not limited to, an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment. The program modules 46 generally perform the functions and / or methods of the embodiments described herein.
[0197] The electronic device 12 may also communicate with one or more external devices 14 (e.g., keyboards, pointing devices, displays 24, etc.), may communicate with one or more devices that enable a user to interact with the electronic device 12, and / or may communicate with any device that enables the electronic device 12 to communicate with one or more other computing devices (e.g., a network card, a modem, etc.). Such communication may be performed via an input / output (I / O) interface 22. Furthermore, the electronic device 12 may also communicate with one or more networks (e.g., a local area network (LAN), a wide area network (WAN), and / or a public network, such as the Internet) via a network adapter 20. As shown, the network adapter 20 communicates with other modules of the electronic device 12 via the bus 18. It should be understood that although Figure 6 Not shown, other hardware and / or software modules may be used in conjunction with the electronic device 12, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0198] The processing unit 16 executes various functional applications and data processing by running the program stored in the system memory 28, for example, a method for converting a stored procedure code provided in an embodiment of the present invention is implemented: the original stored procedure code to be migrated to the target database is obtained from the source database; based on the original stored procedure code, an original AST is generated; the original AST is used to characterize the grammatical structure of the original stored procedure code in the source database; the original AST is input into the target code conversion model to obtain the target AST output by the target code conversion model; wherein the target AST is used to characterize the grammatical structure of the target stored procedure code in the target database, and the target stored procedure code is equivalent to the original stored procedure code and can run normally in the target database; the target code conversion model is obtained by training the initial code conversion model based on the original stored procedure code sample and the target stored procedure code sample; based on the target AST, the target stored procedure code is generated.
[0199] The embodiment of the present invention provides a computer-readable storage medium, on which a computer program is stored. When the program is executed by a processor, a method for converting a stored procedure code as provided in all the embodiments of the present invention is implemented: obtaining the original stored procedure code to be migrated to the target database from the source database; generating an original AST based on the original stored procedure code; the original AST is used to characterize the grammatical structure of the original stored procedure code in the source database; inputting the original AST into a target code conversion model to obtain a target AST output by the target code conversion model; wherein the target AST is used to characterize the grammatical structure of the target stored procedure code in the target database, and the target stored procedure code is equivalent to the original stored procedure code and can run normally in the target database; the target code conversion model is obtained by training the initial code conversion model based on the original stored procedure code sample and the target stored procedure code sample; generating the target stored procedure code based on the target AST. The computer-readable medium may be a computer-readable signal medium or a computer-readable storage medium. The computer-readable storage medium may be, for example, but not limited to, an electronic device, device or device of electrical, magnetic, optical, electromagnetic, infrared, or semiconductor, or any combination thereof. More specific examples of computer-readable storage media (a non-exhaustive list) include: an electrical connection with one or more conductors, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this document, a computer-readable storage medium can be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction-executing electronic device, apparatus, or device.
[0200] Computer-readable signal media may include data signals propagated in baseband or as part of a carrier wave, which carry computer-readable program code. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than a computer-readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction-executing electronic device, apparatus, or device.
[0201] The program code embodied on the computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
[0202] Computer program code for performing the operations of the present invention may be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0203] Note that the above are only preferred embodiments of the present invention and the technical principles used. Those skilled in the art will understand that the present invention is not limited to the specific embodiments herein, and that various obvious changes, readjustments and substitutions can be made by those skilled in the art without departing from the scope of protection of the present invention. Therefore, although the present invention has been described in more detail through the above embodiments, the present invention is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of the present invention, and the scope of the present invention is determined by the scope of the appended claims.
Claims
1. A method for converting stored procedure code, characterized in that: The method comprises: Obtain the original stored procedure code that needs to be migrated to the target database from the source database; Based on the original stored procedure code, an original abstract syntax tree AST is generated; the original AST is used to represent the syntax structure of the original stored procedure code in the source database; The original AST is input into the target code conversion model to obtain the target AST output by the target code conversion model; wherein the target AST is used to characterize the grammatical structure of the target stored procedure code in the target database, and the target stored procedure code is equivalent to the original stored procedure code and can run normally in the target database; the target code conversion model is obtained by training the initial code conversion model based on the original stored procedure code sample and the target stored procedure code sample; Based on the target AST, the target stored procedure code is generated.
2. The method according to claim 1, characterized in that The generating of the original AST based on the original stored procedure code includes: Performing lexical analysis on the original stored procedure code to generate a token list corresponding to the original stored procedure code, wherein the token list includes a plurality of tokens, each token representing a minimum grammatical unit in the original stored procedure code; Based on predefined grammatical rules, each token in the token list is subjected to grammatical analysis to generate the original AST.
3. The method according to claim 1 or 2, characterized in that: The original AST includes a plurality of original nodes, each of which represents a grammatical structure of the original stored procedure code in the source database; The target AST includes a plurality of target nodes, each of which represents a grammatical structure of the target stored procedure code in the target database; The step of inputting the original AST into a target code conversion model to obtain a target AST output by the target code conversion model includes: Input each of the original nodes into the encoder in the target code conversion model to obtain a target encoding vector corresponding to each of the original nodes output by the encoder; the target encoding vector includes a semantic encoding vector of the original node and a target position encoding vector in the original AST; Input each of the target encoding vectors into a decoder in the target code conversion model, so that the decoder converts each of the original nodes into corresponding target nodes based on the semantic encoding vector corresponding to each of the original nodes and the target position encoding vector, and generates the target AST based on each of the target nodes; The target AST output by the decoder is received.
4. The method according to claim 3, characterized in that For each original node, the target encoding vector is determined as follows: Inputting the original node into a pre-trained word vector model to obtain the semantic encoding vector output by the word vector model; Determine the depth position encoding vector and the sequence position encoding vector corresponding to the original node based on the depth parameter, sequence parameter and predefined dimension parameter corresponding to the original node; the dimension parameter is used to characterize the relationship between the current original node and other original nodes in the original AST; Determining the target position encoding vector based on the depth position encoding vector and the sequential position encoding vector; Based on the semantic coding vector and the target position coding vector, the target coding vector is determined.
5. The method according to any one of claims 1, 2 or 4, characterized in that: After generating the target stored procedure code, the method further includes: Determine whether the target stored procedure code meets the migration condition; the migration condition includes that the target stored procedure code is equivalent to the original stored procedure code, and the target stored procedure code can run normally in the target database; When the target stored procedure code meets the migration condition, the target stored procedure code is migrated to the target database.
6. The method according to any one of claims 1, 2 or 4, characterized in that: The target code conversion model is trained in the following way: Preprocessing the original stored procedure code sample and the target stored procedure code sample; Generate an original AST sample based on the preprocessed original stored procedure code sample; Generate a target AST sample based on the preprocessed target stored procedure code sample; The original AST sample is used to represent the grammatical structure of the original stored procedure code sample in the source database, and the target AST sample is used to represent the grammatical structure of the target stored procedure code sample in the target database; The original AST sample is input into the initial code conversion model for training, and during the training process, the model parameters of the initial code conversion model are adjusted based on the target AST sample so that the initial code conversion model learns the mapping relationship between the original AST sample and the target AST sample until the initial code conversion model converges to obtain the target code conversion model.
7. A conversion device for stored procedure code, characterized in that: The device comprises: The code acquisition module is used to obtain the original stored procedure code that needs to be migrated to the target database from the source database; An abstract syntax tree generation module is used to generate an original abstract syntax tree AST based on the original stored procedure code; the original AST is used to represent the syntax structure of the original stored procedure code in the source database; A code conversion module, used for inputting the original AST into a target code conversion model to obtain a target AST output by the target code conversion model; wherein the target AST is used to characterize the grammatical structure of the target stored procedure code in the target database, and the target stored procedure code is equivalent to the original stored procedure code and can run normally in the target database; the target code conversion model is obtained by training an initial code conversion model based on the original stored procedure code sample and the target stored procedure code sample; A code generation module is used to generate the target stored procedure code based on the target AST.
8. An electronic device, characterized in that: include: one or more processors; a memory for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the stored procedure code conversion method according to any one of claims 1 to 6.
9. A storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the stored procedure code conversion method according to any one of claims 1 to 6 is implemented.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the computer program implements the method for converting stored procedure codes according to any one of claims 1 to 6.