npgall / cqengine

Ultra-fast SQL-like queries on Java collections
Apache License 2.0
1.72k stars 252 forks source link

some filed like `action`, `desc` in where clause parse error #250

Closed sdvdxl closed 4 years ago

sdvdxl commented 5 years ago

select * from a where desc=1 or select * from a where action=1 parse error.

test

@Test
  public void testCQEngine() {
    SQLParser<Map> parser = SQLParser.forPojo(Map.class);
    parser.registerAttribute(QueryFactory.mapAttribute("action", Integer.class));
    parser.parse("select * from a where action=1");
  }

error message:

com.googlecode.cqengine.query.parser.common.InvalidQueryException: Failed to parse query at line 1:22: mismatched input 'action' expecting {'(', K_NOT, IDENTIFIER, STRING_LITERAL}

    at com.googlecode.cqengine.query.parser.common.QueryParser$1.syntaxError(QueryParser.java:54)
    at org.antlr.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:41)
    at org.antlr.v4.runtime.Parser.notifyErrorListeners(Parser.java:544)
    at org.antlr.v4.runtime.DefaultErrorStrategy.reportInputMismatch(DefaultErrorStrategy.java:327)
    at org.antlr.v4.runtime.DefaultErrorStrategy.reportError(DefaultErrorStrategy.java:139)
    at com.googlecode.cqengine.query.parser.sql.grammar.SQLGrammarParser.query(SQLGrammarParser.java:474)
    at com.googlecode.cqengine.query.parser.sql.grammar.SQLGrammarParser.whereClause(SQLGrammarParser.java:349)
    at com.googlecode.cqengine.query.parser.sql.grammar.SQLGrammarParser.start(SQLGrammarParser.java:245)
    at com.googlecode.cqengine.query.parser.sql.SQLParser.parse(SQLParser.java:62)
npgall commented 5 years ago

You need to enclose in parentheses: (action=1)

Let me know if you still have trouble though.

sdvdxl commented 5 years ago
select * from a where (action=1)

still error:

but

select * from a where (a=1)

is ok.

error:

com.googlecode.cqengine.query.parser.common.InvalidQueryException: Failed to parse query at line 1:23: no viable alternative at input '(action'

    at com.googlecode.cqengine.query.parser.common.QueryParser$1.syntaxError(QueryParser.java:54)
    at org.antlr.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:41)
    at org.antlr.v4.runtime.Parser.notifyErrorListeners(Parser.java:544)
    at org.antlr.v4.runtime.DefaultErrorStrategy.reportNoViableAlternative(DefaultErrorStrategy.java:310)
    at org.antlr.v4.runtime.DefaultErrorStrategy.reportError(DefaultErrorStrategy.java:136)
    at com.googlecode.cqengine.query.parser.sql.grammar.SQLGrammarParser.query(SQLGrammarParser.java:474)
    at com.googlecode.cqengine.query.parser.sql.grammar.SQLGrammarParser.whereClause(SQLGrammarParser.java:349)
    at com.googlecode.cqengine.query.parser.sql.grammar.SQLGrammarParser.start(SQLGrammarParser.java:245)
    at com.googlecode.cqengine.query.parser.sql.SQLParser.parse(SQLParser.java:62)
xiexw commented 4 years ago

Is this question solved?I meet a similar one: select * from ic where key = 1 error message: com.googlecode.cqengine.query.parser.common.InvalidQueryException: Failed to parse query at line 1:23: mismatched input 'key' expecting {'(', K_NOT, IDENTIFIER, STRING_LITERAL} at com.googlecode.cqengine.query.parser.common.QueryParser$1.syntaxError(QueryParser.java:54) at org.antlr.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:41) at org.antlr.v4.runtime.Parser.notifyErrorListeners(Parser.java:544) at org.antlr.v4.runtime.DefaultErrorStrategy.reportInputMismatch(DefaultErrorStrategy.java:327) at org.antlr.v4.runtime.DefaultErrorStrategy.reportError(DefaultErrorStrategy.java:139) at com.googlecode.cqengine.query.parser.sql.grammar.SQLGrammarParser.query(SQLGrammarParser.java:474) at com.googlecode.cqengine.query.parser.sql.grammar.SQLGrammarParser.whereClause(SQLGrammarParser.java:349) at com.googlecode.cqengine.query.parser.sql.grammar.SQLGrammarParser.start(SQLGrammarParser.java:245) at com.googlecode.cqengine.query.parser.sql.SQLParser.parse(SQLParser.java:62) at com.googlecode.cqengine.query.parser.common.QueryParser.retrieve(QueryParser.java:150) change to: select * from ic where keya = 1 it's ok and returns me the result

npgall commented 4 years ago

Could you try putting "action" in quotes?

"action" doesn't have any special meaning in CQEngine's interpretation of SQL, however CQEngine is using the Antlr grammar from SQLite to parse SQL. It looks like "action" is in fact some kind of keyword in SQLite grammar.

List of SQLite keywords: https://www.sqlite.org/lang_keywords.html

You can see the keyword in the SQLite grammar file here, used by CQEngine: https://github.com/npgall/cqengine/blob/master/code/src/main/antlr4/imports/SQLite.g4#L712

So I'd say that's what the problem is. If you put "action" in quotes it should work around the problem.

The same goes for "key" I think.

npgall commented 4 years ago

For some background: CQEngine uses the SQLite grammar to parse SQL statements, because writing a grammar to parse SQL from scratch is a huge undertaking (as you can see from the size of the SQLite.g4 file).

I recommend to put the keyword in quotes instead. I don't plan to change this behavior in the short term, but let me know if you'd like to volunteer to help with that :)

I will close this now, but please feel free to reopen if you need more help.