Skip to content

JSqlParser fails to parse MERGE statement #401

@arnabmitra

Description

@arnabmitra

JSqlParser fails to parse sql MERGE statements
"MERGE INTO a USING dual ON (col3 = ? AND col1 = ? AND col2 = ?) WHEN NOT MATCHED THEN INSERT (col1, col2, col3, col4) VALUES (?, ?, ?, ?) WHEN MATCHED THEN UPDATE SET col4 = col4 + ?"


Unit test to reproduce the below error

  @Test
  public void testFailedParse() throws JSQLParserException {
    String sql = "MERGE INTO a USING dual ON (col3 = ? AND col1 = ? AND col2 = ?) WHEN NOT MATCHED THEN INSERT (col1, col2, col3, col4) VALUES (?, ?, ?, ?) WHEN MATCHED THEN UPDATE SET col4 = col4 + ?";
    List<String> expected = Lists.newArrayList("a");

    Statement info = CCJSqlParserUtil.parse(sql);
    TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
    List<String> tableList = tablesNamesFinder.getTableList(info);

    assertEquals(expected, tableList);
  }

Error:

Caused by: net.sf.jsqlparser.parser.ParseException: Encountered " "WHEN" "WHEN "" at line 1, column 139.
Was expecting one of:
    <EOF> 
    ";" ...
    
	at net.sf.jsqlparser.parser.CCJSqlParser.generateParseException(CCJSqlParser.java:15112)
	at net.sf.jsqlparser.parser.CCJSqlParser.jj_consume_token(CCJSqlParser.java:14968)
	at net.sf.jsqlparser.parser.CCJSqlParser.Statement(CCJSqlParser.java:90)
	at net.sf.jsqlparser.parser.CCJSqlParserUtil.parse(CCJSqlParserUtil.java:52)
	... 34 more
Caused by:
net.sf.jsqlparser.parser.ParseException: Encountered " "WHEN" "WHEN "" at line 1, column 139.
Was expecting one of:
    <EOF> 
    ";" ...
    
	at net.sf.jsqlparser.parser.CCJSqlParser.generateParseException(CCJSqlParser.java:15112)
	at net.sf.jsqlparser.parser.CCJSqlParser.jj_consume_token(CCJSqlParser.java:14968)
	at net.sf.jsqlparser.parser.CCJSqlParser.Statement(CCJSqlParser.java:90)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions