nene / sql-parser-cst

Parses SQL into Concrete Syntax Tree (CST)
GNU General Public License v2.0
134 stars 8 forks source link

Fails parsing BigQuery EXTRACT() format with time zone #74

Open hrsma2i opened 8 months ago

hrsma2i commented 8 months ago

Thank you for this useful library!

This issue is similar to https://github.com/nene/sql-parser-cst/issues/21 .

When I parse the following SQL (a.sql) :

SELECT EXTRACT(DAYOFWEEK FROM CURRENT_TIMESTAMP() AT TIME ZONE 'Asia/Tokyo') AS as_timestamp

I caught the error:

> parse-sql@1.0.0 sql_parser_cst
> ts-node sql_parser_cst.ts

/Users/user.name/parse-sql/node_modules/sql-parser-cst/lib/main.js:40
            throw new FormattedSyntaxError_2.FormattedSyntaxError(e, sql, options.filename);
                  ^
FormattedSyntaxError: Syntax Error: Unexpected "AT"
Was expecting to see: "!=", "%", "&", "(", ")", "*", "+", "-", "->", "->>", ".", "/", "<", "<<", "<=", "<=>", "<>", "=", "==", ">", ">=", ">>", "AND", "BETWEEN", "DIV", "FILTER", "GLOB", "IN", "IS", "ISNULL", "LIKE", "MATCH", "MOD", "NOT", "NOTNULL", "OR", "OVER", "REGEXP", "RLIKE", "[", "^", "|", "||", or whitespace
--> undefined:1:51
  |
1 | SELECT EXTRACT(DAYOFWEEK FROM CURRENT_TIMESTAMP() AT TIME ZONE 'Asia/Tokyo') AS as_timestamp
  |                                                   ^
    at parse (/Users/user.name/parse-sql/node_modules/sql-parser-cst/lib/main.js:40:19)
    at Object.<anonymous> (/Users/user.name/parse-sql/sql_parser_cst.ts:12:18)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module.m._compile (/Users/user.name/parse-sql/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/user.name/parse-sql/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Function.Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at phase4 (/Users/user.name/parse-sql/node_modules/ts-node/src/bin.ts:649:14)

↓ sql_parser_cst.ts

import fs from 'fs'
import path from 'path'
import { parse } from 'sql-parser-cst'

const sql_path = path.join('a.sql')
const sql = fs.readFileSync(sql_path, 'utf8')

const cst = parse(sql, {
    dialect: "bigquery",
    // These are optional:
    includeSpaces: true, // Adds spaces/tabs
    includeNewlines: true, // Adds newlines
    includeComments: true, // Adds comments
    includeRange: true, // Adds source code location data
})

console.log(cst)
nene commented 8 months ago

Thanks for reporting. Will look into it when I get a chance. Been a bit busy lately.