Custom query DSL for NoSQL

Posted on Updated on


Need to support the custom query DSL for Domain objects, so that user can create various queries in SQL like format for NoSQL database.

e.g. Select * from User where firstName = “vinayak” and age > 18 order by id limit 10.

The result will be the same irrespective of underlying storage either Elastic Search, MongoDB or MySql.


The solution to this problem is to create custom query language by providing SQL like syntax or grammar. The  query defined using this grammar will be translated to the provided data source query language like elastic search query dsl. The tool evaluated for this purpose is ANTLR which is widely used for designing the custom domain specific languages. I highly appreciate the work done by Terence Parr in this area.

The language translation for target data source using ANTLR works in three different Steps.

Step I:

ANTLR provides java based utility which parse the grammar and generate the code as per the target run time like c#, java, javascript etc… The grammar format is supported in Backus Normal Form

Step II:

The code generated from Step I as per target run time will be used to parse the custom query designed using the respective grammar. The output of Step II is parse tree or Abstract Syntax Tree.

Step III:

To translate the parse tree into desired output or targeted data source query ANTLR also provided the parse tree traversal using Depth First Search. The Step I by default generate the  listener interface and empty base implementation for this interface. The Step I also optionally generate the visitor for the parse tree. The listener methods will be called during parse tree traversal, in this callback methods one can perform respective actions as per the need.

This tool will help you to design your own custom language and its translation as per your need in very simple way. For more information one can explore the documentation and The Definitive ANTLR 4 Reference book.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s