Steven Talbot created CALCITE-5311: -------------------------------------- Summary: SqlNode.parserPosition missing closing ')' for expression subquery Key: CALCITE-5311 URL: https://issues.apache.org/jira/browse/CALCITE-5311 Project: Calcite Issue Type: Bug Reporter: Steven Talbot
Here's an obscure one! Background: I'm trying to use the values of parserPos to recover the original slices of SQL corresponding to various SQL entities. When we parse a statement like {code:java} SELECT a IN (SELECT a FROM c WHERE d) FROM t{code} The parsed SqlNode corresponding to "a IN (SELECT a FROM c WHERE d)" stops at the index of "d", not the ")". I am almost sure this is due to this very original special case code in the parser: [https://github.com/apache/calcite/blame/a505b25eacc473c6ec0ef8abd40c1ccae86297b6/core/src/main/codegen/templates/Parser.jj#L3602] The parser drops the "list" of the single parenthesized query in favor of just the query, which completely makes sense, but in doing so it discards the parserPos of the list, which includes the index of ")", for the parserPos of the query, which does not. Then, when the parserPositions of the arguments to the IN are added together, the final parserPosition does not include the ')'. Test is perhaps easier to see: {code:java} @Test void testSubqueryParserPosParse() throws SqlParseException { String sql = "SELECT a IN (SELECT a FROM c WHERE d) FROM t"; SqlNode parsed = SqlParser.create(sql).parseQuery(); SqlCall inCall = (SqlCall) ((SqlSelect) parsed).getSelectList().get(0); assertEquals("a IN (SELECT a FROM c WHERE d)", sql.substring( inCall.getParserPosition().getColumnNum() - 1, // pos is 1-indexed inCall.getParserPosition().getEndColumnNum() // but inclusive )); } {code} {noformat} org.opentest4j.AssertionFailedError: Expected :a IN (SELECT a FROM c WHERE d) Actual :a IN (SELECT a FROM c WHERE d {noformat} -- This message was sent by Atlassian Jira (v8.20.10#820010)