JSqlParser icon indicating copy to clipboard operation
JSqlParser copied to clipboard

net.sf.jsqlparser.parser.ParseException

Open xtdw12345 opened this issue 9 years ago • 7 comments

sql: select count(id) from p_custom_data ignore index(pri) where tenant_id=28257 and entity_id=92609 and delete_flg=0 " + "and ( (dbc_relation_2 = 52701) and (dbc_relation_2 in ( select id from a_order where tenant_id = 28257 and 1=1 ) ) ) order by id desc, id desc

why can't parse the sql.

Caused by: net.sf.jsqlparser.parser.ParseException: Encountered " <S_CHAR_LITERAL> "'index' "" at line 1, column 44. Was expecting one of: "JOIN" ... "LEFT" ... "FULL" ... "WHERE" ... "GROUP" ... "INNER" ... "LIMIT" ... "OUTER" ... "ORDER" ... "RIGHT" ... "OFFSET" ... "HAVING" ... "NATURAL" ... ";" ... "," ... "LEFT" ... "RIGHT" ... "FULL" ... "NATURAL" ... "OUTER" ... "INNER" ... "JOIN" ... "," ... "WHERE" ... "GROUP" ... "HAVING" ... "ORDER" ... "LIMIT" ... "OFFSET" ... "LIMIT" ...

at net.sf.jsqlparser.parser.CCJSqlParser.generateParseException(CCJSqlParser.java:5530)
at net.sf.jsqlparser.parser.CCJSqlParser.jj_consume_token(CCJSqlParser.java:5406)
at net.sf.jsqlparser.parser.CCJSqlParser.Statement(CCJSqlParser.java:149)
at net.sf.jsqlparser.parser.CCJSqlParserManager.parse(CCJSqlParserManager.java:38)
... 25 more

xtdw12345 avatar May 03 '17 06:05 xtdw12345

who can answer me?

xtdw12345 avatar May 03 '17 06:05 xtdw12345

We are still on the start to provide optimizer hints. The problem here seems to be at the first glance: ignore index(pri).

wumpz avatar May 03 '17 11:05 wumpz

this SQL statement can be executed directly by Mysql, why jsqlparse can not parse it?

xtdw12345 avatar May 05 '17 02:05 xtdw12345

because JSqlParser supports a subset of all different SQL grammars. Niche cases, like yours, have only been added slowly by other people via PRs.

It seems from the grammar that ignore index(blah) should be supported:

{
	(actionToken = <K_USE>
	| actionToken = <K_IGNORE>
	| actionToken = <K_FORCE> )
	(indexToken = <K_INDEX>
	| indexToken = <K_KEY>)
	"("
	indexName = Identifier() { indexNameList.add(indexName); }
	("," indexName= Identifier() { indexNameList.add(indexName); })*
	")"
	{
		return new MySQLIndexHint(actionToken.image, indexToken.image, indexNameList);
	}
}

And the Plain select ends up calling FromItem, which has the code for trying to find the hint

I think your best bet would be to try to use the Visualize Parsing section to see what token didn't get consumed properly.

AnEmortalKid avatar May 05 '17 04:05 AnEmortalKid

@AnEmortalKid Thx for pointing that out. I will look into it.

wumpz avatar May 05 '17 12:05 wumpz

Sorry for being a little late. Which version do you use? Using JSqlParser V1.1 I was able to parse your statement successfully.

wumpz avatar May 12 '17 19:05 wumpz

Could be an older version, looks like this was fixed recently: https://github.com/JSQLParser/JSqlParser/pull/429

AnEmortalKid avatar May 12 '17 19:05 AnEmortalKid