taozhi8833998 / node-sql-parser

Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList and convert it back to SQL
https://taozhi8833998.github.io/node-sql-parser/
Apache License 2.0
818 stars 181 forks source link

Indexing into result of function fails to parse #2189

Closed dannflor closed 2 weeks ago

dannflor commented 3 weeks ago

Describe the bug Attempting to index into the result of a function like APPROX_QUANTILES fails to parse.

Database Engine BigQuery

To Reproduce On any version of node-sql-parser try to parse:

SELECT APPROX_QUANTILES(field, 100)[OFFSET(50)] AS p50 FROM mytable

you will get Error parsing query at line 1 offset 36: Expected \"'\", \"+\", \"-\", \"--\", \".\", \"/*\", \"\\\"\", [ \\t\\n\\r], or [0-9] but \"O\" found.

Expected behavior The ability to use OFFSET and ORDINAL within index brackets.

taozhi8833998 commented 2 weeks ago

@dannflor I can't reproduce it. It works when the database option is bigquery.

const { Parser } = require('node-sql-parser');
const parser = new Parser();
const sql = "SELECT APPROX_QUANTILES(field, 100)[OFFSET(50)] AS p50 FROM mytable";
const ast = parser.astify(problemSql, { database: 'bigquery' });
console.log(ast);
dannflor commented 2 weeks ago

You're right, I lost the database type when I was reducing to a reproduction. I believe was still running into an issue while I had the type set correctly, I'll get back to you with a better reproduction

dannflor commented 2 weeks ago

Ok, I wasn't able to reproduce it any more. I'll create a new issue if I find it again.

taozhi8833998 commented 2 weeks ago

Ok, I wasn't able to reproduce it any more. I'll create a new issue if I find it again.

ok~