3

While trying to search for the answer - I've found out that the ANTLR seems to be the standard/reference implementation for the generic task of implementing the Query Language.

And it seems that everyone facing the task is doing same - learning what ANTLR is, how to configure it, how to use it and then building their own implementation of the QL.

Isn't there a free/open example or a reference implementation of a basic/common Query Language (like the one used in Hibernate or Cassandra, which are actually using ANTLR behind the scenes) or the "where" part of a regular SQL? I DO believe it's a very common task for many projects and it's a shame if everyone is re-implementing it from scratch (implying the ANTLR learning-curve).

For example here: Implementing free form query language in Java the accepted answer says:

The problem is that there is no such thing as a generic query language. Each one is different, and yours is no exception

I would disagree with the above statement, since SQL, Cassandra's QL and HibernateQL share more than 90% of similarities, so there is a common part between those - the one I'm looking for with a hope to not-re-implement-the-wheel...

Any level of comprehensiveness between following examples would be fine:

Level 1:

  1. "x=1"
  2. "x=2"
  3. "x=3"

Query/Filter: "x>1", expected result: 2 and 3

Level 2:

  1. "x=1 and y=1"
  2. "x=2 and y=2"
  3. "x=3 and y=3"

Query/Filter: "x>1 and y<3", expected result: 2

Level 3:

  1. "x=1 and y=1"
  2. "x=2 and y=2"
  3. "x=3 and y=3"

Query/Filter: "x>1 and (y<2 or y>3)", expected result: 2

...

Level N:

  1. "x=1 and y=1"
  2. "x=2 and y=2"
  3. "x=3 and y='3|4|5'"

Query/Filter: "(x>2 or x in ('z',1)) and (y in (1,2) or (y like '%3%' and y like '%5%'))" Expected result: 1 and 3

Question: is there an existing (ANTLR based or not) Java solution (extensible or not) which is usable (as a framework or as example to build/expand) as a Query/Filter engine for above-like examples?


P.S. did some "homework" here, but none seems to answer my particular topic... maybe it's just too much to expect in 2019 :)

Vlad
  • 1,157
  • 8
  • 15
  • 2
    I don't know what your question is exactly. Besides that, you mention Hibernate (which is an ORM), you mention Cassandra (which is a NoSQL database) and the examples you give are just simple expressions, not something that looks like SQL. Sure, I can make a (not so) educated guess as what your question is, but it's better if you explicitly ask it yourself. Do note that too open ended questions are not well suited for SO (see: https://stackoverflow.com/help/dont-ask). – Bart Kiers Apr 29 '19 at 21:06
  • If you think the grammar Hibernate uses to parse SQL is 90% similar with other SQL dialects, and is therefor is a common/basic SQL grammar, why not go ahead with that grammar? – Bart Kiers Apr 29 '19 at 21:18
  • @BartKiers question: is there a usable Java implementation of a "where" (Filtering) portion of "some-QL" available? I could take entire HQL + Hibernate + someInMemoryDbImpl to do something described in **Level 2** example - but it'd be an overkill. I could start learning ANTLR and dissecting org.hibernate.hql.antlr package for the "where" subset and after comprehending the code - write some stubs or DB-emulator for input data, but sounds as even-more-overkill. Gut feeling and google're tellin me I'm not the only one having the need for a simple solution for this, maybe there's no ready one... – Vlad Apr 30 '19 at 04:41
  • Questions asking for a software recommendations are off-topic here. This should be asked at [Software Recommendations](https://softwarerecs.stackexchange.com/) –  Apr 30 '19 at 07:33
  • You might want to have a look at [JXPath](http://commons.apache.org/proper/commons-jxpath/), [Gather](https://github.com/sangupta/gather) or [Collection Query Engine](https://github.com/npgall/cqengine) –  Apr 30 '19 at 07:34
  • Ah, it now just occurred to me you're also looking to evaluate the expressions. Then ANTLR isn't directly going to help you with that: ANTLR generates the parser, lexer and tree-visitor classes for you, but the evaluating is for you to do. I'd look into an existing expression evaluator then. – Bart Kiers Apr 30 '19 at 08:10
  • @a_horse_with_no_name - thank You, any way I (or You) could move the question to Software Recommendations? the *Gather* You've referenced seems to be the perfect option and the answer, so if You could answer with "try *Gather*" - I'll be happy to accept it to not-have the open-question dangling around. Or if moving/answering/etc is an overkill - I can erase the question completely. Thanks for the help! – Vlad May 08 '19 at 12:45

0 Answers0