Skip to main content

A CLI tool to transpile SQL to MongoDB Query Language

Project description

SQL to MongoDB Transpiler


Table of Contents


Project Overview

This project aims to build a SQL-to-MongoDB transpiler that converts basic SQL queries into their equivalent MongoDB queries. The current implementation focuses on the lexical analysis (tokenizer) phase using the PLY (Python Lex-Yacc) library.


Folder Structure

sql-to-mongodb-transpiler/
├── ast/
│ └── ast_nodes.py # AST node definitions (for future use)
├── lexer/
│ ├── init.py
│ ├── lexer.py # PLY lexer implementation
│ └── tokens.py # Token definitions & reserved keywords
├── parser/
│ └── parser.py # Placeholder for parser (future)
├── tests/
│ └── test_lexer.py # Unit test for lexer
├── pycache/
├── .gitignore
├── README.md
└── requirements.txt

Intended SQL Subset

The transpiler is designed to support the following subset of SQL:

  • SELECT
  • FROM
  • WHERE
  • Comparisons (=, !=, <, >, <=, >=)
  • Logical operators: AND, OR, NOT
  • ORDER BY
  • LIMIT
  • Aggregation functions (e.g., COUNT, SUM, AVG, MIN, MAX)

Architecture flow diagram:

+--------------------+
|    SQL Query       |
| (User Input)       |
+--------------------+
          |
          v
+--------------------+
|   Lexical Analyzer |
|   (PLY Lexer)      |
|                    |
| - Keywords         |
| - Identifiers      |
| - Literals         |
| - Operators        |
+--------------------+
          |
          v
+--------------------+
|   Token Stream     |
| (Type, Value,      |
|  Line, Position)   |
+--------------------+
          |
          v
+----------------------------+
|   Syntax Analyzer (Parser) |
|   - SQL Grammar Rules      |
|   - Parse Tree / AST       |
+----------------------------+
          |
          v
+----------------------------+
| Intermediate Code          |
| Generation                 |
| - Logical Query Structure  |
| - Query Representation    |
+----------------------------+
          |
          v
+----------------------------+
| MongoDB Query Translation  |
| - find() / projection      |
| - filter conditions        |
+----------------------------+
          |
          v
+----------------------------+
| MongoDB Query Output       |
+----------------------------+

Technologies Used

  • Python
  • PLY (Python Lex-Yacc)
  • sqlparse (for testing and comparison)

Work Done So Far

  • Implemented a lexer using PLY to tokenize SQL queries.
  • Supported SQL keywords: SELECT, FROM, WHERE.
  • Identified tokens for:
    • Identifiers (table names, column names)
    • Numeric literals
    • String literals
    • Relational operators (=, <, >, <=, >=, !=)
    • Comma and semicolon
  • Implemented case-insensitive keyword recognition.
  • Added line number tracking and error handling for illegal characters.
  • Developed a testing script - test_lexer.py to compare PLY-generated tokens with tokens produced by the sqlparse library.

How to Run

Before cloning create venv environment and enter in it.as,

python3 -m venv venv
source venv/bin/activate
  1. Clone the repository:
git clone "https://github.com/MANDADI-PRANATHI/SQL_to_MongoDB_Transpiler.git"

cd SQL-to-MongoDB-Transpiler
pip install -r requirements.txt
pip install ply

To test the lexer implementated until now,in the sql-to-mongodb-transpiler folder

python3 main.py
cd SQL_to_MongoDB_Transpiler-main
  1. Install the CLI Tool globally via pip:
pip install .
  1. Run the tool using the mapped console command:
sql2mongo --schema custom_schema.json --query "SELECT * FROM users;" --pretty

You can also use the interactive shell:

sql2mongo shell --schema custom_schema.json

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sql2mongo-0.1.0.tar.gz (22.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sql2mongo-0.1.0-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file sql2mongo-0.1.0.tar.gz.

File metadata

  • Download URL: sql2mongo-0.1.0.tar.gz
  • Upload date:
  • Size: 22.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for sql2mongo-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dc2f7df79470ed4617934c480c058b6e53aa20c4fc4959c643eccf68425816f6
MD5 802db6bbb644f50ba853c1612dfb2cba
BLAKE2b-256 ba02e73b60309b6b502e5b39faec2333b7aae2efdae307e83bc73b7746b17f53

See more details on using hashes here.

File details

Details for the file sql2mongo-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sql2mongo-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.6

File hashes

Hashes for sql2mongo-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5567c4b5771d5be19c3abfab37d5bba024040f12da8e49a7d8f75924253c8fd3
MD5 b70fcdde2a10cdbe888fcf796e638cdd
BLAKE2b-256 4be27ef8298e2a7c2e3fe19b1b30c881f0b4e26933bb88fbd47988cc8fb09c6d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page