Using the Mozilla SQL Parser — Part 1
In this part, see how the author used the Mozilla SQL Parser to perform some analyses using multiple articles.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
During a couple of project implementations, I have come across a couple of internal solutions that claim to convert one SQL dialect to another. For example, converting Oracle SQL to Hive SQL (HQL) or converting SQL server SQL to Snowflake. As per my observations of their working, most of them are glorified find and replace operations. I agree that such solutions manage to address a lot of conversions, claiming them to be enterprise grade solutions has to be taken with a pinch of salt.
I believe that the better approach is to - at the minimum - use an SQL parser to do the tedious task of SQL statement parsing and getting it converted into some data structure. Once we get the data structure, we will be in a position to navigate it as per our requirement.
Recently, I was part of a project that has a similar requirement - convert one SQL dialect to another dialect. This dialect change is needed as the project involves data platform change. Though this task was with another team, I took it as an opportunity to explore options. Initially, I looked at ANTLR4, which is quite well-known. ANTLR4 is a parser generator that needs to be provided a grammar of the language. It then generates the parser, which in turn can be used to read the input and create an internal data structure called AST (abstract syntax tree), which can be traversed and manipulated as per our requirement.
As I did not have sufficient time to delve into the details of ANTLR4, I continued looking for alternatives that were SQL specific. While I found a couple of options, I would like to mention three libraries I came across and used for initial exploration. The three libraries are JSQLParser, Presto Parser and Mozilla SQL Parser. While the first two libraries are Java based, I was surprised to find a Python package for parsing SQL Select statements developed by Mozilla. Yes the same organization that maintains the Firefox browser and The Thunderbird email client - both of which I use on a regular basis.
Mozilla SQL Parser
In this part, I will cover how I used the Mozilla SQL Parser to perform some analysis using multiple articles. I will cover Presto Parser in a separate article.
The Mozilla SQL Parser is available as a Python package. Before using it, we need to install it aspip install moz-sql-parser
Now that the parser is installed, let us write a simple application to parse an SQL select statement. The code is as follows
xxxxxxxxxx
from moz_sql_parser import parse
import json
def parse_query_and_display(query):
print("----- query -----")
print("")
print("parsing - {}".format(query))
parsedQuery = parse(query)
jsonOutput = json.dumps(parsedQuery, indent=2)
print("")
print("output json - {}".format(jsonOutput))
print("")
if __name__ == "__main__":
query = "SELECT s_name from student where s_id in \
(select s_id from student_course where c_id in \
(select c_id from course where c_name = 'DSA' \
and c_name = 'dbms' or c_name = 'algorithm'));"
parse_query_and_display(query)
The Mozilla SQL Parser parses the SQL statement and converts it into a JSONified structure, that we are displaying using the parse_query_and_display method. On execution, the output is as shown below
xxxxxxxxxx
{
"select": { "value": "s_name" },
"from": "student",
"where": {
"in": [ "s_id", {
"select": { "value": "s_id" },
"from": "student_course",
"where": {
"in": [ "c_id", {
"select": { "value": "c_id" },
"from": "course",
"where": {
"or": [ {
"and": [
{ "eq": [ "c_name", { "literal": "DSA" } ] },
{ "eq": [ "c_name", { "literal": "dbms" } ] }
]
},
{ "eq": [ "c_name", { "literal": "algorithm" } ] }
]
}
}
]
}
}
]
}
}
In the second part, I will cover how I navigated the structure to identify tables in the "from" clause of the SQL statement.
Opinions expressed by DZone contributors are their own.
Comments