Natural language conversion to SQL for complex scenarios
An example operation includes one or more of receiving a natural language input via a software application, executing a machine learning model on the natural language input to determine a plurality of sub-queries corresponding to the natural language input, searching a tree structure for field values that correspond to the plurality of sub-queries, relationships between the field values, and computations to be performed, generating a structured query language (SQL) command based on the field values, relationships between the field values, and the computations to be performed, and executing the SQL command on a database to generate query results and return the query results to the software application.
1 . A computer-implemented method comprising:
receiving a natural language input via a software application;
executing a machine learning model on the natural language input to determine a plurality of sub-queries corresponding to the natural language input;
searching a tree structure for field values that correspond to the plurality of sub-queries and relationships between the field values, wherein the searching comprises searching a plurality of layers of the tree structure and identifying a plurality of query attributes for a structured query language (SQL) command, respectively, and executing a Monte Carlo Tree Search (MCTS) on the tree structure starting from a root node of the tree structure to decide a path in the tree structure which includes a child node that contains a greatest upper confidence bound;
generating the SQL command based on the field values and relationships between the field values, wherein the generating comprises generating the SQL command to include the plurality of query attributes; and
executing the SQL command on a database to generate query results and return the query results to the software application.
2 . The computer-implemented method of claim 1 , wherein the executing the machine learning model comprises determining hidden query content from the natural language input based on execution of the machine learning model and generating a sub-query which includes the hidden query content.
3 . The computer-implemented method of claim 1 , wherein the searching comprises identifying tables to be queried from the database and at least one join operation to be performed on the tables, from the tree structure.
4 . The computer-implemented method of claim 1 , wherein the searching comprises identifying an operation for the SQL command including at least one of an aggregate function, a Boolean function, and a numeric calculation, and column identifiers for the operation, from the tree structure.
5 . The computer-implemented method of claim 1 , wherein the generating comprises executing a second machine learning model on the SQL command, the natural language input, and database schema data to generate a refactored SQL command, wherein the executing comprises executing the refactored SQL command on the database.
6 . The computer-implemented method of claim 1 , further comprising:
retraining the machine learning model.
7 . The computer-implemented method of claim 6 , wherein the machine learning model is retrained using at least the natural language input, the plurality of sub-queries and the SQL command.
8 . A computer system comprising:
a processor set;
a set of one or more computer-readable storage media; and
program instructions, collectively stored in the set of one or more storage media, that cause the processor set to perform computer operations comprising:
receiving a natural language input via a software application;
executing a machine learning model on the natural language input to determine a plurality of sub-queries corresponding to the natural language input;
searching a tree structure for field values that correspond to the plurality of sub-queries and relationships between the field values, wherein the searching comprises searching a plurality of layers of the tree structure and identifying a plurality of query attributes for a structured query language (SQL) command, respectively, and executing a Monte Carlo Tree Search (MCTS) on the tree structure starting from a root node of the tree structure to decide a path in the tree structure which includes a child node that contains a greatest upper confidence bound;
generating the SQL command based on the field values and relationships between the field values, wherein the generating comprises generating the SQL command to include the plurality of query attributes; and
executing the SQL command on a database to generate query results and return the query results to the software application.
9 . The computer system of claim 8 , wherein the executing the machine learning model comprises determining hidden query content from the natural language input based on execution of the machine learning model and generating a sub-query which includes the hidden query content.
10 . The computer system of claim 8 , wherein the searching comprises identifying tables to be queried from the database and at least one join operation to be performed on the tables, from the tree structure.
11 . The computer system of claim 8 , wherein the searching comprises identifying an operation for the SQL command including at least one of an aggregate function, a Boolean function, and a numeric calculation, and column identifiers for the operation, from the tree structure.
12 . The computer system of claim 8 , wherein the generating comprises executing a second machine learning model on the SQL command, the natural language input, and database schema data to generate a refactored SQL command, wherein the executing comprises executing the refactored SQL command on the database.
13 . The computer system of claim 8 , wherein the program instructions further cause the processor set to perform computer operations comprising:
retraining the machine learning model.
14 . The computer system of claim 13 , wherein the machine learning model is retrained using at least the natural language input, the plurality of sub-queries and the SQL command.
15 . A computer program product comprising:
a set of one or more computer-readable storage media; and
program instructions, collectively stored in the set of one or more computer-readable storage media, for causing a processor set to perform computer operations comprising:
receiving a natural language input via a software application;
executing a machine learning model on the natural language input to determine a plurality of sub-queries corresponding to the natural language input;
searching a tree structure for field values that correspond to the plurality of sub-queries and relationships between the field values, wherein the searching comprises searching a plurality of layers of the tree structure and identifying a plurality of query attributes for a structured query language (SQL) command, respectively, and executing a Monte Carlo Tree Search (MCTS) on the tree structure starting from a root node of the tree structure to decide a path in the tree structure which includes a child node that contains a greatest upper confidence bound;
generating the SQL command based on the field values and relationships between the field values, wherein the generating comprises generating the SQL command to include the plurality of query attributes; and
executing the SQL command on a database to generate query results and return the query results to the software application.
16 . The computer program product of claim 15 , wherein the executing the machine learning model comprises determining hidden query content from the natural language input based on execution of the machine learning model and generating a sub-query which includes the hidden query content.
17 . The computer program product of claim 15 , wherein the searching comprises identifying tables to be queried from the database and at least one join operation to be performed on the tables, from the tree structure.
18 . The computer program product of claim 15 , wherein the searching comprises identifying an operation for the SQL command including at least one of an aggregate function, a Boolean function, and a numeric calculation, and column identifiers for the operation, from the tree structure.
19 . The computer program product of claim 15 , wherein the program instructions are further causing the processor set to perform:
retraining the machine learning model.
20 . The computer program product of claim 19 , wherein the machine learning model is retrained using at least the natural language input, the plurality of sub-queries and the SQL command.