Curious to hear what you think!
Introduction I wanted to demonstrate some of my ideas for a SQL transplilation (conversion between Postgres and T-SQL dialects). I chose an AI from one of the most reputable vendors: gemini.google.com. I limited the goals of my project to: Implement a parser that understands a couple of SQL dialects (Postgres and T-SQL). Implement a builder that converts AST into an internal representation of a SQL statement. Implement the output statement generator in the desired dialect.
For example, the Transpiler should be able to convert a T-SQL statement such as SELECT TOP 10 max([dbo].[col1]) FROM [dbo].[tbl] into an equivalent Postgres statement SELECT max("dbo"."col1") FROM "dbo"."tbl" LIMIT 10 and vice-versa.
As an additional benefit of this exercise, I wanted to learn about a modern parsing system. After learning about lex/yacc at college many years ago, I only had some experience with the Gold parser back in the beginning of the 2000s. And I also knew from my previous attempt to learn Antlr4 - it has unpredictable parsing time and numerous other problems, including “reduce-reduce” conflicts that are very hard to resolve. I googled around and found a modern parsing tool called PEG, along with its C++ variant, cpp-peglib, on GitHub.
Source code for this project is available here: https://github.com/phoenicyan/sql_transpiler.
Conclusion Gemini was making numerous assumptions about my intentions and generated something entirely different from what I wanted. But I definitely improved my skills of telling Gemini what I wanted by asking it to do very primitive assignments. In a few cases, the AI saved me a bit of time, e.g., “write procedure to split text in vector of lines” or “write procedure to iterate all files in specified path and read the files as text.” The AI helped teach me about the PEG parser, the Visitor pattern, etc. The idea behind the SQL Transpiler was to start with a very basic parser that extracts identifiers, numbers, and string literals, and then add various rules, such as LIMIT/TOP. This idea could be fruitful, and I would appreciate your feedback (my contact is phoenicyan at gmail dot com). I want to learn alternative ideas for a transpiler, especially from people who have had previous experience in the creation of transpilers. In Part 2, I’m excited to share with you a fully functional transpiler that brings this idea to life.